Added C API example

This commit is contained in:
David Cermak
2021-03-30 08:25:16 +02:00
parent 246572d286
commit 031e41ddf2
10 changed files with 146 additions and 297 deletions

View File

@ -42,7 +42,6 @@ static inline command_result generic_command(CommandableIf* t, const std::string
static inline command_result generic_get_string(CommandableIf* t, const std::string& command, std::string& output, uint32_t timeout_ms)
{
std::cout << command << std::endl;
return t->command(command, [&](uint8_t *data, size_t len) {
size_t pos = 0;
std::string response((char*)data, len);
@ -51,7 +50,6 @@ static inline command_result generic_get_string(CommandableIf* t, const std::str
for (auto i = 0; i<2; ++i) // trip trailing \n\r of last two chars
if (pos >= 1 && (token[pos-1] == '\r' || token[pos-1] == '\n'))
token.pop_back();
std::cout << "###" << token << "#### " << std::endl;
if (token.find("OK") != std::string::npos) {
return command_result::OK;
@ -164,4 +162,29 @@ command_result set_pin(CommandableIf* t, const std::string& pin)
return generic_command_common(t, set_pin_command);
}
command_result get_signal_quality(CommandableIf* t, int &rssi, int &ber)
{
std::cout << "get_signal_quality" << std::endl;
return t->command("AT+CSQ\r", [&](uint8_t *data, size_t len) {
size_t pos = 0;
std::string response((char*)data, len);
while ((pos = response.find('\n')) != std::string::npos) {
std::string token = response.substr(0, pos);
for (auto i = 0; i<2; ++i) // trip trailing \n\r of last two chars
if (pos >= 1 && (token[pos-1] == '\r' || token[pos-1] == '\n'))
token.pop_back();
if (token.find("OK") != std::string::npos) {
return command_result::OK;
} else if (token.find("ERROR") != std::string::npos) {
return command_result::FAIL;
} else if (token.find("+CSQ") != std::string::npos) {
sscanf(token.c_str(), "%*s%d,%d", &rssi, &ber);
}
response = response.substr(pos+1);
}
return command_result::TIMEOUT;
}, 500);
}
} // esp_modem::dce_commands