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 OCStackResult GetResourceFromURI(PIPluginBase * plugin, PIResource ** piResource,
107 if (!plugin || !piResource || !uri)
109 return OC_STACK_INVALID_PARAM;
111 PIResourceBase * out = NULL;
112 PIResourceBase * tmp = NULL;
113 size_t checkUriLength = strlen(uri);
114 size_t indexUriLength = 0;
115 size_t minLength = 0;
116 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
118 indexUriLength = strlen(out->piResource.uri);
119 minLength = indexUriLength > checkUriLength ? checkUriLength : indexUriLength;
120 if ((checkUriLength == indexUriLength) &&
121 memcmp(out->piResource.uri, uri, minLength + 1) == 0)
123 *piResource = (PIResource *) out;
128 return OC_STACK_NO_RESOURCE;
131 OCStackResult AddResourceToPlugin (PIPluginBase * plugin, PIResourceBase * resource)
133 if (!plugin || !resource)
135 return OC_STACK_INVALID_PARAM;
138 LL_APPEND(plugin->resourceList, resource);
140 return OC_STACK_NO_MEMORY;
143 OCStackResult DeleteResource(PIPluginBase * plugin, PIResourceBase * resource)
145 if (!plugin || !resource)
147 return OC_STACK_INVALID_PARAM;
150 //Todo: Free all of resource allocations.
151 PIResourceBase * resourceList = ((PIResourceBase *) plugin->resourceList);
153 LL_DELETE(resourceList, resource);
155 OCStackResult result = OCDeleteResource(resource->piResource.resourceHandle);
156 if(result != OC_STACK_OK)
158 OC_LOG_V(ERROR, TAG, "Failed to delete resource with error: %d", result);
162 OICFree (resource->piResource.uri);
163 if (plugin->type == PLUGIN_ZIGBEE)
165 OICFree (((PIResource_Zigbee *)resource)->eui);
166 OICFree (((PIResource_Zigbee *)resource)->nodeId);
167 OICFree (((PIResource_Zigbee *)resource)->endpointId);
168 OICFree (((PIResource_Zigbee *)resource)->clusterId);
174 OCStackResult DeleteResourceList(PIPluginBase * plugin)
178 return OC_STACK_INVALID_PARAM;
181 OCStackResult result = OC_STACK_OK;
182 PIResourceBase * out = NULL;
183 PIResourceBase * tmp = NULL;
185 LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
187 result = DeleteResource(plugin, out);
188 if (result != OC_STACK_OK)
193 if (result == OC_STACK_OK)
195 plugin->resourceList = NULL;