ublox-cellular-base_R4_PR

Revision:
32:d961d0c06cdf
Parent:
31:b0a6a610d114
Child:
33:64b96bdac04e
--- a/UbloxCellularBase.cpp	Thu Aug 29 16:52:05 2019 +0500
+++ b/UbloxCellularBase.cpp	Thu Sep 05 15:46:11 2019 +0500
@@ -25,6 +25,7 @@
 #define tr_info(format, ...)  debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
 #define tr_warn(format, ...)  debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
 #define tr_error(format, ...) debug_if(_debug_trace_on, format "\n", ## __VA_ARGS__)
+#define tr_critical(format, ...) debug("\n" format "\n", ## __VA_ARGS__)
 #endif
 
 /* Array to convert the 3G qual number into a median EC_NO_LEV number.
@@ -493,15 +494,12 @@
     _dev_info.reg_status_eps = EPS_NOT_REGISTERED_NOT_SEARCHING;
 #ifdef TARGET_UBLOX_C030_R412M
     _dev_info.modem_psm_state = AWAKE;
-    _psm_status = false;
+    _psm_status = UNKNOWN;
     _cb_param_psm_going_in = NULL;
     _func_psm_going_in = NULL;
     _cb_param_psm_coming_out = NULL;
     _func_psm_coming_out = NULL;
 #endif
-#ifdef TARGET_UBLOX_C030_R41XM
-    _edrx_configured = false;
-#endif
 }
 
 // Destructor.
@@ -620,7 +618,7 @@
 
     for (int retry_count = 0; !success && (retry_count < 20); retry_count++) {
         //In case of SARA-R4, modem takes a while to turn on, constantly toggling the power pin every ~2 secs causes the modem to never power up.
-        if ( (retry_count % 5) == 0) { 
+        if ( (retry_count % 5) == 0) {
             modem_power_up();
         }
         wait_ms(500);
@@ -801,7 +799,7 @@
 // Initialise the modem.
 bool UbloxCellularBase::init(const char *pin)
 {
-	int x;
+    int x;
     MBED_ASSERT(_at != NULL);
 
     if (!_modem_initialised) {
@@ -816,29 +814,37 @@
             if (set_functionality_mode(FUNC_AIRPLANE)) {
 #endif
                 if (initialise_sim_card()) {
+#ifdef TARGET_UBLOX_C030_R41XM
                     int mno_profile;
-
+                    if (get_mno_profile(&mno_profile)) {
+                        if (mno_profile == SW_DEFAULT) {
+                            tr_critical("!!CANNOT USE PROFILE 0(SW_DEFAULT). PLEASE SET AN APPROPRIATE MNO PROFILE!!");
+                            _default_profile_is_set = true;
+                            return false;
+                        }
+                    }
 #ifdef TARGET_UBLOX_C030_R412M
-                    if (_psm_status ==  false) { //psm is not enabled by application yet so disable it at start-up
-                        set_power_saving_mode(0, 0);
+                    int status = 0, periodic_time = 0, active_time = 0;
+                    if (_psm_status == UNKNOWN) {
+                        if (get_power_saving_mode(&status, &periodic_time, &active_time)) {
+                            if (status) { //PSM is already enabled either by a previous run or MNO profile
+                                tr_info("PSM is already enabled, periodic_time %d, active_time %d", periodic_time, active_time);
+                                _psm_status = ENABLED;
+                                if ( !(set_psm_urcs(true)) ) { //enable PSM URCs
+                                    tr_error("Modem does not support PSM URCs, disabling PSM");
+                                    set_power_saving_mode(0, 0);
+                                } else if (!_func_psm_going_in){
+                                    tr_critical("!!PSM IS ENABLED, CALLBACK NOT ATTACHED. PLEASE REGISTER ONE!!");
+                                }
+                            }
+                        }
+                    } else if (_psm_status == ENABLED && !_func_psm_going_in){
+                        tr_critical("!!PSM IS ENABLED, CALLBACK NOT ATTACHED. PLEASE REGISTER ONE!!");
                     }
 #endif
-#ifdef TARGET_UBLOX_C030_R41XM
                     if (_at->is_idle_mode_enabled() == false) {
                         set_idle_mode(false); //disable idle mode at start up
                     }
-                    if(_edrx_configured == false) {
-                        // A special form of the command can be given as +CEDRXS=3.
-                        // In this form, eDRX will be disabled and data for all parameters in the command +CEDRXS will be removed or,
-                        // if available, set to the manufacturer specific default values.
-                        set_receive_period(3,UbloxCellularBase::EDRXGSM_A_Gb_mode);
-                        set_receive_period(3,UbloxCellularBase::EDRXEUTRAN_WB_S1_mode);
-                        set_receive_period(3,UbloxCellularBase::EDRXEUTRAN_NB_S1_mode);
-                    }
-                    get_receive_period();
-
-                    if (get_mno_profile(&mno_profile))
-                        tr_info("Current MNO profile is: %d", mno_profile);
 #endif
                     if (set_device_identity(&_dev_info.dev) && // Set up device identity
                         device_init(_dev_info.dev)) {// Initialise this device
@@ -933,7 +939,7 @@
                 // This should return quickly but sometimes the status field is not returned
                 // so make the timeout short
                 at_set_timeout(1000);
-                if (_at->send("AT+COPS?") && _at->recv("+COPS: %*d,%*d,\"%*[^\"]\",%d\n", &status)) {
+                if (_at->send("AT+COPS?") && _at->recv("+COPS: %*d,%*d,\"%*[^\"]\",%d\nOK\n", &status)) {
                     set_rat(status);
                 }
                 at_set_timeout(at_timeout);
@@ -1064,10 +1070,10 @@
     success = _at->send("AT+CGSN") && _at->recv("%15[^\n]\nOK\n", _dev_info.imei);
     tr_info("DevInfo: IMEI=%s", _dev_info.imei);
 
-	if (success)	{
-		memcpy(imei_to_send,_dev_info.imei,size);
-		imei_to_send[size-1] = '\0';
-	}
+    if (success)    {
+        memcpy(imei_to_send,_dev_info.imei,size);
+        imei_to_send[size-1] = '\0';
+    }
 
     UNLOCK();
     return success;
@@ -1164,6 +1170,13 @@
 //RAT should be set in a detached state (AT+COPS=2)
 bool UbloxCellularBase::set_modem_rat(RAT selected_rat, RAT preferred_rat, RAT second_preferred_rat)
 {
+#ifdef TARGET_UBLOX_C030_R41XM
+    if (_default_profile_is_set == true) {
+        tr_critical("!!CANNOT USE PROFILE 0(SW_DEFAULT). PLEASE SET AN APPROPRIATE MNO PROFILE!!");
+        return false;
+    }
+#endif
+
     bool success = false;
     char command[16] = {0x00};
 
@@ -1197,6 +1210,13 @@
 
 bool UbloxCellularBase::get_modem_rat(int *selected_rat, int *preferred_rat, int *second_preferred_rat)
 {
+#ifdef TARGET_UBLOX_C030_R41XM
+    if (_default_profile_is_set == true) {
+        tr_critical("!!CANNOT USE PROFILE 0(SW_DEFAULT). PLEASE SET AN APPROPRIATE MNO PROFILE!!");
+        return false;
+    }
+#endif
+
     bool success = false;
     char buf[24] = {0x00};
 
@@ -1305,12 +1325,18 @@
 
             LOCK();
             if (_at->send("AT+UMNOPROF=%d", current_profile) && _at->recv("OK")) {
-                tr_error("temporary MNO profile set: %d", current_profile);
+                tr_info("temporary MNO profile set: %d", current_profile);
             }
             UNLOCK();
         }
         LOCK();
         if (_at->send("AT+UMNOPROF=%d", profile) && _at->recv("OK")) {
+            if (profile == SW_DEFAULT) {
+                tr_critical("!!CANNOT USE PROFILE 0(SW_DEFAULT). PLEASE SET AN APPROPRIATE MNO PROFILE!!");
+                _default_profile_is_set = true;
+            } else {
+                _default_profile_is_set = false;
+            }
             return_val = true;
         } else {
             tr_error("unable to set user specified profile");
@@ -1347,7 +1373,7 @@
 bool UbloxCellularBase::set_idle_mode(bool enable)
 {
 #ifdef TARGET_UBLOX_C030_R412M
-    if (_psm_status == true && enable == true) {
+    if (_psm_status == ENABLED && enable == true) {
         return false;
     }
 #endif
@@ -1402,7 +1428,6 @@
     LOCK();
 
     if (_at->send("AT+CEDRXS=%d,%d,\"%s\"", mode, act_type, edrx) && _at->recv("OK")) {
-        _edrx_configured = true;
         status = 0;
     }
     else {
@@ -1514,6 +1539,11 @@
 
 bool UbloxCellularBase::set_band_bitmask(RAT rat, uint64_t bitmask) {
 
+    if (_default_profile_is_set == true) {
+        tr_critical("!!CANNOT USE PROFILE 0(SW_DEFAULT). PLEASE SET AN APPROPRIATE MNO PROFILE!!");
+        return false;
+    }
+
     bool status = false;
     UBandmaskRAT eBandMastRat;
 
@@ -1541,8 +1571,12 @@
 
     return status;
 }
-
-bool UbloxCellularBase::get_band_bitmask(uint64_t *m1_bitmask, uint64_t *nb1_bitmask) {
+bool UbloxCellularBase::get_band_bitmask(uint64_t *m1_bitmask, uint64_t *nb1_bitmask)
+{
+    if (_default_profile_is_set == true) {
+        tr_critical("!!CANNOT USE PROFILE 0(SW_DEFAULT). PLEASE SET AN APPROPRIATE MNO PROFILE!!");
+        return false;
+    }
 
     bool status = false;
     int eBandMastRat0, eBandMastRat1;
@@ -1573,7 +1607,7 @@
 
     LOCK();
     //+UCPSMS:1,,,"01000011","01000011"
-    if (_at->send("AT+UCPSMS?") && _at->recv("+UCPSMS:%d,,,\"%8c\",\"%8c\"\n", status, pt_encoded, at_encoded)) {
+    if (_at->send("AT+UCPSMS?") && _at->recv("+UCPSMS:%d,,,\"%8c\",\"%8c\"\nOK\n", status, pt_encoded, at_encoded)) {
         if (*status == true) {
             //PSM is enabled, decode the timer values, periodic TAU first
             value =  (pt_encoded[7]- '0');
@@ -1671,6 +1705,7 @@
 
 bool UbloxCellularBase::set_power_saving_mode(int periodic_time, int active_time)
 {
+
     if (_at->is_idle_mode_enabled() == true && periodic_time != 0 && active_time != 0 ) {
         return false;
     }
@@ -1685,11 +1720,11 @@
         if (periodic_time == 0 && active_time == 0) {
             // disable PSM
             if (_at->send("AT+CPSMS=0") && _at->recv("OK")) {
-                if (_at->send("AT+UPSMR=0") && _at->recv("OK")) {//disable the URC
+                if (set_psm_urcs(false)) {//disable the URC
                     //de-register the callback
                     detach_cb_psm_going_in();
                     detach_cb_psm_coming_out();
-                    _psm_status = false;
+                    _psm_status = DISABLED;
                     return_val = true;
                 }
             }
@@ -1791,9 +1826,9 @@
             at[8] = '\0';
 
             if (_at->send("AT+CPSMS=1,,,\"%s\",\"%s\"", pt, at) && _at->recv("OK")) {
-                if (_at->send("AT+UPSMR=1") && _at->recv("OK")) {//enable the PSM URC
+                if (set_psm_urcs(true)) {//enable the PSM URC
                     tr_info("PSM enabled successfully!");
-                    _psm_status = true;
+                    _psm_status = ENABLED;
                     return_val = true;
                 } else {
                     tr_error("PSM URCs not supported");
@@ -1835,6 +1870,22 @@
 
     UNLOCK();
 }
+
+bool UbloxCellularBase::set_psm_urcs(bool enable)
+{
+
+    bool success = false;
+    LOCK();
+
+    MBED_ASSERT(_at != NULL);
+
+    if (_at->send("AT+UPSMR=%d", enable ? 1 : 0) && _at->recv("OK")) {
+        success = true;
+    }
+
+    UNLOCK();
+    return success;
+}
 #endif
 
 // End of File