2323#include " ReadoutCard/FirmwareChecker.h"
2424#include " ReadoutCard/MemoryMappedFile.h"
2525#include < boost/format.hpp>
26+ #include < boost/property_tree/ptree.hpp>
27+ #include < boost/property_tree/json_parser.hpp>
2628
2729using namespace AliceO2 ::roc::CommandLineUtilities;
2830using namespace AliceO2 ::roc;
29- using std::cout ;
30- using std::endl ;
31+ namespace pt = boost::property_tree ;
32+ namespace po = boost::program_options ;
3133
3234namespace
3335{
@@ -36,11 +38,16 @@ class ProgramListCards : public Program
3638 public:
3739 virtual Description getDescription ()
3840 {
39- return { " List Cards" , " Lists installed cards and some basic information about them" , " roc-list-cards" };
41+ return { " List Cards" , " Lists installed cards and some basic information about them" ,
42+ " roc-list-cards\n "
43+ " roc-list-cards --json" };
4044 }
4145
42- virtual void addOptions (boost::program_options::options_description&)
46+ virtual void addOptions (boost::program_options::options_description& options )
4347 {
48+ options.add_options ()(" json-out" ,
49+ po::bool_switch (&mOptions .jsonOut ),
50+ " Toggle json-formatted output" );
4451 }
4552
4653 virtual void run (const boost::program_options::variables_map&)
@@ -55,7 +62,12 @@ class ProgramListCards : public Program
5562 auto lineFat = std::string (header.length (), ' =' ) + ' \n ' ;
5663 auto lineThin = std::string (header.length (), ' -' ) + ' \n ' ;
5764
58- table << lineFat << header << lineThin;
65+ if (!mOptions .jsonOut ) {
66+ table << lineFat << header << lineThin;
67+ }
68+
69+ // initialize ptree
70+ pt::ptree root;
5971
6072 int i = 0 ;
6173 for (const auto & card : cardsFound) {
@@ -76,8 +88,8 @@ class ProgramListCards : public Program
7688 endpointNumber = bar0->getEndpointNumber ();
7789 } catch (const Exception& e) {
7890 if (isVerbose ()) {
79- cout << " Error parsing card information through BAR\n "
80- << boost::diagnostic_information (e) << ' \n ' ;
91+ std:: cout << " Error parsing card information through BAR\n "
92+ << boost::diagnostic_information (e) << ' \n ' ;
8193 }
8294 }
8395
@@ -89,16 +101,42 @@ class ProgramListCards : public Program
89101 serial = " n/a" ;
90102 }
91103
92- auto format = boost::format (formatRow) % i % CardType::toString (card.cardType ) % card.pciAddress .toString () % serial % endpointNumber % card.numaNode % card.pciId .vendor % card.pciId .device %
93- firmware % cardId;
104+ if (!mOptions .jsonOut ) {
105+ auto format = boost::format (formatRow) % i % CardType::toString (card.cardType ) % card.pciAddress .toString () % serial % endpointNumber % card.numaNode % card.pciId .vendor % card.pciId .device %
106+ firmware % cardId;
107+
108+ table << format;
109+ } else {
110+ pt::ptree cardNode;
111+
112+ // add kv pairs for this card
113+ cardNode.put (" type" , CardType::toString (card.cardType ));
114+ cardNode.put (" pciAddress" , card.pciAddress .toString ());
115+ cardNode.put (" serial" , serial);
116+ cardNode.put (" endpoint" , std::to_string (endpointNumber));
117+ cardNode.put (" numa" , std::to_string (card.numaNode ));
118+ cardNode.put (" firmware" , firmware);
94119
95- table << format;
120+ // add the card node to the tree
121+ root.add_child (std::to_string (i), cardNode);
122+ }
123+
124+ // Update sequence number
96125 i++;
97126 }
98127
99- table << lineFat;
100- cout << table.str ();
128+ if (!mOptions .jsonOut ) {
129+ table << lineFat;
130+ std::cout << table.str ();
131+ } else {
132+ pt::write_json (std::cout, root);
133+ }
101134 }
135+
136+ private:
137+ struct OptionsStruct {
138+ bool jsonOut = false ;
139+ } mOptions ;
102140};
103141} // Anonymous namespace
104142
0 commit comments