91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
#include "connectivity/ml307_at_parser.h"
|
|
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
namespace borla {
|
|
|
|
bool ParseMl307Registration(
|
|
const char* response,
|
|
CellularRegistration* registration)
|
|
{
|
|
if (response == nullptr || registration == nullptr) {
|
|
return false;
|
|
}
|
|
const char* marker = std::strstr(response, "+CEREG:");
|
|
if (marker == nullptr) {
|
|
return false;
|
|
}
|
|
unsigned mode = 0;
|
|
unsigned status = 0;
|
|
const int fields =
|
|
std::sscanf(marker, "+CEREG: %u,%u", &mode, &status);
|
|
if (fields == 1) {
|
|
status = mode;
|
|
mode = 0;
|
|
} else if (fields != 2) {
|
|
return false;
|
|
}
|
|
if (mode > UINT8_MAX || status > UINT8_MAX) {
|
|
return false;
|
|
}
|
|
registration->mode = static_cast<uint8_t>(mode);
|
|
registration->status = static_cast<uint8_t>(status);
|
|
return true;
|
|
}
|
|
|
|
bool ParseMl307SignalQuality(const char* response, int8_t* csq)
|
|
{
|
|
if (response == nullptr || csq == nullptr) {
|
|
return false;
|
|
}
|
|
const char* marker = std::strstr(response, "+CSQ:");
|
|
if (marker == nullptr) {
|
|
return false;
|
|
}
|
|
int value = -1;
|
|
if (std::sscanf(marker, "+CSQ: %d", &value) != 1 ||
|
|
!((value >= 0 && value <= 31) || value == 99)) {
|
|
return false;
|
|
}
|
|
*csq = value == 99 ? -1 : static_cast<int8_t>(value);
|
|
return true;
|
|
}
|
|
|
|
bool CopyMl307ResponseLine(
|
|
const char* response,
|
|
const char* prefix,
|
|
char* output,
|
|
size_t output_capacity)
|
|
{
|
|
if (response == nullptr || prefix == nullptr || output == nullptr ||
|
|
output_capacity == 0) {
|
|
return false;
|
|
}
|
|
const char* begin =
|
|
prefix[0] == '\0' ? response : std::strstr(response, prefix);
|
|
if (begin == nullptr) {
|
|
return false;
|
|
}
|
|
begin += std::strlen(prefix);
|
|
while (*begin == ' ' || *begin == ':' || *begin == '"' ||
|
|
*begin == '\r' || *begin == '\n') {
|
|
++begin;
|
|
}
|
|
const char* end = begin;
|
|
while (*end != '\0' && *end != '\r' && *end != '\n' &&
|
|
*end != '"') {
|
|
++end;
|
|
}
|
|
const size_t size = static_cast<size_t>(end - begin);
|
|
if (size == 0 || size >= output_capacity) {
|
|
return false;
|
|
}
|
|
std::memset(output, 0, output_capacity);
|
|
std::memcpy(output, begin, size);
|
|
return true;
|
|
}
|
|
|
|
} // namespace borla
|
|
|