1 /*
2 * Copyright (c) 2019, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "dbus/common/dbus_message_helper.hpp"
30
31 namespace otbr {
32 namespace DBus {
33
DBusMessageExtract(DBusMessageIter * aIter,bool & aValue)34 otbrError DBusMessageExtract(DBusMessageIter *aIter, bool &aValue)
35 {
36 otbrError error = OTBR_ERROR_DBUS;
37 dbus_bool_t val;
38
39 VerifyOrExit(dbus_message_iter_get_arg_type(aIter) == DBUS_TYPE_BOOLEAN);
40 dbus_message_iter_get_basic(aIter, &val);
41 dbus_message_iter_next(aIter);
42 aValue = (val != 0);
43 error = OTBR_ERROR_NONE;
44
45 exit:
46 return error;
47 }
48
DBusMessageExtract(DBusMessageIter * aIter,int8_t & aValue)49 otbrError DBusMessageExtract(DBusMessageIter *aIter, int8_t &aValue)
50 {
51 otbrError error = OTBR_ERROR_NONE;
52 uint8_t val;
53
54 SuccessOrExit(error = DBusMessageExtract(aIter, val));
55 aValue = static_cast<int8_t>(val);
56 exit:
57 return error;
58 }
59
DBusMessageExtract(DBusMessageIter * aIter,std::string & aValue)60 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::string &aValue)
61 {
62 const char *buf;
63 otbrError error = OTBR_ERROR_NONE;
64
65 VerifyOrExit(dbus_message_iter_get_arg_type(aIter) == DBUS_TYPE_STRING, error = OTBR_ERROR_DBUS);
66 dbus_message_iter_get_basic(aIter, &buf);
67 dbus_message_iter_next(aIter);
68 aValue = buf;
69
70 exit:
71 return error;
72 }
73
DBusMessageExtract(DBusMessageIter * aIter,std::vector<uint8_t> & aValue)74 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<uint8_t> &aValue)
75 {
76 return DBusMessageExtractPrimitive(aIter, aValue);
77 }
78
DBusMessageExtract(DBusMessageIter * aIter,std::vector<uint16_t> & aValue)79 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<uint16_t> &aValue)
80 {
81 return DBusMessageExtractPrimitive(aIter, aValue);
82 }
83
DBusMessageExtract(DBusMessageIter * aIter,std::vector<uint32_t> & aValue)84 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<uint32_t> &aValue)
85 {
86 return DBusMessageExtractPrimitive(aIter, aValue);
87 }
88
DBusMessageExtract(DBusMessageIter * aIter,std::vector<uint64_t> & aValue)89 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<uint64_t> &aValue)
90 {
91 return DBusMessageExtractPrimitive(aIter, aValue);
92 }
93
DBusMessageExtract(DBusMessageIter * aIter,std::vector<int16_t> & aValue)94 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<int16_t> &aValue)
95 {
96 return DBusMessageExtractPrimitive(aIter, aValue);
97 }
98
DBusMessageExtract(DBusMessageIter * aIter,std::vector<int32_t> & aValue)99 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<int32_t> &aValue)
100 {
101 return DBusMessageExtractPrimitive(aIter, aValue);
102 }
103
DBusMessageExtract(DBusMessageIter * aIter,std::vector<int64_t> & aValue)104 otbrError DBusMessageExtract(DBusMessageIter *aIter, std::vector<int64_t> &aValue)
105 {
106 return DBusMessageExtractPrimitive(aIter, aValue);
107 }
108
DBusMessageEncode(DBusMessageIter * aIter,bool aValue)109 otbrError DBusMessageEncode(DBusMessageIter *aIter, bool aValue)
110 {
111 dbus_bool_t val = aValue ? 1 : 0;
112 otbrError error = OTBR_ERROR_NONE;
113
114 VerifyOrExit(dbus_message_iter_append_basic(aIter, DBUS_TYPE_BOOLEAN, &val), error = OTBR_ERROR_DBUS);
115
116 exit:
117 return error;
118 }
119
DBusMessageEncode(DBusMessageIter * aIter,int8_t aValue)120 otbrError DBusMessageEncode(DBusMessageIter *aIter, int8_t aValue)
121 {
122 return DBusMessageEncode(aIter, static_cast<uint8_t>(aValue));
123 }
124
DBusMessageEncode(DBusMessageIter * aIter,const std::string & aValue)125 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::string &aValue)
126 {
127 otbrError error = OTBR_ERROR_NONE;
128 const char *buf = aValue.c_str();
129
130 VerifyOrExit(dbus_message_iter_append_basic(aIter, DBUS_TYPE_STRING, &buf), error = OTBR_ERROR_DBUS);
131
132 exit:
133 return error;
134 }
135
DBusMessageEncode(DBusMessageIter * aIter,const char * aValue)136 otbrError DBusMessageEncode(DBusMessageIter *aIter, const char *aValue)
137 {
138 otbrError error = OTBR_ERROR_NONE;
139
140 VerifyOrExit(dbus_message_iter_append_basic(aIter, DBUS_TYPE_STRING, &aValue), error = OTBR_ERROR_DBUS);
141 exit:
142 return error;
143 }
144
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<uint8_t> & aValue)145 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<uint8_t> &aValue)
146 {
147 return DBusMessageEncodePrimitive(aIter, aValue);
148 }
149
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<uint16_t> & aValue)150 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<uint16_t> &aValue)
151 {
152 return DBusMessageEncodePrimitive(aIter, aValue);
153 }
154
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<uint32_t> & aValue)155 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<uint32_t> &aValue)
156 {
157 return DBusMessageEncodePrimitive(aIter, aValue);
158 }
159
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<uint64_t> & aValue)160 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<uint64_t> &aValue)
161 {
162 return DBusMessageEncodePrimitive(aIter, aValue);
163 }
164
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<int16_t> & aValue)165 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<int16_t> &aValue)
166 {
167 return DBusMessageEncodePrimitive(aIter, aValue);
168 }
169
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<int32_t> & aValue)170 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<int32_t> &aValue)
171 {
172 return DBusMessageEncodePrimitive(aIter, aValue);
173 }
174
DBusMessageEncode(DBusMessageIter * aIter,const std::vector<int64_t> & aValue)175 otbrError DBusMessageEncode(DBusMessageIter *aIter, const std::vector<int64_t> &aValue)
176 {
177 return DBusMessageEncodePrimitive(aIter, aValue);
178 }
179
IsDBusMessageEmpty(DBusMessage & aMessage)180 bool IsDBusMessageEmpty(DBusMessage &aMessage)
181 {
182 DBusMessageIter iter;
183
184 if (!dbus_message_iter_init(&aMessage, &iter))
185 {
186 return true;
187 }
188 return dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INVALID;
189 }
190
191 } // namespace DBus
192 } // namespace otbr
193