2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Contact: Suresh Kumar N (suresh.n@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <zblib_plugin.h>
23 #include <zblib_driver.h>
24 #include <zblib_service.h>
25 #include <zblib_request.h>
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 */
33 GSList *driver_list; /**< List of ZigBee drivers */
35 ZigBeeService *service; /**< ZigBee Service */
38 ZigBeePlugin *zblib_plugin_new(ZigBeeService *service,
39 const gchar *plugin_name,
40 const ZblibPluginDescriptor_t *descriptor,
43 ZigBeePlugin *plugin = NULL;
46 plugin = g_malloc0(sizeof(ZigBeePlugin));
49 plugin->plugin_name = g_strdup(plugin_name);
50 plugin->descriptor = descriptor;
51 plugin->plugin_handle = plugin_handle;
52 plugin->service = service;
54 Z_LOGI("Vendor plug-in created - Name [%s]", descriptor->name);
59 void zblib_plugin_free(ZigBeePlugin *plugin)
61 zblib_check_null_ret("plugin", plugin);
63 Z_LOGI("Freeing Vendor plug-in - Name [%s]", plugin->descriptor->name);
65 if (plugin->driver_list) {
69 for (list = plugin->driver_list; list; list = list->next) {
74 zblib_driver_free(driver);
78 g_slist_free(plugin->driver_list);
79 plugin->driver_list = NULL;
82 if (plugin->plugin_name) {
83 g_free(plugin->plugin_name);
84 plugin->plugin_name = NULL;
87 plugin->descriptor = NULL;
89 if (plugin->plugin_handle) {
90 dlclose(plugin->plugin_handle);
91 plugin->plugin_handle = NULL;
97 const ZblibPluginDescriptor_t *zblib_plugin_get_descriptor(ZigBeePlugin *plugin)
99 zblib_check_null_ret_error("plugin", plugin, NULL);
101 return plugin->descriptor;
104 char *zblib_plugin_get_plugin_name(ZigBeePlugin *plugin)
106 zblib_check_null_ret_error("plugin", plugin, NULL);
108 return g_strdup(plugin->plugin_name);
111 ZigBeeService *zblib_plugin_ref_service(ZigBeePlugin *plugin)
113 zblib_check_null_ret_error("plugin", plugin, NULL);
115 return plugin->service;
118 ZigBeeDriver *zblib_plugin_ref_driver(ZigBeePlugin *plugin, ZblibDriverType_e driver_type)
120 ZigBeeDriver *driver = NULL;
121 ZblibDriverType_e _driver_type;
124 zblib_check_null_ret_error("plugin", plugin, NULL);
126 list = plugin->driver_list;
128 /* Fetch driver type of driver */
129 _driver_type = zblib_driver_ref_driver_type((ZigBeeDriver *)(list->data));
130 if (_driver_type == driver_type) {
132 driver = (ZigBeeDriver *)(list->data);
136 /* Move to next driver */
137 list = g_slist_next(list);
143 void zblib_plugin_link_driver(ZigBeePlugin *plugin, ZigBeeDriver *driver)
145 zblib_check_null_ret("plugin", plugin);
146 zblib_check_null_ret("driver", driver);
148 plugin->driver_list = g_slist_append(plugin->driver_list, driver);
149 Z_LOGD("Driver appended into plugin [%s]", plugin->plugin_name);
152 gboolean zblib_plugin_dispatch_request(ZigBeePlugin *plugin,
155 ZigBeeService *service = NULL;
156 ZigBeeDriver *driver = NULL;
157 ZblibDriverType_e driver_type;
160 zblib_check_null_ret_error("plugin", plugin, FALSE);
163 service = plugin->service;
164 zblib_check_null_ret_error("service", service, FALSE);
166 /* Fetch driver type */
167 request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
168 driver_type = zblib_request_get_driver_type(request_type);
171 driver = zblib_plugin_ref_driver(plugin, driver_type);
172 zblib_check_null_ret_error("driver", driver, FALSE);
174 /* Dispatch request to driver */
175 return zblib_driver_dispatch_request(driver, request_id);
178 void zblib_plugin_send_response(ZigBeePlugin *plugin,
179 guint request_id, gpointer resp_data, guint resp_data_len)
181 ZigBeeService *service = NULL;
183 zblib_check_null_ret("plugin", plugin);
185 service = plugin->service;
186 zblib_check_null_ret("service", service);
188 /* Send response to service */
189 zblib_service_send_response(service,
190 request_id, resp_data, resp_data_len);
193 void zblib_plugin_send_notification(ZigBeePlugin *plugin,
194 guint noti_id, gpointer noti_data, guint noti_data_len)
196 ZigBeeService *service = NULL;
198 zblib_check_null_ret("plugin", plugin);
200 service = plugin->service;
201 zblib_check_null_ret("service", service);
203 /* Send notification to service */
204 zblib_service_send_notification(service,
205 noti_id, noti_data, noti_data_len);