2 * Samsung Ukraine R&D Center (SRK under a contract between)
3 * LLC "Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
4 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
19 * @file dbuslistener.cpp
20 * @brief DBusListener class source
21 * @date Created Aug 21, 2018
22 * @author Mail to: <A HREF="mailto:i.metelytsia@samsung.com">Iurii Metelytsia, i.metelytsia@samsung.com</A>
27 #include <jsoncpp/json/reader.h>
29 #include "samonitor_tag.h"
31 #include "dbuslistener.h"
32 #include "dbushandler.h"
37 DBusMessageHelper::DBusMessageHelper(DBusMessage* message) : m_message(message), m_message_ptr(nullptr)
40 throw std::runtime_error("Empty DBUS message");
43 if (dbus_message_is_signal(m_message, "com.samsung.sam.interface", "Control")) {
45 if (dbus_message_iter_init(m_message, &args) && (dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING)) {
46 dbus_message_iter_get_basic(&args, &m_message_ptr);
51 DBusMessageHelper::~DBusMessageHelper()
53 dbus_message_unref(m_message);
56 DBusListener::DBusListener(AllowedPredicate&& allowed) : m_connection(nullptr), m_predicate(allowed), m_handlers(), m_mutex()
58 if ((m_connection = dbus_bus_get(DBUS_BUS_SYSTEM, nullptr)) == nullptr) {
59 LOG_E(TAG, "dbus_bus_get failed!");
60 throw std::runtime_error("dbus_bus_get failed!");
63 if (dbus_bus_request_name(m_connection, "com.samsung.sam.sink", DBUS_NAME_FLAG_REPLACE_EXISTING , nullptr) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
64 LOG_E(TAG, "dbus_bus_request_name failed!");
65 dbus_connection_unref(m_connection);
66 throw std::runtime_error("dbus_bus_request_name failed!");
69 dbus_bus_add_match(m_connection, "type='signal',interface='com.samsung.sam.interface'", nullptr);
70 dbus_connection_flush(m_connection);
73 DBusListener::~DBusListener()
75 dbus_connection_unref(m_connection);
78 bool DBusListener::addHandler(const std::string& type, DBusHandler* handler)
80 std::lock_guard<std::mutex> lock(m_mutex);
81 return (m_handlers.insert(std::make_pair(type, handler)).second == true);
84 void DBusListener::removeHandler(const std::string& type)
86 std::lock_guard<std::mutex> lock(m_mutex);
87 m_handlers.erase(type);
90 void DBusListener::loop()
94 dbus_connection_read_write(m_connection, 0);
96 DBusMessage* message = dbus_connection_pop_message(m_connection);
97 if (message == nullptr) {
98 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
102 DBusMessageHelper helper(message);
103 const char* message_ptr = helper.message();
106 LOG_D(TAG, "DBUS message received: %.256s", message_ptr);
111 if (reader.parse(message_ptr, value)) {
112 std::lock_guard<std::mutex> lock(m_mutex);
113 auto it = m_handlers.find(value["type"].asString());
114 if (it != m_handlers.end()) {
115 it->second->process(value["data"]);
117 LOG_E(TAG, "Unknown type");
120 LOG_E(TAG, "Incorrect JSON");
123 } catch (std::exception& e) {
124 LOG_E(TAG, "DBusListener loop exception: %s", e.what());