summaryrefslogtreecommitdiff
path: root/src/components/dbus/src/dbus_message.cc
blob: b8a55896ceda21deb0bb75c02cbce97838a0df7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* Copyright (c) 2014, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "dbus/dbus_message.h"

#include <algorithm>
#include <cassert>

#include <dbus/dbus.h>

namespace dbus {

MessageRefKeeper::MessageRefKeeper() : raw_message_(NULL) {}

MessageRefKeeper::MessageRefKeeper(DBusMessage* message)
    : raw_message_(message) {
  assert(raw_message_);
  dbus_message_ref(raw_message_);
}

MessageRefKeeper::~MessageRefKeeper() {
  if (raw_message_) {
    dbus_message_unref(raw_message_);
  }
}

MessageRefKeeper::MessageRefKeeper(const MessageRefKeeper& other)
    : raw_message_(other.raw_message_) {
  assert(raw_message_);
  dbus_message_ref(raw_message_);
}

MessageRefKeeper& MessageRefKeeper::operator=(MessageRefKeeper other) {
  other.swap(*this);
  return *this;
}

DBusMessage* MessageRefKeeper::get() const {
  return raw_message_;
}

DBusMessage* MessageRefKeeper::Pass() {
  DBusMessage* message = raw_message_;
  raw_message_ = NULL;
  return message;
}

void MessageRefKeeper::swap(MessageRefKeeper& other) {
  std::swap(raw_message_, other.raw_message_);
}

MessageRef::MessageRef(DBusMessage* message) : raw_message_ref_(message) {}

MessageRef::~MessageRef() {}

std::string MessageRef::GetInterface() const {
  return dbus_message_get_interface(raw_message_ref_.get());
}

std::string MessageRef::GetMember() const {
  return dbus_message_get_member(raw_message_ref_.get());
}

// Message writer
MessageReader::MessageReader(const MessageRef& message)
    : parent_reader_(NULL), failed_(false) {
  dbus_bool_t success =
      dbus_message_iter_init(message.raw_message_ref_.get(), &iterator_);
  if (!success) {
    MarkFailed();
  }
}

MessageReader::MessageReader(MessageReader* reader,
                             DataType container_data_type)
    : parent_reader_(reader), failed_(reader->has_failed()) {
  assert(parent_reader_);
  if (!failed_) {
    if (reader->NextValueType() != container_data_type) {
      MarkFailed();
    } else {
      dbus_message_iter_recurse(&reader->iterator_, &iterator_);
      reader->MoveToNext();
    }
  }
}

bool MessageReader::NextIsDictEntry() const {
  return NextValueType() == DBUS_TYPE_DICT_ENTRY;
}

MessageReader MessageReader::TakeArrayReader() {
  return MessageReader(this, DBUS_TYPE_ARRAY);
}

MessageReader MessageReader::TakeStructReader() {
  return MessageReader(this, DBUS_TYPE_STRUCT);
}

MessageReader MessageReader::TakeDictEntryReader() {
  return MessageReader(this, DBUS_TYPE_DICT_ENTRY);
}

void MessageReader::MoveToNext() {
  if (!HasNext()) {
    MarkFailed();
  }
  dbus_message_iter_next(&iterator_);
}

void MessageReader::MarkFailed() {
  if (!failed_) {
    failed_ = true;
    if (parent_reader_) {
      parent_reader_->MarkFailed();
    }
  }
}

void MessageReader::ReadNextValue(MessageReader::DataType type, void* value) {
  assert(value);
  if (NextValueType() != type) {
    MarkFailed();
  } else if (!failed_) {
    dbus_message_iter_get_basic(&iterator_, value);
    MoveToNext();
  }
}

// DbusMessageWriter
MessageWriter::MessageWriter(const MessageRef& message)
    : has_opened_subcontainer_(false), parent_writer_(NULL) {
  dbus_message_iter_init_append(message.raw_message_ref_.get(), &iterator_);
}

MessageWriter::MessageWriter(MessageWriter* parent,
                             ContainerType container_type,
                             const char* array_signature)
    : has_opened_subcontainer_(false), parent_writer_(parent) {
  assert(parent_writer_);
  // Only array writers require array_signature
  assert((container_type == kArray && array_signature != NULL) ||
         (container_type != kArray && array_signature == NULL));
  assert(!parent_writer_->has_opened_subcontainer_);
  bool enough_memory = dbus_message_iter_open_container(
      &parent_writer_->iterator_, container_type, array_signature, &iterator_);
  assert(enough_memory);
  parent_writer_->has_opened_subcontainer_ = true;
}

MessageWriter::~MessageWriter() {
  assert(!has_opened_subcontainer_);
  if (parent_writer_ != NULL) {
    CloseWriter();
  }
}

void MessageWriter::WriteAndCheck(MessageWriter::DataType value_type,
                                  const void* value) {
  assert(!has_opened_subcontainer_);
  dbus_bool_t enough_memory =
      dbus_message_iter_append_basic(&iterator_, value_type, value);
  assert(enough_memory);
}

void MessageWriter::CloseWriter() {
  assert(parent_writer_);
  assert(parent_writer_->has_opened_subcontainer_);
  dbus_bool_t enough_memory =
      dbus_message_iter_close_container(&parent_writer_->iterator_, &iterator_);
  assert(enough_memory);
  parent_writer_->has_opened_subcontainer_ = false;
}

// Construction functions

MessageRef MethodCall(const char* bus_name,
                      const char* path,
                      const char* interface,
                      const char* method) {
  return MessageRef(
      dbus_message_new_method_call(bus_name, path, interface, method));
}

MessageRef Signal(const char* path, const char* interface, const char* name) {
  return MessageRef(dbus_message_new_signal(path, interface, name));
}

}  // namespace dbus