Merge "Active Discovery Resource Type Filtering - C SDK & App Changes only."
[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 /// Module Name
30 #define TAG PCF("occlientcb")
31
32 struct ClientCB *cbList = NULL;
33 OCMulticastNode * mcPresenceNodes = NULL;
34
35 OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
36         OCCoAPToken * token, OCDoHandle handle, OCMethod method,
37         unsigned char * requestUri, unsigned char * resourceType) {
38     ClientCB *cbNode;
39     cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
40     if (cbNode) {
41         cbNode->callBack = cbData->cb;
42         cbNode->context = cbData->context;
43         cbNode->deleteCallback = cbData->cd;
44         memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
45         cbNode->handle = handle;
46         cbNode->method = method;
47         cbNode->sequenceNumber = 0;
48         #ifdef WITH_PRESENCE
49         cbNode->presence = NULL;
50         cbNode->filterResourceType = resourceType;
51         #endif
52         cbNode->requestUri = requestUri;
53         LL_APPEND(cbList, cbNode);
54         *clientCB = cbNode;
55         return OC_STACK_OK;
56     }
57     *clientCB = NULL;
58     return OC_STACK_NO_MEMORY;
59 }
60
61 void DeleteClientCB(ClientCB * cbNode) {
62     if(cbNode) {
63         LL_DELETE(cbList, cbNode);
64         OC_LOG(INFO, TAG, PCF("deleting tokens"));
65         OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
66         OCFree(cbNode->handle);
67         OCFree(cbNode->requestUri);
68         if(cbNode->deleteCallback)
69         {
70             cbNode->deleteCallback(cbNode->context);
71         }
72
73         #ifdef WITH_PRESENCE
74         if(cbNode->presence) {
75             OCFree(cbNode->presence->timeOut);
76             OCFree(cbNode->presence);
77             OCFree(cbNode->filterResourceType);
78         }
79         #endif
80         OCFree(cbNode);
81         cbNode = NULL;
82     }
83 }
84
85 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, unsigned char * requestUri) {
86     ClientCB* out = NULL;
87     if(token) {
88         LL_FOREACH(cbList, out) {
89             OC_LOG(INFO, TAG, PCF("comparing tokens"));
90             OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
91             OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
92             if((out->token.tokenLength == token->tokenLength) &&
93                 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
94                 return out;
95             }
96         }
97     }
98     else if(handle) {
99         LL_FOREACH(cbList, out) {
100             if(out->handle == handle) {
101                 return out;
102             }
103         }
104     }
105     else if(requestUri) {
106         LL_FOREACH(cbList, out) {
107             if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
108                 return out;
109             }
110         }
111     }
112     OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
113     return NULL;
114 }
115
116
117 void DeleteClientCBList() {
118     ClientCB* out;
119     ClientCB* tmp;
120     LL_FOREACH_SAFE(cbList, out, tmp) {
121         DeleteClientCB(out);
122     }
123     cbList = NULL;
124 }
125
126 void FindAndDeleteClientCB(ClientCB * cbNode) {
127     ClientCB* tmp;
128     if(cbNode)
129     {
130         LL_FOREACH(cbList, tmp)
131         {
132             if (cbNode == tmp)
133             {
134                 DeleteClientCB(tmp);
135                 break;
136             }
137         }
138     }
139 }
140
141 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
142 {
143     OCMulticastNode *node;
144
145     node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
146
147     if (node) {
148         node->nonce = nonce;
149         node->uri = uri;
150         LL_APPEND(mcPresenceNodes, node);
151         *outnode = node;
152         return OC_STACK_OK;
153     }
154     *outnode = NULL;
155     return OC_STACK_NO_MEMORY;
156 }
157
158 OCMulticastNode* GetMCPresenceNode(unsigned char * uri) {
159     OCMulticastNode* out = NULL;
160
161     if(uri) {
162         LL_FOREACH(mcPresenceNodes, out) {
163             if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
164                 return out;
165             }
166         }
167     }
168     OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));
169     return NULL;
170 }