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"
29 #include "cainterface.h"
32 #define TAG PCF("occlientcb")
34 struct ClientCB *cbList = NULL;
35 static OCMulticastNode * mcPresenceNodes = NULL;
38 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
39 CAToken_t * token, uint8_t tokenLength,
40 OCDoHandle *handle, OCMethod method,
41 char * requestUri, char * resourceTypeName)
43 if(!clientCB || !cbData || !handle || !requestUri || tokenLength > CA_MAX_TOKEN_LEN)
45 return OC_STACK_INVALID_PARAM;
48 ClientCB *cbNode = NULL;
51 if(method == OC_REST_PRESENCE)
52 { // Retrieve the presence callback structure for this specific requestUri.
53 cbNode = GetClientCB(NULL, 0, NULL, requestUri);
55 #endif // WITH_PRESENCE
57 if(!cbNode)// If it does not already exist, create new node.
59 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
67 cbNode->callBack = cbData->cb;
68 cbNode->context = cbData->context;
69 cbNode->deleteCallback = cbData->cd;
70 //Note: token memory is allocated in the caller OCDoResource
71 //but freed in DeleteClientCB
72 cbNode->token = *token;
73 cbNode->tokenLength = tokenLength;
74 cbNode->handle = *handle;
75 cbNode->method = method;
76 cbNode->sequenceNumber = 0;
78 cbNode->presence = NULL;
79 cbNode->filterResourceType = NULL;
80 #endif // WITH_PRESENCE
81 cbNode->requestUri = requestUri;
82 LL_APPEND(cbList, cbNode);
88 // Ensure that the handle the SDK hands back up to the application layer for the
89 // OCDoResource call matches the found ClientCB Node.
94 cbData->cd(cbData->context);
100 *handle = cbNode->handle;
104 if(method == OC_REST_PRESENCE && resourceTypeName)
106 // Amend the found or created node by adding a new resourceType to it.
107 return InsertResourceTypeFilter(cbNode, resourceTypeName);
111 OCFree(resourceTypeName);
114 OCFree(resourceTypeName);
120 return OC_STACK_NO_MEMORY;
123 void DeleteClientCB(ClientCB * cbNode)
126 LL_DELETE(cbList, cbNode);
127 OC_LOG(INFO, TAG, PCF("deleting tokens"));
128 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, cbNode->tokenLength);
129 CADestroyToken (cbNode->token);
130 OCFree(cbNode->handle);
131 OCFree(cbNode->requestUri);
132 if(cbNode->deleteCallback)
134 cbNode->deleteCallback(cbNode->context);
138 if(cbNode->presence) {
139 OCFree(cbNode->presence->timeOut);
140 OCFree(cbNode->presence);
142 if(cbNode->method == OC_REST_PRESENCE)
144 OCResourceType * pointer = cbNode->filterResourceType;
145 OCResourceType * next = NULL;
148 next = pointer->next;
149 OCFree(pointer->resourcetypename);
154 #endif // WITH_PRESENCE
160 ClientCB* GetClientCB(const CAToken_t * token, uint8_t tokenLength,
161 OCDoHandle handle, const char * requestUri)
164 ClientCB* out = NULL;
166 if(token && *token &&
167 tokenLength <= CA_MAX_TOKEN_LEN && tokenLength > 0)
169 LL_FOREACH(cbList, out)
171 OC_LOG(INFO, TAG, PCF("comparing tokens"));
172 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, tokenLength);
173 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, tokenLength);
174 if(memcmp(out->token, *token, tokenLength) == 0)
182 LL_FOREACH(cbList, out)
184 if(out->handle == handle)
192 LL_FOREACH(cbList, out)
194 if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
200 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
205 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName)
207 OCResourceType * newResourceType = NULL;
208 if(cbNode && resourceTypeName)
210 // Form a new resourceType member.
211 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
214 return OC_STACK_NO_MEMORY;
217 newResourceType->next = NULL;
218 newResourceType->resourcetypename = resourceTypeName;
220 LL_APPEND(cbNode->filterResourceType, newResourceType);
223 return OC_STACK_ERROR;
225 #endif // WITH_PRESENCE
227 void DeleteClientCBList() {
230 LL_FOREACH_SAFE(cbList, out, tmp) {
236 void FindAndDeleteClientCB(ClientCB * cbNode) {
240 LL_FOREACH(cbList, tmp)
251 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
255 return OC_STACK_INVALID_PARAM;
258 OCMulticastNode *node;
260 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
265 LL_APPEND(mcPresenceNodes, node);
270 return OC_STACK_NO_MEMORY;
273 OCMulticastNode* GetMCPresenceNode(const char * uri) {
274 OCMulticastNode* out = NULL;
277 LL_FOREACH(mcPresenceNodes, out) {
278 if(out->uri && strcmp(out->uri, uri) == 0) {
283 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));