Impelement Custom cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_custom.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_request.h>
23
24 #include "zblib_driver_custom.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverCustomOps_t *ops; /**< Operations */
29 } ZblibDriverCustomPrivData_t;
30
31 static gboolean __zblib_driver_custom_dispatcher(ZigBeeDriver *driver, guint request_id)
32 {
33         ZigBeeService *service = NULL;
34         ZblibDriverCustomPrivData_t *priv_data = NULL;
35         ZblibDriverCustomOps_t *ops = NULL;
36         guint request_type;
37         guint ops_id;
38         gboolean ret = FALSE;
39
40         zblib_check_null_ret_error("driver", driver, FALSE);
41
42         service = zblib_driver_ref_service(driver);
43         zblib_check_null_ret_error("service", service, FALSE);
44
45         /* Fetch private data */
46         priv_data = zblib_driver_ref_object(driver);
47         zblib_check_null_ret_error("priv_data", priv_data, FALSE);
48
49         ops = priv_data->ops;
50         zblib_check_null_ret_error("ops", ops, FALSE);
51
52         /* Fetch request_type using request_id */
53         request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
54
55         /* Fetch ops ID */
56         ops_id = zblib_request_get_ops_id(request_type);
57         switch (ops_id) {
58         case ZBLIB_CUSTOM_OPS_APS_SEND: {
59                 zblib_check_null_ret_error("ops->aps_send",
60                         ops->aps_send, FALSE);
61
62                 ret = ops->aps_send(driver, request_id);
63         }
64         break;
65
66         case ZBLIB_CUSTOM_OPS_ZCL_SEND: {
67                 zblib_check_null_ret_error("ops->zcl_send",
68                         ops->zcl_send, FALSE);
69
70                 ret = ops->zcl_send(driver, request_id);
71         }
72         break;
73
74         case ZBLIB_CUSTOM_OPS_SEND_TO_LOCAL: {
75                 zblib_check_null_ret_error("ops->send_to_local",
76                         ops->send_to_local, FALSE);
77
78                 ret = ops->send_to_local(driver, request_id);
79         }
80         break;
81
82         default:
83         break;
84         }
85
86         Z_LOGD("ret: [%d]", ret);
87
88         return ret;
89 }
90
91 static void __zblib_driver_custom_free_hook(ZigBeeDriver *driver)
92 {
93         ZblibDriverCustomPrivData_t *priv_data = NULL;
94
95         zblib_check_null_ret("driver", driver);
96
97         /* Fetch private data */
98         priv_data = zblib_driver_ref_object(driver);
99         zblib_check_null_ret("priv_data", priv_data);
100
101         /* Free resources */
102         g_free(priv_data);
103 }
104
105 ZigBeeDriver *zblib_driver_custom_new(ZigBeePlugin *plugin,
106         const gchar *driver_name,
107         ZblibDriverCustomOps_t *ops)
108 {
109         ZigBeeDriver *driver = NULL;
110         ZigBeeService *service = NULL;
111         ZblibDriverCustomPrivData_t *priv_data = NULL;
112         gboolean ret;
113
114         zblib_check_null_ret_error("plugin", plugin, NULL);
115
116         /* Create new driver */
117         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_CUSTOM);
118         zblib_check_null_ret_error("driver", driver, NULL);
119
120         /* Allocate memory for private data */
121         priv_data = g_malloc0(sizeof(ZblibDriverCustomPrivData_t));
122
123         /* Update private data */
124         priv_data->ops = ops;
125
126         /* Link service to driver */
127         service = zblib_plugin_ref_service(plugin);
128         if (NULL == service) {
129                 Z_LOGE("zblib_plugin_ref_service failed!");
130
131                 /* Free allocated resources */
132                 g_free(priv_data);
133                 g_free(driver);
134
135                 return NULL;
136         }
137         zblib_driver_link_service(driver, service);
138
139         /* Link private data to driver */
140         ret = zblib_driver_link_object(driver, priv_data);
141         if (FALSE == ret) {
142                 Z_LOGE("zblib_driver_link_object failed!");
143
144                 /* Free allocated resources */
145                 g_free(priv_data);
146                 g_free(driver);
147
148                 return NULL;
149         }
150
151         /* Set operations dispatcher function */
152         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_custom_dispatcher);
153         if (FALSE == ret) {
154                 Z_LOGE("zblib_driver_set_dispatcher failed!");
155
156                 /* Free allocated resources */
157                 g_free(priv_data);
158                 g_free(driver);
159
160                 return NULL;
161         }
162
163         /* Set free hook function */
164         ret = zblib_driver_set_free_hook(driver, __zblib_driver_custom_free_hook);
165         if (FALSE == ret) {
166                 Z_LOGE("zblib_driver_set_free_hook failed!");
167
168                 /* Free allocated resources */
169                 g_free(priv_data);
170                 g_free(driver);
171
172                 return NULL;
173         }
174
175         return driver;
176 }
177
178 void zblib_driver_custom_free(ZigBeeDriver *driver)
179 {
180         zblib_check_null_ret("driver", driver);
181
182         /* Free driver */
183         zblib_driver_free(driver);
184 }