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"
30 #include "cainterface.h"
33 #define TAG PCF("occlientcb")
35 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
37 struct ClientCB *cbList = NULL;
38 OCMulticastNode * mcPresenceNodes = NULL;
41 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
42 CAToken_t * token, OCDoHandle *handle, OCMethod method,
43 unsigned char * requestUri, unsigned char * resourceTypeName)
46 ClientCB *cbNode = NULL;
49 if(method == OC_REST_PRESENCE)
50 { // Retrieve the presence callback structure for this specific requestUri.
51 cbNode = GetClientCB(NULL, NULL, requestUri);
53 #endif // WITH_PRESENCE
55 if(!cbNode)// If it does not already exist, create new node.
57 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
65 cbNode->callBack = cbData->cb;
66 cbNode->context = cbData->context;
67 cbNode->deleteCallback = cbData->cd;
68 //Note: token memory is allocated in the caller OCDoResource
69 //but freed in DeleteClientCB
70 cbNode->token = *token;
71 cbNode->handle = *handle;
72 cbNode->method = method;
73 cbNode->sequenceNumber = 0;
75 cbNode->presence = NULL;
76 cbNode->filterResourceType = NULL;
77 #endif // WITH_PRESENCE
78 cbNode->requestUri = requestUri;
79 LL_APPEND(cbList, cbNode);
85 // Ensure that the handle the SDK hands back up to the application layer for the
86 // OCDoResource call matches the found ClientCB Node.
90 *handle = cbNode->handle;
94 if(method == OC_REST_PRESENCE && resourceTypeName)
95 { // Amend the found or created node by adding a new resourceType to it.
96 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
103 return OC_STACK_NO_MEMORY;
106 void DeleteClientCB(ClientCB * cbNode) {
108 LL_DELETE(cbList, cbNode);
109 OC_LOG(INFO, TAG, PCF("deleting tokens"));
110 CADestroyToken (cbNode->token);
111 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
112 OCFree(cbNode->handle);
113 OCFree(cbNode->requestUri);
114 if(cbNode->deleteCallback)
116 cbNode->deleteCallback(cbNode->context);
120 if(cbNode->presence) {
121 OCFree(cbNode->presence->timeOut);
122 OCFree(cbNode->presence);
124 if(cbNode->method == OC_REST_PRESENCE)
126 OCResourceType * pointer = cbNode->filterResourceType;
127 OCResourceType * next = NULL;
130 next = pointer->next;
131 OCFree(pointer->resourcetypename);
136 #endif // WITH_PRESENCE
142 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
144 ClientCB* out = NULL;
146 LL_FOREACH(cbList, out) {
147 OC_LOG(INFO, TAG, PCF("comparing tokens"));
148 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
149 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
150 if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
157 LL_FOREACH(cbList, out) {
158 if(out->handle == handle) {
163 else if(requestUri) {
164 LL_FOREACH(cbList, out) {
165 if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
170 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
175 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
177 OCResourceType * newResourceType = NULL;
178 if(cbNode && resourceTypeName)
180 // Form a new resourceType member.
181 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
184 return OC_STACK_NO_MEMORY;
187 newResourceType->next = NULL;
188 newResourceType->resourcetypename = (char *) resourceTypeName;
190 LL_APPEND(cbNode->filterResourceType, newResourceType);
193 return OC_STACK_ERROR;
195 #endif // WITH_PRESENCE
197 void DeleteClientCBList() {
200 LL_FOREACH_SAFE(cbList, out, tmp) {
206 void FindAndDeleteClientCB(ClientCB * cbNode) {
210 LL_FOREACH(cbList, tmp)
221 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
223 OCMulticastNode *node;
225 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
230 LL_APPEND(mcPresenceNodes, node);
235 return OC_STACK_NO_MEMORY;
238 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
239 OCMulticastNode* out = NULL;
242 LL_FOREACH(mcPresenceNodes, out) {
243 if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
248 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));