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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.