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