Skip to content

Commit 87daf8f

Browse files
authored
Merge pull request #7969 from jarvte/cellu_multiple_context
Cellular: CellularDevice:get_default_instance() implemented
2 parents d02cd70 + 8f4acc1 commit 87daf8f

21 files changed

+301
-238
lines changed

features/cellular/easy_cellular/CellularConnectionFSM.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#endif
2525
#include "CellularLog.h"
2626
#include "CellularCommon.h"
27+
#include "CellularDevice.h"
28+
#include "CellularUtil.h"
2729

2830
// timeout to wait for AT responses
2931
#define TIMEOUT_POWER_ON (1*1000)
@@ -42,7 +44,7 @@ namespace mbed {
4244
CellularConnectionFSM::CellularConnectionFSM() :
4345
_serial(0), _state(STATE_INIT), _next_state(_state), _status_callback(0), _event_status_cb(0), _network(0), _power(0), _sim(0),
4446
_queue(8 * EVENTS_EVENT_SIZE), _queue_thread(0), _cellularDevice(0), _retry_count(0), _event_timeout(-1),
45-
_at_queue(8 * EVENTS_EVENT_SIZE), _event_id(0), _plmn(0), _command_success(false), _plmn_network_found(false)
47+
_at_queue(0), _event_id(0), _plmn(0), _command_success(false), _plmn_network_found(false)
4648
{
4749
memset(_sim_pin, 0, sizeof(_sim_pin));
4850
#if MBED_CONF_CELLULAR_RANDOM_MAX_START_DELAY == 0
@@ -82,12 +84,20 @@ void CellularConnectionFSM::stop()
8284
_queue_thread = NULL;
8385
}
8486

85-
delete _cellularDevice;
86-
_cellularDevice = NULL;
87-
// _cellularDevice closes all interfaces in destructor
88-
_power = NULL;
89-
_network = NULL;
90-
_sim = NULL;
87+
if (_power) {
88+
_cellularDevice->close_power();
89+
_power = NULL;
90+
}
91+
92+
if (_network) {
93+
_cellularDevice->close_network();
94+
_network = NULL;
95+
}
96+
97+
if (_sim) {
98+
_cellularDevice->close_sim();
99+
_sim = NULL;
100+
}
91101

92102
_state = STATE_INIT;
93103
_next_state = _state;
@@ -96,7 +106,7 @@ void CellularConnectionFSM::stop()
96106
nsapi_error_t CellularConnectionFSM::init()
97107
{
98108
tr_info("CELLULAR_DEVICE: %s", CELLULAR_STRINGIFY(CELLULAR_DEVICE));
99-
_cellularDevice = new CELLULAR_DEVICE(_at_queue);
109+
_cellularDevice = CellularDevice::get_default_instance();
100110
if (!_cellularDevice) {
101111
stop();
102112
return NSAPI_ERROR_NO_MEMORY;
@@ -120,7 +130,8 @@ nsapi_error_t CellularConnectionFSM::init()
120130
return NSAPI_ERROR_NO_MEMORY;
121131
}
122132

123-
_at_queue.chain(&_queue);
133+
_at_queue = _cellularDevice->get_queue();
134+
_at_queue->chain(&_queue);
124135

125136
_retry_count = 0;
126137
_state = STATE_INIT;

features/cellular/easy_cellular/CellularConnectionFSM.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18-
#ifndef _CELLULAR_CONNECTION_UTIL_H
19-
#define _CELLULAR_CONNECTION_UTIL_H
18+
#ifndef _CELLULAR_CONNECTION_FSM_H
19+
#define _CELLULAR_CONNECTION_FSM_H
2020

2121
#include "CellularTargets.h"
2222
#if defined(CELLULAR_DEVICE) || defined(DOXYGEN_ONLY)
@@ -29,13 +29,11 @@
2929
#include "CellularNetwork.h"
3030
#include "CellularPower.h"
3131
#include "CellularSIM.h"
32-
#include "CellularUtil.h"
33-
34-
// modem type is defined as CELLULAR_DEVICE macro
35-
#include CELLULAR_STRINGIFY(CELLULAR_DEVICE.h)
3632

3733
namespace mbed {
3834

35+
class CellularDevice;
36+
3937
const int PIN_SIZE = 8;
4038
const int MAX_RETRY_ARRAY_SIZE = 10;
4139

@@ -209,7 +207,7 @@ class CellularConnectionFSM {
209207

210208
uint16_t _retry_timeout_array[MAX_RETRY_ARRAY_SIZE];
211209
int _retry_array_length;
212-
events::EventQueue _at_queue;
210+
events::EventQueue *_at_queue;
213211
char _st_string[20];
214212
int _event_id;
215213
const char *_plmn;
@@ -221,4 +219,4 @@ class CellularConnectionFSM {
221219

222220
#endif // CELLULAR_DEVICE || DOXYGEN
223221

224-
#endif /* _CELLULAR_CONNECTION_UTIL_H */
222+
#endif // _CELLULAR_CONNECTION_FSM_H

features/cellular/easy_cellular/EasyCellularConnection.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#endif
2424

2525
#include "CellularConnectionFSM.h"
26-
#include "CellularUtil.h"
26+
#include "CellularDevice.h"
2727

2828
#include "EasyCellularConnection.h"
2929
#include "CellularLog.h"

features/cellular/framework/API/CellularDevice.h

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,22 @@
1818
#ifndef CELLULAR_DEVICE_H_
1919
#define CELLULAR_DEVICE_H_
2020

21-
#include "FileHandle.h"
21+
#include "CellularTargets.h"
22+
#include "EventQueue.h"
23+
#include "nsapi_types.h"
24+
#include "PlatformMutex.h"
2225

23-
#include "CellularSIM.h"
24-
#include "CellularNetwork.h"
25-
#include "CellularSMS.h"
26-
#include "CellularPower.h"
27-
#include "CellularInformation.h"
28-
#include "NetworkStack.h"
26+
class NetworkStack;
2927

3028
namespace mbed {
3129

30+
class CellularPower;
31+
class CellularSMS;
32+
class CellularSIM;
33+
class CellularInformation;
34+
class CellularNetwork;
35+
class FileHandle;
36+
3237
/**
3338
* Class CellularDevice
3439
*
@@ -37,6 +42,24 @@ namespace mbed {
3742
*/
3843
class CellularDevice {
3944
public:
45+
46+
/** Return singleton instance of CellularDevice if CELLULAR_DEVICE is defined. If CELLULAR_DEVICE is not
47+
* defined then returns NULL. Implementation is marked as weak.
48+
*
49+
* @return CellularDevice* instance if any
50+
*/
51+
static CellularDevice *get_default_instance();
52+
53+
/** Get event queue that can be chained to main event queue. EventQueue is created in get_default_instance() or
54+
* given to CELLULAR_DEVICE (for example TELIT_HE910 class).
55+
* @return event queue
56+
*/
57+
virtual events::EventQueue *get_queue() const;
58+
59+
/** Default constructor
60+
*/
61+
CellularDevice();
62+
4063
/** virtual Destructor
4164
*/
4265
virtual ~CellularDevice() {}
@@ -124,6 +147,13 @@ class CellularDevice {
124147
* @return 0 on success
125148
*/
126149
virtual nsapi_error_t init_module(FileHandle *fh) = 0;
150+
151+
protected:
152+
int _network_ref_count;
153+
int _sms_ref_count;
154+
int _power_ref_count;
155+
int _sim_ref_count;
156+
int _info_ref_count;
127157
};
128158

129159
} // namespace mbed

0 commit comments

Comments
 (0)