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 "OIC_RI_CLIENTCB"
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);
64 if (!cbNode)// If it does not already exist, create new node.
65 #endif // WITH_PRESENCE
67 cbNode = (ClientCB*) OICMalloc(sizeof(ClientCB));
75 OIC_LOG(INFO, TAG, "Adding client callback with token");
76 OIC_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 OIC_LOG_V(INFO, TAG, "Added Callback for uri : %s", requestUri);
105 LL_APPEND(cbList, cbNode);
112 // Ensure that the handle the SDK hands back up to the application layer for the
113 // OCDoResource call matches the found ClientCB Node.
118 cbData->cd(cbData->context);
125 *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 OIC_LOG (INFO, TAG, "Deleting token");
154 OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
155 CADestroyToken (cbNode->token);
156 OICFree(cbNode->devAddr);
157 OICFree(cbNode->handle);
158 OIC_LOG_V (INFO, TAG, "Deleting callback with uri %s", cbNode->requestUri);
159 OICFree(cbNode->requestUri);
160 if (cbNode->deleteCallback)
162 cbNode->deleteCallback(cbNode->context);
166 if (cbNode->presence)
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 OIC_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)
219 ClientCB* out = NULL;
221 if (token && tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
223 OIC_LOG (INFO, TAG, "Looking for token");
224 OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
225 OIC_LOG(INFO, TAG, "\tFound in callback list");
226 LL_FOREACH(cbList, out)
228 OIC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
230 if (memcmp(out->token, token, tokenLength) == 0)
234 CheckAndDeleteTimedOutCB(out);
239 LL_FOREACH(cbList, out)
241 if (out->handle == handle)
245 CheckAndDeleteTimedOutCB(out);
250 OIC_LOG_V(INFO, TAG, "Looking for uri %s", requestUri);
251 LL_FOREACH(cbList, out)
253 OIC_LOG_V(INFO, TAG, "\tFound %s", out->requestUri);
254 if (out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
258 CheckAndDeleteTimedOutCB(out);
261 OIC_LOG(INFO, TAG, "Callback Not found !!");
266 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName)
268 OCResourceType * newResourceType = NULL;
269 if (cbNode && resourceTypeName)
271 // Form a new resourceType member.
272 newResourceType = (OCResourceType *) OICMalloc(sizeof(OCResourceType));
273 if (!newResourceType)
275 return OC_STACK_NO_MEMORY;
278 newResourceType->next = NULL;
279 newResourceType->resourcetypename = resourceTypeName;
281 LL_APPEND(cbNode->filterResourceType, newResourceType);
284 return OC_STACK_ERROR;
286 #endif // WITH_PRESENCE
288 void DeleteClientCBList()
292 LL_FOREACH_SAFE(cbList, out, tmp)
299 void FindAndDeleteClientCB(ClientCB * cbNode)
304 LL_FOREACH(cbList, tmp)
315 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
319 return OC_STACK_INVALID_PARAM;
322 OCMulticastNode *node = (OCMulticastNode*) OICMalloc(sizeof(*node));
327 LL_APPEND(mcPresenceNodes, node);
332 return OC_STACK_NO_MEMORY;
335 OCMulticastNode* GetMCPresenceNode(const char * uri)
339 OCMulticastNode* out = NULL;
340 LL_FOREACH(mcPresenceNodes, out)
342 if (out->uri && strcmp(out->uri, uri) == 0)
348 OIC_LOG(INFO, TAG, "MulticastNode Not found !!");