Merge "Added support for multicast presence"
[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) {
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         #endif
51         cbNode->requestUri = requestUri;
52         LL_APPEND(cbList, cbNode);
53         *clientCB = cbNode;
54         return OC_STACK_OK;
55     }
56     *clientCB = NULL;
57     return OC_STACK_NO_MEMORY;
58 }
59
60 void DeleteClientCB(ClientCB * cbNode) {
61     if(cbNode) {
62         LL_DELETE(cbList, cbNode);
63         OC_LOG(INFO, TAG, PCF("deleting tokens"));
64         OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
65         OCFree(cbNode->handle);
66         OCFree(cbNode->requestUri);
67         if(cbNode->deleteCallback)
68         {
69             cbNode->deleteCallback(cbNode->context);
70         }
71
72         #ifdef WITH_PRESENCE
73         if(cbNode->presence) {
74             OCFree(cbNode->presence->timeOut);
75             OCFree(cbNode->presence);
76         }
77         #endif
78         OCFree(cbNode);
79         cbNode = NULL;
80     }
81 }
82
83 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, unsigned char * requestUri) {
84     ClientCB* out = NULL;
85     if(token) {
86         LL_FOREACH(cbList, out) {
87             OC_LOG(INFO, TAG, PCF("comparing tokens"));
88             OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
89             OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
90             if((out->token.tokenLength == token->tokenLength) &&
91                 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
92                 return out;
93             }
94         }
95     }
96     else if(handle) {
97         LL_FOREACH(cbList, out) {
98             if(out->handle == handle) {
99                 return out;
100             }
101         }
102     }
103     else if(requestUri) {
104         LL_FOREACH(cbList, out) {
105             if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
106                 return out;
107             }
108         }
109     }
110     OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
111     return NULL;
112 }
113
114
115 void DeleteClientCBList() {
116     ClientCB* out;
117     ClientCB* tmp;
118     LL_FOREACH_SAFE(cbList, out, tmp) {
119         DeleteClientCB(out);
120     }
121     cbList = NULL;
122 }
123
124 void FindAndDeleteClientCB(ClientCB * cbNode) {
125     ClientCB* tmp;
126     if(cbNode)
127     {
128         LL_FOREACH(cbList, tmp)
129         {
130             if (cbNode == tmp)
131             {
132                 DeleteClientCB(tmp);
133                 break;
134             }
135         }
136     }
137 }
138
139 OCStackResult AddMCPresenceNode(OCMulticastNode** outnode, unsigned char* uri, uint32_t nonce)
140 {
141     OCMulticastNode *node;
142
143     node = (OCMulticastNode*) OCMalloc(sizeof(OCMulticastNode));
144
145     if (node) {
146         node->nonce = nonce;
147         node->uri = uri;
148         LL_APPEND(mcPresenceNodes, node);
149         *outnode = node;
150         return OC_STACK_OK;
151     }
152     *outnode = NULL;
153     return OC_STACK_NO_MEMORY;
154 }
155
156 OCMulticastNode* GetMCPresenceNode(unsigned char * uri) {
157     OCMulticastNode* out = NULL;
158
159     if(uri) {
160         LL_FOREACH(mcPresenceNodes, out) {
161             if(out->uri && strcmp((char *)out->uri, (char *)uri) == 0) {
162                 return out;
163             }
164         }
165     }
166     OC_LOG(INFO, TAG, PCF("MulticastNode Not found !!"));
167     return NULL;
168 }