Skip to content

Commit 4a1f00a

Browse files
author
Seppo Takalo
authored
Merge pull request #10877 from u-blox/ubx_context_activation
Cellular: Ublox cellular context activation updated for C030_R412M
2 parents 54d7d7e + 6cd6017 commit 4a1f00a

File tree

8 files changed

+388
-92
lines changed

8 files changed

+388
-92
lines changed

features/cellular/framework/API/CellularContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class CellularContext : public CellularInterface {
5454
enum AuthenticationType {
5555
NOAUTH = 0,
5656
PAP,
57-
CHAP
57+
CHAP,
58+
AUTOMATIC
5859
};
5960

6061
/* whether the additional exception reports are allowed to be sent when the maximum uplink rate is reached */

features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.cpp

Lines changed: 126 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717

1818
#include "UBLOX_AT.h"
19-
#include "UBLOX_AT_CellularNetwork.h"
20-
#include "UBLOX_AT_CellularContext.h"
2119

2220
using namespace mbed;
2321
using namespace events;
@@ -72,7 +70,8 @@ AT_CellularNetwork *UBLOX_AT::open_network_impl(ATHandler &at)
7270

7371
AT_CellularContext *UBLOX_AT::create_context_impl(ATHandler &at, const char *apn, bool cp_req, bool nonip_req)
7472
{
75-
return new UBLOX_AT_CellularContext(at, this, apn, cp_req, nonip_req);
73+
ubx_context = new UBLOX_AT_CellularContext(at, this, apn, cp_req, nonip_req);
74+
return ubx_context;
7675
}
7776

7877
#if MBED_CONF_UBLOX_AT_PROVIDE_DEFAULT
@@ -89,3 +88,127 @@ CellularDevice *CellularDevice::get_default_instance()
8988
}
9089
#endif
9190

