CA Integration: Enabling OCDoResource to send messages
[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 "occoap.h"
24 #include "utlist.h"
25 #include "logger.h"
26 #include "ocmalloc.h"
27 #include <string.h>
28
29 #ifdef CA_INT
30     #include "cacommon.h"
31     #include "cainterface.h"
32 #endif
33
34 /// Module Name
35 #define TAG PCF("occlientcb")
36
37 #ifdef CA_INT
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)
41 #endif //CA_INT
42
43 struct ClientCB *cbList = NULL;
44 OCMulticastNode * mcPresenceNodes = NULL;
45
46 OCStackResult
47 #ifdef CA_INT
48 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
49              CAToken_t * token, OCDoHandle *handle, OCMethod method,
50              unsigned char * requestUri, unsigned char * resourceTypeName)
51 #else // CA_INT
52 AddClientCB (ClientCB** clientCB, OCCallbackData* cbData,
53              OCCoAPToken * token, OCDoHandle *handle, OCMethod method,
54              unsigned char * requestUri, unsigned char * resourceTypeName)
55 #endif // CA_INT
56 {
57
58     ClientCB *cbNode = NULL;
59
60     #ifdef WITH_PRESENCE
61     if(method == OC_REST_PRESENCE)
62     {   // Retrieve the presence callback structure for this specific requestUri.
63         cbNode = GetClientCB(NULL, NULL, requestUri);
64     }
65     #endif // WITH_PRESENCE
66
67     if(!cbNode)// If it does not already exist, create new node.
68     {
69         cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
70         if(!cbNode)
71         {
72             *clientCB = NULL;
73             goto exit;
74         }
75         else
76         {
77             cbNode->callBack = cbData->cb;
78             cbNode->context = cbData->context;
79             cbNode->deleteCallback = cbData->cd;
80             #ifdef CA_INT
81             cbNode->token = (CAToken_t *)OCMalloc(CA_MAX_TOKEN_LEN);
82             memcpy(&(cbNode->token), token, CA_MAX_TOKEN_LEN);
83             #else // CA_INT
84             memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
85             #endif // CA_INT
86             cbNode->handle = *handle;
87             cbNode->method = method;
88             cbNode->sequenceNumber = 0;
89             #ifdef WITH_PRESENCE
90             cbNode->presence = NULL;
91             cbNode->filterResourceType = NULL;
92             #endif // WITH_PRESENCE
93             cbNode->requestUri = requestUri;
94             LL_APPEND(cbList, cbNode);
95             *clientCB = cbNode;
96         }
97     }
98     else
99     {
100         // Ensure that the handle the SDK hands back up to the application layer for the
101         // OCDoResource call matches the found ClientCB Node.
102         *clientCB = cbNode;
103         OCFree(requestUri);
104         OCFree(*handle);
105         *handle = cbNode->handle;
106     }
107
108     #ifdef WITH_PRESENCE
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);
112     }
113     #endif
114
115     return OC_STACK_OK;
116
117     exit:
118         return OC_STACK_NO_MEMORY;
119 }
120
121 void DeleteClientCB(ClientCB * cbNode) {
122     if(cbNode) {
123         LL_DELETE(cbList, cbNode);
124         OC_LOG(INFO, TAG, PCF("deleting tokens"));
125         #ifdef CA_INT
126         CADestroyToken (cbNode->token);
127         #else
128         OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
129         #endif // CA_INT
130         OCFree(cbNode->handle);
131         OCFree(cbNode->requestUri);
132         if(cbNode->deleteCallback)
133         {
134             cbNode->deleteCallback(cbNode->context);
135         }
136
137         #ifdef WITH_PRESENCE
138         if(cbNode->presence) {
139             OCFree(cbNode->presence->timeOut);
140             OCFree(cbNode->presence);
141         }
142         if(cbNode->method == OC_REST_PRESENCE)
143         {
144             OCResourceType * pointer = cbNode->filterResourceType;
145             OCResourceType * next = NULL;
146             while(pointer)
147             {
148                 next = pointer->next;
149                 OCFree(pointer->resourcetypename);
150                 OCFree(pointer);
151                 pointer = next;
152             }
153         }
154         #endif // WITH_PRESENCE
155         OCFree(cbNode);
156         cbNode = NULL;
157     }
158 }
159
160 #ifdef CA_INT
161 ClientCB* GetClientCB(CAToken_t * token, OCDoHandle handle, const unsigned char * requestUri)
162 #else // CA_INT
163 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, const unsigned char * requestUri)
164 #endif // CA_INT
165 {
166     ClientCB* out = NULL;
167     if(token) {
168         LL_FOREACH(cbList, out) {
169             OC_LOG(INFO, TAG, PCF("comparing tokens"));
170             #ifdef CA_INT
171             if(memcmp(out->token, token, CA_MAX_TOKEN_LEN) == 0)
172             {
173                 return out;
174             }
175             #else // CA_INT
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) ) {
180                 return out;
181             }
182             #endif // CA_INT
183         }
184     }
185     else if(handle) {
186         LL_FOREACH(cbList, out) {
187             if(out->handle == handle) {
188                 return out;
189             }
190         }
191     }
192     else if(requestUri) {
193         LL_FOREACH(cbList, out) {
194             if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
195                 return out;
196             }
197         }
198     }
199     OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
200     return NULL;
201 }
202
203 #ifdef WITH_PRESENCE
204 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
205 {
206     OCResourceType * newResourceType = NULL;
207     if(cbNode && resourceTypeName)
208     {
209         // Form a new resourceType member.
210         newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
211         if(!newResourceType)
212         {
213             return OC_STACK_NO_MEMORY;
214         }
215
216         newResourceType->next = NULL;
217         newResourceType->resourcetypename = (char *) resourceTypeName;
218
219         LL_APPEND(cbNode->filterResourceType, newResourceType);
220         return OC_STACK_OK;
221     }
222     return OC_STACK_ERROR;
223 }
224 #endif // WITH_PRESENCE
225
226 void DeleteClientCBList() {
227     ClientCB* out;
228     ClientCB* tmp;
229     LL_FOREACH_SAFE(cbList, out, tmp) {
230         DeleteClientCB(out);
231     }
232     cbList = NULL;
233 }
234
235 void FindAndDeleteClientCB(ClientCB * cbNode) {
236     ClientCB* tmp;
237     if(cbNode)
238     {
239         LL_FOREACH(cbList, tmp)
240         {
241             if (cbNode == tmp)
242             {
243                 DeleteClientCB(tmp);
244                 break;
245             }
246         }
247     }
248 }
249
250 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
251 {
252     OCMulticastNode *node;
253
254     node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
255
256     if (node) {
257         node->nonce = nonce;
258         node->uri = uri;
259         LL_APPEND(mcPresenceNodes, node);
260         *outnode = node;
261         return OC_STACK_OK;
262     }
263     *outnode = NULL;
264     return OC_STACK_NO_MEMORY;
265 }
266
267 OCMulticastNode* GetMCPresenceNode(const unsigned char * uri) {
268     OCMulticastNode* out = NULL;
269
270     if(uri) {
271         LL_FOREACH(mcPresenceNodes, out) {
272             if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
273                 return out;
274             }
275         }
276     }
277     OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));
278     return NULL;
279 }