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