df81d6917c525a514ed63b55ab52c9d2ee22ce14
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / zblib_service.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 #include <sys/stat.h>
21
22 #include <zblib.h>
23 #include <zblib_service.h>
24 #include <zblib_plugin.h>
25 #include <zblib_service_interface.h>
26 #include <zblib_request.h>
27 #include <zblib_driver.h>
28 #include <zblib_driver_manager.h>
29
30 struct _zblib_async_init_info {
31         gboolean is_initialized; /**< Does initialization succeeded? */
32         gboolean is_finished; /**< Does initialization finished? */
33
34         ZigBeePlugin *plugin; /**< ZigBee plugin */
35         void *user_data; /**< User data */
36 };
37
38 /**< ZigBee Service object */
39 struct zblib_service_type {
40         GMainLoop *main_loop; /**< Service main-loop */
41
42         GSList *interface_objs; /**< ZigBee Service interface objects */
43         GSList *plugins; /**< ZigBee plug-ins */
44
45         GHashTable *request_table; /**< Request Hash table */
46         guint request_id; /**< Request ID */
47
48         GSList *async_initializer; /**< Hash table for asynchronous initializer */
49         zblib_plugin_init_finished_cb callback; /**< Callback */
50 };
51
52 static GSList *__zblib_service_ref_async_initializer(ZigBeeService* service)
53 {
54         zblib_check_null_ret_error("service", service, NULL);
55
56         return service->async_initializer;
57 }
58
59 static void __zblib_service_set_async_initializer(ZigBeeService* service, GSList* list)
60 {
61         zblib_check_null_ret("service", service);
62
63         service->async_initializer = list;
64 }
65
66 static void __on_zblib_init_async_finished(gboolean result, void *user_data)
67 {
68         GSList *list_async = NULL;
69         ZigBeeService *service = NULL;
70         ZigBeePlugin *plugin = NULL;
71         struct _zblib_async_init_info *info = (struct _zblib_async_init_info*)user_data;
72         char *plugin_name = NULL;
73
74         int i = 0;
75         int _finished_count = 0;
76         int _succeeded_count = 0;
77
78         zblib_check_null_ret("info", info);
79
80         plugin = info->plugin;
81         service = zblib_plugin_ref_service(plugin);
82         plugin_name = zblib_plugin_get_plugin_name(plugin);
83
84         zblib_check_null_ret("service", service);
85
86         info->is_finished = TRUE;
87         if (result) {
88                 Z_LOGD("Plugin [%s] succeeded async init", plugin_name);
89                 info->is_initialized = TRUE;
90         } else {
91                 Z_LOGE("Plugin [%s] failed to init !", plugin_name);
92         }
93         g_free(plugin_name);
94
95         /* Check all async initializer list */
96         list_async = __zblib_service_ref_async_initializer(service);
97         while (list_async != NULL) {
98                 /* Initialize each plug-in */
99                 struct _zblib_async_init_info *in =
100                                 (struct _zblib_async_init_info *)(list_async->data);
101                 i++;
102                 if (G_UNLIKELY(NULL == in)) {
103                         Z_LOGE("Invalid parameter !");
104                         list_async = g_slist_next(list_async);
105                         continue;
106                 }
107
108                 if (in->is_finished)
109                         _finished_count++;
110                 if (in->is_initialized)
111                         _succeeded_count++;
112
113                 list_async = g_slist_next(list_async);
114         }
115
116         if (i == _finished_count) {
117                 gboolean ret = TRUE;
118                 guint driver_noti_id = (ZBLIB_DRIVER_TYPE_MANAGER << 24)
119                                 | ZBLIB_MANAGER_NOTI_ZIGBEE_ENABLED;
120
121                 if (_finished_count == _succeeded_count) {
122                         /* All initializer finished successfully */
123                         Z_LOGD("All async init finished !");
124                         ret = TRUE;
125                 } else {
126                         /* Some initializers are failed */
127                         Z_LOGE("There are some failed plugin initializer !");
128                         ret = FALSE;
129                 }
130                 /* Notify whether zigbee service is enabled or not */
131                 zblib_plugin_send_notification(plugin,
132                         driver_noti_id, &ret, sizeof(ret));
133         }
134 }
135
136 static void *__zblib_service_load_plugin(gchar *filename,
137         ZblibPluginDescriptor_t **descriptor_out)
138 {
139         ZblibPluginDescriptor_t *descriptor = NULL;
140         void *handle = NULL;
141         struct stat stat_buf;
142         char file_date[27];
143
144         zblib_check_null_ret_error("descriptor_out", descriptor_out, NULL);
145
146         /* Open .so */
147         handle = dlopen(filename, RTLD_LAZY);
148         if (G_UNLIKELY(NULL == handle)) {
149                 Z_LOGE("dlopen() failed:[%s]", filename);
150                 return NULL;
151         }
152
153         /* Get symbol - "zigbee_plugin_descriptor" */
154         descriptor = dlsym(handle, "zigbee_plugin_descriptor");
155         if (G_UNLIKELY(NULL == descriptor)) {
156                 Z_LOGE("dlsym() failed:[%s]", "plugin_define_desc");
157                 dlclose(handle);
158                 return NULL;
159         }
160
161         Z_LOGD("%s plugin", descriptor->name);
162         Z_LOGD(" - path = %s", filename);
163         Z_LOGD(" - version = %d", descriptor->version);
164
165         memset(&stat_buf, 0x00, sizeof(stat_buf));
166         memset(&file_date, '\0', sizeof(file_date));
167
168         if (0 == stat(filename, &stat_buf)) {
169                 if (NULL != ctime_r(&stat_buf.st_mtime, file_date)) {
170                         if (1 < strlen(file_date))
171                                 file_date[strlen(file_date)-1] = '\0';
172                         Z_LOGD(" - date = %s", file_date);
173                 }
174         }
175
176         /* Load plug-in */
177         if (G_LIKELY(descriptor->load)) {
178                 if (G_UNLIKELY(FALSE == descriptor->load())) {
179                         Z_LOGW("load() failed... Skip this plugin!");
180                         dlclose(handle);
181                         return NULL;
182                 } else {
183                         Z_LOGI("Plug-in (%s) loaded!", descriptor->name);
184                 }
185         }
186
187         *descriptor_out = descriptor;
188
189         return handle;
190 }
191
192 static gboolean __zblib_service_init_plugin(ZigBeePlugin *plugin)
193 {
194         const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
195
196         zblib_check_null_ret_error("descriptor", descriptor, FALSE);
197         if (NULL == descriptor->init) {
198                 Z_LOGD("descriptor->init is NULL, trying async init");
199                 return FALSE;
200         }
201
202         if (G_UNLIKELY(FALSE == descriptor->init(plugin))) {
203                 char *plugin_name = zblib_plugin_get_plugin_name(plugin);
204                 if (G_UNLIKELY(NULL != plugin_name)) {
205                         Z_LOGE("plugin(%s) init failed!", plugin_name);
206                         g_free(plugin_name);
207                 }
208                 return FALSE;
209         }
210
211         return TRUE;
212 }
213
214 static gboolean __zblib_service_init_async_plugin(ZigBeePlugin *plugin,
215                 zblib_plugin_init_finished_cb callback, void *user_data)
216 {
217         const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
218         char *plugin_name = zblib_plugin_get_plugin_name(plugin);
219
220         zblib_check_null_ret_error("descriptor", descriptor, FALSE);
221         zblib_check_null_ret_error("descriptor->init_async", descriptor->init_async, FALSE);
222
223         if (G_UNLIKELY(NULL != plugin_name))
224                 plugin_name = g_strdup("NONAME");
225
226         if (G_UNLIKELY(FALSE == descriptor->init_async(plugin, callback, user_data))) {
227                 Z_LOGE("plugin(%s) init failed!", plugin_name);
228                 g_free(plugin_name);
229                 return FALSE;
230         }
231
232         g_free(plugin_name);
233         return TRUE;
234 }
235
236 static gboolean __zblib_service_unload_plugin(ZigBeePlugin *plugin)
237 {
238         const ZblibPluginDescriptor_t *descriptor = zblib_plugin_get_descriptor(plugin);
239         char *plugin_name = NULL;
240
241         zblib_check_null_ret_error("descriptor", descriptor, FALSE);
242         zblib_check_null_ret_error("descriptor->unload", descriptor->unload, FALSE);
243
244         plugin_name = zblib_plugin_get_plugin_name(plugin);
245
246         descriptor->unload(plugin);
247         Z_LOGI("plugin(%s) unloaded!", plugin_name);
248
249         g_free(plugin_name);
250
251         return TRUE;
252 }
253
254 static void __zblib_service_remove_request_table_iter(gpointer key,
255         gpointer value, gpointer user_data)
256 {
257         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
258         gint request_id = (gint)key;
259
260         NOT_USED(value);
261
262         zblib_check_null_ret("service_interface", service_interface);
263
264         Z_LOGD("Removing request id [%d]", request_id);
265         zblib_request_free(service_interface, request_id);
266 }
267
268 ZigBeeService *zblib_service_new()
269 {
270         ZigBeeService *service;
271
272         service = g_malloc0(sizeof(struct zblib_service_type));
273
274         /* Create g-main loop */
275         service->main_loop = g_main_loop_new(NULL, FALSE);
276         if (G_UNLIKELY(NULL == service->main_loop)) {
277                 Z_LOGE("g-main loop creation failed!!!");
278                 g_free(service);
279                 return NULL;
280         }
281
282         /* Create request hash table */
283         service->request_table = g_hash_table_new_full(g_direct_hash,
284                 g_direct_equal, NULL, NULL);
285
286         return service;
287 }
288
289 void zblib_service_free(ZigBeeService *service)
290 {
291         zblib_check_null_ret("service", service);
292
293         if (service->request_table) {
294                 GSList *interface_objs = NULL;
295                 ZigBeeServiceInterface *service_interface = NULL;
296
297                 interface_objs = service->interface_objs;
298                 if (NULL == interface_objs) {
299                         Z_LOGE("interface_objs is NULL");
300                 } else {
301                         while (interface_objs) {
302                                 service_interface = (ZigBeeServiceInterface *)interface_objs->data;
303
304                                 /* Remove left request */
305                                 g_hash_table_foreach(service->request_table,
306                                                 __zblib_service_remove_request_table_iter,
307                                                 service_interface);
308
309                                 /* Move to next service interface */
310                                 interface_objs = g_slist_next(interface_objs);
311                         }
312                         g_hash_table_remove_all(service->request_table);
313                         g_hash_table_destroy(service->request_table);
314                         service->request_table = NULL;
315                 }
316         }
317
318         /* Free async initializer */
319         if (service->async_initializer) {
320                 g_slist_free(service->async_initializer);
321                 service->async_initializer = NULL;
322         }
323
324         /* Free plug-ins */
325         if (service->plugins) {
326                 g_slist_free(service->plugins);
327                 service->plugins = NULL;
328         }
329
330         /* Unref 'g-main loop' */
331         if (service->main_loop)
332                 g_main_loop_unref(service->main_loop);
333
334         g_free(service);
335 }
336
337 gboolean zblib_service_run(ZigBeeService *service)
338 {
339         zblib_check_null_ret_error("service", service, FALSE);
340         zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
341
342         g_main_loop_run(service->main_loop);
343
344         return TRUE;
345 }
346
347 gboolean zblib_service_exit(ZigBeeService *service)
348 {
349         zblib_check_null_ret_error("service", service, FALSE);
350         zblib_check_null_ret_error("service->main_loop", service->main_loop, FALSE);
351
352         g_main_loop_quit(service->main_loop);
353
354         return TRUE;
355 }
356
357 gboolean zblib_service_add_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
358 {
359         gchar *plugin_name;
360
361         zblib_check_null_ret_error("service", service, FALSE);
362         zblib_check_null_ret_error("plugin", plugin, FALSE);
363
364         plugin_name = zblib_plugin_get_plugin_name(plugin);
365
366         /* All plug-ins would be appended */
367         service->plugins = g_slist_append(service->plugins, plugin);
368         Z_LOGD("%s added", plugin_name);
369         g_free(plugin_name);
370
371         return TRUE;
372 }
373
374 gboolean zblib_service_remove_plugin(ZigBeeService *service, ZigBeePlugin *plugin)
375 {
376         zblib_check_null_ret_error("service", service, FALSE);
377         zblib_check_null_ret_error("plugin", plugin, FALSE);
378
379         /* Specific vendor plug-in would be removed */
380         service->plugins = g_slist_remove(service->plugins, plugin);
381
382         /* Deinitialize plugin */
383         zblib_plugin_free(plugin);
384
385         return TRUE;
386 }
387
388 gboolean zblib_service_load_plugins(ZigBeeService *service, const char *plugin_path)
389 {
390         const gchar *file = NULL;
391         gchar *filename = NULL;
392         GDir *dir = NULL;
393         void *handle = NULL;
394         ZblibPluginDescriptor_t *descriptor = NULL;
395         ZigBeePlugin *plugin = NULL;
396         gboolean ret;
397
398         zblib_check_null_ret_error("service", service, FALSE);
399         zblib_check_null_ret_error("plugin_path", plugin_path, FALSE);
400
401         /* Open plug-in directory */
402         dir = g_dir_open(plugin_path, 0, NULL);
403         if (G_UNLIKELY(dir == NULL)) {
404                 Z_LOGE("Directory open failed!");
405                 return FALSE;
406         }
407
408         /* Scan through all libraries in plug-in directory */
409         while ((file = g_dir_read_name(dir)) != NULL) {
410                 if ((g_str_has_prefix(file, "lib") == TRUE)
411                                 || (g_str_has_suffix(file, ".so") == FALSE))
412                         continue;
413
414                 filename = g_build_filename(plugin_path, file, NULL);
415
416                 /* Load plug-in */
417                 handle = __zblib_service_load_plugin(filename, &descriptor);
418                 if (G_UNLIKELY(NULL == handle)) {
419                         g_free(filename);
420                         continue;
421                 }
422
423                 /* Create new plug-in */
424                 plugin = zblib_plugin_new(service, filename, descriptor, handle);
425                 if (G_UNLIKELY(NULL == plugin)) {
426                         g_free(filename);
427                         continue;
428                 }
429
430                 /* Add new plug-in */
431                 ret = zblib_service_add_plugin(service, plugin);
432                 if (G_UNLIKELY(FALSE == ret))
433                         zblib_plugin_free(plugin);
434
435                 g_free(filename);
436         }
437         g_dir_close(dir);
438
439         return TRUE;
440 }
441
442 gboolean zblib_service_initialize_plugins(ZigBeeService *service)
443 {
444         GSList *list;
445         GSList *list_async;
446
447         zblib_check_null_ret_error("service", service, FALSE);
448
449         /* Refer plug-in list */
450         list = zblib_service_ref_plugins(service);
451         list_async = __zblib_service_ref_async_initializer(service);
452         while (list != NULL) {
453                 /* Initialize each plug-in */
454                 ZigBeePlugin *plugin = (ZigBeePlugin *)(list->data);
455                 if (FALSE == __zblib_service_init_plugin(plugin)) {
456                         /* If there is no initializer, it should have asynchronous one */
457                         const ZblibPluginDescriptor_t *descriptor =
458                                         zblib_plugin_get_descriptor(plugin);
459                         if (NULL != descriptor->init_async) {
460                                 /* Register async initializer */
461                                 struct _zblib_async_init_info *info =
462                                         g_try_new0(struct _zblib_async_init_info, 1);
463                                 if (NULL == info) {
464                                         Z_LOGE("Fatal : Failed to allocate memory");
465                                         return FALSE;
466                                 }
467                                 info->is_finished = FALSE;
468                                 info->is_initialized = FALSE;
469                                 info->plugin = plugin;
470                                 info->user_data = NULL;
471
472                                 list_async = g_slist_append(list_async, info);
473                         } else {
474                                 Z_LOGE("Fatal : Failed to initialize plugin");
475                                 return FALSE;
476                         }
477                 }
478                 list = g_slist_next(list);
479         }
480
481         __zblib_service_set_async_initializer(service, list_async);
482
483         return TRUE;
484 }
485
486 gboolean zblib_service_initialize_async_plugins(ZigBeeService *service)
487 {
488         GSList *list_async;
489
490         zblib_check_null_ret_error("service", service, FALSE);
491
492         /* Refer async initializer list */
493         list_async = __zblib_service_ref_async_initializer(service);
494         while (list_async != NULL) {
495                 /* Initialize each plug-in */
496                 struct _zblib_async_init_info *info =
497                                 (struct _zblib_async_init_info *)(list_async->data);
498
499                 if (G_UNLIKELY(NULL == info)) {
500                         Z_LOGE("Invalid parameter !");
501                         list_async = g_slist_next(list_async);
502                         continue;
503                 }
504
505                 if (G_UNLIKELY(FALSE == __zblib_service_init_async_plugin(
506                                         info->plugin, __on_zblib_init_async_finished, info))) {
507                         Z_LOGE("Fatal : Failed to initialize mandatory plugin");
508                         return FALSE;
509                 }
510                 list_async = g_slist_next(list_async);
511         }
512
513         return TRUE;
514 }
515
516 gboolean zblib_service_unload_plugins(ZigBeeService *service)
517 {
518         GSList *list;
519
520         zblib_check_null_ret_error("service", service, FALSE);
521
522         list = zblib_service_ref_plugins(service);
523         while (list != NULL) {
524                 ZigBeePlugin *plugin = list->data;
525
526                 /* Unload each plug-in */
527                 if (G_UNLIKELY(FALSE == __zblib_service_unload_plugin(plugin))) {
528                         list = g_slist_next(list);
529                         continue;
530                 }
531
532                 list = g_slist_next(list);
533                 zblib_service_remove_plugin(service, plugin);
534         }
535
536         return TRUE;
537 }
538
539 GSList *zblib_service_ref_plugins(ZigBeeService *service)
540 {
541         zblib_check_null_ret_error("service", service, NULL);
542
543         return service->plugins;
544 }
545
546 gboolean zblib_service_add_service_interface(ZigBeeService *service,
547         ZigBeeServiceInterface *service_interface)
548 {
549         gchar *object_name;
550
551         zblib_check_null_ret_error("service", service, FALSE);
552         zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
553
554         object_name = zblib_service_interface_get_name(service_interface);
555
556         /*
557          * All service interface objects would be appended
558          */
559         service->interface_objs = g_slist_append(service->interface_objs, service_interface);
560         Z_LOGD("%s added", object_name);
561         g_free(object_name);
562
563         return TRUE;
564 }
565
566 gboolean zblib_service_remove_service_interface(ZigBeeService *service,
567         ZigBeeServiceInterface *service_interface)
568 {
569         zblib_check_null_ret_error("service", service, FALSE);
570         zblib_check_null_ret_error("service_interface_name", service_interface, FALSE);
571
572         /*
573          * service interface object would be removed
574          */
575         service->interface_objs = g_slist_remove(service->interface_objs, service_interface);
576
577         return TRUE;
578 }
579
580 ZigBeeServiceInterface *zblib_service_ref_service_interface(ZigBeeService *service,
581         const gchar *service_interface_name)
582 {
583         ZigBeeServiceInterface *service_interface = NULL;
584         ZigBeeServiceInterface *tmp_service_interface = NULL;
585         gchar *object_name;
586
587         GSList *list;
588
589         zblib_check_null_ret_error("service", service, NULL);
590         zblib_check_null_ret_error("service_interface_name", service_interface_name, NULL);
591
592         list = service->interface_objs;
593         while (list) {
594                 tmp_service_interface = (ZigBeeServiceInterface *)list->data;
595
596                 /* Get object name of service_interface */
597                 object_name = zblib_service_interface_get_name(tmp_service_interface);
598                 if (g_strcmp0(service_interface_name, object_name) == 0) {
599                         g_free(object_name);
600
601                         service_interface = tmp_service_interface;
602                         break;
603                 }
604                 g_free(object_name);
605
606                 list = g_slist_next(list);
607         }
608
609         if (NULL == service_interface) {
610                 Z_LOGE("Service interface object of name '%s' not found!",
611                         service_interface_name);
612                 return NULL;
613         }
614
615         Z_LOGD("'%s' found", service_interface_name);
616
617         return service_interface;
618 }
619
620 GHashTable *zblib_service_ref_request_hash_table(ZigBeeService *service)
621 {
622         zblib_check_null_ret_error("service", service, NULL);
623
624         return service->request_table;
625 }
626
627 gint zblib_service_generate_request_id(ZigBeeService *service)
628 {
629         zblib_check_null_ret_error("service", service, -1);
630
631         /* Increment request ID */
632         service->request_id++;
633
634         return (gint)service->request_id;
635 }
636
637 gboolean zblib_service_dispatch_request(ZigBeeService *service,
638         guint request_id)
639 {
640         ZigBeePlugin *plugin = NULL;
641         GSList *plugin_list = NULL;
642         gboolean ret = FALSE;
643
644         zblib_check_null_ret_error("service", service, FALSE);
645
646         /* Fetch plugin_list */
647         plugin_list = service->plugins;
648         zblib_check_null_ret_error("plugin_list", plugin_list, FALSE);
649
650         while (plugin_list) {
651                 plugin = (ZigBeePlugin *)plugin_list->data;
652
653                 /* Dispatch request to plugin */
654                 ret = zblib_plugin_dispatch_request(plugin, request_id);
655
656                 /* Move to next plugin */
657                 plugin_list = g_slist_next(plugin_list);
658         }
659
660         return ret;
661 }
662
663 void zblib_service_send_response(ZigBeeService *service,
664         guint request_id, gpointer resp_data, guint resp_data_len)
665 {
666         ZigBeeServiceInterface *service_interface = NULL;
667
668         zblib_check_null_ret("service", service);
669
670         service_interface = zblib_request_ref_service_interface(service, request_id);
671         zblib_check_null_ret("service_interface", service_interface);
672
673         /* Send response to service interface */
674         zblib_service_interface_send_response(service_interface,
675                 request_id, resp_data, resp_data_len);
676 }
677
678 void zblib_service_send_notification(ZigBeeService *service,
679         guint noti_id, gpointer noti_data, guint noti_data_len)
680 {
681         GSList *interface_objs = NULL;
682         ZigBeeServiceInterface *service_interface = NULL;
683
684         zblib_check_null_ret("service", service);
685
686         interface_objs = service->interface_objs;
687         zblib_check_null_ret("interface_objs", interface_objs);
688
689         while (interface_objs) {
690                 service_interface = (ZigBeeServiceInterface *)interface_objs->data;
691
692                 /* Send notification to service interface */
693                 zblib_service_interface_send_notification(service_interface,
694                         noti_id, noti_data, noti_data_len);
695
696                 /* Move to next service interface */
697                 interface_objs = g_slist_next(interface_objs);
698         }
699 }