Add module APIs in zigbee-lib
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / zblib_driver.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 <zblib.h>
20 #include <zblib_driver.h>
21
22 /**< ZigBee driver object */
23 struct zblib_driver_type {
24         ZblibDriverType_e driver_type; /**< ZigBee driver type */
25         gchar *driver_name; /**< ZigBee driver name */
26         gpointer priv_object; /**< ZigBee driver private data */
27
28         ZblibDriverDispatcher_t dispatcher_fn; /**< Driver operations dispatcher function */
29         ZblibDriverFreeHook_t free_hook_fn; /**< Driver free hook function */
30
31         ZigBeePlugin *plugin; /**< ZigBee Plug-in */
32
33         ZigBeeService *service; /**< ZigBee Service */
34 };
35
36 ZigBeeDriver *zblib_driver_new(ZigBeePlugin *plugin,
37         const gchar *driver_name,
38         ZblibDriverType_e driver_type)
39 {
40         ZigBeeDriver *driver = NULL;
41
42         /* Allocate memory */
43         driver = g_malloc0(sizeof(ZigBeeDriver));
44
45         /* Update fields */
46         driver->driver_type = driver_type;
47         driver->driver_name = g_strdup(driver_name);
48         driver->plugin = plugin;
49
50         Z_LOGI("Driver created - Name [%s]", driver->driver_name);
51
52         return driver;
53 }
54
55 void zblib_driver_free(ZigBeeDriver *driver)
56 {
57         if (driver == NULL) {
58                 Z_LOGE("driver is NULL");
59                 return;
60         }
61
62         /* Invoke free hook */
63         if (driver->free_hook_fn)
64                 driver->free_hook_fn(driver);
65
66         Z_LOGI("Freeing Driver - Name [%p]", driver->driver_name);
67
68         /* Free driver memory */
69         g_free(driver);
70 }
71
72 gboolean zblib_driver_link_object(ZigBeeDriver *driver, gpointer object)
73 {
74         if (driver == NULL) {
75                 Z_LOGE("driver is NULL");
76                 return FALSE;
77         }
78
79         driver->priv_object = object;
80
81         return TRUE;
82 }
83
84 gpointer zblib_driver_ref_object(ZigBeeDriver *driver)
85 {
86         if (driver == NULL) {
87                 Z_LOGE("driver is NULL");
88                 return NULL;
89         }
90
91         return driver->priv_object;
92 }
93
94 gboolean zblib_driver_set_dispatcher(ZigBeeDriver *driver, ZblibDriverDispatcher_t dispatcher_fn)
95 {
96         if (driver == NULL) {
97                 Z_LOGE("driver is NULL");
98                 return FALSE;
99         }
100
101         driver->dispatcher_fn = dispatcher_fn;
102
103         return TRUE;
104 }
105
106 gboolean zblib_driver_set_free_hook(ZigBeeDriver *driver, ZblibDriverFreeHook_t free_hook_fn)
107 {
108         if (driver == NULL) {
109                 Z_LOGE("driver is NULL");
110                 return FALSE;
111         }
112
113         driver->free_hook_fn = free_hook_fn;
114
115         return TRUE;
116 }
117
118 ZblibDriverType_e zblib_driver_ref_driver_type(ZigBeeDriver *driver)
119 {
120         if (driver == NULL) {
121                 Z_LOGE("driver is NULL");
122                 return ZBLIB_DRIVER_TYPE_NONE;
123         }
124
125         return driver->driver_type;
126 }