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"
28 #include "oic_malloc.h"
32 #define TAG "pluginlist"
34 static PIPluginBase * pluginList = NULL;
36 OCStackResult AddPlugin (PIPluginBase * plugin)
40 return OC_STACK_INVALID_PARAM;
43 LL_APPEND(pluginList, plugin);
47 OCStackResult DeletePlugin(PIPluginBase * plugin)
49 OCStackResult result = OC_STACK_ERROR;
52 return OC_STACK_INVALID_PARAM;
54 DeleteResourceList(plugin);
55 LL_DELETE(pluginList, plugin);
56 if (plugin->type == PLUGIN_ZIGBEE)
58 result = ZigbeeStop((PIPlugin_Zigbee *) plugin);
64 OCStackResult DeletePluginList()
66 OCStackResult result = OC_STACK_OK;
67 PIPluginBase * out = NULL;
68 PIPluginBase * tmp = NULL;
69 LL_FOREACH_SAFE(pluginList, out, tmp)
71 result = DeletePlugin(out);
72 if (result != OC_STACK_OK)
77 if (result == OC_STACK_OK)
84 OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
85 OCResourceHandle * resourceHandle)
87 if (!plugin || !resourceHandle || !piResource)
89 return OC_STACK_INVALID_PARAM;
91 PIResourceBase * out = NULL;
92 PIResourceBase * tmp = NULL;
93 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
95 if (out->piResource.resourceHandle == resourceHandle)
97 *piResource = (PIResource *) out;
101 return OC_STACK_NO_RESOURCE;
104 static bool ZigbeeStrEquals(const char * s1, const char * s2, size_t s1_length, size_t s2_length)
106 if (!s1 || !s2 || s1_length == 0 || s2_length == 0)
110 if (s1_length == s2_length && memcmp(s1, s2, (s1_length + 1)) == 0)
117 OCStackResult GetResourceFromZigBeeNodeId(PIPluginBase * plugin,
118 PIResource_Zigbee ** piResource,
120 const char * endpointId,
121 const char * clusterId)
123 OC_LOG(INFO, TAG, "Entered GetResourceFromZigBeeNodeId().");
124 if(!plugin || !piResource || !nodeId || !clusterId || !endpointId)
126 OC_LOG(ERROR, TAG, "Invalid param.");
127 return OC_STACK_INVALID_PARAM;
129 if(plugin->type != PLUGIN_ZIGBEE)
131 OC_LOG(ERROR, TAG, "Plugin Type is not Zigbee.");
132 return OC_STACK_INVALID_PARAM;
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)
143 indexLength = strlen(((PIResource_Zigbee *)out)->nodeId);
144 if(ZigbeeStrEquals(nodeId,
145 ((PIResource_Zigbee *)out)->nodeId,
147 indexLength) == false)
151 indexLength = strlen(((PIResource_Zigbee *)out)->endpointId);
152 if(ZigbeeStrEquals(endpointId,
153 ((PIResource_Zigbee *)out)->endpointId,
154 checkEndpointIdLength,
155 indexLength) == false)
159 indexLength = strlen(((PIResource_Zigbee *)out)->clusterId);
160 if(ZigbeeStrEquals(clusterId,
161 ((PIResource_Zigbee *)out)->clusterId,
162 checkClusterIdLength,
163 indexLength) == false)
167 OC_LOG_V(INFO, TAG, "Found a match! URI = %s", out->piResource.uri);
168 *piResource = (PIResource_Zigbee *) out;
172 return OC_STACK_NO_RESOURCE;
175 OCStackResult AddResourceToPlugin (PIPluginBase * plugin, PIResourceBase * resource)
177 if (!plugin || !resource)
179 return OC_STACK_INVALID_PARAM;
182 LL_APPEND(plugin->resourceList, resource);
184 return OC_STACK_NO_MEMORY;
187 OCStackResult DeleteResource(PIPluginBase * plugin, PIResourceBase * resource)
189 if (!plugin || !resource)
191 return OC_STACK_INVALID_PARAM;
194 //Todo: Free all of resource allocations.
195 PIResourceBase * resourceList = ((PIResourceBase *) plugin->resourceList);
197 LL_DELETE(resourceList, resource);
199 OCStackResult result = OCDeleteResource(resource->piResource.resourceHandle);
200 if(result != OC_STACK_OK)
202 OC_LOG_V(ERROR, TAG, "Failed to delete resource with error: %d", result);
206 OICFree (resource->piResource.uri);
207 if (plugin->type == PLUGIN_ZIGBEE)
209 OICFree (((PIResource_Zigbee *)resource)->eui);
210 OICFree (((PIResource_Zigbee *)resource)->nodeId);
211 OICFree (((PIResource_Zigbee *)resource)->endpointId);
212 OICFree (((PIResource_Zigbee *)resource)->clusterId);
218 OCStackResult DeleteResourceList(PIPluginBase * plugin)
222 return OC_STACK_INVALID_PARAM;
225 OCStackResult result = OC_STACK_OK;
226 PIResourceBase * out = NULL;
227 PIResourceBase * tmp = NULL;
229 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
231 result = DeleteResource(plugin, out);
232 if (result != OC_STACK_OK)
237 if (result == OC_STACK_OK)
239 plugin->resourceList = NULL;