Cellular: add Callback functionality to CellularDevice.

pull/8852/head
Teppo Järvelin 2018-11-27 08:05:41 +02:00
parent a3f1ad7b8b
commit 76672db5cc
2 changed files with 26 additions and 1 deletions

View File

@ -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<void(nsapi_event_t, intptr_t)> 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<void(nsapi_event_t, intptr_t)> _status_cb;
};
} // namespace mbed

View File

@ -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<void(nsapi_event_t, intptr_t)> 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