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 #define TAG PCF("occlientcb")
32 struct ClientCB *cbList = NULL;
33 OCMulticastNode * mcPresenceNodes = NULL;
35 OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
36 OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
37 unsigned char * requestUri, unsigned char * resourceTypeName) {
39 ClientCB *cbNode = NULL;
42 if(method == OC_REST_PRESENCE)
43 { // Retrieve the presence callback structure for this specific requestUri.
44 cbNode = GetClientCB(NULL, NULL, requestUri);
46 #endif // WITH_PRESENCE
48 if(!cbNode)// If it does not already exist, create new node.
50 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
58 cbNode->callBack = cbData->cb;
59 cbNode->context = cbData->context;
60 cbNode->deleteCallback = cbData->cd;
61 memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
62 cbNode->handle = *handle;
63 cbNode->method = method;
64 cbNode->sequenceNumber = 0;
66 cbNode->presence = NULL;
67 cbNode->filterResourceType = NULL;
68 #endif // WITH_PRESENCE
69 cbNode->requestUri = requestUri;
70 LL_APPEND(cbList, cbNode);
76 // Ensure that the handle the SDK hands back up to the application layer for the
77 // OCDoResource call matches the found ClientCB Node.
81 *handle = cbNode->handle;
85 if(method == OC_REST_PRESENCE && resourceTypeName)
86 { // Amend the found or created node by adding a new resourceType to it.
87 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
94 return OC_STACK_NO_MEMORY;
97 void DeleteClientCB(ClientCB * cbNode) {
99 LL_DELETE(cbList, cbNode);
100 OC_LOG(INFO, TAG, PCF("deleting tokens"));
101 OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
102 OCFree(cbNode->handle);
103 OCFree(cbNode->requestUri);
104 if(cbNode->deleteCallback)
106 cbNode->deleteCallback(cbNode->context);
110 if(cbNode->presence) {
111 OCFree(cbNode->presence->timeOut);
112 OCFree(cbNode->presence);
114 if(cbNode->method == OC_REST_PRESENCE)
116 OCResourceType * pointer = cbNode->filterResourceType;
117 OCResourceType * next = NULL;
120 next = pointer->next;
121 OCFree(pointer->resourcetypename);
126 #endif // WITH_PRESENCE
132 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, unsigned char * requestUri) {
133 ClientCB* out = NULL;
135 LL_FOREACH(cbList, out) {
136 OC_LOG(INFO, TAG, PCF("comparing tokens"));
137 OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
138 OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
139 if((out->token.tokenLength == token->tokenLength) &&
140 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
146 LL_FOREACH(cbList, out) {
147 if(out->handle == handle) {
152 else if(requestUri) {
153 LL_FOREACH(cbList, out) {
154 if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
159 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
163 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
165 OCResourceType * newResourceType = NULL;
166 if(cbNode && resourceTypeName)
168 // Form a new resourceType member.
169 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
172 return OC_STACK_NO_MEMORY;
175 newResourceType->next = NULL;
176 newResourceType->resourcetypename = (char *) resourceTypeName;
178 LL_APPEND(cbNode->filterResourceType, newResourceType);
181 return OC_STACK_ERROR;
184 void DeleteClientCBList() {
187 LL_FOREACH_SAFE(cbList, out, tmp) {
193 void FindAndDeleteClientCB(ClientCB * cbNode) {
197 LL_FOREACH(cbList, tmp)
208 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
210 OCMulticastNode *node;
212 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
217 LL_APPEND(mcPresenceNodes, node);
222 return OC_STACK_NO_MEMORY;
225 OCMulticastNode* GetMCPresenceNode(unsigned char * uri) {
226 OCMulticastNode* out = NULL;
229 LL_FOREACH(mcPresenceNodes, out) {
230 if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
235 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));