2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 * @brief Implementation of a class for communication with server
27 #include "host-connection.hpp"
28 #include "host-dbus-definitions.hpp"
29 #include "exception.hpp"
31 #include "log/logger.hpp"
34 namespace security_containers {
38 // Timeout in ms for waiting for dbus name.
39 // Can happen if glib loop is busy or not present.
40 // TODO: this should be in host's configuration file
41 const unsigned int NAME_ACQUIRED_TIMEOUT = 5 * 1000;
46 HostConnection::HostConnection()
47 : mNameAcquired(false)
50 LOGT("Connecting to host system DBUS");
51 mDbusConnection = dbus::DbusConnection::createSystem();
53 LOGT("Setting DBUS name");
54 mDbusConnection->setName(hostapi::BUS_NAME,
55 std::bind(&HostConnection::onNameAcquired, this),
56 std::bind(&HostConnection::onNameLost, this));
58 if (!waitForName(NAME_ACQUIRED_TIMEOUT)) {
59 LOGE("Could not acquire dbus name: " << hostapi::BUS_NAME);
60 throw HostConnectionException("Could not acquire dbus name: " + hostapi::BUS_NAME);
63 LOGT("Registering DBUS interface");
64 using namespace std::placeholders;
65 mDbusConnection->registerObject(hostapi::OBJECT_PATH,
67 std::bind(&HostConnection::onMessageCall,
78 HostConnection::~HostConnection()
82 bool HostConnection::waitForName(const unsigned int timeoutMs)
84 std::unique_lock<std::mutex> lock(mNameMutex);
85 mNameCondition.wait_for(lock,
86 std::chrono::milliseconds(timeoutMs),
88 return mNameAcquired || mNameLost;
94 void HostConnection::onNameAcquired()
96 std::unique_lock<std::mutex> lock(mNameMutex);
98 mNameCondition.notify_one();
101 void HostConnection::onNameLost()
103 std::unique_lock<std::mutex> lock(mNameMutex);
105 mNameCondition.notify_one();
108 // TODO implement reconnecting
109 LOGE("TODO Reconnect !!!");
113 void HostConnection::setProxyCallCallback(const ProxyCallCallback& callback)
115 mProxyCallCallback = callback;
118 void HostConnection::onMessageCall(const std::string& objectPath,
119 const std::string& interface,
120 const std::string& methodName,
121 GVariant* parameters,
122 dbus::MethodResultBuilder::Pointer result)
124 if (objectPath != hostapi::OBJECT_PATH || interface != hostapi::INTERFACE) {
128 if (methodName == hostapi::METHOD_PROXY_CALL) {
129 const gchar* target = NULL;
130 const gchar* targetBusName = NULL;
131 const gchar* targetObjectPath = NULL;
132 const gchar* targetInterface = NULL;
133 const gchar* targetMethod = NULL;
134 GVariant* rawArgs = NULL;
135 g_variant_get(parameters,
143 dbus::GVariantPtr args(rawArgs, g_variant_unref);
145 if (mProxyCallCallback) {
146 mProxyCallCallback(target,
157 void HostConnection::proxyCallAsync(const std::string& busName,
158 const std::string& objectPath,
159 const std::string& interface,
160 const std::string& method,
161 GVariant* parameters,
162 const dbus::DbusConnection::AsyncMethodCallCallback& callback)
164 mDbusConnection->callMethodAsync(busName,
174 } // namespace security_containers