91+
nsapi_error_t UBLOX_AT::init()
92+
{
93+
_at->lock();
94+
_at->flush();
95+
_at->cmd_start("AT");
96+
_at->cmd_stop_read_resp();
97+
98+
#ifdef TARGET_UBLOX_C027
99+
_at->cmd_start("AT+CFUN=0");
100+
_at->cmd_stop_read_resp();
101+
if (_at->get_last_error() == NSAPI_ERROR_OK) {
102+
_at->cmd_start("ATE0"); // echo off
103+
_at->cmd_stop_read_resp();
104+
_at->cmd_start("AT+CMEE=1"); // verbose responses
105+
_at->cmd_stop_read_resp();
106+
config_authentication_parameters();
107+
_at->cmd_start("AT+CFUN=1"); // set full functionality
108+
_at->cmd_stop_read_resp();
109+
}
110+
#else
111+
_at->cmd_start("AT+CFUN=4");
112+
_at->cmd_stop_read_resp();
113+
if (_at->get_last_error() == NSAPI_ERROR_OK) {
114+
_at->cmd_start("ATE0"); // echo off
115+
_at->cmd_stop_read_resp();
116+
_at->cmd_start("AT+CMEE=1"); // verbose responses
117+
_at->cmd_stop_read_resp();
118+
config_authentication_parameters();
119+
_at->cmd_start("AT+CFUN=1"); // set full functionality
120+
_at->cmd_stop_read_resp();
121+
}
122+
#endif
123+
124+
return _at->unlock_return_error();
125+
}
126+
127+
nsapi_error_t UBLOX_AT::config_authentication_parameters()
128+
{
129+
char *config = NULL;
130+
nsapi_error_t err;
131+
char imsi[MAX_IMSI_LENGTH + 1];
132+
133+
if (apn == NULL) {
134+
err = get_imsi(imsi);
135+
if (err == NSAPI_ERROR_OK) {
136+
config = (char *)apnconfig(imsi);
137+
}
138+
}
139+
140+
ubx_context->get_next_credentials(&config);
141+
apn = ubx_context->get_apn();
142+
pwd = ubx_context->get_pwd();
143+
uname = ubx_context->get_uname();
144+
auth = ubx_context->get_auth();
145+
146+
auth = (*uname && *pwd) ? auth : CellularContext::NOAUTH;
147+
err = set_authentication_parameters(apn, uname, pwd, auth);
148+
149+
return err;
150+
}
151+
152+
nsapi_error_t UBLOX_AT::set_authentication_parameters(const char *apn,
153+
const char *username,
154+
const char *password,
155+
CellularContext::AuthenticationType auth)
156+
{
157+
int modem_security = ubx_context->nsapi_security_to_modem_security(auth);
158+
159+
_at->cmd_start("AT+CGDCONT=1,\"IP\",");
160+
_at->write_string(apn);
161+
_at->cmd_stop();
162+
_at->resp_start();
163+
_at->resp_stop();
164+
165+
if (_at->get_last_error() == NSAPI_ERROR_OK) {
166+
#ifdef TARGET_UBLOX_C030_R41XM
167+
if (modem_security == CellularContext::CHAP) {
168+
_at->cmd_start("AT+UAUTHREQ=1,");
169+
_at->write_int(modem_security);
170+
_at->write_string(password);
171+
_at->write_string(username);
172+
_at->cmd_stop();
173+
_at->resp_start();
174+
_at->resp_stop();
175+
} else if (modem_security == CellularContext::NOAUTH) {
176+
_at->cmd_start("AT+UAUTHREQ=1,");
177+
_at->write_int(modem_security);
178+
_at->cmd_stop();
179+
_at->resp_start();
180+
_at->resp_stop();
181+
} else {
182+
_at->cmd_start("AT+UAUTHREQ=1,");
183+
_at->write_int(modem_security);
184+
_at->write_string(username);
185+
_at->write_string(password);
186+
_at->cmd_stop();
187+
_at->resp_start();
188+
_at->resp_stop();
189+
}
190+
#else
191+
_at->cmd_start("AT+UAUTHREQ=1,");
192+
_at->write_int(modem_security);
193+
_at->write_string(username);
194+
_at->write_string(password);
195+
_at->cmd_stop();
196+
_at->resp_start();
197+
_at->resp_stop();
198+
#endif
199+
}
200+
201+
return _at->get_last_error();
202+
}
203+
204+
nsapi_error_t UBLOX_AT::get_imsi(char *imsi)
205+
{
206+
_at->lock();
207+
_at->cmd_start("AT+CIMI");
208+
_at->cmd_stop();
209+
_at->resp_start();
210+
_at->read_string(imsi, MAX_IMSI_LENGTH + 1);
211+
_at->resp_stop();
212+
213+
return _at->unlock_return_error();
214+
}

features/cellular/framework/targets/UBLOX/AT/UBLOX_AT.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
#endif
2828
#endif /* TARGET_FF_ARDUINO */
2929

30+
#include "APN_db.h"
3031
#include "AT_CellularDevice.h"
32+
#include "AT_CellularContext.h"
33+
#include "UBLOX_AT_CellularNetwork.h"
34+
#include "UBLOX_AT_CellularContext.h"
3135

3236
namespace mbed {
3337

@@ -40,6 +44,32 @@ class UBLOX_AT : public AT_CellularDevice {
4044
virtual AT_CellularContext *create_context_impl(ATHandler &at, const char *apn, bool cp_req = false, bool nonip_req = false);
4145
public: // NetworkInterface
4246
void handle_urc(FileHandle *fh);
47+
48+
virtual nsapi_error_t init();
49+
50+
private:
51+
52+
UBLOX_AT_CellularContext *ubx_context;
53+
54+
/** Length of IMSI buffer.
55+
*/
56+
static const int MAX_IMSI_LENGTH = 15;
57+
58+
const char *apn;
59+
const char *uname;
60+
const char *pwd;
61+
62+
/** The type of authentication to use.
63+
*/
64+
CellularContext::AuthenticationType auth;
65+
66+
nsapi_error_t config_authentication_parameters();
67+
68+
nsapi_error_t set_authentication_parameters(const char *apn, const char *username, const char *password, CellularContext::AuthenticationType auth);
69+
70+
/** Read IMSI of modem.
71+
*/
72+
nsapi_error_t get_imsi(char *imsi);
4373
};
4474

4575
} // namespace mbed

0 commit comments

Comments
 (0)