OSDN Git Service

modify reference-ril to support 3G modem card on x86
[android-x86/hardware-ril.git] / reference-ril / reference-ril.c
index ccb5b62..77550ae 100644 (file)
 
 #define LOG_TAG "RIL"
 #include <utils/Log.h>
+#include <cutils/properties.h>
 
 #define MAX_AT_RESPONSE 0x1000
 
-/* pathname returned from RIL_REQUEST_SETUP_DEFAULT_PDP */
-#define PPP_TTY_PATH "/dev/omap_csmi_tty1"
+/* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
+#define PPP_TTY_PATH "/dev/ppp0"
 
 #ifdef USE_TI_COMMANDS
 
 #define WORKAROUND_FAKE_CGEV 1
 #endif
 
+typedef enum {
+    SIM_ABSENT = 0,
+    SIM_NOT_READY = 1,
+    SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
+    SIM_PIN = 3,
+    SIM_PUK = 4,
+    SIM_NETWORK_PERSONALIZATION = 5
+} SIM_Status; 
+
 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
 static RIL_RadioState currentState();
 static int onSupports (int requestCode);
 static void onCancel (RIL_Token t);
 static const char *getVersion();
 static int isRadioOn();
-static int getSIMStatus();
-static void onPDPContextListChanged(void *param);
+static SIM_Status getSIMStatus();
+static int getCardStatus(RIL_CardStatus **pp_card_status);
+static void freeCardStatus(RIL_CardStatus *p_card_status);
+static void onDataCallListChanged(void *param);
 
 extern const char * requestToString(int request);
 
@@ -228,7 +240,8 @@ static void onSIMReady()
      * ds = 1   // Status reports routed to TE
      * bfr = 1  // flush buffer
      */
-    at_send_command("AT+CNMI=1,2,2,1,1", NULL);
+    LOGI ("######### SIM READY !!\n");
+    at_send_command("AT+CNMI=1,2,2,1,0", NULL);
 }
 
 static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
@@ -268,19 +281,19 @@ error:
     RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
 }
 
-static void requestOrSendPDPContextList(RIL_Token *t);
+static void requestOrSendDataCallList(RIL_Token *t);
 
-static void onPDPContextListChanged(void *param)
+static void onDataCallListChanged(void *param)
 {
-    requestOrSendPDPContextList(NULL);
+    requestOrSendDataCallList(NULL);
 }
 
-static void requestPDPContextList(void *data, size_t datalen, RIL_Token t)
+static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
 {
-    requestOrSendPDPContextList(&t);
+    requestOrSendDataCallList(&t);
 }
 
-static void requestOrSendPDPContextList(RIL_Token *t)
+static void requestOrSendDataCallList(RIL_Token *t)
 {
     ATResponse *p_response;
     ATLine *p_cur;
@@ -293,7 +306,7 @@ static void requestOrSendPDPContextList(RIL_Token *t)
         if (t != NULL)
             RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
         else
-            RIL_onUnsolicitedResponse(RIL_UNSOL_PDP_CONTEXT_LIST_CHANGED,
+            RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
                                       NULL, 0);
         return;
     }
@@ -302,8 +315,8 @@ static void requestOrSendPDPContextList(RIL_Token *t)
          p_cur = p_cur->p_next)
         n++;
 
-    RIL_PDP_Context_Response *responses =
-        alloca(n * sizeof(RIL_PDP_Context_Response));
+    RIL_Data_Call_Response *responses =
+        alloca(n * sizeof(RIL_Data_Call_Response));
 
     int i;
     for (i = 0; i < n; i++) {
@@ -314,7 +327,7 @@ static void requestOrSendPDPContextList(RIL_Token *t)
         responses[i].address = "";
     }
 
-    RIL_PDP_Context_Response *response = responses;
+    RIL_Data_Call_Response *response = responses;
     for (p_cur = p_response->p_intermediates; p_cur != NULL;
          p_cur = p_cur->p_next) {
         char *line = p_cur->line;
@@ -341,7 +354,7 @@ static void requestOrSendPDPContextList(RIL_Token *t)
         if (t != NULL)
             RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
         else
-            RIL_onUnsolicitedResponse(RIL_UNSOL_PDP_CONTEXT_LIST_CHANGED,
+            RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
                                       NULL, 0);
         return;
     }
@@ -399,11 +412,11 @@ static void requestOrSendPDPContextList(RIL_Token *t)
 
     if (t != NULL)
         RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
-                              n * sizeof(RIL_PDP_Context_Response));
+                              n * sizeof(RIL_Data_Call_Response));
     else
