DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
sigfox.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file sigfox.cpp
6 * @brief Sigfox modem AT-command codec - implementation.
7 *
8 * `AT$SF=<hex>` sends one uplink; the modem replies "OK" on success or "ERROR". The payload
9 * is hex-encoded (uppercase, two nibbles per byte) into the command.
10 */
11
13
14#if DETWS_ENABLE_SIGFOX
15
16namespace
17{
18char hex_nibble(uint8_t v)
19{
20 return (char)(v < 10 ? '0' + v : 'A' + (v - 10));
21}
22
23// Is the needle present in the first len bytes of haystack?
24bool contains(const char *hay, uint16_t len, const char *needle)
25{
26 uint16_t nlen = 0;
27 while (needle[nlen])
28 nlen++;
29 if (nlen == 0 || len < nlen)
30 return false;
31 for (uint16_t i = 0; i + nlen <= len; i++)
32 {
33 uint16_t j = 0;
34 while (j < nlen && hay[i + j] == needle[j])
35 j++;
36 if (j == nlen)
37 return true;
38 }
39 return false;
40}
41} // namespace
42
43uint16_t sigfox_build_uplink(const uint8_t *payload, uint8_t len, char *out, uint16_t cap)
44{
45 if (!out || !payload || len == 0 || len > DETWS_SIGFOX_MAX_PAYLOAD)
46 return 0;
47 // "AT$SF=" (6) + 2*len hex + "\r\n" (2) + NUL (1)
48 uint16_t need = (uint16_t)(6 + 2 * len + 2 + 1);
49 if (need > cap)
50 return 0;
51 const char *pfx = "AT$SF=";
52 uint16_t p = 0;
53 for (const char *s = pfx; *s; s++)
54 out[p++] = *s;
55 for (uint8_t i = 0; i < len; i++)
56 {
57 out[p++] = hex_nibble((uint8_t)(payload[i] >> 4));
58 out[p++] = hex_nibble((uint8_t)(payload[i] & 0x0F));
59 }
60 out[p++] = '\r';
61 out[p++] = '\n';
62 out[p] = '\0';
63 return p;
64}
65
66sigfox_result sigfox_parse_response(const char *buf, uint16_t len)
67{
68 if (!buf || len == 0)
69 return sigfox_result::SIGFOX_PENDING;
70 if (contains(buf, len, "ERROR"))
71 return sigfox_result::SIGFOX_ERROR;
72 if (contains(buf, len, "OK"))
73 return sigfox_result::SIGFOX_OK;
74 return sigfox_result::SIGFOX_PENDING;
75}
76
77#endif // DETWS_ENABLE_SIGFOX
#define DETWS_SIGFOX_MAX_PAYLOAD
Maximum Sigfox uplink payload (the network caps a message at 12 bytes).
Sigfox modem AT-command codec (DETWS_ENABLE_SIGFOX) - Wisol / Murata over UART.