* add C027_Support fork

Fork of C027_Support by u-blox

Revision:
131:965a7cbc1e58
Parent:
130:3189949981ec
Child:
133:57b208dd96fb
--- a/MDM.cpp	Fri Aug 28 12:40:05 2015 +0000
+++ b/MDM.cpp	Tue Sep 01 15:50:06 2015 +0000
@@ -752,6 +752,23 @@
     return false;
 }
 
+bool MDMParser::_activateProfileReuseExternal(void)
+{
+    int cid = -1;
+    sendFormated("AT+CGDCONT?\r\n");
+    if (RESP_OK != waitFinalResp(_cbCGDCONT, &cid))
+        return false;
+    if (cid == -1)
+        return false;
+    // we found a context that provides us a valid IP so lets reuse it for the internal IP stack
+    sendFormated("AT+UPSD=" PROFILE ",100,%d\r\n", cid);
+    if (RESP_OK != waitFinalResp())
+        return false;
+    // Activate the profile and make connection
+    sendFormated("AT+UPSDA=" PROFILE ",3\r\n");
+    return (RESP_OK == waitFinalResp(NULL,NULL,150*1000));
+}
+
 bool MDMParser::_activateProfileByCid(int cid, const char* apn, const char* username, const char* password, Auth auth)
 {
     sendFormated("AT+CGDCONT=%d,\"IP\",\"%s\"\r\n", cid, apn);
@@ -768,6 +785,22 @@
     return (RESP_OK == waitFinalResp(NULL,NULL,150*1000));
 }
  
+int MDMParser::_cbCGDCONT(int type, const char* buf, int len, int* cid)
+{
+    // accept with and without leading \r\n in +CGDCONT:
+    if ((type == TYPE_PLUS) && (buf[0] == '\r') && (buf[1] == '\n') && (len >= 2))
+        buf += 2, len -= 2, type = TYPE_UNKNOWN;
+    if (type == TYPE_UNKNOWN) {
+        int a,b,c,d,t;
+        //+CGDCONT: <cid>,"IP","<apn name>","<ip adr>",0,0,0,0,0,0
+        if (sscanf(buf, "+CGDCONT: %d,\"IP\",\"%*[^\"]\",\"" IPSTR "\",%*d,%*d,%*d,%*d,%*d,%*d", &t, &a,&b,&c,&d) == 5) {
+            if (IPADR(a,b,c,d) != NOIP) 
+                *cid = t;
+        }
+    }
+    return WAIT;
+}
+
 MDMParser::IP MDMParser::join(const char* apn /*= NULL*/, const char* username /*= NULL*/, 
                               const char* password /*= NULL*/, Auth auth /*= AUTH_DETECT*/)
 {
@@ -824,8 +857,13 @@
                 TRACE("Testing APN Settings(\"%s\",\"%s\",\"%s\",%d)\r\n", apn, username, password, auth);
                 if ((_dev.dev != DEV_TOBY_L2) && (_dev.dev != DEV_MPCI_L2))
                     ok = _activateProfile(apn, username, password, auth);
-                else
-                    ok = _activateProfileByCid(1, apn, username, password, auth);
+                else {
+                    ok = _activateProfileReuseExternal();
+                    if (ok) 
+                        TRACE("Reusing External Context\r\n");
+                    else
+                        ok = _activateProfileByCid(1, apn, username, password, auth);
+                }
             } while (!ok && config && *config); // maybe use next setting ? 
             if (!ok) {
                 ERROR("Your modem APN/password/username may be wrong\r\n");