Implement ZCL Thermostat cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_thermostat.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_thermostat.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverZclThermostatOps_t *ops; /**< Operations */
29 } ZblibDriverZclThermostatPrivData_t;
30
31 static gboolean __zblib_driver_zcl_thermostat_dispatcher(ZigBeeDriver *driver, guint request_id)
32 {
33         ZigBeeService *service = NULL;
34         ZblibDriverZclThermostatPrivData_t *priv_data = NULL;
35         ZblibDriverZclThermostatOps_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_THERMOSTAT_OPS_GET_LOCAL_TEMP: {
59                 zblib_check_null_ret_error("ops->get_local_temp",
60                         ops->get_local_temp, FALSE);
61
62                 ret = ops->get_local_temp(driver, request_id);
63         }
64         break;
65
66         case ZBLIB_ZCL_THERMOSTAT_OPS_GET_WEEKLY_SCHEDULE: {
67                 zblib_check_null_ret_error("ops->get_weekly_schedule",
68                         ops->get_weekly_schedule, FALSE);
69
70                 ret = ops->get_weekly_schedule(driver, request_id);
71         }
72         break;
73
74         case ZBLIB_ZCL_THERMOSTAT_OPS_SET_WEEKLY_SCHEDULE: {
75                 zblib_check_null_ret_error("ops->set_weekly_schedule",
76                         ops->set_weekly_schedule, FALSE);
77
78                 ret = ops->set_weekly_schedule(driver, request_id);
79         }
80         break;
81
82         case ZBLIB_ZCL_THERMOSTAT_OPS_CLEAR_WEEKLY_SCHEDULE: {
83                 zblib_check_null_ret_error("ops->clear_weekly_schedule",
84                         ops->clear_weekly_schedule, FALSE);
85
86                 ret = ops->clear_weekly_schedule(driver, request_id);
87         }
88         break;
89
90         case ZBLIB_ZCL_THERMOSTAT_OPS_SETPOINT_RAISE_LOWER: {
91                 zblib_check_null_ret_error("ops->setpoint_raise_lower",
92                         ops->setpoint_raise_lower, FALSE);
93
94                 ret = ops->setpoint_raise_lower(driver, request_id);
95         }
96         break;
97
98         default:
99         break;
100         }
101
102         Z_LOGD("ret: [%d]", ret);
103
104         return ret;
105 }
106
107 static void __zblib_driver_zcl_thermostat_free_hook(ZigBeeDriver *driver)
108 {
109         ZblibDriverZclThermostatPrivData_t *priv_data = NULL;
110
111         zblib_check_null_ret("driver", driver);
112
113         /* Fetch private data */
114         priv_data = zblib_driver_ref_object(driver);
115         zblib_check_null_ret("priv_data", priv_data);
116
117         /* Free resources */
118         g_free(priv_data);
119 }
120
121 ZigBeeDriver *zblib_driver_zcl_thermostat_new(ZigBeePlugin *plugin,
122         const gchar *driver_name,
123         ZblibDriverZclThermostatOps_t *ops)
124 {
125         ZigBeeDriver *driver = NULL;
126         ZigBeeService *service = NULL;
127         ZblibDriverZclThermostatPrivData_t *priv_data = NULL;
128         gboolean ret;
129
130         zblib_check_null_ret_error("plugin", plugin, NULL);
131
132         /* Create new driver */
133         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT);
134         zblib_check_null_ret_error("driver", driver, NULL);
135
136         /* Allocate memory for private data */
137         priv_data = g_malloc0(sizeof(ZblibDriverZclThermostatPrivData_t));
138
139         /* Update private data */
140         priv_data->ops = ops;
141
142         /* Link service to driver */
143         service = zblib_plugin_ref_service(plugin);
144         if (NULL == service) {
145                 Z_LOGE("zblib_plugin_ref_service failed!");
146
147                 /* Free allocated resources */
148                 g_free(priv_data);
149                 g_free(driver);
150
151                 return NULL;
152         }
153         zblib_driver_link_service(driver, service);
154
155         /* Link private data to driver */
156         ret = zblib_driver_link_object(driver, priv_data);
157         if (FALSE == ret) {
158                 Z_LOGE("zblib_driver_link_object failed!");
159
160                 /* Free allocated resources */
161                 g_free(priv_data);
162                 g_free(driver);
163
164                 return NULL;
165         }
166
167         /* Set operations dispatcher function */
168         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_thermostat_dispatcher);
169         if (FALSE == ret) {
170                 Z_LOGE("zblib_driver_set_dispatcher failed!");
171
172                 /* Free allocated resources */
173                 g_free(priv_data);
174                 g_free(driver);
175
176                 return NULL;
177         }
178
179         /* Set free hook function */
180         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_thermostat_free_hook);
181         if (FALSE == ret) {
182                 Z_LOGE("zblib_driver_set_free_hook failed!");
183
184                 /* Free allocated resources */
185                 g_free(priv_data);
186                 g_free(driver);
187
188                 return NULL;
189         }
190
191         return driver;
192 }
193
194 void zblib_driver_zcl_thermostat_free(ZigBeeDriver *driver)
195 {
196         zblib_check_null_ret("driver", driver);
197
198         /* Free driver */
199         zblib_driver_free(driver);
200 }