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.
23 #include <zblib_service.h>
24 #include <zblib_plugin.h>
25 #include <zblib_service_interface.h>
26 #include <zblib_request.h>
28 /**< ZigBee Service object */
29 struct zblib_service_type {
30 GMainLoop *main_loop; /**< Service main-loop */
32 GSList *interface_objs; /**< ZigBee Service interface objects */
33 GSList *plugins; /**< ZigBee plug-ins */
35 GHashTable *request_table; /**< Request Hash table */
36 guint request_id; /**< Request ID */
39 static void *__zblib_service_load_plugin(gchar *filename,
40 ZblibPluginDescriptor_t **descriptor_out)
42 ZblibPluginDescriptor_t *descriptor = NULL;
47 zblib_check_null_ret_error("descriptor_out", descriptor_out, NULL);
50 handle = dlopen(filename, RTLD_LAZY);
51 if (G_UNLIKELY(NULL == handle)) {
52 Z_LOGE("dlopen() failed:[%s]", filename);
56 /* Get symbol - "zigbee_plugin_descriptor" */
57 descriptor = dlsym(handle, "zigbee_plugin_descriptor");
58 if (G_UNLIKELY(NULL == descriptor)) {
59 Z_LOGE("dlsym() failed:[%s]", "plugin_define_desc");
64 Z_LOGD("%s plugin", descriptor->name);
65 Z_LOGD(" - path = %s", filename);
66 Z_LOGD(" - version = %d", descriptor->version);
68 memset(&stat_buf, 0x00, sizeof(stat_buf));
69 memset(&file_date, '\0', sizeof(file_date));
71 if (0 == stat(filename, &stat_buf)) {
72 if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
73 if (1 < strlen(file_date))
74 file_date[strlen(file_date)-1] = '\0';
75 Z_LOGD(" - date = %s", file_date);
80 if (G_LIKELY(descriptor->load)) {
81 if (G_UNLIKELY(FALSE == descriptor->load())) {
82 Z_LOGW("load() failed... Skip this plugin!");
86 Z_LOGI("Plug-in (%s) loaded!", descriptor->name);
90 *descriptor_out = descriptor;
95 static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
97 const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
99 zblib_check_null_ret_error("descriptor", descriptor, FALSE);
100 zblib_check_null_ret_error("descriptor->init", descriptor->init, FALSE);
102 if (G_UNLIKELY(FALSE == descriptor->init(plugin))) {
103 char *plugin_name = zblib_plugin_get_plugin_name(plugin);
104 if (G_UNLIKELY(NULL != plugin_name)) {
105 Z_LOGE("plugin(%s) init failed!", plugin_name);
114 static gboolean __zblib_service_unload_plugin(ZigBeePlugin *plugin)
116 const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
117 char *plugin_name = zblib_plugin_get_plugin_name(plugin);
119 zblib_check_null_ret_error("descriptor", descriptor, FALSE);
120 zblib_check_null_ret_error("descriptor->unload", descriptor->unload, FALSE);
122 descriptor->unload(plugin);
123 Z_LOGI("plugin(%s) unloaded!", plugin_name);
128 ZigBeeService *zblib_service_new()
130 ZigBeeService *service;
132 service = g_malloc0(sizeof(struct zblib_service_type));
134 /* Create g-main loop */
135 service->main_loop = g_main_loop_new(NULL, FALSE);
136 if (G_UNLIKELY(NULL == service->main_loop)) {
137 Z_LOGE("g-main loop creation failed!!!");
142 /* Create request hash table */
143 service->request_table = g_hash_table_new_full(g_direct_hash,
144 g_direct_equal, NULL, NULL);
149 void zblib_service_free(ZigBeeService *service)
151 zblib_check_null_ret("service", service);
154 if (service->plugins) {
155 g_slist_free(service->plugins);
156 service->plugins = NULL;
159 /* Unref 'g-main loop' */
160 if (service->main_loop)
161 g_main_loop_unref(service->main_loop);
166 gboolean zblib_service_run(ZigBeeService *service)
168 zblib_check_null_ret_error("service", service, FALSE);
169 zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
171 g_main_loop_run(service->main_loop);
176 gboolean zblib_service_exit(ZigBeeService *service)
178 zblib_check_null_ret_error("service", service, FALSE);
179 zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
181 g_main_loop_quit(service->main_loop);
186 gboolean zblib_service_add_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
190 zblib_check_null_ret_error("service", service, FALSE);
191 zblib_check_null_ret_error("plugin", plugin, FALSE);
193 plugin_name = zblib_plugin_get_plugin_name(plugin);
195 /* All plug-ins would be appended */
196 service->plugins = g_slist_append(service->plugins, plugin);
197 Z_LOGD("%s added", plugin_name);
203 gboolean zblib_service_remove_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
205 zblib_check_null_ret_error("service", service, FALSE);
206 zblib_check_null_ret_error("plugin", plugin, FALSE);
208 /* Specific vendor plug-in would be removed */
209 service->plugins = g_slist_remove(service->plugins, plugin);
214 gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_path)
216 const gchar *file = NULL;
217 gchar *filename = NULL;
220 ZblibPluginDescriptor_t *descriptor = NULL;
221 ZigBeePlugin *plugin = NULL;
224 zblib_check_null_ret_error("service", service, FALSE);
225 zblib_check_null_ret_error("plugin_path", plugin_path, FALSE);
227 /* Open plug-in directory */
228 dir = g_dir_open(plugin_path, 0, NULL);
229 if (G_UNLIKELY(dir == NULL)) {
230 Z_LOGE("Directory open failed!");
234 /* Scan through all libraries in plug-in directory */
235 while ((file = g_dir_read_name(dir)) != NULL) {
236 if ((g_str_has_prefix(file, "lib") == TRUE)
237 || (g_str_has_suffix(file, ".so") == FALSE))
240 filename = g_build_filename(plugin_path, file, NULL);
243 handle = __zblib_service_load_plugin(filename, &descriptor);
244 if (G_UNLIKELY(NULL == handle)) {
249 /* Create new plug-in */
250 plugin = zblib_plugin_new(service, filename, descriptor, handle);
251 if (G_UNLIKELY(NULL == plugin)) {
256 /* Add new plug-in */
257 ret = zblib_service_add_plugin(service, plugin);
258 if (G_UNLIKELY(FALSE == ret)) {
259 zblib_plugin_free(plugin);
269 gboolean zblib_service_initialize_plugins(ZigBeeService *service)
273 zblib_check_null_ret_error("service", service, FALSE);
275 /* Refer plug-in list */
276 list = zblib_service_ref_plugins(service);
277 while (list != NULL) {
278 /* Initialize each plug-in */
279 if (G_UNLIKELY(FALSE == __zblib_service_init_plugin((ZigBeePlugin *)(list->data)))) {
280 list = g_slist_next(list);
283 list = g_slist_next(list);
289 gboolean zblib_service_unload_plugins(ZigBeeService *service)
293 zblib_check_null_ret_error("service", service, FALSE);
295 list = zblib_service_ref_plugins(service);
296 while (list != NULL) {
297 ZigBeePlugin *plugin = list->data;
299 /* Unload each plug-in */
300 if (G_UNLIKELY(FALSE == __zblib_service_unload_plugin(plugin))) {
301 list = g_slist_next(list);
305 list = g_slist_next(list);
306 zblib_service_remove_plugin(service, plugin);
312 GSList *zblib_service_ref_plugins(ZigBeeService *service)
314 zblib_check_null_ret_error("service", service, NULL);
316 return service->plugins;
319 gboolean zblib_service_add_service_interface(ZigBeeService *service,
320 ZigBeeServiceInterface *service_interface)
324 zblib_check_null_ret_error("service", service, FALSE);
325 zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
327 object_name = zblib_service_interface_get_name(service_interface);
330 * All service interface objects would be appended
332 service->interface_objs = g_slist_append(service->interface_objs, service_interface);
333 Z_LOGD("%s added", object_name);
339 gboolean zblib_service_remove_service_interface(ZigBeeService *service,
340 ZigBeeServiceInterface *service_interface)
342 zblib_check_null_ret_error("service", service, FALSE);
343 zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
346 * service interface object would be removed
348 service->interface_objs = g_slist_remove(service->interface_objs, service_interface);
353 ZigBeeServiceInterface *zblib_service_ref_service_interface(ZigBeeService *service,
354 const gchar *service_interface_name)
356 ZigBeeServiceInterface *service_interface = NULL;
357 ZigBeeServiceInterface *tmp_service_interface = NULL;
362 zblib_check_null_ret_error("service", service, NULL);
363 zblib_check_null_ret_error("service_interface_name", service_interface_name, NULL);
365 list = service->interface_objs;
367 tmp_service_interface = (ZigBeeServiceInterface *)list->data;
369 /* Get object name of service_interface */
370 object_name = zblib_service_interface_get_name(tmp_service_interface);
371 if (g_strcmp0(service_interface_name, object_name) == 0) {
374 service_interface = tmp_service_interface;
378 list = g_slist_next(list);
381 if (NULL == service_interface) {
382 Z_LOGE("Service interface object of name '%s' not found!",
383 service_interface_name);
387 Z_LOGD("'%s' found", service_interface_name);
389 return service_interface;
392 GHashTable *zblib_service_ref_request_hash_table(ZigBeeService *service)
394 zblib_check_null_ret_error("service", service, NULL);
396 return service->request_table;
399 gint zblib_service_generate_request_id(ZigBeeService *service)
401 zblib_check_null_ret_error("service", service, -1);
403 /* Increment request ID */
404 service->request_id++;
406 return (gint)service->request_id;
409 gboolean zblib_service_dispatch_request(ZigBeeService *service,
412 ZigBeePlugin *plugin = NULL;
413 GSList *plugin_list = NULL;
414 gboolean ret = FALSE;
416 zblib_check_null_ret_error("service", service, FALSE);
418 /* Fetch plugin_list */
419 plugin_list = service->plugins;
420 zblib_check_null_ret_error("plugin_list", plugin_list, FALSE);
422 while (plugin_list) {
423 plugin = (ZigBeePlugin *)plugin_list->data;
425 /* Dispatch request to plugin */
426 ret = zblib_plugin_dispatch_request(plugin, request_id);
428 /* Move to next plugin */
429 plugin_list = g_slist_next(plugin_list);
435 void zblib_service_send_response(ZigBeeService *service,
436 guint request_id, gpointer resp_data, guint resp_data_len)
438 ZigBeeServiceInterface *service_interface = NULL;
440 zblib_check_null_ret("service", service);
442 service_interface = zblib_request_ref_service_interface(service, request_id);
443 zblib_check_null_ret("service_interface", service_interface);
445 /* Send response to service interface */
446 zblib_service_interface_send_response(service_interface,
447 request_id, resp_data, resp_data_len);
450 void zblib_service_send_notification(ZigBeeService *service,
451 guint noti_id, gpointer noti_data, guint noti_data_len)
453 GSList *interface_objs = NULL;
454 ZigBeeServiceInterface *service_interface = NULL;
456 zblib_check_null_ret("service", service);
458 interface_objs = service->interface_objs;
459 zblib_check_null_ret("interface_objs", interface_objs);
461 while (interface_objs) {
462 service_interface = (ZigBeeServiceInterface *)interface_objs->data;
464 /* Send notification to service interface */
465 zblib_service_interface_send_notification(service_interface,
466 noti_id, noti_data, noti_data_len);
468 /* Move to next service interface */
469 interface_objs = g_slist_next(interface_objs);