Hi,
I've been reading the GSMPIN.cpp code from the MKRGSM library to check if the function getPINUsed() actually reads the PIN requirement status from the SIM card, but it seems to use an internal variable to reply. That variable is only set (within the cpp file) when the setPINUsed() or switchPIN() functions are called.
Is there a proper way to check that the function does not really read anything from the SIM and I cannot use it like this?
/////////////////
/*
SIM has PIN disabled due to undefined incompatibilities.
We use the constructor without PIN.
***** UNDER DEVELOPMENT *****
Should use the PIN management example as a reference to detect if the SIM has PIN access enabled to prevent blocking it.
*/
// Set watchdog timer
myWatchDog.setup(WDT_SOFTCYCLE2M); // initialize WDT soft counter to 2 minutes interval since GSM is so slow to connect
// Start GSM connection
LOCAL_DEBUGGING(DEBUG_LEVEL_INFO, "Starting GSM...");
// Clear watchdog counter
myWatchDog.clear(); // Let the dog know we are still in bussiness
// Check if the SIM card requires a PIN
if (PINManager.getPINUsed()) {
LOCAL_DEBUGGING(DEBUG_LEVEL_ERROR, "SIM card requires PIN. PIN management needed.");
// Perform necessary actions for handling SIM PIN if needed
// Set watchdog timer off
myWatchDog.setup(WDT_OFF);
// Then loop forever because we cannot deal with PIN yet
while(1) {
LowPower.deepSleep(longLoopPeriod);
LOCAL_DEBUGGING(DEBUG_LEVEL_ERROR, "SIM card requires PIN. PIN management needed.");
}
} else {
LOCAL_DEBUGGING(DEBUG_LEVEL_TRACE, "SIM card does not require PIN, as expected.");
}
// Clear watchdog counter
myWatchDog.clear(); // Let the dog know we are still in bussiness
// We won't try forever to connect
int attemptsConnectGSM = 0;
while ((attemptsConnectGSM < MAX_ATTEMPTS_CONNECT_GSM) && (gsmAccess.begin() != GSM_READY)) {
LOCAL_DEBUGGING(DEBUG_LEVEL_WARN, "[" + String(attemptsConnectGSM) + "/" + String(MAX_ATTEMPTS_CONNECT_GSM) + "] GSM not connected yet.");
attemptsConnectGSM++;
// Disable watchdog timer
myWatchDog.setup(WDT_OFF);
// Put the board into sleep mode
LowPower.deepSleep(connectWaitingTimeForGSM);
// Enable watchdog timer
myWatchDog.setup(WDT_SOFTCYCLE2M); // initialize WDT soft counter to 2 minutes interval since GSM is so slow to connect
}
// Enable watchdog timer of 16 seconds
myWatchDog.setup(WDT_SOFTCYCLE16S);
// Then check if we tried too much to connect and could not. If we could not, it doesn't make sense to run the board because we cannot report back.
if (attemptsConnectGSM >= MAX_ATTEMPTS_CONNECT_GSM) {
LOCAL_DEBUGGING(DEBUG_LEVEL_ERROR, "Maximum GSM connection attempts reached. Going to sleep and restart.");
// Disable watchdog timer
myWatchDog.setup(WDT_OFF);
// Put the board into sleep mode
LowPower.deepSleep(loopPeriod);
restartBoard();
} else {
myWatchDog.clear(); // Let the dog know we are still in bussiness
DEBUGGING(DEBUG_LEVEL_INFO, "GSM initialized.");
}
I've seen in the PIN manager examples that the isPIN() function returns 0 if PIN kit disabled, 1 if SIM is PIN enabled but not locked, -1 if PIN is enabled and locked, -2 if PUK is locked, something else for unknown errors.
int pin_query = PINManager.isPIN();
if (pin_query == 1) {
// if SIM is locked, enter PIN code
Serial.print("Enter PIN code: ");
user_input = readSerial();
// check PIN code
if (PINManager.checkPIN(user_input) == 0) {
auth = true;
PINManager.setPINUsed(true);
Serial.println(oktext);
} else {
// if PIN code was incorrect
Serial.println("Incorrect PIN. Remember that you have 3 opportunities.");
}
} else if (pin_query == -1) {
// PIN code is locked, user must enter PUK code
Serial.println("PIN locked. Enter PUK code: ");
String puk = readSerial();
Serial.print("Now, enter a new PIN code: ");
user_input = readSerial();
// check PUK code
if (PINManager.checkPUK(puk, user_input) == 0) {
auth = true;
PINManager.setPINUsed(true);
Serial.println(oktext);
} else {
// if PUK or the new PIN are incorrect
Serial.println("Incorrect PUK or invalid new PIN. Try again!.");
}
} else if (pin_query == -2) {
// the worst case, PIN and PUK are locked
Serial.println("PIN and PUK locked. Use PIN2/PUK2 in a mobile phone.");
while (true);
} else {
// SIM does not require authentication
Serial.println("No pin necessary.");
auth = true;
}
Leading to code like this in my sketch:
/////////////////
// GSM Module //
/////////////////
/*
SIM has PIN disabled due to undefined incompatibilities.
We use the constructor without PIN.
***** UNDER DEVELOPMENT *****
Should use the PIN management example as a reference to detect if the SIM has PIN access enabled to prevent blocking it.
*/
// Set watchdog timer
myWatchDog.setup(WDT_SOFTCYCLE2M); // initialize WDT soft counter to 2 minutes interval since GSM is so slow to connect
// Start GSM connection
LOCAL_DEBUGGING(DEBUG_LEVEL_INFO, "Starting GSM...");
// Clear watchdog counter
myWatchDog.clear(); // Let the dog know we are still in bussiness
// Check if the SIM card requires a PIN
PINManager.begin();
switch (PINManager.isPIN()) {
case 0:
// Proceed with GSM connection setup
LOCAL_DEBUGGING(DEBUG_LEVEL_TRACE, "SIM card: PIN is not enabled.");
// Nothing left to do.
break;
case 1:
// Perform necessary actions for handling SIM PIN
// Set watchdog timer off
myWatchDog.setup(WDT_OFF);
// Then loop forever because we cannot tell when the SIM we have is ok or not. That requires to read how many PIN attempts are left.
do {
LOCAL_DEBUGGING(DEBUG_LEVEL_ERROR, "SIM card: PIN required. PIN management needed.");
LowPower.deepSleep(longLoopPeriod);
} while (1);
break;
case -1:
// Handle PIN locked case
// Set watchdog timer off
myWatchDog.setup(WDT_OFF);
// Then loop forever because we cdo not want to automatically use the PUK.
do {
LOCAL_DEBUGGING(DEBUG_LEVEL_SILENT, "SIM card: PIN is locked.");
LowPower.deepSleep(longLoopPeriod);
} while (1);
break;
case -2:
// Handle PUK locked case
// Set watchdog timer off
myWatchDog.setup(WDT_OFF);
// Then loop forever because we do not know the PIN2 or PUK2.
do {
LOCAL_DEBUGGING(DEBUG_LEVEL_SILENT, "SIM card: PUK is locked.");
LowPower.deepSleep(longLoopPeriod);
} while (1);
break;
default:
// Handle unknown case
// Set watchdog timer off
myWatchDog.setup(WDT_OFF);
// Then loop forever because we cannot tell what happened.
do {
LOCAL_DEBUGGING(DEBUG_LEVEL_SILENT, "SIM card: Unknown PIN status.");
LowPower.deepSleep(longLoopPeriod);
} while (1);
break;
}
// Clear watchdog counter
myWatchDog.clear(); // Let the dog know we are still in bussiness
// We won't try forever to connect
int attemptsConnectGSM = 0;
while ((attemptsConnectGSM < MAX_ATTEMPTS_CONNECT_GSM) && (gsmAccess.begin() != GSM_READY)) {
[...]
I already locked 2 SIM cards three times each trying to find out about the difference and tell if I can use the simplified version where I just check if PIN is required. Can you please help so I can prevent more locks and more calls to the mobile operator support for unlocking them?
Thanks