b6d15385808801991d9774615585666b749eeb7c
[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         zblib_check_null_ret("driver", driver);
58
59         /* Invoke free hook */
60         if (driver->free_hook_fn)
61                 driver->free_hook_fn(driver);
62
63         Z_LOGI("Freeing Driver - Name [%p]", driver->driver_name);
64
65         /* Free driver memory */
66         g_free(driver);
67 }
68
69 ZigBeeService *zblib_driver_ref_service(ZigBeeDriver *driver)
70 {
71         zblib_check_null_ret_error("driver", driver, NULL);
72
73         return driver->service;
74 }
75
76 ZigBeePlugin *zblib_driver_ref_plugin(ZigBeeDriver *driver)
77 {
78         zblib_check_null_ret_error("driver", driver, NULL);
79
80         return driver->plugin;
81 }
82
83 gboolean zblib_driver_link_object(ZigBeeDriver *driver,
84         gpointer object)
85 {
86         zblib_check_null_ret_error("driver", driver, FALSE);
87
88         driver->priv_object = object;
89
90         return TRUE;
91 }
92
93 gpointer zblib_driver_ref_object(ZigBeeDriver *driver)
94 {
95         zblib_check_null_ret_error("driver", driver, NULL);
96
97         return driver->priv_object;
98 }
99
100 gboolean zblib_driver_set_dispatcher(ZigBeeDriver *driver,
101         ZblibDriverDispatcher_t dispatcher_fn)
102 {
103         zblib_check_null_ret_error("driver", driver, FALSE);
104
105         driver->dispatcher_fn = dispatcher_fn;
106
107         return TRUE;
108 }
109
110 gboolean zblib_driver_set_free_hook(ZigBeeDriver *driver,
111         ZblibDriverFreeHook_t free_hook_fn)
112 {
113         zblib_check_null_ret_error("driver", driver, FALSE);
114
115         driver->free_hook_fn = free_hook_fn;
116
117         return TRUE;
118 }
119
120 ZblibDriverType_e zblib_driver_ref_driver_type(ZigBeeDriver *driver)
121 {
122         zblib_check_null_ret_error("driver",
123                 driver, ZBLIB_DRIVER_TYPE_NONE);
124
125         return driver->driver_type;
126 }
127
128 gboolean zblib_driver_dispatch_request(ZigBeeDriver *driver,
129         guint request_id)
130 {
131         zblib_check_null_ret_error("driver", driver, FALSE);
132         zblib_check_null_ret_error("driver->dispatcher_fn",
133                 driver->dispatcher_fn, FALSE);
134
135         /* Dispatch request to driver dispatcher function */
136         return driver->dispatcher_fn(driver, request_id);
137 }