ESPHome 2026.11.0
Loading...
Searching...
No Matches
at_reply.cpp
Go to the documentation of this file.
1#include "at_reply.h"
2
3#include <cctype>
4#include <cstdlib>
5#include <cstring>
6#include <strings.h>
7
8namespace esphome::modem {
9
10bool collect_reply(char *buf, size_t size, size_t &len, const char *data, size_t data_len, bool whole) {
11 const size_t capacity = size - 1;
12 if (whole) {
13 len = 0;
14 }
15 bool dropped = false;
16 if (data_len > capacity) {
17 memcpy(buf, data + data_len - capacity, capacity);
18 len = capacity;
19 dropped = true;
20 } else {
21 if (len + data_len > capacity) {
22 const size_t drop = len + data_len - capacity;
23 memmove(buf, buf + drop, len - drop);
24 len -= drop;
25 dropped = true;
26 }
27 memcpy(buf + len, data, data_len);
28 len += data_len;
29 }
30 buf[len] = '\0';
31 return dropped;
32}
33
34ReplyEnd reply_end(const char *data, size_t len, bool skip_first) {
35 const char *end = data + len;
36 const char *line = data;
37 if (skip_first) {
38 const auto *newline = static_cast<const char *>(memchr(data, '\n', len));
39 line = newline != nullptr ? newline + 1 : end;
40 }
41 while (line < end) {
42 const auto *newline = static_cast<const char *>(memchr(line, '\n', end - line));
43 if (newline == nullptr) {
44 break; // the line has not ended yet
45 }
46 size_t size = newline - line;
47 if (size != 0 && line[size - 1] == '\r') {
48 size--;
49 }
50 if (size == 2 && memcmp(line, "OK", 2) == 0) {
52 }
53 // esp_modem drops a CMUX frame without a line end, so the ": <reason>" part may be missing
54 if ((size == 5 && memcmp(line, "ERROR", 5) == 0) || (size >= 10 && memcmp(line, "+CME ERROR", 10) == 0) ||
55 (size >= 10 && memcmp(line, "+CMS ERROR", 10) == 0)) {
57 }
58 line = newline + 1;
59 }
61}
62
63size_t flatten_reply(char *reply, size_t len) {
64 for (size_t i = 0; i < len; i++) {
65 if (reply[i] == '\r' || reply[i] == '\n') {
66 reply[i] = ' ';
67 }
68 }
69 while (len != 0 && reply[len - 1] == ' ') {
70 len--;
71 }
72 if (len >= 2 && memcmp(reply + len - 2, "OK", 2) == 0 && (len == 2 || reply[len - 3] == ' ')) {
73 len -= 2;
74 while (len != 0 && reply[len - 1] == ' ') {
75 len--;
76 }
77 }
78 size_t start = 0;
79 while (start < len && reply[start] == ' ') {
80 start++;
81 }
82 len -= start;
83 memmove(reply, reply + start, len);
84 reply[len] = '\0';
85 return len;
86}
87
88int registration_from_reply(const char *reply, const char *prefix) {
89 const size_t prefix_len = strlen(prefix);
90 for (const char *line = strstr(reply, prefix); line != nullptr; line = strstr(line + 1, prefix)) {
91 const char *start = line + prefix_len;
92 char *end = nullptr;
93 strtol(start, &end, 10);
94 if (end == start || *end != ',') {
95 continue;
96 }
97 start = end + 1;
98 const auto stat = static_cast<int32_t>(strtol(start, &end, 10));
99 if (end != start) {
100 return stat;
101 }
102 }
103 return REGISTRATION_UNKNOWN;
104}
105
106int merge_registration(int lte, int legacy) {
107 // A multimode modem can be registered or still searching on one network type while the other refuses it
108 if (is_registered(lte)) {
109 return lte;
110 }
111 if (is_registered(legacy)) {
112 return legacy;
113 }
114 // A definite answer from one query outweighs a timeout of the other
115 if (lte == REGISTRATION_SEARCHING || legacy == REGISTRATION_SEARCHING) {
116 return REGISTRATION_SEARCHING;
117 }
118 if (lte == REGISTRATION_DENIED || legacy == REGISTRATION_DENIED) {
119 return REGISTRATION_DENIED;
120 }
121 if (lte >= 0 || legacy >= 0) {
122 return lte >= 0 ? lte : legacy;
123 }
124 if (lte == REGISTRATION_NO_ANSWER || legacy == REGISTRATION_NO_ANSWER) {
125 return REGISTRATION_NO_ANSWER;
126 }
127 if (lte == REGISTRATION_UNSUPPORTED && legacy == REGISTRATION_UNSUPPORTED) {
128 return REGISTRATION_UNSUPPORTED;
129 }
130 return REGISTRATION_UNKNOWN;
131}
132
133bool reply_names_unsupported(const char *reply) {
134 static constexpr char PREFIX[] = "+CME ERROR:";
135 static constexpr char TEXT[] = "operation not supported";
136 const char *code = strstr(reply, PREFIX);
137 if (code == nullptr) {
138 return false;
139 }
140 code += sizeof(PREFIX) - 1;
141 while (*code == ' ') {
142 code++;
143 }
144 return strncasecmp(code, TEXT, sizeof(TEXT) - 1) == 0 || (code[0] == '4' && !isdigit(code[1]));
145}
146
147const char *sim_status(char *reply) {
148 static constexpr char PREFIX[] = "+CPIN:";
149 char *status = strstr(reply, PREFIX);
150 if (status == nullptr) {
151 return nullptr;
152 }
153 status += sizeof(PREFIX) - 1;
154 while (*status == ' ') {
155 status++;
156 }
157 // A URC in the same reply follows on its own line, which flatten_reply() turned into two spaces
158 char *end = strstr(status, " ");
159 if (end != nullptr) {
160 *end = '\0';
161 }
162 return status;
163}
164
165} // namespace esphome::modem
uint8_t status
Definition bl0942.h:8
bool is_registered(int registration)
Registered on a network, so the data attach can be requested.
Definition at_reply.h:34
int registration_from_reply(const char *reply, const char *prefix)
The <stat> after the first comma of the "<n>,<stat>" line starting with prefix (+CREG: and friends),...
Definition at_reply.cpp:88
ReplyEnd reply_end(const char *data, size_t len, bool skip_first)
How the reply ends.
Definition at_reply.cpp:34
size_t flatten_reply(char *reply, size_t len)
Puts a raw reply on one line for the log and drops the final OK; returns the new length.
Definition at_reply.cpp:63
bool reply_names_unsupported(const char *reply)
Whether an error reply says the command is not supported (+CME ERROR: 4 or its verbose text)
Definition at_reply.cpp:133
int merge_registration(int lte, int legacy)
The state that tells most from the AT+CEREG? (LTE) and AT+CREG? (2G and 3G) results.
Definition at_reply.cpp:106
const char * sim_status(char *reply)
The SIM status in a flattened AT+CPIN? reply, cut before any following line, or nullptr.
Definition at_reply.cpp:147
bool collect_reply(char *buf, size_t size, size_t &len, const char *data, size_t data_len, bool whole)
Add data to the reply in buf (size includes the terminator, len is the current length).
Definition at_reply.cpp:10
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
uint16_t uint16_t & capacity
Definition helpers.cpp:34
uint8_t end[39]
Definition sun_gtil2.cpp:17