-
Notifications
You must be signed in to change notification settings - Fork 129
/
openbmc_dbus_rest.hpp
2616 lines (2424 loc) · 99.2 KB
/
openbmc_dbus_rest.hpp
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2018 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <tinyxml2.h>
#include <app.hpp>
#include <async_resp.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/container/flat_set.hpp>
#include <dbus_singleton.hpp>
#include <dbus_utility.hpp>
#include <sdbusplus/message/types.hpp>
#include <filesystem>
#include <fstream>
#include <regex>
#include <utility>
namespace crow
{
namespace openbmc_mapper
{
using GetSubTreeType = std::vector<
std::pair<std::string,
std::vector<std::pair<std::string, std::vector<std::string>>>>>;
const constexpr char* notFoundMsg = "404 Not Found";
const constexpr char* badReqMsg = "400 Bad Request";
const constexpr char* methodNotAllowedMsg = "405 Method Not Allowed";
const constexpr char* forbiddenMsg = "403 Forbidden";
const constexpr char* methodFailedMsg = "500 Method Call Failed";
const constexpr char* methodOutputFailedMsg = "500 Method Output Error";
const constexpr char* notFoundDesc =
"org.freedesktop.DBus.Error.FileNotFound: path or object not found";
const constexpr char* propNotFoundDesc =
"The specified property cannot be found";
const constexpr char* noJsonDesc = "No JSON object could be decoded";
const constexpr char* methodNotFoundDesc =
"The specified method cannot be found";
const constexpr char* methodNotAllowedDesc = "Method not allowed";
const constexpr char* forbiddenPropDesc =
"The specified property cannot be created";
const constexpr char* forbiddenResDesc =
"The specified resource cannot be created";
inline void setErrorResponse(crow::Response& res,
boost::beast::http::status result,
const std::string& desc,
const std::string_view msg)
{
res.result(result);
res.jsonValue = {{"data", {{"description", desc}}},
{"message", msg},
{"status", "error"}};
}
inline void
introspectObjects(const std::string& processName,
const std::string& objectPath,
const std::shared_ptr<bmcweb::AsyncResp>& transaction)
{
if (transaction->res.jsonValue.is_null())
{
transaction->res.jsonValue = {{"status", "ok"},
{"bus_name", processName},
{"objects", nlohmann::json::array()}};
}
crow::connections::systemBus->async_method_call(
[transaction, processName{std::string(processName)},
objectPath{std::string(objectPath)}](
const boost::system::error_code ec,
const std::string& introspectXml) {
if (ec)
{
BMCWEB_LOG_ERROR
<< "Introspect call failed with error: " << ec.message()
<< " on process: " << processName << " path: " << objectPath
<< "\n";
return;
}
transaction->res.jsonValue["objects"].push_back(
{{"path", objectPath}});
tinyxml2::XMLDocument doc;
doc.Parse(introspectXml.c_str());
tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
if (pRoot == nullptr)
{
BMCWEB_LOG_ERROR << "XML document failed to parse "
<< processName << " " << objectPath << "\n";
}
else
{
tinyxml2::XMLElement* node = pRoot->FirstChildElement("node");
while (node != nullptr)
{
const char* childPath = node->Attribute("name");
if (childPath != nullptr)
{
std::string newpath;
if (objectPath != "/")
{
newpath += objectPath;
}
newpath += std::string("/") + childPath;
// introspect the subobjects as well
introspectObjects(processName, newpath, transaction);
}
node = node->NextSiblingElement("node");
}
}
},
processName, objectPath, "org.freedesktop.DBus.Introspectable",
"Introspect");
}
inline void getPropertiesForEnumerate(
const std::string& objectPath, const std::string& service,
const std::string& interface,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " "
<< service << " " << interface;
crow::connections::systemBus->async_method_call(
[asyncResp, objectPath, service, interface](
const boost::system::error_code ec,
const std::vector<std::pair<
std::string, dbus::utility::DbusVariantType>>& propertiesList) {
if (ec)
{
BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface "
<< interface << " service " << service
<< " failed with code " << ec;
return;
}
nlohmann::json& dataJson = asyncResp->res.jsonValue["data"];
nlohmann::json& objectJson = dataJson[objectPath];
if (objectJson.is_null())
{
objectJson = nlohmann::json::object();
}
for (const auto& [name, value] : propertiesList)
{
nlohmann::json& propertyJson = objectJson[name];
std::visit(
[&propertyJson](auto&& val) {
if constexpr (std::is_same_v<
std::decay_t<decltype(val)>,
sdbusplus::message::unix_fd>)
{
propertyJson = val.fd;
}
else
{
propertyJson = val;
}
},
value);
}
},
service, objectPath, "org.freedesktop.DBus.Properties", "GetAll",
interface);
}
// Find any results that weren't picked up by ObjectManagers, to be
// called after all ObjectManagers are searched for and called.
inline void findRemainingObjectsForEnumerate(
const std::string& objectPath,
const std::shared_ptr<GetSubTreeType>& subtree,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate";
const nlohmann::json& dataJson = asyncResp->res.jsonValue["data"];
for (const auto& [path, interface_map] : *subtree)
{
if (path == objectPath)
{
// An enumerate does not return the target path's properties
continue;
}
if (dataJson.find(path) == dataJson.end())
{
for (const auto& [service, interfaces] : interface_map)
{
for (const auto& interface : interfaces)
{
if (!boost::starts_with(interface, "org.freedesktop.DBus"))
{
getPropertiesForEnumerate(path, service, interface,
asyncResp);
}
}
}
}
}
}
struct InProgressEnumerateData
{
InProgressEnumerateData(
const std::string& objectPathIn,
const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
objectPath(objectPathIn),
asyncResp(asyncRespIn)
{}
~InProgressEnumerateData()
{
findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
}
const std::string objectPath;
std::shared_ptr<GetSubTreeType> subtree;
std::shared_ptr<bmcweb::AsyncResp> asyncResp;
};
inline void getManagedObjectsForEnumerate(
const std::string& objectName, const std::string& objectManagerPath,
const std::string& connectionName,
const std::shared_ptr<InProgressEnumerateData>& transaction)
{
BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << objectName
<< " object_manager_path " << objectManagerPath
<< " connection_name " << connectionName;
crow::connections::systemBus->async_method_call(
[transaction, objectName,
connectionName](const boost::system::error_code ec,
const dbus::utility::ManagedObjectType& objects) {
if (ec)
{
BMCWEB_LOG_ERROR << "GetManagedObjects on path " << objectName
<< " on connection " << connectionName
<< " failed with code " << ec;
return;
}
nlohmann::json& dataJson =
transaction->asyncResp->res.jsonValue["data"];
for (const auto& objectPath : objects)
{
if (boost::starts_with(objectPath.first.str, objectName))
{
BMCWEB_LOG_DEBUG << "Reading object "
<< objectPath.first.str;
nlohmann::json& objectJson = dataJson[objectPath.first.str];
if (objectJson.is_null())
{
objectJson = nlohmann::json::object();
}
for (const auto& interface : objectPath.second)
{
for (const auto& property : interface.second)
{
nlohmann::json& propertyJson =
objectJson[property.first];
std::visit(
[&propertyJson](auto&& val) {
if constexpr (
std::is_same_v<
std::decay_t<decltype(val)>,
sdbusplus::message::unix_fd>)
{
propertyJson = val.fd;
}
else
{
propertyJson = val;
}
},
property.second);
}
}
}
for (const auto& interface : objectPath.second)
{
if (interface.first == "org.freedesktop.DBus.ObjectManager")
{
getManagedObjectsForEnumerate(
objectPath.first.str, objectPath.first.str,
connectionName, transaction);
}
}
}
},
connectionName, objectManagerPath, "org.freedesktop.DBus.ObjectManager",
"GetManagedObjects");
}
inline void findObjectManagerPathForEnumerate(
const std::string& objectName, const std::string& connectionName,
const std::shared_ptr<InProgressEnumerateData>& transaction)
{
BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << objectName
<< " on connection:" << connectionName;
crow::connections::systemBus->async_method_call(
[transaction, objectName, connectionName](
const boost::system::error_code ec,
const boost::container::flat_map<
std::string, boost::container::flat_map<
std::string, std::vector<std::string>>>&
objects) {
if (ec)
{
BMCWEB_LOG_ERROR << "GetAncestors on path " << objectName
<< " failed with code " << ec;
return;
}
for (const auto& pathGroup : objects)
{
for (const auto& connectionGroup : pathGroup.second)
{
if (connectionGroup.first == connectionName)
{
// Found the object manager path for this resource.
getManagedObjectsForEnumerate(
objectName, pathGroup.first, connectionName,
transaction);
return;
}
}
}
},
"xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetAncestors", objectName,
std::array<const char*, 1>{"org.freedesktop.DBus.ObjectManager"});
}
// Uses GetObject to add the object info about the target /enumerate path to
// the results of GetSubTree, as GetSubTree will not return info for the
// target path, and then continues on enumerating the rest of the tree.
inline void getObjectAndEnumerate(
const std::shared_ptr<InProgressEnumerateData>& transaction)
{
using GetObjectType =
std::vector<std::pair<std::string, std::vector<std::string>>>;
crow::connections::systemBus->async_method_call(
[transaction](const boost::system::error_code ec,
const GetObjectType& objects) {
if (ec)
{
BMCWEB_LOG_ERROR << "GetObject for path "
<< transaction->objectPath
<< " failed with code " << ec;
return;
}
BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
<< " has " << objects.size() << " entries";
if (!objects.empty())
{
transaction->subtree->emplace_back(transaction->objectPath,
objects);
}
// Map indicating connection name, and the path where the object
// manager exists
boost::container::flat_map<std::string, std::string> connections;
for (const auto& object : *(transaction->subtree))
{
for (const auto& connection : object.second)
{
std::string& objectManagerPath =
connections[connection.first];
for (const auto& interface : connection.second)
{
BMCWEB_LOG_DEBUG << connection.first
<< " has interface " << interface;
if (interface == "org.freedesktop.DBus.ObjectManager")
{
BMCWEB_LOG_DEBUG << "found object manager path "
<< object.first;
objectManagerPath = object.first;
}
}
}
}
BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
for (const auto& connection : connections)
{
// If we already know where the object manager is, we don't
// need to search for it, we can call directly in to
// getManagedObjects
if (!connection.second.empty())
{
getManagedObjectsForEnumerate(
transaction->objectPath, connection.second,
connection.first, transaction);
}
else
{
// otherwise we need to find the object manager path
// before we can continue
findObjectManagerPathForEnumerate(
transaction->objectPath, connection.first, transaction);
}
}
},
"xyz.openbmc_project.ObjectMapper",
"/xyz/openbmc_project/object_mapper",
"xyz.openbmc_project.ObjectMapper", "GetObject",
transaction->objectPath, std::array<const char*, 0>());
}
// Structure for storing data on an in progress action
struct InProgressActionData
{
InProgressActionData(crow::Response& resIn) : res(resIn)
{}
~InProgressActionData()
{
// Methods could have been called across different owners
// and interfaces, where some calls failed and some passed.
//
// The rules for this are:
// * if no method was called - error
// * if a method failed and none passed - error
// (converse: if at least one method passed - OK)
// * for the method output:
// * if output processing didn't fail, return the data
// Only deal with method returns if nothing failed earlier
if (res.result() == boost::beast::http::status::ok)
{
if (!methodPassed)
{
if (!methodFailed)
{
setErrorResponse(res, boost::beast::http::status::not_found,
methodNotFoundDesc, notFoundMsg);
}
}
else
{
if (outputFailed)
{
setErrorResponse(
res, boost::beast::http::status::internal_server_error,
"Method output failure", methodOutputFailedMsg);
}
else
{
res.jsonValue = {{"status", "ok"},
{"message", "200 OK"},
{"data", methodResponse}};
}
}
}
res.end();
}
void setErrorStatus(const std::string& desc)
{
setErrorResponse(res, boost::beast::http::status::bad_request, desc,
badReqMsg);
}
crow::Response& res;
std::string path;
std::string methodName;
std::string interfaceName;
bool methodPassed = false;
bool methodFailed = false;
bool outputFailed = false;
bool convertedToArray = false;
nlohmann::json methodResponse;
nlohmann::json arguments;
};
inline std::vector<std::string> dbusArgSplit(const std::string& string)
{
std::vector<std::string> ret;
if (string.empty())
{
return ret;
}
ret.emplace_back("");
int containerDepth = 0;
for (std::string::const_iterator character = string.begin();
character != string.end(); character++)
{
ret.back() += *character;
switch (*character)
{
case ('a'):
break;
case ('('):
case ('{'):
containerDepth++;
break;
case ('}'):
case (')'):
containerDepth--;
if (containerDepth == 0)
{
if (character + 1 != string.end())
{
ret.emplace_back("");
}
}
break;
default:
if (containerDepth == 0)
{
if (character + 1 != string.end())
{
ret.emplace_back("");
}
}
break;
}
}
return ret;
}
inline int convertJsonToDbus(sd_bus_message* m, const std::string& argType,
const nlohmann::json& inputJson)
{
int r = 0;
BMCWEB_LOG_DEBUG << "Converting "
<< inputJson.dump(2, ' ', true,
nlohmann::json::error_handler_t::replace)
<< " to type: " << argType;
const std::vector<std::string> argTypes = dbusArgSplit(argType);
// Assume a single object for now.
const nlohmann::json* j = &inputJson;
nlohmann::json::const_iterator jIt = inputJson.begin();
for (const std::string& argCode : argTypes)
{
// If we are decoding multiple objects, grab the pointer to the
// iterator, and increment it for the next loop
if (argTypes.size() > 1)
{
if (jIt == inputJson.end())
{
return -2;
}
j = &*jIt;
jIt++;
}
const int64_t* intValue = j->get_ptr<const int64_t*>();
const std::string* stringValue = j->get_ptr<const std::string*>();
const double* doubleValue = j->get_ptr<const double*>();
const bool* b = j->get_ptr<const bool*>();
int64_t v = 0;
double d = 0.0;
// Do some basic type conversions that make sense. uint can be
// converted to int. int and uint can be converted to double
if (intValue == nullptr)
{
const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
if (uintValue != nullptr)
{
v = static_cast<int64_t>(*uintValue);
intValue = &v;
}
}
if (doubleValue == nullptr)
{
const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
if (uintValue != nullptr)
{
d = static_cast<double>(*uintValue);
doubleValue = &d;
}
}
if (doubleValue == nullptr)
{
if (intValue != nullptr)
{
d = static_cast<double>(*intValue);
doubleValue = &d;
}
}
if (argCode == "s")
{
if (stringValue == nullptr)
{
return -1;
}
r = sd_bus_message_append_basic(
m, argCode[0], static_cast<const void*>(stringValue->data()));
if (r < 0)
{
return r;
}
}
else if (argCode == "i")
{
if (intValue == nullptr)
{
return -1;
}
if ((*intValue < std::numeric_limits<int32_t>::lowest()) ||
(*intValue > std::numeric_limits<int32_t>::max()))
{
return -ERANGE;
}
int32_t i = static_cast<int32_t>(*intValue);
r = sd_bus_message_append_basic(m, argCode[0], &i);
if (r < 0)
{
return r;
}
}
else if (argCode == "b")
{
// lots of ways bool could be represented here. Try them all
int boolInt = false;
if (intValue != nullptr)
{
if (*intValue == 1)
{
boolInt = true;
}
else if (*intValue == 0)
{
boolInt = false;
}
else
{
return -ERANGE;
}
}
else if (b != nullptr)
{
boolInt = *b ? 1 : 0;
}
else if (stringValue != nullptr)
{
boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
}
else
{
return -1;
}
r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
if (r < 0)
{
return r;
}
}
else if (argCode == "n")
{
if (intValue == nullptr)
{
return -1;
}
if ((*intValue < std::numeric_limits<int16_t>::lowest()) ||
(*intValue > std::numeric_limits<int16_t>::max()))
{
return -ERANGE;
}
int16_t n = static_cast<int16_t>(*intValue);
r = sd_bus_message_append_basic(m, argCode[0], &n);
if (r < 0)
{
return r;
}
}
else if (argCode == "x")
{
if (intValue == nullptr)
{
return -1;
}
r = sd_bus_message_append_basic(m, argCode[0], intValue);
if (r < 0)
{
return r;
}
}
else if (argCode == "y")
{
const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
if (uintValue == nullptr)
{
return -1;
}
if (*uintValue > std::numeric_limits<uint8_t>::max())
{
return -ERANGE;
}
uint8_t y = static_cast<uint8_t>(*uintValue);
r = sd_bus_message_append_basic(m, argCode[0], &y);
}
else if (argCode == "q")
{
const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
if (uintValue == nullptr)
{
return -1;
}
if (*uintValue > std::numeric_limits<uint16_t>::max())
{
return -ERANGE;
}
uint16_t q = static_cast<uint16_t>(*uintValue);
r = sd_bus_message_append_basic(m, argCode[0], &q);
}
else if (argCode == "u")
{
const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
if (uintValue == nullptr)
{
return -1;
}
if (*uintValue > std::numeric_limits<uint32_t>::max())
{
return -ERANGE;
}
uint32_t u = static_cast<uint32_t>(*uintValue);
r = sd_bus_message_append_basic(m, argCode[0], &u);
}
else if (argCode == "t")
{
const uint64_t* uintValue = j->get_ptr<const uint64_t*>();
if (uintValue == nullptr)
{
return -1;
}
r = sd_bus_message_append_basic(m, argCode[0], uintValue);
}
else if (argCode == "d")
{
if (doubleValue == nullptr)
{
return -1;
}
if ((*doubleValue < std::numeric_limits<double>::lowest()) ||
(*doubleValue > std::numeric_limits<double>::max()))
{
return -ERANGE;
}
sd_bus_message_append_basic(m, argCode[0], doubleValue);
}
else if (boost::starts_with(argCode, "a"))
{
std::string containedType = argCode.substr(1);
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
containedType.c_str());
if (r < 0)
{
return r;
}
for (const auto& it : *j)
{
r = convertJsonToDbus(m, containedType, it);
if (r < 0)
{
return r;
}
}
sd_bus_message_close_container(m);
}
else if (boost::starts_with(argCode, "v"))
{
std::string containedType = argCode.substr(1);
BMCWEB_LOG_DEBUG << "variant type: " << argCode
<< " appending variant of type: " << containedType;
r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
containedType.c_str());
if (r < 0)
{
return r;
}
r = convertJsonToDbus(m, containedType, inputJson);
if (r < 0)
{
return r;
}
r = sd_bus_message_close_container(m);
if (r < 0)
{
return r;
}
}
else if (boost::starts_with(argCode, "(") &&
boost::ends_with(argCode, ")"))
{
std::string containedType = argCode.substr(1, argCode.size() - 1);
r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
containedType.c_str());
if (r < 0)
{
return r;
}
nlohmann::json::const_iterator it = j->begin();
for (const std::string& argCode2 : dbusArgSplit(argType))
{
if (it == j->end())
{
return -1;
}
r = convertJsonToDbus(m, argCode2, *it);
if (r < 0)
{
return r;
}
it++;
}
r = sd_bus_message_close_container(m);
}
else if (boost::starts_with(argCode, "{") &&
boost::ends_with(argCode, "}"))
{
std::string containedType = argCode.substr(1, argCode.size() - 1);
r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
containedType.c_str());
if (r < 0)
{
return r;
}
std::vector<std::string> codes = dbusArgSplit(containedType);
if (codes.size() != 2)
{
return -1;
}
const std::string& keyType = codes[0];
const std::string& valueType = codes[1];
for (const auto& it : j->items())
{
r = convertJsonToDbus(m, keyType, it.key());
if (r < 0)
{
return r;
}
r = convertJsonToDbus(m, valueType, it.value());
if (r < 0)
{
return r;
}
}
r = sd_bus_message_close_container(m);
}
else
{
return -2;
}
if (r < 0)
{
return r;
}
if (argTypes.size() > 1)
{
jIt++;
}
}
return r;
}
template <typename T>
int readMessageItem(const std::string& typeCode, sdbusplus::message::message& m,
nlohmann::json& data)
{
T value;
int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value);
if (r < 0)
{
BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode
<< " failed!";
return r;
}
data = value;
return 0;
}
int convertDBusToJSON(const std::string& returnType,
sdbusplus::message::message& m, nlohmann::json& response);
inline int readDictEntryFromMessage(const std::string& typeCode,
sdbusplus::message::message& m,
nlohmann::json& object)
{
std::vector<std::string> types = dbusArgSplit(typeCode);
if (types.size() != 2)
{
BMCWEB_LOG_ERROR << "wrong number contained types in dictionary: "
<< types.size();
return -1;
}
int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_DICT_ENTRY,
typeCode.c_str());
if (r < 0)
{
BMCWEB_LOG_ERROR << "sd_bus_message_enter_container with rc " << r;
return r;
}
nlohmann::json key;
r = convertDBusToJSON(types[0], m, key);
if (r < 0)
{
return r;
}
const std::string* keyPtr = key.get_ptr<const std::string*>();
if (keyPtr == nullptr)
{
// json doesn't support non-string keys. If we hit this condition,
// convert the result to a string so we can proceed
key = key.dump(2, ' ', true, nlohmann::json::error_handler_t::replace);
keyPtr = key.get_ptr<const std::string*>();
// in theory this can't fail now, but lets be paranoid about it
// anyway
if (keyPtr == nullptr)
{
return -1;
}
}
nlohmann::json& value = object[*keyPtr];
r = convertDBusToJSON(types[1], m, value);
if (r < 0)
{
return r;
}
r = sd_bus_message_exit_container(m.get());
if (r < 0)
{
BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
return r;
}
return 0;
}
inline int readArrayFromMessage(const std::string& typeCode,
sdbusplus::message::message& m,
nlohmann::json& data)
{
if (typeCode.size() < 2)
{
BMCWEB_LOG_ERROR << "Type code " << typeCode
<< " too small for an array";
return -1;
}
std::string containedType = typeCode.substr(1);
int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY,
containedType.c_str());
if (r < 0)
{
BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
<< r;
return r;
}
bool dict = boost::starts_with(containedType, "{") &&
boost::ends_with(containedType, "}");
if (dict)
{
// Remove the { }