Merge remote-tracking branch 'origin/routing-manager'
[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         OC_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         OC_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             {OC_LOG_V(ERROR, TAG, "%s failed!!", #op); goto exit;} }
86
87 /**
88  * This structure is used to hold the hopcount, source and destination address.
89  */
90 typedef struct
91 {
92     uint32_t srcGw;               /**< Source gateway for this packet. */
93     uint32_t destGw;              /**< Destination gateway for this packet. */
94     uint16_t mSeqNum;             /**< HopCount. */
95     uint16_t srcEp;               /**< Source endpoint for this packet. */
96     uint16_t destEp;              /**< Destination endpoint for this packet. */
97 } RMRouteOption_t;
98
99 /**
100  * Adds the destination address to the Route options.
101  * If Route option is already present, it adds the destination address information to
102  * Route option else creates a new Route option with the destination address info.
103  * @param[in]       endpoint        Destination address.
104  * @param[in,out]   options         Header options present in the Request/response message.
105  * @param[in,out]   numOptions      Number of options present in the message.
106  * @return  ::CA_STATUS_OK or Appropriate error code.
107  */
108 OCStackResult RMAddInfo(const char *destination, CAHeaderOption_t **options,
109                         uint8_t *numOptions);
110
111 /**
112  * Removes the Route Option from the header options.
113  * @param[in,out]   options     Header options present in request/response message.
114  * @param[in,out]   numOptions  Number of options present in request/response message.
115  * @param[in,out]   endpoint    Remote address updated with the actual source of request/response.
116  * @return  ::CA_STATUS_OK or Appropriate error code.
117  */
118 OCStackResult RMUpdateInfo(CAHeaderOption_t **options, uint8_t *numOptions,
119                            CAEndpoint_t *endpoint);
120
121 /**
122  * Gets the index of the routing option if present.
123  * @param[in]    options     Header options present in request/response message.
124  * @param[in]    numOptions  Number of options present in request/response message.
125  * @param[out]   index       Index of the route option present in Header options.
126  * @return  NONE.
127  */
128 void RMGetRouteOptionIndex(const CAHeaderOption_t *options, uint8_t numOptions,
129                            int8_t *index);
130
131 /**
132  * To create a Routing option from the CARouteOption_t structure.
133  * @param[in]    optValue    Routing information.
134  * @param[out]   options     Routing information in the form of Header options.
135  * @return  ::CA_STATUS_OK or Appropriate error code.
136  */
137 OCStackResult RMCreateRouteOption(const RMRouteOption_t *optValue, CAHeaderOption_t *options);
138
139 /**
140  * To parse the routing option from the Headeroptions.
141  * @param[in]    options    Routing information in the form of Header options.
142  * @param[out]   optValue   Route information after parsing.
143  * @return  ::CA_STATUS_OK or Appropriate error code.
144  */
145 OCStackResult RMParseRouteOption(const CAHeaderOption_t *options, RMRouteOption_t *optValue);
146
147 #ifdef __cplusplus
148 } /* extern "C" */
149 #endif
150
151 #endif /* ROUTING_MANAGER_H_ */