-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathskeletonbase.cpp
640 lines (473 loc) · 17.1 KB
/
skeletonbase.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include "simppl/skeletonbase.h"
#include "simppl/dispatcher.h"
#include "simppl/interface.h"
#include "simppl/string.h"
#include "simppl/vector.h"
#include "simppl/map.h"
#define SIMPPL_SKELETONBASE_CPP
#include "simppl/serverside.h"
#undef SIMPPL_SKELETONBASE_CPP
#include "simppl/detail/util.h"
#include <cxxabi.h>
#include <algorithm>
#include <iostream>
#include <memory>
namespace simppl
{
namespace dbus
{
namespace detail
{
using DemangledNamePtr = std::unique_ptr<char, FreeDeleter>;
} // namespace detail
/*static*/
DBusHandlerResult SkeletonBase::method_handler(DBusConnection* connection, DBusMessage* msg, void *user_data)
{
SkeletonBase* skel = (SkeletonBase*)user_data;
return skel->handle_request(msg);
}
SkeletonBase::SkeletonBase(std::size_t iface_count)
: ifaces_(iface_count, std::string())
, disp_(nullptr)
, method_heads_(iface_count, nullptr)
, property_heads_(iface_count, nullptr)
#if SIMPPL_HAVE_INTROSPECTION
, signal_heads_(iface_count, nullptr)
#endif
{}
SkeletonBase::~SkeletonBase()
{
if (disp_)
disp_->remove_server(*this);
}
void SkeletonBase::init(const char* mangled_iface_list, const char* role)
{
assert(role);
detail::DemangledNamePtr iface(abi::__cxa_demangle(mangled_iface_list, 0, 0, 0));
ifaces_ = detail::extract_interfaces(1, iface.get());
objectpath_ = detail::create_objectpath(ifaces_[0].c_str(), role);
busname_ = detail::create_busname(ifaces_[0].c_str(), role);
}
void SkeletonBase::init(size_type iface_count, const char* mangled_iface_list, std::string busname, std::string objectpath)
{
assert(busname.length() > 0);
assert(objectpath.length() > 0);
detail::DemangledNamePtr iface(abi::__cxa_demangle(mangled_iface_list, 0, 0, 0));
ifaces_ = detail::extract_interfaces(iface_count, iface.get());
busname_ = std::move(busname);
objectpath_ = std::move(objectpath);
}
void SkeletonBase::init(std::string busname, std::string objectpath)
{
assert(busname.length() > 0);
assert(objectpath.length() > 0);
busname_ = std::move(busname);
objectpath_ = std::move(objectpath);
}
Dispatcher& SkeletonBase::disp()
{
assert(disp_);
return *disp_;
}
ServerRequestDescriptor SkeletonBase::defer_response()
{
assert(current_request_);
// assert(current_request_.requestor_->hasResponse());
return std::move(current_request_);
}
void SkeletonBase::respond_with(detail::ServerResponseHolder response)
{
assert(current_request_);
//assert(response.responder_->allowedRequests_.find(current_request_.requestor_) != response.responder_->allowedRequests_.end());
message_ptr_t rmsg = make_message(dbus_message_new_method_return(current_request_.msg_));
if (response.f_)
{
DBusMessageIter iter;
dbus_message_iter_init_append(rmsg.get(), &iter);
response.f_(iter);
}
dbus_connection_send(disp_->conn_, rmsg.get(), nullptr);
current_request_.clear(); // only respond once!!!
}
void SkeletonBase::respond_on(ServerRequestDescriptor& req, detail::ServerResponseHolder response)
{
assert(req);
//assert(response.responder_->allowedRequests_.find(req.requestor_) != response.responder_->allowedRequests_.end());
message_ptr_t rmsg = make_message(dbus_message_new_method_return(req.msg_));
if (response.f_)
{
DBusMessageIter iter;
dbus_message_iter_init_append(rmsg.get(), &iter);
response.f_(iter);
}
dbus_connection_send(disp_->conn_, rmsg.get(), nullptr);
req.clear();
}
void SkeletonBase::respond_with(const Error& err)
{
assert(current_request_);
//assert(current_request_.requestor_->hasResponse());
message_ptr_t rmsg = current_request_.requestor_->_throw(*current_request_.msg_, err);
dbus_connection_send(disp_->conn_, rmsg.get(), nullptr);
current_request_.clear(); // only respond once!!!
}
void SkeletonBase::respond_on(ServerRequestDescriptor& req, const Error& err)
{
assert(req);
//assert(req.requestor_->hasResponse());
message_ptr_t rmsg = req.requestor_->_throw(*req.msg_, err);
dbus_connection_send(disp_->conn_, rmsg.get(), nullptr);
req.clear();
}
const ServerRequestDescriptor& SkeletonBase::current_request() const
{
assert(current_request_);
return current_request_;
}
DBusHandlerResult SkeletonBase::handle_objectmanager_request(DBusMessage* msg)
{
return handle_error(msg, DBUS_ERROR_UNKNOWN_INTERFACE);
}
DBusHandlerResult SkeletonBase::handle_request(DBusMessage* msg)
{
const char* method_name = dbus_message_get_member(msg);
const char* interface_name = dbus_message_get_interface(msg);
#if SIMPPL_HAVE_INTROSPECTION
if (!strcmp(interface_name, "org.freedesktop.DBus.Introspectable"))
return handle_introspect_request(msg);
#endif
#if SIMPPL_HAVE_OBJECTMANAGER
if (!strcmp(interface_name, "org.freedesktop.DBus.ObjectManager"))
return handle_objectmanager_request(msg);
#endif
if (!strcmp(interface_name, "org.freedesktop.DBus.Properties"))
{
if (!has_any_properties())
return handle_error(msg, DBUS_ERROR_UNKNOWN_INTERFACE);
return handle_property_request(msg);
}
int iface_id = find_interface(interface_name);
if (iface_id == invalid_iface_id)
return handle_error(msg, DBUS_ERROR_UNKNOWN_INTERFACE);
ServerMethodBase* method = find_method(iface_id, method_name);
if (!method)
return handle_error(msg, DBUS_ERROR_UNKNOWN_METHOD);
return handle_interface_request(msg, *method);
}
#if SIMPPL_HAVE_INTROSPECTION
DBusHandlerResult SkeletonBase::handle_introspect_request(DBusMessage* msg)
{
const char* method = dbus_message_get_member(msg);
if (strcmp(method, "Introspect") != 0)
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
std::ostringstream oss;
oss << "<?xml version=\"1.0\" ?>\n"
"<node name=\""<< objectpath() << "\">\n";
for (size_type i = 0; i < ifaces_.size(); ++i)
{
introspect_interface(oss, i);
}
// introspectable
oss << " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
" <method name=\"Introspect\">\n"
" <arg name=\"data\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n";
// properties
if (has_any_properties())
{
oss << " <interface name=\"org.freedesktop.DBus.Properties\">\n"
" <method name=\"Get\">\n"
" <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"value\" type=\"v\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"Set\">\n"
" <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"property_name\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"value\" type=\"v\" direction=\"in\"/>\n"
" </method>\n"
" <method name=\"GetAll\">\n"
" <arg name=\"interface_name\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"properties\" type=\"a{sv}\" direction=\"out\"/>\n"
" </method>\n"
" <signal name=\"PropertiesChanged\">\n"
" <arg name=\"interface_name\" type=\"s\"/>\n"
" <arg name=\"changed_properties\" type=\"a{sv}\"/>\n"
" <arg name=\"invalidated_properties\" type=\"as\"/>\n"
" </signal>\n"
" </interface>\n";
}
// recurse into any objects below this path
char** children = nullptr;
dbus_connection_list_registered(disp_->conn_, objectpath(), &children);
if (children)
{
char** child = children;
while(*child != nullptr)
{
oss << "<node name=\"" << *child << "\"/>\n";
++child;
}
dbus_free_string_array(children);
}
oss << "</node>\n";
DBusMessage* reply = dbus_message_new_method_return(msg);
DBusMessageIter iter;
dbus_message_iter_init_append(reply, &iter);
encode(iter, oss.str());
dbus_connection_send(disp_->conn_, reply, nullptr);
return DBUS_HANDLER_RESULT_HANDLED;
}
#endif // defined(SIMPPL_HAVE_INTROSPECTION)
DBusHandlerResult SkeletonBase::handle_property_getall_request(DBusMessage* msg, int iface_id)
{
message_ptr_t response = make_message(dbus_message_new_method_return(msg));
DBusMessageIter iter;
DBusMessageIter _iter;
DBusMessageIter __iter;
dbus_message_iter_init_append(response.get(), &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &_iter);
for (auto pp = property_heads_[iface_id]; pp; pp = pp->next_)
{
dbus_message_iter_open_container(&_iter, DBUS_TYPE_DICT_ENTRY, nullptr, &__iter);
encode(__iter, pp->name_);
pp->eval(&__iter);
dbus_message_iter_close_container(&_iter, &__iter);
}
dbus_message_iter_close_container(&iter, &_iter);
dbus_connection_send(disp_->conn_, response.get(), nullptr);
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult SkeletonBase::handle_property_request(DBusMessage* msg)
{
DBusMessageIter iter;
std::string interface_name;
int iface_id;
const char* method = dbus_message_get_member(msg);
try
{
dbus_message_iter_init(msg, &iter);
decode(iter, interface_name);
}
catch(DecoderError&)
{
return handle_error(msg, DBUS_ERROR_INVALID_ARGS);
}
iface_id = find_interface(interface_name);
if (iface_id == invalid_iface_id)
return handle_error(msg, DBUS_ERROR_UNKNOWN_INTERFACE);
if(!strcmp(method, "GetAll"))
{
return handle_property_getall_request(msg, iface_id);
}
else
{
std::string property_name;
try
{
decode(iter, property_name);
}
catch(DecoderError&)
{
return handle_error(msg, DBUS_ERROR_INVALID_ARGS);
}
ServerPropertyBase* property = find_property(iface_id, property_name);
if (!property)
return handle_error(msg, DBUS_ERROR_UNKNOWN_PROPERTY);
if (!strcmp(method, "Get"))
{
return handle_property_get_request(msg, *property);
}
else if(!strcmp(method, "Set"))
{
return handle_property_set_request(msg, *property, iter);
}
else
return handle_error(msg, DBUS_ERROR_UNKNOWN_METHOD);
}
}
DBusHandlerResult SkeletonBase::handle_property_get_request(DBusMessage* msg, ServerPropertyBase& property)
{
message_ptr_t response = make_message(dbus_message_new_method_return(msg));
DBusMessageIter iter;
dbus_message_iter_init_append(response.get(), &iter);
try
{
property.eval(&iter);
}
catch(const simppl::dbus::Error& err)
{
response = detail::ErrorFactory<Error>::reply(*msg, err);
}
catch(...)
{
simppl::dbus::Error e("simppl.dbus.UnhandledException");
response = detail::ErrorFactory<Error>::reply(*msg, e);
}
dbus_connection_send(disp_->conn_, response.get(), nullptr);
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult SkeletonBase::handle_property_set_request(DBusMessage* msg, ServerPropertyBase& property, DBusMessageIter& iter)
{
message_ptr_t response = make_message(nullptr);
try
{
property.evalSet(iter);
response = make_message(dbus_message_new_method_return(msg));
}
catch(const simppl::dbus::Error& err)
{
response = detail::ErrorFactory<Error>::reply(*msg, err);
}
catch(...)
{
simppl::dbus::Error e("simppl.dbus.UnhandledException");
response = detail::ErrorFactory<Error>::reply(*msg, e);
}
dbus_connection_send(disp_->conn_, response.get(), nullptr);
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult SkeletonBase::handle_interface_request(DBusMessage* msg, ServerMethodBase& method)
{
current_request_.set(&method, msg);
try
{
method.eval(msg);
}
catch(DecoderError&)
{
// don't use `handle_error` as we may need to clear the current request
simppl::dbus::Error err(DBUS_ERROR_INVALID_ARGS);
auto r = detail::ErrorFactory<Error>::reply(*msg, err);
dbus_connection_send(disp_->conn_, r.get(), nullptr);
}
catch(...)
{
// don't use `handle_error` as we may need to clear the current request
simppl::dbus::Error e("simppl.dbus.UnhandledException");
auto r = detail::ErrorFactory<Error>::reply(*msg, e);
dbus_connection_send(disp_->conn_, r.get(), nullptr);
}
// current_request_ is only valid if no response handler was called
if (current_request_)
current_request_.clear();
return DBUS_HANDLER_RESULT_HANDLED;
}
DBusHandlerResult SkeletonBase::handle_error(DBusMessage* msg, const char* dbus_error)
{
simppl::dbus::Error err(dbus_error);
auto r = detail::ErrorFactory<Error>::reply(*msg, err);
dbus_connection_send(disp_->conn_, r.get(), nullptr);
return DBUS_HANDLER_RESULT_HANDLED;
}
#if SIMPPL_HAVE_INTROSPECTION
void SkeletonBase::introspect_interface(std::ostream& os, size_type index) const
{
os << " <interface name=\""<< iface(index).c_str() << "\">\n";
for (auto pm = method_heads_[index]; pm; pm = pm->next_)
{
pm->introspect(os);
}
for (auto pp = this->property_heads_[index]; pp; pp = pp->next_)
{
pp->introspect(os);
}
for (auto ps = this->signal_heads_[index]; ps; ps = ps->next_)
{
ps->introspect(os);
}
os << " </interface>\n";
}
#endif //
bool SkeletonBase::has_any_properties() const
{
const auto first = property_heads_.begin();
const auto last = property_heads_.end();
return std::find_if(first, last, [](ServerPropertyBase* ptr) { return ptr != nullptr; }) != last;
}
int SkeletonBase::find_interface(const char* name) const
{
for (size_type i = 0; i < ifaces_.size(); ++i)
{
// use strcmp() because ifaces_[i] contains trailing zeros which causes
// operator== to fail.
if (!strcmp(ifaces_[i].c_str(), name))
{
// `ifaces_[0]` has the highest ID (e.g. N-1), so we need to
// "reverse" the order.
return static_cast<int>(ifaces_.size() - i - 1);
}
}
return invalid_iface_id;
}
ServerPropertyBase* SkeletonBase::find_property(int iface_id, const char* name) const
{
for (auto pp = property_heads_[iface_id]; pp; pp = pp->next_)
{
if (!strcmp(pp->name_, name))
{
return pp;
}
}
return nullptr;
}
ServerMethodBase* SkeletonBase::find_method(int iface_id, const char* name) const
{
for (auto pm = method_heads_[iface_id]; pm; pm = pm->next_)
{
if (!strcmp(pm->name_, name))
{
return pm;
}
}
return nullptr;
}
void SkeletonBase::send_signal(const char* signame, int iface_id, std::function<void(DBusMessageIter&)>&& f)
{
message_ptr_t msg = make_message(dbus_message_new_signal(objectpath(), iface(iface_id).c_str(), signame));
DBusMessageIter iter;
dbus_message_iter_init_append(msg.get(), &iter);
f(iter);
dbus_connection_send(disp_->conn_, msg.get(), nullptr);
}
void SkeletonBase::send_property_invalidate(const char* prop, int iface_id)
{
// dummy map for changed properties, is empty -> type doesn't matter
static std::map<int, int> m;
message_ptr_t msg = make_message(dbus_message_new_signal(objectpath(), "org.freedesktop.DBus.Properties", "PropertiesChanged"));
DBusMessageIter iter;
dbus_message_iter_init_append(msg.get(), &iter);
encode(iter, iface(iface_id));
encode(iter, m);
// the invalidated property as the only element in a vector
DBusMessageIter inv_iter;
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &inv_iter);
encode(inv_iter, prop);
dbus_message_iter_close_container(&iter, &inv_iter);
dbus_connection_send(disp_->conn_, msg.get(), nullptr);
}
void SkeletonBase::send_property_change(const char* prop, int iface_id, std::function<void(DBusMessageIter&)>&& f)
{
static std::vector<std::string> invalid;
message_ptr_t msg = make_message(dbus_message_new_signal(objectpath(), "org.freedesktop.DBus.Properties", "PropertiesChanged"));
DBusMessageIter iter;
dbus_message_iter_init_append(msg.get(), &iter);
encode(iter, iface(iface_id));
DBusMessageIter vec_iter;
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING
, &vec_iter);
DBusMessageIter item_iterator;
dbus_message_iter_open_container(&vec_iter, DBUS_TYPE_DICT_ENTRY, nullptr, &item_iterator);
encode(item_iterator, prop);
f(item_iterator);
// the dict entry
dbus_message_iter_close_container(&vec_iter, &item_iterator);
// the map
dbus_message_iter_close_container(&iter, &vec_iter);
encode(iter, invalid);
dbus_connection_send(disp_->conn_, msg.get(), nullptr);
}
} // namespace dbus
} // namespace simppl