Chagne DBus object name following Zigbee Specification
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_global_control.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_request.h>
22
23 #include "zblib_driver_zcl_global_control.h"
24
25 /**< Private data */
26 typedef struct {
27         ZblibDriverZclglobalControlOps_t *ops; /**< Operations */
28 } ZblibDriverZclglobalControlPrivData_t;
29
30 static gboolean __zblib_driver_zcl_global_control_dispatcher(ZigBeeDriver *driver, guint request_id)
31 {
32         ZigBeeService *service = NULL;
33         ZblibDriverZclglobalControlPrivData_t *priv_data = NULL;
34         ZblibDriverZclglobalControlOps_t *ops = NULL;
35         guint request_type;
36         guint ops_id;
37         gboolean ret = FALSE;
38
39         zblib_check_null_ret_error("driver", driver, FALSE);
40
41         service = zblib_driver_ref_service(driver);
42         zblib_check_null_ret_error("service", service, FALSE);
43
44         /* Fetch private data */
45         priv_data = zblib_driver_ref_object(driver);
46         zblib_check_null_ret_error("priv_data", priv_data, FALSE);
47
48         ops = priv_data->ops;
49         zblib_check_null_ret_error("ops", ops, FALSE);
50
51         /* Fetch request_type using request_id */
52         request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
53
54         /* Fetch ops ID */
55         ops_id = zblib_request_get_ops_id(request_type);
56         switch (ops_id) {
57         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_READ_ATTRIBUTES_REQ: {
58                 zblib_check_null_ret_error("ops->read_attributes_req",
59                         ops->read_attributes_req, FALSE);
60
61                 ret = ops->read_attributes_req(driver, request_id);
62         }
63         break;
64
65         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_WRITE_ATTRIBUTES_REQ: {
66                 zblib_check_null_ret_error("ops->write_attributes_req",
67                         ops->write_attributes_req, FALSE);
68
69                 ret = ops->write_attributes_req(driver, request_id);
70         }
71         break;
72
73         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_WRITE_ATTRIBUTES_UNDIVIDED_REQ: {
74                 zblib_check_null_ret_error("ops->write_attributes_undivided_req",
75                         ops->write_attributes_undivided_req, FALSE);
76
77                 ret = ops->write_attributes_undivided_req(driver, request_id);
78         }
79         break;
80
81         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_WRITE_ATTRIBUTES_NO_RESP: {
82                 zblib_check_null_ret_error("ops->write_attributes_no_resp",
83                         ops->write_attributes_no_resp, FALSE);
84
85                 ret = ops->write_attributes_no_resp(driver, request_id);
86         }
87         break;
88
89         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_WRITE_ATTRIBUTES_STRUCTURED: {
90                 zblib_check_null_ret_error("ops->write_attributes_structured",
91                         ops->write_attributes_structured, FALSE);
92
93                 ret = ops->write_attributes_structured(driver, request_id);
94         }
95         break;
96
97         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_READ_ATTRIBUTES_STRUCTURED: {
98                 zblib_check_null_ret_error("ops->read_attributes_structured",
99                         ops->read_attributes_structured, FALSE);
100
101                 ret = ops->read_attributes_structured(driver, request_id);
102         }
103         break;
104
105         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_CONFIGURE_REPORTING_REQ: {
106                 zblib_check_null_ret_error("ops->configure_reporting_req",
107                         ops->configure_reporting_req, FALSE);
108
109                 ret = ops->configure_reporting_req(driver, request_id);
110         }
111         break;
112
113         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_READ_CONFIGURE_REPORTING: {
114                 zblib_check_null_ret_error("ops->read_configure_reporting",
115                         ops->read_configure_reporting, FALSE);
116
117                 ret = ops->read_configure_reporting(driver, request_id);
118         }
119         break;
120
121         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_DISCOVER_ATTRIBUTES: {
122                 zblib_check_null_ret_error("ops->discover_attributes",
123                         ops->discover_attributes, FALSE);
124
125                 ret = ops->discover_attributes(driver, request_id);
126         }
127         break;
128
129         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_DISCOVER_ATTRIBUTES_EXTENDED: {
130                 zblib_check_null_ret_error("ops->discover_attributes_extended",
131                         ops->discover_attributes_extended, FALSE);
132
133                 ret = ops->discover_attributes_extended(driver, request_id);
134         }
135         break;
136
137         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_DISCOVER_COMMANDS_RECEIVED: {
138                 zblib_check_null_ret_error("ops->discover_commands_received",
139                         ops->discover_commands_received, FALSE);
140
141                 ret = ops->discover_commands_received(driver, request_id);
142         }
143         break;
144
145         case ZBLIB_ZCLGLOBAL_CONTROL_OPS_DISCOVER_COMMANDS_GENERATED: {
146                 zblib_check_null_ret_error("ops->discover_commands_generated",
147                         ops->discover_commands_generated, FALSE);
148
149                 ret = ops->discover_commands_generated(driver, request_id);
150         }
151         break;
152
153         default:
154         break;
155         }
156
157         Z_LOGD("ret: [%d]", ret);
158
159         return ret;
160 }
161
162 static void __zblib_driver_zcl_global_control_free_hook(ZigBeeDriver *driver)
163 {
164         ZblibDriverZclglobalControlPrivData_t *priv_data = NULL;
165
166         zblib_check_null_ret("driver", driver);
167
168         /* Fetch private data */
169         priv_data = zblib_driver_ref_object(driver);
170         zblib_check_null_ret("priv_data", priv_data);
171
172         /* Free resources */
173         g_free(priv_data);
174 }
175
176 ZigBeeDriver *zblib_driver_zcl_global_control_new(ZigBeePlugin *plugin,
177         const gchar *driver_name,
178         ZblibDriverZclglobalControlOps_t *ops)
179 {
180         ZigBeeDriver *driver = NULL;
181         ZblibDriverZclglobalControlPrivData_t *priv_data = NULL;
182         gboolean ret;
183
184         zblib_check_null_ret_error("plugin", plugin, NULL);
185
186         /* Create new driver */
187         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCLGLOBAL_CONTROL);
188         zblib_check_null_ret_error("driver", driver, NULL);
189
190         /* Allocate memory for private data */
191         priv_data = g_malloc0(sizeof(ZblibDriverZclglobalControlPrivData_t));
192
193         /* Update private data */
194         priv_data->ops = ops;
195
196         /* Link private data to driver */
197         ret = zblib_driver_link_object(driver, priv_data);
198         if (FALSE == ret) {
199                 Z_LOGE("zblib_driver_link_object failed!");
200
201                 /* Free allocated resources */
202                 g_free(priv_data);
203                 g_free(driver);
204
205                 return NULL;
206         }
207
208         /* Set operations dispatcher function */
209         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_global_control_dispatcher);
210         if (FALSE == ret) {
211                 Z_LOGE("zblib_driver_set_dispatcher failed!");
212
213                 /* Free allocated resources */
214                 g_free(priv_data);
215                 g_free(driver);
216
217                 return NULL;
218         }
219
220         /* Set free hook function */
221         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_global_control_free_hook);
222         if (FALSE == ret) {
223                 Z_LOGE("zblib_driver_set_free_hook failed!");
224
225                 /* Free allocated resources */
226                 g_free(priv_data);
227                 g_free(driver);
228
229                 return NULL;
230         }
231
232         return driver;
233 }
234
235 void zblib_driver_zcl_global_control_free(ZigBeeDriver *driver)
236 {
237         zblib_check_null_ret("driver", driver);
238
239         /* Free driver */
240         zblib_driver_free(driver);
241 }