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