Skip to content

Cellular: Add CellularDevice::init_module API to be called at startup #7667

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 2 commits into from
Aug 17, 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
33 changes: 19 additions & 14 deletions features/cellular/easy_cellular/CellularConnectionFSM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@

#define RETRY_COUNT_DEFAULT 3

namespace mbed
{
namespace mbed {

CellularConnectionFSM::CellularConnectionFSM() :
_serial(0), _state(STATE_INIT), _next_state(_state), _status_callback(0), _event_status_cb(0), _network(0), _power(0), _sim(0),
_queue(8 * EVENTS_EVENT_SIZE), _queue_thread(0), _cellularDevice(0), _retry_count(0), _event_timeout(-1),
_at_queue(8 * EVENTS_EVENT_SIZE), _event_id(0), _plmn(0), _command_success(false), _plmn_network_found(false)
_serial(0), _state(STATE_INIT), _next_state(_state), _status_callback(0), _event_status_cb(0), _network(0), _power(0), _sim(0),
_queue(8 * EVENTS_EVENT_SIZE), _queue_thread(0), _cellularDevice(0), _retry_count(0), _event_timeout(-1),
_at_queue(8 * EVENTS_EVENT_SIZE), _event_id(0), _plmn(0), _command_success(false), _plmn_network_found(false)
{
memset(_sim_pin, 0, sizeof(_sim_pin));
#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
Expand Down Expand Up @@ -147,7 +146,7 @@ bool CellularConnectionFSM::power_on()
void CellularConnectionFSM::set_sim_pin(const char *sim_pin)
{
strncpy(_sim_pin, sim_pin, sizeof(_sim_pin));
_sim_pin[sizeof(_sim_pin)-1] = '\0';
_sim_pin[sizeof(_sim_pin) - 1] = '\0';
}

void CellularConnectionFSM::set_plmn(const char *plmn)
Expand Down Expand Up @@ -202,7 +201,7 @@ bool CellularConnectionFSM::is_registered()
}

bool CellularConnectionFSM::get_network_registration(CellularNetwork::RegistrationType type,
CellularNetwork::RegistrationStatus &status, bool &is_registered)
CellularNetwork::RegistrationStatus &status, bool &is_registered)
{
is_registered = false;
bool is_roaming = false;
Expand Down Expand Up @@ -395,23 +394,28 @@ void CellularConnectionFSM::state_power_on()
}
}

void CellularConnectionFSM::device_ready()
bool CellularConnectionFSM::device_ready()
{
if (_cellularDevice->init_module(_serial) != NSAPI_ERROR_OK) {
return false;
}
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));
_cellularDevice->close_power();
_power = NULL;
return true;
}

