GNU Radio's M17 Package
m17_log.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2026 M17 Project.
4 *
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8// Console output shared by the M17 blocks: one line per event, always in the form
9// HH:MM:SS.mmm [TAG] message
10// where TAG is M17_ENC / M17_DEC, or the block alias if one is set in GRC.
11// Numbers are always formatted with a decimal point, regardless of the system locale.
12
13#ifndef INCLUDED_M17_LOG_H
14#define INCLUDED_M17_LOG_H
15
16#include <cstdarg>
17#include <cstdint>
18#include <cstdio>
19#include <locale.h>
20#include <string>
21#include <time.h>
22
23#include "m17.h"
24
25namespace gr
26{
27 namespace m17
28 {
29 __attribute__((format(printf, 2, 3))) inline void m17_log(const std::string &tag, const char *fmt, ...)
30 {
31 static locale_t c_locale = newlocale(LC_NUMERIC_MASK, "C", (locale_t)0);
32 locale_t prev = uselocale(c_locale); // decimal point, not the system's decimal comma
33
34 char msg[2048];
35 va_list ap;
36 va_start(ap, fmt);
37 vsnprintf(msg, sizeof(msg), fmt, ap);
38 va_end(ap);
39
40 struct timespec ts;
41 clock_gettime(CLOCK_REALTIME, &ts);
42 struct tm t;
43 localtime_r(&ts.tv_sec, &t);
44 fprintf(stderr, "%02d:%02d:%02d.%03ld [%s] %s\n", t.tm_hour, t.tm_min, t.tm_sec,
45 ts.tv_nsec / 1000000L, tag.c_str(), msg);
46
47 uselocale(prev);
48 }
49
50 inline std::string m17_hex(const uint8_t *data, size_t len, size_t group = 0)
51 {
52 std::string s;
53 char b[3];
54 for (size_t i = 0; i < len; i++)
55 {
56 if (group && i && (i % group) == 0)
57 s += ' ';
58 snprintf(b, sizeof(b), "%02X", data[i]);
59 s += b;
60 }
61 return s;
62 }
63
64 // one block of a split UTF-8 text: drop partial characters at either end (marked with '~'),
65 // so the console never receives invalid UTF-8
66 inline std::string m17_utf8_chunk(const std::string &in)
67 {
68 size_t a = 0, b = in.size();
69 while (a < b && ((uint8_t)in[a] & 0xC0) == 0x80) // continuation bytes of a character started in the previous block
70 a++;
71 size_t i = b;
72 while (i > a && ((uint8_t)in[i - 1] & 0xC0) == 0x80)
73 i--;
74 if (i > a) // is the last character complete?
75 {
76 uint8_t lead = (uint8_t)in[i - 1];
77 size_t need = (lead >= 0xF0) ? 4 : (lead >= 0xE0) ? 3 : (lead >= 0xC0) ? 2 : 1;
78 if (b - (i - 1) < need)
79 b = i - 1;
80 }
81 return std::string(a ? "~" : "") + in.substr(a, b - a) + std::string(b < in.size() ? "~" : "");
82 }
83
84 // "voice stream, AES-128, CAN 0, signed" / "packet, CAN 0"
85 inline std::string m17_type_str(uint16_t type)
86 {
87 std::string s;
88 if (!(type & 1))
89 return "packet, CAN " + std::to_string((type >> 7) & 0xF);
90
91 static const char *data[4] = {"reserved", "data", "voice", "voice+data"};
92 s = std::string(data[(type >> 1) & 3]) + " stream, ";
93
94 uint8_t encr = (type >> 3) & 3, sub = (type >> 5) & 3;
95 if (encr == 0)
96 s += "no encryption";
97 else if (encr == 1)
98 s += (sub < 3) ? "scrambler " + std::to_string(8 * (sub + 1)) + "-bit" : "scrambler (reserved size)";
99 else if (encr == 2)
100 s += (sub < 3) ? "AES-" + std::to_string(128 + 64 * sub) : "AES (reserved size)";
101 else
102 s += "reserved encryption";
103
104 s += ", CAN " + std::to_string((type >> 7) & 0xF);
105 if ((type >> 11) & 1)
106 s += ", signed";
107 return s;
108 }
109
110 // readable META contents (spec 2.0.x), selected by the TYPE field
111 inline std::string m17_meta_str(const uint8_t meta[14], uint16_t type)
112 {
113 uint8_t encr = (type >> 3) & 3, sub = (type >> 5) & 3;
114 char b[160];
115
116 if (!(type & 1))
117 return "META " + m17_hex(meta, 14);
118 if (encr == 2)
119 return "AES nonce " + m17_hex(meta, 14);
120 if (encr != 0)
121 return "META " + m17_hex(meta, 14);
122
123 bool empty = true;
124 for (int i = 0; i < 14; i++)
125 empty &= (meta[i] == 0);
126 if (empty)
127 return "no META";
128
129 if (sub == 0) // Text Data
130 {
131 uint8_t used = meta[0] >> 4, which = meta[0] & 0xF;
132 int total = __builtin_popcount(used), idx = __builtin_ctz(which ? which : 1) + 1;
133 bool valid = (used == 0x1 || used == 0x3 || used == 0x7 || used == 0xF) && which && !(which & (which - 1)) && (which & used);
134 if (!valid)
135 {
136 snprintf(b, sizeof(b), "META text block (invalid control byte 0x%02X): ", meta[0]);
137 return b + m17_hex(&meta[1], 13);
138 }
139 std::string t((const char *)&meta[1], 13);
140 if (idx == total) // the last block is padded with spaces
141 {
142 size_t end = t.find_last_not_of(std::string(" \0", 2));
143 t = (end == std::string::npos) ? "" : t.substr(0, end + 1);
144 }
145 if (total == 1)
146 return "META text \"" + t + "\"";
147 snprintf(b, sizeof(b), "META text block %d/%d \"", idx, total);
148 return b + m17_utf8_chunk(t) + "\"";
149 }
150 if (sub == 1) // GNSS position
151 {
152 static const char *station[16] = {"fixed", "mobile", "handheld", "reserved", "reserved", "reserved",
153 "reserved", "reserved", "reserved", "reserved", "reserved", "reserved",
154 "reserved", "reserved", "reserved", "other"};
155 uint8_t valid = meta[1] >> 4;
156 int32_t lat = (int32_t)(((uint32_t)meta[3] << 24) | ((uint32_t)meta[4] << 16) | ((uint32_t)meta[5] << 8)) >> 8;
157 int32_t lon = (int32_t)(((uint32_t)meta[6] << 24) | ((uint32_t)meta[7] << 16) | ((uint32_t)meta[8] << 8)) >> 8;
158 std::string s = "META GNSS (" + std::string(station[meta[0] & 0xF]) + ")";
159 if (valid & 8)
160 {
161 snprintf(b, sizeof(b), " %.5f, %.5f", lat * 90.0 / 8388607.0, lon * 180.0 / 8388607.0);
162 s += b;
163 }
164 if (valid & 4)
165 {
166 snprintf(b, sizeof(b), ", %.1f m", (((uint16_t)meta[9] << 8) | meta[10]) / 2.0 - 500.0);
167 s += b;
168 }
169 if (valid & 2)
170 {
171 snprintf(b, sizeof(b), ", %.1f km/h, bearing %d deg",
172 (((uint16_t)meta[11] << 4) | (meta[12] >> 4)) / 2.0, ((meta[1] & 1) << 8) | meta[2]);
173 s += b;
174 }
175 return s;
176 }
177 if (sub == 2) // Extended Callsign Data
178 {
179 char cf1[10] = {0}, cf2[10] = {0};
180 decode_callsign_bytes(cf1, &meta[0]);
181 decode_callsign_bytes(cf2, &meta[6]);
182 std::string s = "META ECD " + std::string(cf1);
183 bool has_cf2 = false;
184 for (int i = 6; i < 12; i++)
185 has_cf2 |= (meta[i] != 0);
186 if (has_cf2)
187 s += ", " + std::string(cf2);
188 return s;
189 }
190 return "META " + m17_hex(meta, 14); // reserved
191 }
192
193 // "N0CALL -> @ALL, TYPE 0015 (voice stream, AES-128, CAN 0), AES nonce ..."
194 inline std::string m17_lsf_str(const lsf_t &lsf, bool callsigns)
195 {
196 uint16_t type = ((uint16_t)lsf.type[0] << 8) | lsf.type[1];
197 std::string src, dst;
198 if (callsigns)
199 {
200 char d[10] = {0}, s[10] = {0};
201 decode_callsign_bytes(d, lsf.dst);
202 decode_callsign_bytes(s, lsf.src);
203 src = s;
204 dst = d;
205 }
206 else
207 {
208 src = m17_hex(lsf.src, 6);
209 dst = m17_hex(lsf.dst, 6);
210 }
211 char t[8];
212 snprintf(t, sizeof(t), "%04X", type);
213 return src + " -> " + dst + ", TYPE " + t + " (" + m17_type_str(type) + "), " + m17_meta_str(lsf.meta, type);
214 }
215
216 } // namespace m17
217} // namespace gr
218
219#endif /* INCLUDED_M17_LOG_H */
Definition codec2_decoder.h:17
std::string m17_meta_str(const uint8_t meta[14], uint16_t type)
Definition m17_log.h:111
std::string m17_hex(const uint8_t *data, size_t len, size_t group=0)
Definition m17_log.h:50
std::string m17_type_str(uint16_t type)
Definition m17_log.h:85
__attribute__((format(printf, 2, 3))) inline void m17_log(const std
Definition m17_log.h:29
std::string m17_lsf_str(const lsf_t &lsf, bool callsigns)
Definition m17_log.h:194
std::string m17_utf8_chunk(const std::string &in)
Definition m17_log.h:66
Definition codec2_decoder.h:15