2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@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 Jan Olszak (j.olszak@samsung.com)
22 * @brief Dbus API for the Zone Daemon
27 #include "daemon-connection.hpp"
28 #include "daemon-dbus-definitions.hpp"
29 #include "exception.hpp"
31 #include "logger/logger.hpp"
35 namespace zone_daemon {
39 // Timeout in ms for waiting for dbus name.
40 // Can happen if glib loop is busy or not present.
41 const unsigned int NAME_ACQUIRED_TIMEOUT = 5 * 1000;
46 DaemonConnection::DaemonConnection(const NameLostCallback& nameLostCallback,
47 const GainFocusCallback& gainFocusCallback,
48 const LoseFocusCallback& loseFocusCallback)
49 : mNameAcquired(false)
52 LOGD("Connecting to DBUS on system bus");
53 mDbusConnection = dbus::DbusConnection::createSystem();
55 LOGD("Setting DBUS name");
56 mDbusConnection->setName(zone_daemon::api::BUS_NAME,
57 std::bind(&DaemonConnection::onNameAcquired, this),
58 std::bind(&DaemonConnection::onNameLost, this));
60 if (!waitForNameAndSetCallback(NAME_ACQUIRED_TIMEOUT, nameLostCallback)) {
61 LOGE("Could not acquire dbus name: " << zone_daemon::api::BUS_NAME);
62 throw ZoneDaemonException("Could not acquire dbus name: " + zone_daemon::api::BUS_NAME);
65 LOGD("Setting callbacks");
66 mGainFocusCallback = gainFocusCallback;
67 mLoseFocusCallback = loseFocusCallback;
70 LOGD("Registering DBUS interface");
71 using namespace std::placeholders;
72 mDbusConnection->registerObject(zone_daemon::api::OBJECT_PATH,
73 zone_daemon::api::DEFINITION,
74 std::bind(&DaemonConnection::onMessageCall,
75 this, _1, _2, _3, _4, _5),
80 DaemonConnection::~DaemonConnection()
84 bool DaemonConnection::waitForNameAndSetCallback(const unsigned int timeoutMs,
85 const NameLostCallback& nameLostCallback)
87 std::unique_lock<std::mutex> lock(mNameMutex);
88 mNameCondition.wait_for(lock,
89 std::chrono::milliseconds(timeoutMs),
91 return mNameAcquired || mNameLost;
95 mNameLostCallback = nameLostCallback;
101 void DaemonConnection::onNameAcquired()
103 std::unique_lock<std::mutex> lock(mNameMutex);
104 mNameAcquired = true;
105 mNameCondition.notify_one();
108 void DaemonConnection::onNameLost()
110 std::unique_lock<std::mutex> lock(mNameMutex);
112 mNameCondition.notify_one();
114 if (mNameLostCallback) {
119 void DaemonConnection::onMessageCall(const std::string& objectPath,
120 const std::string& interface,
121 const std::string& methodName,
122 GVariant* /*parameters*/,
123 dbus::MethodResultBuilder::Pointer result)
125 if (objectPath != api::OBJECT_PATH || interface != api::INTERFACE) {
129 if (methodName == api::METHOD_GAIN_FOCUS) {
130 if (mGainFocusCallback) {
131 mGainFocusCallback();
134 } else if (methodName == api::METHOD_LOSE_FOCUS) {
135 if (mLoseFocusCallback) {
136 mLoseFocusCallback();
142 } // namespace zone_daemon