Unverified Commit 0fa4aa63 authored by WebDust21's avatar WebDust21 Committed by GitHub

PPP: add "battery status" read stub functions (#10043)

* add "getBattery" functions + #define simplicity

replaced all function protections with a #define to simplify code functionality.  Also added "getBattery" functions to leverage "esp_modem_get_battery_status" call in ESP-MODEM.

* add "getBattery" function defs to PPP.h

* fix(ppp): uncomment error logging, change new function naming

* fix(ppp): rename new functions for naming conformity

* ci(pre-commit): Apply automatic fixes

---------
Co-authored-by: default avatarpre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
parent b2e67ca2
......@@ -8,6 +8,15 @@
#include "driver/uart.h"
#include "hal/uart_ll.h"
#define PPP_CMD_MODE_CHECK(x) \
if (_dce == NULL) { \
return x; \
} \
if (_mode == ESP_MODEM_MODE_DATA) { \
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND"); \
return x; \
}
typedef struct {
void *arg;
} PdpContext;
......@@ -416,26 +425,13 @@ void PPPClass::end(void) {
}
bool PPPClass::sync() const {
if (_dce == NULL) {
return false;
}
PPP_CMD_MODE_CHECK(false);
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
return esp_modem_sync(_dce) == ESP_OK;
}
bool PPPClass::attached() const {
if (_dce == NULL) {
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
PPP_CMD_MODE_CHECK(false);
int m = 0;
esp_err_t err = esp_modem_get_network_attachment_state(_dce, m);
......@@ -500,52 +496,31 @@ bool PPPClass::setPin(const char *pin) {
}
int PPPClass::RSSI() const {
if (_dce == NULL) {
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
PPP_CMD_MODE_CHECK(-1);
int rssi, ber;
esp_err_t err = esp_modem_get_signal_quality(_dce, rssi, ber);
if (err != ESP_OK) {
// log_e("esp_modem_get_signal_quality failed with %d %s", err, esp_err_to_name(err));
log_e("esp_modem_get_signal_quality failed with %d %s", err, esp_err_to_name(err));
return -1;
}
return rssi;
}
int PPPClass::BER() const {
if (_dce == NULL) {
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
PPP_CMD_MODE_CHECK(-1);
int rssi, ber;
esp_err_t err = esp_modem_get_signal_quality(_dce, rssi, ber);
if (err != ESP_OK) {
// log_e("esp_modem_get_signal_quality failed with %d %s", err, esp_err_to_name(err));
log_e("esp_modem_get_signal_quality failed with %d %s", err, esp_err_to_name(err));
return -1;
}
return ber;
}
String PPPClass::IMSI() const {
if (_dce == NULL) {
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
PPP_CMD_MODE_CHECK(String());
char imsi[32];
esp_err_t err = esp_modem_get_imsi(_dce, (std::string &)imsi);
......@@ -558,14 +533,7 @@ String PPPClass::IMSI() const {
}
String PPPClass::IMEI() const {
if (_dce == NULL) {
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
PPP_CMD_MODE_CHECK(String());
char imei[32];
esp_err_t err = esp_modem_get_imei(_dce, (std::string &)imei);
......@@ -578,14 +546,7 @@ String PPPClass::IMEI() const {
}
String PPPClass::moduleName() const {
if (_dce == NULL) {
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
PPP_CMD_MODE_CHECK(String());
char name[32];
esp_err_t err = esp_modem_get_module_name(_dce, (std::string &)name);
......@@ -598,14 +559,7 @@ String PPPClass::moduleName() const {
}
String PPPClass::operatorName() const {
if (_dce == NULL) {
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
PPP_CMD_MODE_CHECK(String());
char oper[32];
int act = 0;
......@@ -619,14 +573,7 @@ String PPPClass::operatorName() const {
}
int PPPClass::networkMode() const {
if (_dce == NULL) {
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
PPP_CMD_MODE_CHECK(-1);
int m = 0;
esp_err_t err = esp_modem_get_network_system_mode(_dce, m);
......@@ -638,14 +585,7 @@ int PPPClass::networkMode() const {
}
int PPPClass::radioState() const {
if (_dce == NULL) {
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
PPP_CMD_MODE_CHECK(-1);
int m = 0;
esp_err_t err = esp_modem_get_radio_state(_dce, m);
......@@ -657,14 +597,7 @@ int PPPClass::radioState() const {
}
bool PPPClass::powerDown() {
if (_dce == NULL) {
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
PPP_CMD_MODE_CHECK(false);
esp_err_t err = esp_modem_power_down(_dce);
if (err != ESP_OK) {
......@@ -675,14 +608,7 @@ bool PPPClass::powerDown() {
}
bool PPPClass::reset() {
if (_dce == NULL) {
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
PPP_CMD_MODE_CHECK(false);
esp_err_t err = esp_modem_reset(_dce);
if (err != ESP_OK) {
......@@ -693,14 +619,7 @@ bool PPPClass::reset() {
}
bool PPPClass::storeProfile() {
if (_dce == NULL) {
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
PPP_CMD_MODE_CHECK(false);
esp_err_t err = esp_modem_store_profile(_dce);
if (err != ESP_OK) {
......@@ -711,14 +630,7 @@ bool PPPClass::storeProfile() {
}
bool PPPClass::setBaudrate(int baudrate) {
if (_dce == NULL) {
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
PPP_CMD_MODE_CHECK(false);
esp_err_t err = esp_modem_set_baud(_dce, baudrate);
if (err != ESP_OK) {
......@@ -737,15 +649,44 @@ bool PPPClass::setBaudrate(int baudrate) {
return true;
}
bool PPPClass::sms(const char *num, const char *message) {
if (_dce == NULL) {
return false;
int PPPClass::batteryVoltage() const {
PPP_CMD_MODE_CHECK(-1);
int volt, bcs, bcl;
esp_err_t err = esp_modem_get_battery_status(_dce, volt, bcs, bcl);
if (err != ESP_OK) {
log_e("esp_modem_get_battery_status failed with %d %s", err, esp_err_to_name(err));
return -1;
}
return volt;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
int PPPClass::batteryLevel() const {
PPP_CMD_MODE_CHECK(-1);
int volt, bcs, bcl;
esp_err_t err = esp_modem_get_battery_status(_dce, volt, bcs, bcl);
if (err != ESP_OK) {
log_e("esp_modem_get_battery_status failed with %d %s", err, esp_err_to_name(err));
return -1;
}
return bcl;
}
int PPPClass::batteryStatus() const {
PPP_CMD_MODE_CHECK(-1);
int volt, bcs, bcl;
esp_err_t err = esp_modem_get_battery_status(_dce, volt, bcs, bcl);
if (err != ESP_OK) {
log_e("esp_modem_get_battery_status failed with %d %s", err, esp_err_to_name(err));
return -1;
}
return bcs;
}
bool PPPClass::sms(const char *num, const char *message) {
PPP_CMD_MODE_CHECK(false);
for (int i = 0; i < strlen(num); i++) {
if (num[i] != '+' && num[i] != '#' && num[i] != '*' && (num[i] < 0x30 || num[i] > 0x39)) {
......@@ -775,14 +716,8 @@ bool PPPClass::sms(const char *num, const char *message) {
}
String PPPClass::cmd(const char *at_command, int timeout) {
if (_dce == NULL) {
return String();
}
PPP_CMD_MODE_CHECK(String());
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
char out[128] = {0};
esp_err_t err = _esp_modem_at(_dce, at_command, out, timeout);
if (err != ESP_OK) {
......
......@@ -49,6 +49,9 @@ public:
int radioState() const; // 0:minimal, 1:full
bool attached() const; // true is attached to network
bool sync() const; // true if responds to 'AT'
int batteryVoltage() const;
int batteryLevel() const;
int batteryStatus() const;
// Switch the communication mode
bool mode(esp_modem_dce_mode_t m);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment