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"
33 #include "coap_time.h"
36 #include "cainterface.h"
39 #define TAG PCF("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 char * requestUri, char * resourceTypeName, OCConnectivityType conType, uint32_t ttl)
50 if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
52 return OC_STACK_INVALID_PARAM;
55 ClientCB *cbNode = NULL;
58 if(method == OC_REST_PRESENCE)
59 { // Retrieve the presence callback structure for this specific requestUri.
60 cbNode = GetClientCB(NULL, 0, NULL, requestUri);
62 #endif // WITH_PRESENCE
64 if(!cbNode)// If it does not already exist, create new node.
66 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
74 cbNode->callBack = cbData->cb;
75 cbNode->context = cbData->context;
76 cbNode->deleteCallback = cbData->cd;
77 //Note: token memory is allocated in the caller OCDoResource
78 //but freed in DeleteClientCB
79 cbNode->token = token;
80 cbNode->tokenLength = tokenLength;
81 cbNode->handle = *handle;
82 cbNode->method = method;
83 cbNode->sequenceNumber = 0;
85 cbNode->presence = NULL;
86 cbNode->filterResourceType = NULL;
87 #endif // WITH_PRESENCE
89 if (method == OC_REST_PRESENCE ||
90 method == OC_REST_OBSERVE ||
91 method == OC_REST_OBSERVE_ALL)
99 cbNode->requestUri = requestUri;
100 cbNode->conType = conType;
101 LL_APPEND(cbList, cbNode);
107 // Ensure that the handle the SDK hands back up to the application layer for the
108 // OCDoResource call matches the found ClientCB Node.
113 cbData->cd(cbData->context);
119 *handle = cbNode->handle;
123 if(method == OC_REST_PRESENCE && resourceTypeName)
125 // Amend the found or created node by adding a new resourceType to it.
126 return InsertResourceTypeFilter(cbNode,(char *)resourceTypeName);
130 OCFree(resourceTypeName);
133 OCFree(resourceTypeName);
139 return OC_STACK_NO_MEMORY;
142 void DeleteClientCB(ClientCB * cbNode)
146 LL_DELETE(cbList, cbNode);
147 OC_LOG(INFO, TAG, PCF("deleting tokens"));
148 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
149 CADestroyToken (cbNode->token);
150 OCFree(cbNode->handle);
151 OCFree(cbNode->requestUri);
152 if(cbNode->deleteCallback)
154 cbNode->deleteCallback(cbNode->context);
160 OCFree(cbNode->presence->timeOut);
161 OCFree(cbNode->presence);
163 if(cbNode->method == OC_REST_PRESENCE)
165 OCResourceType * pointer = cbNode->filterResourceType;
166 OCResourceType * next = NULL;
169 next = pointer->next;
170 OCFree(pointer->resourcetypename);
175 #endif // WITH_PRESENCE
182 * This function checks if the node is past its time to live and
183 * deletes it if timed-out. Calling this function with a presence or observe
184 * callback with ttl set to 0 will not delete anything as presence nodes have
185 * their own mechanisms for timeouts. A null argument will cause the function to
188 static void CheckAndDeleteTimedOutCB(ClientCB* cbNode)
194 if (cbNode->TTL == 0)
201 if (cbNode->TTL < now)
203 OC_LOG(INFO, TAG, PCF("Deleting timed-out callback"));
204 DeleteClientCB(cbNode);
208 ClientCB* GetClientCB(const CAToken_t token, uint8_t tokenLength,
209 OCDoHandle handle, const char * requestUri)
212 ClientCB* out = NULL;
214 if(token && *token && tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
216 LL_FOREACH(cbList, out)
218 OC_LOG(INFO, TAG, PCF("comparing tokens"));
219 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)token, tokenLength);
220 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
222 if(memcmp(out->token, token, tokenLength) == 0)
226 CheckAndDeleteTimedOutCB(out);
231 LL_FOREACH(cbList, out)
233 if(out->handle == handle)
237 CheckAndDeleteTimedOutCB(out);
242 LL_FOREACH(cbList, out)
244 if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
248 CheckAndDeleteTimedOutCB(out);
251 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
256 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName)
258 OCResourceType * newResourceType = NULL;
259 if(cbNode && resourceTypeName)
261 // Form a new resourceType member.
262 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
265 return OC_STACK_NO_MEMORY;
268 newResourceType->next = NULL;
269 newResourceType->resourcetypename = resourceTypeName;
271 LL_APPEND(cbNode->filterResourceType, newResourceType);
274 return OC_STACK_ERROR;
276 #endif // WITH_PRESENCE
278 void DeleteClientCBList()
282 LL_FOREACH_SAFE(cbList, out, tmp)
289 void FindAndDeleteClientCB(ClientCB * cbNode)
294 LL_FOREACH(cbList, tmp)
305 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
309 return OC_STACK_INVALID_PARAM;
312 OCMulticastNode *node;
314 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
320 LL_APPEND(mcPresenceNodes, node);
325 return OC_STACK_NO_MEMORY;
328 OCMulticastNode* GetMCPresenceNode(const char * uri)
330 OCMulticastNode* out = NULL;
334 LL_FOREACH(mcPresenceNodes, out)
336 if(out->uri && strcmp(out->uri, uri) == 0)
342 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));