Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / resource / csdk / routing / include / routingutility.h
1 /* ****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 /**
22  * @file
23  * This file contains the utility functions used by Routing manager and RI
24  */
25
26 #ifndef ROUTING_UTILITY_H_
27 #define ROUTING_UTILITY_H_
28
29 //TODO Endpoint will also include this file, remove unnecessary includes.
30
31 #include "cacommon.h"
32 #include "octypes.h"
33 #ifdef ROUTING_GATEWAY
34 #include "routingmanager.h"
35 #endif
36
37 #ifdef __cplusplus
38 extern "C"
39 {
40 #endif
41
42 /**
43  * Maximum source or destination address length added to Route Option.
44  */
45 #define MAX_ADDR_LEN 40
46
47 /**
48  * Gateway ID length.
49  */
50 #define GATEWAY_ID_LENGTH sizeof(uint32_t)
51
52 /**
53  * Endpoint ID length.
54  */
55 #define ENDPOINT_ID_LENGTH sizeof(uint16_t)
56
57 /**
58  * Routing option number.
59  */
60 // TODO: We need to define proper Option number.
61 #define RM_OPTION_MESSAGE_SWITCHING 65524
62
63 /**
64  * Macro to verify the validity of input argument with the return.
65  */
66 #define RM_NULL_CHECK_WITH_RET(arg, log_tag, log_message) \
67     if (NULL == arg ){ \
68         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
69         return OC_STACK_INVALID_PARAM; \
70     } \
71
72 /**
73  * Macro to verify the validity of input argument.
74  */
75 #define RM_NULL_CHECK_VOID(arg, log_tag, log_message) \
76     if (NULL == arg ){ \
77         OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
78         return; \
79     } \
80
81 /**
82  * Macro to verify the return of an API.
83  */
84 #define RM_VERIFY_SUCCESS(op, successCode) { if (op != successCode) \
85             {OIC_LOG_V(ERROR, TAG, "%s failed!!", #op); goto exit;} }
86
87 /**
88  * Message types in RouteOption to differentiate a normal response and Empty response.
89  */
90 typedef enum
91 {
92    NOR = 0,  /**< Normal Message. */
93    ACK,      /**< Empty Acknowledgement message. */
94    RST      /**< Empty Reset message. */
95 }MSGType;
96
97 /**
98  * This structure is used to hold the source address, destination address, message type and
99  * sequence number. This collectively forms the value of Route option in the message.
100  */
101 typedef struct
102 {
103     uint32_t srcGw;               /**< Source gateway for this packet. */
104     uint32_t destGw;              /**< Destination gateway for this packet. */
105     uint16_t mSeqNum;             /**< Multicast sequence Number. */
106     uint16_t srcEp;               /**< Source endpoint for this packet. */
107     uint16_t destEp;              /**< Destination endpoint for this packet. */
108     uint8_t msgType;              /**< Type of Message: Empty or normal. */
109 } RMRouteOption_t;
110
111 /**
112  * To set the stack mode in Routing manager.
113  */
114 void RMSetStackMode(OCMode mode);
115
116 /**
117  * Adds the destination address to the Route options.
118  * If Route option is already present, it adds the destination address information to
119  * Route option else creates a new Route option with the destination address info.
120  * @param[in]       endpoint        Destination address.
121  * @param[in,out]   message         Request/response message to add the route option
122  * @param[in]       isRequest       True if message is request else false.
123  * @param[out]      doPost          True if a POST message be sent for empty packet to
124  *                                  Routing gateway.
125  * @return  ::CA_STATUS_OK or Appropriate error code.
126  */
127 OCStackResult RMAddInfo(const char *destination, void *message, bool isRequest,
128                         bool *doPost);
129
130 /**
131  * Removes the Route Option from the header options.
132  * @param[in,out]   options     Header options present in request/response message.
133  * @param[in,out]   numOptions  Number of options present in request/response message.
134  * @param[in,out]   endpoint    Remote address updated with the actual source of request/response.
135  * @return  ::CA_STATUS_OK or Appropriate error code.
136  */
137 OCStackResult RMUpdateInfo(CAHeaderOption_t **options, uint8_t *numOptions,
138                            CAEndpoint_t *endpoint);
139
140 /**
141  * Gets the index of the routing option if present.
142  * @param[in]    options     Header options present in request/response message.
143  * @param[in]    numOptions  Number of options present in request/response message.
144  * @param[out]   index       Index of the route option present in Header options.
145  * @return  NONE.
146  */
147 void RMGetRouteOptionIndex(const CAHeaderOption_t *options, uint8_t numOptions,
148                            int8_t *index);
149
150 /**
151  * To create a Routing option from the CARouteOption_t structure.
152  * @param[in]    optValue    Routing information.
153  * @param[out]   options     Routing information in the form of Header options.
154  * @return  ::CA_STATUS_OK or Appropriate error code.
155  */
156 OCStackResult RMCreateRouteOption(const RMRouteOption_t *optValue, CAHeaderOption_t *options);
157
158 /**
159  * To parse the routing option from the Headeroptions.
160  * @param[in]    options    Routing information in the form of Header options.
161  * @param[out]   optValue   Route information after parsing.
162  * @return  ::CA_STATUS_OK or Appropriate error code.
163  */
164 OCStackResult RMParseRouteOption(const CAHeaderOption_t *options, RMRouteOption_t *optValue);
165
166 #ifdef __cplusplus
167 } /* extern "C" */
168 #endif
169
170 #endif /* ROUTING_MANAGER_H_ */