Chagne DBus object name following Zigbee Specification
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_color_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_color_control.h"
24
25 /**< Private data */
26 typedef struct {
27         ZblibDriverZclColorOps_t *ops; /**< Operations */
28 } ZblibDriverZclColorPrivData_t;
29
30 static gboolean __zblib_driver_zcl_color_control_dispatcher(ZigBeeDriver *driver, guint request_id)
31 {
32         ZigBeeService *service = NULL;
33         ZblibDriverZclColorPrivData_t *priv_data = NULL;
34         ZblibDriverZclColorOps_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_ZCL_COLOR_OPS_MOVE_TO_HUE: {
58                 zblib_check_null_ret_error("ops->move_to_hue",
59                         ops->move_to_hue, FALSE);
60
61                 ret = ops->move_to_hue(driver, request_id);
62         }
63         break;
64
65         case ZBLIB_ZCL_COLOR_OPS_MOVE_HUE: {
66                 zblib_check_null_ret_error("ops->move_hue",
67                         ops->move_hue, FALSE);
68
69                 ret = ops->move_hue(driver, request_id);
70         }
71         break;
72
73         case ZBLIB_ZCL_COLOR_OPS_STEP_HUE: {
74                 zblib_check_null_ret_error("ops->step_hue",
75                         ops->step_hue, FALSE);
76
77                 ret = ops->step_hue(driver, request_id);
78         }
79         break;
80
81         case ZBLIB_ZCL_COLOR_OPS_MOVE_TO_STAURATION: {
82                 zblib_check_null_ret_error("ops->move_to_saturation",
83                         ops->move_to_saturation, FALSE);
84
85                 ret = ops->move_to_saturation(driver, request_id);
86         }
87         break;
88
89         case ZBLIB_ZCL_COLOR_OPS_MOVE_STAURATION: {
90                 zblib_check_null_ret_error("ops->move_saturation",
91                         ops->move_saturation, FALSE);
92
93                 ret = ops->move_saturation(driver, request_id);
94         }
95         break;
96
97         case ZBLIB_ZCL_COLOR_OPS_STEP_STAURATION: {
98                 zblib_check_null_ret_error("ops->step_saturation",
99                         ops->step_saturation, FALSE);
100
101                 ret = ops->step_saturation(driver, request_id);
102         }
103         break;
104
105         case ZBLIB_ZCL_COLOR_OPS_MOVE_TO_HUE_AND_SATURATION: {
106                 zblib_check_null_ret_error("ops->move_to_hue_and_saturation",
107                         ops->move_to_hue_and_saturation, FALSE);
108
109                 ret = ops->move_to_hue_and_saturation(driver, request_id);
110         }
111         break;
112
113         case ZBLIB_ZCL_COLOR_OPS_MOVE_TO_COLOR: {
114                 zblib_check_null_ret_error("ops->move_to_color",
115                         ops->move_to_color, FALSE);
116
117                 ret = ops->move_to_color(driver, request_id);
118         }
119         break;
120
121         case ZBLIB_ZCL_COLOR_OPS_MOVE_COLOR: {
122                 zblib_check_null_ret_error("ops->move_color",
123                         ops->move_color, FALSE);
124
125                 ret = ops->move_color(driver, request_id);
126         }
127         break;
128
129         case ZBLIB_ZCL_COLOR_OPS_STEP_COLOR: {
130                 zblib_check_null_ret_error("ops->step_color",
131                         ops->step_color, FALSE);
132
133                 ret = ops->step_color(driver, request_id);
134         }
135         break;
136
137         case ZBLIB_ZCL_COLOR_OPS_MOVE_COLOR_TEMPERATURE: {
138                 zblib_check_null_ret_error("ops->move_color_temperature",
139                         ops->move_color_temperature, FALSE);
140
141                 ret = ops->move_color_temperature(driver, request_id);
142         }
143         break;
144
145         default:
146         break;
147         }
148
149         Z_LOGD("ret: [%d]", ret);
150
151         return ret;
152 }
153
154 static void __zblib_driver_zcl_color_control_free_hook(ZigBeeDriver *driver)
155 {
156         ZblibDriverZclColorPrivData_t *priv_data = NULL;
157
158         zblib_check_null_ret("driver", driver);
159
160         /* Fetch private data */
161         priv_data = zblib_driver_ref_object(driver);
162         zblib_check_null_ret("priv_data", priv_data);
163
164         /* Free resources */
165         g_free(priv_data);
166 }
167
168 ZigBeeDriver *zblib_driver_zcl_color_control_new(ZigBeePlugin *plugin,
169         const gchar *driver_name,
170         ZblibDriverZclColorOps_t *ops)
171 {
172         ZigBeeDriver *driver = NULL;
173         ZblibDriverZclColorPrivData_t *priv_data = NULL;
174         gboolean ret;
175
176         zblib_check_null_ret_error("plugin", plugin, NULL);
177
178         /* Create new driver */
179         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_COLOR);
180         zblib_check_null_ret_error("driver", driver, NULL);
181
182         /* Allocate memory for private data */
183         priv_data = g_malloc0(sizeof(ZblibDriverZclColorPrivData_t));
184
185         /* Update private data */
186         priv_data->ops = ops;
187
188         /* Link private data to driver */
189         ret = zblib_driver_link_object(driver, priv_data);
190         if (FALSE == ret) {
191                 Z_LOGE("zblib_driver_link_object failed!");
192
193                 /* Free allocated resources */
194                 g_free(priv_data);
195                 g_free(driver);
196
197                 return NULL;
198         }
199
200         /* Set operations dispatcher function */
201         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_color_control_dispatcher);
202         if (FALSE == ret) {
203                 Z_LOGE("zblib_driver_set_dispatcher failed!");
204
205                 /* Free allocated resources */
206                 g_free(priv_data);
207                 g_free(driver);
208
209                 return NULL;
210         }
211
212         /* Set free hook function */
213         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_color_control_free_hook);
214         if (FALSE == ret) {
215                 Z_LOGE("zblib_driver_set_free_hook failed!");
216
217                 /* Free allocated resources */
218                 g_free(priv_data);
219                 g_free(driver);
220
221                 return NULL;
222         }
223
224         return driver;
225 }
226
227 void zblib_driver_zcl_color_control_free(ZigBeeDriver *driver)
228 {
229         zblib_check_null_ret("driver", driver);
230
231         /* Free driver */
232         zblib_driver_free(driver);
233 }