[WGID-279204] Fix a potential memory leak problem
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / zblib_plugin.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Suresh Kumar N (suresh.n@samsung.com)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <dlfcn.h>
20
21 #include <zblib.h>
22 #include <zblib_plugin.h>
23 #include <zblib_driver.h>
24 #include <zblib_service.h>
25 #include <zblib_request.h>
26
27 /**< ZigBee plug-in object */
28 struct zblib_plugin_type {
29         gchar *plugin_name; /**< ZigBee plug-in name */
30         const ZblibPluginDescriptor_t *descriptor; /**< ZigBee plug-in descriptor */
31         void *plugin_handle; /**< ZigBee Plug-in .so handle */
32
33         GSList *driver_list; /**< List of ZigBee drivers */
34
35         ZigBeeService *service; /**< ZigBee Service */
36 };
37
38 ZigBeePlugin *zblib_plugin_new(ZigBeeService *service,
39         const gchar *plugin_name,
40         const ZblibPluginDescriptor_t *descriptor,
41         void *plugin_handle)
42 {
43         ZigBeePlugin *plugin = NULL;
44
45         /* Allocate memory */
46         plugin = g_malloc0(sizeof(ZigBeePlugin));
47         if (NULL == plugin)
48                 return NULL;
49
50         /* Update fields */
51         plugin->plugin_name = g_strdup(plugin_name);
52         plugin->descriptor = descriptor;
53         plugin->plugin_handle = plugin_handle;
54         plugin->service = service;
55
56         Z_LOGI("Vendor plug-in created - Name [%s]", descriptor->name);
57
58         return plugin;
59 }
60
61 void zblib_plugin_free(ZigBeePlugin *plugin)
62 {
63         zblib_check_null_ret("plugin", plugin);
64
65         Z_LOGI("Freeing Vendor plug-in - Name [%s]", plugin->descriptor->name);
66
67         if (plugin->driver_list) {
68                 GSList *list;
69                 ZigBeeDriver *driver;
70
71                 for (list = plugin->driver_list; list; list = list->next) {
72                         driver = list->data;
73                         if (NULL == driver)
74                                 continue;
75
76                         zblib_driver_free(driver);
77                         list->data = NULL;
78                 }
79
80                 g_slist_free(plugin->driver_list);
81                 plugin->driver_list = NULL;
82         }
83
84         if (plugin->plugin_name) {
85                 g_free(plugin->plugin_name);
86                 plugin->plugin_name = NULL;
87         }
88
89         plugin->descriptor = NULL;
90
91         if (plugin->plugin_handle) {
92                 dlclose(plugin->plugin_handle);
93                 plugin->plugin_handle = NULL;
94         }
95
96         g_free(plugin);
97 }
98
99 const ZblibPluginDescriptor_t *zblib_plugin_get_descriptor(ZigBeePlugin *plugin)
100 {
101         zblib_check_null_ret_error("plugin", plugin, NULL);
102
103         return plugin->descriptor;
104 }
105
106 char *zblib_plugin_get_plugin_name(ZigBeePlugin *plugin)
107 {
108         zblib_check_null_ret_error("plugin", plugin, NULL);
109
110         return g_strdup(plugin->plugin_name);
111 }
112
113 ZigBeeService *zblib_plugin_ref_service(ZigBeePlugin *plugin)
114 {
115         zblib_check_null_ret_error("plugin", plugin, NULL);
116
117         return plugin->service;
118 }
119
120 ZigBeeDriver *zblib_plugin_ref_driver(ZigBeePlugin *plugin, ZblibDriverType_e driver_type)
121 {
122         ZigBeeDriver *driver = NULL;
123         ZblibDriverType_e _driver_type;
124         GSList *list = NULL;
125
126         zblib_check_null_ret_error("plugin", plugin, NULL);
127
128         list = plugin->driver_list;
129         while (list) {
130                 /* Fetch driver type of driver */
131                 _driver_type = zblib_driver_ref_driver_type((ZigBeeDriver *)(list->data));
132                 if (_driver_type == driver_type) {
133                         /* Driver found */
134                         driver = (ZigBeeDriver *)(list->data);
135                         break;
136                 }
137
138                 /* Move to next driver */
139                 list = g_slist_next(list);
140         }
141
142         return driver;
143 }
144
145 void zblib_plugin_link_driver(ZigBeePlugin *plugin, ZigBeeDriver *driver)
146 {
147         zblib_check_null_ret("plugin", plugin);
148         zblib_check_null_ret("driver", driver);
149
150         plugin->driver_list = g_slist_append(plugin->driver_list, driver);
151         Z_LOGD("Driver appended into plugin [%s]", plugin->plugin_name);
152 }
153
154 void zblib_plugin_unlink_driver(ZigBeePlugin *plugin, ZblibDriverType_e driver_type)
155 {
156         ZblibDriverType_e _driver_type;
157         GSList *list = NULL;
158
159         zblib_check_null_ret("plugin", plugin);
160
161         list = plugin->driver_list;
162         while (list) {
163                 /* Fetch driver type of driver */
164                 _driver_type = zblib_driver_ref_driver_type((ZigBeeDriver *)(list->data));
165                 if (_driver_type == driver_type) {
166                         /* Driver found */
167                         Z_LOGD("Driver[%p] removed from plugin", list->data);
168                         list->data = NULL;
169                         break;
170                 }
171
172                 /* Move to next driver */
173                 list = g_slist_next(list);
174         }
175 }
176
177 gboolean zblib_plugin_dispatch_request(ZigBeePlugin *plugin,
178         guint request_id)
179 {
180         ZigBeeService *service = NULL;
181         ZigBeeDriver *driver = NULL;
182         ZblibDriverType_e driver_type;
183         guint request_type;
184
185         zblib_check_null_ret_error("plugin", plugin, FALSE);
186
187         /* Fetch service */
188         service = plugin->service;
189         zblib_check_null_ret_error("service", service, FALSE);
190
191         /* Fetch driver type */
192         request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
193         driver_type = zblib_request_get_driver_type(request_type);
194
195         /* Fetch driver */
196         driver = zblib_plugin_ref_driver(plugin, driver_type);
197         zblib_check_null_ret_error("driver", driver, FALSE);
198
199         /* Dispatch request to driver */
200         return zblib_driver_dispatch_request(driver, request_id);
201 }
202
203 void zblib_plugin_send_response(ZigBeePlugin *plugin,
204         guint request_id, gpointer resp_data, guint resp_data_len)
205 {
206         ZigBeeService *service = NULL;
207
208         zblib_check_null_ret("plugin", plugin);
209
210         service = plugin->service;
211         zblib_check_null_ret("service", service);
212
213         /* Send response to service */
214         zblib_service_send_response(service,
215                 request_id, resp_data, resp_data_len);
216 }
217
218 void zblib_plugin_send_notification(ZigBeePlugin *plugin,
219         guint noti_id, gpointer noti_data, guint noti_data_len)
220 {
221         ZigBeeService *service = NULL;
222
223         zblib_check_null_ret("plugin", plugin);
224
225         service = plugin->service;
226         zblib_check_null_ret("service", service);
227
228         /* Send notification to service */
229         zblib_service_send_notification(service,
230                 noti_id, noti_data, noti_data_len);
231 }