2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
22 #include <Commons/Exception.h>
24 namespace WrtDeviceApis {
26 Message::Message(DBusMessage* message) : m_message(message)
28 assert(m_message && "Message cannot be NULL.");
32 Message::Message(int type)
34 m_message = dbus_message_new(type);
36 ThrowMsg(Commons::PlatformException, "Out of memory.");
46 int Message::getType() const
48 return dbus_message_get_type(m_message);
51 std::string Message::getInterface() const
53 const char* interface = dbus_message_get_interface(m_message);
54 return (interface ? interface : std::string());
57 std::string Message::getPath() const
59 const char* path = dbus_message_get_path(m_message);
60 return (path ? path : std::string());
63 bool Message::isSignal(const std::string& name) const
65 return (dbus_message_is_signal(m_message,
66 getInterface().c_str(),
71 Message::ReadIterator Message::getReadIterator() const
73 return ReadIterator(new ReadIterator_(m_message));
76 void Message::addRef()
78 m_message = dbus_message_ref(m_message);
83 dbus_message_unref(m_message);
86 Message::ReadIterator_::ReadIterator_(DBusMessage* message)
88 assert(message && "Message cannot be NULL.");
89 m_valid = dbus_message_iter_init(message, &m_iterator);
92 bool Message::ReadIterator_::next()
95 Throw(Commons::OutOfRangeException);
97 return (m_valid = (dbus_message_iter_next(&m_iterator) == TRUE));
100 bool Message::ReadIterator_::isValid() const
105 int Message::ReadIterator_::getArgType() const
108 Throw(Commons::OutOfRangeException);
110 return dbus_message_iter_get_arg_type(&m_iterator);
113 int Message::ReadIterator_::getInt() const
115 return static_cast<int>(getArgBasic<dbus_int32_t>(DBUS_TYPE_INT32));
118 std::string Message::ReadIterator_::getString() const
120 return getArgBasic<const char*>(DBUS_TYPE_STRING);
124 T Message::ReadIterator_::getArgBasic(int type) const
126 if (getArgType() != type) {
127 ThrowMsg(Commons::ConversionException, "Type mismatch.");
131 Throw(Commons::OutOfRangeException);
135 dbus_message_iter_get_basic(&m_iterator, &result);