Revert "Generate iotivity_config.h at build time"
[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 HAVE_SYS_TIME_H
29 #include <sys/time.h>
30 #endif
31 #ifdef HAVE_ARDUINO_TIME_H
32 #include "Time.h"
33 #endif
34 #include "coap_time.h"
35
36 #include "cacommon.h"
37 #include "cainterface.h"
38
39 /// Module Name
40 #define TAG "OIC_RI_CLIENTCB"
41
42 struct ClientCB *cbList = NULL;
43 static OCMulticastNode * mcPresenceNodes = NULL;
44
45 OCStackResult
46 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
47              CAToken_t token, uint8_t tokenLength,
48              OCDoHandle *handle, OCMethod method,
49              OCDevAddr *devAddr, char * requestUri,
50              char * resourceTypeName, uint32_t ttl)
51 {
52     if (!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
53     {
54         return OC_STACK_INVALID_PARAM;
55     }
56
57     ClientCB *cbNode = NULL;
58
59 #ifdef WITH_PRESENCE
60     if (method == OC_REST_PRESENCE)
61     {   // Retrieve the presence callback structure for this specific requestUri.
62         cbNode = GetClientCB(NULL, 0, NULL, requestUri);
63     }
64
65     if (!cbNode)// If it does not already exist, create new node.
66 #endif // WITH_PRESENCE
67     {
68         cbNode = (ClientCB*) OICMalloc(sizeof(ClientCB));
69         if (!cbNode)
70         {
71             *clientCB = NULL;
72             goto exit;
73         }
74         else
75         {
76             OIC_LOG(INFO, TAG, "Adding client callback with token");
77             OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
78             cbNode->callBack = cbData->cb;
79             cbNode->context = cbData->context;
80             cbNode->deleteCallback = cbData->cd;
81             //Note: token memory is allocated in the caller OCDoResource
82             //but freed in DeleteClientCB
83             cbNode->token = token;
84             cbNode->tokenLength = tokenLength;
85             cbNode->handle = *handle;
86             cbNode->method = method;
87             cbNode->sequenceNumber = 0;
88 #ifdef WITH_PRESENCE
89             cbNode->presence = NULL;
90             cbNode->filterResourceType = NULL;
91 #endif // WITH_PRESENCE
92
93             if (method == OC_REST_PRESENCE ||
94                 method == OC_REST_OBSERVE  ||
95                 method == OC_REST_OBSERVE_ALL)
96             {
97                 cbNode->TTL = 0;
98             }
99             else
100             {
101                 cbNode->TTL = ttl;
102             }
103             cbNode->requestUri = requestUri;    // I own it now
104             cbNode->devAddr = devAddr;          // I own it now
105             OIC_LOG_V(INFO, TAG, "Added Callback for uri : %s", requestUri);
106             LL_APPEND(cbList, cbNode);
107             *clientCB = cbNode;
108         }
109     }
110 #ifdef WITH_PRESENCE
111     else
112     {
113         // Ensure that the handle the SDK hands back up to the application layer for the
114         // OCDoResource call matches the found ClientCB Node.
115         *clientCB = cbNode;
116
117         if (cbData->cd)
118         {
119             cbData->cd(cbData->context);
120         }
121
122         OICFree(token);
123         OICFree(*handle);
124         OICFree(requestUri);
125         OICFree(devAddr);
126         *handle = cbNode->handle;
127     }
128
129     if (method == OC_REST_PRESENCE && resourceTypeName)
130     {
131         // Amend the found or created node by adding a new resourceType to it.
132         return InsertResourceTypeFilter(cbNode,(char *)resourceTypeName);
133         // I own resourceTypName now.
134     }
135     else
136     {
137         OICFree(resourceTypeName);
138     }
139 #else
140     OICFree(resourceTypeName);
141 #endif
142
143     return OC_STACK_OK;
144
145 exit:
146     return OC_STACK_NO_MEMORY;
147 }
148
149 void DeleteClientCB(ClientCB * cbNode)
150 {
151     if (cbNode)
152     {
153         LL_DELETE(cbList, cbNode);
154         OIC_LOG (INFO, TAG, "Deleting token");
155         OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
156         CADestroyToken (cbNode->token);
157         OICFree(cbNode->devAddr);
158         OICFree(cbNode->handle);
159         OIC_LOG_V (INFO, TAG, "Deleting callback with uri %s", cbNode->requestUri);
160         OICFree(cbNode->requestUri);
161         if (cbNode->deleteCallback)
162         {
163             cbNode->deleteCallback(cbNode->context);
164         }
165
166 #ifdef WITH_PRESENCE
167         if (cbNode->presence)
168         {
169             OICFree(cbNode->presence->timeOut);
170             OICFree(cbNode->presence);
171         }
172         if (cbNode->method == OC_REST_PRESENCE)
173         {
174             OCResourceType * pointer = cbNode->filterResourceType;
175             OCResourceType * next = NULL;
176             while(pointer)
177             {
178                 next = pointer->next;
179                 OICFree(pointer->resourcetypename);
180                 OICFree(pointer);
181                 pointer = next;
182             }
183         }
184 #endif // WITH_PRESENCE
185         OICFree(cbNode);
186         cbNode = NULL;
187     }
188 }
189
190 /*
191  * This function checks if the node is past its time to live and
192  * deletes it if timed-out. Calling this function with a  presence or observe
193  * callback with ttl set to 0 will not delete anything as presence nodes have
194  * their own mechanisms for timeouts. A null argument will cause the function to
195  * silently return.
196  */
197 static void CheckAndDeleteTimedOutCB(ClientCB* cbNode)
198 {
199     if (!cbNode)
200     {
201         return;
202     }
203     if (cbNode->TTL == 0)
204     {
205         return;
206     }
207     coap_tick_t now;
208     coap_ticks(&now);
209
210     if (cbNode->TTL < now)
211     {
212         OIC_LOG(INFO, TAG, "Deleting timed-out callback");
213         DeleteClientCB(cbNode);
214     }
215 }
216
217 ClientCB* GetClientCB(const CAToken_t token, uint8_t tokenLength,
218                       OCDoHandle handle, const char * requestUri)
219 {
220     ClientCB* out = NULL;
221
222     if (token && tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
223     {
224         OIC_LOG (INFO, TAG,  "Looking for token");
225         OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
226         OIC_LOG(INFO, TAG, "\tFound in callback list");
227         LL_FOREACH(cbList, out)
228         {
229             OIC_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         OIC_LOG_V(INFO, TAG, "Looking for uri %s", requestUri);
252         LL_FOREACH(cbList, out)
253         {
254             OIC_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     OIC_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 = (OCMulticastNode*) OICMalloc(sizeof(*node));
324     if (node)
325     {
326         node->nonce = nonce;
327         node->uri = uri;
328         LL_APPEND(mcPresenceNodes, node);
329         *outnode = node;
330         return OC_STACK_OK;
331     }
332     *outnode = NULL;
333     return OC_STACK_NO_MEMORY;
334 }
335
336 OCMulticastNode* GetMCPresenceNode(const char * uri)
337 {
338     if (uri)
339     {
340         OCMulticastNode* out = NULL;
341         LL_FOREACH(mcPresenceNodes, out)
342         {
343             if (out->uri && strcmp(out->uri, uri) == 0)
344             {
345                 return out;
346             }
347         }
348     }
349     OIC_LOG(INFO, TAG, "MulticastNode Not found !!");
350     return NULL;
351 }