Changeset for reviewing RI-CA integration changes.
[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         OCFree(requestUri);
90         OCFree(*handle);
91         *handle = cbNode->handle;
92     }
93
94     #ifdef WITH_PRESENCE
95     if(method == OC_REST_PRESENCE && resourceTypeName)
96     {   // Amend the found or created node by adding a new resourceType to it.
97         return InsertResourceTypeFilter(cbNode, (const char *)resourceTypeName);
98     }
99     #endif
100
101     return OC_STACK_OK;
102
103     exit:
104         return OC_STACK_NO_MEMORY;
105 }
106
107 void DeleteClientCB(ClientCB * cbNode)
108 {
109     if(cbNode) {
110         LL_DELETE(cbList, cbNode);
111         OC_LOG(INFO, TAG, PCF("deleting tokens"));
112         CADestroyToken (cbNode->token);
113         OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)cbNode->token, CA_MAX_TOKEN_LEN);
114         OCFree(cbNode->handle);
115         OCFree(cbNode->requestUri);
116         if(cbNode->deleteCallback)
117         {
118             cbNode->deleteCallback(cbNode->context);
119         }
120
121         #ifdef WITH_PRESENCE
122         if(cbNode->presence) {
123             OCFree(cbNode->presence->timeOut);
124             OCFree(cbNode->presence);
125         }
126         if(cbNode->method == OC_REST_PRESENCE)
127         {
128             OCResourceType * pointer = cbNode->filterResourceType;
129             OCResourceType * next = NULL;
130             while(pointer)
131             {
132                 next = pointer->next;
133                 OCFree(pointer->resourcetypename);
134                 OCFree(pointer);
135                 pointer = next;
136             }
137         }
138         #endif // WITH_PRESENCE
139         OCFree(cbNode);
140         cbNode = NULL;
141     }
142 }
143
144 ClientCB* GetClientCB(const CAToken_t * token, OCDoHandle handle, const char * requestUri)
145 {
146     ClientCB* out = NULL;
147     if(token) {
148         LL_FOREACH(cbList, out) {
149             OC_LOG(INFO, TAG, PCF("comparing tokens"));
150             OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)*token, CA_MAX_TOKEN_LEN);
151             OC_LOG_BUFFER(INFO, TAG, (const uint8_t *)out->token, CA_MAX_TOKEN_LEN);
152             if(memcmp(out->token, *token, CA_MAX_TOKEN_LEN) == 0)
153             {
154                 return out;
155             }
156         }
157     }
158     else if(handle) {
159         LL_FOREACH(cbList, out) {
160             if(out->handle == handle) {
161                 return out;
162             }
163         }
164     }
165     else if(requestUri) {
166         LL_FOREACH(cbList, out) {
167             if(out->requestUri && strcmp(out->requestUri, requestUri ) == 0)
168             {
169                 return out;
170             }
171         }
172     }
173     OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
174     return NULL;
175 }
176
177 #ifdef WITH_PRESENCE
178 OCStackResult InsertResourceTypeFilter(ClientCB * cbNode, const char * resourceTypeName)
179 {
180     OCResourceType * newResourceType = NULL;
181     if(cbNode && resourceTypeName)
182     {
183         // Form a new resourceType member.
184         newResourceType = (OCResourceType *) OCMalloc(sizeof(OCResourceType));
185         if(!newResourceType)
186         {
187             return OC_STACK_NO_MEMORY;
188         }
189
190         newResourceType->next = NULL;
191         newResourceType->resourcetypename = (char *) resourceTypeName;
192
193         LL_APPEND(cbNode->filterResourceType, newResourceType);
194         return OC_STACK_OK;
195     }
196     return OC_STACK_ERROR;
197 }
198 #endif // WITH_PRESENCE
199
200 void DeleteClientCBList() {
201     ClientCB* out;
202     ClientCB* tmp;
203     LL_FOREACH_SAFE(cbList, out, tmp) {
204         DeleteClientCB(out);
205     }
206     cbList = NULL;
207 }
208
209 void FindAndDeleteClientCB(ClientCB * cbNode) {
210     ClientCB* tmp;
211     if(cbNode)
212     {
213         LL_FOREACH(cbList, tmp)
214         {
215             if (cbNode == tmp)
216             {
217                 DeleteClientCB(tmp);
218                 break;
219             }
220         }
221     }
222 }
223
224 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, char* uri, uint32_t nonce)
225 {
226     if(!outnode)
227     {
228         return OC_STACK_INVALID_PARAM;
229     }
230
231     OCMulticastNode *node;
232
233     node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
234
235     if (node) {
236         node->nonce = nonce;
237         node->uri = uri;
238         LL_APPEND(mcPresenceNodes, node);
239         *outnode = node;
240         return OC_STACK_OK;
241     }
242     *outnode = NULL;
243     return OC_STACK_NO_MEMORY;
244 }
245
246 OCMulticastNode* GetMCPresenceNode(const char * uri) {
247     OCMulticastNode* out = NULL;
248
249     if(uri) {
250         LL_FOREACH(mcPresenceNodes, out) {
251             if(out->uri && strcmp(out->uri, uri) == 0) {
252                 return out;
253             }
254         }
255     }
256     OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));
257     return NULL;
258 }
259