ESPHome 2026.11.0
Loading...
Searching...
No Matches
at_reply.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
6// Parsers for raw AT replies; free of esp_modem so they build and run in host unit tests
7namespace esphome::modem {
8
9enum class ReplyEnd : uint8_t {
10 REPLY_END_NONE, // no complete final result line yet
11 REPLY_END_OK, // an OK line
12 REPLY_END_ERROR, // an ERROR, +CME ERROR: or +CMS ERROR: line
13};
14
15// AT+CEREG? and AT+CREG? <stat> values (3GPP TS 27.007)
16static constexpr int REGISTRATION_NOT_SEARCHING = 0;
17static constexpr int REGISTRATION_HOME = 1;
18static constexpr int REGISTRATION_SEARCHING = 2;
19static constexpr int REGISTRATION_DENIED = 3;
20static constexpr int REGISTRATION_REPORTED_UNKNOWN = 4;
21static constexpr int REGISTRATION_ROAMING = 5;
22static constexpr int REGISTRATION_SMS_ONLY_HOME = 6;
23static constexpr int REGISTRATION_SMS_ONLY_ROAMING = 7;
24static constexpr int REGISTRATION_EMERGENCY_ONLY = 8;
25static constexpr int REGISTRATION_CSFB_NOT_PREFERRED_HOME = 9;
26static constexpr int REGISTRATION_CSFB_NOT_PREFERRED_ROAMING = 10;
27// Results beyond the 3GPP values
28static constexpr int REGISTRATION_UNKNOWN = -1; // no readable answer
29static constexpr int REGISTRATION_UNSUPPORTED = -2; // the query is not supported
30static constexpr int REGISTRATION_NO_ANSWER = -3; // the query timed out
31
34inline bool is_registered(int registration) {
35 return registration == REGISTRATION_HOME || registration == REGISTRATION_ROAMING ||
36 (registration >= REGISTRATION_SMS_ONLY_HOME && registration <= REGISTRATION_SMS_ONLY_ROAMING) ||
37 (registration >= REGISTRATION_CSFB_NOT_PREFERRED_HOME &&
38 registration <= REGISTRATION_CSFB_NOT_PREFERRED_ROAMING);
39}
40
44bool collect_reply(char *buf, size_t size, size_t &len, const char *data, size_t data_len, bool whole);
45
49ReplyEnd reply_end(const char *data, size_t len, bool skip_first);
50
52size_t flatten_reply(char *reply, size_t len);
53
56int registration_from_reply(const char *reply, const char *prefix);
57
59int merge_registration(int lte, int legacy);
60
62bool reply_names_unsupported(const char *reply);
63
65const char *sim_status(char *reply);
66
67} // namespace esphome::modem
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