Skip to content

Commit

Permalink
Merge pull request #2726 from particle-iot/fix/zero-mnc
Browse files Browse the repository at this point in the history
[cellular] fixes handling of 00 or 000 MNC
  • Loading branch information
avtolstoy authored Jan 18, 2024
2 parents cf540b2 + d05c0f7 commit e5eea6d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions hal/network/ncp/cellular/cellular_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,12 @@ cellular_result_t cellular_global_identity(CellularGlobalIdentity* cgi, void* re
const auto client = mgr->ncpClient();
CHECK_TRUE(client, SYSTEM_ERROR_UNKNOWN);

// Load cached data into result struct
// Load data into result struct
CHECK(client->getCellularGlobalIdentity(cgi));

// Validate cache
CHECK_TRUE(0 != cgi->mobile_country_code, SYSTEM_ERROR_BAD_DATA);
CHECK_TRUE(0 != cgi->mobile_network_code, SYSTEM_ERROR_BAD_DATA);
// MNC may be 00 or 000. In case of incorrect parsing getCellularGlobalIdentity() should have failed anyway
CHECK_TRUE(0xFFFF != cgi->location_area_code, SYSTEM_ERROR_BAD_DATA);
CHECK_TRUE(0xFFFFFFFF != cgi->cell_id, SYSTEM_ERROR_BAD_DATA);

Expand Down
6 changes: 5 additions & 1 deletion hal/network/ncp_client/quectel/quectel_ncp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,12 @@ int QuectelNcpClient::queryAndParseAtCops(CellularSignalQuality* qual) {
cgi_.cgi_flags &= ~CGI_FLAG_TWO_DIGIT_MNC;
}

// `atoi` returns zero on error, which is an invalid `mcc` and `mnc`
// NOTE: MCC cannot be zero, MNC may be 00 or even 000
// We should not need to check whether str -> int coversion works
// as scanf() above along with the return value check guarantees that we've
// scanned the correct MCC MNC combo returned from +COPS.
cgi_.mobile_country_code = static_cast<uint16_t>(::atoi(mobileCountryCode));
CHECK_TRUE(cgi_.mobile_country_code != 0, SYSTEM_ERROR_BAD_DATA);
cgi_.mobile_network_code = static_cast<uint16_t>(::atoi(mobileNetworkCode));

switch (static_cast<CellularAccessTechnology>(act)) {
Expand Down
6 changes: 5 additions & 1 deletion hal/network/ncp_client/sara/sara_ncp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,8 +681,12 @@ int SaraNcpClient::queryAndParseAtCops(CellularSignalQuality* qual) {
cgi_.cgi_flags &= ~CGI_FLAG_TWO_DIGIT_MNC;
}

// `atoi` returns zero on error, which is an invalid `mcc` and `mnc`
// NOTE: MCC cannot be zero, MNC may be 00 or even 000
// We should not need to check whether str -> int coversion works
// as scanf() above along with the return value check guarantees that we've
// scanned the correct MCC MNC combo returned from +COPS.
cgi_.mobile_country_code = static_cast<uint16_t>(::atoi(mobileCountryCode));
CHECK_TRUE(cgi_.mobile_country_code != 0, SYSTEM_ERROR_BAD_DATA);
cgi_.mobile_network_code = static_cast<uint16_t>(::atoi(mobileNetworkCode));

if (ncpId() == PLATFORM_NCP_SARA_R410 || ncpId() == PLATFORM_NCP_SARA_R510) {
Expand Down

0 comments on commit e5eea6d

Please sign in to comment.