| Directory: | src/ |
|---|---|
| File: | src/message_parser/sub_message_parser/parse_beacons.cpp |
| Date: | 2026-03-27 13:31:42 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 21 | 21 | 100.0% |
| Branches: | 21 | 36 | 58.3% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sub_message_parser.h" | ||
| 2 | |||
| 3 | |||
| 4 | /// @brief Parses beacon data from a raw beacon payload (value from tlv) | ||
| 5 | /// @param payload The raw beacon data value | ||
| 6 | /// returns ERROR_VALUE on error, on success - json string of the format: | ||
| 7 | /// {"beaconData":{"Tag":31,"Beacons":[{"id":24,"rssi":1},{"id":194,"rssi":3},{"id":203,"rssi":0}]}} | ||
| 8 | 3 | std::string sub_message_parser::parse_beacons(const std::vector<uint8_t>& payload) { | |
| 9 | 3 | size_t payload_size = payload.size(); | |
| 10 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (payload_size % 2 == 0) { |
| 11 |
2/4✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
2 | std::cerr << "Warning: Beacon data found with even byte count!!!." << std::endl; |
| 12 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | return ERROR_VALUE; |
| 13 | } | ||
| 14 | |||
| 15 | 1 | int current_index = 0; | |
| 16 | |||
| 17 | // Start constructing the JSON-like string manually | ||
| 18 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | std::ostringstream oss; |
| 19 |
3/6✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
|
1 | oss << "{\"beaconData\":{\"Tag\":" << static_cast<int>(payload[current_index]) << ",\"Beacons\":["; |
| 20 | |||
| 21 | 1 | current_index++; | |
| 22 | |||
| 23 | 1 | bool first_beacon = true; | |
| 24 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | while (current_index < payload_size) { |
| 25 | 2 | uint8_t beacon_id = payload[current_index++]; | |
| 26 | 2 | int8_t beacon_rssi = payload[current_index++]; | |
| 27 | |||
| 28 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!first_beacon) { |
| 29 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | oss << ","; |
| 30 | } | ||
| 31 | 2 | first_beacon = false; | |
| 32 | |||
| 33 | // Add each beacon as an object | ||
| 34 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | oss << "{\"id\":" << static_cast<int>(beacon_id) |
| 35 |
4/8✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
|
2 | << ",\"rssi\":" << static_cast<int>(beacon_rssi) << "}"; |
| 36 | } | ||
| 37 | |||
| 38 | // Close the JSON-like string | ||
| 39 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | oss << "]}}"; |
| 40 | |||
| 41 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | return oss.str(); |
| 42 | 1 | } | |
| 43 |