Skip to content

Commit

Permalink
Merge pull request #2263 from particle-iot/ch70471/cimi_delay
Browse files Browse the repository at this point in the history
[Boron LTE] Fix external SIM getting stuck in init
  • Loading branch information
keeramis authored Jan 13, 2021
2 parents ecdb028 + e78c239 commit 11968f7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
48 changes: 30 additions & 18 deletions hal/network/ncp_client/sara/sara_ncp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ const system_tick_t UBLOX_NCP_R4_WINDOW_SIZE_MS = 50;
const int UBLOX_DEFAULT_CID = 1;
const char UBLOX_DEFAULT_PDP_TYPE[] = "IP";

const int IMSI_MAX_RETRY_CNT = 5;
const int CCID_MAX_RETRY_CNT = 2;

} // anonymous

Expand Down Expand Up @@ -938,6 +940,30 @@ int SaraNcpClient::waitReady(bool powerOn) {
return SYSTEM_ERROR_NONE;
}

int SaraNcpClient::checkNetConfForImsi() {
int imsiCount = 0;
char buf[32] = {};
do {
auto resp = parser_.sendCommand(UBLOX_CIMI_TIMEOUT, "AT+CIMI");
memset(buf, 0, sizeof(buf));
int imsiLength = 0;
if (resp.hasNextLine()) {
imsiLength = CHECK(resp.readLine(buf, sizeof(buf)));
}
const int r = CHECK_PARSER(resp.readResult());
if (r == AtResponse::OK && imsiLength > 0) {
netConf_ = networkConfigForImsi(buf, imsiLength);
break;
} else if (imsiCount >= IMSI_MAX_RETRY_CNT) {
// if max retries are exhausted
return SYSTEM_ERROR_AT_RESPONSE_UNEXPECTED;
}
++imsiCount;
HAL_Delay_Milliseconds(100*imsiCount);
} while (imsiCount < IMSI_MAX_RETRY_CNT);
return SYSTEM_ERROR_NONE;
}

int SaraNcpClient::selectSimCard(ModemState& state) {
// Read current GPIO configuration
int mode = -1;
Expand Down Expand Up @@ -1069,21 +1095,12 @@ int SaraNcpClient::selectSimCard(ModemState& state) {
netConf_ = networkConfigForIccid(buf, lenCcid);
break;
}
} while (++ccidCount < 2);
} while (++ccidCount < CCID_MAX_RETRY_CNT);
// If failed above i.e., netConf_ is still not valid, look for network settings based on IMSI
if (!netConf_.isValid()) {
int imsiCount = 0;
do {
memset(buf, 0, sizeof(buf));
auto resp = parser_.sendCommand(UBLOX_CIMI_TIMEOUT, "AT+CIMI");
CHECK_PARSER(resp.readLine(buf, sizeof(buf)));
const int r = CHECK_PARSER(resp.readResult());
if (r == AtResponse::OK) {
netConf_ = networkConfigForImsi(buf, strlen(buf));
break;
}
} while (++imsiCount < 2);
CHECK(checkNetConfForImsi());
}

do {
// Set UMNOPROF and UBANDMASK as appropriate based on SIM
auto respUmnoprof = parser_.sendCommand("AT+UMNOPROF?");
Expand Down Expand Up @@ -1533,12 +1550,7 @@ int SaraNcpClient::configureApn(const CellularNetworkConfig& conf) {

// If failed above i.e., netConf_ is still not valid, look for network settings based on IMSI
if (!netConf_.isValid()) {
memset(buf, 0, sizeof(buf));
auto resp = parser_.sendCommand(UBLOX_CIMI_TIMEOUT, "AT+CIMI");
CHECK_PARSER(resp.readLine(buf, sizeof(buf)));
const int r = CHECK_PARSER(resp.readResult());
CHECK_TRUE(r == AtResponse::OK, SYSTEM_ERROR_AT_NOT_OK);
netConf_ = networkConfigForImsi(buf, strlen(buf));
CHECK(checkNetConfForImsi());
}
}

Expand Down
1 change: 1 addition & 0 deletions hal/network/ncp_client/sara/sara_ncp_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class SaraNcpClient: public CellularNcpClient {
int checkRunningImsi();
int processEventsImpl();
int getIccidImpl(char* buf, size_t size);
int checkNetConfForImsi();

int modemInit() const;
bool waitModemPowerState(bool onOff, system_tick_t timeout);
Expand Down

0 comments on commit 11968f7

Please sign in to comment.