1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "iotivity_config.h"
22 #include "occlientcb.h"
25 #include "oic_malloc.h"
28 #ifdef HAVE_SYS_TIME_H
31 #ifdef HAVE_ARDUINO_TIME_H
34 #include "coap_time.h"
37 #include "cainterface.h"
40 #define TAG "OIC_RI_CLIENTCB"
42 struct ClientCB *cbList = NULL;
43 static OCMulticastNode * mcPresenceNodes = NULL;
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)
52 if (!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
54 return OC_STACK_INVALID_PARAM;
57 ClientCB *cbNode = NULL;
60 if (method == OC_REST_PRESENCE)
61 { // Retrieve the presence callback structure for this specific requestUri.
62 cbNode = GetClientCB(NULL, 0, NULL, requestUri);
65 if (!cbNode)// If it does not already exist, create new node.
66 #endif // WITH_PRESENCE
68 cbNode = (ClientCB*) OICMalloc(sizeof(ClientCB));
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;
89 cbNode->presence = NULL;
90 cbNode->filterResourceType = NULL;
91 #endif // WITH_PRESENCE
93 if (method == OC_REST_PRESENCE ||
94 method == OC_REST_OBSERVE ||
95 method == OC_REST_OBSERVE_ALL)
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);
113 // Ensure that the handle the SDK hands back up to the application layer for the
114 // OCDoResource call matches the found ClientCB Node.
119 cbData->cd(cbData->context);
126 *handle = cbNode->handle;
129 if (method == OC_REST_PRESENCE && resourceTypeName)
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.
137 OICFree(resourceTypeName);
140 OICFree(resourceTypeName);
146 return OC_STACK_NO_MEMORY;
149 void DeleteClientCB(ClientCB * cbNode)
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)
163 cbNode->deleteCallback(cbNode->context);
167 if (cbNode->presence)
169 OICFree(cbNode->presence->timeOut);
170 OICFree(cbNode->presence);
172 if (cbNode->method == OC_REST_PRESENCE)
174 OCResourceType * pointer = cbNode->filterResourceType;
175 OCResourceType * next = NULL;
178 next = pointer->next;
179 OICFree(pointer->resourcetypename);
184 #endif // WITH_PRESENCE
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
197 static void CheckAndDeleteTimedOutCB(ClientCB* cbNode)
203 if (cbNode->TTL == 0)
210 if (cbNode->TTL < now)
212 OIC_LOG(INFO, TAG, "Deleting timed-out callback");
213 DeleteClientCB(cbNode);
217 ClientCB* GetClientCB(const CAToken_t token, uint8_t tokenLength,
218 OCDoHandle handle, const char * requestUri)
220 ClientCB* out = NULL;
222 if (token && tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
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)
229 OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
231 if (memcmp(out->token, token, tokenLength) == 0)
235 CheckAndDeleteTimedOutCB(out);
240 LL_FOREACH(cbList, out)
242 if (out->handle == handle)
246 CheckAndDeleteTimedOutCB(out);
251 OIC_LOG_V(INFO, TAG, "Looking for uri %s", requestUri);
252 LL_FOREACH(cbList, out)
254 OIC_LOG_V(INFO, TAG, "\tFound %s", out->requestUri);
255 if (out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
259 CheckAndDeleteTimedOutCB(out);
262 OIC_LOG(INFO, TAG, "Callback Not found !!");
267 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName)
269 OCResourceType * newResourceType = NULL;
270 if (cbNode && resourceTypeName)
272 // Form a new resourceType member.
273 newResourceType = (OCResourceType *) OICMalloc(sizeof(OCResourceType));
274 if (!newResourceType)
276 return OC_STACK_NO_MEMORY;
279 newResourceType->next = NULL;
280 newResourceType->resourcetypename = resourceTypeName;
282 LL_APPEND(cbNode->filterResourceType, newResourceType);
285 return OC_STACK_ERROR;
287 #endif // WITH_PRESENCE
289 void DeleteClientCBList()
293 LL_FOREACH_SAFE(cbList, out, tmp)
300 void FindAndDeleteClientCB(ClientCB * cbNode)
305 LL_FOREACH(cbList, tmp)
316 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
320 return OC_STACK_INVALID_PARAM;
323 OCMulticastNode *node = (OCMulticastNode*) OICMalloc(sizeof(*node));
328 LL_APPEND(mcPresenceNodes, node);
333 return OC_STACK_NO_MEMORY;
336 OCMulticastNode* GetMCPresenceNode(const char * uri)
340 OCMulticastNode* out = NULL;
341 LL_FOREACH(mcPresenceNodes, out)
343 if (out->uri && strcmp(out->uri, uri) == 0)
349 OIC_LOG(INFO, TAG, "MulticastNode Not found !!");