Updated Makefiles and CMakeLists.txt to point to resource, not oic-resource
[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
34 OCStackResult AddClientCB(ClientCB** clientCB, OCCallbackData* cbData,
35         OCCoAPToken * token, OCDoHandle handle, OCMethod method,
36         unsigned char * requestUri) {
37     ClientCB *cbNode;
38     cbNode = (ClientCB*) OCMalloc(sizeof(ClientCB));
39     if (cbNode) {
40         cbNode->callBack = cbData->cb;
41         cbNode->context = cbData->context;
42         cbNode->deleteCallback = cbData->cd;
43         memcpy(&(cbNode->token), token, sizeof(OCCoAPToken));
44         cbNode->handle = handle;
45         cbNode->method = method;
46         cbNode->sequenceNumber = 0;
47         #ifdef WITH_PRESENCE
48         cbNode->presence = NULL;
49         #endif
50         cbNode->requestUri = requestUri;
51         LL_APPEND(cbList, cbNode);
52         *clientCB = cbNode;
53         return OC_STACK_OK;
54     }
55     *clientCB = NULL;
56     return OC_STACK_NO_MEMORY;
57 }
58
59 void DeleteClientCB(ClientCB * cbNode) {
60     if(cbNode) {
61         LL_DELETE(cbList, cbNode);
62         OC_LOG(INFO, TAG, PCF("deleting tokens"));
63         OC_LOG_BUFFER(INFO, TAG, cbNode->token.token, cbNode->token.tokenLength);
64         OCFree(cbNode->handle);
65         OCFree(cbNode->requestUri);
66         if(cbNode->deleteCallback)
67         {
68             cbNode->deleteCallback(cbNode->context);
69         }
70
71         #ifdef WITH_PRESENCE
72         if(cbNode->presence) {
73             OCFree(cbNode->presence->timeOut);
74             OCFree(cbNode->presence);
75         }
76         #endif
77         OCFree(cbNode);
78         cbNode = NULL;
79     }
80 }
81
82 ClientCB* GetClientCB(OCCoAPToken * token, OCDoHandle handle, unsigned char * requestUri) {
83     ClientCB* out = NULL;
84     if(token) {
85         LL_FOREACH(cbList, out) {
86             OC_LOG(INFO, TAG, PCF("comparing tokens"));
87             OC_LOG_BUFFER(INFO, TAG, token->token, token->tokenLength);
88             OC_LOG_BUFFER(INFO, TAG, out->token.token, out->token.tokenLength);
89             if((out->token.tokenLength == token->tokenLength) &&
90                 (memcmp(out->token.token, token->token, token->tokenLength) == 0) ) {
91                 return out;
92             }
93         }
94     }
95     else if(handle) {
96         LL_FOREACH(cbList, out) {
97             if(out->handle == handle) {
98                 return out;
99             }
100         }
101     }
102     else if(requestUri) {
103         LL_FOREACH(cbList, out) {
104             if(out->requestUri && strcmp((char *)out->requestUri, (char *)requestUri) == 0) {
105                 return out;
106             }
107         }
108     }
109     OC_LOG(INFO, TAG, PCF("Callback Not found !!"));
110     return NULL;
111 }
112
113
114 void DeleteClientCBList() {
115     ClientCB* out;
116     ClientCB* tmp;
117     LL_FOREACH_SAFE(cbList, out, tmp) {
118         DeleteClientCB(out);
119     }
120     cbList = NULL;
121 }
122
123 void FindAndDeleteClientCB(ClientCB * cbNode) {
124     ClientCB* tmp;
125     if(cbNode)
126     {
127         LL_FOREACH(cbList, tmp)
128         {
129             if (cbNode == tmp)
130             {
131                 DeleteClientCB(tmp);
132                 break;
133             }
134         }
135     }
136 }
137