-        RIL_onUnsolicitedResponse(RIL_UNSOL_PDP_CONTEXT_LIST_CHANGED,
+        RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
                                   responses,
-                                  n * sizeof(RIL_PDP_Context_Response));
+                                  n * sizeof(RIL_Data_Call_Response));
 
     return;
 
@@ -411,7 +424,7 @@ error:
     if (t != NULL)
         RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
     else
-        RIL_onUnsolicitedResponse(RIL_UNSOL_PDP_CONTEXT_LIST_CHANGED,
+        RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
                                   NULL, 0);
 
     at_response_free(p_response);
@@ -814,8 +827,8 @@ static void requestRegistrationState(int request, void *data,
     }
 
     asprintf(&responseStr[0], "%d", response[0]);
-    asprintf(&responseStr[1], "%d", response[1]);
-    asprintf(&responseStr[2], "%d", response[2]);
+    asprintf(&responseStr[1], "%x", response[1]);
+    asprintf(&responseStr[2], "%x", response[2]);
 
     if (count > 3)
         asprintf(&responseStr[3], "%d", response[3]);
@@ -941,7 +954,7 @@ error:
     at_response_free(p_response);
 }
 
-static void requestSetupDefaultPDP(void *data, size_t datalen, RIL_Token t)
+static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
 {
     const char *apn;
     char *cmd;
@@ -949,7 +962,7 @@ static void requestSetupDefaultPDP(void *data, size_t datalen, RIL_Token t)
     ATResponse *p_response = NULL;
     char *response[2] = { "1", PPP_TTY_PATH };
 
-    apn = ((const char **)data)[0];
+    apn = ((const char **)data)[2];
 
 #ifdef USE_TI_COMMANDS
     // Config for multislot class 10 (probably default anyway eh?)
@@ -976,18 +989,18 @@ static void requestSetupDefaultPDP(void *data, size_t datalen, RIL_Token t)
            len = strlen(cmd);
 
            while (cur < len) {
-                   do {
+               do {
                    written = write (fd, cmd + cur, len - cur);
                } while (written < 0 && errno == EINTR);
 
                if (written < 0) {
-                LOGE("### ERROR writing to /dev/qmi");
-                close(fd);
-                goto error;
-            }
+                    LOGE("### ERROR writing to /dev/qmi");
+                    close(fd);
+                    goto error;
+                }
 
-            cur += written;
-        }
+                cur += written;
+            }
 
         // wait for interface to come online
 
@@ -1011,8 +1024,8 @@ static void requestSetupDefaultPDP(void *data, size_t datalen, RIL_Token t)
 
         if (retry == 0) {
             LOGE("### Failed to get data connection up\n");
-               goto error;
-               }
+            goto error;
+        }
 
         qmistatus = system("netcfg rmnet0 dhcp");
 
@@ -1020,31 +1033,41 @@ static void requestSetupDefaultPDP(void *data, size_t datalen, RIL_Token t)
 
            if (qmistatus < 0) goto error;
 
