2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
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.
17 #include "dbus-proxy.hh"
18 #include "log-private.hh"
25 constexpr const char BUS_NAME[] = "tizen.appfw.rpcport.benchmark.dbus";
26 constexpr const char OBJECT_PATH[] = "/tizen/appfw/rpcport/benchmark/dbus";
27 constexpr const char INTERFACE_NAME[] = "tizen.appfw.rpcport.benchmark.dbus";
31 DbusProxy::DbusProxy() {
34 void DbusProxy::Connect() {
35 if (system_conn_ != nullptr)
38 GError* error = nullptr;
39 system_conn_ = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error);
40 if (system_conn_ == nullptr) {
41 _E("g_bus_get_sync() is failed. error(%s)",
42 error ? error->message : "Unknown");
43 g_clear_error(&error);
47 int DbusProxy::Test(std::string data) {
48 return MethodCall("Test", g_variant_new("(s)", data.c_str()));
51 int DbusProxy::Start(pid_t caller_pid) {
52 return MethodCall("Start", g_variant_new("(i)", caller_pid));
55 int DbusProxy::Stop(pid_t caller_pid) {
56 return MethodCall("Stop", g_variant_new("(i)", caller_pid));
59 int DbusProxy::MethodCall(const std::string_view method_name,
60 GVariant* parameters) {
61 auto* msg = g_dbus_message_new_method_call(
62 BUS_NAME, OBJECT_PATH, INTERFACE_NAME, method_name.data());
64 _E("g_dbus_message_new_method_call() is failed");
67 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_auto(
70 g_dbus_message_set_body(msg, parameters);
71 GError* error = nullptr;
72 auto* reply = g_dbus_connection_send_message_with_reply_sync(
73 system_conn_, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr, nullptr,
75 if (reply == nullptr || error != nullptr) {
76 _E("g_dbus_connection_send_message_with_reply_sync() is failed. error(%s)",
77 error ? error->message : "Unknown");
78 g_clear_error(&error);
81 std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_auto(
82 reply, g_object_unref);
84 auto* body = g_dbus_message_get_body(reply);
85 if (body == nullptr) {
86 _E("g_dbus_message_get_body() is failed");
91 g_variant_get(body, "(i)", &ret);
95 } // namespace benchmark
96 } // namespace rpc_port