Skip to content

Commit 1fd9ba6

Browse files
Teppo JärvelinAri Parkkila
authored andcommitted
APN lookup from database support.
1 parent 6670ebb commit 1fd9ba6

File tree

7 files changed

+121
-46
lines changed

7 files changed

+121
-46
lines changed

features/cellular/easy_cellular/CellularConnectionUtil.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static CELLULAR_DEVICE cellularDevice(at_queue);
3636
static char device_info_buf[2048];
3737

3838
CellularConnectionUtil::CellularConnectionUtil() : _serial(0), _state(STATE_POWER_ON), _next_state(_state),
39-
_status_callback(0), _network(0), _power(0), _queue(8 * EVENTS_EVENT_SIZE),
39+
_status_callback(0), _network(0), _power(0), _sim(0), _queue(8 * EVENTS_EVENT_SIZE),
4040
_queue_thread(0), _cellularDevice(&cellularDevice)
4141
{
4242
memset(_sim_pin, 0, sizeof(_sim_pin));
@@ -60,6 +60,12 @@ nsapi_error_t CellularConnectionUtil::init()
6060
return NSAPI_ERROR_NO_MEMORY;
6161
}
6262

63+
_sim = _cellularDevice->open_sim(_serial);
64+
if (!_sim) {
65+
stop();
66+
return NSAPI_ERROR_NO_MEMORY;
67+
}
68+
6369
at_queue.chain(&_queue);
6470

6571
tr_info("init done...");
@@ -94,43 +100,38 @@ void CellularConnectionUtil::set_sim_pin(const char * sim_pin)
94100

95101
bool CellularConnectionUtil::open_sim()
96102
{
103+
CellularSIM::SimState state = CellularSIM::SimStateUnknown;
104+
// wait until SIM is readable
105+
// here you could add wait(secs) if you know start delay of your SIM
106+
while (_sim->get_sim_state(state) != NSAPI_ERROR_OK || state == CellularSIM::SimStateUnknown) {
107+
tr_info("Waiting for SIM (state %d)...", state);
108+
return false;
109+
}
110+
tr_info("Initial SIM state: %d", state);
111+
97112
if (strlen(_sim_pin)) {
98113
nsapi_error_t err;
99-
static CellularSIM *sim;
100-
if (!sim) {
101-
sim = _cellularDevice->open_sim(_serial);
102-
}
103-
if (!sim) {
104-
return false;
105-
}
106-
CellularSIM::SimState state = CellularSIM::SimStateUnknown;
107-
// wait until SIM is readable
108-
// here you could add wait(secs) if you know start delay of your SIM
109-
while (sim->get_sim_state(state) != NSAPI_ERROR_OK || state == CellularSIM::SimStateUnknown) {
110-
tr_debug("Waiting for SIM (state %d)...", state);
111-
return false;
112-
}
113114
if (state == CellularSIM::SimStatePinNeeded) {
114115
tr_info("SIM pin required, entering pin: %s", _sim_pin);
115-
err = sim->set_pin(_sim_pin);
116+
err = _sim->set_pin(_sim_pin);
116117
if (err) {
117118
tr_error("SIM pin set failed with: %d, bailing out...", err);
118119
return false;
119120
}
120121
// here you could add wait(secs) if you know delay of changing PIN on your SIM
121122
for (int i = 0; i < MAX_SIM_READY_WAITING_TIME; i++) {
122-
if (sim->get_sim_state(state) == NSAPI_ERROR_OK && state == CellularSIM::SimStateReady) {
123+
if (_sim->get_sim_state(state) == NSAPI_ERROR_OK && state == CellularSIM::SimStateReady) {
123124
break;
124125
}
125126
tr_debug("SIM state: %d", state);
126127
return false;
127128
}
128129
}
129-
return state == CellularSIM::SimStateReady;
130130
} else {
131-
tr_info("Continue without SIM.");
132-
return true;
131+
tr_info("No SIM pin provided.");
133132
}
133+
134+
return state == CellularSIM::SimStateReady;
134135
}
135136

136137
void CellularConnectionUtil::device_ready()
@@ -506,6 +507,11 @@ CellularDevice* CellularConnectionUtil::get_device()
506507
return _cellularDevice;
507508
}
508509

