diff --git a/features/cellular/framework/API/CellularDevice.h b/features/cellular/framework/API/CellularDevice.h index f58e0ad2b5..09dbceee5b 100644 --- a/features/cellular/framework/API/CellularDevice.h +++ b/features/cellular/framework/API/CellularDevice.h @@ -20,6 +20,7 @@ #include "CellularTargets.h" #include "CellularStateMachine.h" +#include "Callback.h" namespace mbed { @@ -145,6 +146,19 @@ public: */ nsapi_error_t attach_to_network(); + /** Register callback for status reporting. + * + * The specified status callback function will be called on the network and cellular device status changes. + * The parameters on the callback are the event type and event-type dependent reason parameter. + * + * @remark deleting CellularDevice/CellularContext in callback not allowed. + * @remark application should not attach to this function if using CellularContext::attach as it will contain the + * same information. + * + * @param status_cb The callback for status changes. + */ + void attach(Callback status_cb); + /** Create new CellularNetwork interface. * * @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default @@ -258,6 +272,7 @@ private: char _sim_pin[MAX_PIN_SIZE + 1]; char _plmn[MAX_PLMN_SIZE + 1]; PlatformMutex _mutex; + Callback _status_cb; }; } // namespace mbed diff --git a/features/cellular/framework/device/CellularDevice.cpp b/features/cellular/framework/device/CellularDevice.cpp index 7ebd1f14ef..c450922108 100644 --- a/features/cellular/framework/device/CellularDevice.cpp +++ b/features/cellular/framework/device/CellularDevice.cpp @@ -51,7 +51,7 @@ MBED_WEAK CellularDevice *CellularDevice::get_default_instance() #endif // CELLULAR_DEVICE CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0), _power_ref_count(0), _sim_ref_count(0), - _info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0) + _info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0), _status_cb(0) { set_sim_pin(NULL); set_plmn(NULL); @@ -158,6 +158,11 @@ nsapi_error_t CellularDevice::start_state_machine(CellularStateMachine::Cellular return err; } +void CellularDevice::attach(Callback status_cb) +{ + _status_cb = status_cb; +} + void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr) { if (ev >= NSAPI_EVENT_CELLULAR_STATUS_BASE && ev <= NSAPI_EVENT_CELLULAR_STATUS_END) { @@ -200,6 +205,11 @@ void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr) curr->cellular_callback(ev, ptr); curr = curr->_next; } + + // forward to callback function if set by attach(...) + if (_status_cb) { + _status_cb(ev, ptr); + } } } // namespace mbed