Skip to content

Commit 8f1bf43

Browse files
authored
Merge pull request #11414 from AriParkkila/cell-clear
Cellular: Add API to clear CellularDevice
2 parents 0f1962e + 94dcab9 commit 8f1bf43

11 files changed

+135
-1
lines changed

UNITTESTS/stubs/AT_CellularDevice_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,8 @@ void AT_CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, Cellul
256256
void AT_CellularDevice::set_at_urcs_impl()
257257
{
258258
}
259+
260+
nsapi_error_t AT_CellularDevice::clear()
261+
{
262+
return NSAPI_ERROR_OK;
263+
}

UNITTESTS/stubs/AT_CellularNetwork_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,8 @@ nsapi_error_t AT_CellularNetwork::set_packet_domain_event_reporting(bool on)
166166
{
167167
return NSAPI_ERROR_OK;
168168
}
169+
170+
nsapi_error_t AT_CellularNetwork::clear()
171+
{
172+
return NSAPI_ERROR_OK;
173+
}

UNITTESTS/stubs/CellularDevice_stub.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ nsapi_error_t CellularDevice::shutdown()
124124
void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx)
125125
{
126126
}
127+
128+
nsapi_error_t CellularDevice::clear()
129+
{
130+
return NSAPI_ERROR_OK;
131+
}

features/cellular/framework/API/CellularDevice.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ class CellularDevice {
9090
*/
9191
virtual ~CellularDevice();
9292

93+
/** Clear modem to a default initial state
94+
*
95+
* Clear persistent user data from the modem, such as PDP contexts.
96+
*
97+
* @pre All open network services on modem, such as contexts and sockets, must be closed.
98+
* @post Modem power off/on may be needed to clear modem's runtime state.
99+
* @remark CellularStateMachine calls this on connect when `cellular.clear-on-connect: true`.
100+
*
101+
* @return NSAPI_ERROR_OK on success, otherwise modem may be need power cycling
102+
*/
103+
virtual nsapi_error_t clear();
104+
93105
/** Sets the modem up for powering on
94106
* This is equivalent to plugging in the device, i.e., attaching power and serial port.
95107
* In general, hard_power_on and soft_power_on provides a simple hardware abstraction layer

features/cellular/framework/AT/AT_CellularDevice.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,3 +624,12 @@ void AT_CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr, Cellul
624624
}
625625
CellularDevice::cellular_callback(ev, ptr, ctx);
626626
}
627+
628+
nsapi_error_t AT_CellularDevice::clear()
629+
{
630+
AT_CellularNetwork *net = static_cast<AT_CellularNetwork *>(open_network());
631+
nsapi_error_t err = net->clear();
632+
close_network();
633+
634+
return err;
635+
}

features/cellular/framework/AT/AT_CellularDevice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class AT_CellularDevice : public CellularDevice {
3939
AT_CellularDevice(FileHandle *fh);
4040
virtual ~AT_CellularDevice();
4141

42+
virtual nsapi_error_t clear();
43+
4244
virtual nsapi_error_t hard_power_on();
4345

4446
virtual nsapi_error_t hard_power_off();

features/cellular/framework/AT/AT_CellularNetwork.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,69 @@ nsapi_error_t AT_CellularNetwork::set_packet_domain_event_reporting(bool on)
631631

632632
return _at.at_cmd_discard("+CGEREP", "=", "%d", on ? 1 : 0);
633633
}
634+
635+
nsapi_error_t AT_CellularNetwork::clear()
636+
{
637+
tr_info("AT_CellularNetwork::clear");
638+
639+
_at.lock();
640+
_at.cmd_start_stop("+CGDCONT", "?");
641+
_at.resp_start("+CGDCONT:");
642+
643+
struct context_s {
644+
int id;
645+
context_s *next;
646+
};
647+
CellularList<context_s> contexts;
648+
while (_at.info_resp()) {
649+
int cid = _at.read_int();
650+
// clear all but the default context
651+
if (cid <= 0) {
652+
continue;
653+
} else if (cid == 1) {
654+
#ifndef MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN
655+
continue;
656+
#else
657+
char pdp_type_from_context[10];
658+
int pdp_type_len = _at.read_string(pdp_type_from_context, sizeof(pdp_type_from_context));
659+
if (pdp_type_len > 0) {
660+
char apn[MAX_ACCESSPOINT_NAME_LENGTH];
661+
int apn_len = _at.read_string(apn, sizeof(apn));
662+
if (apn_len >= 0) {
663+
if (strcmp(apn, MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN) == 0) {
664+
continue;
665+
}
666+
}
667+
}
668+
#endif
669+
}
670+
contexts.add_new()->id = cid;
671+
}
672+
_at.resp_stop();
673+
674+
if (contexts.get_head()) {
675+
// try to detach from network before deleting contexts
676+
(void)detach();
677+
context_s *context = contexts.get_head();
678+
while (context) {
679+
if (_at.at_cmd_discard("+CGDCONT", "=", "%d", context->id) != NSAPI_ERROR_OK) {
680+
tr_warn("Clear context %d failed", context->id);
681+
}
682+
context = context->next;
683+
}
684+
#ifdef MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN
685+
char pdp_type_str[sizeof("IPV4V6")];
686+
if (get_property(PROPERTY_IPV4V6_PDP_TYPE) ||
687+
(get_property(PROPERTY_IPV4_PDP_TYPE) && get_property(PROPERTY_IPV6_PDP_TYPE))) {
688+
strcpy(pdp_type_str, "IPV4V6");
689+
} else if (get_property(PROPERTY_IPV6_PDP_TYPE)) {
690+
strcpy(pdp_type_str, "IPV6");
691+
} else {
692+
strcpy(pdp_type_str, "IP");
693+
}
694+
_at.at_cmd_discard("+CGDCONT", "=", "%d%s%s", 1, pdp_type_str, MBED_CONF_NSAPI_DEFAULT_CELLULAR_APN);
695+
#endif
696+
}
697+
698+
return _at.unlock_return_error();
699+
}

features/cellular/framework/AT/AT_CellularNetwork.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ class AT_CellularNetwork : public CellularNetwork, public AT_CellularBase {
110110
* Can be overridden by the target class.
111111
*/
112112
virtual void get_context_state_command();
113+
114+
/** Clear the network and contexts to a known default state
115+
*
116+
* @return NSAPI_ERROR_OK on success
117+
*/
118+
nsapi_error_t clear();
119+
113120
private:
114121
void urc_creg();
115122
void urc_cereg();

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ MBED_WEAK CellularDevice *CellularDevice::get_target_default_instance()
3434
}
3535

