-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPinManagement_example.ino
180 lines (161 loc) · 4.07 KB
/
PinManagement_example.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*Copyright (C) 2020 Telit Communications S.p.A. Italy - All Rights Reserved.*/
/* See LICENSE file in the project root for full license information. */
/**
@file
- TLTMDM.h
@brief
This example enables you to change or remove the PIN of a SIM card inserted into a board.
@details
@version
1.0.0
@note
@author
Cristina Desogus
@date
09/23/2021
*/
// libraries
#include <TLTMDM.h>
ME310* myME310 = new ME310();
// pin manager object
TLTPIN PINManager(myME310);
// save input in serial by user
String user_input = "";
// authenticated with PIN code
boolean auth = false;
// serial monitor result messages
String oktext = "OK";
String errortext = "ERROR";
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(115200);
myME310->begin(115200);
delay(1000);
myME310->powerOn(ON_OFF);
delay(5000);
Serial.println("Change PIN example\n");
PINManager.begin();
// check if the SIM have pin lock
while (!auth)
{
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 incorrected
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 o 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.");
}
else
{
// SIM does not requires authentication
Serial.println("No pin necessary.");
auth = true;
}
}
// start module
Serial.print("Checking register in NB IoT / LTE Cat M1 network...");
if (PINManager.checkReg() == 0)
{
Serial.println(oktext);
}
// if you are connect by roaming
else if (PINManager.checkReg() == 1)
{
Serial.println("ROAMING " + oktext);
}
else
{
// error connection
Serial.println(errortext);
}
}
void loop() {
// Function loop implements pin management user menu
// You can only change PIN code if your SIM uses pin lock
Serial.println("Choose an option:\n1 - On/Off PIN.");
if (PINManager.getPINUsed())
{
Serial.println("2 - Change PIN.");
}
// save user input to user_op variable
String user_op = readSerial();
if (user_op == "1")
{
Serial.println("Enter your PIN code:");
user_input = readSerial();
// activate/deactivate PIN lock
PINManager.switchPIN(user_input);
}
else if (user_op == "2" && PINManager.getPINUsed())
{
Serial.println("Enter your actual PIN code:");
String oldPIN = readSerial();
Serial.println("Now, enter your new PIN code:");
String newPIN = readSerial();
// change PIN
PINManager.changePIN(oldPIN, newPIN);
}
else
{
Serial.println("Incorrect option. Try again!.");
}
delay(1000);
}
/*
Read serial input
*/
String readSerial()
{
String text = "";
while (1) {
while (Serial.available() > 0)
{
char inChar = Serial.read();
if (inChar == '\n')
{
return text;
}
if (inChar != '\r')
{
text += inChar;
}
}
}
}