Request ID (skeleton)
[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
25 /**< ZigBee plug-in object */
26 struct zblib_plugin_type {
27         gchar *plugin_name; /**< ZigBee plug-in name */
28         const struct zblib_plugin_descriptor *descriptor; /**< ZigBee plug-in descriptor */
29         void *plugin_handle; /**< ZigBee Plug-in .so handle */
30
31         GSList *driver_list; /**< List of ZigBee drivers */
32
33         ZigBeeService *service; /**< ZigBee Service */
34 };
35
36 ZigBeePlugin *zblib_plugin_new(ZigBeeService *service,
37         const gchar *plugin_name,
38         const struct zblib_plugin_descriptor *descriptor,
39         void *plugin_handle)
40 {
41         ZigBeePlugin *plugin = NULL;
42
43         /* Allocate memory */
44         plugin = g_malloc0(sizeof(ZigBeePlugin));
45
46         /* Update fields */
47         plugin->plugin_name = g_strdup(plugin_name);
48         plugin->descriptor = descriptor;
49         plugin->plugin_handle = plugin_handle;
50         plugin->service = service;
51
52         Z_LOGI("Vendor plug-in created - Name [%s]", descriptor->name);
53
54         return plugin;
55 }
56
57 void zblib_plugin_free(ZigBeePlugin *plugin)
58 {
59         if (NULL == plugin) {
60                 Z_LOGE("ZigBee vendor plug-in is NULL");
61                 return;
62         }
63
64         Z_LOGI("Freeing Vendor plug-in - Name [%s]", plugin->descriptor->name);
65
66         if (plugin->driver_list) {
67                 GSList *list;
68                 ZigBeeDriver *driver;
69
70                 for (list = plugin->driver_list; list; list = list->next) {
71                         driver = list->data;
72                         if (NULL == driver)
73                                 continue;
74
75                         zblib_driver_free(driver);
76                         list->data = NULL;
77                 }
78
79                 g_slist_free(plugin->driver_list);
80                 plugin->driver_list = NULL;
81         }
82
83         if (plugin->plugin_name) {
84                 g_free(plugin->plugin_name);
85                 plugin->plugin_name = NULL;
86         }
87
88         plugin->descriptor = NULL;
89
90         if (plugin->plugin_handle) {
91                 dlclose(plugin->plugin_handle);
92                 plugin->plugin_handle = NULL;
93         }
94
95         g_free(plugin);
96 }
97
98 const struct zblib_plugin_descriptor *zblib_plugin_get_descriptor(ZigBeePlugin *plugin)
99 {
100         if (NULL == plugin) {
101                 Z_LOGE("ZigBee vendor plug-in is NULL");
102                 return NULL;
103         }
104
105         return plugin->descriptor;
106 }
107
108 char *zblib_plugin_get_plugin_name(ZigBeePlugin *plugin)
109 {
110         if (NULL == plugin) {
111                 Z_LOGE("ZigBee vendor plug-in is NULL");
112                 return NULL;
113         }
114
115         return g_strdup(plugin->plugin_name);
116 }
117
118 ZigBeeDriver *zblib_plugin_ref_driver(ZigBeePlugin *plugin, ZblibDriverType_e driver_type)
119 {
120         ZigBeeDriver *driver = NULL;
121         ZblibDriverType_e _driver_type;
122         GSList *list = NULL;
123
124         if (NULL == plugin) {
125                 Z_LOGE("ZigBee vendor plug-in is NULL");
126                 return NULL;
127         }
128
129         list = plugin->driver_list;
130         while (list) {
131                 /* Fetch driver type of driver */
132                 _driver_type = zblib_driver_ref_driver_type((ZigBeeDriver *)(list->data));
133                 if (_driver_type == driver_type) {
134                         /* Driver found */
135                         driver = (ZigBeeDriver *)(list->data);
136                         break;
137                 }
138
139                 /* Move to next driver */
140                 list = g_slist_next(list);
141         }
142
143         return driver;
144 }