2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Contact: Suresh Kumar N (suresh.n@samsung.com)
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "zigbee_service_interface_common.h"
21 #include <zblib_driver_zcl_thermostat.h>
23 static void *_service_interface_ref_zigbee_zcl_thermostat(
24 ZigBeeServiceInterface *service_interface)
26 ZigbeeObjectSkeleton *zigbee_object = NULL;
27 ZigbeeCustomData_t *custom_data = NULL;
28 ZigbeeZcl_thermostat *thermo_object = NULL;
30 custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
31 if (NULL == custom_data) {
32 Z_LOGE("D-BUS service interface custom_data is NULL!");
36 /* Get zigbee object */
37 zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
38 if (NULL == zigbee_object) {
39 Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
43 thermo_object = zigbee_object_get_zcl_thermostat(ZIGBEE_OBJECT(zigbee_object));
47 static void on_thermostat_get_local_temp_resp(ZigBeeServiceInterface *service_interface,
48 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
50 ZigbeeServiceInterfaceRespCbData_t *cb_data =
51 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
53 ZigbeeZcl_thermostat *thermo_object = NULL;
54 GDBusMethodInvocation *invocation = NULL;
56 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
58 NOT_USED(service_interface);
61 if (NULL == resp_data || 0 == resp_data_len) {
62 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
67 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
68 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
70 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
71 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
73 zigbee_zcl_thermostat_complete_get_local_temp(thermo_object, invocation,
79 static gboolean on_thermostat_get_local_temp(ZigbeeZcl_thermostat *thermostat_object,
80 GDBusMethodInvocation *invocation,
85 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
86 ZigbeeZclThermostatGetLocalTemp_t req;
87 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
89 GVariantIter *iter = NULL;
94 memset(&req, 0x0, sizeof(ZigbeeZclThermostatGetLocalTemp_t));
96 /* Update request structure */
97 g_variant_get(eui64, "a(y)", &iter);
98 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
100 if (i >= ZIGBEE_EUI64_SIZE)
103 req.endpoint = endpoint;
105 /* Allocate response callback data */
107 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
108 invocation, NULL, 0);
109 if (NULL == resp_cb_data) {
110 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
112 /* Send failure response */
113 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
118 /* Dispatch request */
119 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
120 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
121 ZBLIB_ZCL_THERMOSTAT_OPS_GET_LOCAL_TEMP,
123 on_thermostat_get_local_temp_resp, resp_cb_data);
125 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
127 /* Free response callback data */
128 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
130 /* Send failure response */
131 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
139 static void on_thermostat_get_weekly_schedule_resp(ZigBeeServiceInterface *service_interface,
140 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
142 ZigbeeServiceInterfaceRespCbData_t *cb_data =
143 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
145 ZigbeeZcl_thermostat *thermo_object = NULL;
146 GDBusMethodInvocation *invocation = NULL;
148 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
150 NOT_USED(service_interface);
151 NOT_USED(request_id);
153 if (NULL == resp_data || 0 == resp_data_len) {
154 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
159 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
160 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
162 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
163 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
165 zigbee_zcl_thermostat_complete_get_weekly_schedule(thermo_object, invocation,
171 static gboolean on_thermostat_get_weekly_schedule(ZigbeeZcl_thermostat *thermostat_object,
172 GDBusMethodInvocation *invocation,
179 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
180 ZigbeeZclThermostatGetWeeklySchedule_t req;
181 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
183 GVariantIter *iter = NULL;
188 memset(&req, 0x0, sizeof(ZigbeeZclThermostatGetWeeklySchedule_t));
190 /* Update request structure */
191 g_variant_get(eui64, "a(y)", &iter);
192 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
194 if (i >= ZIGBEE_EUI64_SIZE)
197 req.endpoint = endpoint;
198 req.num_of_days = no_of_days;
201 /* Allocate response callback data */
203 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
204 invocation, NULL, 0);
205 if (NULL == resp_cb_data) {
206 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
208 /* Send failure response */
209 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
214 /* Dispatch request */
215 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
216 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
217 ZBLIB_ZCL_THERMOSTAT_OPS_GET_WEEKLY_SCHEDULE,
219 on_thermostat_get_weekly_schedule_resp, resp_cb_data);
221 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
223 /* Free response callback data */
224 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
226 /* Send failure response */
227 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
235 static void on_thermostat_set_weekly_schedule_resp(ZigBeeServiceInterface *service_interface,
236 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
238 ZigbeeServiceInterfaceRespCbData_t *cb_data =
239 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
241 ZigbeeZcl_thermostat *thermo_object = NULL;
242 GDBusMethodInvocation *invocation = NULL;
244 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
246 NOT_USED(service_interface);
247 NOT_USED(request_id);
249 if (NULL == resp_data || 0 == resp_data_len) {
250 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
255 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
256 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
258 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
259 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
261 zigbee_zcl_thermostat_complete_set_weekly_schedule(thermo_object, invocation,
267 static gboolean on_thermostat_set_weekly_schedule(ZigbeeZcl_thermostat *thermostat_object,
268 GDBusMethodInvocation *invocation,
271 gchar no_of_transitions,
278 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
279 ZigbeeZclThermostatSetWeeklySchedule_t req;
280 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
282 GVariantIter *iter = NULL;
287 memset(&req, 0x0, sizeof(ZigbeeZclThermostatSetWeeklySchedule_t));
289 /* Update request structure */
290 g_variant_get(eui64, "a(y)", &iter);
291 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
293 if (i >= ZIGBEE_EUI64_SIZE)
296 req.endpoint = endpoint;
297 req.no_of_transitions = no_of_transitions;
298 req.num_of_days = no_of_days;
302 g_variant_get(payload, "a(y)", &iter);
303 while (g_variant_iter_loop(iter, "(y)", &(req.payload[i]))) {
305 if (i >= (guint)payload_len)
308 req.payload_len = payload_len;
310 /* Allocate response callback data */
312 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
313 invocation, NULL, 0);
314 if (NULL == resp_cb_data) {
315 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
317 /* Send failure response */
318 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
323 /* Dispatch request */
324 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
325 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
326 ZBLIB_ZCL_THERMOSTAT_OPS_SET_WEEKLY_SCHEDULE,
328 on_thermostat_set_weekly_schedule_resp, resp_cb_data);
330 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
332 /* Free response callback data */
333 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
335 /* Send failure response */
336 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
344 static void on_thermostat_clear_weekly_schedule_resp(ZigBeeServiceInterface *service_interface,
345 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
347 ZigbeeServiceInterfaceRespCbData_t *cb_data =
348 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
350 ZigbeeZcl_thermostat *thermo_object = NULL;
351 GDBusMethodInvocation *invocation = NULL;
353 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
355 NOT_USED(service_interface);
356 NOT_USED(request_id);
358 if (NULL == resp_data || 0 == resp_data_len) {
359 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
364 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
365 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
367 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
368 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
370 zigbee_zcl_thermostat_complete_clear_weekly_schedule(thermo_object, invocation,
376 static gboolean on_thermostat_clear_weekly_schedule(ZigbeeZcl_thermostat *thermostat_object,
377 GDBusMethodInvocation *invocation,
382 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
383 ZigbeeZclThermostatClearWeeklySchedule_t req;
384 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
386 GVariantIter *iter = NULL;
391 memset(&req, 0x0, sizeof(ZigbeeZclThermostatClearWeeklySchedule_t));
393 /* Update request structure */
394 g_variant_get(eui64, "a(y)", &iter);
395 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
397 if (i >= ZIGBEE_EUI64_SIZE)
400 req.endpoint = endpoint;
402 /* Allocate response callback data */
404 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
405 invocation, NULL, 0);
406 if (NULL == resp_cb_data) {
407 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
409 /* Send failure response */
410 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
415 /* Dispatch request */
416 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
417 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
418 ZBLIB_ZCL_THERMOSTAT_OPS_CLEAR_WEEKLY_SCHEDULE,
420 on_thermostat_clear_weekly_schedule_resp, resp_cb_data);
422 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
424 /* Free response callback data */
425 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
427 /* Send failure response */
428 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
436 static void on_thermostat_setpoint_raise_lower_resp(ZigBeeServiceInterface *service_interface,
437 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
439 ZigbeeServiceInterfaceRespCbData_t *cb_data =
440 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
442 ZigbeeZcl_thermostat *thermo_object = NULL;
443 GDBusMethodInvocation *invocation = NULL;
445 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
447 NOT_USED(service_interface);
448 NOT_USED(request_id);
450 if (NULL == resp_data || 0 == resp_data_len) {
451 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
456 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
457 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
459 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
460 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
462 zigbee_zcl_thermostat_complete_setpoint_raise_lower(thermo_object, invocation,
468 static gboolean on_thermostat_setpoint_raise_lower(ZigbeeZcl_thermostat *thermostat_object,
469 GDBusMethodInvocation *invocation,
476 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
477 ZigbeeZclThermostatSetpointRaiseLower_t req;
478 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
482 memset(&req, 0x0, sizeof(ZigbeeZclThermostatSetpointRaiseLower_t));
484 /* Update request structure */
485 req.node_id = node_id;
486 req.endpoint = endpoint;
490 /* Allocate response callback data */
492 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
493 invocation, NULL, 0);
494 if (NULL == resp_cb_data) {
495 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
497 /* Send failure response */
498 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
503 /* Dispatch request */
504 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
505 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
506 ZBLIB_ZCL_THERMOSTAT_OPS_SETPOINT_RAISE_LOWER,
508 on_thermostat_setpoint_raise_lower_resp, resp_cb_data);
510 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
512 /* Free response callback data */
513 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
515 /* Send failure response */
516 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
524 void zigbee_service_dbus_interface_zcl_thermostat_notification(ZigBeeServiceInterface *service_interface,
525 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
527 ZigbeeZcl_thermostat *thermo_object;
529 zblib_check_null_ret("service_interface", service_interface);
531 if (NULL == noti_data || 0 == noti_data_len) {
532 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
536 thermo_object = _service_interface_ref_zigbee_zcl_thermostat(service_interface);
537 zblib_check_null_ret("thermo_object", thermo_object);
539 NOT_USED(noti_cb_data);
543 Z_LOGE("Unexpected notification [%x]", noti_id);
547 /* ZigbeeZcl_thermostat should be dereferenced */
548 g_object_unref(thermo_object);
552 gboolean zigbee_service_dbus_interface_zcl_thermostat_init(ZigBeeServiceInterface *service_interface,
553 ZigbeeObjectSkeleton *zigbee_object)
555 ZigbeeZcl_thermostat *thermostat_object;
557 if (NULL == service_interface) {
558 Z_LOGE("service_interface is NULL");
562 thermostat_object = zigbee_zcl_thermostat_skeleton_new();
563 zigbee_object_skeleton_set_zcl_thermostat(zigbee_object, thermostat_object);
564 g_object_unref(thermostat_object);
566 Z_LOGI("thermostat_object: [%p]", thermostat_object);
569 * Register signal handlers for 'thermostat' interface
571 g_signal_connect(thermostat_object,
572 "handle-get-local-temp",
573 G_CALLBACK(on_thermostat_get_local_temp), service_interface);
575 g_signal_connect(thermostat_object,
576 "handle-get-weekly-schedule",
577 G_CALLBACK(on_thermostat_get_weekly_schedule), service_interface);
579 g_signal_connect(thermostat_object,
580 "handle-set-weekly-schedule",
581 G_CALLBACK(on_thermostat_set_weekly_schedule), service_interface);
583 g_signal_connect(thermostat_object,
584 "handle-clear-weekly-schedule",
585 G_CALLBACK(on_thermostat_clear_weekly_schedule), service_interface);
587 g_signal_connect(thermostat_object,
588 "handle-setpoint-raise-lower",
589 G_CALLBACK(on_thermostat_setpoint_raise_lower), service_interface);