-       } else {
+    } else {
 
-        asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
+        //asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
            //FIXME check for error here
-           err = at_send_command(cmd, NULL);
-           free(cmd);
+           //err = at_send_command(cmd, NULL);
+           //free(cmd);
 
            // Set required QoS params to default
-           err = at_send_command("AT+CGQREQ=1", NULL);
+           //err = at_send_command("AT+CGQREQ=1", NULL);
 
            // Set minimum QoS params to default
-           err = at_send_command("AT+CGQMIN=1", NULL);
+           //err = at_send_command("AT+CGQMIN=1", NULL);
 
            // packet-domain event reporting
-           err = at_send_command("AT+CGEREP=1,0", NULL);
+           //err = at_send_command("AT+CGEREP=1,0", NULL);
 
            // Hangup anything that's happening there now
-           err = at_send_command("AT+CGACT=1,0", NULL);
+           //err = at_send_command("AT+CGACT=1,0", NULL);
+           //err = at_send_command("AT+CGACT=1,1", NULL);
 
            // Start data on PDP context 1
-           err = at_send_command("ATD*99***1#", &p_response);
-
-           if (err < 0 || p_response->success == 0) {
+           //err = at_send_command("ATD*99***1#", &p_response);
+           //err = at_send_command("ATD*99#", &p_response);
+        //system("echo test");
+        //system("pppd call gprs");
+               //property_set("ctl.start", "pppd_gprs");
+        //LOGD("ctrl.start err = %d\n", err);
+           //err = system("pppd call gprs");
+        //LOGD("pppd err = %d\n", err);
+           /*if (err < 0 || p_response->success == 0) {
                goto error;
-           }
+           }*/
+        // add pppd init code under this line
+
+
     }
 
     RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
@@ -1224,11 +1247,20 @@ onRequest (int request, void *data, size_t datalen, RIL_Token t)
 
     switch (request) {
         case RIL_REQUEST_GET_SIM_STATUS: {
-            int simStatus;
-
-            simStatus = getSIMStatus();
-
-            RIL_onRequestComplete(t, RIL_E_SUCCESS, &simStatus, sizeof(simStatus));
+            RIL_CardStatus *p_card_status;
+            char *p_buffer;
+            int buffer_size;
+
+            int result = getCardStatus(&p_card_status);
+            if (result == RIL_E_SUCCESS) {
+                p_buffer = (char *)p_card_status;
+                buffer_size = sizeof(*p_card_status);
+            } else {
+                p_buffer = NULL;
+                buffer_size = 0;
+            }
+            RIL_onRequestComplete(t, result, p_buffer, buffer_size);
+            freeCardStatus(p_card_status);
             break;
         }
         case RIL_REQUEST_GET_CURRENT_CALLS:
@@ -1347,8 +1379,8 @@ onRequest (int request, void *data, size_t datalen, RIL_Token t)
         case RIL_REQUEST_SEND_SMS:
             requestSendSMS(data, datalen, t);
             break;
-        case RIL_REQUEST_SETUP_DEFAULT_PDP:
-            requestSetupDefaultPDP(data, datalen, t);
+        case RIL_REQUEST_SETUP_DATA_CALL:
+            requestSetupDataCall(data, datalen, t);
             break;
         case RIL_REQUEST_SMS_ACKNOWLEDGE:
             requestSMSAcknowledge(data, datalen, t);
@@ -1405,8 +1437,8 @@ onRequest (int request, void *data, size_t datalen, RIL_Token t)
             at_send_command("AT+COPS=0", NULL);
             break;
 
-        case RIL_REQUEST_PDP_CONTEXT_LIST:
-            requestPDPContextList(data, datalen, t);
+        case RIL_REQUEST_DATA_CALL_LIST:
+            requestDataCallList(data, datalen, t);
             break;
 
         case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
@@ -1550,8 +1582,8 @@ setRadioState(RIL_RadioState newState)
     }
 }
 
-/** returns one of RIM_SIM_*. Returns RIL_SIM_NOT_READY on error */
-static int
+/** Returns SIM_NOT_READY on error */
+static SIM_Status 
 getSIMStatus()
 {
     ATResponse *p_response = NULL;
@@ -1561,14 +1593,14 @@ getSIMStatus()
     char *cpinResult;
 
     if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
-        ret = RIL_SIM_NOT_READY;
+        ret = SIM_NOT_READY;
         goto done;
     }
 
     err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
 
     if (err != 0) {
-        ret = RIL_SIM_NOT_READY;
+        ret = SIM_NOT_READY;
         goto done;
     }
 
@@ -1577,11 +1609,11 @@ getSIMStatus()
             break;
 
         case CME_SIM_NOT_INSERTED:
-            ret = RIL_SIM_ABSENT;
+            ret = SIM_ABSENT;
             goto done;
 
         default:
-            ret = RIL_SIM_NOT_READY;
+            ret = SIM_NOT_READY;
             goto done;
     }
 
@@ -1591,28 +1623,28 @@ getSIMStatus()
     err = at_tok_start (&cpinLine);
 
     if (err < 0) {
-        ret = RIL_SIM_NOT_READY;
+        ret = SIM_NOT_READY;
         goto done;
     }
 
     err = at_tok_nextstr(&cpinLine, &cpinResult);
 
     if (err < 0) {
-        ret = RIL_SIM_NOT_READY;
+        ret = SIM_NOT_READY;
         goto done;
     }
 
     if (0 == strcmp (cpinResult, "SIM PIN")) {
-        ret = RIL_SIM_PIN;
+        ret = SIM_PIN;
         goto done;
     } else if (0 == strcmp (cpinResult, "SIM PUK")) {
-        ret = RIL_SIM_PUK;
+        ret = SIM_PUK;
         goto done;
     } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
-        return RIL_SIM_NETWORK_PERSONALIZATION;
+        return SIM_NETWORK_PERSONALIZATION;
     } else if (0 != strcmp (cpinResult, "READY"))  {
         /* we're treating unsupported lock types as "sim absent" */
-        ret = RIL_SIM_ABSENT;
+        ret = SIM_ABSENT;
         goto done;
     }
 
