Added function to config terminal for read/write.
[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
30 static PIPluginBase * pluginList = NULL;
31
32 OCStackResult AddPlugin (PIPluginBase * plugin)
33 {
34     if (!plugin)
35     {
36         return OC_STACK_INVALID_PARAM;
37     }
38
39     LL_APPEND(pluginList, plugin);
40     return OC_STACK_OK;
41 }
42
43 OCStackResult DeletePlugin(PIPluginBase * plugin)
44 {
45     OCStackResult result = OC_STACK_ERROR;
46     if (!plugin)
47     {
48         return OC_STACK_INVALID_PARAM;
49     }
50     DeleteResourceList(plugin);
51     LL_DELETE(pluginList, plugin);
52     if (plugin->type == PLUGIN_ZIGBEE)
53     {
54         result = ZigbeeStop((PIPlugin_Zigbee *) plugin);
55     }
56     return result;
57 }
58
59
60 OCStackResult DeletePluginList()
61 {
62     OCStackResult result = OC_STACK_OK;
63     PIPluginBase * out = NULL;
64     PIPluginBase * tmp = NULL;
65     LL_FOREACH_SAFE(pluginList, out, tmp)
66     {
67         result = DeletePlugin(out);
68         if (result != OC_STACK_OK)
69         {
70             break;
71         }
72     }
73     if (result == OC_STACK_OK)
74     {
75         pluginList = NULL;
76     }
77     return result;
78 }
79
80 OCStackResult GetResourceFromHandle(PIPluginBase * plugin, PIResource ** piResource,
81                                     OCResourceHandle * resourceHandle)
82 {
83     if (!plugin || !resourceHandle || !piResource)
84     {
85         return OC_STACK_INVALID_PARAM;
86     }
87     PIResourceBase * out = NULL;
88     PIResourceBase * tmp = NULL;
89     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
90     {
91         if (out->piResource.resourceHandle == resourceHandle)
92         {
93             *piResource = (PIResource *) out;
94             return OC_STACK_OK;
95         }
96     }
97     return OC_STACK_NO_RESOURCE;
98 }
99
100 OCStackResult AddResourceToPlugin (PIPluginBase * plugin, PIResourceBase * resource)
101 {
102     if (!plugin || !resource)
103     {
104         return OC_STACK_INVALID_PARAM;
105     }
106
107     LL_APPEND(plugin->resourceList, resource);
108
109     return OC_STACK_NO_MEMORY;
110 }
111
112 OCStackResult DeleteResource(PIPluginBase * plugin, PIResourceBase * resource)
113 {
114     if (!plugin || !resource)
115     {
116         return OC_STACK_INVALID_PARAM;
117     }
118
119     //Todo: Free all of resource allocations.
120     PIResourceBase * resourceList = ((PIResourceBase *) plugin->resourceList);
121
122     LL_DELETE(resourceList, resource);
123
124     OICFree (resource->piResource.uri);
125     if (plugin->type == PLUGIN_ZIGBEE)
126     {
127         OICFree (((PIResource_Zigbee *)resource)->eui);
128         OICFree (((PIResource_Zigbee *)resource)->nodeId);
129         OICFree (((PIResource_Zigbee *)resource)->endpointId);
130         OICFree (((PIResource_Zigbee *)resource)->clusterId);
131     }
132     OICFree (resource);
133     return OC_STACK_OK;
134 }
135
136 OCStackResult DeleteResourceList(PIPluginBase * plugin)
137 {
138     if (!plugin)
139     {
140         return OC_STACK_INVALID_PARAM;
141     }
142
143     OCStackResult result = OC_STACK_OK;
144     PIResourceBase * out = NULL;
145     PIResourceBase * tmp = NULL;
146
147     LL_FOREACH_SAFE(plugin->resourceList, out, tmp)
148     {
149         result = DeleteResource(plugin, out);
150         if (result != OC_STACK_OK)
151         {
152             break;
153         }
154     }
155     if (result == OC_STACK_OK)
156     {
157         plugin->resourceList = NULL;
158     }
159     return result;
160 }