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 OCMulticastNode * mcPresenceNodes = NULL;
38 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
39 CAToken_t * token, OCDoHandle *handle, OCMethod method,
40 unsigned char * requestUri, unsigned char * resourceTypeName)
43 ClientCB *cbNode = NULL;
46 if(method == OC_REST_PRESENCE)
47 { // Retrieve the presence callback structure for this specific requestUri.
48 cbNode = GetClientCB(NULL, NULL, requestUri);
50 #endif // WITH_PRESENCE
52 if(!cbNode)// If it does not already exist, create new node.
54 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
62 cbNode->callBack = cbData->cb;
63 cbNode->context = cbData->context;
64 cbNode->deleteCallback = cbData->cd;
65 //Note: token memory is allocated in the caller OCDoResource
66 //but freed in DeleteClientCB
67 cbNode->token = *token;
68 cbNode->handle = *handle;
69 cbNode->method = method;
70 cbNode->sequenceNumber = 0;
72 cbNode->presence = NULL;
73 cbNode->filterResourceType = NULL;
74 #endif // WITH_PRESENCE
75 cbNode->requestUri = requestUri;
76 LL_APPEND(cbList, cbNode);
82 // Ensure that the handle the SDK hands back up to the application layer for the
83 // OCDoResource call matches the found ClientCB Node.
87 *handle = cbNode->handle;
91 if(method == OC_REST_PRESENCE && resourceTypeName)
92 { // Amend the found or created node by adding a new resourceType to it.
93 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
100 return OC_STACK_NO_MEMORY;
103 void DeleteClientCB(ClientCB * cbNode) {
105 LL_DELETE(cbList, cbNode);
106 OC_LOG(INFO, TAG, PCF("deleting tokens"));
107 CADestroyToken (cbNode->token);
108 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
109 OCFree(cbNode->handle);
110 OCFree(cbNode->requestUri);
111 if(cbNode->deleteCallback)
113 cbNode->deleteCallback(cbNode->context);
117 if(cbNode->presence) {
118 OCFree(cbNode->presence->timeOut);
119 OCFree(cbNode->presence);
121 if(cbNode->method == OC_REST_PRESENCE)
123 OCResourceType * pointer = cbNode->filterResourceType;
124 OCResourceType * next = NULL;
127 next = pointer->next;
128 OCFree(pointer->resourcetypename);
133 #endif // WITH_PRESENCE
139 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
141 ClientCB* out = NULL;
143 LL_FOREACH(cbList, out) {
144 OC_LOG(INFO, TAG, PCF("comparing tokens"));
145 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
146 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
147 if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
154 LL_FOREACH(cbList, out) {
155 if(out->handle == handle) {
160 else if(requestUri) {
161 LL_FOREACH(cbList, out) {
162 if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
167 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
172 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
174 OCResourceType * newResourceType = NULL;
175 if(cbNode && resourceTypeName)
177 // Form a new resourceType member.
178 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
181 return OC_STACK_NO_MEMORY;
184 newResourceType->next = NULL;
185 newResourceType->resourcetypename = (char *) resourceTypeName;
187 LL_APPEND(cbNode->filterResourceType, newResourceType);
190 return OC_STACK_ERROR;
192 #endif // WITH_PRESENCE
194 void DeleteClientCBList() {
197 LL_FOREACH_SAFE(cbList, out, tmp) {
203 void FindAndDeleteClientCB(ClientCB * cbNode) {
207 LL_FOREACH(cbList, tmp)
218 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
220 OCMulticastNode *node;
222 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
227 LL_APPEND(mcPresenceNodes, node);
232 return OC_STACK_NO_MEMORY;
235 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
236 OCMulticastNode* out = NULL;
239 LL_FOREACH(mcPresenceNodes, out) {
240 if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
245 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));