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 "logger/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(api::host::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: " << api::host::BUS_NAME);
60 throw HostConnectionException("Could not acquire dbus name: " + api::host::BUS_NAME);
63 LOGT("Registering DBUS interface");
64 using namespace std::placeholders;
65 mDbusConnection->registerObject(api::host::OBJECT_PATH,
66 api::host::DEFINITION,
67 std::bind(&HostConnection::onMessageCall,
68 this, _1, _2, _3, _4, _5));
73 HostConnection::~HostConnection()
77 bool HostConnection::waitForName(const unsigned int timeoutMs)
79 std::unique_lock<std::mutex> lock(mNameMutex);
80 mNameCondition.wait_for(lock,
81 std::chrono::milliseconds(timeoutMs),
83 return mNameAcquired || mNameLost;
89 void HostConnection::onNameAcquired()
91 std::unique_lock<std::mutex> lock(mNameMutex);
93 mNameCondition.notify_one();
96 void HostConnection::onNameLost()
98 std::unique_lock<std::mutex> lock(mNameMutex);
100 mNameCondition.notify_one();
103 // TODO implement reconnecting
104 LOGE("TODO Reconnect !!!");
108 void HostConnection::setProxyCallCallback(const ProxyCallCallback& callback)
110 mProxyCallCallback = callback;
113 void HostConnection::setGetContainerDbusesCallback(const GetContainerDbusesCallback& callback)
115 mGetContainerDbusesCallback = callback;
118 void HostConnection::setGetContainerIdsCallback(const GetContainerIdsCallback& callback)
120 mGetContainerIdsCallback = callback;
123 void HostConnection::setGetActiveContainerIdCallback(const GetActiveContainerIdCallback& callback)
125 mGetActiveContainerIdCallback = callback;
128 void HostConnection::setSetActiveContainerCallback(const SetActiveContainerCallback& callback)
130 mSetActiveContainerCallback = callback;
133 void HostConnection::setAddContainerCallback(const AddContainerCallback& callback)
135 mAddContainerCallback = callback;
138 void HostConnection::onMessageCall(const std::string& objectPath,
139 const std::string& interface,
140 const std::string& methodName,
141 GVariant* parameters,
142 dbus::MethodResultBuilder::Pointer result)
144 if (objectPath != api::host::OBJECT_PATH || interface != api::host::INTERFACE) {
148 if (methodName == api::host::METHOD_SET_ACTIVE_CONTAINER) {
149 const gchar* id = NULL;
150 g_variant_get(parameters, "(&s)", &id);
152 if (mSetActiveContainerCallback) {
153 mSetActiveContainerCallback(id, result);
158 if (methodName == api::host::METHOD_GET_CONTAINER_DBUSES) {
159 if (mGetContainerDbusesCallback) {
160 mGetContainerDbusesCallback(result);
165 if (methodName == api::METHOD_PROXY_CALL) {
166 const gchar* target = NULL;
167 const gchar* targetBusName = NULL;
168 const gchar* targetObjectPath = NULL;
169 const gchar* targetInterface = NULL;
170 const gchar* targetMethod = NULL;
171 GVariant* rawArgs = NULL;
172 g_variant_get(parameters,
180 dbus::GVariantPtr args(rawArgs, g_variant_unref);
182 if (mProxyCallCallback) {
183 mProxyCallCallback(target,
194 if (methodName == api::host::METHOD_GET_CONTAINER_ID_LIST){
195 if (mGetContainerIdsCallback){
196 mGetContainerIdsCallback(result);
201 if (methodName == api::host::METHOD_GET_ACTIVE_CONTAINER_ID){
202 if (mGetActiveContainerIdCallback){
203 mGetActiveContainerIdCallback(result);
208 if (methodName == api::host::METHOD_ADD_CONTAINER) {
209 const gchar* id = NULL;
210 g_variant_get(parameters, "(&s)", &id);
212 if (mAddContainerCallback){
213 mAddContainerCallback(id, result);
218 void HostConnection::proxyCallAsync(const std::string& busName,
219 const std::string& objectPath,
220 const std::string& interface,
221 const std::string& method,
222 GVariant* parameters,
223 const dbus::DbusConnection::AsyncMethodCallCallback& callback)
225 mDbusConnection->callMethodAsync(busName,
234 void HostConnection::signalContainerDbusState(const std::string& containerId,
235 const std::string& dbusAddress)
237 GVariant* parameters = g_variant_new("(ss)", containerId.c_str(), dbusAddress.c_str());
238 mDbusConnection->emitSignal(api::host::OBJECT_PATH,
239 api::host::INTERFACE,
240 api::host::SIGNAL_CONTAINER_DBUS_STATE,
245 } // namespace security_containers