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;
33 GDBusConnection *conn = this->m_pConnection;
34 GCancellable *cancel = this->m_pCancellable;
37 g_cancellable_cancel(cancel);
38 g_object_unref(cancel);
48 error_e GDbus::Create(void)
52 #if !GLIB_CHECK_VERSION(2, 36, 0)
56 this->m_pConnection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
57 if (this->m_pConnection == NULL) {
59 GLOGD("Failed to connect to the D-BUS daemon [%s]", err->message);
63 return ERROR_OPERATION_FAILED;
66 this->m_pCancellable = g_cancellable_new();
71 error_e GDbus::Destroy(void)
73 g_cancellable_cancel(this->m_pCancellable);
74 g_object_unref(this->m_pCancellable);
75 this->m_pCancellable = NULL;
77 g_object_unref(this->m_pConnection);
78 this->m_pConnection = NULL;
83 GDBusConnection *GDbus::GetConnection(void)
85 return this->m_pConnection;
88 GCancellable *GDbus::GetCancellable(void)
90 return this->m_pCancellable;
93 error_e GDbus::ConvertErrorStringToEnum(const char *error)
95 if (NULL != strstr(error, "NoReply"))
96 return ERROR_INVALID_OPERATION;
97 else if (NULL != strstr(error, "Failed"))
98 return ERROR_OPERATION_FAILED;
99 else if (NULL != strstr(error, "UnknownMethod"))
100 return ERROR_INVALID_OPERATION;
101 else if (NULL != strstr(error, "InvalidArguments"))
102 return ERROR_INVALID_PARAMETER;
103 else if (NULL != strstr(error, "AccessDenied"))
104 return ERROR_PERMISSION_DENIED;
105 else if (NULL != strstr(error, "PermissionDenied"))
106 return ERROR_PERMISSION_DENIED;
107 else if (NULL != strstr(error, "NotSupported"))
108 return ERROR_NOT_SUPPORTED;
109 else if (NULL != strstr(error, "InProgress"))
110 return ERROR_IN_PROGRESS;
112 return ERROR_OPERATION_FAILED;
115 GVariant *GDbus::InvokeMethod(const char *dest, const char *path,
116 const char *iface_name, const char *method, GVariant *params, error_e *dbus_error)
118 GError *error = NULL;
119 GVariant *reply = NULL;
120 GDBusConnection *connection = NULL;
121 *dbus_error = ERROR_NONE;
123 connection = GetConnection();
124 if (connection == NULL) {
125 GLOGD("GDBusconnection is NULL");
126 *dbus_error = ERROR_NOT_INITIALIZED;
130 reply = g_dbus_connection_call_sync(connection,
137 G_DBUS_CALL_FLAGS_NONE,
144 GLOGD("g_dbus_connection_call_sync() failed "
145 "error [%d: %s]", error->code, error->message);
146 *dbus_error = ConvertErrorStringToEnum(error->message);
149 GLOGD("g_dbus_connection_call_sync() failed");
150 *dbus_error = ERROR_OPERATION_FAILED;
159 error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
160 const char *iface_name, const char *method, GVariant *params, int timeout,
161 GAsyncReadyCallback notify_func, void *user_data)
163 GDBusConnection *connection = NULL;
165 connection = GetConnection();
166 if (connection == NULL) {
167 GLOGD("GDBusconnection is NULL");
168 return ERROR_NOT_INITIALIZED;
171 g_dbus_connection_call(connection,
178 G_DBUS_CALL_FLAGS_NONE,
181 (GAsyncReadyCallback) notify_func,
182 (gpointer)user_data);