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>
24 static void *_service_interface_ref_zigbee_zcl_thermostat(
25 ZigBeeServiceInterface *service_interface)
27 ZigbeeObjectSkeleton *zigbee_object = NULL;
28 ZigbeeCustomData_t *custom_data = NULL;
29 ZigbeeZcl_thermostat *thermo_object = NULL;
31 custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
32 if (NULL == custom_data) {
33 Z_LOGE("D-BUS service interface custom_data is NULL!");
37 /* Get zigbee object */
38 zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
39 if (NULL == zigbee_object) {
40 Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
44 thermo_object = zigbee_object_get_zcl_thermostat(ZIGBEE_OBJECT(zigbee_object));
48 static void on_thermostat_get_local_temp_resp(ZigBeeServiceInterface *service_interface,
49 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
51 ZigbeeServiceInterfaceRespCbData_t *cb_data =
52 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
54 ZigbeeZcl_thermostat *thermo_object = NULL;
55 GDBusMethodInvocation *invocation = NULL;
57 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
59 NOT_USED(service_interface);
62 if (NULL == resp_data || 0 == resp_data_len) {
63 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
68 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
69 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
71 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
72 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
74 zigbee_zcl_thermostat_complete_get_local_temp(thermo_object, invocation,
80 static gboolean on_thermostat_get_local_temp(ZigbeeZcl_thermostat *thermostat_object,
81 GDBusMethodInvocation *invocation,
86 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
87 ZigbeeZclThermostatGetLocalTemp_t req;
88 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
90 GVariantIter *iter = NULL;
95 memset(&req, 0x0, sizeof(ZigbeeZclThermostatGetLocalTemp_t));
97 /* Update request structure */
98 g_variant_get(eui64, "a(y)", &iter);
99 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
101 if (i >= ZIGBEE_EUI64_SIZE)
104 req.endpoint = endpoint;
106 /* Allocate response callback data */
108 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
109 invocation, NULL, 0);
110 if (NULL == resp_cb_data) {
111 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
113 /* Send failure response */
114 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
119 /* Dispatch request */
120 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
121 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
122 ZBLIB_ZCL_THERMOSTAT_OPS_GET_LOCAL_TEMP,
124 on_thermostat_get_local_temp_resp, resp_cb_data);
126 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
128 /* Free response callback data */
129 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
131 /* Send failure response */
132 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
140 static void on_thermostat_get_weekly_schedule_resp(ZigBeeServiceInterface *service_interface,
141 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
143 ZigbeeServiceInterfaceRespCbData_t *cb_data =
144 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
146 ZigbeeZcl_thermostat *thermo_object = NULL;
147 GDBusMethodInvocation *invocation = NULL;
149 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
151 NOT_USED(service_interface);
152 NOT_USED(request_id);
154 if (NULL == resp_data || 0 == resp_data_len) {
155 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
160 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
161 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
163 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
164 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
166 zigbee_zcl_thermostat_complete_get_weekly_schedule(thermo_object, invocation,
172 static gboolean on_thermostat_get_weekly_schedule(ZigbeeZcl_thermostat *thermostat_object,
173 GDBusMethodInvocation *invocation,
180 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
181 ZigbeeZclThermostatGetWeeklySchedule_t req;
182 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
184 GVariantIter *iter = NULL;
189 memset(&req, 0x0, sizeof(ZigbeeZclThermostatGetWeeklySchedule_t));
191 /* Update request structure */
192 g_variant_get(eui64, "a(y)", &iter);
193 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
195 if (i >= ZIGBEE_EUI64_SIZE)
198 req.endpoint = endpoint;
199 req.num_of_days = no_of_days;
202 /* Allocate response callback data */
204 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
205 invocation, NULL, 0);
206 if (NULL == resp_cb_data) {
207 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
209 /* Send failure response */
210 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
215 /* Dispatch request */
216 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
217 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
218 ZBLIB_ZCL_THERMOSTAT_OPS_GET_WEEKLY_SCHEDULE,
220 on_thermostat_get_weekly_schedule_resp, resp_cb_data);
222 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
224 /* Free response callback data */
225 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
227 /* Send failure response */
228 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
236 static void on_thermostat_set_weekly_schedule_resp(ZigBeeServiceInterface *service_interface,
237 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
239 ZigbeeServiceInterfaceRespCbData_t *cb_data =
240 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
242 ZigbeeZcl_thermostat *thermo_object = NULL;
243 GDBusMethodInvocation *invocation = NULL;
245 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
247 NOT_USED(service_interface);
248 NOT_USED(request_id);
250 if (NULL == resp_data || 0 == resp_data_len) {
251 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
256 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
257 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
259 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
260 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
262 zigbee_zcl_thermostat_complete_set_weekly_schedule(thermo_object, invocation,
268 static gboolean on_thermostat_set_weekly_schedule(ZigbeeZcl_thermostat *thermostat_object,
269 GDBusMethodInvocation *invocation,
272 gchar no_of_transitions,
279 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
280 ZigbeeZclThermostatSetWeeklySchedule_t req;
281 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
283 GVariantIter *iter = NULL;
288 memset(&req, 0x0, sizeof(ZigbeeZclThermostatSetWeeklySchedule_t));
290 /* Update request structure */
291 g_variant_get(eui64, "a(y)", &iter);
292 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
294 if (i >= ZIGBEE_EUI64_SIZE)
297 req.endpoint = endpoint;
298 req.no_of_transitions = no_of_transitions;
299 req.num_of_days = no_of_days;
303 g_variant_get(payload, "a(y)", &iter);
304 while (g_variant_iter_loop(iter, "(y)", &(req.payload[i]))) {
306 if (i >= (guint)payload_len)
309 req.payload_len = payload_len;
311 /* Allocate response callback data */
313 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
314 invocation, NULL, 0);
315 if (NULL == resp_cb_data) {
316 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
318 /* Send failure response */
319 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
324 /* Dispatch request */
325 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
326 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
327 ZBLIB_ZCL_THERMOSTAT_OPS_SET_WEEKLY_SCHEDULE,
329 on_thermostat_set_weekly_schedule_resp, resp_cb_data);
331 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
333 /* Free response callback data */
334 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
336 /* Send failure response */
337 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
345 static void on_thermostat_clear_weekly_schedule_resp(ZigBeeServiceInterface *service_interface,
346 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
348 ZigbeeServiceInterfaceRespCbData_t *cb_data =
349 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
351 ZigbeeZcl_thermostat *thermo_object = NULL;
352 GDBusMethodInvocation *invocation = NULL;
354 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
356 NOT_USED(service_interface);
357 NOT_USED(request_id);
359 if (NULL == resp_data || 0 == resp_data_len) {
360 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
365 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
366 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
368 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
369 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
371 zigbee_zcl_thermostat_complete_clear_weekly_schedule(thermo_object, invocation,
377 static gboolean on_thermostat_clear_weekly_schedule(ZigbeeZcl_thermostat *thermostat_object,
378 GDBusMethodInvocation *invocation,
383 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
384 ZigbeeZclThermostatClearWeeklySchedule_t req;
385 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
387 GVariantIter *iter = NULL;
392 memset(&req, 0x0, sizeof(ZigbeeZclThermostatClearWeeklySchedule_t));
394 /* Update request structure */
395 g_variant_get(eui64, "a(y)", &iter);
396 while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
398 if (i >= ZIGBEE_EUI64_SIZE)
401 req.endpoint = endpoint;
403 /* Allocate response callback data */
405 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
406 invocation, NULL, 0);
407 if (NULL == resp_cb_data) {
408 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
410 /* Send failure response */
411 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
416 /* Dispatch request */
417 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
418 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
419 ZBLIB_ZCL_THERMOSTAT_OPS_CLEAR_WEEKLY_SCHEDULE,
421 on_thermostat_clear_weekly_schedule_resp, resp_cb_data);
423 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
425 /* Free response callback data */
426 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
428 /* Send failure response */
429 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
437 static void on_thermostat_setpoint_raise_lower_resp(ZigBeeServiceInterface *service_interface,
438 guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
440 ZigbeeServiceInterfaceRespCbData_t *cb_data =
441 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
443 ZigbeeZcl_thermostat *thermo_object = NULL;
444 GDBusMethodInvocation *invocation = NULL;
446 ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
448 NOT_USED(service_interface);
449 NOT_USED(request_id);
451 if (NULL == resp_data || 0 == resp_data_len) {
452 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
457 thermo_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
458 zblib_check_null_free_and_ret("thermo_object", thermo_object, cb_data);
460 invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
461 zblib_check_null_free_and_ret("invocation", invocation, cb_data);
463 zigbee_zcl_thermostat_complete_setpoint_raise_lower(thermo_object, invocation,
469 static gboolean on_thermostat_setpoint_raise_lower(ZigbeeZcl_thermostat *thermostat_object,
470 GDBusMethodInvocation *invocation,
477 ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
478 ZigbeeZclThermostatSetpointRaiseLower_t req;
479 ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
483 memset(&req, 0x0, sizeof(ZigbeeZclThermostatSetpointRaiseLower_t));
485 /* Update request structure */
486 req.node_id = node_id;
487 req.endpoint = endpoint;
491 /* Allocate response callback data */
493 zigbee_service_dbus_interface_create_resp_cb_data(thermostat_object,
494 invocation, NULL, 0);
495 if (NULL == resp_cb_data) {
496 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
498 /* Send failure response */
499 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
504 /* Dispatch request */
505 ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
506 ZBLIB_DRIVER_TYPE_ZCL_THERMOSTAT,
507 ZBLIB_ZCL_THERMOSTAT_OPS_SETPOINT_RAISE_LOWER,
509 on_thermostat_setpoint_raise_lower_resp, resp_cb_data);
511 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
513 /* Free response callback data */
514 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
516 /* Send failure response */
517 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
525 void zigbee_service_dbus_interface_zcl_thermostat_notification(ZigBeeServiceInterface *service_interface,
526 guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
528 ZigbeeZcl_thermostat *thermo_object;
530 zblib_check_null_ret("service_interface", service_interface);
532 if (NULL == noti_data || 0 == noti_data_len) {
533 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
537 thermo_object = _service_interface_ref_zigbee_zcl_thermostat(service_interface);
538 zblib_check_null_ret("thermo_object", thermo_object);
540 NOT_USED(noti_cb_data);
544 Z_LOGE("Unexpected notification [%x]", noti_id);
548 /* ZigbeeZcl_thermostat should be dereferenced */
549 g_object_unref(thermo_object);
554 gboolean zigbee_service_dbus_interface_zcl_thermostat_init(ZigBeeServiceInterface *service_interface,
555 ZigbeeObjectSkeleton *zigbee_object)
557 ZigbeeZcl_thermostat *thermostat_object;
559 if (NULL == service_interface) {
560 /* LCOV_EXCL_START */
561 Z_LOGE("service_interface is NULL");
566 thermostat_object = zigbee_zcl_thermostat_skeleton_new();
567 zigbee_object_skeleton_set_zcl_thermostat(zigbee_object, thermostat_object);
568 g_object_unref(thermostat_object);
570 Z_LOGI("thermostat_object: [%p]", thermostat_object);
573 * Register signal handlers for 'thermostat' interface
575 g_signal_connect(thermostat_object,
576 "handle-get-local-temp",
577 G_CALLBACK(on_thermostat_get_local_temp), service_interface);
579 g_signal_connect(thermostat_object,
580 "handle-get-weekly-schedule",
581 G_CALLBACK(on_thermostat_get_weekly_schedule), service_interface);
583 g_signal_connect(thermostat_object,
584 "handle-set-weekly-schedule",
585 G_CALLBACK(on_thermostat_set_weekly_schedule), service_interface);
587 g_signal_connect(thermostat_object,
588 "handle-clear-weekly-schedule",
589 G_CALLBACK(on_thermostat_clear_weekly_schedule), service_interface);
591 g_signal_connect(thermostat_object,
592 "handle-setpoint-raise-lower",
593 G_CALLBACK(on_thermostat_setpoint_raise_lower), service_interface);