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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "occlientcb.h"
25 #include "oic_malloc.h"
33 #include "coap_time.h"
36 #include "cainterface.h"
39 #define TAG "occlientcb"
41 struct ClientCB *cbList = NULL;
42 static OCMulticastNode * mcPresenceNodes = NULL;
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)
51 if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
53 return OC_STACK_INVALID_PARAM;
56 ClientCB *cbNode = NULL;
59 if(method == OC_REST_PRESENCE)
60 { // Retrieve the presence callback structure for this specific requestUri.
61 cbNode = GetClientCB(NULL, 0, NULL, requestUri);
63 #endif // WITH_PRESENCE
65 if(!cbNode)// If it does not already exist, create new node.
67 cbNode = (ClientCB*) OICMalloc(sizeof(ClientCB));
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;
88 cbNode->presence = NULL;
89 cbNode->filterResourceType = NULL;
90 #endif // WITH_PRESENCE
92 if (method == OC_REST_PRESENCE ||
93 method == OC_REST_OBSERVE ||
94 method == OC_REST_OBSERVE_ALL)
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);
111 // Ensure that the handle the SDK hands back up to the application layer for the
112 // OCDoResource call matches the found ClientCB Node.
117 cbData->cd(cbData->context);
124 *handle = cbNode->handle;
128 if(method == OC_REST_PRESENCE && resourceTypeName)
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.
136 OICFree(resourceTypeName);
139 OICFree(resourceTypeName);
145 return OC_STACK_NO_MEMORY;
148 void DeleteClientCB(ClientCB * cbNode)
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)
162 cbNode->deleteCallback(cbNode->context);
168 OICFree(cbNode->presence->timeOut);
169 OICFree(cbNode->presence);
171 if(cbNode->method == OC_REST_PRESENCE)
173 OCResourceType * pointer = cbNode->filterResourceType;
174 OCResourceType * next = NULL;
177 next = pointer->next;
178 OICFree(pointer->resourcetypename);
183 #endif // WITH_PRESENCE
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
196 static void CheckAndDeleteTimedOutCB(ClientCB* cbNode)
202 if (cbNode->TTL == 0)
209 if (cbNode->TTL < now)
211 OC_LOG(INFO, TAG, "Deleting timed-out callback");
212 DeleteClientCB(cbNode);
216 ClientCB* GetClientCB(const CAToken_t token, uint8_t tokenLength,
217 OCDoHandle handle, const char * requestUri)
220 ClientCB* out = NULL;
222 if(token && *token && tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
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)
229 OC_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 OC_LOG_V(INFO, TAG, "Looking for uri %s", requestUri);
252 LL_FOREACH(cbList, out)
254 OC_LOG_V(INFO, TAG, "\tFound %s", out->requestUri);
255 if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
259 CheckAndDeleteTimedOutCB(out);
262 OC_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));
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;
325 node = (OCMulticastNode*) OICMalloc(sizeof(OCMulticastNode));
331 LL_APPEND(mcPresenceNodes, node);
336 return OC_STACK_NO_MEMORY;
339 OCMulticastNode* GetMCPresenceNode(const char * uri)
341 OCMulticastNode* out = NULL;
345 LL_FOREACH(mcPresenceNodes, out)
347 if(out->uri && strcmp(out->uri, uri) == 0)
353 OC_LOG(INFO, TAG, "MulticastNode Not found !!");