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"
31 #include "cainterface.h"
35 #define TAG PCF("occlientcb")
38 // TODO-CA: This has been defined in CA layer as well, but is not exposed externally.
39 // Need to expose it from CA and remove the definition below.
40 #define CA_MAX_TOKEN_LEN (8)
43 struct ClientCB *cbList = NULL;
44 OCMulticastNode * mcPresenceNodes = NULL;
48 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
49 CAToken_t * token, OCDoHandle *handle, OCMethod method,
50 unsigned char * requestUri, unsigned char * resourceTypeName)
52 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
53 OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
54 unsigned char * requestUri, unsigned char * resourceTypeName)
58 ClientCB *cbNode = NULL;
61 if(method == OC_REST_PRESENCE)
62 { // Retrieve the presence callback structure for this specific requestUri.
63 cbNode = GetClientCB(NULL, NULL, requestUri);
65 #endif // WITH_PRESENCE
67 if(!cbNode)// If it does not already exist, create new node.
69 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
77 cbNode->callBack = cbData->cb;
78 cbNode->context = cbData->context;
79 cbNode->deleteCallback = cbData->cd;
81 cbNode->token = (CAToken_t *)OCMalloc(CA_MAX_TOKEN_LEN);
82 memcpy(&(cbNode->token), token, CA_MAX_TOKEN_LEN);
84 memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
86 cbNode->handle = *handle;
87 cbNode->method = method;
88 cbNode->sequenceNumber = 0;
90 cbNode->presence = NULL;
91 cbNode->filterResourceType = NULL;
92 #endif // WITH_PRESENCE
93 cbNode->requestUri = requestUri;
94 LL_APPEND(cbList, cbNode);
100 // Ensure that the handle the SDK hands back up to the application layer for the
101 // OCDoResource call matches the found ClientCB Node.
105 *handle = cbNode->handle;
109 if(method == OC_REST_PRESENCE && resourceTypeName)
110 { // Amend the found or created node by adding a new resourceType to it.
111 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
118 return OC_STACK_NO_MEMORY;
121 void DeleteClientCB(ClientCB * cbNode) {
123 LL_DELETE(cbList, cbNode);
124 OC_LOG(INFO, TAG, PCF("deleting tokens"));
126 CADestroyToken (cbNode->token);
128 OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
130 OCFree(cbNode->handle);
131 OCFree(cbNode->requestUri);
132 if(cbNode->deleteCallback)
134 cbNode->deleteCallback(cbNode->context);
138 if(cbNode->presence) {
139 OCFree(cbNode->presence->timeOut);
140 OCFree(cbNode->presence);
142 if(cbNode->method == OC_REST_PRESENCE)
144 OCResourceType * pointer = cbNode->filterResourceType;
145 OCResourceType * next = NULL;
148 next = pointer->next;
149 OCFree(pointer->resourcetypename);
154 #endif // WITH_PRESENCE
161 ClientCB* GetClientCB(CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
163 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri)
166 ClientCB* out = NULL;
168 LL_FOREACH(cbList, out) {
169 OC_LOG(INFO, TAG, PCF("comparing tokens"));
171 if(memcmp(out->token, token, CA_MAX_TOKEN_LEN) == 0)
176 OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
177 OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
178 if((out->token.tokenLength == token->tokenLength) &&
179 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
186 LL_FOREACH(cbList, out) {
187 if(out->handle == handle) {
192 else if(requestUri) {
193 LL_FOREACH(cbList, out) {
194 if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
199 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
204 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
206 OCResourceType * newResourceType = NULL;
207 if(cbNode && resourceTypeName)
209 // Form a new resourceType member.
210 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
213 return OC_STACK_NO_MEMORY;
216 newResourceType->next = NULL;
217 newResourceType->resourcetypename = (char *) resourceTypeName;
219 LL_APPEND(cbNode->filterResourceType, newResourceType);
222 return OC_STACK_ERROR;
224 #endif // WITH_PRESENCE
226 void DeleteClientCBList() {
229 LL_FOREACH_SAFE(cbList, out, tmp) {
235 void FindAndDeleteClientCB(ClientCB * cbNode) {
239 LL_FOREACH(cbList, tmp)
250 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
252 OCMulticastNode *node;
254 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
259 LL_APPEND(mcPresenceNodes, node);
264 return OC_STACK_NO_MEMORY;
267 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
268 OCMulticastNode* out = NULL;
271 LL_FOREACH(mcPresenceNodes, out) {
272 if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
277 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));