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  void stop();
226 
227  /** Get the current FileHandle item used when communicating with the modem.
228  *
229  * @return reference to FileHandle
230  */
231  FileHandle &get_file_handle() const;
232 
233  /** Get event queue that can be chained to main event queue.
234  * @return event queue
235  */
236  virtual events::EventQueue *get_queue();
237 
238  /** Set the pin code for SIM card
239  *
240  * @param sim_pin PIN for the SIM card
241  */
242  void set_sim_pin(const char *sim_pin);
243 
244  /** Plmn to use when registering to cellular network.
245  * If plmn is set, then registering is forced to this plmn. If plmn is not set, then automatic
246  * registering is used when registering to a cellular network. It doesn't start any operations.
247  *
248  * @param plmn plmn used when registering to cellular network
249  */
250  void set_plmn(const char *plmn);
251 
252  /** Start the interface
253  *
254  * Initializes the modem for communication.
255  * API is asynchronous. Application can get results from CellularContext callback, which is set
256  * with attach(...), or callback, which is set by attach(...), in this class.
257  *
258  * @return NSAPI_ERROR_OK on success
259  * NSAPI_ERROR_NO_MEMORY on case of memory failure
260  */
262 
263  /** Start the interface
264  *
265  * Attempts to open the sim.
266  * API is asynchronous. Application can get results from CellularContext callback, which is set
267  * with attach(...), or callback, which is set by attach(...), in this class.
268  *
269  * @return NSAPI_ERROR_OK on success
270  * NSAPI_ERROR_NO_MEMORY on case of memory failure
271  */
273 
274  /** Start the interface
275  *
276  * Attempts to register the device to cellular network.
277  * API is asynchronous. Application can get results from CellularContext callback, which is set
278  * with attach(...), or callback, which is set by attach(...), in this class.
279  *
280  * @return NSAPI_ERROR_OK on success
281  * NSAPI_ERROR_NO_MEMORY on case of memory failure
282  */
284 
285  /** Start the interface
286  *
287  * Attempts to attach the device to cellular network.
288  * API is asynchronous. Application can get results from CellularContext callback, which is set
289  * with attach(...), or callback, which is set by attach(...), in this class.
290  *
291  * @return NSAPI_ERROR_OK on success
292  * NSAPI_ERROR_NO_MEMORY on case of memory failure
293  */
295 
296  /** Register callback for status reporting.
297  *
298  * The specified status callback function will be called on the network and cellular device status changes.
299  * The parameters on the callback are the event type and event-type dependent reason parameter.
300  *
301  * @remark deleting CellularDevice/CellularContext in callback not allowed.
302  * @remark Allocating/adding lots of traces not recommended as callback is called mostly from State machines thread which
303  * is now 2048. You can change to main thread for example via EventQueue.
304  *
305  * @param status_cb The callback for status changes.
306  */
307  void attach(Callback<void(nsapi_event_t, intptr_t)> status_cb);
308 
309  /** Create new CellularNetwork interface.
310  *
311  * @param fh file handle used in communication to modem. This can be, for example, UART handle. If null, then the default
312  * file handle is used.
313  * @return New instance of interface CellularNetwork.
314  */
315  virtual CellularNetwork *open_network(FileHandle *fh = NULL) = 0;
316 
317  /** Create new CellularSMS interface.
318  *
319  * @param fh file handle used in communication to modem. This can be, for example, UART handle. If null, then the default
320  * file handle is used.
321  * @return New instance of interface CellularSMS.
322  */
323  virtual CellularSMS *open_sms(FileHandle *fh = NULL) = 0;
324 
325  /** Create new CellularInformation interface.
326  *
327  * @param fh file handle used in communication to modem. This can be, for example, UART handle. If null, then the default
328  * file handle is used.
329  * @return New instance of interface CellularInformation.
330  */
331  virtual CellularInformation *open_information(FileHandle *fh = NULL) = 0;
332 
333  /** Closes the opened CellularNetwork by deleting the CellularNetwork instance.
334  */
335  virtual void close_network() = 0;
336 
337  /** Closes the opened CellularSMS by deleting the CellularSMS instance.
338  */
339  virtual void close_sms() = 0;
340 
341  /** Closes the opened CellularInformation by deleting the CellularInformation instance.
342  */
343  virtual void close_information() = 0;
344 
345  /** Set the default response timeout.
346  *
347  * @remark CellularStateMachine timeouts for all states are also changed to `timeout`.
348  *
349  * @param timeout milliseconds to wait response from modem
350  */
351  virtual void set_timeout(int timeout) = 0;
352 
353  /** Set an array of timeouts to wait before CellularStateMachine retries after failure.
354  * To disable retry behavior completely use `set_retry_timeout_array(NULL, 0)`.
355  * CellularContext callback event `cell_callback_data_t.final_try` indicates true when all retries have failed.
356  *
357  * @remark Use `set_retry_timeout_array` for CellularStateMachine to wait before it retries again after failure,
358  * this is useful to send repetitive requests when don't know exactly when modem is ready to accept requests.
359  * Use `set_timeout` for timeout how long to wait for a response from modem for each request,
360  * this is useful if modem can accept requests but processing takes long time before sending response.
361  *
362  * @param timeout timeout array using seconds
363  * @param array_len length of the array
364  */
365  void set_retry_timeout_array(const uint16_t timeout[], int array_len);
366 
367  /** Turn modem debug traces on
368  *
369  * @param on set true to enable debug traces
370  */
371  virtual void modem_debug_on(bool on) = 0;
372 
373  /** Initialize cellular device must be called right after the module is ready.
374  *
375  * For example, when multiple cellular modules are supported in a single driver this function
376  * detects and adapts to an actual module at runtime.
377  *
378  * @remark CellularStateMachine calls soft_power_on and init repeatedly when starting to connect
379  * until the modem responds.
380  *
381  * @return NSAPI_ERROR_OK on success
382  * NSAPI_ERROR_NO_MEMORY on case of memory failure
383  * NSAPI_ERROR_UNSUPPORTED if current cellular module type is not detected
384  * NSAPI_ERROR_DEVICE_ERROR if model information could not be read
385  *
386  */
387  virtual nsapi_error_t init() = 0;
388 
389  /** Shutdown cellular device to minimum functionality.
390  *
391  * Actual functionality is modem specific, for example UART may is not be responsive without
392  * explicit wakeup signal (such as RTS) after shutdown.
393  *
394  * @remark You must call shutdown before power off to prepare the modem and to quit cellular network.
395  *
396  * @return NSAPI_ERROR_OK on success
397  * NSAPI_ERROR_DEVICE_ERROR on failure
398  */
399  virtual nsapi_error_t shutdown();
400 
401  /** Check whether the device is ready to accept commands.
402  *
403  * @return NSAPI_ERROR_OK on success
404  * NSAPI_ERROR_DEVICE_ERROR on failure
405  */
406  virtual nsapi_error_t is_ready() = 0;
407 
408  /** Set callback function to listen when device is ready.
409  *
410  * @param callback function to call on device ready, or NULL to remove callback.
411  */
412  virtual void set_ready_cb(Callback<void()> callback) = 0;
413 
414  /** Set power save mode
415  *
416  * @remark See 3GPP TS 27.007 PSM for details
417  *
418  * @param periodic_time in seconds to enable power save, or zero to disable
419  * @param active_time in seconds to wait before entering power save mode
420  *
421  * @return NSAPI_ERROR_OK on success
422  * NSAPI_ERROR_DEVICE_ERROR on failure
423  */
424  virtual nsapi_error_t set_power_save_mode(int periodic_time, int active_time = 0) = 0;
425 
426  /** Get the linked list of CellularContext instances
427  *
428  * @return Pointer to first item in linked list
429  */
430  virtual CellularContext *get_context_list() const;
431 
432  /** Get the current ATHandler instance in use for debug purposes etc.
433  * Once use has been finished call to release_at_handler() has to be made
434  *
435  * @return Pointer to the ATHandler in use
436  */
437  virtual ATHandler *get_at_handler() = 0;
438 
439  /** Release the ATHandler taken into use with get_at_handler()
440  *
441  * @param at_handler
442  * @return NSAPI_ERROR_OK on success, NSAPI_ERROR_PARAMETER on failure
443  */
444  virtual nsapi_error_t release_at_handler(ATHandler *at_handler) = 0;
445 
446 protected:
447  friend class AT_CellularNetwork;
448  friend class AT_CellularContext;
449  friend class CellularContext;
450 
451  /** Get the retry array from the CellularStateMachine. Array is used in retry logic.
452  * Array contains seconds and retry logic uses those second to wait before trying again.
453  *
454  * @param timeout timeout array containing seconds for retry logic. Must have space for
455  * CELLULAR_RETRY_ARRAY_SIZE (defined in CellularCommon.h)
456  * @param array_len length of the timeout array on return
457  */
458  void get_retry_timeout_array(uint16_t *timeout, int &array_len) const;
459 
460  /** Cellular callback to be attached to Network and CellularStateMachine classes.
461  * CellularContext calls this when in PPP mode to provide network changes.
462  * This method will broadcast to every interested classes:
463  * CellularContext (might be many) and CellularStateMachine if available.
464  */
465  virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr, CellularContext *ctx = NULL);
466  void stm_callback(nsapi_event_t ev, intptr_t ptr);
467  int _network_ref_count;
468  int _sms_ref_count;
469  int _info_ref_count;
470  FileHandle *_fh;
471  events::EventQueue _queue;
472  CellularStateMachine *_state_machine;
473 private:
474  nsapi_error_t start_state_machine(CellularStateMachine::CellularState target_state);
475  nsapi_error_t create_state_machine();
476 
477  CellularNetwork *_nw;
478  char _sim_pin[MAX_PIN_SIZE + 1];
479  char _plmn[MAX_PLMN_SIZE + 1];
480  PlatformMutex _mutex;
482 };
483 
484 /**
485  * @}
486  */
487 
488 } // namespace mbed
489 
490 #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 ~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.
Class CellularSMS.
Definition: CellularSMS.h:48
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:66
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.
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.