@@ -1620,7 +1652,7 @@ getSIMStatus()
     p_response = NULL;
     cpinResult = NULL;
 
-    ret = RIL_SIM_READY;
+    ret = SIM_READY;
 
 done:
     at_response_free(p_response);
@@ -1629,6 +1661,81 @@ done:
 
 
 /**
+ * Get the current card status.
+ *
+ * This must be freed using freeCardStatus.
+ * @return: On success returns RIL_E_SUCCESS
+ */
+static int getCardStatus(RIL_CardStatus **pp_card_status) {
+    static RIL_AppStatus app_status_array[] = {
+        // SIM_ABSENT = 0
+        { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
+          NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
+        // SIM_NOT_READY = 1
+        { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
+          NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
+        // SIM_READY = 2
+        { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
+          NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
+        // SIM_PIN = 3
+        { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
+          NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
+        // SIM_PUK = 4
+        { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
+          NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
+        // SIM_NETWORK_PERSONALIZATION = 5
+        { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
+          NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
+    };
+    RIL_CardState card_state;
+    int num_apps;
+
+    int sim_status = getSIMStatus();
+    if (sim_status == SIM_ABSENT) {
+        card_state = RIL_CARDSTATE_ABSENT;
+        num_apps = 0;
+    } else {
+        card_state = RIL_CARDSTATE_PRESENT;
+        num_apps = 1;
+    }
+
+    // Allocate and initialize base card status.
+    RIL_CardStatus *p_card_status = malloc(sizeof(RIL_CardStatus));
+    p_card_status->card_state = card_state;
+    p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
+    p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
+    p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
+    p_card_status->num_applications = num_apps;
+
+    // Initialize application status
+    int i;
+    for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
+        p_card_status->applications[i] = app_status_array[SIM_ABSENT];
+    }
+
+    // Pickup the appropriate application status
+    // that reflects sim_status for gsm.
+    if (num_apps != 0) {
+        // Only support one app, gsm
+        p_card_status->num_applications = 1;
+        p_card_status->gsm_umts_subscription_app_index = 0;
+
+        // Get the correct app status
+        p_card_status->applications[0] = app_status_array[sim_status];
+    }
+
+    *pp_card_status = p_card_status;
+    return RIL_E_SUCCESS;
+}
+
+/**
+ * Free the card status returned by getCardStatus
+ */
+static void freeCardStatus(RIL_CardStatus *p_card_status) {
+    free(p_card_status);
+}
+
+/**
  * SIM ready means any commands that access the SIM will work, including:
  *  AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
  *  (all SMS-related commands)
@@ -1645,19 +1752,19 @@ static void pollSIMState (void *param)
     }
 
     switch(getSIMStatus()) {
-        case RIL_SIM_ABSENT:
-        case RIL_SIM_PIN:
-        case RIL_SIM_PUK:
-        case RIL_SIM_NETWORK_PERSONALIZATION:
+        case SIM_ABSENT:
+        case SIM_PIN:
+        case SIM_PUK:
+        case SIM_NETWORK_PERSONALIZATION:
         default:
             setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
         return;
 
-        case RIL_SIM_NOT_READY:
+        case SIM_NOT_READY:
             RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
         return;
 
-        case RIL_SIM_READY:
+        case SIM_READY:
             setRadioState(RADIO_STATE_SIM_READY);
         return;
     }
@@ -1741,9 +1848,6 @@ static void initializeCallback(void *param)
     /*  Alternating voice/data off */
     at_send_command("AT+CMOD=0", NULL);
 
-    /*  Not muted */
-    at_send_command("AT+CMUT=0", NULL);
-
     /*  +CSSU unsolicited supp service notifications */
     at_send_command("AT+CSSN=0,1", NULL);
 
@@ -1798,7 +1902,7 @@ static void onUnsolicited (const char *s, const char *sms_pdu)
 {
     char *line = NULL;
     int err;
-
+LOGI("!!!!!!!!!!!!!!!!!!!!!!  reference-ril.c onUnsolicited !!!!!!!!!!!!!!!!!!!!!!1\n");
     /* Ignore unsolicited responses until we're initialized.
      * This is OK because the RIL library will poll for initial state
      */
@@ -1831,7 +1935,7 @@ static void onUnsolicited (const char *s, const char *sms_pdu)
             RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
             NULL, 0);
 #ifdef WORKAROUND_FAKE_CGEV
-        RIL_requestTimedCallback (onPDPContextListChanged, NULL, NULL);
+        RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
 #endif /* WORKAROUND_FAKE_CGEV */
     } else if (strStartsWith(s,"+CREG:")
                 || strStartsWith(s,"+CGREG:")
@@ -1840,7 +1944,7 @@ static void onUnsolicited (const char *s, const char *sms_pdu)
             RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
             NULL, 0);
 #ifdef WORKAROUND_FAKE_CGEV
-        RIL_requestTimedCallback (onPDPContextListChanged, NULL, NULL);
+        RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
 #endif /* WORKAROUND_FAKE_CGEV */
     } else if (strStartsWith(s, "+CMT:")) {
         RIL_onUnsolicitedResponse (
@@ -1853,13 +1957,13 @@ static void onUnsolicited (const char *s, const char *sms_pdu)
     } else if (strStartsWith(s, "+CGEV:")) {
         /* Really, we can ignore NW CLASS and ME CLASS events here,
          * but right now we don't since extranous
-         * RIL_UNSOL_PDP_CONTEXT_LIST_CHANGED calls are tolerated
+         * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
          */
         /* can't issue AT commands here -- call on main thread */
-        RIL_requestTimedCallback (onPDPContextListChanged, NULL, NULL);
+        RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
 #ifdef WORKAROUND_FAKE_CGEV
     } else if (strStartsWith(s, "+CME ERROR: 150")) {
-        RIL_requestTimedCallback (onPDPContextListChanged, NULL, NULL);
+        RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
 #endif /* WORKAROUND_FAKE_CGEV */
     }
 }
@@ -1913,12 +2017,30 @@ mainLoop(void *param)
             if (s_port > 0) {
                 fd = socket_loopback_client(s_port, SOCK_STREAM);
             } else if (s_device_socket) {
-                fd = socket_local_client( s_device_path,
-                                          ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
-                                          SOCK_STREAM );
+                if (!strcmp(s_device_path, "/dev/socket/qemud")) {
+                    /* Qemu-specific control socket */
+                    fd = socket_local_client( "qemud",
+                                              ANDROID_SOCKET_NAMESPACE_RESERVED,
+                                              SOCK_STREAM );
+                    if (fd >= 0 ) {
+                        char  answer[2];
+
+                        if ( write(fd, "gsm", 3) != 3 ||
+                             read(fd, answer, 2) != 2 ||
+                             memcmp(answer, "OK", 2) != 0)
+                        {
+                            close(fd);
+                            fd = -1;
+                        }
+                   }
+                }
+                else
+                    fd = socket_local_client( s_device_path,
+                                            ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
+                                            SOCK_STREAM );
             } else if (s_device_path != NULL) {
                 fd = open (s_device_path, O_RDWR);
-                if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
+                if ( fd >= 0) {
                     /* disable echo on serial ports */
                     struct termios  ios;
                     tcgetattr( fd, &ios );
@@ -1934,6 +2056,50 @@ mainLoop(void *param)
             }
         }
 
+#if 0
+    fd=-1;
+    while  (fd < 0) {
+        LOGI ("xp-debug mainLoop\n");
+    //  signal(SIGIO, SIG_IGN); // the important one.
+
+        fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
+        LOGD ("##############open /dev/ttyUSB0##############\n");
+        if (fd < 0) {
+              LOGI ("fd < 0  fd:%d\n",fd);
+              //return 0;
+        } else {
+            fcntl(fd,F_SETFL,0);
+            struct termios my_termios;
+            tcgetattr(fd, &my_termios);
+
+            cfsetispeed(&my_termios,B9600);  // Baud rate 9600
+            cfsetospeed(&my_termios,B9600);
+
+            my_termios.c_cflag &= ~CSIZE;   // 8 data bits
+            my_termios.c_cflag &= ~PARENB;  // no parity
+            my_termios.c_cflag &= ~CSTOPB;  // No hw flow control
+            my_termios.c_cflag &= ~CRTSCTS;
+            my_termios.c_cflag |= CS8;
+            my_termios.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);  // No terminal processing
+            my_termios.c_oflag &= ~OPOST;
+
+            my_termios.c_cc[VMIN] = 0;   // Timeout before packet (unused)
+            my_termios.c_cc[VTIME] = 10;  // Timeout between characters
+            my_termios.c_iflag &= ~(IXON|IXOFF|IXANY); // Disable sw flow control
+
+            my_termios.c_cflag |= (CLOCAL|CREAD);
+
+            tcsetattr(fd,TCSANOW, &my_termios);
+            fcntl(fd,F_SETFL,0);
+        }
+        if (fd < 0) {
+            perror ("opening AT interface. retrying...");
+            sleep(10);
+            /* never returns */
+        }
+    }
+#endif
+
         s_closed = 0;
         ret = at_open(fd, onUnsolicited);