fork

Revision:
24:35d5b2a02df8
Parent:
19:7013521e75e7
Child:
25:aabc50bc418e
--- a/UbloxATCellularInterface.cpp	Tue Feb 12 18:42:43 2019 +0500
+++ b/UbloxATCellularInterface.cpp	Fri Apr 19 15:40:46 2019 +0500
@@ -114,12 +114,18 @@
         case NSAPI_SECURITY_CHAP:
             modem_security = 2;
             break;
+#ifndef TARGET_UBLOX_C030_R41XM
         case NSAPI_SECURITY_UNKNOWN:
             modem_security = 3;
             break;
         default:
             modem_security = 3;
             break;
+#else
+        default:
+            modem_security = 0;
+            break;
+#endif
     }
 
     return modem_security;
@@ -329,6 +335,7 @@
     bool success = false;
     int at_timeout = _at_timeout;
 
+#ifndef TARGET_UBLOX_C030_R41XM
     if (_at->send("AT+CGDCONT=%d,\"IP\",\"%s\"", cid, apn) && _at->recv("OK") &&
         _at->send("AT+UAUTHREQ=%d,%d,\"%s\",\"%s\"", cid, nsapi_security_to_modem_security(auth),
                   username, password) && _at->recv("OK") &&
@@ -340,10 +347,79 @@
         success = _at->send("AT+UPSDA=" PROFILE ",3") && _at->recv("OK");
         at_set_timeout(at_timeout);
     }
+#else
+    int modem_security = nsapi_security_to_modem_security(auth);
+    if (_at->send("AT+CGDCONT=%d,\"IP\",\"%s\"", cid, apn) && _at->recv("OK")) {
+        if ( modem_security == NSAPI_SECURITY_CHAP) {
+            if (_at->send("AT+UAUTHREQ=%d,%d,\"%s\",\"%s\"", cid, modem_security,
+                        password, username) && _at->recv("OK")) {
+                success = true;
+            }
+        } else if (modem_security == NSAPI_SECURITY_NONE) {
+            if (_at->send("AT+UAUTHREQ=%d,%d", cid, modem_security) && _at->recv("OK")) {
+                success = true;
+            }
+        } else {
+            if (_at->send("AT+UAUTHREQ=%d,%d,\"%s\",\"%s\"", cid, modem_security,
+                          username, password) && _at->recv("OK")) {
+                success = true;
+            }
+        }
+    }
+#endif
 
     return success;
 }
 
+#ifdef TARGET_UBLOX_C030_R41XM
+//define PDP context for R412M
+bool UbloxATCellularInterface::define_context()
+{
+    bool success = false;
+    const char * config = NULL;
+    LOCK();
+
+    // If the caller hasn't entered an APN, try to find it
+    if (_apn == NULL) {
+        config = apnconfig(_dev_info.imsi);
+    }
+
+    // Attempt to connect
+    do {
+        // Set up APN and IP protocol for PDP context
+        get_next_credentials(&config);
+        _auth = (*_uname && *_pwd) ? _auth : NSAPI_SECURITY_NONE;
+        if (activate_profile_by_cid(1, _apn, _uname, _pwd, _auth)) {
+            success = true;
+        }
+    } while (!success && config && *config);
+
+    if (!success) {
+        tr_error("Failed to connect, check your APN/username/password");
+    }
+
+    UNLOCK();
+    return success;
+}
+bool UbloxATCellularInterface::activate_context()
+{
+    bool status = false;
+    int at_timeout;
+    LOCK();
+
+    at_timeout = _at_timeout; // Has to be inside LOCK()s
+    at_set_timeout(150000);
+    if (_at->send("AT+CGACT=1,1") && _at->recv("OK")) {
+        status = true;
+    }
+
+    at_set_timeout(at_timeout);
+
+    UNLOCK();
+    return status;
+}
+#endif
+
 // Connect the on board IP stack of the modem.
 bool UbloxATCellularInterface::connect_modem_stack()
 {
@@ -1094,25 +1170,28 @@
             }
             _sim_pin_change_pending = false;
         }
-
+#ifdef TARGET_UBLOX_C030_R41XM
+        define_context();
+#endif
         if (nsapi_error == NSAPI_ERROR_NO_CONNECTION) {
             for (int retries = 0; !registered && (retries < 3); retries++) {
                 if (nwk_registration()) {
-                    registered = true;;
+                    registered = true;
+#ifdef TARGET_UBLOX_C030_R41XM
+                    if (activate_context()) {
+                        nsapi_error = NSAPI_ERROR_OK;
+                    }
+#endif
                 }
             }
         }
     }
-
+#ifndef TARGET_UBLOX_C030_R41XM
     // Attempt to establish a connection
-#ifdef TARGET_UBLOX_C030_R41XM
-    if (registered) {
-#else
     if (registered && connect_modem_stack()) {
-#endif
         nsapi_error = NSAPI_ERROR_OK;
     }
-
+#endif
     return nsapi_error;
 }