void CellularConnectionFSM::state_device_ready()
{
_cellularDevice->set_timeout(TIMEOUT_POWER_ON);
if (_power->set_at_mode() == NSAPI_ERROR_OK) {
device_ready();
enter_to_state(STATE_SIM_PIN);
if (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 Down Expand Up @@ -581,7 +585,7 @@ void CellularConnectionFSM::event()
if (_event_timeout == -1) {
_event_timeout = 0;
}
_event_id = _queue.call_in(_event_timeout*1000, callback(this, &CellularConnectionFSM::event));
_event_id = _queue.call_in(_event_timeout * 1000, callback(this, &CellularConnectionFSM::event));
if (!_event_id) {
report_failure("Cellular event failure!");
return;
Expand Down Expand Up @@ -659,9 +663,10 @@ void CellularConnectionFSM::ready_urc_cb()
tr_debug("Device ready URC func called");
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);
device_ready();
continue_from_state(STATE_SIM_PIN);
if (device_ready()) {
_queue.cancel(_event_id);
continue_from_state(STATE_SIM_PIN);
}
}
}

Expand Down
11 changes: 5 additions & 6 deletions features/cellular/easy_cellular/CellularConnectionFSM.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const int MAX_RETRY_ARRAY_SIZE = 10;
*
* Finite State Machine for connecting to cellular network
*/
class CellularConnectionFSM
{
class CellularConnectionFSM {
public:
CellularConnectionFSM();
virtual ~CellularConnectionFSM();
Expand Down Expand Up @@ -148,7 +147,7 @@ class CellularConnectionFSM
*
* @param plmn operator in numeric format. See more from 3GPP TS 27.007 chapter 7.3.
*/
void set_plmn(const char* plmn);
void set_plmn(const char *plmn);

/** returns readable format of the given state. Used for printing states while debugging.
*
Expand All @@ -162,7 +161,7 @@ class CellularConnectionFSM
bool open_sim();
bool get_network_registration(CellularNetwork::RegistrationType type, CellularNetwork::RegistrationStatus &status, bool &is_registered);
bool is_registered();
void device_ready();
bool device_ready();

// state functions to keep state machine simple
void state_init();
Expand All @@ -186,7 +185,7 @@ class CellularConnectionFSM
NetworkStack *get_stack();

private:
void report_failure(const char* msg);
void report_failure(const char *msg);
void event();
void ready_urc_cb();

Expand All @@ -203,7 +202,7 @@ class CellularConnectionFSM
events::EventQueue _queue;
rtos::Thread *_queue_thread;
CellularDevice *_cellularDevice;
char _sim_pin[PIN_SIZE+1];
char _sim_pin[PIN_SIZE + 1];
int _retry_count;
int _start_time;
int _event_timeout;
Expand Down
16 changes: 12 additions & 4 deletions features/cellular/framework/API/CellularDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@
#include "CellularInformation.h"
#include "NetworkStack.h"

namespace mbed
{
namespace mbed {

/**
* Class CellularDevice
*
* An abstract interface that defines opening and closing of cellular interfaces.
* Deleting/Closing of opened interfaces can be done only via this class.
*/
class CellularDevice
{
class CellularDevice {
public:
/** virtual Destructor
*/
Expand Down Expand Up @@ -116,6 +114,16 @@ class CellularDevice
* @return network stack
*/
virtual NetworkStack *get_stack() = 0;

/** Initialize cellular module must be called right after module is ready.
* For example, when multiple modules are supported in a single AT driver this function detects
* and adapts to an actual module at runtime.
*
* @param fh file handle used in communication to modem.
*
* @return 0 on success
*/
virtual nsapi_error_t init_module(FileHandle *fh) = 0;
};

} // namespace mbed
Expand Down
5 changes: 5 additions & 0 deletions features/cellular/framework/AT/AT_CellularDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,8 @@ NetworkStack *AT_CellularDevice::get_stack()
}
return _network->get_stack();
}

nsapi_error_t AT_CellularDevice::init_module(FileHandle *fh)
{
return NSAPI_ERROR_OK;
}
14 changes: 7 additions & 7 deletions features/cellular/framework/AT/AT_CellularDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@

#include "ATHandler.h"

namespace mbed
{
namespace mbed {

/**
* Class AT_CellularDevice
*
* A class defines opening and closing of cellular interfaces.
* Deleting/Closing of opened interfaces can be done only through this class.
*/
class AT_CellularDevice : public CellularDevice
{
class AT_CellularDevice : public CellularDevice {
public:
AT_CellularDevice(events::EventQueue &queue);
virtual ~AT_CellularDevice();
Expand All @@ -52,7 +50,7 @@ class AT_CellularDevice : public CellularDevice
*
* @param at_handler
*/
void release_at_handler(ATHandler* at_handler);
void release_at_handler(ATHandler *at_handler);

public: // CellularDevice
virtual CellularNetwork *open_network(FileHandle *fh);
Expand Down Expand Up @@ -83,12 +81,14 @@ class AT_CellularDevice : public CellularDevice

virtual NetworkStack *get_stack();

virtual nsapi_error_t init_module(FileHandle *fh);

protected:
AT_CellularNetwork *_network;
AT_CellularSMS *_sms;
AT_CellularSIM *_sim;
AT_CellularPower* _power;
AT_CellularInformation* _information;
AT_CellularPower *_power;
AT_CellularInformation *_information;

protected:
events::EventQueue &_queue;
Expand Down