Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 62 additions & 129 deletions libraries/PPP/src/PPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
#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;
Expand Down Expand Up @@ -416,26 +423,13 @@ void PPPClass::end(void) {
}

bool PPPClass::sync() 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);

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);
Expand Down Expand Up @@ -500,15 +494,8 @@ 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) {
Expand All @@ -519,14 +506,7 @@ int PPPClass::RSSI() const {
}

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);
Expand All @@ -538,14 +518,7 @@ int PPPClass::BER() const {
}

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);
Expand All @@ -558,15 +531,8 @@ 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);
if (err != ESP_OK) {
Expand All @@ -578,15 +544,8 @@ 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);
if (err != ESP_OK) {
Expand All @@ -598,14 +557,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;
Expand All @@ -619,14 +571,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);
Expand All @@ -638,14 +583,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);
Expand All @@ -657,14 +595,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) {
Expand All @@ -675,14 +606,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) {
Expand All @@ -693,14 +617,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) {
Expand All @@ -711,14 +628,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) {
Expand All @@ -737,15 +647,44 @@ bool PPPClass::setBaudrate(int baudrate) {
return true;
}

bool PPPClass::sms(const char *num, const char *message) {
if (_dce == NULL) {
return false;
int PPPClass::getBatteryVoltage() 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are the log lines commented out?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I copied the functionality from other functions in PPP.cpp (RSSI + BER), which had them commented out:

// 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));

I guess I'm not sure why those two are commented out...when none of the others in the entire file are.

That's literally the only reason I commented them out...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the were commented out because they were causing floods during normal operation. If you expect the battery functions to often fail, you can leave them commented

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL, OK...well...I uncommented those two lines referenced above in the latest commit...oops?

I would not expect those commands to be "frequent failures", but maybe they are problematic. No issues on my end though.

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::getBatteryLevel() 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::getBatteryStatus() 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)) {
Expand Down Expand Up @@ -775,14 +714,8 @@ bool PPPClass::sms(const char *num, const char *message) {
}

String PPPClass::cmd(const char *at_command, int timeout) {
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 out[128] = {0};
esp_err_t err = _esp_modem_at(_dce, at_command, out, timeout);
if (err != ESP_OK) {
Expand Down
3 changes: 3 additions & 0 deletions libraries/PPP/src/PPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class PPPClass : public NetworkInterface {
// Modem DCE APIs
int RSSI() const;
int BER() const;
int getBatteryVoltage() const;
int getBatteryLevel() const;
int getBatteryStatus() const;
String IMSI() const;
String IMEI() const;
String moduleName() const; // modem module name
Expand Down
Loading