2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Contact: Suresh Kumar N (suresh.n@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <zblib_driver.h>
21 #include <zblib_plugin.h>
22 #include <zblib_service.h>
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 */
30 ZblibDriverDispatcher_t dispatcher_fn; /**< Driver operations dispatcher function */
31 ZblibDriverFreeHook_t free_hook_fn; /**< Driver free hook function */
33 ZigBeePlugin *plugin; /**< ZigBee Plug-in */
35 ZigBeeService *service; /**< ZigBee Service */
38 ZigBeeDriver *zblib_driver_new(ZigBeePlugin *plugin,
39 const gchar *driver_name,
40 ZblibDriverType_e driver_type)
42 ZigBeeDriver *driver = NULL;
45 driver = g_malloc0(sizeof(ZigBeeDriver));
48 driver->driver_type = driver_type;
49 driver->driver_name = g_strdup(driver_name);
50 driver->plugin = plugin;
52 Z_LOGI("Driver created - Name [%s]", driver->driver_name);
57 void zblib_driver_free(ZigBeeDriver *driver)
59 zblib_check_null_ret("driver", driver);
61 /* Invoke free hook */
62 if (driver->free_hook_fn)
63 driver->free_hook_fn(driver);
65 Z_LOGI("Freeing Driver - Name [%p]", driver->driver_name);
67 /* Free driver memory */
71 ZigBeeService *zblib_driver_ref_service(ZigBeeDriver *driver)
73 zblib_check_null_ret_error("driver", driver, NULL);
75 return driver->service;
78 ZigBeePlugin *zblib_driver_ref_plugin(ZigBeeDriver *driver)
80 zblib_check_null_ret_error("driver", driver, NULL);
82 return driver->plugin;
85 gboolean zblib_driver_link_object(ZigBeeDriver *driver,
88 zblib_check_null_ret_error("driver", driver, FALSE);
90 driver->priv_object = object;
95 gpointer zblib_driver_ref_object(ZigBeeDriver *driver)
97 zblib_check_null_ret_error("driver", driver, NULL);
99 return driver->priv_object;
102 gboolean zblib_driver_set_dispatcher(ZigBeeDriver *driver,
103 ZblibDriverDispatcher_t dispatcher_fn)
105 zblib_check_null_ret_error("driver", driver, FALSE);
107 driver->dispatcher_fn = dispatcher_fn;
112 gboolean zblib_driver_set_free_hook(ZigBeeDriver *driver,
113 ZblibDriverFreeHook_t free_hook_fn)
115 zblib_check_null_ret_error("driver", driver, FALSE);
117 driver->free_hook_fn = free_hook_fn;
122 ZblibDriverType_e zblib_driver_ref_driver_type(ZigBeeDriver *driver)
124 zblib_check_null_ret_error("driver",
125 driver, ZBLIB_DRIVER_TYPE_NONE);
127 return driver->driver_type;
130 gboolean zblib_driver_dispatch_request(ZigBeeDriver *driver,
133 zblib_check_null_ret_error("driver", driver, FALSE);
134 zblib_check_null_ret_error("driver->dispatcher_fn",
135 driver->dispatcher_fn, FALSE);
137 /* Dispatch request to driver dispatcher function */
138 return driver->dispatcher_fn(driver, request_id);
141 void zblib_driver_send_response(ZigBeeDriver *driver,
142 guint request_id, gpointer resp_data, guint resp_data_len)
144 ZigBeePlugin *plugin = NULL;
146 zblib_check_null_ret("driver", driver);
148 plugin = driver->plugin;
149 zblib_check_null_ret("plugin", plugin);
151 /* Send response to plugin */
152 zblib_plugin_send_response(plugin,
153 request_id, resp_data, resp_data_len);