d045afff185884dc88257281d545aa715d643729
[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 #include <zblib_plugin.h>
22 #include <zblib_service.h>
23 #include <zblib_request.h>
24
25 /**< ZigBee driver object */
26 struct zblib_driver_type {
27         ZblibDriverType_e driver_type; /**< ZigBee driver type */
28         gchar *driver_name; /**< ZigBee driver name */
29         gpointer priv_object; /**< ZigBee driver private data */
30
31         ZblibDriverDispatcher_t dispatcher_fn; /**< Driver operations dispatcher function */
32         ZblibDriverFreeHook_t free_hook_fn; /**< Driver free hook function */
33
34         ZigBeePlugin *plugin; /**< ZigBee Plug-in */
35
36         ZigBeeService *service; /**< ZigBee Service */
37 };
38
39 ZigBeeDriver *zblib_driver_new(ZigBeePlugin *plugin,
40         const gchar *driver_name,
41         ZblibDriverType_e driver_type)
42 {
43         ZigBeeDriver *driver = NULL;
44
45         /* Allocate memory */
46         driver = g_malloc0(sizeof(ZigBeeDriver));
47
48         /* Update fields */
49         driver->driver_type = driver_type;
50         driver->driver_name = g_strdup(driver_name);
51         driver->plugin = plugin;
52
53         Z_LOGI("Driver created - Name [%s]", driver->driver_name);
54
55         return driver;
56 }
57
58 void zblib_driver_free(ZigBeeDriver *driver)
59 {
60         zblib_check_null_ret("driver", driver);
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 ZigBeeService *zblib_driver_ref_service(ZigBeeDriver *driver)
73 {
74         zblib_check_null_ret_error("driver", driver, NULL);
75
76         return driver->service;
77 }
78
79 ZigBeePlugin *zblib_driver_ref_plugin(ZigBeeDriver *driver)
80 {
81         zblib_check_null_ret_error("driver", driver, NULL);
82
83         return driver->plugin;
84 }
85
86 gboolean zblib_driver_link_object(ZigBeeDriver *driver,
87         gpointer object)
88 {
89         zblib_check_null_ret_error("driver", driver, FALSE);
90
91         driver->priv_object = object;
92
93         return TRUE;
94 }
95
96 gpointer zblib_driver_ref_object(ZigBeeDriver *driver)
97 {
98         zblib_check_null_ret_error("driver", driver, NULL);
99
100         return driver->priv_object;
101 }
102
103 gboolean zblib_driver_set_dispatcher(ZigBeeDriver *driver,
104         ZblibDriverDispatcher_t dispatcher_fn)
105 {
106         zblib_check_null_ret_error("driver", driver, FALSE);
107
108         driver->dispatcher_fn = dispatcher_fn;
109
110         return TRUE;
111 }
112
113 gboolean zblib_driver_set_free_hook(ZigBeeDriver *driver,
114         ZblibDriverFreeHook_t free_hook_fn)
115 {
116         zblib_check_null_ret_error("driver", driver, FALSE);
117
118         driver->free_hook_fn = free_hook_fn;
119
120         return TRUE;
121 }
122
123 ZblibDriverType_e zblib_driver_ref_driver_type(ZigBeeDriver *driver)
124 {
125         zblib_check_null_ret_error("driver",
126                 driver, ZBLIB_DRIVER_TYPE_NONE);
127
128         return driver->driver_type;
129 }
130
131 gpointer zblib_driver_ref_request_data(ZigBeeDriver *driver,
132         guint request_id)
133 {
134         ZigBeeService *service = NULL;
135
136         zblib_check_null_ret_error("driver", driver, NULL);
137
138         service = driver->service;
139         zblib_check_null_ret_error("service", service, NULL);
140
141         /* Fetch request data */
142         return zblib_request_ref_request_data_by_id(service,
143                 request_id);
144 }
145
146 gboolean zblib_driver_dispatch_request(ZigBeeDriver *driver,
147         guint request_id)
148 {
149         zblib_check_null_ret_error("driver", driver, FALSE);
150         zblib_check_null_ret_error("driver->dispatcher_fn",
151                 driver->dispatcher_fn, FALSE);
152
153         /* Dispatch request to driver dispatcher function */
154         return driver->dispatcher_fn(driver, request_id);
155 }
156
157 void zblib_driver_send_response(ZigBeeDriver *driver,
158         guint request_id, gpointer resp_data, guint resp_data_len)
159 {
160         ZigBeePlugin *plugin = NULL;
161
162         zblib_check_null_ret("driver", driver);
163
164         plugin = driver->plugin;
165         zblib_check_null_ret("plugin", plugin);
166
167         /* Send response to plugin */
168         zblib_plugin_send_response(plugin,
169                 request_id, resp_data, resp_data_len);
170 }