Skip to content

Commit 76672db

Browse files
author
Teppo Järvelin
committed
Cellular: add Callback functionality to CellularDevice.
1 parent a3f1ad7 commit 76672db

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

features/cellular/framework/API/CellularDevice.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "CellularTargets.h"
2222
#include "CellularStateMachine.h"
23+
#include "Callback.h"
2324

2425
namespace mbed {
2526

@@ -145,6 +146,19 @@ class CellularDevice {
145146
*/
146147
nsapi_error_t attach_to_network();
147148

149+
/** Register callback for status reporting.
150+
*
151+
* The specified status callback function will be called on the network and cellular device status changes.
152+
* The parameters on the callback are the event type and event-type dependent reason parameter.
153+
*
154+
* @remark deleting CellularDevice/CellularContext in callback not allowed.
155+
* @remark application should not attach to this function if using CellularContext::attach as it will contain the
156+
* same information.
157+
*
158+
* @param status_cb The callback for status changes.
159+
*/
160+
void attach(Callback<void(nsapi_event_t, intptr_t)> status_cb);
161+
148162
/** Create new CellularNetwork interface.
149163
*
150164
* @param fh file handle used in communication to modem. Can be for example UART handle. If null then the default
@@ -258,6 +272,7 @@ class CellularDevice {
258272
char _sim_pin[MAX_PIN_SIZE + 1];
259273
char _plmn[MAX_PLMN_SIZE + 1];
260274
PlatformMutex _mutex;
275+
Callback<void(nsapi_event_t, intptr_t)> _status_cb;
261276
};
262277

263278
} // namespace mbed

features/cellular/framework/device/CellularDevice.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ MBED_WEAK CellularDevice *CellularDevice::get_default_instance()
5151
#endif // CELLULAR_DEVICE
5252

5353
CellularDevice::CellularDevice(FileHandle *fh) : _network_ref_count(0), _sms_ref_count(0), _power_ref_count(0), _sim_ref_count(0),
54-
_info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0)
54+
_info_ref_count(0), _fh(fh), _queue(5 * EVENTS_EVENT_SIZE), _state_machine(0), _nw(0), _status_cb(0)
5555
{
5656
set_sim_pin(NULL);
5757
set_plmn(NULL);
@@ -158,6 +158,11 @@ nsapi_error_t CellularDevice::start_state_machine(CellularStateMachine::Cellular
158158
return err;
159159
}
160160

161+
void CellularDevice::attach(Callback<void(nsapi_event_t, intptr_t)> status_cb)
162+
{
163+
_status_cb = status_cb;
164+
}
165+
161166
void CellularDevice::cellular_callback(nsapi_event_t ev, intptr_t ptr)
162167
{
163168
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)
200205
curr->cellular_callback(ev, ptr);
201206
curr = curr->_next;
202207
}
208+
209+
// forward to callback function if set by attach(...)
210+
if (_status_cb) {
211+
_status_cb(ev, ptr);
212+
}
203213
}
204214

205215
} // namespace mbed

0 commit comments

Comments
 (0)