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