Cellular security change only: improve reliability of AT+USECDEVINFO?
When a module has very recently booted AT+USECDEVINFO? can return ERROR so with this commit we retry a few times to improve reliability.
*/
#define U_CELL_SEC_HEX_BUFFER_LENGTH_BYTES 32
+#ifndef U_CELL_SEC_USECDEVINFO_RETRY
+/** Number of times to retry AT+USECDEVINFO? since a module may
+ * not respond if it's freshly booted.
+ */
+# define U_CELL_SEC_USECDEVINFO_RETRY 3
+#endif
+
+#ifndef U_CELL_SEC_USECDEVINFO_DELAY_SECONDS
+/** Wait between retries of AT+USECDEVINFO?.
+ */
+# define U_CELL_SEC_USECDEVINFO_DELAY_SECONDS 5
+#endif
+
// Check that U_SECURITY_SERIAL_NUMBER_MAX_LENGTH_BYTES is big enough
// to hold the IMEI as a string
#if U_SECURITY_SERIAL_NUMBER_MAX_LENGTH_BYTES < (U_CELL_INFO_IMEI_SIZE + 1)
@@ -106,9 +119,12 @@ static bool moduleIsSealed(const uCellPrivateInstance_t *pInstance)
bool isSealed = false;
uAtClientHandle_t atHandle = pInstance->atHandle;
int32_t moduleIsRegistered;
- int32_t deviceIsActivated;
int32_t deviceIsRegistered;
+ int32_t deviceIsActivated = -1;
+ // Try this a few times in case we've just booted
+ for (size_t x = 0; (x < U_CELL_SEC_USECDEVINFO_RETRY) &&
+ (deviceIsActivated < 0); x++) {
// Sealed is when AT+USECDEVINFO
// returns 1,1,1
uAtClientLock(atHandle);
@@ -125,6 +141,10 @@ static bool moduleIsSealed(const uCellPrivateInstance_t *pInstance)
isSealed = (moduleIsRegistered == 1) &&
(deviceIsRegistered == 1) &&
(deviceIsActivated == 1);
+ } else {
+ // Wait between tries
+ uPortTaskBlock(U_CELL_SEC_USECDEVINFO_DELAY_SECONDS);
+ }
}
return isSealed;
@@ -165,7 +185,7 @@ bool uCellSecIsBootstrapped(int32_t cellHandle)
uCellPrivateInstance_t *pInstance;
uAtClientHandle_t atHandle;
int32_t moduleIsRegistered;
- int32_t deviceIsActivated;
+ int32_t deviceIsActivated = -1;
if (gUCellPrivateMutex != NULL) {
@@ -178,6 +198,9 @@ bool uCellSecIsBootstrapped(int32_t cellHandle)
// Bootstrapped is when AT+USECDEVINFO
// returns 1,x,1
atHandle = pInstance->atHandle;
+ // Try this a few times in case we've just booted
+ for (size_t x = 0; (x < U_CELL_SEC_USECDEVINFO_RETRY) &&
+ (deviceIsActivated < 0); x++) {
uAtClientLock(atHandle);
uAtClientTimeoutSet(atHandle,
U_CELL_SEC_TRANSACTION_TIMEOUT_SECONDS * 1000);
@@ -193,6 +216,10 @@ bool uCellSecIsBootstrapped(int32_t cellHandle)
if (uAtClientUnlock(atHandle) == 0) {
isBootstrapped = (moduleIsRegistered == 1) &&
(deviceIsActivated == 1);
+ } else {
+ // Wait between tries
+ uPortTaskBlock(U_CELL_SEC_USECDEVINFO_DELAY_SECONDS);
+ }
}
}
}
