File: TestAdaptor.cpp

package info (click to toggle)
sdbus-cpp 2.2.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,556 kB
  • sloc: cpp: 12,626; ansic: 239; xml: 170; makefile: 39
file content (475 lines) | stat: -rw-r--r-- 13,632 bytes parent folder | download | duplicates (2)
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
/**
 * (C) 2016 - 2021 KISTLER INSTRUMENTE AG, Winterthur, Switzerland
 * (C) 2016 - 2024 Stanislav Angelovic <stanislav.angelovic@protonmail.com>
 *
 * @file TestAdaptor.cpp
 *
 * Created on: May 23, 2020
 * Project: sdbus-c++
 * Description: High-level D-Bus IPC C++ library based on sd-bus
 *
 * This file is part of sdbus-c++.
 *
 * sdbus-c++ is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * sdbus-c++ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with sdbus-c++. If not, see <http://www.gnu.org/licenses/>.
 */

#include "TestAdaptor.h"
#include <thread>
#include <chrono>
#include <atomic>

namespace sdbus { namespace test {

TestAdaptor::TestAdaptor(sdbus::IConnection& connection, sdbus::ObjectPath path) :
    AdaptorInterfaces(connection, std::move(path))
{
    registerAdaptor();
}

TestAdaptor::~TestAdaptor()
{
    unregisterAdaptor();
}

void TestAdaptor::noArgNoReturn()
{
}

int32_t TestAdaptor::getInt()
{
    return INT32_VALUE;
}

std::tuple<uint32_t, std::string> TestAdaptor::getTuple()
{
    return std::make_tuple(UINT32_VALUE, STRING_VALUE);
}

double TestAdaptor::multiply(const int64_t& a, const double& b)
{
    return a * b;
}

void TestAdaptor::multiplyWithNoReply(const int64_t& a, const double& b)
{
    m_multiplyResult = a * b;
    m_wasMultiplyCalled = true;
}

std::vector<int16_t> TestAdaptor::getInts16FromStruct(const sdbus::Struct<uint8_t, int16_t, double, std::string, std::vector<int16_t>>& x)
{
    std::vector<int16_t> res{x.get<1>()};
    auto y = std::get<std::vector<int16_t>>(x);
    res.insert(res.end(), y.begin(), y.end());
    return res;
}

sdbus::Variant TestAdaptor::processVariant(const std::variant<int32_t, double, std::string>& v)
{
    sdbus::Variant res{static_cast<int32_t>(std::get<double>(v))};
    return res;
}

std::map<int32_t, sdbus::Variant> TestAdaptor::getMapOfVariants(const std::vector<int32_t>& x, const sdbus::Struct<sdbus::Variant, sdbus::Variant>& y)
{
    std::map<int32_t, sdbus::Variant> res;
    for (auto item : x)
    {
        res[item] = (item <= 0) ? std::get<0>(y) : std::get<1>(y);
    }
    return res;
}

sdbus::Struct<std::string, sdbus::Struct<std::map<int32_t, int32_t>>> TestAdaptor::getStructInStruct()
{
    return sdbus::Struct{STRING_VALUE, sdbus::Struct{std::map<int32_t, int32_t>{{INT32_VALUE, INT32_VALUE}}}};
}

int32_t TestAdaptor::sumStructItems(const sdbus::Struct<uint8_t, uint16_t>& a, const sdbus::Struct<int32_t, int64_t>& b)
{
    int32_t res{0};
    res += std::get<0>(a) + std::get<1>(a);
    res += std::get<0>(b) + std::get<1>(b);
    return res;
}

uint32_t TestAdaptor::sumArrayItems(const std::vector<uint16_t>& a, const std::array<uint64_t, 3>& b)
{
    uint32_t res{0};
    for (auto x : a)
    {
        res += x;
    }
    for (auto x : b)
    {
        res += x;
    }
    return res;
}

uint32_t TestAdaptor::doOperation(const uint32_t& param)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(param));

    m_methodCallMsg = std::make_unique<const Message>(getObject().getCurrentlyProcessedMessage());
    m_methodName = m_methodCallMsg->getMemberName();

    return param;
}

std::map<int32_t, std::string> TestAdaptor::doOperationWithLargeData(const std::map<int32_t, std::string>& largeParam)
{
    m_methodCallMsg = std::make_unique<const Message>(getObject().getCurrentlyProcessedMessage());
    m_methodName = m_methodCallMsg->getMemberName();

    return largeParam;
}

