23 memmove(buf, buf + drop,
len - drop);
27 memcpy(buf +
len, data, data_len);
35 const char *
end = data +
len;
36 const char *line = data;
38 const auto *newline =
static_cast<const char *
>(memchr(data,
'\n',
len));
39 line = newline !=
nullptr ? newline + 1 :
end;
42 const auto *newline =
static_cast<const char *
>(memchr(line,
'\n',
end - line));
43 if (newline ==
nullptr) {
46 size_t size = newline - line;
47 if (
size != 0 && line[
size - 1] ==
'\r') {
50 if (
size == 2 && memcmp(line,
"OK", 2) == 0) {
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)) {
64 for (
size_t i = 0; i <
len; i++) {
65 if (reply[i] ==
'\r' || reply[i] ==
'\n') {
69 while (
len != 0 && reply[
len - 1] ==
' ') {
72 if (
len >= 2 && memcmp(reply +
len - 2,
"OK", 2) == 0 && (
len == 2 || reply[
len - 3] ==
' ')) {
74 while (
len != 0 && reply[
len - 1] ==
' ') {
79 while (start <
len && reply[start] ==
' ') {
83 memmove(reply, reply + start,
len);
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;
93 strtol(start, &
end, 10);
94 if (
end == start || *
end !=
',') {
98 const auto stat =
static_cast<int32_t
>(strtol(start, &
end, 10));
103 return REGISTRATION_UNKNOWN;
115 if (lte == REGISTRATION_SEARCHING || legacy == REGISTRATION_SEARCHING) {
116 return REGISTRATION_SEARCHING;
118 if (lte == REGISTRATION_DENIED || legacy == REGISTRATION_DENIED) {
119 return REGISTRATION_DENIED;
121 if (lte >= 0 || legacy >= 0) {
122 return lte >= 0 ? lte : legacy;
124 if (lte == REGISTRATION_NO_ANSWER || legacy == REGISTRATION_NO_ANSWER) {
125 return REGISTRATION_NO_ANSWER;
127 if (lte == REGISTRATION_UNSUPPORTED && legacy == REGISTRATION_UNSUPPORTED) {
128 return REGISTRATION_UNSUPPORTED;
130 return REGISTRATION_UNKNOWN;
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) {
140 code +=
sizeof(PREFIX) - 1;
141 while (*code ==
' ') {
144 return strncasecmp(code, TEXT,
sizeof(TEXT) - 1) == 0 || (code[0] ==
'4' && !isdigit(code[1]));
148 static constexpr char PREFIX[] =
"+CPIN:";
149 char *
status = strstr(reply, PREFIX);
153 status +=
sizeof(PREFIX) - 1;
159 if (
end !=
nullptr) {
bool is_registered(int registration)
Registered on a network, so the data attach can be requested.
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),...
ReplyEnd reply_end(const char *data, size_t len, bool skip_first)
How the reply ends.
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.
bool reply_names_unsupported(const char *reply)
Whether an error reply says the command is not supported (+CME ERROR: 4 or its verbose text)
int merge_registration(int lte, int legacy)
The state that tells most from the AT+CEREG? (LTE) and AT+CREG? (2G and 3G) results.
const char * sim_status(char *reply)
The SIM status in a flattened AT+CPIN? reply, cut before any following line, or nullptr.
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).
uint16_t uint16_t & capacity