Removed unused macro.
[platform/upstream/iotivity.git] / resource / csdk / stack / src / occlientcb.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21
22 #include "occlientcb.h"
23 #include "utlist.h"
24 #include "logger.h"
25 #include "ocmalloc.h"
26 #include <string.h>
27
28 #include "cacommon.h"
29 #include "cainterface.h"
30
31 /// Module Name
32 #define TAG PCF("occlientcb")
33
34 struct ClientCB *cbList = NULL;
35 OCMulticastNode * mcPresenceNodes = NULL;
36
37 OCStackResult
38 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
39              CAToken_t * token, OCDoHandle *handle, OCMethod method,
40              unsigned char * requestUri, unsigned char * resourceTypeName)
41 {
42
43     ClientCB *cbNode = NULL;
44
45     #ifdef WITH_PRESENCE
46     if(method == OC_REST_PRESENCE)
47     {   // Retrieve the presence callback structure for this specific requestUri.
48         cbNode = GetClientCB(NULL, NULL, requestUri);
49     }
50     #endif // WITH_PRESENCE
51
52     if(!cbNode)// If it does not already exist, create new node.
53     {
54         cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
55         if(!cbNode)
56         {
57             *clientCB = NULL;
58             goto exit;
59         }
60         else
61         {
62             cbNode->callBack = cbData->cb;
63             cbNode->context = cbData->context;
64             cbNode->deleteCallback = cbData->cd;
65             //Note: token memory is allocated in the caller OCDoResource
66             //but freed in DeleteClientCB
67             cbNode->token = *token;
68             cbNode->handle = *handle;
69             cbNode->method = method;
70             cbNode->sequenceNumber = 0;
71             #ifdef WITH_PRESENCE
72             cbNode->presence = NULL;
73             cbNode->filterResourceType = NULL;
74             #endif // WITH_PRESENCE
75             cbNode->requestUri = requestUri;
76             LL_APPEND(cbList, cbNode);
77             *clientCB = cbNode;
78         }
79     }
80     else
81     {
82         // Ensure that the handle the SDK hands back up to the application layer for the
83         // OCDoResource call matches the found ClientCB Node.
84         *clientCB = cbNode;
85         OCFree(requestUri);
86         OCFree(*handle);
87         *handle = cbNode->handle;
88     }
89
90     #ifdef WITH_PRESENCE
91     if(method == OC_REST_PRESENCE && resourceTypeName)
92     {   // Amend the found or created node by adding a new resourceType to it.
93         return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
94     }
95     #endif
96
97     return OC_STACK_OK;
98
99     exit:
100         return OC_STACK_NO_MEMORY;
101 }
102
103 void DeleteClientCB(ClientCB * cbNode) {
104     if(cbNode) {
105         LL_DELETE(cbList, cbNode);
106         OC_LOG(INFO, TAG, PCF("deleting tokens"));
107         CADestroyToken (cbNode->token);
108         OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
109         OCFree(cbNode->handle);
110         OCFree(cbNode->requestUri);
111         if(cbNode->deleteCallback)
112         {
113             cbNode->deleteCallback(cbNode->context);
114         }
115
116         #ifdef WITH_PRESENCE
117         if(cbNode->presence) {
118             OCFree(cbNode->presence->timeOut);
119             OCFree(cbNode->presence);
120         }
121         if(cbNode->method == OC_REST_PRESENCE)
122         {
123             OCResourceType * pointer = cbNode->filterResourceType;
124             OCResourceType * next = NULL;
125             while(pointer)
126             {
127                 next = pointer->next;
128                 OCFree(pointer->resourcetypename);
129                 OCFree(pointer);
130                 pointer = next;
131             }
132         }
133         #endif // WITH_PRESENCE
134         OCFree(cbNode);
135         cbNode = NULL;
136     }
137 }
138
139 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
140 {
141     ClientCB* out = NULL;
142     if(token) {
143         LL_FOREACH(cbList, out) {
144             OC_LOG(INFO, TAG, PCF("comparing tokens"));
145             OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
146             OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
147             if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
148             {
149                 return out;
150             }
151         }
152     }
153     else if(handle) {
154         LL_FOREACH(cbList, out) {
155             if(out->handle == handle) {
156                 return out;
157             }
158         }
159     }
160     else if(requestUri) {
161         LL_FOREACH(cbList, out) {
162             if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
163                 return out;
164             }
165         }
166     }
167     OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
168     return NULL;
169 }
170
171 #ifdef WITH_PRESENCE
172 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
173 {
174     OCResourceType * newResourceType = NULL;
175     if(cbNode && resourceTypeName)
176     {
177         // Form a new resourceType member.
178         newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
179         if(!newResourceType)
180         {
181             return OC_STACK_NO_MEMORY;
182         }
183
184         newResourceType->next = NULL;
185         newResourceType->resourcetypename = (char *) resourceTypeName;
186
187         LL_APPEND(cbNode->filterResourceType, newResourceType);
188         return OC_STACK_OK;
189     }
190     return OC_STACK_ERROR;
191 }
192 #endif // WITH_PRESENCE
193
194 void DeleteClientCBList() {
195     ClientCB* out;
196     ClientCB* tmp;
197     LL_FOREACH_SAFE(cbList, out, tmp) {
198         DeleteClientCB(out);
199     }
200     cbList = NULL;
201 }
202
203 void FindAndDeleteClientCB(ClientCB * cbNode) {
204     ClientCB* tmp;
205     if(cbNode)
206     {
207         LL_FOREACH(cbList, tmp)
208         {
209             if (cbNode == tmp)
210             {
211                 DeleteClientCB(tmp);
212                 break;
213             }
214         }
215     }
216 }
217
218 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
219 {
220     OCMulticastNode *node;
221
222     node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
223
224     if (node) {
225         node->nonce = nonce;
226         node->uri = uri;
227         LL_APPEND(mcPresenceNodes, node);
228         *outnode = node;
229         return OC_STACK_OK;
230     }
231     *outnode = NULL;
232     return OC_STACK_NO_MEMORY;
233 }
234
235 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
236     OCMulticastNode* out = NULL;
237
238     if(uri) {
239         LL_FOREACH(mcPresenceNodes, out) {
240             if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
241                 return out;
242             }
243         }
244     }
245     OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));
246     return NULL;
247 }