ublox-cellular-base_mno

Revision:
29:8a38f91009ad
Parent:
27:250eaef6232d
--- a/UbloxCellularBase.cpp	Mon Aug 05 11:55:52 2019 +0500
+++ b/UbloxCellularBase.cpp	Mon Aug 26 14:55:37 2019 +0500
@@ -1281,28 +1281,41 @@
 bool UbloxCellularBase::set_mno_profile(MNOProfile profile)
 {
     bool return_val = false;
+    int current_profile;
+    MNOProfile arr[MAX_NUM_PROFILES] = { SW_DEFAULT, SIM_ICCID, ATT, TMO, VODAFONE, DT, STANDARD_EU
+#ifdef  TARGET_UBLOX_C030_R410M
+                                         , VERIZON, TELSTRA, CT, SPRINT, TELUS
+#endif
+                                       };
 
-    int mno_profile;
-    if (get_mno_profile(&mno_profile)) {
-        tr_info("Current MNO profile is: %d", mno_profile);
-        if (mno_profile != profile) {
+    if (is_registered_csd() || is_registered_psd() || is_registered_eps()) {
+        tr_error("MNO profile should only be set in detached state");
+        return false;
+    }
 
-            if (is_registered_csd() || is_registered_psd() || is_registered_eps()) {
-                tr_error("MNO profile should only be set in detached state");
-                return false;
+    if (get_mno_profile(&current_profile)) {
+        if (current_profile == profile) { //Ref to UBX-18019856 7.1.7, parameters will be updated only if we switch to another profile first
+            for (uint8_t index = 0; index < MAX_NUM_PROFILES; index++) { //get the index of current profile and use the next one
+                if (arr[index] == current_profile) {
+                    index = ((index + 1) % MAX_NUM_PROFILES);
+                    current_profile = arr[index];
+                    break;
+                }
             }
 
             LOCK();
-            if (_at->send("AT+UMNOPROF=%d", profile) && _at->recv("OK")) {
-                return_val = true;
-            } else {
-                tr_error("unable to set specified profile");
+            if (_at->send("AT+UMNOPROF=%d", current_profile) && _at->recv("OK")) {
+                tr_error("temporary MNO profile set: %d", current_profile);
             }
             UNLOCK();
-
+        }
+        LOCK();
+        if (_at->send("AT+UMNOPROF=%d", profile) && _at->recv("OK")) {
+            return_val = true;
         } else {
-            return_val = true;
+            tr_error("unable to set user specified profile");
         }
+        UNLOCK();
     } else {
         tr_error("could not read MNO profile");
     }
@@ -1313,6 +1326,7 @@
 bool UbloxCellularBase::get_mno_profile(int *profile)
 {
     bool return_val = false;
+    char buf[4] = {0x00};
 
     if (profile == NULL) {
         return false;
@@ -1321,7 +1335,8 @@
     LOCK();
     MBED_ASSERT(_at != NULL);
 
-    if ( (_at->send("AT+UMNOPROF?") && _at->recv("+UMNOPROF: %d", profile) && _at->recv("OK")) )  {
+    if (_at->send("AT+UMNOPROF?") && _at->recv("+UMNOPROF: %3[^\n]\nOK\n", buf))  {
+        *profile = atoi(buf);
         return_val = true;
     }
 
@@ -1377,6 +1392,125 @@
     UNLOCK();
     return return_val;
 }
+
+int UbloxCellularBase::set_receive_period(int mode, tEDRXAccessTechnology act_type, uint8_t edrx_value) {
+    char edrx[5];
+    uint_to_binary_str(edrx_value, edrx, 5, 4);
+    edrx[4] = '\0';
+    int status = 1;
+
+    LOCK();
+
+    if (_at->send("AT+CEDRXS=%d,%d,\"%s\"", mode, act_type, edrx) && _at->recv("OK")) {
+        _edrx_configured = true;
+        status = 0;
+    }
+    else {
+        status = 1;
+    }
+
+
+    UNLOCK();
+
+    return status;
+}
+
+int UbloxCellularBase::set_receive_period(int mode, tEDRXAccessTechnology act_type) {
+    int status = 1;
+
+    LOCK();
+
+    if (_at->send("AT+CEDRXS=%d,%d", mode, act_type) && _at->recv("OK")) {
+
+        status = 0;
+    }
+    else {
+        status = 1;
+    }
+
+    UNLOCK();
+
+    return status;
+}
+
+int UbloxCellularBase::set_receive_period(int mode) {
+    int status = 1;
+
+    LOCK();
+
+    if (_at->send("AT+CEDRXS=%d", mode) && _at->recv("OK")) {
+
+        status = 0;
+    }
+    else {
+        status = 1;
+    }
+
+    UNLOCK();
+
+    return status;
+}
+
+uint32_t UbloxCellularBase::get_receive_period() {
+    uint32_t edrx_value = 2;
+    char buf[24] = {0x00};
+    char edrx_val[5];
+    tEDRXAccessTechnology act_type;
+
+    LOCK();
+
+    if (_at->send("AT+CEDRXS?") && _at->recv("%23[^\n]\nOK\n", buf)) {
+        if (sscanf(buf, "+CEDRXS: %d,\"%s\"", (int *)&act_type, edrx_val) == 2) {
+
+            edrx_value = binary_str_to_uint(edrx_val,4);
+        }
+    }
+
+    if (_at->send("AT+CEDRXRDP") && _at->recv("OK")) {
+    }
+
+    tr_info("edrx_value. %d", edrx_value);
+
+    UNLOCK();
+    return edrx_value;
+}
+
+void UbloxCellularBase::uint_to_binary_str(uint32_t num, char* str, int str_size, int bit_cnt)
+{
+    if (!str || str_size < bit_cnt) {
+        return;
+    }
+    int tmp, pos = 0;
+
+    for (int i = 31; i >= 0; i--) {
+        tmp = num >> i;
+        if (i < bit_cnt) {
+            if (tmp&1) {
+                str[pos] = 1 + '0';
+            } else {
+                str[pos] = 0 + '0';
+            }
+            pos++;
+        }
+    }
+}
+
+uint32_t UbloxCellularBase::binary_str_to_uint(const char *binary_string, int binary_string_length)
+{
+    if (!binary_string || !binary_string_length) {
+        return 0;
+    }
+
+    int integer_output = 0, base_exp = 1;
+
+    for (int i = binary_string_length - 1; i >= 0; i--) {
+        if (binary_string[i] == '1') {
+            integer_output += (base_exp << (binary_string_length - (i+1)));
+        }
+    }
+
+    return integer_output;
+}
 #endif
 
 #ifdef TARGET_UBLOX_C030_R412M
@@ -1632,132 +1766,11 @@
     return return_val;
 }
 
-void UbloxCellularBase::uint_to_binary_str(uint32_t num, char* str, int str_size, int bit_cnt)
-{
-    if (!str || str_size < bit_cnt) {
-        return;
-    }
-    int tmp, pos = 0;
-
-    for (int i = 31; i >= 0; i--) {
-        tmp = num >> i;
-        if (i < bit_cnt) {
-            if (tmp&1) {
-                str[pos] = 1 + '0';
-            } else {
-                str[pos] = 0 + '0';
-            }
-            pos++;
-        }
-    }
-}
-
-uint32_t UbloxCellularBase::binary_str_to_uint(const char *binary_string, int binary_string_length)
-{
-    if (!binary_string || !binary_string_length) {
-        return 0;
-    }
-
-    int integer_output = 0, base_exp = 1;
-
-    for (int i = binary_string_length - 1; i >= 0; i--) {
-        if (binary_string[i] == '1') {
-            integer_output += (base_exp << (binary_string_length - (i+1)));
-        }
-    }
-
-    return integer_output;
-}
-
 bool UbloxCellularBase::is_modem_awake()
 {
   return (_dev_info.modem_psm_state == AWAKE);
 }
 
-#ifdef TARGET_UBLOX_C030_R41XM
-int UbloxCellularBase::set_receive_period(int mode, tEDRXAccessTechnology act_type, uint8_t edrx_value) {
-    char edrx[5];
-    uint_to_binary_str(edrx_value, edrx, 5, 4);
-    edrx[4] = '\0';
-    int status = 1;
-
-    LOCK();
-
-    if (_at->send("AT+CEDRXS=%d,%d,\"%s\"", mode, act_type, edrx) && _at->recv("OK")) {
-        _edrx_configured = true;
-        status = 0;
-    }
-    else {
-        status = 1;
-    }
-
-
-    UNLOCK();
-
-    return status;
-}
-
-int UbloxCellularBase::set_receive_period(int mode, tEDRXAccessTechnology act_type) {
-    int status = 1;
-
-    LOCK();
-
-    if (_at->send("AT+CEDRXS=%d,%d", mode, act_type) && _at->recv("OK")) {
-
-        status = 0;
-    }
-    else {
-        status = 1;
-    }
-
-    UNLOCK();
-
-    return status;
-}
-
-int UbloxCellularBase::set_receive_period(int mode) {
-    int status = 1;
-
-    LOCK();
-
-    if (_at->send("AT+CEDRXS=%d", mode) && _at->recv("OK")) {
-
-        status = 0;
-    }
-    else {
-        status = 1;
-    }
-
-    UNLOCK();
-
-    return status;
-}
-
-uint32_t UbloxCellularBase::get_receive_period() {
-    uint32_t edrx_value = 2;
-    char buf[24] = {0x00};
-    char edrx_val[5];
-    tEDRXAccessTechnology act_type;
-
-    LOCK();
-
-    if (_at->send("AT+CEDRXS?") && _at->recv("%23[^\n]\nOK\n", buf)) {
-        if (sscanf(buf, "+CEDRXS: %d,\"%s\"", (int *)&act_type, edrx_val) == 2) {
-
-            edrx_value = binary_str_to_uint(edrx_val,4);
-        }
-    }
-
-    if (_at->send("AT+CEDRXRDP") && _at->recv("OK")) {
-    }
-
-    tr_info("edrx_value. %d", edrx_value);
-
-    UNLOCK();
-    return edrx_value;
-}
-#endif //TARGET_UBLOX_C030_R41XM
-
 //application should call init() or connect() in order to initialize the modem
 void UbloxCellularBase::wakeup_modem()
 {
@@ -1777,5 +1790,6 @@
     UNLOCK();
 }
 #endif
+
 // End of File