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, OCDoHandle *handle, OCMethod method,
40 char * requestUri, char * resourceTypeName)
42 if(!clientCB || !cbData || !token || !handle || !requestUri)
44 return OC_STACK_INVALID_PARAM;
47 ClientCB *cbNode = NULL;
50 if(method == OC_REST_PRESENCE)
51 { // Retrieve the presence callback structure for this specific requestUri.
52 cbNode = GetClientCB(NULL, NULL, requestUri);
54 #endif // WITH_PRESENCE
56 if(!cbNode)// If it does not already exist, create new node.
58 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
66 cbNode->callBack = cbData->cb;
67 cbNode->context = cbData->context;
68 cbNode->deleteCallback = cbData->cd;
69 //Note: token memory is allocated in the caller OCDoResource
70 //but freed in DeleteClientCB
71 cbNode->token = *token;
72 cbNode->handle = *handle;
73 cbNode->method = method;
74 cbNode->sequenceNumber = 0;
76 cbNode->presence = NULL;
77 cbNode->filterResourceType = NULL;
78 #endif // WITH_PRESENCE
79 cbNode->requestUri = requestUri;
80 LL_APPEND(cbList, cbNode);
86 // Ensure that the handle the SDK hands back up to the application layer for the
87 // OCDoResource call matches the found ClientCB Node.
89 cbData->cd(cbData->context);
93 *handle = cbNode->handle;
97 if(method == OC_REST_PRESENCE && resourceTypeName)
99 // Amend the found or created node by adding a new resourceType to it.
100 return InsertResourceTypeFilter(cbNode, resourceTypeName);
104 OCFree(resourceTypeName);
107 OCFree(resourceTypeName);
113 return OC_STACK_NO_MEMORY;
116 void DeleteClientCB(ClientCB * cbNode)
119 LL_DELETE(cbList, cbNode);
120 OC_LOG(INFO, TAG, PCF("deleting tokens"));
121 CADestroyToken (cbNode->token);
122 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
123 OCFree(cbNode->handle);
124 OCFree(cbNode->requestUri);
125 if(cbNode->deleteCallback)
127 cbNode->deleteCallback(cbNode->context);
131 if(cbNode->presence) {
132 OCFree(cbNode->presence->timeOut);
133 OCFree(cbNode->presence);
135 if(cbNode->method == OC_REST_PRESENCE)
137 OCResourceType * pointer = cbNode->filterResourceType;
138 OCResourceType * next = NULL;
141 next = pointer->next;
142 OCFree(pointer->resourcetypename);
147 #endif // WITH_PRESENCE
153 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri)
155 ClientCB* out = NULL;
157 LL_FOREACH(cbList, out) {
158 OC_LOG(INFO, TAG, PCF("comparing tokens"));
159 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
160 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
161 if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
168 LL_FOREACH(cbList, out) {
169 if(out->handle == handle) {
174 else if(requestUri) {
175 LL_FOREACH(cbList, out) {
176 if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
182 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
187 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, char * resourceTypeName)
189 OCResourceType * newResourceType = NULL;
190 if(cbNode && resourceTypeName)
192 // Form a new resourceType member.
193 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
196 return OC_STACK_NO_MEMORY;
199 newResourceType->next = NULL;
200 newResourceType->resourcetypename = resourceTypeName;
202 LL_APPEND(cbNode->filterResourceType, newResourceType);
205 return OC_STACK_ERROR;
207 #endif // WITH_PRESENCE
209 void DeleteClientCBList() {
212 LL_FOREACH_SAFE(cbList, out, tmp) {
218 void FindAndDeleteClientCB(ClientCB * cbNode) {
222 LL_FOREACH(cbList, tmp)
233 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
237 return OC_STACK_INVALID_PARAM;
240 OCMulticastNode *node;
242 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
247 LL_APPEND(mcPresenceNodes, node);
252 return OC_STACK_NO_MEMORY;
255 OCMulticastNode* GetMCPresenceNode(const char * uri) {
256 OCMulticastNode* out = NULL;
259 LL_FOREACH(mcPresenceNodes, out) {
260 if(out->uri && strcmp(out->uri, uri) == 0) {
265 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));