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 @@ ...@@ -8,6 +8,15 @@
#include "driver/uart.h" #include "driver/uart.h"
#include "hal/uart_ll.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 { typedef struct {
void *arg; void *arg;
} PdpContext; } PdpContext;
...@@ -416,26 +425,13 @@ void PPPClass::end(void) { ...@@ -416,26 +425,13 @@ void PPPClass::end(void) {
} }
bool PPPClass::sync() const { bool PPPClass::sync() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(false);
return 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; return esp_modem_sync(_dce) == ESP_OK;
} }
bool PPPClass::attached() const { bool PPPClass::attached() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(false);
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
int m = 0; int m = 0;
esp_err_t err = esp_modem_get_network_attachment_state(_dce, m); esp_err_t err = esp_modem_get_network_attachment_state(_dce, m);
...@@ -500,52 +496,31 @@ bool PPPClass::setPin(const char *pin) { ...@@ -500,52 +496,31 @@ bool PPPClass::setPin(const char *pin) {
} }
int PPPClass::RSSI() const { int PPPClass::RSSI() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(-1);
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
int rssi, ber; int rssi, ber;
esp_err_t err = esp_modem_get_signal_quality(_dce, rssi, ber); esp_err_t err = esp_modem_get_signal_quality(_dce, rssi, ber);
if (err != ESP_OK) { 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 -1;
} }
return rssi; return rssi;
} }
int PPPClass::BER() const { int PPPClass::BER() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(-1);
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
int rssi, ber; int rssi, ber;
esp_err_t err = esp_modem_get_signal_quality(_dce, rssi, ber); esp_err_t err = esp_modem_get_signal_quality(_dce, rssi, ber);
if (err != ESP_OK) { 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 -1;
} }
return ber; return ber;
} }
String PPPClass::IMSI() const { String PPPClass::IMSI() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(String());
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
char imsi[32]; char imsi[32];
esp_err_t err = esp_modem_get_imsi(_dce, (std::string &)imsi); esp_err_t err = esp_modem_get_imsi(_dce, (std::string &)imsi);
...@@ -558,14 +533,7 @@ String PPPClass::IMSI() const { ...@@ -558,14 +533,7 @@ String PPPClass::IMSI() const {
} }
String PPPClass::IMEI() const { String PPPClass::IMEI() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(String());
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
char imei[32]; char imei[32];
esp_err_t err = esp_modem_get_imei(_dce, (std::string &)imei); esp_err_t err = esp_modem_get_imei(_dce, (std::string &)imei);
...@@ -578,14 +546,7 @@ String PPPClass::IMEI() const { ...@@ -578,14 +546,7 @@ String PPPClass::IMEI() const {
} }
String PPPClass::moduleName() const { String PPPClass::moduleName() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(String());
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
char name[32]; char name[32];
esp_err_t err = esp_modem_get_module_name(_dce, (std::string &)name); esp_err_t err = esp_modem_get_module_name(_dce, (std::string &)name);
...@@ -598,14 +559,7 @@ String PPPClass::moduleName() const { ...@@ -598,14 +559,7 @@ String PPPClass::moduleName() const {
} }
String PPPClass::operatorName() const { String PPPClass::operatorName() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(String());
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
char oper[32]; char oper[32];
int act = 0; int act = 0;
...@@ -619,14 +573,7 @@ String PPPClass::operatorName() const { ...@@ -619,14 +573,7 @@ String PPPClass::operatorName() const {
} }
int PPPClass::networkMode() const { int PPPClass::networkMode() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(-1);
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
int m = 0; int m = 0;
esp_err_t err = esp_modem_get_network_system_mode(_dce, m); esp_err_t err = esp_modem_get_network_system_mode(_dce, m);
...@@ -638,14 +585,7 @@ int PPPClass::networkMode() const { ...@@ -638,14 +585,7 @@ int PPPClass::networkMode() const {
} }
int PPPClass::radioState() const { int PPPClass::radioState() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(-1);
return -1;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return -1;
}
int m = 0; int m = 0;
esp_err_t err = esp_modem_get_radio_state(_dce, m); esp_err_t err = esp_modem_get_radio_state(_dce, m);
...@@ -657,14 +597,7 @@ int PPPClass::radioState() const { ...@@ -657,14 +597,7 @@ int PPPClass::radioState() const {
} }
bool PPPClass::powerDown() { bool PPPClass::powerDown() {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(false);
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
esp_err_t err = esp_modem_power_down(_dce); esp_err_t err = esp_modem_power_down(_dce);
if (err != ESP_OK) { if (err != ESP_OK) {
...@@ -675,14 +608,7 @@ bool PPPClass::powerDown() { ...@@ -675,14 +608,7 @@ bool PPPClass::powerDown() {
} }
bool PPPClass::reset() { bool PPPClass::reset() {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(false);
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
esp_err_t err = esp_modem_reset(_dce); esp_err_t err = esp_modem_reset(_dce);
if (err != ESP_OK) { if (err != ESP_OK) {
...@@ -693,14 +619,7 @@ bool PPPClass::reset() { ...@@ -693,14 +619,7 @@ bool PPPClass::reset() {
} }
bool PPPClass::storeProfile() { bool PPPClass::storeProfile() {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(false);
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
esp_err_t err = esp_modem_store_profile(_dce); esp_err_t err = esp_modem_store_profile(_dce);
if (err != ESP_OK) { if (err != ESP_OK) {
...@@ -711,14 +630,7 @@ bool PPPClass::storeProfile() { ...@@ -711,14 +630,7 @@ bool PPPClass::storeProfile() {
} }
bool PPPClass::setBaudrate(int baudrate) { bool PPPClass::setBaudrate(int baudrate) {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(false);
return false;
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return false;
}
esp_err_t err = esp_modem_set_baud(_dce, baudrate); esp_err_t err = esp_modem_set_baud(_dce, baudrate);
if (err != ESP_OK) { if (err != ESP_OK) {
...@@ -737,15 +649,44 @@ bool PPPClass::setBaudrate(int baudrate) { ...@@ -737,15 +649,44 @@ bool PPPClass::setBaudrate(int baudrate) {
return true; return true;
} }
bool PPPClass::sms(const char *num, const char *message) { int PPPClass::batteryVoltage() const {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(-1);
return false;
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) { int PPPClass::batteryLevel() const {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND"); PPP_CMD_MODE_CHECK(-1);
return false;
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++) { for (int i = 0; i < strlen(num); i++) {
if (num[i] != '+' && num[i] != '#' && num[i] != '*' && (num[i] < 0x30 || num[i] > 0x39)) { 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) { ...@@ -775,14 +716,8 @@ bool PPPClass::sms(const char *num, const char *message) {
} }
String PPPClass::cmd(const char *at_command, int timeout) { String PPPClass::cmd(const char *at_command, int timeout) {
if (_dce == NULL) { PPP_CMD_MODE_CHECK(String());
return String();
}
if (_mode == ESP_MODEM_MODE_DATA) {
log_e("Wrong modem mode. Should be ESP_MODEM_MODE_COMMAND");
return String();
}
char out[128] = {0}; char out[128] = {0};
esp_err_t err = _esp_modem_at(_dce, at_command, out, timeout); esp_err_t err = _esp_modem_at(_dce, at_command, out, timeout);
if (err != ESP_OK) { if (err != ESP_OK) {
......
...@@ -49,6 +49,9 @@ public: ...@@ -49,6 +49,9 @@ public:
int radioState() const; // 0:minimal, 1:full int radioState() const; // 0:minimal, 1:full
bool attached() const; // true is attached to network bool attached() const; // true is attached to network
bool sync() const; // true if responds to 'AT' bool sync() const; // true if responds to 'AT'
int batteryVoltage() const;
int batteryLevel() const;
int batteryStatus() const;
// Switch the communication mode // Switch the communication mode
bool mode(esp_modem_dce_mode_t m); 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