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