3636
CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0),
37-
_info_ref_count(0), _fh(fh), _queue(8 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0), _status_cb(0)
37+
_info_ref_count(0), _fh(fh), _queue(10 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0), _status_cb(0)
3838
{
3939
MBED_ASSERT(fh);
4040
set_sim_pin(NULL);
@@ -250,4 +250,9 @@ void CellularDevice::set_retry_timeout_array(const uint16_t timeout[], int array
250250
}
251251
}
252252

253+
nsapi_error_t CellularDevice::clear()
254+
{
255+
return NSAPI_ERROR_OK;
256+
}
257+
253258
} // namespace mbed

features/cellular/framework/device/CellularStateMachine.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,13 @@ bool CellularStateMachine::device_ready()
345345
}
346346
#endif // MBED_CONF_CELLULAR_DEBUG_AT
347347

348+
#ifdef MBED_CONF_CELLULAR_CLEAR_ON_CONNECT
349+
if (_cellularDevice.clear() != NSAPI_ERROR_OK) {
350+
tr_warning("CellularDevice clear failed");
351+
return false;
352+
}
353+
#endif
354+
348355
send_event_cb(CellularDeviceReady);
349356
_cellularDevice.set_ready_cb(0);
350357

@@ -364,6 +371,13 @@ void CellularStateMachine::state_device_ready()
364371
if (device_ready()) {
365372
_status = 0;
366373
enter_to_state(STATE_SIM_PIN);
374+
} else {
375+
tr_warning("Power cycle CellularDevice and restart connecting");
376+
(void) _cellularDevice.soft_power_off();
377+
(void) _cellularDevice.hard_power_off();
378+
_status = 0;
379+
_is_retry = true;
380+
enter_to_state(STATE_INIT);
367381
}
368382
} else {
369383
_status = 0;

features/cellular/mbed_lib.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
"offload-dns-queries" : {
2525
"help": "Use modem IP stack for DNS queries, null or numeric simultaneous queries",
2626
"value": null
27+
},
28+
"clear-on-connect" : {
29+
"help": "Clear modem to a known default state on connect()",
30+
"value": true
2731
}
2832
}
2933
}

0 commit comments

Comments
 (0)