Merge branch 'master' into resource-encapsulation
[platform/upstream/iotivity.git] / resource / csdk / stack / src / occlientcb.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 #include "occlientcb.h"
23 #include "utlist.h"
24 #include "logger.h"
25 #include "oic_malloc.h"
26 #include <string.h>
27
28 #ifdef WITH_ARDUINO
29 #include "Time.h"
30 #else
31 #include <sys/time.h>
32 #endif
33 #include "coap_time.h"
34
35 #include "cacommon.h"
36 #include "cainterface.h"
37
38 /// Module Name
39 #define TAG "occlientcb"
40
41 struct ClientCB *cbList = NULL;
42 static OCMulticastNode * mcPresenceNodes = NULL;
43
44 OCStackResult
45 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
46              CAToken_t token, uint8_t tokenLength,
47              OCDoHandle *handle, OCMethod method,
48              OCDevAddr *devAddr, char * requestUri,
49              char * resourceTypeName, uint32_t ttl)
50 {
51     if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
52     {
53         return OC_STACK_INVALID_PARAM;
54     }
55
56     ClientCB *cbNode = NULL;
57
58     #ifdef WITH_PRESENCE
59     if(method == OC_REST_PRESENCE)
60     {   // Retrieve the presence callback structure for this specific requestUri.
61         cbNode = GetClientCB(NULL, 0, NULL, requestUri);
62     }
63     #endif // WITH_PRESENCE
64
65     if(!cbNode)// If it does not already exist, create new node.
66     {
67         cbNode = (ClientCB*) OICMalloc(sizeof(ClientCB));
68         if(!cbNode)
69         {
70             *clientCB = NULL;
71             goto exit;
72         }
73         else
74         {
75             OC_LOG(INFO, TAG, "Adding client callback with token");
76             OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
77             cbNode->callBack = cbData->cb;
78             cbNode->context = cbData->context;
79             cbNode->deleteCallback = cbData->cd;
80             //Note: token memory is allocated in the caller OCDoResource
81             //but freed in DeleteClientCB
82             cbNode->token = token;
83             cbNode->tokenLength = tokenLength;
84             cbNode->handle = *handle;
85             cbNode->method = method;
86             cbNode->sequenceNumber = 0;
87             #ifdef WITH_PRESENCE
88             cbNode->presence = NULL;
89             cbNode->filterResourceType = NULL;
90             #endif // WITH_PRESENCE
91
92             if (method == OC_REST_PRESENCE ||
93                 method == OC_REST_OBSERVE  ||
94                 method == OC_REST_OBSERVE_ALL)
95             {
96                 cbNode->TTL = 0;
97             }
98             else
99             {
100                 cbNode->TTL = ttl;
101             }
102             cbNode->requestUri = requestUri;    // I own it now
103             cbNode->devAddr = devAddr;          // I own it now
104             OC_LOG_V(INFO, TAG, "Added Callback for uri : %s", requestUri);
105             LL_APPEND(cbList, cbNode);
106             *clientCB = cbNode;
107         }
108     }
109     else
110     {
111         // Ensure that the handle the SDK hands back up to the application layer for the
112         // OCDoResource call matches the found ClientCB Node.
113         *clientCB = cbNode;
114
115         if (cbData->cd)
116         {
117             cbData->cd(cbData->context);
118         }
119
120         OICFree(token);
121         OICFree(*handle);
122         OICFree(requestUri);
123         OICFree(devAddr);
124         *handle = cbNode->handle;
125     }
126
127     #ifdef WITH_PRESENCE
128     if(method == OC_REST_PRESENCE && resourceTypeName)
129     {
130         // Amend the found or created node by adding a new resourceType to it.
131         return InsertResourceTypeFilter(cbNode,(char *)resourceTypeName);
132         // I own resourceTypName now.
133     }
134     else
135     {
136         OICFree(resourceTypeName);
137     }
138     #else
139     OICFree(resourceTypeName);
140     #endif
141
142     return OC_STACK_OK;
143
144     exit:
145         return OC_STACK_NO_MEMORY;
146 }
147
148 void DeleteClientCB(ClientCB * cbNode)
149 {
150     if(cbNode)
151     {
152         LL_DELETE(cbList, cbNode);
153         OC_LOG (INFO, TAG, "Deleting token");
154         OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
155         CADestroyToken (cbNode->token);
156         OICFree(cbNode->devAddr);
157         OICFree(cbNode->handle);
158         OC_LOG_V (INFO, TAG, "Deleting callback with uri %s", cbNode->requestUri);
159         OICFree(cbNode->requestUri);
160         if(cbNode->deleteCallback)
161         {
162             cbNode->deleteCallback(cbNode->context);
163         }
164
165         #ifdef WITH_PRESENCE
166         if(cbNode->presence)
167         {
168             OICFree(cbNode->presence->timeOut);
169             OICFree(cbNode->presence);
170         }
171         if(cbNode->method == OC_REST_PRESENCE)
172         {
173             OCResourceType * pointer = cbNode->filterResourceType;
174             OCResourceType * next = NULL;
175             while(pointer)
176             {
177                 next = pointer->next;
178                 OICFree(pointer->resourcetypename);
179                 OICFree(pointer);
180                 pointer = next;
181             }
182         }
183         #endif // WITH_PRESENCE
184         OICFree(cbNode);
185         cbNode = NULL;
186     }
187 }
188
189 /*
190  * This function checks if the node is past its time to live and
191  * deletes it if timed-out. Calling this function with a  presence or observe
192  * callback with ttl set to 0 will not delete anything as presence nodes have
193  * their own mechanisms for timeouts. A null argument will cause the function to
194  * silently return.
195  */
196 static void CheckAndDeleteTimedOutCB(ClientCB* cbNode)
197 {
198     if (!cbNode)
199     {
200         return;
201     }
202     if (cbNode->TTL == 0)
203     {
204         return;
205     }
206     coap_tick_t now;
207     coap_ticks(&now);
208
209     if (cbNode->TTL < now)
210     {
211         OC_LOG(INFO, TAG, "Deleting timed-out callback");
212         DeleteClientCB(cbNode);
213     }
214 }
215
216 ClientCB* GetClientCB(const CAToken_t token, uint8_t tokenLength,
217         OCDoHandle handle, const char * requestUri)
218 {
219
220     ClientCB* out = NULL;
221
222     if(token && *token && tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
223     {
224         OC_LOG (INFO, TAG,  "Looking for token");
225         OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
226         OC_LOG(INFO, TAG, "\tFound in callback list");
227         LL_FOREACH(cbList, out)
228         {
229             OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
230
231             if(memcmp(out->token, token, tokenLength) == 0)
232             {
233                 return out;
234             }
235             CheckAndDeleteTimedOutCB(out);
236         }
237     }
238     else if(handle)
239     {
240         LL_FOREACH(cbList, out)
241         {
242             if(out->handle == handle)
243             {
244                 return out;
245             }
246             CheckAndDeleteTimedOutCB(out);
247         }
248     }
249     else if(requestUri)
250     {
251         OC_LOG_V(INFO, TAG, "Looking for uri %s", requestUri);
252         LL_FOREACH(cbList, out)
253         {
254             OC_LOG_V(INFO, TAG, "\tFound %s", out->requestUri);
255             if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
256             {
257                 return out;
258             }
259             CheckAndDeleteTimedOutCB(out);
260         }
261     }
262     OC_LOG(INFO, TAG, "Callback Not found !!");
263     return NULL;
264 }
265
266 #ifdef WITH_PRESENCE
267 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName)
268 {
269     OCResourceType * newResourceType = NULL;
270     if(cbNode && resourceTypeName)
271     {
272         // Form a new resourceType member.
273         newResourceType = (OCResourceType *) OICMalloc(sizeof(OCResourceType));
274         if(!newResourceType)
275         {
276             return OC_STACK_NO_MEMORY;
277         }
278
279         newResourceType->next = NULL;
280         newResourceType->resourcetypename = resourceTypeName;
281
282         LL_APPEND(cbNode->filterResourceType, newResourceType);
283         return OC_STACK_OK;
284     }
285     return OC_STACK_ERROR;
286 }
287 #endif // WITH_PRESENCE
288
289 void DeleteClientCBList()
290 {
291     ClientCB* out;
292     ClientCB* tmp;
293     LL_FOREACH_SAFE(cbList, out, tmp)
294     {
295         DeleteClientCB(out);
296     }
297     cbList = NULL;
298 }
299
300 void FindAndDeleteClientCB(ClientCB * cbNode)
301 {
302     ClientCB* tmp;
303     if(cbNode)
304     {
305         LL_FOREACH(cbList, tmp)
306         {
307             if (cbNode == tmp)
308             {
309                 DeleteClientCB(tmp);
310                 break;
311             }
312         }
313     }
314 }
315
316 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
317 {
318     if(!outnode)
319     {
320         return OC_STACK_INVALID_PARAM;
321     }
322
323     OCMulticastNode *node;
324
325     node = (OCMulticastNode*) OICMalloc(sizeof(OCMulticastNode));
326
327     if (node)
328     {
329         node->nonce = nonce;
330         node->uri = uri;
331         LL_APPEND(mcPresenceNodes, node);
332         *outnode = node;
333         return OC_STACK_OK;
334     }
335     *outnode = NULL;
336     return OC_STACK_NO_MEMORY;
337 }
338
339 OCMulticastNode* GetMCPresenceNode(const char * uri)
340 {
341     OCMulticastNode* out = NULL;
342
343     if(uri)
344     {
345         LL_FOREACH(mcPresenceNodes, out)
346         {
347             if(out->uri && strcmp(out->uri, uri) == 0)
348             {
349                 return out;
350             }
351         }
352     }
353     OC_LOG(INFO, TAG, "MulticastNode Not found !!");
354     return NULL;
355 }
356