Mbed OS Reference
Loading...
Searching...
No Matches
CellularDevice.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, Arm Limited and affiliates.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef CELLULAR_DEVICE_H_
19#define CELLULAR_DEVICE_H_
20
21#include "CellularStateMachine.h"
22#include "Callback.h"
23#include "ATHandler.h"
24#include "PinNames.h"
25
26#ifdef MBED_CONF_RTOS_PRESENT
27#include "rtos/Thread.h"
28#endif // MBED_CONF_RTOS_PRESENT
29
30/** @file CellularDevice.h
31 * @brief Class CellularDevice
32 *
33 */
34namespace mbed {
35
36class CellularSMS;
37class CellularInformation;
38class CellularNetwork;
39class CellularContext;
40
41const int MAX_PIN_SIZE = 8;
42const int MAX_PLMN_SIZE = 16;
43const int MAX_SIM_READY_WAITING_TIME = 30;
44
45/**
46 * @addtogroup Cellular
47 * @{
48 */
49
50/**
51 * Class CellularDevice
52 *
53 * An abstract interface that defines opening and closing of cellular interfaces.
54 * You can delete or close opened interfaces only through this class.
55 */
57public:
58 /* enumeration for possible SIM states */
59 enum SimState {
60 SimStateReady = 0,
61 SimStatePinNeeded,
62 SimStatePukNeeded,
63 SimStateUnknown
64 };
65
66 /** Returns singleton instance of CellularDevice, if Mbed target board has a supported
67 * onboard modem, or provide-default is defined for a cellular driver in JSON configuration
68 * files. Otherwise returns NULL. See NetworkInterface::get_default_instance for details.
69 *
70 * @remark Application may override this (non-weak) default implementation.
71 *
72 * @return default CellularDevice, NULL if not defined
73 */
75
76 /** Return target onboard instance of CellularDevice
77 *
78 * @remark Mbed OS target shall override (non-weak) this function for an onboard modem.
79 *
80 * @return CellularDevice* instance, NULL if not defined
81 */
83
84 /** Default constructor
85 */
87
88 /** virtual Destructor
89 */
90 virtual ~CellularDevice();
91
92public: //Virtual functions
93
94 /** Shutdown cellular device to minimum functionality.
95 *
96 * Actual functionality is modem specific, for example UART may is not be responsive without
97 * explicit wakeup signal (such as RTS) after shutdown.
98 *
99 * @remark You must call shutdown before power off to prepare the modem and to quit cellular network.
100 *
101 * @return NSAPI_ERROR_OK on success
102 * NSAPI_ERROR_DEVICE_ERROR on failure
103 */
105
106 /** Get event queue that can be chained to main event queue.
107 * @return event queue
108 */
110
111protected: //Virtual functions
112 /** Cellular callback to be attached to Network and CellularStateMachine classes.
113 * CellularContext calls this when in PPP mode to provide network changes.
114 * This method will broadcast to every interested classes:
115 * CellularContext (might be many) and CellularStateMachine if available.
116 */
117 virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL);
118
119public: //Pure virtual functions
120
121 /** Clear modem to a default initial state
122 *
123 * Clear persistent user data from the modem, such as PDP contexts.
124 *
125 * @pre All open network services on modem, such as contexts and sockets, must be closed.
126 * @post Modem power off/on may be needed to clear modem's runtime state.
127 * @remark CellularStateMachine calls this on connect when `cellular.clear-on-connect: true`.
128 *
129 * @return NSAPI_ERROR_OK on success, otherwise modem may be need power cycling
130 */
131 virtual nsapi_error_t clear() = 0;
132
133
134 /** Get the linked list of CellularContext instances
135 *
136 * @return Pointer to first item in linked list
137 */
138 virtual CellularContext *get_context_list() const = 0;
139
140 /** Sets the modem up for powering on
141 * This is equivalent to plugging in the device, i.e., attaching power and serial port.
142 * In general, hard_power_on and soft_power_on provides a simple hardware abstraction layer
143 * on top of the modem drivers written for Mbed OS; they can be overridden
144 * in a derived class to perform custom power controls in a particular board configuration.
145 * In many boards this will be a no-op if there is no separate power supply control circuitry.
146 *
147 * @remark CellularStateMachine calls hard_power_on at first until successful,
148 * then soft_power_on and init until the modem responds.
149 * If you are not using CellularStateMachine then you need to call these functions yourself.
150 *
151 * @post You must call soft_power_on to power on the modem after calling hard_power_on.
152 *
153 * @return NSAPI_ERROR_OK on success
154 */
156
157 /** Sets the modem in unplugged state
158 *
159 * This is equivalent to pulling the plug off of the device, i.e.,
160 * detaching power and serial port.
161 *
162 * This puts the modem in the lowest power state.
163 *
164 * @remark CellularStateMachine disconnect or destruct does not shutdown or power off the modem,
165 * but you need to do that yourself.
166 *
167 * @pre You must call soft_power_off to power off the modem before calling hard_power_off.
168 *
169 * @return NSAPI_ERROR_OK on success
170 */
172
173 /** Powers up the modem
174 *
175 * This is equivalent to pressing the "power button" to activate or reset the modem
176 * and usually implemented as a short pulse on a dedicated GPIO signal.
177 * It is expected to be present to make it possible to reset the modem.
178 * The driver may repeat this if the modem is not responsive to AT commands.
179 *
180 * @remark CellularStateMachine calls this when requested to connect.
181 * If you are not using CellularStateMachine then you need to call this function yourself.
182 *
183 * @post You must call init to setup the modem.
184 *
185 * @return NSAPI_ERROR_OK on success
186 */
188
189 /** Powers down the modem
190 *
191 * This is equivalent to turning off the modem by button press.
192 *
193 * @remark CellularStateMachine disconnect or destruct does not shutdown or power off the modem,
194 * but you need to do that yourself.
195 *
196 * @pre You must call shutdown to prepare the modem for power off.
197 *
198 * @return NSAPI_ERROR_OK on success
199 */
201
202 /** Open the SIM card by setting the pin code for SIM.
203 *
204 * @param sim_pin PIN for the SIM card
205 * @return NSAPI_ERROR_OK on success
206 * NSAPI_ERROR_PARAMETER if sim_pin is null and sim is not ready
207 * NSAPI_ERROR_DEVICE_ERROR on failure
208 */
209 virtual nsapi_error_t set_pin(const char *sim_pin) = 0;
210
211 /** Get SIM card's state
212 *
213 * @param state current state of SIM
214 * @return NSAPI_ERROR_OK on success
215 * NSAPI_ERROR_DEVICE_ERROR on failure
216 */
217 virtual nsapi_error_t get_sim_state(SimState &state) = 0;
218
219 /** Creates a new CellularContext interface.
220 *
221 *
222 * @param apn access point to use with context, can be null.
223 * @param cp_req flag indicating if EPS control plane optimization is required
224 * @param nonip_req flag indicating if this context is required to be Non-IP
225 *
226 * @return new instance of class CellularContext or NULL in case of failure
227 *
228 */
229 virtual CellularContext *create_context(const char *apn = NULL, bool cp_req = false, bool nonip_req = false) = 0;
230
231 /** Deletes the given CellularContext instance
232 *
233 * @param context CellularContext to delete
234 */
235 virtual void delete_context(CellularContext *context) = 0;
236
237 /** Turn modem debug traces on
238 *
239 * @param on set true to enable debug traces
240 */
241 virtual void modem_debug_on(bool on) = 0;
242
243 /** Initialize cellular device must be called right after the module is ready.
244 *
245 * For example, when multiple cellular modules are supported in a single driver this function
246 * detects and adapts to an actual module at runtime.
247 *
248 * @remark CellularStateMachine calls soft_power_on and init repeatedly when starting to connect
249 * until the modem responds.
250 *
251 * @return NSAPI_ERROR_OK on success
252 * NSAPI_ERROR_NO_MEMORY on case of memory failure
253 * NSAPI_ERROR_UNSUPPORTED if current cellular module type is not detected
254 * NSAPI_ERROR_DEVICE_ERROR if model information could not be read
255 *
256 */
257 virtual nsapi_error_t init() = 0;
258
259 /** Check whether the device is ready to accept commands.
260 *
261 * @return NSAPI_ERROR_OK on success
262 * NSAPI_ERROR_DEVICE_ERROR on failure
263 */
264 virtual nsapi_error_t is_ready() = 0;
265
266 /** Set callback function to listen when device is ready.
267 *
268 * @param callback function to call on device ready, or NULL to remove callback.
269 */
270 virtual void set_ready_cb(Callback<void()> callback) = 0;
271
272 /** Set power save mode
273 *
274 * @remark See 3GPP TS 27.007 PSM for details
275 *
276 * @param periodic_time in seconds to enable power save, or zero to disable
277 * @param active_time in seconds to wait before entering power save mode
278 *
279 * @return NSAPI_ERROR_OK on success
280 * NSAPI_ERROR_DEVICE_ERROR on failure
281 */
282 virtual nsapi_error_t set_power_save_mode(int periodic_time, int active_time = 0) = 0;
283
284 /** Get the current ATHandler instance in use for debug purposes etc.
285 *
286 * @return Pointer to the ATHandler in use, NULL if device is non-AT -device.
287 */
288 virtual ATHandler *get_at_handler() = 0;
289
290 /** Sets cellular modem to given baud rate
291 *
292 * @param baud_rate
293 * @return NSAPI_ERROR_OK on success, NSAPI_ERROR_DEVICE_ERROR on failure
294 */
295 virtual nsapi_error_t set_baud_rate(int baud_rate) = 0;
296
297 /** Create new CellularNetwork interface.
298 *
299 * @return New instance of interface CellularNetwork.
300 */
302
303#if MBED_CONF_CELLULAR_USE_SMS || defined(DOXYGEN_ONLY)
304 /** Create new CellularSMS interface.
305 *
306 * @return New instance of interface CellularSMS.
307 */
308 virtual CellularSMS *open_sms() = 0;
309
310 /** Closes the opened CellularSMS by deleting the CellularSMS instance.
311 */
312 virtual void close_sms() = 0;
313
314#endif // MBED_CONF_CELLULAR_USE_SMS
315
316 /** Create new CellularInformation interface.
317 *
318 * @return New instance of interface CellularInformation.
319 */
321
322 /** Closes the opened CellularNetwork by deleting the CellularNetwork instance.
323 */
324 virtual void close_network() = 0;
325
326 /** Closes the opened CellularInformation by deleting the CellularInformation instance.
327 */
328 virtual void close_information() = 0;
329
330 /** Set the default response timeout.
331 *
332 * @remark CellularStateMachine timeouts for all states are also changed to `timeout`.
333 *
334 * @param timeout milliseconds to wait response from modem
335 */
336 virtual void set_timeout(int timeout) = 0;
337
338
339public: //Common functions
340
341
342 /** Set the pin code for SIM card
343 *
344 * @param sim_pin PIN for the SIM card
345 */
346 void set_sim_pin(const char *sim_pin);
347
348 /** Plmn to use when registering to cellular network.
349 * If plmn is set, then registering is forced to this plmn. If plmn is not set, then automatic
350 * registering is used when registering to a cellular network. It doesn't start any operations.
351 *
352 * @param plmn plmn used when registering to cellular network
353 */
354 void set_plmn(const char *plmn);
355
356 /** Start the interface
357 *
358 * Initializes the modem for communication.
359 * API is asynchronous. Application can get results from CellularContext callback, which is set
360 * with attach(...), or callback, which is set by attach(...), in this class.
361 *
362 * @return NSAPI_ERROR_OK on success
363 * NSAPI_ERROR_NO_MEMORY on case of memory failure
364 */
366
367 /** Start the interface
368 *
369 * Attempts to open the sim.
370 * API is asynchronous. Application can get results from CellularContext callback, which is set
371 * with attach(...), or callback, which is set by attach(...), in this class.
372 *
373 * @return NSAPI_ERROR_OK on success
374 * NSAPI_ERROR_NO_MEMORY on case of memory failure
375 */
377
378 /** Start the interface
379 *
380 * Attempts to register the device to cellular network.
381 * API is asynchronous. Application can get results from CellularContext callback, which is set
382 * with attach(...), or callback, which is set by attach(...), in this class.
383 *
384 * @return NSAPI_ERROR_OK on success
385 * NSAPI_ERROR_NO_MEMORY on case of memory failure
386 */
388
389 /** Start the interface
390 *
391 * Attempts to attach the device to cellular network.
392 * API is asynchronous. Application can get results from CellularContext callback, which is set
393 * with attach(...), or callback, which is set by attach(...), in this class.
394 *
395 * @return NSAPI_ERROR_OK on success
396 * NSAPI_ERROR_NO_MEMORY on case of memory failure
397 */
399
400 /** Register callback for status reporting.
401 *
402 * The specified status callback function will be called on the network and cellular device status changes.
403 * The parameters on the callback are the event type and event-type dependent reason parameter.
404 *
405 * @remark deleting CellularDevice/CellularContext in callback not allowed.
406 * @remark Allocating/adding lots of traces not recommended as callback is called mostly from State machines thread which
407 * is now 2048. You can change to main thread for example via EventQueue.
408 *
409 * @param status_cb The callback for status changes.
410 */
411 void attach(Callback<void(nsapi_event_t, intptr_t)> status_cb);
412
413 /** Set an array of timeouts to wait before CellularStateMachine retries after failure.
414 * To disable retry behavior completely use `set_retry_timeout_array(NULL, 0)`.
415 * CellularContext callback event `cell_callback_data_t.final_try` indicates true when all retries have failed.
416 *
417 * @remark Use `set_retry_timeout_array` for CellularStateMachine to wait before it retries again after failure,
418 * this is useful to send repetitive requests when don't know exactly when modem is ready to accept requests.
419 * Use `set_timeout` for timeout how long to wait for a response from modem for each request,
420 * this is useful if modem can accept requests but processing takes long time before sending response.
421 *
422 * @param timeout timeout array using seconds
423 * @param array_len length of the array
424 */
425 void set_retry_timeout_array(const uint16_t timeout[], int array_len);
426
427protected: //Common functions
428 friend class AT_CellularNetwork;
429 friend class AT_CellularContext;
430 friend class CellularContext;
431
432 /** Get the retry array from the CellularStateMachine. Array is used in retry logic.
433 * Array contains seconds and retry logic uses those second to wait before trying again.
434 *
435 * @param timeout timeout array containing seconds for retry logic. Must have space for
436 * CELLULAR_RETRY_ARRAY_SIZE (defined in CellularCommon.h)
437 * @param array_len length of the timeout array on return
438 */
439 void get_retry_timeout_array(uint16_t *timeout, int &array_len) const;
440
441private: //Common functions
442 nsapi_error_t start_state_machine(CellularStateMachine::CellularState target_state);
443 nsapi_error_t create_state_machine();
444 void stm_callback(nsapi_event_t ev, intptr_t ptr);
445
446protected: //Member variables
447 int _network_ref_count;
448#if MBED_CONF_CELLULAR_USE_SMS
449 int _sms_ref_count;
450#endif // MBED_CONF_CELLULAR_USE_SMS
451 int _info_ref_count;
452 events::EventQueue _queue;
453 CellularStateMachine *_state_machine;
454 Callback<void(nsapi_event_t, intptr_t)> _status_cb;
455
456private: //Member variables
457 CellularNetwork *_nw;
458 char _sim_pin[MAX_PIN_SIZE + 1];
459 char _plmn[MAX_PLMN_SIZE + 1];
460 rtos::Mutex _mutex;
461
462#ifdef MBED_CONF_RTOS_PRESENT
463 rtos::Thread _queue_thread;
464#endif
465};
466
467/**
468 * @}
469 */
470
471} // namespace mbed
472
473#endif // CELLULAR_DEVICE_H_
EventQueue.
Definition: EventQueue.h:62
Class AT_CellularNetwork.
Class for sending AT commands and parsing AT responses.
Definition: ATHandler.h:68
Callback class based on template specialization.
Definition: Callback.h:53
CellularContext is CellularInterface/NetworkInterface with extensions for cellular connectivity.
Class CellularDevice.
virtual void close_information()=0
Closes the opened CellularInformation by deleting the CellularInformation instance.
virtual nsapi_error_t shutdown()
Shutdown cellular device to minimum functionality.
nsapi_error_t set_device_ready()
Start the interface.
virtual void close_sms()=0
Closes the opened CellularSMS by deleting the CellularSMS instance.
void attach(Callback< void(nsapi_event_t, intptr_t)> status_cb)
Register callback for status reporting.
virtual ~CellularDevice()
virtual Destructor
virtual CellularSMS * open_sms()=0
Create new CellularSMS interface.
virtual void set_ready_cb(Callback< void()> callback)=0
Set callback function to listen when device is ready.
virtual nsapi_error_t hard_power_off()=0
Sets the modem in unplugged state.
void set_sim_pin(const char *sim_pin)
Set the pin code for SIM card.
static CellularDevice * get_target_default_instance()
Return target onboard instance of CellularDevice.
CellularDevice()
Default constructor.
virtual void modem_debug_on(bool on)=0
Turn modem debug traces on.
virtual CellularNetwork * open_network()=0
Create new CellularNetwork interface.
virtual nsapi_error_t init()=0
Initialize cellular device must be called right after the module is ready.
virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx=NULL)
Cellular callback to be attached to Network and CellularStateMachine classes.
virtual nsapi_error_t clear()=0
Clear modem to a default initial state.
void set_plmn(const char *plmn)
Plmn to use when registering to cellular network.
virtual nsapi_error_t hard_power_on()=0
Sets the modem up for powering on This is equivalent to plugging in the device, i....
virtual void delete_context(CellularContext *context)=0
Deletes the given CellularContext instance.
virtual nsapi_error_t set_power_save_mode(int periodic_time, int active_time=0)=0
Set power save mode.
void get_retry_timeout_array(uint16_t *timeout, int &array_len) const
Get the retry array from the CellularStateMachine.
virtual void close_network()=0
Closes the opened CellularNetwork by deleting the CellularNetwork instance.
nsapi_error_t set_sim_ready()
Start the interface.
virtual nsapi_error_t set_pin(const char *sim_pin)=0
Open the SIM card by setting the pin code for SIM.
virtual CellularContext * get_context_list() const =0
Get the linked list of CellularContext instances.
static CellularDevice * get_default_instance()
Returns singleton instance of CellularDevice, if Mbed target board has a supported onboard modem,...
virtual events::EventQueue * get_queue()
Get event queue that can be chained to main event queue.
virtual CellularContext * create_context(const char *apn=NULL, bool cp_req=false, bool nonip_req=false)=0
Creates a new CellularContext interface.
virtual nsapi_error_t soft_power_on()=0
Powers up the modem.
virtual nsapi_error_t is_ready()=0
Check whether the device is ready to accept commands.
nsapi_error_t register_to_network()
Start the interface.
virtual nsapi_error_t get_sim_state(SimState &state)=0
Get SIM card's state.
nsapi_error_t attach_to_network()
Start the interface.
virtual void set_timeout(int timeout)=0
Set the default response timeout.
virtual CellularInformation * open_information()=0
Create new CellularInformation interface.
virtual nsapi_error_t set_baud_rate(int baud_rate)=0
Sets cellular modem to given baud rate.
void set_retry_timeout_array(const uint16_t timeout[], int array_len)
Set an array of timeouts to wait before CellularStateMachine retries after failure.
virtual ATHandler * get_at_handler()=0
Get the current ATHandler instance in use for debug purposes etc.
virtual nsapi_error_t soft_power_off()=0
Powers down the modem.
Class CellularInformation.
An abstract interface for connecting to a network and getting information from it.
CellularStateMachine class.
CellularState
Cellular connection states.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
The Thread class allow defining, creating, and controlling thread functions in the system.
Definition: Thread.h:92
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition: Callback.h:678