Merge branch 'tizen' into tizen_5.5
[platform/upstream/iotivity.git] / resource / csdk / routing / include / routingtablemanager.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 APIs for routing table manager.
24  */
25 #ifndef ROUTING_TABLE_MANAGER_H_
26 #define ROUTING_TABLE_MANAGER_H_
27
28 #ifndef SINGLE_THREAD
29 #include <unistd.h>
30 #include <time.h>
31 #include <sys/time.h>
32 #endif
33
34 #if defined(__ANDROID__)
35 #include <linux/time.h>
36 #endif
37 #include "ulinklist.h"
38 #include "uarraylist.h"
39 #include "octypes.h"
40
41
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #endif
46
47 /**
48  * Maximum hop/destination address length.
49  */
50 #define MAX_DEST_ADDR_LEN 40
51
52 /**
53  * Maximum number of observers for the gateway resource.
54  */
55 #define MAX_NUM_OBSERVERS 10
56
57 /**
58  * Maximum time after which gateway should send a notification for its existence.
59  * Every 30s gateway will send its existence notification.
60  */
61 #define GATEWAY_ALIVE_TIMEOUT 30
62
63 /**
64  * The routing table entries are validated for every 40 seconds for its existence.
65  */
66 #define ROUTINGTABLE_REFRESH_TIMEOUT 40
67
68 /**
69  * The routing table entries are removed if entries are invalid every 45 seconds.
70  */
71 #define ROUTINGTABLE_VALIDATION_TIMEOUT 45
72
73 /**
74  * Destination Interface Address entries.
75  */
76 typedef struct
77 {
78     uint32_t observerId;                    /**< Observer Id. */
79     CAEndpoint_t destIntfAddr;              /**< Destination Interface Address. */
80     uint32_t timeElapsed;                   /**< Time elapsed. */
81     bool isValid;                           /**< Valid check for Gateway. */
82 } RTMDestIntfInfo_t;
83
84 /**
85  * Endpoint Address entries.
86  */
87 typedef struct
88 {
89     uint16_t endpointId;                    /**< Endpoint Id. */
90     CAEndpoint_t destIntfAddr;              /**< Destination Interface Address. */
91 } RTMEndpointEntry_t;
92
93 /**
94  * Gateway Address entries.
95  */
96 typedef struct gatewayAddress
97 {
98     uint32_t gatewayId;                     /**< Gateway Id. */
99     u_arraylist_t *destIntfAddr;            /**< Destination Interface Addresses. */
100 } RTMGatewayId_t;
101
102 /**
103  * Routing table entries at Gateway.
104  */
105 typedef struct
106 {
107     RTMGatewayId_t *destination;            /**< destination Address. */
108     RTMGatewayId_t *nextHop;                /**< Next Hop Information. */
109     uint32_t routeCost;                     /**< routeCost. */
110     uint16_t mcastMessageSeqNum;            /**< sequence number for last mcast packet. */
111     uint32_t seqNum;                        /**< sequence number for notification. */
112 } RTMGatewayEntry_t;
113
114 /**
115  * Initialize the Routing Table Manager.
116  * @param[in/out] gatewayTable      Gateway Routing Table.
117  * @param[in/out] endpointTable     Endpoint Routing Table.
118  * @return  ::OC_STACK_OK or Appropriate error code.
119  */
120 OCStackResult RTMInitialize(u_linklist_t **gatewayTable, u_linklist_t **endpointTable);
121
122 /**
123  * Terminates the Routing Table Manager.
124  * @param[in/out] gatewayTable      Gateway Routing Table.
125  * @param[in/out] endpointTable     Endpoint Routing Table.
126  * @return  ::OC_STACK_OK or Appropriate error code.
127  */
128 OCStackResult RTMTerminate(u_linklist_t **gatewayTable, u_linklist_t **endpointTable);
129
130 /**
131  * Frees the gateway table memory with nodes containing structure RTMGatewayEntry_t.
132  * @param[in/out] gatewayTable      Gateway Routing Table.
133  * @return  ::OC_STACK_OK or Appropriate error code.
134  */
135 OCStackResult RTMFreeGatewayRouteTable(u_linklist_t **gatewayTable);
136
137 /**
138  * Frees the gateway ID list memory with nodes containing structute RTMGatewayId_t.
139  * @param[in/out] gatewayIdTable      Gateway ID list.
140  * @return  ::OC_STACK_OK or Appropriate error code.
141  */
142 OCStackResult RTMFreeGatewayIdList(u_linklist_t **gatewayIdTable);
143
144 /**
145  * Adds the entry to the routing table if the entry for Gateway id is
146  * not preset in Routing table, Updates the Old entry if Entry for
147  * Gateway Id is already present in Routing table i.e routeCost and NextHop
148  * will be updated for efficient hop result.
149  *
150  * @param[in]       gatewayId           Gateway Id.
151  * @param[in]       nextHop             Next Hop address.
152  * @param[in]       routeCost           Shortest Path to Destination - Hopcount.
153  * @param[in]       destInterfaces      Destination Interface Information.
154  * @param[in/out]   gatewayTable        Gateway Routing Table.
155  * @return  ::OC_STACK_OK or Appropriate error code.
156  */
157 OCStackResult RTMAddGatewayEntry(uint32_t gatewayId, uint32_t nextHop, uint32_t routeCost,
158                                  const RTMDestIntfInfo_t *destInterfaces, u_linklist_t **gatewayTable);
159
160 /**
161  * Removes the gateway entry from the routing table and also removes
162  * corresponding entries having nexthop as removed gateway.
163  * @param[in]        gatewayId              Gateway id of node need to be removed.
164  * @param[in/out]    removedGatewayNodes    Linklist containing removed gateway nodes
165  *                                          list need to be freed by caller.
166  * @param[in/out]    gatewayTable           Gateway Routing Table.
167  * @return  ::OC_STACK_OK or Appropriate error code.
168  */
169 OCStackResult RTMRemoveGatewayEntry(uint32_t gatewayId, u_linklist_t **removedGatewayNodes,
170                                     u_linklist_t **gatewayTable);
171
172 /**
173  * Removes the gateway entry from the routing table which has gateway id and nexthop as given.
174  * @param[in]        gatewayId              Gateway Id.
175  * @param[in]        nextHop                Next Hop address.
176  * @param[in]        destInfAdr             Destination Address of Next Hop to update time.
177  * @param[in/out]    existEntry             Entry which has different Next Hop.
178  * @param[in/out]    gatewayTable           Gateway Routing Table.
179  * @return  ::OC_STACK_OK or Appropriate error code.
180  */
181 OCStackResult RTMRemoveGatewayDestEntry(uint32_t gatewayId, uint32_t nextHop,
182                                         const RTMDestIntfInfo_t *destInfAdr,
183                                         RTMGatewayEntry_t **existEntry, u_linklist_t **gatewayTable);
184 /**
185  * Removes the gateway nodes.
186  * @param[in/out]    gatewayTable           Gateway Routing Table.
187  * @return  ::OC_STACK_OK or Appropriate error code.
188  */
189 OCStackResult RTMRemoveGateways(u_linklist_t **gatewayTable);
190
191 /**
192  * Gets the neighbor nodes i.e nodes with routecost 1.
193  * @param[in/out]   neighbourNodes        link list containing neighbor nodes.
194                                           this list will be pointer to GatewayIds
195                                           and must be freed by caller.
196  * @param[in]       gatewayTable           Gateway Routing Table.
197  */
198 void RTMGetNeighbours(u_linklist_t **neighbourNodes, const u_linklist_t *gatewayTable);
199
200 /**
201  * Gets next hop from the routing table.
202  * @param[in]        gatewayId              Gateway Id.
203  * @param[in]        gatewayTable           Gateway Routing Table.
204  * @return  Next Hop address - returns NULL if it is End Device.
205  */
206 RTMGatewayId_t *RTMGetNextHop(uint32_t gatewayId, const u_linklist_t *gatewayTable);
207
208 /**
209  * Updates destination interface address of an entry with provided gateway id
210  * as destination.
211  * @param[in]        gatewayId               Gateway Id of Hop need to be updated.
212  * @param[in]        destInterfaces          Destination Interface Information.
213  * @param[in]        addAdr                  Add/Remove destination address.
214  * @param[in/out]    gatewayTable            Gateway Routing Table.
215  * @return  ::OC_STACK_OK or Appropriate error code.
216  */
217 OCStackResult RTMUpdateDestinationIntfAdr(uint32_t gatewayId, RTMDestIntfInfo_t destInterfaces,
218                                           bool addAdr, u_linklist_t **gatewayTable);
219
220 /**
221  * Updates Multicast sequence number for gatewayID
222  * @param[in]       gatewayId           Gateway Id of Hop need to be updated.
223  * @param[in]       seqNum              Sequence number for last cast packet from gateway.
224  * @param[in/out]   gatewayTable        Gateway Routing Table.
225  * @return  ::OC_STACK_OK or Appropriate error code.
226  */
227 OCStackResult RTMUpdateMcastSeqNumber(uint32_t gatewayId, uint16_t seqNum,
228                                       u_linklist_t **gatewayTable);
229
230 /**
231  * Prints the routing table
232  * @param[in]    gatewayTable           Gateway Routing Table.
233  * @param[in]    endpointTable          Endpoint Routing Table.
234  */
235 void RTMPrintTable(const u_linklist_t *gatewayTable, const u_linklist_t *endpointTable);
236
237 /**
238  * Frees the GatewayId
239  * @param[in]        gateway                Gateway Structure pointer.
240  * @param[in/out]    gatewayTable           Gateway Routing Table.
241  */
242 void RTMFreeGateway(RTMGatewayId_t *gateway, u_linklist_t **gatewayTable);
243
244 /**
245  * Gets the list of observer IDs.
246  * @param[in/out]    obsList                List of Observation IDs.
247  * @param[in/out]    obsListLen             Length if Observation ID list.
248  * @param[in]        gatewayTable           Gateway Routing Table.
249  */
250 void RTMGetObserverList(OCObservationId **obsList, uint8_t *obsListLen,
251                         const u_linklist_t *gatewayTable);
252
253 /**
254  * Adds a observer address and obsID to the list.
255  * @param[in]        obsID                  Observation ID.
256  * @param[in]        devAddr                Address of Gateway.
257  * @param[in/out]    gatewayTable           Gateway Routing Table.
258  * @return  ::OC_STACK_OK or Appropriate error code.
259  */
260 OCStackResult RTMAddObserver(uint32_t obsID, CAEndpoint_t devAddr, u_linklist_t **gatewayTable);
261
262 /**
263  * Check if a particular observer address is already registerd and returns
264  * its obsID if present.
265  * @param[in]        devAddr                Address of Gateway.
266  * @param[in/out]    obsID                  Observation ID.
267  * @param[in]        gatewayTable           Gateway Routing Table.
268  * @return  true or false.
269  */
270 bool RTMIsObserverPresent(CAEndpoint_t devAddr, OCObservationId *obsID,
271                           const u_linklist_t *gatewayTable);
272
273 /**
274  * Gets Current Time in Micro Seconds.
275  * @return  Current Time.
276  */
277 uint64_t RTMGetCurrentTime();
278
279 /**
280  * Update Gateway Address Validity.
281  * @param[in/out]    invalidTable      Removed entries Table.
282  * @param[in/out]    gatewayTable      Gateway Routing Table.
283  * @return  ::OC_STACK_OK or Appropriate error code.
284  */
285 OCStackResult RTMUpdateDestAddrValidity(u_linklist_t **invalidTable, u_linklist_t **gatewayTable);
286
287 /**
288  * Removes invalid gateways.
289  * @param[in/out]    invalidTable      Removed entries Table.
290  * @param[in/out]    gatewayTable      Gateway Routing Table.
291  * @return  ::OC_STACK_OK or Appropriate error code.
292  */
293 OCStackResult RTMRemoveInvalidGateways(u_linklist_t **invalidTable, u_linklist_t **gatewayTable);
294
295 /**
296  * Update Sequence number of Gateway Entry.
297  * @param[in]       gatewayId           Gateway Id.
298  * @param[in]       seqNum              Sequence Number of Entry.
299  * @param[in]       destInterfaces      Destination Interface Information.
300  * @param[out]      gatewayTable        Gateway Routing Table.
301  * @param[in]       forceUpdate         To Update parameters forcefully.
302  * @return  ::OC_STACK_OK or Appropriate error code.
303  */
304 OCStackResult RTMUpdateEntryParameters(uint32_t gatewayId, uint32_t seqNum,
305                                        const RTMDestIntfInfo_t *destAdr, u_linklist_t **gatewayTable,
306                                        bool forceUpdate);
307
308 #ifdef __cplusplus
309 } /* extern "C" */
310 #endif
311
312 #endif /* ROUTING_TABLE_MANAGER_H_ */
313