1*4a64e381SAndroid Build Coastguard Worker /*
2*4a64e381SAndroid Build Coastguard Worker * Copyright (c) 2019, The OpenThread Authors.
3*4a64e381SAndroid Build Coastguard Worker * All rights reserved.
4*4a64e381SAndroid Build Coastguard Worker *
5*4a64e381SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*4a64e381SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*4a64e381SAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
8*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*4a64e381SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*4a64e381SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*4a64e381SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*4a64e381SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*4a64e381SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*4a64e381SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*4a64e381SAndroid Build Coastguard Worker *
16*4a64e381SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*4a64e381SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*4a64e381SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*4a64e381SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*4a64e381SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*4a64e381SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*4a64e381SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*4a64e381SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*4a64e381SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*4a64e381SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*4a64e381SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*4a64e381SAndroid Build Coastguard Worker */
28*4a64e381SAndroid Build Coastguard Worker
29*4a64e381SAndroid Build Coastguard Worker #define OTBR_LOG_TAG "DBUS"
30*4a64e381SAndroid Build Coastguard Worker
31*4a64e381SAndroid Build Coastguard Worker #include <assert.h>
32*4a64e381SAndroid Build Coastguard Worker #include <stdio.h>
33*4a64e381SAndroid Build Coastguard Worker #include <string.h>
34*4a64e381SAndroid Build Coastguard Worker
35*4a64e381SAndroid Build Coastguard Worker #include <dbus/dbus.h>
36*4a64e381SAndroid Build Coastguard Worker
37*4a64e381SAndroid Build Coastguard Worker #include "common/logging.hpp"
38*4a64e381SAndroid Build Coastguard Worker #include "dbus/common/dbus_message_dump.hpp"
39*4a64e381SAndroid Build Coastguard Worker #include "dbus/server/dbus_object.hpp"
40*4a64e381SAndroid Build Coastguard Worker
41*4a64e381SAndroid Build Coastguard Worker using std::placeholders::_1;
42*4a64e381SAndroid Build Coastguard Worker
43*4a64e381SAndroid Build Coastguard Worker namespace otbr {
44*4a64e381SAndroid Build Coastguard Worker namespace DBus {
45*4a64e381SAndroid Build Coastguard Worker
DBusObject(DBusConnection * aConnection,const std::string & aObjectPath)46*4a64e381SAndroid Build Coastguard Worker DBusObject::DBusObject(DBusConnection *aConnection, const std::string &aObjectPath)
47*4a64e381SAndroid Build Coastguard Worker : mConnection(aConnection)
48*4a64e381SAndroid Build Coastguard Worker , mObjectPath(aObjectPath)
49*4a64e381SAndroid Build Coastguard Worker {
50*4a64e381SAndroid Build Coastguard Worker }
51*4a64e381SAndroid Build Coastguard Worker
Init(void)52*4a64e381SAndroid Build Coastguard Worker otbrError DBusObject::Init(void)
53*4a64e381SAndroid Build Coastguard Worker {
54*4a64e381SAndroid Build Coastguard Worker return Initialize(/* aIsAsyncPropertyHandler */ false);
55*4a64e381SAndroid Build Coastguard Worker }
56*4a64e381SAndroid Build Coastguard Worker
Initialize(bool aIsAsyncPropertyHandler)57*4a64e381SAndroid Build Coastguard Worker otbrError DBusObject::Initialize(bool aIsAsyncPropertyHandler)
58*4a64e381SAndroid Build Coastguard Worker {
59*4a64e381SAndroid Build Coastguard Worker otbrError error = OTBR_ERROR_NONE;
60*4a64e381SAndroid Build Coastguard Worker DBusObjectPathVTable vTable;
61*4a64e381SAndroid Build Coastguard Worker
62*4a64e381SAndroid Build Coastguard Worker memset(&vTable, 0, sizeof(vTable));
63*4a64e381SAndroid Build Coastguard Worker
64*4a64e381SAndroid Build Coastguard Worker vTable.message_function = DBusObject::sMessageHandler;
65*4a64e381SAndroid Build Coastguard Worker
66*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_connection_register_object_path(mConnection, mObjectPath.c_str(), &vTable, this),
67*4a64e381SAndroid Build Coastguard Worker error = OTBR_ERROR_DBUS);
68*4a64e381SAndroid Build Coastguard Worker
69*4a64e381SAndroid Build Coastguard Worker if (aIsAsyncPropertyHandler)
70*4a64e381SAndroid Build Coastguard Worker {
71*4a64e381SAndroid Build Coastguard Worker RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_METHOD,
72*4a64e381SAndroid Build Coastguard Worker std::bind(&DBusObject::AsyncGetPropertyMethodHandler, this, _1));
73*4a64e381SAndroid Build Coastguard Worker }
74*4a64e381SAndroid Build Coastguard Worker else
75*4a64e381SAndroid Build Coastguard Worker {
76*4a64e381SAndroid Build Coastguard Worker RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_METHOD,
77*4a64e381SAndroid Build Coastguard Worker std::bind(&DBusObject::GetPropertyMethodHandler, this, _1));
78*4a64e381SAndroid Build Coastguard Worker RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_SET_METHOD,
79*4a64e381SAndroid Build Coastguard Worker std::bind(&DBusObject::SetPropertyMethodHandler, this, _1));
80*4a64e381SAndroid Build Coastguard Worker RegisterMethod(DBUS_INTERFACE_PROPERTIES, DBUS_PROPERTY_GET_ALL_METHOD,
81*4a64e381SAndroid Build Coastguard Worker std::bind(&DBusObject::GetAllPropertiesMethodHandler, this, _1));
82*4a64e381SAndroid Build Coastguard Worker }
83*4a64e381SAndroid Build Coastguard Worker
84*4a64e381SAndroid Build Coastguard Worker exit:
85*4a64e381SAndroid Build Coastguard Worker return error;
86*4a64e381SAndroid Build Coastguard Worker }
87*4a64e381SAndroid Build Coastguard Worker
RegisterMethod(const std::string & aInterfaceName,const std::string & aMethodName,const MethodHandlerType & aHandler)88*4a64e381SAndroid Build Coastguard Worker void DBusObject::RegisterMethod(const std::string &aInterfaceName,
89*4a64e381SAndroid Build Coastguard Worker const std::string &aMethodName,
90*4a64e381SAndroid Build Coastguard Worker const MethodHandlerType &aHandler)
91*4a64e381SAndroid Build Coastguard Worker {
92*4a64e381SAndroid Build Coastguard Worker std::string fullPath = aInterfaceName + "." + aMethodName;
93*4a64e381SAndroid Build Coastguard Worker
94*4a64e381SAndroid Build Coastguard Worker assert(mMethodHandlers.find(fullPath) == mMethodHandlers.end());
95*4a64e381SAndroid Build Coastguard Worker mMethodHandlers.emplace(fullPath, aHandler);
96*4a64e381SAndroid Build Coastguard Worker }
97*4a64e381SAndroid Build Coastguard Worker
RegisterGetPropertyHandler(const std::string & aInterfaceName,const std::string & aPropertyName,const PropertyHandlerType & aHandler)98*4a64e381SAndroid Build Coastguard Worker void DBusObject::RegisterGetPropertyHandler(const std::string &aInterfaceName,
99*4a64e381SAndroid Build Coastguard Worker const std::string &aPropertyName,
100*4a64e381SAndroid Build Coastguard Worker const PropertyHandlerType &aHandler)
101*4a64e381SAndroid Build Coastguard Worker {
102*4a64e381SAndroid Build Coastguard Worker mGetPropertyHandlers[aInterfaceName].emplace(aPropertyName, aHandler);
103*4a64e381SAndroid Build Coastguard Worker }
104*4a64e381SAndroid Build Coastguard Worker
RegisterSetPropertyHandler(const std::string & aInterfaceName,const std::string & aPropertyName,const PropertyHandlerType & aHandler)105*4a64e381SAndroid Build Coastguard Worker void DBusObject::RegisterSetPropertyHandler(const std::string &aInterfaceName,
106*4a64e381SAndroid Build Coastguard Worker const std::string &aPropertyName,
107*4a64e381SAndroid Build Coastguard Worker const PropertyHandlerType &aHandler)
108*4a64e381SAndroid Build Coastguard Worker {
109*4a64e381SAndroid Build Coastguard Worker std::string fullPath = aInterfaceName + "." + aPropertyName;
110*4a64e381SAndroid Build Coastguard Worker
111*4a64e381SAndroid Build Coastguard Worker assert(mSetPropertyHandlers.find(fullPath) == mSetPropertyHandlers.end());
112*4a64e381SAndroid Build Coastguard Worker mSetPropertyHandlers.emplace(fullPath, aHandler);
113*4a64e381SAndroid Build Coastguard Worker }
114*4a64e381SAndroid Build Coastguard Worker
RegisterAsyncGetPropertyHandler(const std::string & aInterfaceName,const std::string & aPropertyName,const AsyncPropertyHandlerType & aHandler)115*4a64e381SAndroid Build Coastguard Worker void DBusObject::RegisterAsyncGetPropertyHandler(const std::string &aInterfaceName,
116*4a64e381SAndroid Build Coastguard Worker const std::string &aPropertyName,
117*4a64e381SAndroid Build Coastguard Worker const AsyncPropertyHandlerType &aHandler)
118*4a64e381SAndroid Build Coastguard Worker {
119*4a64e381SAndroid Build Coastguard Worker mAsyncGetPropertyHandlers[aInterfaceName].emplace(aPropertyName, aHandler);
120*4a64e381SAndroid Build Coastguard Worker }
121*4a64e381SAndroid Build Coastguard Worker
sMessageHandler(DBusConnection * aConnection,DBusMessage * aMessage,void * aData)122*4a64e381SAndroid Build Coastguard Worker DBusHandlerResult DBusObject::sMessageHandler(DBusConnection *aConnection, DBusMessage *aMessage, void *aData)
123*4a64e381SAndroid Build Coastguard Worker {
124*4a64e381SAndroid Build Coastguard Worker DBusObject *server = reinterpret_cast<DBusObject *>(aData);
125*4a64e381SAndroid Build Coastguard Worker
126*4a64e381SAndroid Build Coastguard Worker return server->MessageHandler(aConnection, aMessage);
127*4a64e381SAndroid Build Coastguard Worker }
128*4a64e381SAndroid Build Coastguard Worker
MessageHandler(DBusConnection * aConnection,DBusMessage * aMessage)129*4a64e381SAndroid Build Coastguard Worker DBusHandlerResult DBusObject::MessageHandler(DBusConnection *aConnection, DBusMessage *aMessage)
130*4a64e381SAndroid Build Coastguard Worker {
131*4a64e381SAndroid Build Coastguard Worker DBusHandlerResult handled = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
132*4a64e381SAndroid Build Coastguard Worker DBusRequest request(aConnection, aMessage);
133*4a64e381SAndroid Build Coastguard Worker std::string interface = dbus_message_get_interface(aMessage);
134*4a64e381SAndroid Build Coastguard Worker std::string memberName = interface + "." + dbus_message_get_member(aMessage);
135*4a64e381SAndroid Build Coastguard Worker auto iter = mMethodHandlers.find(memberName);
136*4a64e381SAndroid Build Coastguard Worker
137*4a64e381SAndroid Build Coastguard Worker if (dbus_message_get_type(aMessage) == DBUS_MESSAGE_TYPE_METHOD_CALL && iter != mMethodHandlers.end())
138*4a64e381SAndroid Build Coastguard Worker {
139*4a64e381SAndroid Build Coastguard Worker otbrLogDebug("Handling method %s", memberName.c_str());
140*4a64e381SAndroid Build Coastguard Worker if (otbrLogGetLevel() >= OTBR_LOG_DEBUG)
141*4a64e381SAndroid Build Coastguard Worker {
142*4a64e381SAndroid Build Coastguard Worker DumpDBusMessage(*aMessage);
143*4a64e381SAndroid Build Coastguard Worker }
144*4a64e381SAndroid Build Coastguard Worker (iter->second)(request);
145*4a64e381SAndroid Build Coastguard Worker handled = DBUS_HANDLER_RESULT_HANDLED;
146*4a64e381SAndroid Build Coastguard Worker }
147*4a64e381SAndroid Build Coastguard Worker
148*4a64e381SAndroid Build Coastguard Worker return handled;
149*4a64e381SAndroid Build Coastguard Worker }
150*4a64e381SAndroid Build Coastguard Worker
GetPropertyMethodHandler(DBusRequest & aRequest)151*4a64e381SAndroid Build Coastguard Worker void DBusObject::GetPropertyMethodHandler(DBusRequest &aRequest)
152*4a64e381SAndroid Build Coastguard Worker {
153*4a64e381SAndroid Build Coastguard Worker UniqueDBusMessage reply{dbus_message_new_method_return(aRequest.GetMessage())};
154*4a64e381SAndroid Build Coastguard Worker
155*4a64e381SAndroid Build Coastguard Worker DBusMessageIter iter;
156*4a64e381SAndroid Build Coastguard Worker std::string interfaceName;
157*4a64e381SAndroid Build Coastguard Worker std::string propertyName;
158*4a64e381SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
159*4a64e381SAndroid Build Coastguard Worker otError replyError = OT_ERROR_NONE;
160*4a64e381SAndroid Build Coastguard Worker
161*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(reply != nullptr, error = OT_ERROR_NO_BUFS);
162*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_init(aRequest.GetMessage(), &iter), error = OT_ERROR_FAILED);
163*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(DBusMessageExtract(&iter, interfaceName) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
164*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(DBusMessageExtract(&iter, propertyName) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
165*4a64e381SAndroid Build Coastguard Worker {
166*4a64e381SAndroid Build Coastguard Worker auto propertyIter = mGetPropertyHandlers.find(interfaceName);
167*4a64e381SAndroid Build Coastguard Worker
168*4a64e381SAndroid Build Coastguard Worker otbrLogDebug("GetProperty %s.%s", interfaceName.c_str(), propertyName.c_str());
169*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(propertyIter != mGetPropertyHandlers.end(), error = OT_ERROR_NOT_FOUND);
170*4a64e381SAndroid Build Coastguard Worker {
171*4a64e381SAndroid Build Coastguard Worker DBusMessageIter replyIter;
172*4a64e381SAndroid Build Coastguard Worker auto &interfaceHandlers = propertyIter->second;
173*4a64e381SAndroid Build Coastguard Worker auto interfaceIter = interfaceHandlers.find(propertyName);
174*4a64e381SAndroid Build Coastguard Worker
175*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(interfaceIter != interfaceHandlers.end(), error = OT_ERROR_NOT_FOUND);
176*4a64e381SAndroid Build Coastguard Worker dbus_message_iter_init_append(reply.get(), &replyIter);
177*4a64e381SAndroid Build Coastguard Worker SuccessOrExit(replyError = interfaceIter->second(replyIter));
178*4a64e381SAndroid Build Coastguard Worker }
179*4a64e381SAndroid Build Coastguard Worker }
180*4a64e381SAndroid Build Coastguard Worker exit:
181*4a64e381SAndroid Build Coastguard Worker if (error == OT_ERROR_NONE && replyError == OT_ERROR_NONE)
182*4a64e381SAndroid Build Coastguard Worker {
183*4a64e381SAndroid Build Coastguard Worker if (otbrLogGetLevel() >= OTBR_LOG_DEBUG)
184*4a64e381SAndroid Build Coastguard Worker {
185*4a64e381SAndroid Build Coastguard Worker otbrLogDebug("GetProperty %s.%s reply:", interfaceName.c_str(), propertyName.c_str());
186*4a64e381SAndroid Build Coastguard Worker DumpDBusMessage(*reply);
187*4a64e381SAndroid Build Coastguard Worker }
188*4a64e381SAndroid Build Coastguard Worker
189*4a64e381SAndroid Build Coastguard Worker dbus_connection_send(aRequest.GetConnection(), reply.get(), nullptr);
190*4a64e381SAndroid Build Coastguard Worker }
191*4a64e381SAndroid Build Coastguard Worker else if (error == OT_ERROR_NONE)
192*4a64e381SAndroid Build Coastguard Worker {
193*4a64e381SAndroid Build Coastguard Worker otbrLogInfo("GetProperty %s.%s reply:%s", interfaceName.c_str(), propertyName.c_str(),
194*4a64e381SAndroid Build Coastguard Worker ConvertToDBusErrorName(replyError));
195*4a64e381SAndroid Build Coastguard Worker aRequest.ReplyOtResult(replyError);
196*4a64e381SAndroid Build Coastguard Worker }
197*4a64e381SAndroid Build Coastguard Worker else
198*4a64e381SAndroid Build Coastguard Worker {
199*4a64e381SAndroid Build Coastguard Worker otbrLogWarning("GetProperty %s.%s error:%s", interfaceName.c_str(), propertyName.c_str(),
200*4a64e381SAndroid Build Coastguard Worker ConvertToDBusErrorName(error));
201*4a64e381SAndroid Build Coastguard Worker aRequest.ReplyOtResult(error);
202*4a64e381SAndroid Build Coastguard Worker }
203*4a64e381SAndroid Build Coastguard Worker }
204*4a64e381SAndroid Build Coastguard Worker
GetAllPropertiesMethodHandler(DBusRequest & aRequest)205*4a64e381SAndroid Build Coastguard Worker void DBusObject::GetAllPropertiesMethodHandler(DBusRequest &aRequest)
206*4a64e381SAndroid Build Coastguard Worker {
207*4a64e381SAndroid Build Coastguard Worker UniqueDBusMessage reply{dbus_message_new_method_return(aRequest.GetMessage())};
208*4a64e381SAndroid Build Coastguard Worker DBusMessageIter iter, subIter, dictEntryIter;
209*4a64e381SAndroid Build Coastguard Worker std::string interfaceName;
210*4a64e381SAndroid Build Coastguard Worker auto args = std::tie(interfaceName);
211*4a64e381SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
212*4a64e381SAndroid Build Coastguard Worker
213*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(reply != nullptr, error = OT_ERROR_NO_BUFS);
214*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(DBusMessageToTuple(*aRequest.GetMessage(), args) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
215*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(mGetPropertyHandlers.find(interfaceName) != mGetPropertyHandlers.end(), error = OT_ERROR_NOT_FOUND);
216*4a64e381SAndroid Build Coastguard Worker dbus_message_iter_init_append(reply.get(), &iter);
217*4a64e381SAndroid Build Coastguard Worker
218*4a64e381SAndroid Build Coastguard Worker for (auto &p : mGetPropertyHandlers.at(interfaceName))
219*4a64e381SAndroid Build Coastguard Worker {
220*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
221*4a64e381SAndroid Build Coastguard Worker "{" DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING "}",
222*4a64e381SAndroid Build Coastguard Worker &subIter),
223*4a64e381SAndroid Build Coastguard Worker error = OT_ERROR_FAILED);
224*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_open_container(&subIter, DBUS_TYPE_DICT_ENTRY, nullptr, &dictEntryIter),
225*4a64e381SAndroid Build Coastguard Worker error = OT_ERROR_FAILED);
226*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(DBusMessageEncode(&dictEntryIter, p.first) == OTBR_ERROR_NONE, error = OT_ERROR_FAILED);
227*4a64e381SAndroid Build Coastguard Worker
228*4a64e381SAndroid Build Coastguard Worker SuccessOrExit(error = p.second(dictEntryIter));
229*4a64e381SAndroid Build Coastguard Worker
230*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_close_container(&subIter, &dictEntryIter), error = OT_ERROR_FAILED);
231*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_close_container(&iter, &subIter));
232*4a64e381SAndroid Build Coastguard Worker }
233*4a64e381SAndroid Build Coastguard Worker
234*4a64e381SAndroid Build Coastguard Worker exit:
235*4a64e381SAndroid Build Coastguard Worker if (error == OT_ERROR_NONE)
236*4a64e381SAndroid Build Coastguard Worker {
237*4a64e381SAndroid Build Coastguard Worker dbus_connection_send(aRequest.GetConnection(), reply.get(), nullptr);
238*4a64e381SAndroid Build Coastguard Worker }
239*4a64e381SAndroid Build Coastguard Worker else
240*4a64e381SAndroid Build Coastguard Worker {
241*4a64e381SAndroid Build Coastguard Worker aRequest.ReplyOtResult(error);
242*4a64e381SAndroid Build Coastguard Worker }
243*4a64e381SAndroid Build Coastguard Worker }
244*4a64e381SAndroid Build Coastguard Worker
SetPropertyMethodHandler(DBusRequest & aRequest)245*4a64e381SAndroid Build Coastguard Worker void DBusObject::SetPropertyMethodHandler(DBusRequest &aRequest)
246*4a64e381SAndroid Build Coastguard Worker {
247*4a64e381SAndroid Build Coastguard Worker DBusMessageIter iter;
248*4a64e381SAndroid Build Coastguard Worker std::string interfaceName;
249*4a64e381SAndroid Build Coastguard Worker std::string propertyName;
250*4a64e381SAndroid Build Coastguard Worker std::string propertyFullPath;
251*4a64e381SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
252*4a64e381SAndroid Build Coastguard Worker
253*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_init(aRequest.GetMessage(), &iter), error = OT_ERROR_FAILED);
254*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(DBusMessageExtract(&iter, interfaceName) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
255*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(DBusMessageExtract(&iter, propertyName) == OTBR_ERROR_NONE, error = OT_ERROR_PARSE);
256*4a64e381SAndroid Build Coastguard Worker
257*4a64e381SAndroid Build Coastguard Worker propertyFullPath = interfaceName + "." + propertyName;
258*4a64e381SAndroid Build Coastguard Worker otbrLogInfo("SetProperty %s", propertyFullPath.c_str());
259*4a64e381SAndroid Build Coastguard Worker {
260*4a64e381SAndroid Build Coastguard Worker auto handlerIter = mSetPropertyHandlers.find(propertyFullPath);
261*4a64e381SAndroid Build Coastguard Worker
262*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(handlerIter != mSetPropertyHandlers.end(), error = OT_ERROR_NOT_FOUND);
263*4a64e381SAndroid Build Coastguard Worker error = handlerIter->second(iter);
264*4a64e381SAndroid Build Coastguard Worker }
265*4a64e381SAndroid Build Coastguard Worker
266*4a64e381SAndroid Build Coastguard Worker exit:
267*4a64e381SAndroid Build Coastguard Worker if (error != OT_ERROR_NONE)
268*4a64e381SAndroid Build Coastguard Worker {
269*4a64e381SAndroid Build Coastguard Worker otbrLogWarning("SetProperty %s.%s error:%s", interfaceName.c_str(), propertyName.c_str(),
270*4a64e381SAndroid Build Coastguard Worker ConvertToDBusErrorName(error));
271*4a64e381SAndroid Build Coastguard Worker }
272*4a64e381SAndroid Build Coastguard Worker aRequest.ReplyOtResult(error);
273*4a64e381SAndroid Build Coastguard Worker return;
274*4a64e381SAndroid Build Coastguard Worker }
275*4a64e381SAndroid Build Coastguard Worker
AsyncGetPropertyMethodHandler(DBusRequest & aRequest)276*4a64e381SAndroid Build Coastguard Worker void DBusObject::AsyncGetPropertyMethodHandler(DBusRequest &aRequest)
277*4a64e381SAndroid Build Coastguard Worker {
278*4a64e381SAndroid Build Coastguard Worker DBusMessageIter iter;
279*4a64e381SAndroid Build Coastguard Worker std::string interfaceName;
280*4a64e381SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
281*4a64e381SAndroid Build Coastguard Worker std::string propertyName;
282*4a64e381SAndroid Build Coastguard Worker
283*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(dbus_message_iter_init(aRequest.GetMessage(), &iter), error = OT_ERROR_FAILED);
284*4a64e381SAndroid Build Coastguard Worker SuccessOrExit(error = OtbrErrorToOtError(DBusMessageExtract(&iter, interfaceName)));
285*4a64e381SAndroid Build Coastguard Worker SuccessOrExit(error = OtbrErrorToOtError(DBusMessageExtract(&iter, propertyName)));
286*4a64e381SAndroid Build Coastguard Worker
287*4a64e381SAndroid Build Coastguard Worker {
288*4a64e381SAndroid Build Coastguard Worker auto propertyIter = mAsyncGetPropertyHandlers.find(interfaceName);
289*4a64e381SAndroid Build Coastguard Worker
290*4a64e381SAndroid Build Coastguard Worker otbrLogDebug("AsyncGetProperty %s.%s", interfaceName.c_str(), propertyName.c_str());
291*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(propertyIter != mAsyncGetPropertyHandlers.end(), error = OT_ERROR_NOT_FOUND);
292*4a64e381SAndroid Build Coastguard Worker {
293*4a64e381SAndroid Build Coastguard Worker auto &interfaceHandlers = propertyIter->second;
294*4a64e381SAndroid Build Coastguard Worker auto interfaceIter = interfaceHandlers.find(propertyName);
295*4a64e381SAndroid Build Coastguard Worker
296*4a64e381SAndroid Build Coastguard Worker VerifyOrExit(interfaceIter != interfaceHandlers.end(), error = OT_ERROR_NOT_FOUND);
297*4a64e381SAndroid Build Coastguard Worker (interfaceIter->second)(aRequest);
298*4a64e381SAndroid Build Coastguard Worker }
299*4a64e381SAndroid Build Coastguard Worker }
300*4a64e381SAndroid Build Coastguard Worker
301*4a64e381SAndroid Build Coastguard Worker exit:
302*4a64e381SAndroid Build Coastguard Worker if (error != OT_ERROR_NONE)
303*4a64e381SAndroid Build Coastguard Worker {
304*4a64e381SAndroid Build Coastguard Worker otbrLogWarning("GetProperty %s.%s error:%s", interfaceName.c_str(), propertyName.c_str(),
305*4a64e381SAndroid Build Coastguard Worker ConvertToDBusErrorName(error));
306*4a64e381SAndroid Build Coastguard Worker aRequest.ReplyOtResult(error);
307*4a64e381SAndroid Build Coastguard Worker }
308*4a64e381SAndroid Build Coastguard Worker }
309*4a64e381SAndroid Build Coastguard Worker
~DBusObject(void)310*4a64e381SAndroid Build Coastguard Worker DBusObject::~DBusObject(void)
311*4a64e381SAndroid Build Coastguard Worker {
312*4a64e381SAndroid Build Coastguard Worker }
313*4a64e381SAndroid Build Coastguard Worker
NewSignalMessage(const std::string & aInterfaceName,const std::string & aSignalName)314*4a64e381SAndroid Build Coastguard Worker UniqueDBusMessage DBusObject::NewSignalMessage(const std::string &aInterfaceName, const std::string &aSignalName)
315*4a64e381SAndroid Build Coastguard Worker {
316*4a64e381SAndroid Build Coastguard Worker return UniqueDBusMessage(dbus_message_new_signal(mObjectPath.c_str(), aInterfaceName.c_str(), aSignalName.c_str()));
317*4a64e381SAndroid Build Coastguard Worker }
318*4a64e381SAndroid Build Coastguard Worker
Flush(void)319*4a64e381SAndroid Build Coastguard Worker void DBusObject::Flush(void)
320*4a64e381SAndroid Build Coastguard Worker {
321*4a64e381SAndroid Build Coastguard Worker dbus_connection_flush(mConnection);
322*4a64e381SAndroid Build Coastguard Worker }
323*4a64e381SAndroid Build Coastguard Worker
324*4a64e381SAndroid Build Coastguard Worker } // namespace DBus
325*4a64e381SAndroid Build Coastguard Worker } // namespace otbr
326