29 __attribute__((format(printf, 2, 3))) inline
void m17_log(const std::
string &tag, const
char *fmt, ...)
31 static locale_t c_locale = newlocale(LC_NUMERIC_MASK,
"C", (locale_t)0);
32 locale_t prev = uselocale(c_locale);
37 vsnprintf(msg,
sizeof(msg), fmt, ap);
41 clock_gettime(CLOCK_REALTIME, &ts);
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);
68 size_t a = 0, b = in.size();
69 while (a < b && ((uint8_t)in[a] & 0xC0) == 0x80)
72 while (i > a && ((uint8_t)in[i - 1] & 0xC0) == 0x80)
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)
81 return std::string(a ?
"~" :
"") + in.substr(a, b - a) + std::string(b < in.size() ?
"~" :
"");
89 return "packet, CAN " + std::to_string((type >> 7) & 0xF);
91 static const char *data[4] = {
"reserved",
"data",
"voice",
"voice+data"};
92 s = std::string(data[(type >> 1) & 3]) +
" stream, ";
94 uint8_t encr = (type >> 3) & 3, sub = (type >> 5) & 3;
98 s += (sub < 3) ?
"scrambler " + std::to_string(8 * (sub + 1)) +
"-bit" :
"scrambler (reserved size)";
100 s += (sub < 3) ?
"AES-" + std::to_string(128 + 64 * sub) :
"AES (reserved size)";
102 s +=
"reserved encryption";
104 s +=
", CAN " + std::to_string((type >> 7) & 0xF);
105 if ((type >> 11) & 1)
113 uint8_t encr = (type >> 3) & 3, sub = (type >> 5) & 3;
117 return "META " +
m17_hex(meta, 14);
119 return "AES nonce " +
m17_hex(meta, 14);
121 return "META " +
m17_hex(meta, 14);
124 for (
int i = 0; i < 14; i++)
125 empty &= (meta[i] == 0);
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);
136 snprintf(b,
sizeof(b),
"META text block (invalid control byte 0x%02X): ", meta[0]);
137 return b +
m17_hex(&meta[1], 13);
139 std::string t((
const char *)&meta[1], 13);
142 size_t end = t.find_last_not_of(std::string(
" \0", 2));
143 t = (end == std::string::npos) ?
"" : t.substr(0, end + 1);
146 return "META text \"" + t +
"\"";
147 snprintf(b,
sizeof(b),
"META text block %d/%d \"", idx, total);
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]) +
")";
161 snprintf(b,
sizeof(b),
" %.5f, %.5f", lat * 90.0 / 8388607.0, lon * 180.0 / 8388607.0);
166 snprintf(b,
sizeof(b),
", %.1f m", (((uint16_t)meta[9] << 8) | meta[10]) / 2.0 - 500.0);
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]);
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);
187 s +=
", " + std::string(cf2);
190 return "META " +
m17_hex(meta, 14);
196 uint16_t type = ((uint16_t)lsf.type[0] << 8) | lsf.type[1];
197 std::string src, dst;
200 char d[10] = {0}, s[10] = {0};
201 decode_callsign_bytes(d, lsf.dst);
202 decode_callsign_bytes(s, lsf.src);
212 snprintf(t,
sizeof(t),
"%04X", type);