Separate benchmark tool from rpc-port
[platform/core/appfw/rpc-port.git] / benchmark / tool / dbus-proxy.cc
1 /*
2  * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "dbus-proxy.hh"
18 #include "log-private.hh"
19
20 namespace rpc_port {
21 namespace benchmark {
22
23 namespace {
24
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";
28
29 }
30
31 DbusProxy::DbusProxy() {
32 }
33
34 void DbusProxy::Connect() {
35   if (system_conn_ != nullptr)
36     return;
37
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);
44   }
45 }
46
47 int DbusProxy::Test(std::string data) {
48   return MethodCall("Test", g_variant_new("(s)", data.c_str()));
49 }
50
51 int DbusProxy::Start(pid_t caller_pid) {
52   return MethodCall("Start", g_variant_new("(i)", caller_pid));
53 }
54
55 int DbusProxy::Stop(pid_t caller_pid) {
56   return MethodCall("Stop", g_variant_new("(i)", caller_pid));
57 }
58
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());
63   if (msg == nullptr) {
64     _E("g_dbus_message_new_method_call() is failed");
65     return -1;
66   }
67   std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> msg_auto(
68       msg, g_object_unref);
69
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,
74       &error);
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);
79     return -1;
80   }
81   std::unique_ptr<GDBusMessage, decltype(g_object_unref)*> reply_auto(
82       reply, g_object_unref);
83
84   auto* body = g_dbus_message_get_body(reply);
85   if (body == nullptr) {
86     _E("g_dbus_message_get_body() is failed");
87     return -1;
88   }
89
90   int ret = -1;
91   g_variant_get(body, "(i)", &ret);
92   return ret;
93 }
94
95 }  // namespace benchmark
96 }  // namespace rpc_port