Implement ZCL Basic Cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zcl_basic.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 "zigbee_service_interface_common.h"
20
21 #include <zblib_driver_zcl_basic.h>
22
23 static void *_service_interface_ref_zigbee_zcl_basic(ZigBeeServiceInterface *service_interface)
24 {
25         ZigbeeObjectSkeleton *zigbee_object = NULL;
26         ZigbeeCustomData_t *custom_data = NULL;
27         ZigbeeZcl_basic *basic_object = NULL;
28
29         custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
30         if (NULL == custom_data) {
31                 Z_LOGE("D-BUS service interface custom_data is NULL!");
32                 return NULL;
33         }
34
35         /* Get zigbee object */
36         zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
37         if (NULL == zigbee_object) {
38                 Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
39                 return NULL;
40         }
41
42         basic_object = zigbee_object_get_zcl_basic(ZIGBEE_OBJECT(zigbee_object));
43         return basic_object;
44 }
45
46 static void on_zclbasic_control_reset_factory_default_resp(ZigBeeServiceInterface *service_interface,
47         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
48 {
49         ZigbeeServiceInterfaceRespCbData_t *cb_data =
50                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
51
52         ZigbeeZcl_basic *zcl_basic_object = NULL;
53         GDBusMethodInvocation *invocation = NULL;
54
55         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
56
57         NOT_USED(service_interface);
58         NOT_USED(request_id);
59
60         if (NULL == resp_data || 0 == resp_data_len) {
61                 Z_LOGE("resp_data is null");
62                 g_free(cb_data);
63                 return;
64         }
65
66         zcl_basic_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
67         zblib_check_null_free_and_ret("zcl_basic_object", zcl_basic_object, cb_data);
68
69         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
70         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
71
72         zigbee_zcl_basic_complete_reset_factory_default(zcl_basic_object, invocation,
73                 payload->result);
74
75         g_free(cb_data);
76 }
77
78 static gboolean on_zclbasic_control_reset_factory_default(ZigbeeZcl_basic *zclbasic_control_object,
79         GDBusMethodInvocation *invocation,
80         gshort node_id,
81         gchar dest_ep,
82         gpointer user_data)
83 {
84         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
85         ZigbeeZclBasicResetFactoryDefault_t req;
86         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
87
88         gboolean ret;
89
90         memset(&req, 0x0, sizeof(ZigbeeZclBasicResetFactoryDefault_t));
91
92         /* Update request structure */
93         req.node_id = node_id;
94         req.dest_ep = dest_ep;
95
96         /* Allocate response callback data */
97         resp_cb_data =
98                 zigbee_service_dbus_interface_create_resp_cb_data(zclbasic_control_object,
99                         invocation, NULL, 0);
100         if (NULL == resp_cb_data) {
101                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
102
103                 /* Send failure response */
104                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
105
106                 return TRUE;
107         }
108
109         /* Dispatch request */
110         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
111                 ZBLIB_DRIVER_TYPE_ZCL_BASIC,
112                 ZBLIB_ZCL_BASIC_OPS_RESET_FACTORY_DEFAULT,
113                 &req, sizeof(req),
114                 on_zclbasic_control_reset_factory_default_resp, resp_cb_data);
115         if (FALSE == ret) {
116                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
117
118                 /* Free response callback data */
119                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
120
121                 /* Send failure response */
122                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
123
124                 return TRUE;
125         }
126
127         return TRUE;
128 }
129
130 void zigbee_service_dbus_interface_zcl_basic_notification(ZigBeeServiceInterface *service_interface,
131         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
132 {
133         ZigbeeZcl_basic *basic_object;
134
135         zblib_check_null_ret("service_interface", service_interface);
136
137         if (NULL == noti_data || 0 == noti_data_len) {
138                 Z_LOGE("noti_data is NULL");
139                 return;
140         }
141
142         basic_object = _service_interface_ref_zigbee_zcl_basic(service_interface);
143         zblib_check_null_ret("basic_object", basic_object);
144
145         NOT_USED(noti_cb_data);
146
147         switch(noti_id) {
148         default:
149                 Z_LOGE("Unexpected notification [%x]", noti_id);
150         break;
151         }
152
153         /* ZigbeeZcl_basic should be dereferenced */
154         g_object_unref(basic_object);
155 }
156
157 gboolean zigbee_service_dbus_interface_zcl_basic_init(ZigBeeServiceInterface *service_interface,
158         ZigbeeObjectSkeleton *zigbee_object)
159 {
160         ZigbeeZcl_basic *zclbasic_control_object;
161
162         if (NULL == service_interface) {
163                 Z_LOGE("service_interface is NULL");
164                 return FALSE;
165         }
166
167         zclbasic_control_object = zigbee_zcl_basic_skeleton_new();
168         zigbee_object_skeleton_set_zcl_basic(zigbee_object, zclbasic_control_object);
169         g_object_unref(zclbasic_control_object);
170
171         Z_LOGI("zclbasic_control_object: [%p]", zclbasic_control_object);
172
173         /*
174          * Register signal handlers for 'zclbasic_control' interface
175          */
176         g_signal_connect(zclbasic_control_object,
177                 "handle-reset-factory-default",
178                 G_CALLBACK(on_zclbasic_control_reset_factory_default), service_interface);
179
180         return TRUE;
181 }