Skip to content

Commit

Permalink
add example application to parse failed slots
Browse files Browse the repository at this point in the history
  • Loading branch information
marenz2569 committed Aug 21, 2024
1 parent 180e4ae commit 5ba88d5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
36 changes: 29 additions & 7 deletions include/nlohmann/slots.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@
#include "l2/slot.hpp"
#include <nlohmann/json.hpp>

static auto stob(const std::string& str) -> bool {
bool retval = false;

// convert integer (0 and 1) to bool
std::istringstream is(str);
is >> retval;

// convert boolean string (false and true) to bool
if (is.fail()) {
is.clear();
is >> std::boolalpha >> retval;
}

if (is.fail()) {
throw std::invalid_argument("Cannot convert: " + str + " to bool.");
}

return retval;
};

namespace nlohmann {
template <> struct adl_serializer<Slots> {
static void to_json(json& j, const Slots& slots) {
Expand All @@ -32,12 +52,13 @@ template <> struct adl_serializer<Slots> {
}

static void from_json(const json& j, Slots& slots) {
auto burst_type = j["burst_type"].template get<BurstType>();
auto slot_type = j["slot_type"].template get<SlotType>();
auto first_slot_logical_channel = j["first_slot_logical_channel"].template get<LogicalChannel>();
auto burst_type = BurstType(std::stoi(j["burst_type"].template get<std::string>()));
auto slot_type = SlotType(std::stoi(j["slot_type"].template get<std::string>()));
auto first_slot_logical_channel =
LogicalChannel(std::stoi(j["first_slot_logical_channel"].template get<std::string>()));
auto first_slot_data = j["first_slot_data"].template get<BitVector>();
auto first_slot_crc_ok = j["first_slot_crc_ok"].template get<bool>();
auto second_slot_present = j["second_slot_present"].template get<bool>();
auto first_slot_crc_ok = stob(j["first_slot_crc_ok"].template get<std::string>());
auto second_slot_present = stob(j["second_slot_present"].template get<std::string>());

auto first_slot = Slot(LogicalChannelDataAndCrc{
.channel = first_slot_logical_channel,
Expand All @@ -46,9 +67,10 @@ template <> struct adl_serializer<Slots> {
});

if (second_slot_present) {
auto second_slot_logical_channel = j["second_slot_logical_channel"].template get<LogicalChannel>();
auto second_slot_logical_channel =
LogicalChannel(std::stoi(j["second_slot_logical_channel"].template get<std::string>()));
auto second_slot_data = j["second_slot_data"].template get<BitVector>();
auto second_slot_crc_ok = j["second_slot_crc_ok"].template get<bool>();
auto second_slot_crc_ok = stob(j["second_slot_crc_ok"].template get<std::string>());

auto second_slot = Slot(LogicalChannelDataAndCrc{
.channel = second_slot_logical_channel,
Expand Down
7 changes: 6 additions & 1 deletion src/experiments/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
add_executable(packet-parser-example
src/experiments/packet_parser_example.cpp)

target_link_libraries(packet-parser-example tetra-decoder-library)
target_link_libraries(packet-parser-example tetra-decoder-library)

add_executable(slots-parser-example
src/experiments/slots_parser_example.cpp)

target_link_libraries(slots-parser-example tetra-decoder-library)
3 changes: 1 addition & 2 deletions src/experiments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ To download the selected data as CSV click on: `Query inspector` -> `Data` -> `D

Query: `SELECT * FROM tetra_failed_slots WHERE $__timeFilter(time) ORDER BY time desc`.

There is no example application provided showing how to convert the csv from grafana to C++ structures.
However, the principle is the same as in the `packet_parser_example`.
The application `slots_parser_example` show how to convert this data back into the correct C++ structures.
2 changes: 1 addition & 1 deletion src/experiments/packet_parser_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
auto main(int argc, char** argv) -> int {
std::string datafile;

cxxopts::Options options("parser-example", "Reads a csv file and extracts the contained packets.");
cxxopts::Options options("packet-parser-example", "Reads a csv file and extracts the contained packets.");

// clang-format off
options.add_options()
Expand Down

0 comments on commit 5ba88d5

Please sign in to comment.