void TestAdaptor::doOperationAsync(sdbus::Result<uint32_t>&& result, uint32_t param)
{
    m_methodCallMsg = std::make_unique<const Message>(getObject().getCurrentlyProcessedMessage());
    m_methodName = m_methodCallMsg->getMemberName();

    if (param == 0)
    {
        // Don't sleep and return the result from this thread
        result.returnResults(param);
    }
    else
    {
        // Process asynchronously in another thread and return the result from there
        std::thread([param, result = std::move(result)]()
        {
            std::this_thread::sleep_for(std::chrono::milliseconds(param));
            result.returnResults(param);
        }).detach();
    }
}

void TestAdaptor::doOperationAsyncWithLargeData(sdbus::Result<std::map<int32_t, std::string>>&& result, uint32_t param, const std::map<int32_t, std::string>& largeMap)
{
    m_methodCallMsg = std::make_unique<const Message>(getObject().getCurrentlyProcessedMessage());
    m_methodName = m_methodCallMsg->getMemberName();

    if (param == 0)
    {
        // Don't sleep and return the result from this thread
        result.returnResults(largeMap);
    }
    else
    {
        // Process asynchronously in another thread and return the result from there
        std::thread([param, largeMap, result = std::move(result)]()
                    {
                        std::this_thread::sleep_for(std::chrono::milliseconds(param));
                        result.returnResults(largeMap);
                    }).detach();
    }
}

sdbus::Signature TestAdaptor::getSignature()
{
    return SIGNATURE_VALUE;
}
sdbus::ObjectPath TestAdaptor::getObjPath()
{
    return OBJECT_PATH_VALUE;
}
sdbus::UnixFd TestAdaptor::getUnixFd()
{
    return sdbus::UnixFd{UNIX_FD_VALUE};
}

std::unordered_map<uint64_t, sdbus::Struct<std::map<uint8_t, std::vector<sdbus::Struct<sdbus::ObjectPath, bool, sdbus::Variant, std::map<int32_t, std::string>>>>, sdbus::Signature, std::string>> TestAdaptor::getComplex()
{
    return { // unordered_map
        {
            0,  // uint_64_t
            {   // struct
                {   // map
                    {
                        23,  // uint8_t
                        {   // vector
                            {   // struct
                                    sdbus::ObjectPath{"/object/path"}, // object path
                                    false,
                                    Variant{3.14},
                                    {   // map
                                        {0, "zero"}
                                    }
                            }
                        }
                    }
                },
                sdbus::Signature{"a{t(a{ya(obva{is})}gs)}"}, // signature
                std::string{}
            }
        }
    };
}

void TestAdaptor::throwError()
{
    m_wasThrowErrorCalled = true;
    throw sdbus::createError(1, "A test error occurred");
}

void TestAdaptor::throwErrorWithNoReply()
{
    TestAdaptor::throwError();
}

void TestAdaptor::doPrivilegedStuff()
{
    // Intentionally left blank
}

void TestAdaptor::emitTwoSimpleSignals()
{
    emitSimpleSignal();
    emitSignalWithMap({});
}

void TestAdaptor::sendLargeMessage(const std::map<int, std::string>& /*collection*/)
{
    //printf("Adaptor: got collection with %zu items", collection.size());
}

std::map<std::string, sdbus::Variant> TestAdaptor::returnDictionary(const std::map<std::string, sdbus::Variant>& dict)
{
    return dict;
}

std::string TestAdaptor::state()
{
    return m_state;
}

uint32_t TestAdaptor::action()
{
    return m_action;
}

void TestAdaptor::action(const uint32_t& value)
{
    m_action = value;
}

sdbus::Variant TestAdaptor::actionVariant()
{
    return m_actionVariant;
}

void TestAdaptor::actionVariant(const sdbus::Variant& value)
{
    m_actionVariant = value;
}

bool TestAdaptor::blocking()
{
    return m_blocking;
}

void TestAdaptor::blocking(const bool& value)
{
    m_propertySetMsg = std::make_unique<const Message>(getObject().getCurrentlyProcessedMessage());
    m_propertySetSender = m_propertySetMsg->getSender();

    m_blocking = value;
}

void TestAdaptor::emitSignalWithoutRegistration(const sdbus::Struct<std::string, sdbus::Struct<sdbus::Signature>>& s)
{
    getObject().emitSignal("signalWithoutRegistration").onInterface(sdbus::test::INTERFACE_NAME).withArguments(s);
}

