Skip to content

Cellular: Fixed con-disc sequence can now be called multiple times. #6928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 43 additions & 88 deletions features/cellular/easy_cellular/CellularConnectionFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ bool CellularConnectionFSM::power_on()
return true;
}

void CellularConnectionFSM::set_sim_pin(const char * sim_pin)
void CellularConnectionFSM::set_sim_pin(const char *sim_pin)
{
strncpy(_sim_pin, sim_pin, sizeof(_sim_pin));
_sim_pin[sizeof(_sim_pin)-1] = '\0';
}

void CellularConnectionFSM::set_plmn(const char* plmn)
void CellularConnectionFSM::set_plmn(const char *plmn)
{
_plmn = plmn;
}
Expand Down Expand Up @@ -184,15 +184,6 @@ bool CellularConnectionFSM::open_sim()
return state == CellularSIM::SimStateReady;
}

bool CellularConnectionFSM::set_network_registration()
{
if (_network->set_registration(_plmn) != NSAPI_ERROR_OK) {
tr_error("Failed to set network registration.");
return false;
}
return true;
}

bool CellularConnectionFSM::is_registered()
{
CellularNetwork::RegistrationStatus status;
Expand Down Expand Up @@ -259,33 +250,15 @@ bool CellularConnectionFSM::get_network_registration(CellularNetwork::Registrati
return true;
}

bool CellularConnectionFSM::get_attach_network(CellularNetwork::AttachStatus &status)
{
nsapi_error_t err = _network->get_attach(status);
if (err != NSAPI_ERROR_OK) {
return false;
}
return true;
}

bool CellularConnectionFSM::set_attach_network()
{
nsapi_error_t attach_err = _network->set_attach();
if (attach_err != NSAPI_ERROR_OK) {
return false;
}
return true;
}

void CellularConnectionFSM::report_failure(const char* msg)
void CellularConnectionFSM::report_failure(const char *msg)
{
tr_error("Cellular network failed: %s", msg);
if (_status_callback) {
_status_callback(_state, _next_state);
}
}

const char* CellularConnectionFSM::get_state_string(CellularState state)
const char *CellularConnectionFSM::get_state_string(CellularState state)
{
#if MBED_CONF_MBED_TRACE_ENABLE
static const char *strings[] = { "Init", "Power", "Device ready", "SIM pin", "Registering network", "Manual registering", "Attaching network", "Activating PDP Context", "Connecting network", "Connected"};
Expand All @@ -295,17 +268,6 @@ const char* CellularConnectionFSM::get_state_string(CellularState state)
#endif // #if MBED_CONF_MBED_TRACE_ENABLE
}

nsapi_error_t CellularConnectionFSM::is_automatic_registering(bool& auto_reg)
{
CellularNetwork::NWRegisteringMode mode;
nsapi_error_t err = _network->get_network_registering_mode(mode);
if (err == NSAPI_ERROR_OK) {
tr_debug("automatic registering mode: %d", mode);
auto_reg = (mode == CellularNetwork::NWModeAutomatic);
}
return err;
}

bool CellularConnectionFSM::is_registered_to_plmn()
{
int format;
Expand All @@ -325,7 +287,7 @@ bool CellularConnectionFSM::is_registered_to_plmn()
CellularNetwork::operator_names_list names_list;
nsapi_error_t err = _network->get_operator_names(names_list);
if (err == NSAPI_ERROR_OK) {
CellularNetwork::operator_names_t* op_names = names_list.get_head();
CellularNetwork::operator_names_t *op_names = names_list.get_head();
bool found_match = false;
while (op_names) {
if (format == 0) {
Expand Down Expand Up @@ -407,9 +369,18 @@ void CellularConnectionFSM::retry_state_or_fail()

void CellularConnectionFSM::state_init()
{
_event_timeout = _start_time;
tr_info("Init state, waiting %d ms before POWER state)", _start_time);
enter_to_state(STATE_POWER_ON);
// we should check that if power is already on then we can jump to device ready state
_cellularDevice->set_timeout(TIMEOUT_POWER_ON);
tr_info("Cellular state init (timeout %d ms)", TIMEOUT_POWER_ON);
nsapi_error_t err = _power->is_device_ready();
if (err != NSAPI_ERROR_OK) {
_event_timeout = _start_time;
tr_info("Init state, waiting %d ms before POWER state)", _start_time);
enter_to_state(STATE_POWER_ON);
} else {
tr_info("Device was ready to accept commands, jump to device ready");
enter_to_state(STATE_DEVICE_READY);
}
}

void CellularConnectionFSM::state_power_on()
Expand All @@ -424,37 +395,21 @@ void CellularConnectionFSM::state_power_on()
}
}

bool CellularConnectionFSM::device_ready()
void CellularConnectionFSM::device_ready()
{
tr_info("Cellular device ready");
if (_event_status_cb) {
_event_status_cb((nsapi_event_t)CellularDeviceReady, 0);
}

_power->remove_device_ready_urc_cb(mbed::callback(this, &CellularConnectionFSM::ready_urc_cb));

bool success = false;
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
if (!_network->set_registration_urc((CellularNetwork::RegistrationType)type, true)) {
success = true;
}
}
if (!success) {
tr_error("Failed to set any URC's for registration");
report_failure(get_state_string(_state));
return false;
}

return true;
}

void CellularConnectionFSM::state_device_ready()
{
_cellularDevice->set_timeout(TIMEOUT_POWER_ON);
if (_power->set_at_mode() == NSAPI_ERROR_OK) {
if (device_ready()) {
enter_to_state(STATE_SIM_PIN);
}
device_ready();
enter_to_state(STATE_SIM_PIN);
} else {
if (_retry_count == 0) {
(void)_power->set_device_ready_urc_cb(mbed::callback(this, &CellularConnectionFSM::ready_urc_cb));
Expand All @@ -468,6 +423,18 @@ void CellularConnectionFSM::state_sim_pin()
_cellularDevice->set_timeout(TIMEOUT_SIM_PIN);
tr_info("Sim state (timeout %d ms)", TIMEOUT_SIM_PIN);
if (open_sim()) {
bool success = false;
for (int type = 0; type < CellularNetwork::C_MAX; type++) {
if (!_network->set_registration_urc((CellularNetwork::RegistrationType)type, true)) {
success = true;
}
}
if (!success) {
tr_warn("Failed to set any URC's for registration");
retry_state_or_fail();
return;
}

if (_plmn) {
enter_to_state(STATE_MANUAL_REGISTERING_NETWORK);
} else {
Expand All @@ -485,12 +452,9 @@ void CellularConnectionFSM::state_registering()
// we are already registered, go to attach
enter_to_state(STATE_ATTACHING_NETWORK);
} else {
bool auto_reg = false;
nsapi_error_t err = is_automatic_registering(auto_reg);
if (err == NSAPI_ERROR_OK && !auto_reg) {
// automatic registering is not on, set registration and retry
_cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
set_network_registration();
_cellularDevice->set_timeout(TIMEOUT_REGISTRATION);
if (!_command_success) {
_command_success = (_network->set_registration() == NSAPI_ERROR_OK);
}
retry_state_or_fail();
}
Expand All @@ -507,7 +471,7 @@ void CellularConnectionFSM::state_manual_registering_network()
enter_to_state(STATE_ATTACHING_NETWORK);
} else {
if (!_command_success) {
_command_success = set_network_registration();
_command_success = (_network->set_registration(_plmn) == NSAPI_ERROR_OK);
}
retry_state_or_fail();
}
Expand All @@ -517,16 +481,8 @@ void CellularConnectionFSM::state_manual_registering_network()
void CellularConnectionFSM::state_attaching()
{
_cellularDevice->set_timeout(TIMEOUT_CONNECT);
CellularNetwork::AttachStatus attach_status;
if (get_attach_network(attach_status)) {
if (attach_status == CellularNetwork::Attached) {
enter_to_state(STATE_ACTIVATING_PDP_CONTEXT);
} else {
if (!_command_success) {
_command_success = set_attach_network();
}
retry_state_or_fail();
}
if (_network->set_attach() == NSAPI_ERROR_OK) {
enter_to_state(STATE_ACTIVATING_PDP_CONTEXT);
} else {
retry_state_or_fail();
}
Expand Down Expand Up @@ -701,9 +657,8 @@ void CellularConnectionFSM::ready_urc_cb()
if (_state == STATE_DEVICE_READY && _power->set_at_mode() == NSAPI_ERROR_OK) {
tr_debug("State was STATE_DEVICE_READY and at mode ready, cancel state and move to next");
_queue.cancel(_event_id);
if (device_ready()) {
continue_from_state(STATE_SIM_PIN);
}
device_ready();
continue_from_state(STATE_SIM_PIN);
}
}

Expand All @@ -712,17 +667,17 @@ events::EventQueue *CellularConnectionFSM::get_queue()
return &_queue;
}

CellularNetwork* CellularConnectionFSM::get_network()
CellularNetwork *CellularConnectionFSM::get_network()
{
return _network;
}

CellularDevice* CellularConnectionFSM::get_device()
CellularDevice *CellularConnectionFSM::get_device()
{
return _cellularDevice;
}

CellularSIM* CellularConnectionFSM::get_sim()
CellularSIM *CellularConnectionFSM::get_sim()
{
return _sim;
}
Expand Down
18 changes: 7 additions & 11 deletions features/cellular/easy_cellular/CellularConnectionFSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CellularConnectionFSM
/** Get event queue that can be chained to main event queue (or use start_dispatch)
* @return event queue
*/
events::EventQueue* get_queue();
events::EventQueue *get_queue();

/** Start event queue dispatching
* @return see nsapi_error_t, 0 on success
Expand All @@ -110,17 +110,17 @@ class CellularConnectionFSM
/** Get cellular network interface
* @return network interface, NULL on failure
*/
CellularNetwork* get_network();
CellularNetwork *get_network();

/** Get cellular device interface
* @return device interface, NULL on failure
*/
CellularDevice* get_device();
CellularDevice *get_device();

/** Get cellular sim interface
* @return sim interface, NULL on failure
*/
CellularSIM* get_sim();
CellularSIM *get_sim();

/** Change cellular connection to the target state
* @param state to continue. Default is to connect.
Expand Down Expand Up @@ -153,18 +153,14 @@ class CellularConnectionFSM
* @param state state which is returned in string format
* @return string format of the given state
*/
const char* get_state_string(CellularState state);
const char *get_state_string(CellularState state);

private:
bool power_on();
bool open_sim();
bool get_network_registration(CellularNetwork::RegistrationType type, CellularNetwork::RegistrationStatus &status, bool &is_registered);
bool set_network_registration();
bool get_attach_network(CellularNetwork::AttachStatus &status);
bool set_attach_network();
bool is_registered();
bool device_ready();
nsapi_error_t is_automatic_registering(bool& auto_reg);
void device_ready();

// state functions to keep state machine simple
void state_init();
Expand Down Expand Up @@ -215,7 +211,7 @@ class CellularConnectionFSM
events::EventQueue _at_queue;
char _st_string[20];
int _event_id;
const char* _plmn;
const char *_plmn;
bool _command_success;
bool _plmn_network_found;
};
Expand Down
Loading