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, "NOT_PERMITTED"))
96 return ERROR_NOT_PERMITTED;
97 else if (NULL != strstr(error, "OUT_OF_MEMORY"))
98 return ERROR_OUT_OF_MEMORY;
99 else if (NULL != strstr(error, "PERMISSION_DENIED"))
100 return ERROR_PERMISSION_DENIED;
101 else if (NULL != strstr(error, "INVALID_OPERATION"))
102 return ERROR_INVALID_OPERATION;
103 else if (NULL != strstr(error, "INVALID_PARAMETER"))
104 return ERROR_INVALID_PARAMETER;
105 else if (NULL != strstr(error, "OPERATION_FAILED"))
106 return ERROR_OPERATION_FAILED;
108 return ERROR_OPERATION_FAILED;
111 GVariant *GDbus::InvokeMethod(const char *dest, const char *path,
112 const char *iface_name, const char *method, GVariant *params, error_e *dbus_error)
114 GError *error = NULL;
115 GVariant *reply = NULL;
116 GDBusConnection *connection = NULL;
117 *dbus_error = ERROR_NONE;
119 connection = GetConnection();
120 if (connection == NULL) {
121 GLOGD("GDBusconnection is NULL");
122 *dbus_error = ERROR_NOT_INITIALIZED;
126 reply = g_dbus_connection_call_sync(connection,
133 G_DBUS_CALL_FLAGS_NONE,
140 GLOGD("g_dbus_connection_call_sync() failed "
141 "error [%d: %s]", error->code, error->message);
142 *dbus_error = ConvertErrorStringToEnum(error->message);
145 GLOGD("g_dbus_connection_call_sync() failed");
146 *dbus_error = ERROR_OPERATION_FAILED;
155 error_e GDbus::InvokeMethodNonblock(const char *dest, const char *path,
156 const char *iface_name, const char *method, GVariant *params, int timeout,
157 GAsyncReadyCallback notify_func, void *user_data)
159 GDBusConnection *connection = NULL;
161 connection = GetConnection();
162 if (connection == NULL) {
163 GLOGD("GDBusconnection is NULL");
164 return ERROR_NOT_INITIALIZED;
167 g_dbus_connection_call(connection,
174 G_DBUS_CALL_FLAGS_NONE,
177 (GAsyncReadyCallback) notify_func,
178 (gpointer)user_data);