std::string TestAdaptor::getExpectedXmlApiDescription() const
{
    return
R"delimiter(<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.freedesktop.DBus.Peer">
<method name="Ping"/>
<method name="GetMachineId">
<arg type="s" name="machine_uuid" direction="out"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect">
<arg name="data" type="s" direction="out"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Properties">
<method name="Get">
<arg name="interface" direction="in" type="s"/>
<arg name="property" direction="in" type="s"/>
<arg name="value" direction="out" type="v"/>
</method>
<method name="GetAll">
<arg name="interface" direction="in" type="s"/>
<arg name="properties" direction="out" type="a{sv}"/>
</method>
<method name="Set">
<arg name="interface" direction="in" type="s"/>
<arg name="property" direction="in" type="s"/>
<arg name="value" direction="in" type="v"/>
</method>
<signal name="PropertiesChanged">
<arg type="s" name="interface"/>
<arg type="a{sv}" name="changed_properties"/>
<arg type="as" name="invalidated_properties"/>
</signal>
</interface>
<interface name="org.freedesktop.DBus.ObjectManager">
<method name="GetManagedObjects">
<arg type="a{oa{sa{sv}}}" name="object_paths_interfaces_and_properties" direction="out"/>
</method>
<signal name="InterfacesAdded">
<arg type="o" name="object_path"/>
<arg type="a{sa{sv}}" name="interfaces_and_properties"/>
</signal>
<signal name="InterfacesRemoved">
<arg type="o" name="object_path"/>
<arg type="as" name="interfaces"/>
</signal>
</interface>
<interface name="org.sdbuscpp.integrationtests">
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
<method name="doOperation">
<arg type="u" direction="in"/>
<arg type="u" direction="out"/>
</method>
<method name="doOperationAsync">
<arg type="u" direction="in"/>
<arg type="u" direction="out"/>
</method>
<method name="doPrivilegedStuff">
<annotation name="org.freedesktop.systemd1.Privileged" value="true"/>
</method>
<method name="emitTwoSimpleSignals">
</method>
<method name="getComplex">
<arg type="a{t(a{ya(obva{is})}gs)}" direction="out"/>
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
</method>
<method name="getInt">)delimiter"
#if LIBSYSTEMD_VERSION>=242
R"delimiter(
<arg type="i" name="anInt" direction="out"/>)delimiter"
#else
R"delimiter(
<arg type="i" direction="out"/>)delimiter"
#endif
R"delimiter(
</method>
<method name="getInts16FromStruct">
<arg type="(yndsan)" direction="in"/>
<arg type="an" direction="out"/>
</method>
<method name="getMapOfVariants">)delimiter"
#if LIBSYSTEMD_VERSION>=242
R"delimiter(
<arg type="ai" name="x" direction="in"/>
<arg type="(vv)" name="y" direction="in"/>
<arg type="a{iv}" name="aMapOfVariants" direction="out"/>)delimiter"
#else
R"delimiter(
<arg type="ai" direction="in"/>
<arg type="(vv)" direction="in"/>
<arg type="a{iv}" direction="out"/>)delimiter"
#endif
R"delimiter(
</method>
<method name="getObjPath">
<arg type="o" direction="out"/>
</method>
<method name="getSignature">
<arg type="g" direction="out"/>
</method>
<method name="getStructInStruct">
<arg type="(s(a{ii}))" direction="out"/>
</method>
<method name="getTuple">
<arg type="u" direction="out"/>
<arg type="s" direction="out"/>
</method>
<method name="getUnixFd">
<arg type="h" direction="out"/>
</method>
<method name="multiply">)delimiter"
#if LIBSYSTEMD_VERSION>=242
R"delimiter(
<arg type="x" name="a" direction="in"/>
<arg type="d" name="b" direction="in"/>
<arg type="d" name="result" direction="out"/>)delimiter"
#else
R"delimiter(
<arg type="x" direction="in"/>
<arg type="d" direction="in"/>
<arg type="d" direction="out"/>)delimiter"
#endif
R"delimiter(
</method>
<method name="multiplyWithNoReply">
<arg type="x" direction="in"/>
<arg type="d" direction="in"/>
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
<annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
</method>
<method name="noArgNoReturn">
</method>
<method name="processVariant">
<arg type="v" direction="in"/>
<arg type="v" direction="out"/>
</method>
<method name="sumStructItems">
<arg type="(yq)" direction="in"/>
<arg type="(ix)" direction="in"/>
<arg type="i" direction="out"/>
</method>
<method name="sumArrayItems">
<arg type="aq" direction="in"/>
<arg type="at" direction="in"/>
<arg type="u" direction="out"/>
</method>
<method name="throwError">
</method>
<method name="throwErrorWithNoReply">
<annotation name="org.freedesktop.DBus.Method.NoReply" value="true"/>
</method>
<signal name="signalWithMap">
<arg type="a{is}"/>
</signal>
<signal name="signalWithVariant">
<arg type="v"/>
</signal>
<signal name="simpleSignal">
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
</signal>
<property name="action" type="u" access="readwrite">
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="invalidates"/>
</property>
<property name="blocking" type="b" access="readwrite">
</property>
<property name="state" type="s" access="read">
<annotation name="org.freedesktop.DBus.Deprecated" value="true"/>
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="const"/>
</property>
</interface>
</node>
)delimiter";
}

}}