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")
37 struct ClientCB *cbList = NULL;
38 OCMulticastNode * mcPresenceNodes = NULL;
42 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
43 CAToken_t * token, OCDoHandle *handle, OCMethod method,
44 unsigned char * requestUri, unsigned char * resourceTypeName)
46 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
47 OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
48 unsigned char * requestUri, unsigned char * resourceTypeName)
52 ClientCB *cbNode = NULL;
55 if(method == OC_REST_PRESENCE)
56 { // Retrieve the presence callback structure for this specific requestUri.
57 cbNode = GetClientCB(NULL, NULL, requestUri);
59 #endif // WITH_PRESENCE
61 if(!cbNode)// If it does not already exist, create new node.
63 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
71 cbNode->callBack = cbData->cb;
72 cbNode->context = cbData->context;
73 cbNode->deleteCallback = cbData->cd;
75 cbNode->token = (CAToken_t *)OCMalloc(CA_MAX_TOKEN_LEN);
76 memcpy(&(cbNode->token), token, CA_MAX_TOKEN_LEN);
78 memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
80 cbNode->handle = *handle;
81 cbNode->method = method;
82 cbNode->sequenceNumber = 0;
84 cbNode->presence = NULL;
85 cbNode->filterResourceType = NULL;
86 #endif // WITH_PRESENCE
87 cbNode->requestUri = requestUri;
88 LL_APPEND(cbList, cbNode);
94 // Ensure that the handle the SDK hands back up to the application layer for the
95 // OCDoResource call matches the found ClientCB Node.
99 *handle = cbNode->handle;
103 if(method == OC_REST_PRESENCE && resourceTypeName)
104 { // Amend the found or created node by adding a new resourceType to it.
105 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
112 return OC_STACK_NO_MEMORY;
115 void DeleteClientCB(ClientCB * cbNode) {
117 LL_DELETE(cbList, cbNode);
118 OC_LOG(INFO, TAG, PCF("deleting tokens"));
120 CADestroyToken (cbNode->token);
122 OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
124 OCFree(cbNode->handle);
125 OCFree(cbNode->requestUri);
126 if(cbNode->deleteCallback)
128 cbNode->deleteCallback(cbNode->context);
132 if(cbNode->presence) {
133 OCFree(cbNode->presence->timeOut);
134 OCFree(cbNode->presence);
136 if(cbNode->method == OC_REST_PRESENCE)
138 OCResourceType * pointer = cbNode->filterResourceType;
139 OCResourceType * next = NULL;
142 next = pointer->next;
143 OCFree(pointer->resourcetypename);
148 #endif // WITH_PRESENCE
155 ClientCB* GetClientCB(CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
157 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri)
160 ClientCB* out = NULL;
162 LL_FOREACH(cbList, out) {
163 OC_LOG(INFO, TAG, PCF("comparing tokens"));
165 if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
170 OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
171 OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
172 if((out->token.tokenLength == token->tokenLength) &&
173 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
180 LL_FOREACH(cbList, out) {
181 if(out->handle == handle) {
186 else if(requestUri) {
187 LL_FOREACH(cbList, out) {
188 if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
193 OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
198 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
200 OCResourceType * newResourceType = NULL;
201 if(cbNode && resourceTypeName)
203 // Form a new resourceType member.
204 newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
207 return OC_STACK_NO_MEMORY;
210 newResourceType->next = NULL;
211 newResourceType->resourcetypename = (char *) resourceTypeName;
213 LL_APPEND(cbNode->filterResourceType, newResourceType);
216 return OC_STACK_ERROR;
218 #endif // WITH_PRESENCE
220 void DeleteClientCBList() {
223 LL_FOREACH_SAFE(cbList, out, tmp) {
229 void FindAndDeleteClientCB(ClientCB * cbNode) {
233 LL_FOREACH(cbList, tmp)
244 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
246 OCMulticastNode *node;
248 node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
253 LL_APPEND(mcPresenceNodes, node);
258 return OC_STACK_NO_MEMORY;
261 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
262 OCMulticastNode* out = NULL;
265 LL_FOREACH(mcPresenceNodes, out) {
266 if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
271 OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));