Skip to content

Commit 73a96a6

Browse files
author
Cruz Monrreal
authored
Merge pull request #8147 from AriParkkila/cell-auth
Fix authentication on a cellular network for Access Point Name (APN)
2 parents 5eb314f + 13106fb commit 73a96a6

File tree

9 files changed

+52
-6
lines changed

9 files changed

+52
-6
lines changed

features/cellular/TESTS/socket/udp/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static void udp_network_stack()
162162
cellular.set_sim_pin(MBED_CONF_APP_CELLULAR_SIM_PIN);
163163
#ifdef MBED_CONF_APP_APN
164164
CellularNetwork *network = cellular.get_network();
165-
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN) == NSAPI_ERROR_OK);
165+
TEST_ASSERT(network->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == NSAPI_ERROR_OK);
166166
#endif
167167
cellular_target_state = CellularConnectionFSM::STATE_CONNECTED;
168168
TEST_ASSERT(cellular.continue_to_state(cellular_target_state) == NSAPI_ERROR_OK);

features/cellular/framework/API/CellularNetwork.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class CellularNetwork : public NetworkInterface {
390390
*
391391
* @return NSAPI_ERROR_OK on success
392392
* NSAPI_ERROR_NO_CONNECTION if fails to find suitable context to activate or activation failed (if not already activated)
393-
* NSAPI_ERROR_UNSUPPORTED if NetworkStack was not found
393+
* NSAPI_ERROR_UNSUPPORTED if NetworkStack was not found or cellular device does not support authentication
394394
* NSAPI_ERROR_AUTH_FAILURE if password and username were provided and authentication to network failed
395395
* Also if PPP mode
396396
* NSAPI_ERROR_DEVICE_ERROR on failure and check more error from nsapi_ppp_connect(...)

features/cellular/framework/AT/AT_CellularBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class AT_CellularBase {
5151
enum SupportedFeature {
5252
AT_CGSN_WITH_TYPE, // AT+CGSN without type is likely always supported similar to AT+GSN
5353
AT_CGDATA, // alternative is to support only ATD*99***<cid>#
54+
AT_CGAUTH, // APN authentication AT commands supported
5455
SUPPORTED_FEATURE_END_MARK // must be last element in the array of features
5556
};
5657
static void set_unsupported_features(const SupportedFeature *unsupported_features);

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ nsapi_error_t AT_CellularNetwork::set_credentials(const char *apn,
210210
}
211211

212212
if (username && (len = strlen(username)) > 0) {
213+
if (!is_supported(AT_CGAUTH)) { // APN authentication is needed with username/password
214+
return NSAPI_ERROR_UNSUPPORTED;
215+
}
213216
_uname = (char *)malloc(len * sizeof(char) + 1);
214217
if (_uname) {
215218
memcpy(_uname, username, len + 1);
@@ -279,10 +282,7 @@ nsapi_error_t AT_CellularNetwork::activate_context()
279282
nsapi_error_t err = NSAPI_ERROR_OK;
280283

281284
// try to find or create context with suitable stack
282-
if (get_context()) {
283-
// try to authenticate user before activating or modifying context
284-
err = do_user_authentication();
285-
} else {
285+
if (!get_context()) {
286286
err = NSAPI_ERROR_NO_CONNECTION;
287287
}
288288

@@ -315,6 +315,12 @@ nsapi_error_t AT_CellularNetwork::activate_context()
315315
_at.resp_stop();
316316

317317
if (!_is_context_active) {
318+
// authenticate before activating or modifying context
319+
if (do_user_authentication() != NSAPI_ERROR_OK) {
320+
tr_error("Cellular authentication failed!");
321+
return _at.unlock_return_error();
322+
}
323+
318324
tr_info("Activate PDP context %d", _cid);
319325
_at.cmd_start("AT+CGACT=1,");
320326
_at.write_int(_cid);
@@ -509,6 +515,9 @@ nsapi_error_t AT_CellularNetwork::do_user_authentication()
509515
{
510516
// if user has defined user name and password we need to call CGAUTH before activating or modifying context
511517
if (_pwd && _uname) {
518+
if (!is_supported(AT_CGAUTH)) {
519+
return NSAPI_ERROR_UNSUPPORTED;
520+
}
512521
_at.cmd_start("AT+CGAUTH=");
513522
_at.write_int(_cid);
514523
_at.write_int(_authentication_type);

features/cellular/framework/AT/AT_CellularNetwork.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase {
146146
*/
147147
virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology op_rat);
148148

149+
/** APN user authentication
150+
*
151+
* @return NSAPI_ERROR_OK on success
152+
* NSAPI_ERROR_UNSUPPORTED on authentication not supported by cellular device
153+
* NSAPI_ERROR_AUTH_FAILURE on authentication to network failed
154+
*/
149155
virtual nsapi_error_t do_user_authentication();
150156
private:
151157
// "NO CARRIER" urc

features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
using namespace events;
3232
using namespace mbed;
3333

34+
static const AT_CellularBase::SupportedFeature unsupported_features[] = {
35+
AT_CellularBase::AT_CGAUTH, // BC95_AT_Commands_Manual_V1.9
36+
AT_CellularBase::SUPPORTED_FEATURE_END_MARK
37+
};
38+
3439
QUECTEL_BC95::QUECTEL_BC95(EventQueue &queue) : AT_CellularDevice(queue)
3540
{
41+
AT_CellularBase::set_unsupported_features(unsupported_features);
3642
}
3743

3844
QUECTEL_BC95::~QUECTEL_BC95()

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularNetwork.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,24 @@ nsapi_error_t QUECTEL_BG96_CellularNetwork::set_access_technology_impl(RadioAcce
111111

112112
return _at.unlock_return_error();
113113
}
114+
115+
nsapi_error_t QUECTEL_BG96_CellularNetwork::do_user_authentication()
116+
{
117+
if (_pwd && _uname) {
118+
_at.cmd_start("AT+QICSGP=");
119+
_at.write_int(_cid);
120+
_at.write_int(1); // IPv4
121+
_at.write_string(_apn);
122+
_at.write_string(_uname);
123+
_at.write_string(_pwd);
124+
_at.write_int(_authentication_type);
125+
_at.cmd_stop();
126+
_at.resp_start();
127+
_at.resp_stop();
128+
if (_at.get_last_error() != NSAPI_ERROR_OK) {
129+
return NSAPI_ERROR_AUTH_FAILURE;
130+
}
131+
}
132+
133+
return NSAPI_ERROR_OK;
134+
}

features/cellular/framework/targets/QUECTEL/BG96/QUECTEL_BG96_CellularNetwork.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class QUECTEL_BG96_CellularNetwork : public AT_CellularNetwork {
3333
virtual nsapi_error_t set_access_technology_impl(RadioAccessTechnology opRat);
3434

3535
virtual bool get_modem_stack_type(nsapi_ip_stack_t requested_stack);
36+
37+
virtual nsapi_error_t do_user_authentication();
3638
};
3739

3840
} // namespace mbed

features/cellular/framework/targets/TELIT/HE910/TELIT_HE910.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ using namespace events;
2424

2525
static const AT_CellularBase::SupportedFeature unsupported_features[] = {
2626
AT_CellularBase::AT_CGSN_WITH_TYPE, // HE910/UE910/UL865/UE866 AT Commands Reference Guide Rev. 11-2006-10-14
27+
AT_CellularBase::AT_CGAUTH, // HE910/UE910/UL865/UE866 AT Commands Reference Guide Rev. 11-2006-10-14
2728
AT_CellularBase::SUPPORTED_FEATURE_END_MARK
2829
};
2930

0 commit comments

Comments
 (0)