Update snapshot(2018-01-04)
[platform/upstream/iotivity.git] / plugins / src / pluginlist.c
1 //******************************************************************
2 //
3 // Copyright 2015 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 #include "pluginlist.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "zigbee_wrapper.h"
27 #include <coap/utlist.h>
28 #include "oic_malloc.h"
29 #include "oic_string.h"
30 #include "ocstack.h"
31 #include "logger.h"
32
33 #define TAG "pluginlist"
34
35 static PIPluginBase * pluginList = NULL;
36
37 OCStackResult AddPlugin (PIPluginBase * plugin)
38 {
39     if (!plugin)
40     {
41         return OC_STACK_INVALID_PARAM;
42     }
43
44     LL_APPEND(pluginList, plugin);
45     return OC_STACK_OK;
46 }
47
48 OCStackResult DeletePlugin(PIPluginBase * plugin)
49 {
50     OCStackResult result = OC_STACK_ERROR;
51     if (!plugin)
52     {
53         return OC_STACK_INVALID_PARAM;
54     }
55     DeleteResourceList(plugin);
56     LL_DELETE(pluginList, plugin);
57     if (plugin->type == PLUGIN_ZIGBEE)
58     {
59         result = ZigbeeStop((PIPlugin_Zigbee *) plugin);
60     }
61     return result;
62 }
63
64
65 OCStackResult DeletePluginList()
66 {
67     OCStackResult result = OC_STACK_OK;
68     PIPluginBase * out = NULL;
69     PIPluginBase * tmp = NULL;
70     LL_FOREACH_SAFE(pluginList, out, tmp)
71     {
72         result = DeletePlugin(out);
73         if (result != OC_STACK_OK)
74         {
75             break;
76         }
77     }
78     if (result == OC_STACK_OK)
79     {
80         pluginList = NULL;
81     }
82     return result;
83 }
84
85 OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
86                                     OCResourceHandle * resourceHandle)
87 {
88     if (!plugin || !resourceHandle || !piResource)
89     {
90         return OC_STACK_INVALID_PARAM;
91     }
92     PIResourceBase * out = NULL;
93     PIResourceBase * tmp = NULL;
94     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
95     {
96         if (out->piResource.resourceHandle == resourceHandle)
97         {
98             *piResource = (PIResource *) out;
99             return OC_STACK_OK;
100         }
101     }
102     return OC_STACK_NO_RESOURCE;
103 }
104
105 static bool ZigbeeStrEquals(const char * s1, const char * s2, size_t s1_length, size_t s2_length)
106 {
107     if (!s1 || !s2 || s1_length == 0 || s2_length == 0)
108     {
109         return false;
110     }
111     if (s1_length == s2_length && memcmp(s1, s2, (s1_length + 1)) == 0)
112     {
113         return true;
114     }
115     return false;
116 }
117
118 OCStackResult GetResourceFromZigBeeNodeId(PIPluginBase * plugin,
119                                           PIResource_Zigbee ** piResource,
120                                           const char * nodeId,
121                                           const char * endpointId,
122                                           const char * clusterId)
123 {
124     OIC_LOG(INFO, TAG, "Entered GetResourceFromZigBeeNodeId().");
125     if(!plugin || !piResource || !nodeId || !clusterId || !endpointId)
126     {
127         OIC_LOG(ERROR, TAG, "Invalid param.");
128         return OC_STACK_INVALID_PARAM;
129     }
130     if(plugin->type != PLUGIN_ZIGBEE)
131     {
132         OIC_LOG(ERROR, TAG, "Plugin Type is not Zigbee.");
133         return OC_STACK_INVALID_PARAM;
134     }
135
136     PIResourceBase * out = NULL;
137     PIResourceBase * tmp = NULL;
138     size_t checkNodeIdLength = strlen(nodeId);
139     size_t checkEndpointIdLength = strlen(endpointId);
140     size_t checkClusterIdLength = strlen(clusterId);
141     size_t indexLength = 0;
142     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
143     {
144         indexLength = strlen(((PIResource_Zigbee *)out)->nodeId);
145         if(ZigbeeStrEquals(nodeId,
146                            ((PIResource_Zigbee *)out)->nodeId,
147                            checkNodeIdLength,
148                            indexLength) == false)
149         {
150             continue;
151         }
152         indexLength = strlen(((PIResource_Zigbee *)out)->endpointId);
153         if(ZigbeeStrEquals(endpointId,
154                            ((PIResource_Zigbee *)out)->endpointId,
155                            checkEndpointIdLength,
156                            indexLength) == false)
157         {
158             continue;
159         }
160         indexLength = strlen(((PIResource_Zigbee *)out)->clusterId);
161         if(ZigbeeStrEquals(clusterId,
162                            ((PIResource_Zigbee *)out)->clusterId,
163                            checkClusterIdLength,
164                            indexLength) == false)
165         {
166             continue;
167         }
168         OIC_LOG_V(INFO, TAG, "Found a match! URI = %s", out->piResource.uri);
169         *piResource = (PIResource_Zigbee *) out;
170         return OC_STACK_OK;
171     }
172     *piResource = NULL;
173     return OC_STACK_NO_RESOURCE;
174 }
175
176 OCStackResult AddResourceToPlugin (PIPluginBase * plugin, PIResourceBase * resource)
177 {
178     if (!plugin || !resource)
179     {
180         return OC_STACK_INVALID_PARAM;
181     }
182
183     LL_APPEND(plugin->resourceList, resource);
184
185     return OC_STACK_NO_MEMORY;
186 }
187
188 OCStackResult UpdateZigbeeResourceNodeId(PIPluginBase * plugin,
189                                          const char * eui,
190                                          const char * nodeId)
191 {
192     if (!plugin || !eui || !nodeId)
193     {
194         return OC_STACK_INVALID_PARAM;
195     }
196     if(plugin->type != PLUGIN_ZIGBEE)
197     {
198         return OC_STACK_INVALID_PARAM;
199     }
200     PIResourceBase * out = NULL;
201     PIResourceBase * tmp = NULL;
202     size_t checkLength = strlen(eui);
203     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
204     {
205         size_t indexLength = strlen(((PIResource_Zigbee *)out)->eui);
206         if(ZigbeeStrEquals(eui, ((PIResource_Zigbee *)out)->eui, checkLength, indexLength) != true)
207         {
208             continue;
209         }
210         OICStrcpy(((PIResource_Zigbee *)out)->nodeId, (strlen(nodeId)+1)*sizeof(char), nodeId);
211     }
212     return OC_STACK_OK;
213 }
214
215 OCStackResult DeleteResource(PIPluginBase * plugin, PIResourceBase * resource)
216 {
217     if (!plugin || !resource)
218     {
219         return OC_STACK_INVALID_PARAM;
220     }
221
222     //Todo: Free all of resource allocations.
223     PIResourceBase * resourceList = ((PIResourceBase *) plugin->resourceList);
224
225     LL_DELETE(resourceList, resource);
226
227     OCStackResult result = OCDeleteResource(resource->piResource.resourceHandle);
228     if(result != OC_STACK_OK)
229     {
230         OIC_LOG_V(ERROR, TAG, "Failed to delete resource with error: %d", result);
231         return result;
232     }
233
234     OICFree (resource->piResource.uri);
235     if (plugin->type == PLUGIN_ZIGBEE)
236     {
237         OICFree (((PIResource_Zigbee *)resource)->eui);
238         OICFree (((PIResource_Zigbee *)resource)->nodeId);
239         OICFree (((PIResource_Zigbee *)resource)->endpointId);
240         OICFree (((PIResource_Zigbee *)resource)->clusterId);
241     }
242     OICFree (resource);
243     return OC_STACK_OK;
244 }
245
246 OCStackResult DeleteResourceList(PIPluginBase * plugin)
247 {
248     if (!plugin)
249     {
250         return OC_STACK_INVALID_PARAM;
251     }
252
253     OCStackResult result = OC_STACK_OK;
254     PIResourceBase * out = NULL;
255     PIResourceBase * tmp = NULL;
256
257     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
258     {
259         result = DeleteResource(plugin, out);
260         if (result != OC_STACK_OK)
261         {
262             break;
263         }
264     }
265     if (result == OC_STACK_OK)
266     {
267         plugin->resourceList = NULL;
268     }
269     return result;
270 }