Imported Upstream version 1.0.0
[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 OCStackResult GetResourceFromURI(PIPluginBase * plugin, PIResource ** piResource,
105                                     const char * uri)
106 {
107     if (!plugin || !piResource || !uri)
108     {
109         return OC_STACK_INVALID_PARAM;
110     }
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)
117     {
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)
122         {
123             *piResource = (PIResource *) out;
124             return OC_STACK_OK;
125         }
126     }
127     *piResource = NULL;
128     return OC_STACK_NO_RESOURCE;
129 }
130
131 OCStackResult AddResourceToPlugin (PIPluginBase * plugin, PIResourceBase * resource)
132 {
133     if (!plugin || !resource)
134     {
135         return OC_STACK_INVALID_PARAM;
136     }
137
138     LL_APPEND(plugin->resourceList, resource);
139
140     return OC_STACK_NO_MEMORY;
141 }
142
143 OCStackResult DeleteResource(PIPluginBase * plugin, PIResourceBase * resource)
144 {
145     if (!plugin || !resource)
146     {
147         return OC_STACK_INVALID_PARAM;
148     }
149
150     //Todo: Free all of resource allocations.
151     PIResourceBase * resourceList = ((PIResourceBase *) plugin->resourceList);
152
153     LL_DELETE(resourceList, resource);
154
155     OCStackResult result = OCDeleteResource(resource->piResource.resourceHandle);
156     if(result != OC_STACK_OK)
157     {
158         OC_LOG_V(ERROR, TAG, "Failed to delete resource with error: %d", result);
159         return result;
160     }
161
162     OICFree (resource->piResource.uri);
163     if (plugin->type == PLUGIN_ZIGBEE)
164     {
165         OICFree (((PIResource_Zigbee *)resource)->eui);
166         OICFree (((PIResource_Zigbee *)resource)->nodeId);
167         OICFree (((PIResource_Zigbee *)resource)->endpointId);
168         OICFree (((PIResource_Zigbee *)resource)->clusterId);
169     }
170     OICFree (resource);
171     return OC_STACK_OK;
172 }
173
174 OCStackResult DeleteResourceList(PIPluginBase * plugin)
175 {
176     if (!plugin)
177     {
178         return OC_STACK_INVALID_PARAM;
179     }
180
181     OCStackResult result = OC_STACK_OK;
182     PIResourceBase * out = NULL;
183     PIResourceBase * tmp = NULL;
184
185     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
186     {
187         result = DeleteResource(plugin, out);
188         if (result != OC_STACK_OK)
189         {
190             break;
191         }
192     }
193     if (result == OC_STACK_OK)
194     {
195         plugin->resourceList = NULL;
196     }
197     return result;
198 }