Implement ZCL Level Control cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_level_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_plugin.h>
22 #include <zblib_request.h>
23
24 #include "zblib_driver_zcl_level_control.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverZclLevelControlOps_t *ops; /**< Operations */
29 } ZblibDriverZclLevelControlPrivData_t;
30
31 static gboolean __zblib_driver_zcl_level_control_dispatcher(ZigBeeDriver *driver, guint request_id)
32 {
33         ZigBeeService *service = NULL;
34         ZblibDriverZclLevelControlPrivData_t *priv_data = NULL;
35         ZblibDriverZclLevelControlOps_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_ZCL_LEVEL_CONTROL_OPS_MOVE_TO_LEVEL: {
59                 zblib_check_null_ret_error("ops->move_to_level",
60                         ops->move_to_level, FALSE);
61
62                 ret = ops->move_to_level(driver, request_id);
63         }
64         break;
65
66         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_MOVE: {
67                 zblib_check_null_ret_error("ops->move",
68                         ops->move, FALSE);
69
70                 ret = ops->move(driver, request_id);
71         }
72         break;
73
74         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_STEP: {
75                 zblib_check_null_ret_error("ops->step",
76                         ops->step, FALSE);
77
78                 ret = ops->step(driver, request_id);
79         }
80         break;
81
82         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_MOVE_TO_LEVEL_WITH_ON_OFF: {
83                 zblib_check_null_ret_error("ops->move_to_level_with_on_off",
84                         ops->move_to_level_with_on_off, FALSE);
85
86                 ret = ops->move_to_level_with_on_off(driver, request_id);
87         }
88         break;
89
90         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_MOVE_WITH_ON_OFF: {
91                 zblib_check_null_ret_error("ops->move_with_on_off",
92                         ops->move_with_on_off, FALSE);
93
94                 ret = ops->move_with_on_off(driver, request_id);
95         }
96         break;
97
98         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_STEP_WITH_ON_OFF: {
99                 zblib_check_null_ret_error("ops->step_with_on_off",
100                         ops->step_with_on_off, FALSE);
101
102                 ret = ops->step_with_on_off(driver, request_id);
103         }
104         break;
105
106         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_STOP: {
107                 zblib_check_null_ret_error("ops->stop",
108                         ops->stop, FALSE);
109
110                 ret = ops->stop(driver, request_id);
111         }
112         break;
113
114         default:
115         break;
116         }
117
118         Z_LOGD("ret: [%d]", ret);
119
120         return ret;
121 }
122
123 static void __zblib_driver_zcl_level_control_free_hook(ZigBeeDriver *driver)
124 {
125         ZblibDriverZclLevelControlPrivData_t *priv_data = NULL;
126
127         zblib_check_null_ret("driver", driver);
128
129         /* Fetch private data */
130         priv_data = zblib_driver_ref_object(driver);
131         zblib_check_null_ret("priv_data", priv_data);
132
133         /* Free resources */
134         g_free(priv_data);
135 }
136
137 ZigBeeDriver *zblib_driver_zcl_level_control_new(ZigBeePlugin *plugin,
138         const gchar *driver_name,
139         ZblibDriverZclLevelControlOps_t *ops)
140 {
141         ZigBeeDriver *driver = NULL;
142         ZigBeeService *service = NULL;
143         ZblibDriverZclLevelControlPrivData_t *priv_data = NULL;
144         gboolean ret;
145
146         zblib_check_null_ret_error("plugin", plugin, NULL);
147
148         /* Create new driver */
149         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_LEVEL_CONTROL);
150         zblib_check_null_ret_error("driver", driver, NULL);
151
152         /* Allocate memory for private data */
153         priv_data = g_malloc0(sizeof(ZblibDriverZclLevelControlPrivData_t));
154
155         /* Update private data */
156         priv_data->ops = ops;
157
158         /* Link service to driver */
159         service = zblib_plugin_ref_service(plugin);
160         if (NULL == service) {
161                 Z_LOGE("zblib_plugin_ref_service failed!");
162
163                 /* Free allocated resources */
164                 g_free(priv_data);
165                 g_free(driver);
166
167                 return NULL;
168         }
169         zblib_driver_link_service(driver, service);
170
171         /* Link private data to driver */
172         ret = zblib_driver_link_object(driver, priv_data);
173         if (FALSE == ret) {
174                 Z_LOGE("zblib_driver_link_object failed!");
175
176                 /* Free allocated resources */
177                 g_free(priv_data);
178                 g_free(driver);
179
180                 return NULL;
181         }
182
183         /* Set operations dispatcher function */
184         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_level_control_dispatcher);
185         if (FALSE == ret) {
186                 Z_LOGE("zblib_driver_set_dispatcher failed!");
187
188                 /* Free allocated resources */
189                 g_free(priv_data);
190                 g_free(driver);
191
192                 return NULL;
193         }
194
195         /* Set free hook function */
196         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_level_control_free_hook);
197         if (FALSE == ret) {
198                 Z_LOGE("zblib_driver_set_free_hook failed!");
199
200                 /* Free allocated resources */
201                 g_free(priv_data);
202                 g_free(driver);
203
204                 return NULL;
205         }
206
207         return driver;
208 }
209
210 void zblib_driver_zcl_level_control_free(ZigBeeDriver *driver)
211 {
212         zblib_check_null_ret("driver", driver);
213
214         /* Free driver */
215         zblib_driver_free(driver);
216 }