Skip to content
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

Fix CGI for Quectel modems #2019

Merged
merged 2 commits into from Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
149 changes: 101 additions & 48 deletions hal/src/b5som/network/quectel_ncp_client.cpp
Expand Up @@ -38,6 +38,7 @@
#include "spark_wiring_vector.h"

#include <algorithm>
#include <limits>

#undef LOG_COMPILE_TIME_LEVEL
#define LOG_COMPILE_TIME_LEVEL LOG_LEVEL_ALL
Expand Down Expand Up @@ -152,54 +153,97 @@ int QuectelNcpClient::initParser(Stream* stream) {
auto parserConf = AtParserConfig().stream(stream).commandTerminator(AtCommandTerminator::CRLF);
parser_.destroy();
CHECK(parser_.init(std::move(parserConf)));
CHECK(parser_.addUrcHandler("+CREG",
[](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (QuectelNcpClient*)data;
int val[2];
int r = CHECK_PARSER_URC(reader->scanf("+CREG: %d,%d", &val[0], &val[1]));
CHECK_TRUE(r >= 1, SYSTEM_ERROR_UNKNOWN);
// Home network or roaming
if (val[r - 1] == 1 || val[r - 1] == 5) {
self->creg_ = RegistrationState::Registered;
} else {
self->creg_ = RegistrationState::NotRegistered;
}
self->checkRegistrationState();
return SYSTEM_ERROR_NONE;
},
this));
CHECK(parser_.addUrcHandler("+CGREG",
[](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (QuectelNcpClient*)data;
int val[2];
int r = CHECK_PARSER_URC(reader->scanf("+CGREG: %d,%d", &val[0], &val[1]));
CHECK_TRUE(r >= 1, SYSTEM_ERROR_UNKNOWN);
// Home network or roaming
if (val[r - 1] == 1 || val[r - 1] == 5) {
self->cgreg_ = RegistrationState::Registered;
} else {
self->cgreg_ = RegistrationState::NotRegistered;
}
self->checkRegistrationState();
return SYSTEM_ERROR_NONE;
},
this));
CHECK(parser_.addUrcHandler("+CEREG",
[](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (QuectelNcpClient*)data;
int val[2];
int r = CHECK_PARSER_URC(reader->scanf("+CEREG: %d,%d", &val[0], &val[1]));
CHECK_TRUE(r >= 1, SYSTEM_ERROR_UNKNOWN);
// Home network or roaming
if (val[r - 1] == 1 || val[r - 1] == 5) {
self->cereg_ = RegistrationState::Registered;
} else {
self->cereg_ = RegistrationState::NotRegistered;
}
self->checkRegistrationState();
return SYSTEM_ERROR_NONE;
},
this));

// NOTE: These URC handlers need to take care of both the URCs and direct responses to the commands.
// See CH28408

using LacType = decltype(CellularGlobalIdentity::location_area_code);
using CidType = decltype(CellularGlobalIdentity::cell_id);

//+CREG: <n>,<stat>[,<lac>,<ci>[,<Act>]]
keeramis marked this conversation as resolved.
Show resolved Hide resolved
//+CREG: <stat>[,<lac>,<ci>[,<Act>]]
CHECK(parser_.addUrcHandler("+CREG", [](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (QuectelNcpClient*)data;
unsigned int val[4] = {};
char atResponse[64] = {};
// Take a copy of AT response for multi-pass scanning
CHECK_PARSER_URC(reader->readLine(atResponse, sizeof(atResponse)));
// Parse response ignoring mode (replicate URC response)
int r = ::sscanf(atResponse, "+CREG: %*u,%u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]);
// Reparse URC as direct response
if (0 >= r) {
r = CHECK_PARSER_URC(
::sscanf(atResponse, "+CREG: %u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]));
}
CHECK_TRUE(r >= 1, SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED);
// Home network or roaming
if (val[0] == 1 || val[0] == 5) {
self->creg_ = RegistrationState::Registered;
} else {
self->creg_ = RegistrationState::NotRegistered;
}
self->checkRegistrationState();
// Cellular Global Identity (partial)
self->cgi_.location_area_code = r >= 2 ? static_cast<LacType>(val[1]) : std::numeric_limits<LacType>::max();
self->cgi_.cell_id = r >= 3 ? static_cast<CidType>(val[2]) : std::numeric_limits<CidType>::max();
return SYSTEM_ERROR_NONE;
}, this));
//+CGREG: <n>,<stat>[,<lac>,<ci>[,<Act>]]
//+CGREG: <stat>[,<lac>,<ci>[,<Act>]]
CHECK(parser_.addUrcHandler("+CGREG", [](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (QuectelNcpClient*)data;
unsigned int val[4] = {};
char atResponse[64] = {};
// Take a copy of AT response for multi-pass scanning
CHECK_PARSER_URC(reader->readLine(atResponse, sizeof(atResponse)));
// Parse response ignoring mode (replicate URC response)
int r = ::sscanf(atResponse, "+CGREG: %*u,%u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]);
// Reparse URC as direct response
if (0 >= r) {
r = CHECK_PARSER_URC(
::sscanf(atResponse, "+CGREG: %u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]));
}
CHECK_TRUE(r >= 1, SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED);
// Home network or roaming
if (val[0] == 1 || val[0] == 5) {
self->cgreg_ = RegistrationState::Registered;
} else {
self->cgreg_ = RegistrationState::NotRegistered;
}
self->checkRegistrationState();
// Cellular Global Identity (partial)
self->cgi_.location_area_code = r >= 2 ? static_cast<LacType>(val[1]) : std::numeric_limits<LacType>::max();
self->cgi_.cell_id = r >= 3 ? static_cast<CidType>(val[2]) : std::numeric_limits<CidType>::max();
return SYSTEM_ERROR_NONE;
}, this));
//+CEREG: <n>,<stat>[,<tac>,<ci>[,<Act>]]
//+CEREG: <stat>[,<tac>,<ci>[,<Act>]]
CHECK(parser_.addUrcHandler("+CEREG", [](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (QuectelNcpClient*)data;
unsigned int val[4] = {};
char atResponse[64] = {};
// Take a copy of AT response for multi-pass scanning
CHECK_PARSER_URC(reader->readLine(atResponse, sizeof(atResponse)));
// Parse response ignoring mode (replicate URC response)
int r = ::sscanf(atResponse, "+CEREG: %*u,%u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]);
// Reparse URC as direct response
if (0 >= r) {
r = CHECK_PARSER_URC(
::sscanf(atResponse, "+CEREG: %u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]));
}
CHECK_TRUE(r >= 1, SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED);
// Home network or roaming
if (val[0] == 1 || val[0] == 5) {
self->cereg_ = RegistrationState::Registered;
} else {
self->cereg_ = RegistrationState::NotRegistered;
}
self->checkRegistrationState();
// Cellular Global Identity (partial)
self->cgi_.location_area_code = r >= 2 ? static_cast<LacType>(val[1]) : std::numeric_limits<LacType>::max();
self->cgi_.cell_id = r >= 3 ? static_cast<CidType>(val[2]) : std::numeric_limits<CidType>::max();
return SYSTEM_ERROR_NONE;
}, this));
return SYSTEM_ERROR_NONE;
}

Expand Down Expand Up @@ -374,6 +418,15 @@ int QuectelNcpClient::queryAndParseAtCops(CellularSignalQuality* qual) {
r = CHECK_PARSER(resp.readResult());
CHECK_TRUE(r == AtResponse::OK, SYSTEM_ERROR_AT_NOT_OK);

// Preserve digit format data
const int mnc_digits = ::strnlen(mobileNetworkCode, sizeof(mobileNetworkCode));
CHECK_TRUE((2 == mnc_digits || 3 == mnc_digits), SYSTEM_ERROR_BAD_DATA);
if (2 == mnc_digits) {
cgi_.cgi_flags |= CGI_FLAG_TWO_DIGIT_MNC;
} else {
cgi_.cgi_flags &= ~CGI_FLAG_TWO_DIGIT_MNC;
}

// `atoi` returns zero on error, which is an invalid `mcc` and `mnc`
cgi_.mobile_country_code = static_cast<uint16_t>(::atoi(mobileCountryCode));
cgi_.mobile_network_code = static_cast<uint16_t>(::atoi(mobileNetworkCode));
Expand Down
44 changes: 26 additions & 18 deletions hal/src/boron/network/sara_ncp_client.cpp
Expand Up @@ -38,6 +38,7 @@
#include "spark_wiring_vector.h"

#include <algorithm>
#include <limits>

#undef LOG_COMPILE_TIME_LEVEL
#define LOG_COMPILE_TIME_LEVEL LOG_LEVEL_ALL
Expand Down Expand Up @@ -162,19 +163,26 @@ int SaraNcpClient::initParser(Stream* stream) {
.commandTerminator(AtCommandTerminator::CRLF);
parser_.destroy();
CHECK(parser_.init(std::move(parserConf)));

// NOTE: These URC handlers need to take care of both the URCs and direct responses to the commands.
// See CH28408

using LacType = decltype(CellularGlobalIdentity::location_area_code);
using CidType = decltype(CellularGlobalIdentity::cell_id);

// +CREG: <stat>[,<lac>,<ci>[,<AcTStatus>]]
CHECK(parser_.addUrcHandler("+CREG", [](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (SaraNcpClient*)data;
int val[4] = {-1,-1,-1,-1};
char atResponse[64] = {0};
unsigned int val[4] = {};
char atResponse[64] = {};
// Take a copy of AT response for multi-pass scanning
CHECK_PARSER_URC(reader->readLine(atResponse, sizeof(atResponse)));
// Parse response ignoring mode (replicate URC response)
int r = ::sscanf(atResponse, "+CREG: %*d,%d,\"%x\",\"%x\",%d", &val[0], &val[1], &val[2], &val[3]);
int r = ::sscanf(atResponse, "+CREG: %*u,%u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]);
// Reparse URC as direct response
if (0 >= r) {
r = CHECK_PARSER_URC(
::sscanf(atResponse, "+CREG: %d,\"%x\",\"%x\",%d", &val[0], &val[1], &val[2], &val[3]));
::sscanf(atResponse, "+CREG: %u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]));
}
CHECK_TRUE(r >= 1, SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED);
// Home network or roaming
Expand All @@ -185,24 +193,24 @@ int SaraNcpClient::initParser(Stream* stream) {
}
self->checkRegistrationState();
// Cellular Global Identity (partial)
self->cgi_.location_area_code = static_cast<uint16_t>(val[1]);
self->cgi_.cell_id = val[2];
self->cgi_.location_area_code = r >= 2 ? static_cast<LacType>(val[1]) : std::numeric_limits<LacType>::max();
self->cgi_.cell_id = r >= 3 ? static_cast<CidType>(val[2]) : std::numeric_limits<CidType>::max();
return 0;
}, this));
// n={0,1} +CGREG: <stat>
// n=2 +CGREG: <stat>[,<lac>,<ci>[,<AcT>,<rac>]]
CHECK(parser_.addUrcHandler("+CGREG", [](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (SaraNcpClient*)data;
int val[4] = {-1,-1,-1,-1};
char atResponse[64] = {0};
unsigned int val[4] = {};
char atResponse[64] = {};
// Take a copy of AT response for multi-pass scanning
CHECK_PARSER_URC(reader->readLine(atResponse, sizeof(atResponse)));
// Parse response ignoring mode (replicate URC response)
int r = ::sscanf(atResponse, "+CGREG: %*d,%d,\"%x\",\"%x\",%d,\"%*x\"", &val[0], &val[1], &val[2], &val[3]);
int r = ::sscanf(atResponse, "+CGREG: %*u,%u,\"%x\",\"%x\",%u,\"%*x\"", &val[0], &val[1], &val[2], &val[3]);
// Reparse URC as direct response
if (0 >= r) {
r = CHECK_PARSER_URC(
::sscanf(atResponse, "+CGREG: %d,\"%x\",\"%x\",%d,\"%*x\"", &val[0], &val[1], &val[2], &val[3]));
::sscanf(atResponse, "+CGREG: %u,\"%x\",\"%x\",%u,\"%*x\"", &val[0], &val[1], &val[2], &val[3]));
}
CHECK_TRUE(r >= 1, SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED);
// Home network or roaming
Expand All @@ -213,23 +221,23 @@ int SaraNcpClient::initParser(Stream* stream) {
}
self->checkRegistrationState();
// Cellular Global Identity (partial)
self->cgi_.location_area_code = val[1];
self->cgi_.cell_id = val[2];
self->cgi_.location_area_code = r >= 2 ? static_cast<LacType>(val[1]) : std::numeric_limits<LacType>::max();
self->cgi_.cell_id = r >= 3 ? static_cast<CidType>(val[2]) : std::numeric_limits<CidType>::max();
return 0;
}, this));
// +CEREG: <stat>[,[<tac>],[<ci>],[<AcT>][,<cause_type>,<reject_cause>[,[<Active_Time>],[<Periodic_TAU>]]]]
CHECK(parser_.addUrcHandler("+CEREG", [](AtResponseReader* reader, const char* prefix, void* data) -> int {
const auto self = (SaraNcpClient*)data;
int val[4] = {-1,-1,-1,-1};
char atResponse[64] = {0};
unsigned int val[4] = {};
char atResponse[64] = {};
// Take a copy of AT response for multi-pass scanning
CHECK_PARSER_URC(reader->readLine(atResponse, sizeof(atResponse)));
// Parse response ignoring mode (replicate URC response)
int r = ::sscanf(atResponse, "+CEREG: %*d,%d,\"%x\",\"%x\",%d", &val[0], &val[1], &val[2], &val[3]);
int r = ::sscanf(atResponse, "+CEREG: %*u,%u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]);
// Reparse URC as direct response
if (0 >= r) {
r = CHECK_PARSER_URC(
::sscanf(atResponse, "+CEREG: %d,\"%x\",\"%x\",%d", &val[0], &val[1], &val[2], &val[3]));
::sscanf(atResponse, "+CEREG: %u,\"%x\",\"%x\",%u", &val[0], &val[1], &val[2], &val[3]));
}
CHECK_TRUE(r >= 1, SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED);
// Home network or roaming
Expand All @@ -240,8 +248,8 @@ int SaraNcpClient::initParser(Stream* stream) {
}
self->checkRegistrationState();
// Cellular Global Identity (partial)
self->cgi_.location_area_code = val[1];
self->cgi_.cell_id = val[2];
self->cgi_.location_area_code = r >= 2 ? static_cast<LacType>(val[1]) : std::numeric_limits<LacType>::max();
self->cgi_.cell_id = r >= 3 ? static_cast<CidType>(val[2]) : std::numeric_limits<CidType>::max();
return 0;
}, this));
return 0;
Expand Down