1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "pluginlist.h"
26 #include "zigbee_wrapper.h"
27 #include <coap/utlist.h>
28 #include "oic_malloc.h"
29 #include "oic_string.h"
33 #define TAG "pluginlist"
35 static PIPluginBase * pluginList = NULL;
37 OCStackResult AddPlugin (PIPluginBase * plugin)
41 return OC_STACK_INVALID_PARAM;
44 LL_APPEND(pluginList, plugin);
48 OCStackResult DeletePlugin(PIPluginBase * plugin)
50 OCStackResult result = OC_STACK_ERROR;
53 return OC_STACK_INVALID_PARAM;
55 DeleteResourceList(plugin);
56 LL_DELETE(pluginList, plugin);
57 if (plugin->type == PLUGIN_ZIGBEE)
59 result = ZigbeeStop((PIPlugin_Zigbee *) plugin);
65 OCStackResult DeletePluginList()
67 OCStackResult result = OC_STACK_OK;
68 PIPluginBase * out = NULL;
69 PIPluginBase * tmp = NULL;
70 LL_FOREACH_SAFE(pluginList, out, tmp)
72 result = DeletePlugin(out);
73 if (result != OC_STACK_OK)
78 if (result == OC_STACK_OK)
85 OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
86 OCResourceHandle * resourceHandle)
88 if (!plugin || !resourceHandle || !piResource)
90 return OC_STACK_INVALID_PARAM;
92 PIResourceBase * out = NULL;
93 PIResourceBase * tmp = NULL;
94 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
96 if (out->piResource.resourceHandle == resourceHandle)
98 *piResource = (PIResource *) out;
102 return OC_STACK_NO_RESOURCE;
105 static bool ZigbeeStrEquals(const char * s1, const char * s2, size_t s1_length, size_t s2_length)
107 if (!s1 || !s2 || s1_length == 0 || s2_length == 0)
111 if (s1_length == s2_length && memcmp(s1, s2, (s1_length + 1)) == 0)
118 OCStackResult GetResourceFromZigBeeNodeId(PIPluginBase * plugin,
119 PIResource_Zigbee ** piResource,
121 const char * endpointId,
122 const char * clusterId)
124 OIC_LOG(INFO, TAG, "Entered GetResourceFromZigBeeNodeId().");
125 if(!plugin || !piResource || !nodeId || !clusterId || !endpointId)
127 OIC_LOG(ERROR, TAG, "Invalid param.");
128 return OC_STACK_INVALID_PARAM;
130 if(plugin->type != PLUGIN_ZIGBEE)
132 OIC_LOG(ERROR, TAG, "Plugin Type is not Zigbee.");
133 return OC_STACK_INVALID_PARAM;
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)
144 indexLength = strlen(((PIResource_Zigbee *)out)->nodeId);
145 if(ZigbeeStrEquals(nodeId,
146 ((PIResource_Zigbee *)out)->nodeId,
148 indexLength) == false)
152 indexLength = strlen(((PIResource_Zigbee *)out)->endpointId);
153 if(ZigbeeStrEquals(endpointId,
154 ((PIResource_Zigbee *)out)->endpointId,
155 checkEndpointIdLength,
156 indexLength) == false)
160 indexLength = strlen(((PIResource_Zigbee *)out)->clusterId);
161 if(ZigbeeStrEquals(clusterId,
162 ((PIResource_Zigbee *)out)->clusterId,
163 checkClusterIdLength,
164 indexLength) == false)
168 OIC_LOG_V(INFO, TAG, "Found a match! URI = %s", out->piResource.uri);
169 *piResource = (PIResource_Zigbee *) out;
173 return OC_STACK_NO_RESOURCE;
176 OCStackResult AddResourceToPlugin (PIPluginBase * plugin, PIResourceBase * resource)
178 if (!plugin || !resource)
180 return OC_STACK_INVALID_PARAM;
183 LL_APPEND(plugin->resourceList, resource);
185 return OC_STACK_NO_MEMORY;
188 OCStackResult UpdateZigbeeResourceNodeId(PIPluginBase * plugin,
192 if (!plugin || !eui || !nodeId)
194 return OC_STACK_INVALID_PARAM;
196 if(plugin->type != PLUGIN_ZIGBEE)
198 return OC_STACK_INVALID_PARAM;
200 PIResourceBase * out = NULL;
201 PIResourceBase * tmp = NULL;
202 size_t checkLength = strlen(eui);
203 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
205 size_t indexLength = strlen(((PIResource_Zigbee *)out)->eui);
206 if(ZigbeeStrEquals(eui, ((PIResource_Zigbee *)out)->eui, checkLength, indexLength) != true)
210 OICStrcpy(((PIResource_Zigbee *)out)->nodeId, (strlen(nodeId)+1)*sizeof(char), nodeId);
215 OCStackResult DeleteResource(PIPluginBase * plugin, PIResourceBase * resource)
217 if (!plugin || !resource)
219 return OC_STACK_INVALID_PARAM;
222 //Todo: Free all of resource allocations.
223 PIResourceBase * resourceList = ((PIResourceBase *) plugin->resourceList);
225 LL_DELETE(resourceList, resource);
227 OCStackResult result = OCDeleteResource(resource->piResource.resourceHandle);
228 if(result != OC_STACK_OK)
230 OIC_LOG_V(ERROR, TAG, "Failed to delete resource with error: %d", result);
234 OICFree (resource->piResource.uri);
235 if (plugin->type == PLUGIN_ZIGBEE)
237 OICFree (((PIResource_Zigbee *)resource)->eui);
238 OICFree (((PIResource_Zigbee *)resource)->nodeId);
239 OICFree (((PIResource_Zigbee *)resource)->endpointId);
240 OICFree (((PIResource_Zigbee *)resource)->clusterId);
246 OCStackResult DeleteResourceList(PIPluginBase * plugin)
250 return OC_STACK_INVALID_PARAM;
253 OCStackResult result = OC_STACK_OK;
254 PIResourceBase * out = NULL;
255 PIResourceBase * tmp = NULL;
257 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
259 result = DeleteResource(plugin, out);
260 if (result != OC_STACK_OK)
265 if (result == OC_STACK_OK)
267 plugin->resourceList = NULL;