510+
CellularSIM* CellularConnectionUtil::get_sim()
511+
{
512+
return _sim;
513+
}
514+
509515
NetworkStack *CellularConnectionUtil::get_stack()
510516
{
511517
return _cellularDevice->get_stack();

features/cellular/easy_cellular/CellularConnectionUtil.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "CellularNetwork.h"
3030
#include "CellularPower.h"
31+
#include "CellularSIM.h"
3132

3233
// modem type is defined as CELLULAR_DEVICE macro
3334
#define _CELLULAR_STRINGIFY(a) #a
@@ -105,6 +106,11 @@ class CellularConnectionUtil
105106
*/
106107
CellularDevice* get_device();
107108

109+
/** Get cellular sim interface
110+
* @return sim interface, NULL on failure
111+
*/
112+
CellularSIM* get_sim();
113+
108114
/** Change cellular connection to the target state
109115
* @param state to continue
110116
* @return see nsapi_error_t, 0 on success
@@ -142,6 +148,7 @@ class CellularConnectionUtil
142148

143149
CellularNetwork *_network;
144150
CellularPower *_power;
151+
CellularSIM *_sim;
145152
events::EventQueue _queue;
146153
rtos::Thread *_queue_thread;
147154
CellularDevice *_cellularDevice;

features/cellular/easy_cellular/EasyCellularConnection.cpp

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#include "CellularUtil.h"
2727

2828
#include "EasyCellularConnection.h"
29-
3029
#include "CellularLog.h"
3130

31+
#if MBED_CONF_APP_CELLULAR_USE_APN_LOOKUP
32+
#include "APN_db.h"
33+
#endif //MBED_CONF_APP_CELLULAR_USE_APN_LOOKUP
34+
3235
namespace mbed {
3336

3437
bool EasyCellularConnection::cellular_status(int state, int next_state)
@@ -50,7 +53,7 @@ bool EasyCellularConnection::cellular_status(int state, int next_state)
5053
}
5154

5255
EasyCellularConnection::EasyCellularConnection() : _is_connected(false), _is_initialized(false),
53-
_target_state(CellularConnectionUtil::STATE_POWER_ON),
56+
_credentials_set(false), _target_state(CellularConnectionUtil::STATE_POWER_ON),
5457
_cellularSerial(MDMTXD, MDMRXD, MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE),
5558
_cellularSemaphore(0), _cellularConnectionUtil(), _credentials_err(NSAPI_ERROR_OK)
5659
{
@@ -67,7 +70,7 @@ nsapi_error_t EasyCellularConnection::init()
6770
nsapi_error_t err = NSAPI_ERROR_OK;
6871
if (!_is_initialized) {
6972
#if defined (MDMRTS) && defined (MDMCTS)
70-
_cellularSerial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
73+
_cellularSerial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);
7174
#endif
7275
_cellularConnectionUtil.set_serial(&_cellularSerial);
7376
_cellularConnectionUtil.set_callback(callback(this, &EasyCellularConnection::cellular_status));
@@ -85,16 +88,21 @@ nsapi_error_t EasyCellularConnection::init()
8588

8689
void EasyCellularConnection::set_credentials(const char *apn, const char *uname, const char *pwd)
8790
{
88-
_credentials_err = init();
91+
if (apn && strlen(apn) > 0) {
92+
_credentials_err = init();
8993

90-
if (_credentials_err) {
91-
return;
92-
}
93-
CellularNetwork * network = _cellularConnectionUtil.get_network();
94-
if (network) {
95-
_credentials_err = network->set_credentials(apn, uname, pwd);
96-
} else {
97-
tr_error("NO Network...");
94+
if (_credentials_err) {
95+
return;
96+
}
97+
CellularNetwork * network = _cellularConnectionUtil.get_network();
98+
if (network) {
99+
_credentials_err = network->set_credentials(apn, uname, pwd);
100+
if (_credentials_err == NSAPI_ERROR_OK) {
101+
_credentials_set = true;
102+
}
103+
} else {
104+
tr_error("NO Network...");
105+
}
98106
}
99107
}
100108

@@ -107,18 +115,17 @@ void EasyCellularConnection::set_sim_pin(const char *sim_pin)
107115

108116
nsapi_error_t EasyCellularConnection::connect(const char *sim_pin, const char *apn, const char *uname, const char *pwd)
109117
{
110-
nsapi_error_t err = check_connect();
111-
if (err) {
112-
return err;
118+
if (_is_connected) {
119+
return NSAPI_ERROR_IS_CONNECTED;
113120
}
114121

115-
if (sim_pin) {
116-
_cellularConnectionUtil.set_sim_pin(sim_pin);
122+
set_credentials(apn, uname, pwd);
123+
if (_credentials_err) {
124+
return _credentials_err;
117125
}
118126

119-
err = _cellularConnectionUtil.get_network()->set_credentials(apn, uname, pwd);
120-
if (err) {
121-
return err;
127+
if (sim_pin) {
128+
_cellularConnectionUtil.set_sim_pin(sim_pin);
122129
}
123130

124131
return connect();
@@ -129,6 +136,12 @@ nsapi_error_t EasyCellularConnection::check_connect()
129136
if (_is_connected) {
130137
return NSAPI_ERROR_IS_CONNECTED;
131138
}
139+
140+
// there was an error while setting credentials but it's a void function so check error here...
141+
if (_credentials_err) {
142+
return _credentials_err;
143+
}
144+
132145
nsapi_error_t err = init();
133146
if (err) {
134147
return err;
@@ -139,15 +152,39 @@ nsapi_error_t EasyCellularConnection::check_connect()
139152

140153
nsapi_error_t EasyCellularConnection::connect()
141154
{
142-
// there was an error while setting credentials but it's a void function so check error here...
143-
if (_credentials_err) {
144-
return _credentials_err;
145-
}
146-
147155
nsapi_error_t err = check_connect();
148156
if (err) {
149157
return err;
150158
}
159+
#if MBED_CONF_APP_CELLULAR_USE_APN_LOOKUP
160+
if (!_credentials_set) {
161+
_target_state = CellularConnectionUtil::STATE_SIM_PIN;
162+
err = _cellularConnectionUtil.continue_to_state(_target_state);
163+
if (err == NSAPI_ERROR_OK) {
164+
int sim_wait = _cellularSemaphore.wait(6*1000); // reserve 6 seconds to access to SIM
165+
if (sim_wait != 1) {
166+
tr_error("NO SIM ACCESS");
167+
err = NSAPI_ERROR_NO_CONNECTION;
168+
} else {
169+
char imsi[MAX_IMSI_LENGTH+1];
170+
err = _cellularConnectionUtil.get_sim()->get_imsi(imsi);
171+
if (err == NSAPI_ERROR_OK) {
172+
const char *apn_config = apnconfig(imsi);
173+
if (apn_config) {
174+
const char* apn = _APN_GET(apn_config);
175+
const char* uname = _APN_GET(apn_config);
176+
const char* pwd = _APN_GET(apn_config);
177+
tr_info("Looked up APN %s", apn);
178+
err = _cellularConnectionUtil.get_network()->set_credentials(apn, uname, pwd);
179+
}
180+
}
181+
}
182+
}
183+
if (err) {
184+
return err;
185+
}
186+
}
187+
#endif // MBED_CONF_APP_CELLULAR_USE_APN_LOOKUP
151188

152189
_target_state = CellularConnectionUtil::STATE_CONNECTED;
153190
err = _cellularConnectionUtil.continue_to_state(_target_state);
@@ -166,6 +203,7 @@ nsapi_error_t EasyCellularConnection::disconnect()
166203
{
167204
_credentials_err = NSAPI_ERROR_OK;
168205
_is_connected = false;
206+
_credentials_set = false;
169207
if (!_cellularConnectionUtil.get_network()) {
170208
return NSAPI_ERROR_NO_CONNECTION;
171209
}

features/cellular/easy_cellular/EasyCellularConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ class EasyCellularConnection: public CellularBase {
136136

137137
bool _is_connected;
138138
bool _is_initialized;
139+
bool _credentials_set;
139140
CellularConnectionUtil::CellularState _target_state;
140141

141142
UARTSerial _cellularSerial;

features/cellular/framework/API/CellularSIM.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
namespace mbed {
2424

2525
const int MAX_SIM_READY_WAITING_TIME = 30;
26-
26+
const int MAX_IMSI_LENGTH = 15;
2727
/**
2828
* Class CellularSIM
2929
*
@@ -78,6 +78,13 @@ class CellularSIM
7878
* @return zero on success
7979
*/
8080
virtual nsapi_error_t get_sim_state(SimState &state) = 0;
81+
82+
/** Get IMSI from the sim card
83+
*
84+
* @param imsi preallocated char* which after successful request contains imsi
85+
* @return zero on success
86+
*/
87+
virtual nsapi_error_t get_imsi(char* imsi) = 0;
8188
};
8289

8390
} // namespace mbed

features/cellular/framework/AT/AT_CellularSIM.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,17 @@ nsapi_error_t AT_CellularSIM::set_pin_query(const char *sim_pin, bool query_pin)
114114
}
115115
return _at.unlock_return_error();
116116
}
117+
118+
nsapi_error_t AT_CellularSIM::get_imsi(char* imsi)
119+
{
120+
_at.lock();
121+
_at.cmd_start("AT+CIMI");
122+
_at.cmd_stop();
123+
_at.resp_start();
124+
int len = _at.read_string(imsi, MAX_IMSI_LENGTH);
125+
if (len > 0) {
126+
imsi[len] = '\0';
127+
}
128+
_at.resp_stop();
129+
return _at.unlock_return_error();
130+
}

features/cellular/framework/AT/AT_CellularSIM.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class AT_CellularSIM : public CellularSIM, public AT_CellularBase
4343
virtual nsapi_error_t set_pin_query(const char *sim_pin, bool query_pin);
4444

4545
virtual nsapi_error_t get_sim_state(SimState &state);
46+
47+
virtual nsapi_error_t get_imsi(char* imsi);
4648
};
4749

4850
} // namespace mbed

0 commit comments

Comments
 (0)