2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
27 this->m_pConnection = NULL;
28 this->m_pCancellable = NULL;
29 this->service_gproxy = NULL;
30 this->on_off_gproxy = NULL;
31 this->door_lock_gproxy = NULL;
32 this->level_control_gproxy = NULL;
33 this->thermostat_gproxy = NULL;
34 this->alarm_gproxy = NULL;
35 this->fan_control_gproxy = NULL;
37 this->mfglib_gproxy = NULL;
38 this->zcl_global_proxy = NULL;
39 this->zdo_dev_proxy = NULL;
40 this->zcl_basic_proxy = NULL;
41 this->zcl_identify_proxy = NULL;
42 this->zcl_ias_zone_proxy = NULL;
43 this->zcl_poll_control_proxy = NULL;
44 this->zcl_group_proxy = NULL;
45 this->zcl_scene_proxy = NULL;
46 this->zdo_bind_proxy = NULL;
47 this->zcl_color_control_proxy = NULL;
48 this->custom_gproxy = NULL;
53 GDBusConnection *conn = this->m_pConnection;
54 GCancellable *cancel = this->m_pCancellable;
57 g_cancellable_cancel(cancel);
58 g_object_unref(cancel);
63 g_object_unref(this->service_gproxy);
64 this->service_gproxy = NULL;
65 g_object_unref(this->on_off_gproxy);
66 this->on_off_gproxy = NULL;
67 g_object_unref(this->door_lock_gproxy);
68 this->door_lock_gproxy = NULL;
69 g_object_unref(this->level_control_gproxy);
70 this->level_control_gproxy = NULL;
71 g_object_unref(this->thermostat_gproxy);
72 this->thermostat_gproxy = NULL;
73 g_object_unref(this->alarm_gproxy);
74 this->alarm_gproxy = NULL;
75 g_object_unref(this->fan_control_gproxy);
76 this->fan_control_gproxy = NULL;
78 g_object_unref(this->mfglib_gproxy);
79 this->mfglib_gproxy = NULL;
80 g_object_unref(this->zcl_global_proxy);
81 this->zcl_global_proxy = NULL;
83 g_object_unref(this->zdo_dev_proxy);
84 this->zdo_dev_proxy = NULL;
85 g_object_unref(this->zcl_basic_proxy);
86 this->zcl_basic_proxy = NULL;
87 g_object_unref(this->zcl_identify_proxy);
88 this->zcl_identify_proxy = NULL;
89 g_object_unref(this->zcl_ias_zone_proxy);
90 this->zcl_ias_zone_proxy = NULL;
91 g_object_unref(this->zcl_poll_control_proxy);
92 this->zcl_poll_control_proxy = NULL;
93 g_object_unref(this->zcl_group_proxy);
94 this->zcl_group_proxy = NULL;
95 g_object_unref(this->zcl_scene_proxy);
96 this->zcl_scene_proxy = NULL;
97 g_object_unref(this->zdo_bind_proxy);
98 this->zdo_bind_proxy = NULL;
100 g_object_unref(this->zcl_color_control_proxy);
101 this->zcl_color_control_proxy = NULL;
102 g_object_unref(this->custom_gproxy);
103 this->custom_gproxy = NULL;
105 g_object_unref(conn);
110 zb_error_e GDbus::Create(void)
113 GDBusConnection *gdbus_conn;
115 #if !GLIB_CHECK_VERSION(2, 36, 0)
119 this->m_pConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
120 if (this->m_pConnection == NULL) {
122 GLOGD("Failed to connect to the D-BUS daemon [%s]", err->message);
126 return ZIGBEE_ERROR_OPERATION_FAILED;
129 gdbus_conn = this->m_pConnection;
131 this->service_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
132 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_SERVICE_INTERFACE,
135 this->on_off_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
136 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
137 ZIGBEE_ZCL_ON_OFF_INTERFACE, NULL, NULL);
139 this->door_lock_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
140 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
141 ZIGBEE_ZCL_DOOR_LOCK_INTERFACE, NULL, NULL);
143 this->level_control_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
144 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
145 ZIGBEE_ZCL_LEVEL_CONTROL_INTERFACE, NULL, NULL);
147 this->thermostat_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
148 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
149 ZIGBEE_ZCL_THERMOSTAT_INTERFACE, NULL, NULL);
151 this->alarm_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
152 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_ZCL_ALARM_INTERFACE,
155 this->fan_control_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
156 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
157 ZIGBEE_ZCL_FAN_CONTROL_INTERFACE, NULL, NULL);
159 this->mfglib_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
160 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
161 ZIGBEE_MFGLIB_CONTROL_INTERFACE, NULL, NULL);
163 this->zcl_global_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
164 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
165 ZIGBEE_ZCL_GLOBAL_CONTROL_INTERFACE, NULL, NULL);
167 this->zdo_dev_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
168 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
169 ZIGBEE_ZDO_DEV_CONTROL_INTERFACE, NULL, NULL);
171 this->zcl_basic_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
172 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_ZCL_BASIC_INTERFACE,
175 this->zcl_identify_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
176 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
177 ZIGBEE_ZCL_IDENTIFY_INTERFACE, NULL, NULL);
179 this->zcl_ias_zone_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
180 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
181 ZIGBEE_ZCL_IAS_ZONE_INTERFACE, NULL, NULL);
183 this->zcl_poll_control_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
184 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
185 ZIGBEE_ZCL_POLL_CONTROL_INTERFACE, NULL, NULL);
187 this->zcl_group_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
188 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_ZCL_GROUP_INTERFACE,
191 this->zcl_scene_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
192 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_ZCL_SCENE_INTERFACE,
195 this->zdo_bind_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
196 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_ZDO_BIND_INTERFACE,
199 this->zcl_color_control_proxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
200 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH,
201 ZIGBEE_ZCL_COLOR_CONTROL_INTERFACE, NULL, NULL);
203 this->custom_gproxy = g_dbus_proxy_new_sync(gdbus_conn, G_DBUS_PROXY_FLAGS_NONE, NULL,
204 ZIGBEE_SERVER_NAME, ZIGBEE_CONTROL_OBJECT_PATH, ZIGBEE_CUSTOM_INTERFACE,
207 this->m_pCancellable = g_cancellable_new();
209 return ZIGBEE_ERROR_NONE;
212 zb_error_e GDbus::Destroy(void)
214 g_object_unref(this->service_gproxy);
215 this->service_gproxy = NULL;
216 g_object_unref(this->on_off_gproxy);
217 this->on_off_gproxy = NULL;
218 g_object_unref(this->door_lock_gproxy);
219 this->door_lock_gproxy = NULL;
220 g_object_unref(this->level_control_gproxy);
221 this->level_control_gproxy = NULL;
222 g_object_unref(this->thermostat_gproxy);
223 this->thermostat_gproxy = NULL;
224 g_object_unref(this->alarm_gproxy);
225 this->alarm_gproxy = NULL;
226 g_object_unref(this->fan_control_gproxy);
227 this->fan_control_gproxy = NULL;
229 g_object_unref(this->mfglib_gproxy);
230 this->mfglib_gproxy = NULL;
231 g_object_unref(this->zcl_global_proxy);
232 this->zcl_global_proxy = NULL;
234 g_object_unref(this->zdo_dev_proxy);
235 this->zdo_dev_proxy = NULL;
236 g_object_unref(this->zcl_basic_proxy);
237 this->zcl_basic_proxy = NULL;
238 g_object_unref(this->zcl_identify_proxy);
239 this->zcl_identify_proxy = NULL;
240 g_object_unref(this->zcl_ias_zone_proxy);
241 this->zcl_ias_zone_proxy = NULL;
242 g_object_unref(this->zcl_poll_control_proxy);
243 this->zcl_poll_control_proxy = NULL;
244 g_object_unref(this->zcl_group_proxy);
245 this->zcl_group_proxy = NULL;
246 g_object_unref(this->zcl_scene_proxy);
247 this->zcl_scene_proxy = NULL;
248 g_object_unref(this->zdo_bind_proxy);
249 this->zdo_bind_proxy = NULL;
250 g_object_unref(this->zcl_color_control_proxy);
251 this->zcl_color_control_proxy = NULL;
252 g_object_unref(this->custom_gproxy);
253 this->custom_gproxy = NULL;
255 g_cancellable_cancel(this->m_pCancellable);
256 g_object_unref(this->m_pCancellable);
257 this->m_pCancellable = NULL;
259 g_object_unref(this->m_pConnection);
260 this->m_pConnection = NULL;
262 return ZIGBEE_ERROR_NONE;
265 GDBusConnection *GDbus::GetConnection(void)
267 return this->m_pConnection;
270 GCancellable *GDbus::GetCancellable(void)
272 return this->m_pCancellable;
275 GDBusProxy *GDbus::get_proxy_type(gproxy_type_e type)
277 GDBusProxy *proxy = NULL;
282 proxy = this->service_gproxy;
285 proxy = this->on_off_gproxy;
287 case DOOR_LOCK_GPROXY:
288 proxy = this->door_lock_gproxy;
290 case LEVEL_CONTROL_GPROXY:
291 proxy = this->level_control_gproxy;
293 case THERMOSTAT_GPROXY:
294 proxy = this->thermostat_gproxy;
297 proxy = this->alarm_gproxy;
299 case FAN_CONTROL_GPROXY:
300 proxy = this->fan_control_gproxy;
303 proxy = this->mfglib_gproxy;
305 case ZCL_GLOBAL_PROXY:
306 proxy = this->zcl_global_proxy;
309 proxy = this->zdo_dev_proxy;
311 case ZCL_BASIC_PROXY:
312 proxy = this->zcl_basic_proxy;
314 case ZCL_IDENTIFY_PROXY:
315 proxy = this->zcl_identify_proxy;
317 case ZCL_IAS_ZONE_PROXY:
318 proxy = this->zcl_ias_zone_proxy;
320 case ZCL_POLL_CONTROL_PROXY:
321 proxy = this->zcl_poll_control_proxy;
323 case ZCL_GROUP_PROXY:
324 proxy = this->zcl_group_proxy;
326 case ZCL_SCENE_PROXY:
327 proxy = this->zcl_scene_proxy;
330 proxy = this->zdo_bind_proxy;
332 case ZCL_COLOR_CONTROL_PROXY:
333 proxy = this->zcl_color_control_proxy;
336 proxy = this->custom_gproxy;
343 GVariant *GDbus::invoke_proxy_method(gproxy_type_e type, const char *method, GVariant *params, int *error)
345 GError *dbus_error = NULL;
346 GVariant *reply = NULL;
347 GDBusConnection *connection = NULL;
348 GDBusProxy *proxy = NULL;
350 connection = GetConnection();
351 if (connection == NULL) {
352 GLOGD("GDBusconnection is NULL");
353 *error = ZIGBEE_ERROR_IO_ERROR;
357 proxy = get_proxy_type(type);
359 GLOGD("GDBusProxy is NULL");
360 *error = ZIGBEE_ERROR_IO_ERROR;
364 reply = g_dbus_proxy_call_sync(proxy,
367 G_DBUS_CALL_FLAGS_NONE,
368 ZIGBEE_BROADCAST_TIMEOUT,
373 if (dbus_error != NULL) {
374 GLOGD("g_dbus_connection_call_sync() failed "
375 "error [%d: %s]", dbus_error->code, dbus_error->message);
376 if (strstr(dbus_error->message, "AccessDenied"))
377 *error = ZIGBEE_ERROR_PERMISSION_DENIED;
379 *error = ZIGBEE_ERROR_IO_ERROR;
380 g_error_free(dbus_error);
382 GLOGD("g_dbus_connection_call_sync() failed");
383 *error = ZIGBEE_ERROR_OPERATION_FAILED;
391 GVariant *GDbus::InvokeMethod(const char *dest, const char *path,
392 const char *iface_name, const char *method, GVariant *params, int *error)
394 GError *dbus_error = NULL;
395 GVariant *reply = NULL;
396 GDBusConnection *connection = NULL;
398 connection = GetConnection();
399 if (connection == NULL) {
400 GLOGD("GDBusconnection is NULL");
401 *error = ZIGBEE_ERROR_IO_ERROR;
405 reply = g_dbus_connection_call_sync(connection,
412 G_DBUS_CALL_FLAGS_NONE,
413 ZIGBEE_BROADCAST_TIMEOUT,
418 if (dbus_error != NULL) {
419 GLOGD("g_dbus_connection_call_sync() failed "
420 "error [%d: %s]", dbus_error->code, dbus_error->message);
421 if (strstr(dbus_error->message, "AccessDenied"))
422 *error = ZIGBEE_ERROR_PERMISSION_DENIED;
424 *error = ZIGBEE_ERROR_IO_ERROR;
425 g_error_free(dbus_error);
427 GLOGD("g_dbus_connection_call_sync() failed");
428 *error = ZIGBEE_ERROR_OPERATION_FAILED;
436 zb_error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
437 const char *iface_name, const char *method, GVariant *params, int timeout,
438 GAsyncReadyCallback notify_func, void *user_data)
440 GDBusConnection *connection = NULL;
442 connection = GetConnection();
443 if (connection == NULL) {
444 GLOGD("GDBusconnection is NULL");
445 return ZIGBEE_ERROR_IO_ERROR;
448 g_dbus_connection_call(connection,
455 G_DBUS_CALL_FLAGS_NONE,
458 (GAsyncReadyCallback) notify_func,
459 (gpointer)user_data);
461 return ZIGBEE_ERROR_NONE;