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 #define VERIFY_NON_NULL(arg) { if (!arg) {OC_LOG(FATAL, TAG, #arg " is NULL"); goto exit;} }
39 struct ClientCB *cbList = NULL;
40 OCMulticastNode * mcPresenceNodes = NULL;
44 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
45 CAToken_t * token, OCDoHandle *handle, OCMethod method,
46 unsigned char * requestUri, unsigned char * resourceTypeName)
48 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
49 OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
50 unsigned char * requestUri, unsigned char * resourceTypeName)
54 ClientCB *cbNode = NULL;
57 if(method == OC_REST_PRESENCE)
58 { // Retrieve the presence callback structure for this specific requestUri.
59 cbNode = GetClientCB(NULL, NULL, requestUri);
61 #endif // WITH_PRESENCE
63 if(!cbNode)// If it does not already exist, create new node.
65 cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
73 cbNode->callBack = cbData->cb;
74 cbNode->context = cbData->context;
75 cbNode->deleteCallback = cbData->cd;
77 //Note: token memory is allocated in the caller OCDoResource
78 //but freed in DeleteClientCB
79 cbNode->token = *token;
81 memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
83 cbNode->handle = *handle;
84 cbNode->method = method;
85 cbNode->sequenceNumber = 0;
87 cbNode->presence = NULL;
88 cbNode->filterResourceType = NULL;
89 #endif // WITH_PRESENCE
90 cbNode->requestUri = requestUri;
91 LL_APPEND(cbList, cbNode);
97 // Ensure that the handle the SDK hands back up to the application layer for the
98 // OCDoResource call matches the found ClientCB Node.
102 *handle = cbNode->handle;
106 if(method == OC_REST_PRESENCE && resourceTypeName)
107 { // Amend the found or created node by adding a new resourceType to it.
108 return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
115 return OC_STACK_NO_MEMORY;
118 void DeleteClientCB(ClientCB * cbNode) {
120 LL_DELETE(cbList, cbNode);
121 OC_LOG(INFO, TAG, PCF("deleting tokens"));
123 CADestroyToken (cbNode->token);
124 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
126 OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
128 OCFree(cbNode->handle);
129 OCFree(cbNode->requestUri);
130 if(cbNode->deleteCallback)
132 cbNode->deleteCallback(cbNode->context);
136 if(cbNode->presence) {
137 OCFree(cbNode->presence->timeOut);
138 OCFree(cbNode->presence);
140 if(cbNode->method == OC_REST_PRESENCE)
142 OCResourceType * pointer = cbNode->filterResourceType;
143 OCResourceType * next = NULL;
146 next = pointer->next;
147 OCFree(pointer->resourcetypename);
152 #endif // WITH_PRESENCE
159 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
161 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri)
164 ClientCB* out = NULL;
166 LL_FOREACH(cbList, out) {
167 OC_LOG(INFO, TAG, PCF("comparing tokens"));
169 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
170 OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
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 !!"));