Exemple #1
0
int ACK_Pre_Process (char *buf_cmd)
{
	if (strstr(buf_cmd, "RING"))
	{
		LOGD(TAG "MT call: RING\n");
		send_at(g_fd_atcmd, "ATA\r");
		wait4_ack (g_fd_atcmd, NULL, 1000);
	}
	return 0;
}
Exemple #2
0
static void *rf_update_thread(void *priv)
{
    struct rf_factory *rf = (struct rf_factory*)priv;
    struct itemview *iv = rf->iv;
    int rssi_level = 0;
    int mGsmRfRssidbm = read_preferred_para("Gsm.RF.rssi.dbm",-80);
    int len = 0;
    const int HALT_TIME = 200000;//0.2s (200000ms)

    LOGD(TAG "%s: Start\n", __FUNCTION__);

    const int BUF_SIZE = 256;
    char cmd_buf[BUF_SIZE];
    char rsp_buf[BUF_SIZE];
    //[ALPS01679754]-start
    char bandSetting[BUF_SIZE];
    bool isBackup = false;
    //[ALPS01679754]-end

    int fd = -1;
    fd = open(DEVICE_NAME, O_RDWR);
    if(fd < 0) {
        LOGD(TAG "Fail to open %s: %s\n",DEVICE_NAME, strerror(errno));
        goto err;
    }
    LOGD(TAG "%s has been opened...\n",DEVICE_NAME);

    int rdCount = 0;
	int ret = 0;
    int retryCount = 0;
    int dynamicBandSupport = 0;

    memset(rf->info, 0, sizeof(rf->info) / sizeof(*(rf->info)));
    if(!rf->test_done) {
        bool ret = false;
        rf->test_done = true;

        LOGD(TAG "[AT]AT polling first:\n");
        do
        {
            send_at (fd, "AT\r\n");
        } while (wait4_ack (fd, NULL, 300));

        LOGD(TAG "[AT]Disable Sleep Mode:\n");
        send_at (fd, "AT+ESLP=0\r\n");
        if (wait4_ack (fd, NULL, 3000))goto err;

        //[ALPS01679754]-start : backup default bend mode settings
        while(1){
            const char *tok_epbse = "+EPBSE:";
            const char *tok_split = " ";
            char *p_epbse = NULL;
            char *p_str = NULL;

            memset(cmd_buf, 0, sizeof(cmd_buf));
            memset(rsp_buf, 0, sizeof(rsp_buf));
            memset(bandSetting, 0, sizeof(bandSetting));

            strcpy(cmd_buf, "AT+EPBSE?\r\n");
            write(fd, cmd_buf, strlen(cmd_buf));
            LOGD(TAG "Send AT+EPBSE?\n");
            read(fd, rsp_buf, BUF_SIZE);
            LOGD(TAG "------AT+EPBSE? start------\n");
            LOGD(TAG "%s\n", rsp_buf);
            LOGD(TAG "------AT+EPBSE? end------\n");

            p_epbse = strstr(rsp_buf, tok_epbse);

            if(p_epbse!= NULL){
                p_str = strstr(p_epbse, tok_split);
                LOGD(TAG "p_str=%s\n", p_str+1);
                strcpy(bandSetting, p_str+1);
                LOGD(TAG "bandSetting=%s\n", bandSetting);
                isBackup = true;
                break;
            }
        }
        //[ALPS01679754]-end

        while(1){
            /* Set Band to GSM 900 */
            LOGD(TAG "\n");
            send_at (fd, "AT+EPBSE=2\r\n");
            LOGD(TAG "Send AT+EPBASE=2 to set Band GSM 900, retryCount=%d \n",retryCount);
            if (wait4_ack (fd, NULL, 3000)){
				if(retryCount == 5){
                    LOGD(TAG "EPBSE fail too many times\n");
                    goto err;
				}else{
                    if(retryCount == 0){
                        /* ALPS01432749 : to backward compatilbe for old modem. we need to activate modem first */
                        LOGD(TAG "[AT]Try to activate modem first:\n");
                        retryCount++;
                        send_at (fd, "AT+CFUN=4\r\n");
                        if (wait4_ack (fd, NULL, 3000))goto err;
                    }

                    retryCount++;
                    usleep(HALT_TIME);
				}
            }else{
               LOGD(TAG "AT+EPBSE=2 got OK\n");
                break;
            }
        }

        /* Check if band setting can take effect without rebooting modem. Dynamic band setting is only supported in newer version modem */
        send_at (fd, "AT+CESQ=?\r\n");
        LOGD(TAG "Send AT+CESQ=? to check if it's newer version modem \n");
        if (wait4_ack (fd, NULL, 5000)!= -1){
            LOGD(TAG "AT+CESQ=? got OK, set dynamicBandSupport \n");
            dynamicBandSupport = 1;
        }

        if(dynamicBandSupport == 0){
        /* Reboot modem to make new band setting work */
        send_at (fd, "AT+EPON\r\n");
        LOGD(TAG "Send AT+EPON to reboot modem \n");
        if (wait4_ack (fd, NULL, 5000))goto err;


        /* Wait modem ready URC +EIND:128 */
        LOGD(TAG "AT+EPON OK ,Wait modem ready +EIND:128 \n");
        LOGD(TAG "[AT]Sleep:\n");
        usleep(10 * HALT_TIME);

        /* Reopen ccci FD. otherwise cannot access CCCI normally after rebooting modem */
        close(fd);
        fd = open(DEVICE_NAME, O_RDWR);
        if(fd < 0) {
            LOGD(TAG "Fail to open %s: %s\n",DEVICE_NAME, strerror(errno));
            goto err;
        }
        LOGD(TAG "%s has been reopened...\n",DEVICE_NAME);

        retryCount = 0;

        while(retryCount < 100){
            char *p = NULL;

            memset(rsp_buf, 0, sizeof(rsp_buf));
            read(fd, rsp_buf, BUF_SIZE);
            LOGD(TAG "------Wait EIND URC start------\n");
            LOGD(TAG "%s\n", rsp_buf);
            LOGD(TAG "------Wait ENID URC end------\n");

            retryCount++;
            p = strstr(rsp_buf, "+EIND:");

            if(p!= NULL){
                LOGD(TAG "+EIND:128 detected\n");
				break;
			}
 	        usleep(HALT_TIME);
        }

        LOGD(TAG "[AT]Disable Sleep Mode:\n");
        send_at (fd, "AT+ESLP=0\r\n");
        if (wait4_ack (fd, NULL, 3000))goto err;

		}

        //[ALPS01723693]-start: avoide AT+ERAT return ERROR
        /* To start SIM*/
        LOGD(TAG "Send AT+CFUN=4 to start SIM \n");
        send_at (fd, "AT+CFUN=4\r\n");
        if (wait4_ack (fd, NULL, 5000))goto err;
        LOGD(TAG "AT+CFUN=4 OK ,Start to turn on RF\n");
        //[ALPS01723693]-end

        /* To trun on RF . AT+CFUN=1 can be used for single or mulitple SIM project */
        LOGD(TAG "Send AT+CFUN=1 to turn on RF \n");
        send_at (fd, "AT+CFUN=1\r\n");
        if (wait4_ack (fd, NULL, 5000))goto err;
        LOGD(TAG "AT+CFUN=1 OK ,Start to test RF \n");

        /* To set GSM only modem. To make sure GSM 900 work */
        LOGD(TAG "Send AT+ERAT=0 to set GSM only \n");
        send_at (fd, "AT+ERAT=0\r\n");
        if (wait4_ack (fd, NULL, 5000))goto err;
        LOGD(TAG "AT+ERAT=0 OK ,Start to test RF \n");

        /* Start RF test */
        retryCount = 0;

        while(retryCount < 100){
            const char *tok = "+ECSQ";
            const char *minus_tok = "-";
            char *p = NULL;
            char *minus_p = NULL;
            rssi_level = 0;

            /* Check RF RSSI level. The verdict of RF test is the RSSI level shall be greater than -80dbm  */
            LOGD(TAG "\n");
  	        usleep(HALT_TIME);

            memset(cmd_buf, 0, sizeof(cmd_buf));
            strcpy(cmd_buf, "AT+ECSQ\r\n");
            write(fd, cmd_buf, strlen(cmd_buf));

            LOGD(TAG "Send AT+ECSQ to check RF, retryCount=%d \n",retryCount);

            memset(rsp_buf, 0, sizeof(rsp_buf));
            read(fd, rsp_buf, BUF_SIZE);
            LOGD(TAG "------AT+ECSQ start------\n");
            LOGD(TAG "%s\n", rsp_buf);
            LOGD(TAG "------AT+ECSQ end------\n");
            retryCount++;
            p = strstr(rsp_buf, tok);

            /* ex: +ECSQ: 26, 99, -241 */
            if(p!= NULL){
                LOGD(TAG "p=%s\n", p);
                minus_p = strstr(p, minus_tok);
                if(minus_p != NULL){
	                /* (qdbm value) /4 = dbm value */
                    LOGD(TAG "rssi_level str =%s\n", minus_p);
                    rssi_level = atoi(minus_p) / 4;
                }
            }
            return_data.rf.rf_data = rssi_level ;
            /* AT+ECSQ might got null immeidate response or rssi_in_qdbm=1 ,which means modem did not have any measurement result yet. keep retry polling */
            LOGD(TAG "mGsmRfRssidbm=%d, rssi_level=%d\n",mGsmRfRssidbm, rssi_level);
            if(rssi_level > mGsmRfRssidbm && rssi_level!=0){
                ret = 1;
                LOGD(TAG "rssi_level pass in RF test");
                break;
            }else if(rssi_level <= mGsmRfRssidbm){
                ret = 0;
                LOGD(TAG "rssi_level fail in RF test");
				break;
            }
        }

        //[ALPS01679754]-start:restore band mode settings
        while(isBackup){
            LOGD(TAG "restore band setting");
            const char tok_end[] = "\r\n";
            memset(cmd_buf, 0, sizeof(cmd_buf));
            strcpy(cmd_buf, "AT+EPBSE=");
            strcat(cmd_buf, bandSetting);
            strcat(cmd_buf, tok_end);
            LOGD(TAG "Send %s\n",cmd_buf);
            send_at(fd, cmd_buf);
            if (wait4_ack (fd, NULL, 5000)) {
                isBackup = false;
                goto err;
            } else {
                isBackup = false;
                LOGD(TAG "restore OK");
                break;
            }
        }
        //[ALPS01679754]-end

        if(ret) {
            LOGD(TAG "RF Test result pass\n");
            len = 0;
            len += sprintf(rf->info + len,
                "%s: %s. \n", uistr_rf_test, uistr_info_pass);
            len += sprintf(rf->info + len,
                "Verdict RSSI: %d dbm , Received RSSI: %d dbm\n", mGsmRfRssidbm,rssi_level);
            close(fd);
            fd = -1;

            LOGD(TAG "%s: Exit\n", __FUNCTION__);
            iv->redraw(iv);
            usleep(HALT_TIME * 30);

            return NULL;
        } else {
            LOGD(TAG "RF Test result fail\n");
            goto err;
        }

        LOGD(TAG "redraw\n");
        iv->redraw(iv);
    } // end if(!sim->test_done)

err:
    LOGD(TAG "%s: FAIL\n",__FUNCTION__);

    //[ALPS01723693]-start:error handling
    while(isBackup){
        LOGD(TAG "err:restore band setting");
        const char tok_end[] = "\r\n";
        memset(cmd_buf, 0, sizeof(cmd_buf));
        strcpy(cmd_buf, "AT+EPBSE=");
        strcat(cmd_buf, bandSetting);
        strcat(cmd_buf, tok_end);
        LOGD(TAG "err:Send %s\n",cmd_buf);
        send_at(fd, cmd_buf);
        if (wait4_ack (fd, NULL, 5000)) {
            LOGD(TAG "err:restore fail");
        } else {
            LOGD(TAG "err:restore OK");
            break;
        }
    }
    //[ALPS01723693]-end

    len = 0;
    len += sprintf(rf->info + len,
            "%s: %s. \n", uistr_rf_test, uistr_info_fail);
    len += sprintf(rf->info + len,
            "Verdict RSSI: %d dbm , Received RSSI: %d dbm\n", mGsmRfRssidbm,rssi_level);

    close(fd);
    fd = -1;

    LOGD(TAG "redraw\n");
    iv->redraw(iv);
    usleep(HALT_TIME * 30);
    LOGD(TAG "%s: Exit\n", __FUNCTION__);

    return NULL;
}
Exemple #3
0
static void *lte_rf_update_thread(void *priv)
{
    struct rf_factory *rf = (struct rf_factory*)priv;
    struct itemview *iv = rf->iv;
    int rssi0_dbm = 0;
    int rssi1_dbm = 0;
    int ret = 0;
    int len = 0;
    int retryCount = 0;
    int mLteRfBand = read_preferred_para("RF.band",1);;
    int mLteRfDlEarfcn = read_preferred_para("RF.dl.earfcn",300);
    int mLteRfRssi0dbm = read_preferred_para("RF.rssi0.dbm",-75);
    int mLteRfRssi1dbm = read_preferred_para("RF.rssi1.dbm",-80);
    const int BUF_SIZE = 256;
    char cmd_buf[BUF_SIZE];
    char rsp_buf[BUF_SIZE];
    const int HALT_TIME = 200000;//0.2s (200000ms)

    LOGD(TAG "%s: Start\n", __FUNCTION__);

    int fd = -1;
    fd = open(DEVICE_NAME, O_RDWR);
    if(fd < 0) {
        LOGD(TAG "Fail to open %s: %s\n",DEVICE_NAME, strerror(errno));
        goto err;
    }
    LOGD(TAG "%s has been opened...\n",DEVICE_NAME);

    memset(rf->info, 0, sizeof(rf->info) / sizeof(*(rf->info)));
    if(!rf->test_done) {
        rf->test_done = true;

        LOGD(TAG "[AT]AT polling first:\n");
        do
        {
            send_at (fd, "AT\r\n");
        } while (wait4_ack (fd, NULL, 300));

        LOGD(TAG "[AT]Disable Sleep Mode:\n");
        send_at (fd, "AT+ESLP=0\r\n");
        if (wait4_ack (fd, NULL, 3000))goto err;

        /* To start SIM*/
        LOGD(TAG "Send AT+CFUN=4 to startup modem \n");
        send_at (fd, "AT+CFUN=4\r\n");
        if (wait4_ack (fd, NULL, 5000))goto err;
        LOGD(TAG "AT+CFUN=4 OK ,Start to set 4G RSSI test\n");

        /* To start test 4G RSSI */
        LOGD(TAG "Send AT+ERFTX=7,%d,%d to set test 4G RSSI \n",mLteRfBand,mLteRfDlEarfcn);
        memset(cmd_buf, 0, sizeof(cmd_buf));
		sprintf(cmd_buf,"AT+ERFTX=7,%d,%d\r\n",mLteRfBand,mLteRfDlEarfcn);
        send_at (fd, cmd_buf);
        if (wait4_ack (fd, NULL, 5000))goto err;
        LOGD(TAG "%s OK ,Start to turn on RF\n",cmd_buf);

        /* Wait URC +ERXRSSI: <rssi0_dBm>, <rssi1_dBm> for verification */
        retryCount = 0;

        while(retryCount < 100){
            char *p = NULL;
            rssi0_dbm = 0;
            rssi1_dbm = 0;

            memset(rsp_buf, 0, sizeof(rsp_buf));
            read(fd, rsp_buf, BUF_SIZE);
            LOGD(TAG "------Wait ERXRSSI URC start------\n");
            LOGD(TAG "%s\n", rsp_buf);
            LOGD(TAG "------Wait ERXRSSI URC end------\n");

            retryCount++;
            p = strstr(rsp_buf, "+ERXRSSI:");

            if(p!= NULL){
				//example: p now is pointed to "+ERXRSSI: -80,-96"
                at_tok_start(&p);

                //parse out "-80" and assigned to rssi0_dbm
                at_tok_nextint(&p, &rssi0_dbm);

				//eamplex: p now is pointed to "-96"
                at_tok_nextint(&p, &rssi1_dbm);

                //<rssi0_dbm>,<rssi1_dbm> shall be negative integer value and -60 dbm is a stronger signal than -80
                if(((rssi0_dbm <0) && (rssi0_dbm > mLteRfRssi0dbm)) &&
                   ((rssi1_dbm <0) && (rssi1_dbm > mLteRfRssi1dbm))){
                    ret = 1;
                    LOGD(TAG "rssi0_dbm=%d ,rssi1_dbm=%d pass in RF test",rssi0_dbm,rssi1_dbm);
                    break;
                } else {
                    ret = 0;
                    LOGD(TAG "rssi0_dbm=%d ,rssi1_dbm=%d fail in RF test",rssi0_dbm,rssi1_dbm);
                    break;
                }
            }
            usleep(HALT_TIME);
        }

        return_data.rf.rf_data = 0-rssi0_dbm;
        return_data.rf.rf_data_lte = 0-rssi1_dbm;

        if(ret) {
            LOGD(TAG "RF Test result pass\n");

            len = 0;
            len += sprintf(rf->info + len,
                "%s: %s. \n", uistr_rf_test, uistr_info_pass);
            len += sprintf(rf->info + len,
                "Verdict RSSI_0: %d dbm , RSSI_1: %d dbm\n", mLteRfRssi0dbm,mLteRfRssi1dbm);
            len += sprintf(rf->info + len,
                "Received RSSI_0: %d dbm , RSSI_1: %d dbm\n", rssi0_dbm,rssi1_dbm);
            len += sprintf(rf->info + len,
                "LTE Band: %d , DL Channel Number: %d \n", mLteRfBand,mLteRfDlEarfcn);

            close(fd);
            fd = -1;

            LOGD(TAG "%s: Exit\n", __FUNCTION__);
            iv->redraw(iv);
            usleep(HALT_TIME * 30);

            return NULL;
        } else {
            LOGD(TAG "RF Test result fail\n");
            goto err;
        }
    } // end if(!sim->test_done)

err:
    LOGD(TAG "%s: FAIL\n",__FUNCTION__);

    len = 0;
    len += sprintf(rf->info + len,
            "%s: %s. \n", uistr_rf_test, uistr_info_fail);
    len += sprintf(rf->info + len,
            "Verdict RSSI_0: %d dbm , RSSI_1: %d dbm\n", mLteRfRssi0dbm,mLteRfRssi1dbm);
    len += sprintf(rf->info + len,
            "Received RSSI_0: %d dbm , RSSI_1: %d dbm\n", rssi0_dbm,rssi1_dbm);
    len += sprintf(rf->info + len,
            "LTE Band: %d , DL Channel Number: %d \n", mLteRfBand,mLteRfDlEarfcn);

    close(fd);
    fd = -1;

    LOGD(TAG "redraw\n");
    iv->redraw(iv);
    usleep(HALT_TIME * 30);
    LOGD(TAG "%s: Exit\n", __FUNCTION__);

    return NULL;
}
Exemple #4
0
static void *rf_c2k_update_thread(void *priv)
{
    struct rf_c2k_factory *rf = (struct rf_c2k_factory*)priv;
    struct itemview *iv = rf->iv;
    const int THRESHOLD = -95;//-95dbm
    int sqm = -1;
    int rssi = 0;
    int fd3 = -1;
    int fd1 = -1;
    int i = 0;
    const int HALT_TIME = 200000;//0.2s (200000ms)
    char atDevPath1[32] = {0};

    LOGD(TAG "%s: Start\n", __FUNCTION__);

    if (is_support_modem(4)) {
#ifdef MTK_FTM_SVLTE_SUPPORT
        LOGD(TAG "Get CCCI path of modem1");
        if (get_ccci_path(0, atDevPath1) == 0) {
        	LOGD(TAG "Can't get CCCI path!");
        	goto err;
        }
        LOGD(TAG "Go to open modem1 fd1 atDevPath1 = %s", atDevPath1);
        fd1 = openDeviceWithDeviceName(atDevPath1);
        if (fd1 < 0)
         {
             LOGD(TAG "Fail to open fd1\n");
             goto err;
         }
         LOGD(TAG "OK to open fd1\n");

         for (i = 0; i < 30; i++) usleep(50000); //sleep 1.5s wait for modem bootup
#endif
         LOGD(TAG "Go to open C2K modem fd3!");
         fd3 = openDeviceWithDeviceName("/dev/ttySDIO4");
         if (fd3 < 0)
         {
             LOGD(TAG "Fail to open ttySDIO4\n");
             goto err;
         }
         LOGD(TAG "OK to open ttySDIO4\n");
         for (i = 0; i < 30; i++) usleep(50000); //sleep 1.5s wait for modem bootup
    } else {
        LOGD(TAG "Not support C2K modem\n");
        goto err;
    }

    int ret = 0;
    int retryCount = 0;
    memset(rf->info, 0, sizeof(rf->info) / sizeof(*(rf->info)));
    if (!rf->test_done) {
        bool ret = false;
        rf->test_done = true;

        const int BUF_SIZE = 256;
        char cmd_buf[BUF_SIZE];
        char rsp_buf[BUF_SIZE];
#ifdef MTK_FTM_SVLTE_SUPPORT
        LOGD(TAG "[MD1] AT polling first:\n");
        do
        {
            send_at (fd1, "AT\r\n");
        } while (wait4_ack (fd1, NULL, 300));
        LOGD(TAG "[MD1]Send AT+ESLP=0 to disable sleep mode:\n");
        if (send_at (fd1, "AT+ESLP=0\r\n")) goto err;
        if (wait4_ack (fd1, NULL, 5000)) goto err;

        LOGD(TAG "[MD1]Send AT+ESIMS=1 to reset SIM1:\n");
        if (send_at (fd1, "AT+ESIMS=1\r\n")) goto err;
        if (wait4_ack (fd1, NULL, 5000)) goto err;

        LOGD(TAG "[MD1]Send AT+EFUN=0\n");
        if (send_at (fd1, "AT+EFUN=0\r\n")) goto err;
        if (wait4_ack(fd1, NULL, 5000)) goto err;

        LOGD(TAG "[MD1]Send AT+EMDSTATUS=1,1\n");
        if (send_at(fd1, "AT+EMDSTATUS=1,1\r\n")) goto err;
        if (wait4_ack(fd1, NULL, 5000)) goto err;

        LOGD(TAG "[MD1]send AT+EFUN=1\n");
        if (send_at (fd1, "AT+EFUN=1\r\n")) goto err;
        if (wait4_ack (fd1, NULL, 3000)) goto err;

#endif
        LOGD(TAG "[MD2]AT polling first:\n");
        send_at(fd3, "ate0q0v1\r\n");
        do
        {
            send_at(fd3, "AT\r\n");
        } while (wait4_ack(fd3, NULL, 3000));

        LOGD(TAG "[MD2]Send AT+CPOF to reboot modem \n");
        if (send_at(fd3, "AT+CPOF\r\n")) goto err;
        wait4_ack(fd3, NULL, 5000);

        LOGD(TAG "Wait for +VPON:0, C2K modem turn off:\n");
        wait4_ack (fd3, "+VPON:0", 3000);
#ifdef MTK_FTM_SVLTE_SUPPORT
        LOGD(TAG "[MD2]Send AT+EMDSTATUS=1,1\n");
        if (send_at(fd3, "AT+EMDSTATUS=1,1\r\n")) goto err;
        wait4_ack(fd3, NULL, 5000);
#endif
        /* Reboot modem to make new band setting work */
        LOGD(TAG "[MD2]Send AT+CPON to reboot modem \n");
        if (send_at(fd3, "AT+CPON\r\n")) goto err;
        if (wait4_ack(fd3, NULL, 5000)) goto err;

        usleep(150 * HALT_TIME); // Wait for regist network

        /* Start RF test */
        retryCount = 0;

        while (retryCount < 100) {
            const char *tok = "+CSQ";
            char *p = NULL;
            char *value = NULL;
            sqm = -1;
            rssi = 0;

            /* Check RF SQM level. The verdict of RF test is the SQM level shall be greater than 18 */
            LOGD(TAG "\n");
            usleep(HALT_TIME);

            memset(cmd_buf, 0, sizeof(cmd_buf));
            strcpy(cmd_buf, "AT+CSQ\r\n");
            write(fd3, cmd_buf, strlen(cmd_buf));

            LOGD(TAG "Send AT+CSQ to check RF, retryCount=%d \n",retryCount);

            memset(rsp_buf, 0, sizeof(rsp_buf));
            read(fd3, rsp_buf, BUF_SIZE);
            LOGD(TAG "------AT+CSQ start------\n");
            LOGD(TAG "%s\n", rsp_buf);
            LOGD(TAG "------AT+CSQ end------\n");
            retryCount++;

            p = strstr(rsp_buf, tok);
            if (p != NULL)
            {
                LOGD(TAG "p=%s\n", p);
                p = strchr(p, ':');
                if (p == NULL)
                {
                    LOGE(TAG "Invalid CSQ response urc");
                    goto err;
                }
                p++;
                //skip whitespace if any
                while (*p != '\0' && isspace(*p)) {
                   p++;
                }

                value = strsep(&p, ":");
                sqm = atoi(value);
                LOGD(TAG "sqm=%d\n", sqm);

                if (sqm > 0 && sqm < 31) {
                    rssi = sqm * 2 - 114;
                } else if (sqm == 0) {
                    rssi = -113;
                } else if (sqm == 31) {
                    rssi = -51;
                } else {
                    printf("invalid sqm value.\n");
                }
                LOGD(TAG "rssi=%d\n", rssi);

            }

            /* AT+CSQ might got null immeidate response or modem did not have any measurement result yet. keep retry polling */
            if (rssi >= THRESHOLD && rssi != 0) {
                ret = 1;
                LOGD(TAG "rssi pass in RF test");
                break;
            } else if (rssi < THRESHOLD) {
                ret = 0;
                LOGD(TAG "rssi fail in RF test");
                break;
            }
        }

        if (ret) {
            LOGD(TAG "C2K RF Test result pass\n");
            
            sprintf(rf->info + strlen(rf->info),
                "%s: %s. RSSI value is %d dbm\n", uistr_rf_c2k_test, uistr_info_pass, rssi);
            close(fd3);
            fd3 = -1;
            close(fd1);
            fd1 = -1;

            iv->redraw(iv);
            usleep(25 * HALT_TIME); //Wait 5s to show test info
            LOGD(TAG "%s: Exit\n", __FUNCTION__);

            return NULL;
        } else {
            LOGD(TAG "C2K RF Test result fail\n");
            goto err;
        }

        LOGD(TAG "redraw\n");
        iv->redraw(iv);
    }

err:
    LOGD(TAG "%s: FAIL\n", __FUNCTION__);
    sprintf(rf->info + strlen(rf->info),
        "%s: %s. RSSI value is %d dbm\n", uistr_rf_c2k_test, uistr_info_fail, rssi);

    if (fd1 > 0)
    {
        close(fd1);
    }
    fd1 = -1;
    if (fd3 > 0)
    {
        close(fd3);
    }
    fd3 = -1;

    LOGD(TAG "redraw\n");
    iv->redraw(iv);
    usleep(25 * HALT_TIME); //Wait 5s to show test info
    LOGD(TAG "%s: Exit\n", __FUNCTION__);

    return NULL;
}
Exemple #5
0
static void *sim_update_thread(void *priv)
{
    LOGD(TAG "%s: Start\n", __FUNCTION__);
    struct sim_factory *sim = (struct sim_factory*)priv;
    struct itemview *iv = sim->iv;
    int ret_ioctl_val = -1, fd_mdreset = -1, i = 0;
    int switchMode;
    char cmd_buf[BUF_SIZE] = {0};
    char rsp_buf[BUF_SIZE] = {0};
/// choose fd
    #ifdef EVDO_DT_VIA_SUPPORT
        unsigned char* str_sim1_mode = MTK_TELEPHONY_BOOTUP_MODE_SLOT1;
        unsigned char* str_sim2_mode = MTK_TELEPHONY_BOOTUP_MODE_SLOT2;
        int sim1_mode = atoi(str_sim1_mode);
        int sim2_mode = atoi(str_sim2_mode);        
        LOGD("bootup telephony mode [%d, %d].\n", sim1_mode, sim2_mode);
        char dev_node_ioctrl[32] = {0};
        if (sim1_mode == 0 || sim2_mode == 0) {
            /// use ioctrl to do sim switch
        if (MTK_ENABLE_MD1) {
                snprintf(dev_node_ioctrl, 32, "%s", ccci_get_node_name(USR_FACTORY_SIM_IOCTL,MD_SYS1));
            } else if (MTK_ENABLE_MD2) {
                snprintf(dev_node_ioctrl, 32, "%s", ccci_get_node_name(USR_FACTORY_SIM_IOCTL,MD_SYS2));

            } else {
                LOGD("not open md1 and md2's ioctrl");
            }
        }
        if (MTK_ENABLE_MD1) {
            fd_at= open(dev_node, O_RDWR | O_NONBLOCK);
            if (fd_at < 0) {
                LOGD(TAG "md1 open fd_at error");
                return 0;
            }
        else {
            LOGD(TAG "md1 open fd_at %d",fd_at);
        }

            if (sim1_mode == 0 || sim2_mode == 0) {
                fd_ioctlmd = open(dev_node_ioctrl, O_RDWR | O_NONBLOCK);
        if (fd_ioctlmd < 0) {
            LOGD(TAG "open fd_ioctlmd error");
            return 0;
        }
        else {
            LOGD(TAG "open fd_ioctlmd %d",fd_ioctlmd);
        }
            }
        //for (i = 0; i < 30; i++) usleep(50000); // sleep 1s wait for modem bootup
        } else {
            fd_at= open(dev_node, O_RDWR | O_NONBLOCK);
            if (fd_at < 0) {
                LOGD(TAG "md2 open fd_at error");
                return 0;
            } else {
            LOGD(TAG "md2 open fd_at %d",fd_at);
            }
 
            if (sim1_mode == 0 || sim2_mode == 0) {
                fd_ioctlmd = open(dev_node_ioctrl, O_RDWR | O_NONBLOCK);
            if (fd_ioctlmd < 0) {
                LOGD(TAG "open fd_ioctlmd error");
                return 0;
            } else {
                LOGD(TAG "open fd_ioctlmd %d",fd_ioctlmd);
            }
        }
        }
        /// if bootup mode is W+C or C+G, should do sim switch
        if (sim1_mode == 0 || sim2_mode == 0) {
        ///step1:off modem:AT+EPOF
        do
        {
            send_at (fd_at, "AT\r\n");
        } while (wait4_ack (fd_at, NULL, 300));

        LOGD(TAG "[AT]Enable Sleep Mode:\n");
        if (send_at (fd_at, "AT+ESLP=1\r\n")) return -1;
        if (wait4_ack (fd_at, NULL, 3000)) return -1;

        LOGD(TAG "[AT]Power OFF Modem:\n");
        if (send_at (fd_at, "AT+EFUN=0\r\n")) return -1;
        wait4_ack (fd_at, NULL, 15000);
        if (send_at (fd_at, "AT+EPOF\r\n")) return -1;
        wait4_ack (fd_at, NULL, 10000);
        ///step2:CCCI_IOC_ENTER_DEEP_FLIGHT
        LOGD(TAG "[AT]CCCI_IOC_ENTER_DEEP_FLIGHT \n");
        ret_ioctl_val = ioctl(fd_ioctlmd, CCCI_IOC_ENTER_DEEP_FLIGHT);
        LOGD("[AT]CCCI ioctl result: ret_val=%d, request=%d", ret_ioctl_val, CCCI_IOC_ENTER_DEEP_FLIGHT);

        ///step3:modem switch
        switchMode = SIM_SWITCH_MODE_GSM;
        LOGD(TAG "Begin:switchMode to gsm with index %d", switchMode);
        ret_ioctl_val = ioctl(fd_ioctlmd, CCCI_IOC_SIM_SWITCH, &switchMode);
        if (ret_ioctl_val  == -1) {
            LOGD(TAG "strerror(errno)=%s", strerror(errno));
        }
        ///step4:CCCI_IOC_LEAVE_DEEP_FLIGHT
        LOGD(TAG "[AT]CCCI_IOC_LEAVE_DEEP_FLIGHT \n");
        ret_ioctl_val = ioctl(fd_ioctlmd, CCCI_IOC_LEAVE_DEEP_FLIGHT);
        LOGD("[AT]CCCI ioctl result: ret_val=%d, request=%d", ret_ioctl_val, CCCI_IOC_LEAVE_DEEP_FLIGHT);
        ///wait 50ms close() for
        usleep(50000);

        ///close ttyC0 because of enter/leave flight modem, and  sim switch
        close(fd_at);
        LOGD(TAG "close fd_at %d",fd_at);
            char state_buf[6] = {0};
            /// check md open status
        int ret_mdreset = -1;
            int md_flag = 0;
        fd_mdreset = open("sys/class/BOOT/BOOT/boot/md", O_RDWR);
            LOGD(TAG "ret_mdreset = %d", fd_mdreset);
            LOGD(TAG "open sys/class/BOOT/BOOT/boot/md \n");
            if (fd_mdreset < 0) {
                fd_mdreset = open("sys/kernel/ccci/boot", O_RDWR);			
                LOGD(TAG "open sys/kernel/ccci/boot \n");
                md_flag = 1;
            }
            if (fd_mdreset >= 0 && md_flag == 0) {
        do{
            usleep(500000);
            ret_mdreset = read(fd_mdreset, state_buf, sizeof(state_buf));
                    LOGD(TAG "flag 0's state_buf = %s",state_buf);
        } while (state_buf[0]!= '2');
            } else if (fd_mdreset >= 0 && md_flag == 1) {		
                do{
                    usleep(500000);
                    ret_mdreset = read(fd_mdreset, state_buf, sizeof(state_buf));
                    LOGD(TAG "flag 1's state_buf = %s",state_buf);
                } while (state_buf[4]!= '2');
            } else {
                LOGE (TAG "open md open status file error");
            }
        ///wait a while for modem reset

        //for (i = 0; i < 10; i++) usleep(50000); // sleep 500ms wait for modem bootup

        ///step5: open ttyC0 again for AT cmd
            fd_at= open(dev_node, O_RDWR);
            if (fd_at < 0) {
                LOGD(TAG "open fd_at error");
                return 0;
            } else {
                LOGD(TAG "open fd_at %d",fd_at);
            }
        }
    #else //EVDO_DT_VIA_SUPPORT
        #if defined(PURE_AP_USE_EXTERNAL_MODEM)
            fd_at = open(DEVICE_NAME_3, O_RDWR);
        #else
            fd_at = open(dev_node, O_RDWR);
        #endif
        if(fd_at < 0) {
            LOGD(TAG "Fail to open %s: %s\n", dev_node,strerror(errno));
            return 0;
        }
    #endif  //EVDO_DT_VIA_SUPPORT
    LOGD(TAG "Device has been opened...\n");
    const int rdTimes = 3;
    int rdCount = 0;
    int tobreak = 0;
    int tr = 0, rsp_len = 0, isEsims = 1;

    struct termios options;
    cfmakeraw(&options);
    // no timeout but request at least one character per read
    options.c_cc[VTIME] = 0;
    options.c_cc[VMIN]  = 1;
    tcflush(fd_at, TCIOFLUSH);
    if (tcsetattr(fd_at, TCSANOW, &options) == -1) {
        LOGD(TAG "Fail to set %s attributes!! : %s\n",dev_node, strerror(errno));
    }    

#if defined(FTM_SIM_USE_USIMSMT)
    isEsims = 0;
#endif
#if defined(GEMINI) || defined(EVDO_DT_VIA_SUPPORT)
    isEsims = 1;
#endif
    LOGD(TAG "isEsims: %d\n", isEsims);

#if defined(PURE_AP_USE_EXTERNAL_MODEM) || defined(FTM_SIM_USE_USIMSMT)
    usleep(3000000);  //ALPS01194291: sleep 3s to wait device ready
    memset(cmd_buf, 0, sizeof(cmd_buf));
    memset(rsp_buf, 0, sizeof(rsp_buf));
    strcpy(cmd_buf, "ATE0\r\n");
    tcflush(fd_at, TCIOFLUSH);   //ALPS01194291: clear buffer to avoid wrong data
    write(fd_at, cmd_buf, strlen(cmd_buf));
    LOGD(TAG "Send ATE0\n");
    while (tobreak == 0) {
        read(fd_at, rsp_buf, BUF_SIZE);
        rsp_len = strlen(rsp_buf);
        LOGD(TAG "------AT+ATE0 echo start------\n");
        LOGD(TAG "%d\n", rsp_len);
        LOGD(TAG "%s\n", rsp_buf);
        LOGD(TAG "------AT+ATE0 echo end------\n");
        for (tr = 0; tr < rsp_len; tr++) {
            if (rsp_buf[tr] == 'O') {
                tobreak = 1;
                LOGD(TAG "Got Ok!------\n");
                break;
            }
        }
    }
    usleep(HALT_TIME * 10);
#endif

    int retryTime = 0;

    while(1) {
        usleep(500000);
        if (sim->exit_thread) {
            LOGD(TAG "Exit thread\n");
            break;
        }
        //memset(sim->info, 0, sizeof(sim->info) / sizeof(*(sim->info)));
        if (!sim->test_done) {
            int ret = -1;
            sim->test_done = true;
            memset(cmd_buf, 0, sizeof(cmd_buf));
            memset(rsp_buf, 0, sizeof(rsp_buf));

            // to detect 3G capability
            retryTime = 0;

            if (isEsims == 1) {
                 /* Use ESIMS to do SIM detect */
                int sim_switch_flag = 0;
                int swtich_to_SIM2 = 0;
                int swtich_to_SIM3 = 0;
                sim_switch_flag = check3GSwitchStatus(fd_at);
                swtich_to_SIM2 = (((sim->sim_id == SIM_ID_1) && (sim_switch_flag == 2)) ||
                                        ((sim->sim_id == SIM_ID_2) && (sim_switch_flag == 1)));

                swtich_to_SIM3 = (((sim->sim_id == SIM_ID_1) && (sim_switch_flag == 3)) ||
                                        ((sim->sim_id == SIM_ID_3) && (sim_switch_flag == 1)));

                //SIM1=4, SIM2=5, SIM3=6 
                if(swtich_to_SIM2) {
                    // switch UART to SIM2
                    sendEsuo(fd_at, 5);
                } else if (swtich_to_SIM3) {
                    // switch UART to SIM3
                    sendEsuo(fd_at, 6);
                }

                int nread = 0;
                tcflush(fd_at, TCIOFLUSH);   //ALPS01194291: clear buffer to avoid wrong data
                memset(cmd_buf, 0, sizeof(cmd_buf));
                memset(rsp_buf, 0, sizeof(rsp_buf));

                strcpy(cmd_buf, "AT+ESIMS\r\n");
                write(fd_at, cmd_buf, strlen(cmd_buf));
                LOGD(TAG "Send AT+ESIMS\n");
                tobreak = 0;
                while (tobreak == 0) {
                    nread = read(fd_at, rsp_buf, BUF_SIZE);
                    rsp_len = strlen(rsp_buf);
                    LOGD(TAG "------AT+ESIMS(SIM%d) start------\n", sim->sim_id);
                    LOGD(TAG "nread= %d len=%d buf=%s \n", nread, rsp_len,rsp_buf);
                    LOGD(TAG "------AT+ESIMS(SIM%d) end------\n", sim->sim_id);
                    ret = checkESIMSStatus(rsp_buf);
                    if (ret != -1) {
                        tobreak = 1;
                        LOGD(TAG "Got response!------\n");
                        break;
                    }
                }

                 // switch only if 3G on SIM 1
                if (swtich_to_SIM2 || swtich_to_SIM3) {
                    sendEsuo(fd_at, 4);
                }
            } else {
                /* Use USIMSMT to do SIM detect */
                do {
                    retryTime++;
                    if (sim->sim_id == SIM_ID_1) {
                        strcpy(cmd_buf, "AT+USIMSMT=1\r\n");
                    } else if (sim->sim_id == SIM_ID_2) {
                        strcpy(cmd_buf, "AT+USIMSMT=2\r\n");
                    }
                    write(fd_at, cmd_buf, strlen(cmd_buf));
                    LOGD(TAG "Send AT+USIMSMT=%d\n", sim->sim_id);
                    usleep(HALT_TIME);
                    read(fd_at, rsp_buf, BUF_SIZE);
                    //usleep(HALT_TIME);
                    tr = 0;
                    tobreak = 0;
                    rsp_len = strlen(rsp_buf);
                    LOGD(TAG "------AT+USIMSMT(SIM%d) start------\n", sim->sim_id);
                    LOGD(TAG "%s\n", rsp_buf);
                    ret = checkUsimSmtStatus(rsp_buf);
                    if ((ret == RET_ESIMS_YES) || (ret == RET_ESIMS_NO)) {
                        for (tr = 0; tr < rsp_len; tr++) {
                            if (rsp_buf[tr] == 'O') {
                                tobreak = 1;
                                LOGD(TAG "Got USIMSMT Ok!------\n");
                                break;
                            }
                        }
                        if (tobreak == 0) {
                            // Didn't get OK yet, so read again
                            memset(rsp_buf, 0, sizeof(rsp_buf));
                            read(fd_at, rsp_buf, BUF_SIZE);
                            LOGD(TAG "%s\n", rsp_buf);
                        }
                    }
                    LOGD(TAG "------AT+USIMSMT(SIM%d) end------\n", sim->sim_id);
                }while (ret == -1 && (retryTime < 3));
            }
            
            if (ret != -1) {
                rdCount = 0;
            } else {
                if (rdCount < rdTimes) {
                    LOGD(TAG "detect unknown response, redo\n");
                    rdCount++;
                    sim->test_done = false;
                    continue;
                }
            }

            if(ret == RET_ESIMS_YES) {
                sprintf(sim->info + strlen(sim->info),
                    "%s%d: %s.\n", uistr_info_detect_sim, sim->sim_id, uistr_info_pass);
                LOGD(TAG "Detect SIM%d: Pass.\n",sim->sim_id);
            } else {
                sprintf(sim->info + strlen(sim->info),
                    "%s%d: %s!!\n", uistr_info_detect_sim, sim->sim_id, uistr_info_fail);
                LOGD(TAG "Detect SIM%d: Fail.\n",sim->sim_id);
            }
            //LOGD(TAG "redraw\n");
            //iv->redraw(iv);
        } // end if(!sim->test_done)
    } // end while(1)
    #ifdef EVDO_DT_VIA_SUPPORT
        ///maybe not need to do. This is Factory mode test, when normal power on, modem will reset again.
	if (sim1_mode == 0 || sim2_mode == 0) {
        close(fd_at);
        fd_at = -1;
        switchMode = SIM_SWITCH_MODE_CDMA;
        LOGD(TAG "End:switchMode to cdma with index %d", switchMode);
        ret_ioctl_val = ioctl(fd_ioctlmd, CCCI_IOC_SIM_SWITCH, &switchMode);
        if (ret_ioctl_val  == -1) {
            LOGD(TAG "strerror(errno)=%s", strerror(errno));
        }
        close(fd_ioctlmd);
        fd_ioctlmd = -1;
        close(fd_mdreset);
        fd_mdreset = -1;
	} else {
        close(fd_at);
        fd_at = -1;
	}
    #else
        close(fd_at);
        fd_at = -1;
    #endif
    LOGD(TAG "%s: Exit\n", __FUNCTION__);

    return NULL;
}