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 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
36 struct ClientCB *cbList = NULL;
37 OCMulticastNode * mcPresenceNodes = NULL;
40 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
41 CAToken_t * token, OCDoHandle *handle, OCMethod method,
42 unsigned char * requestUri, unsigned char * resourceTypeName)
45 ClientCB *cbNode = NULL;
48 if(method == OC_REST_PRESENCE)
49 { // Retrieve the presence callback structure for this specific requestUri.
50 cbNode = GetClientCB(NULL, NULL, requestUri);
52 #endif // WITH_PRESENCE
54 if(!cbNode)// If it does not already exist, create new node.
56 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
64 cbNode->callBack = cbData->cb;
65 cbNode->context = cbData->context;
66 cbNode->deleteCallback = cbData->cd;
67 //Note: token memory is allocated in the caller OCDoResource
68 //but freed in DeleteClientCB
69 cbNode->token = *token;
70 cbNode->handle = *handle;
71 cbNode->method = method;
72 cbNode->sequenceNumber = 0;
74 cbNode->presence = NULL;
75 cbNode->filterResourceType = NULL;
76 #endif // WITH_PRESENCE
77 cbNode->requestUri = requestUri;
78 LL_APPEND(cbList, cbNode);
84 // Ensure that the handle the SDK hands back up to the application layer for the
85 // OCDoResource call matches the found ClientCB Node.
89 *handle = cbNode->handle;
93 if(method == OC_REST_PRESENCE && resourceTypeName)
94 { // Amend the found or created node by adding a new resourceType to it.
95 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
102 return OC_STACK_NO_MEMORY;
105 void DeleteClientCB(ClientCB * cbNode) {
107 LL_DELETE(cbList, cbNode);
108 OC_LOG(INFO, TAG, PCF("deleting tokens"));
109 CADestroyToken (cbNode->token);
110 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
111 OCFree(cbNode->handle);
112 OCFree(cbNode->requestUri);
113 if(cbNode->deleteCallback)
115 cbNode->deleteCallback(cbNode->context);
119 if(cbNode->presence) {
120 OCFree(cbNode->presence->timeOut);
121 OCFree(cbNode->presence);
123 if(cbNode->method == OC_REST_PRESENCE)
125 OCResourceType * pointer = cbNode->filterResourceType;
126 OCResourceType * next = NULL;
129 next = pointer->next;
130 OCFree(pointer->resourcetypename);
135 #endif // WITH_PRESENCE
141 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
143 ClientCB* out = NULL;
145 LL_FOREACH(cbList, out) {
146 OC_LOG(INFO, TAG, PCF("comparing tokens"));
147 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
148 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
149 if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
156 LL_FOREACH(cbList, out) {
157 if(out->handle == handle) {
162 else if(requestUri) {
163 LL_FOREACH(cbList, out) {
164 if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
169 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
174 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
176 OCResourceType * newResourceType = NULL;
177 if(cbNode && resourceTypeName)
179 // Form a new resourceType member.
180 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
183 return OC_STACK_NO_MEMORY;
186 newResourceType->next = NULL;
187 newResourceType->resourcetypename = (char *) resourceTypeName;
189 LL_APPEND(cbNode->filterResourceType, newResourceType);
192 return OC_STACK_ERROR;
194 #endif // WITH_PRESENCE
196 void DeleteClientCBList() {
199 LL_FOREACH_SAFE(cbList, out, tmp) {
205 void FindAndDeleteClientCB(ClientCB * cbNode) {
209 LL_FOREACH(cbList, tmp)
220 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
222 OCMulticastNode *node;
224 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
229 LL_APPEND(mcPresenceNodes, node);
234 return OC_STACK_NO_MEMORY;
237 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
238 OCMulticastNode* out = NULL;
241 LL_FOREACH(mcPresenceNodes, out) {
242 if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
247 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));