Wajahat Abbas / ublox-cellular-base_PSM
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UbloxCellularBase.h Source File

UbloxCellularBase.h

00001 /* Copyright (c) 2017 ARM Limited
00002  *
00003  * Licensed under the Apache License, Version 2.0 (the "License");
00004  * you may not use this file except in compliance with the License.
00005  * You may obtain a copy of the License at
00006  *
00007  *     http://www.apache.org/licenses/LICENSE-2.0
00008  *
00009  * Unless required by applicable law or agreed to in writing, software
00010  * distributed under the License is distributed on an "AS IS" BASIS,
00011  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012  * See the License for the specific language governing permissions and
00013  * limitations under the License.
00014  */
00015 
00016 #ifndef _UBLOX_CELLULAR_BASE_
00017 #define _UBLOX_CELLULAR_BASE_
00018 
00019 #include "mbed.h"
00020 #include "mbed_toolchain.h" // for MBED_DEPRECATED
00021 #include "ATCmdParser.h"
00022 #include "FileHandle.h"
00023 
00024 /**********************************************************************
00025  * CLASSES
00026  **********************************************************************/
00027 
00028 /** UbloxCellularBase class.
00029  *
00030  *  This class provides all the base support for generic u-blox modems
00031  *  on C030 and C027 boards: module identification, power-up, network
00032  *  registration, etc.
00033  */
00034 class UbloxCellularBase {
00035 
00036 public:
00037     /** Circuit Switched network registration status (CREG Usage).
00038      * UBX-13001820 - AT Commands Example Application Note (Section 7.10.3).
00039      */
00040     typedef enum {
00041         CSD_NOT_REGISTERED_NOT_SEARCHING = 0,
00042         CSD_REGISTERED = 1,
00043         CSD_NOT_REGISTERED_SEARCHING = 2,
00044         CSD_REGISTRATION_DENIED = 3,
00045         CSD_UNKNOWN_COVERAGE = 4,
00046         CSD_REGISTERED_ROAMING = 5,
00047         CSD_SMS_ONLY = 6,
00048         CSD_SMS_ONLY_ROAMING = 7,
00049         CSD_CSFB_NOT_PREFERRED = 9
00050     } NetworkRegistrationStatusCsd;
00051 
00052     /** Packet Switched network registration status (CGREG Usage).
00053      * UBX-13001820 - AT Commands Example Application Note (Section 18.27.3).
00054      */
00055     typedef enum {
00056         PSD_NOT_REGISTERED_NOT_SEARCHING = 0,
00057         PSD_REGISTERED = 1,
00058         PSD_NOT_REGISTERED_SEARCHING = 2,
00059         PSD_REGISTRATION_DENIED = 3,
00060         PSD_UNKNOWN_COVERAGE = 4,
00061         PSD_REGISTERED_ROAMING = 5,
00062         PSD_EMERGENCY_SERVICES_ONLY = 8
00063     } NetworkRegistrationStatusPsd;
00064 
00065     /** EPS network registration status (CEREG Usage).
00066      * UBX-13001820 - AT Commands Example Application Note (Section 18.36.3).
00067      */
00068     typedef enum {
00069         EPS_NOT_REGISTERED_NOT_SEARCHING = 0,
00070         EPS_REGISTERED = 1,
00071         EPS_NOT_REGISTERED_SEARCHING = 2,
00072         EPS_REGISTRATION_DENIED = 3,
00073         EPS_UNKNOWN_COVERAGE = 4,
00074         EPS_REGISTERED_ROAMING = 5,
00075         EPS_EMERGENCY_SERVICES_ONLY = 8
00076     } NetworkRegistrationStatusEps;
00077 
00078     /** modem PSM states.
00079      *
00080      */
00081     typedef enum {
00082         AWAKE = 0,
00083         ASLEEP = 1
00084     } ModemPSMState;
00085 
00086     /** Initialise the modem, ready for use.
00087      *
00088      *  @param pin     PIN for the SIM card.
00089      *  @return        true if successful, otherwise false.
00090      */
00091     bool init(const char *pin = 0);
00092 
00093     /** Perform registration with the network.
00094      *
00095      * @return true if successful, otherwise false.
00096      */
00097     bool nwk_registration();
00098 
00099     /** True if the modem is registered for circuit
00100      * switched data, otherwise false.
00101      */
00102     bool is_registered_csd();
00103 
00104     /** True if the modem is registered for packet
00105      * switched data, otherwise false.
00106      */
00107     bool is_registered_psd();
00108 
00109     /** True if the modem is registered for enhanced
00110      * packet switched data (i.e. LTE and beyond),
00111      * otherwise false.
00112      */
00113     bool is_registered_eps();
00114 
00115     /** Perform deregistration from the network.
00116      *
00117      * @return true if successful, otherwise false.
00118      */
00119     bool nwk_deregistration();
00120 
00121     /** Put the modem into its lowest power state.
00122      */
00123     void deinit();
00124 
00125     /** Set the PIN code for the SIM card.
00126      *
00127      *  @param pin PIN for the SIM card.
00128      */
00129     void set_pin(const char *pin);
00130 
00131     /** Enable or disable SIM pin checking.
00132      *
00133      * @param enableNotDisable true if SIM PIN checking is to be enabled,
00134      *                         otherwise false.
00135      * @return                 true if successful, otherwise false.
00136      */
00137     bool sim_pin_check_enable(bool enableNotDisable);
00138 
00139     /** Change the SIM pin.
00140      *
00141      * @param new_pin the new PIN to use.
00142      * @return        true if successful, otherwise false.
00143      */
00144     bool change_sim_pin(const char *new_pin);
00145 
00146     /** Get the IMEI.
00147      *
00148      * @return true if successful, otherwise false.
00149      */
00150     MBED_DEPRECATED("This method is now replaced by const char * imei(), please use that instead")
00151     bool get_imei(char *imei_to_send, int size);
00152 
00153     /** Get the IMEI of the module.
00154      *
00155      * @return a pointer to the IMEI as a null-terminated string.
00156      */
00157     const char *imei();
00158 
00159     /** Get the Mobile Equipment ID (which may be the same as the IMEI).
00160      *
00161      * @return a pointer to the Mobile Equipment ID as a null-terminated string.
00162      */
00163     const char *meid();
00164 
00165     /** Get the IMSI of the SIM.
00166      *
00167      * @return a pointer to the IMSI as a null-terminated string.
00168      */
00169     const char *imsi();
00170 
00171     /** Get the ICCID of the SIM.
00172      *
00173      * @return a pointer to the ICCID as a null-terminated string.
00174      */
00175     const char *iccid();
00176 
00177     /** Get the RSSI.
00178      *
00179      * @return the RSSI in dBm. If it is not possible to obtain an
00180      *         RSSI reading at the time (e.g. because the modem is in
00181      *         data mode rather than AT command mode) then 0 is returned.
00182      */
00183     int rssi();
00184 
00185     /** RAT values for +URAT command
00186      * R412M only supports value 7 (CatM1), 8 (NB1), and 9 (GPRS)
00187      */
00188     typedef enum {
00189         GSM_GPRS_EGPRS = 0,
00190         GSM_UMTS = 1,
00191         UMTS = 2,
00192         URAT_LTE = 3,
00193         GSM_UMTS_LTE = 4,
00194         GSM_LTE = 5,
00195         UMTS_LTE = 6,
00196         LTE_CATM1 = 7,
00197         LTE_CATNB1 = 8,
00198         GPRS_EGPRS = 9,
00199         NOT_USED = -1
00200     } RAT;
00201 
00202 #ifdef TARGET_UBLOX_C030_R41XM
00203     /** Supported MNO profiles for SARA-R4.
00204      */
00205     typedef enum {
00206         SW_DEFAULT = 0,
00207         SIM_ICCID = 1,
00208         ATT = 2,
00209         VERIZON = 3,
00210         TELSTRA = 4,
00211         TMO = 5,
00212         CT = 6,
00213         VODAFONE = 19,
00214         TELUS = 21,
00215         DT = 31,
00216         STANDARD_EU = 100
00217     } MNOProfile;
00218 
00219     #if MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE
00220     #define DEFAULT_MNO_PROFILE     (MNOProfile)MBED_CONF_UBLOX_CELL_DEFAULT_MNO_PROFILE
00221     #else
00222     #define DEFAULT_MNO_PROFILE     SW_DEFAULT
00223     #endif
00224 
00225     /** Reads the current MNO profile from modem and sets it to user specified profile.
00226      * User can also specify profile in mbed_lib.json file and call set_mno_profile without any arguments.
00227      *
00228      * Note: MNO profile should only be set in detached state and a reboot is required for settings to take effect
00229      *
00230      * @param profile MNO profile to use
00231      * @return    true if operation was successful, false if there was an error
00232      */
00233     bool set_mno_profile(MNOProfile profile = DEFAULT_MNO_PROFILE);
00234 
00235     /** Get current MNO profile.
00236      *
00237      * @param profile pointer to variable that can hold the value for returned profile
00238      * @return    true if operation was successful, false if there was an error
00239      */
00240     bool get_mno_profile(int *profile);
00241 #endif
00242 
00243     /** Set Radio Access Technology on modem.
00244      *
00245      * Note: RAT should only be set in detached state and a reboot is required for settings to take effect
00246      *
00247      * @param selected_rat Radio Access Technology to use
00248      * @param preferred_rat Radio Access Technology to use if selected_rat is not available
00249      * @param second_preferred_rat Radio Access Technology to use if selected_rat and preferred_rat are not available
00250      *
00251      * @return        true if successful, otherwise false.
00252      */
00253     bool set_modem_rat(RAT selected_rat, RAT preferred_rat = NOT_USED, RAT second_preferred_rat = NOT_USED);
00254 
00255     /** Get last saved values for RAT using +URAT read command. Note: The current selected RAT is indicated by DeviceInfo.rat
00256      *
00257      * @param selected_rat pointer to variable that can hold the value for selected_rat
00258      * @param preferred_rat pointer to variable that can hold the value for preferred_rat
00259      * @param second_preferred_rat pointer to variable that can hold the value for second_preferred_rat
00260      *
00261      * Note: NOT_USED will be returned in the variables if dual or tri modes are not enabled.
00262      *
00263      * @return        true if successful, otherwise false.
00264      */
00265     bool get_modem_rat(int *selected_rat, int *preferred_rat, int *second_preferred_rat);
00266 
00267     /** reboot the modem using AT+CFUN=15. Application should call init() or connect() before making any other API calls.
00268      *
00269      * @return        true if successful, otherwise false.
00270      */
00271     bool reboot_modem();
00272 
00273 #ifdef TARGET_UBLOX_C030_R412M
00274     /** Important: Callback function is executed in context of AT parser so a user should not issue any AT commands from inside the callback.
00275      * It is recommended to set a flag/event/signal in callback and application can use that to wake up the modem and re-initialize it
00276      *
00277      * application callback for modem going in to PSM sleep
00278      *
00279      * @param func     callback function to be executed when modem is going in to PSM sleep
00280      * @param param    parameter to be passed to callback function.
00281      */
00282     void attach_cb_psm_going_in(Callback<void(void*)> func, void *param)
00283     {
00284         _func_psm_going_in = func;
00285         _cb_param_psm_going_in = param;
00286     }
00287 
00288     /** Important: Callback function is executed in context of AT parser so a user should not issue any AT commands from inside the callback.
00289      * It is recommended to set a flag/event/signal in callback and application can use that to wake up the modem and re-initialize it
00290      *
00291      * application callback for modem coming out of PSM sleep
00292      *
00293      * @param func     callback function to be executed when modem is coming out of PSM sleep.
00294      * @param param    parameter to be passed to callback function.
00295      */
00296     void attach_cb_psm_coming_out(Callback<void(void*)> func, void *param)
00297     {
00298         _func_psm_coming_out = func;
00299         _cb_param_psm_coming_out = param;
00300     }
00301 
00302     /** de-register the application callback for modem going in to PSM sleep
00303      *
00304      */
00305     void detach_cb_psm_going_in()
00306     {
00307         _func_psm_going_in = NULL;
00308         _cb_param_psm_going_in = NULL;
00309     }
00310 
00311     /** de-register application callback for modem coming out of PSM sleep
00312      *
00313      */
00314     void detach_cb_psm_coming_out()
00315     {
00316         _func_psm_coming_out = NULL;
00317         _cb_param_psm_coming_out = NULL;
00318     }
00319 
00320     /** Enable or disable the 3GPP PSM.
00321      *
00322      *  Note: Application should reboot the module after enabling PSM in order to enter PSM state. (reboot_modem())
00323      *  Note: Modem can be woken up by toggling the power-on signal. (wakeup_modem())
00324      *  Note: When device enters PSM, all connections(PPP, sockets) and settings that are not saved in NV memory(ATE0, CREG etc) are lost.
00325      *        host application should be prepared to re-initialize the modem and re-establish the connections.
00326      *  Note: PSM is disabled if both periodic_time and active_time are 0.
00327      *  Note: Not all variants/firmware versions support PSM URCs and in that case function will return false.
00328      *
00329      *  PSM string encoding code is borrowed from AT_CellularPower.cpp
00330      *
00331      * @param periodic_time    requested periodic TAU in seconds.
00332      * @param active_time      requested active time in seconds.
00333      * @param func             callback function to execute when modem goes to sleep
00334      * @param ptr              parameter to callback function
00335      * @return         True if successful, otherwise false.
00336      */
00337     bool set_power_saving_mode(int periodic_tau, int active_time);
00338 
00339     /** Reads the 3GPP PSM status (enabled or disabled) and returns assigned periodic tau and active time values.
00340      *
00341      * @param status           0: PSM disabled, 1: PSM enabled
00342      * @param periodic_tau     assigned periodic TAU in seconds.
00343      * @param active_time      assigned active time in seconds
00344      * @return         True if command successful, otherwise false.
00345      */
00346     bool get_power_saving_mode(int *status, int *periodic_tau, int *active_time);
00347 
00348     /** Wake up the modem from PSM. Ref to comment on set_power_saving_mode, application should call init() or connect()
00349      * before making any other API calls.
00350      */
00351     void wakeup_modem();
00352 
00353     /** True if the modem is not in PSM sleep
00354      * otherwise false.
00355      */
00356     bool is_modem_awake();
00357 #endif
00358 
00359 protected:
00360 
00361     #define OUTPUT_ENTER_KEY  "\r"
00362 
00363     #if MBED_CONF_UBLOX_CELL_GEN_DRV_AT_PARSER_BUFFER_SIZE
00364     #define AT_PARSER_BUFFER_SIZE   MBED_CONF_UBLOX_CELL_GEN_DRV_AT_PARSER_BUFFER_SIZE
00365     #else
00366     #define AT_PARSER_BUFFER_SIZE   256
00367     #endif
00368 
00369     #if MBED_CONF_UBLOX_CELL_GEN_DRV_AT_PARSER_TIMEOUT
00370     #define AT_PARSER_TIMEOUT       MBED_CONF_UBLOX_CELL_GEN_DRV_AT_PARSER_TIMEOUT
00371     #else
00372     #define AT_PARSER_TIMEOUT       8*1000 // Milliseconds
00373     #endif
00374 
00375     /** A string that would not normally be sent by the modem on the AT interface.
00376      */
00377     #define UNNATURAL_STRING "\x01"
00378 
00379     /** Supported u-blox modem variants.
00380      */
00381     typedef enum {
00382         DEV_TYPE_NONE = 0,
00383         DEV_SARA_G35,
00384         DEV_LISA_U2,
00385         DEV_LISA_U2_03S,
00386         DEV_SARA_U2,
00387         DEV_SARA_R4,
00388         DEV_LEON_G2,
00389         DEV_TOBY_L2,
00390         DEV_MPCI_L2
00391     } DeviceType;
00392 
00393     /** Network registration status.
00394      * UBX-13001820 - AT Commands Example Application Note (Section 4.1.4.5).
00395      */
00396     typedef enum {
00397        GSM = 0,
00398        COMPACT_GSM = 1,
00399        UTRAN = 2,
00400        EDGE = 3,
00401        HSDPA = 4,
00402        HSUPA = 5,
00403        HSDPA_HSUPA = 6,
00404        LTE = 7,
00405        EC_GSM_IoT = 8,
00406        E_UTRAN_NB_S1 = 9
00407     } RadioAccessNetworkType;
00408 
00409     /** Info about the modem.
00410      */
00411     typedef struct {
00412         DeviceType dev;
00413         char iccid[20 + 1];   //!< Integrated Circuit Card ID.
00414         char imsi[15 + 1];    //!< International Mobile Station Identity.
00415         char imei[15 + 1];    //!< International Mobile Equipment Identity.
00416         char meid[18 + 1];    //!< Mobile Equipment IDentifier.
00417         volatile RadioAccessNetworkType rat;  //!< Type of network (e.g. 2G, 3G, LTE).
00418         volatile NetworkRegistrationStatusCsd reg_status_csd; //!< Circuit switched attach status.
00419         volatile NetworkRegistrationStatusPsd reg_status_psd; //!< Packet switched attach status.
00420         volatile NetworkRegistrationStatusEps reg_status_eps; //!< Evolved Packet Switched (e.g. LTE) attach status.
00421 #ifdef TARGET_UBLOX_C030_R412M
00422         volatile ModemPSMState modem_psm_state; //!< last known modem PSM state
00423 #endif
00424     } DeviceInfo;
00425 
00426     /* IMPORTANT: the variables below are available to
00427      * classes that inherit this in order to keep things
00428      * simple. However, ONLY this class should free
00429      * any of the pointers, or there will be havoc.
00430      */
00431 
00432     /** Point to the instance of the AT parser in use.
00433      */
00434     ATCmdParser *_at;
00435 
00436     /** The current AT parser timeout value.
00437      */
00438     int _at_timeout;
00439 
00440     /** File handle used by the AT parser.
00441      */
00442     FileHandle *_fh;
00443 
00444     /** The mutex resource.
00445      */
00446     Mutex _mtx;
00447 
00448     /** General info about the modem as a device.
00449      */
00450     DeviceInfo _dev_info;
00451 
00452     /** The SIM PIN to use.
00453      */
00454     const char *_pin;
00455 
00456     /** Set to true to spit out debug traces.
00457      */
00458     bool _debug_trace_on;
00459     
00460     /** The baud rate to the modem.
00461      */
00462     int _baud;
00463 
00464     /** True if the modem is ready register to the network,
00465      * otherwise false.
00466      */
00467     bool _modem_initialised;
00468 
00469     /** True it the SIM requires a PIN, otherwise false.
00470      */
00471     bool _sim_pin_check_enabled;
00472 
00473     /** Sets the modem up for powering on
00474      *
00475      *  modem_init() is equivalent to plugging in the device, e.g., attaching power and serial port.
00476      *  Uses onboard_modem_api.h where the implementation of onboard_modem_api is provided by the target.
00477      */
00478     virtual void modem_init();
00479 
00480     /** Sets the modem in unplugged state
00481      *
00482      *  modem_deinit() will be equivalent to pulling the plug off of the device, i.e., detaching power
00483      *  and serial port. This puts the modem in lowest power state.
00484      *  Uses onboard_modem_api.h where the implementation of onboard_modem_api is provided by the target.
00485      */
00486     virtual void modem_deinit();
00487 
00488     /** Powers up the modem
00489      *
00490      *  modem_power_up() is equivalent to pressing the soft power button.
00491      *  The driver may repeat this if the modem is not responsive to AT commands.
00492      *  Uses onboard_modem_api.h where the implementation of onboard_modem_api is provided by the target.
00493      */
00494     virtual void modem_power_up();
00495 
00496     /** Powers down the modem
00497      *
00498      *  modem_power_down() is equivalent to turning off the modem by button press.
00499      *  Uses onboard_modem_api.h where the implementation of onboard_modem_api is provided by the target.
00500      */
00501     virtual void modem_power_down();
00502 
00503     /* Note: constructor and destructor protected so that this
00504     * class can only ever be inherited, never used directly.
00505     */
00506     UbloxCellularBase();
00507     ~UbloxCellularBase();
00508 
00509     /** Initialise this class.
00510      *
00511      * @param tx       the UART TX data pin to which the modem is attached.
00512      * @param rx       the UART RX data pin to which the modem is attached.
00513      * @param baud     the UART baud rate.
00514      * @param debug_on true to switch AT interface debug on, otherwise false.
00515      *
00516      * Note: it would be more natural to do this in the constructor
00517      * however, to avoid the diamond of death, this class is only
00518      * every inherited virtually.  Classes that are inherited virtually
00519      * do not get passed parameters in their constructor and hence
00520      * classInit() must be called separately by the first one to wake
00521      * the beast.
00522      */
00523     void baseClassInit(PinName tx = MDMTXD,
00524                        PinName rx = MDMRXD,
00525                        int baud = MBED_CONF_UBLOX_CELL_BAUD_RATE,
00526                        bool debug_on = false);
00527 
00528     /** Set the AT parser timeout.
00529      */
00530     void at_set_timeout(int timeout);
00531 
00532     /** Read up to size characters from buf
00533      * or until the character "end" is reached, overwriting
00534      * the newline with 0 and ensuring a terminator
00535      * in all cases.
00536      *
00537      * @param buf  the buffer to write to.
00538      * @param size the size of the buffer.
00539      * @param end  the character to stop at.
00540      * @return     the number of characters read,
00541      *             not including the terminator.
00542      */
00543     int read_at_to_char(char * buf, int size, char end);
00544 
00545     /** Powers up the modem.
00546      *
00547      * @return true if successful, otherwise false.
00548      */
00549     bool power_up();
00550 
00551     /** Power down the modem.
00552      */
00553     void power_down();
00554 
00555     /** Lock a mutex when accessing the modem.
00556      */
00557     void lock(void)     { _mtx.lock(); }
00558 
00559     /** Helper to make sure that lock unlock pair is always balanced
00560      */
00561     #define LOCK()         { lock()
00562 
00563     /** Unlock the modem when done accessing it.
00564      */
00565     void unlock(void)   { _mtx.unlock(); }
00566 
00567     /** Helper to make sure that lock unlock pair is always balanced
00568      */
00569     #define UNLOCK()       } unlock()
00570 
00571     /** Set the device identity in _dev_info
00572      * based on the ATI string returned by
00573      * the module.
00574      *
00575      * @return true if dev is a known value,
00576      *         otherwise false.
00577      */
00578     bool set_device_identity(DeviceType *dev);
00579 
00580     /** Perform any modem initialisation that is
00581      * specialised by device type.
00582      *
00583      * @return true if successful, otherwise false.
00584      */
00585     bool device_init(DeviceType dev);
00586 
00587     /** Set up the SIM.
00588      *
00589      * @return true if successful, otherwiss false.
00590      */
00591     bool initialise_sim_card();
00592 
00593 #ifdef TARGET_UBLOX_C030_R412M
00594     /** Converts the given uint to binary string. Fills the given str starting from [0] with the number of bits defined by bit_cnt
00595      *  For example uint_to_binary_string(9, str, 10) would fill str "0000001001"
00596      *  For example uint_to_binary_string(9, str, 3) would fill str "001"
00597      *
00598      *  @param num       uint to converts to binary string
00599      *  @param str       buffer for converted binary string
00600      *  @param str_size  size of the str buffer
00601      *  @param bit_cnt   defines how many bits are filled to buffer started from lsb
00602      */
00603     void uint_to_binary_str(uint32_t num, char* str, int str_size, int bit_cnt);
00604 #endif
00605 
00606 private:
00607 
00608     void set_nwk_reg_status_csd(int status);
00609     void set_nwk_reg_status_psd(int status);
00610     void set_nwk_reg_status_eps(int status);
00611     void set_rat(int AcTStatus);
00612     bool get_iccid();
00613     bool get_imsi();
00614     bool get_imei();
00615     bool get_meid();
00616     bool set_sms();
00617     void parser_abort_cb();
00618     void CMX_ERROR_URC();
00619     void CREG_URC();
00620     void CGREG_URC();
00621     void CEREG_URC();
00622     void UMWI_URC();
00623 #ifdef TARGET_UBLOX_C030_R412M
00624     void UUPSMR_URC();
00625     bool _psm_status;
00626     void *_cb_param_psm_going_in;
00627     Callback<void(void*)>    _func_psm_going_in;  /**< Callback. */
00628     void *_cb_param_psm_coming_out;
00629     Callback<void(void*)>    _func_psm_coming_out;  /**< Callback. */
00630     void set_modem_psm_state(int state);
00631 #endif
00632 };
00633 
00634 #endif // _UBLOX_CELLULAR_BASE_
00635