1. 程式人生 > >Android——4.2 - 3G移植之路之 reference-ril .pppd 撥號上網 (三)

Android——4.2 - 3G移植之路之 reference-ril .pppd 撥號上網 (三)

而且 init.rc nal null ann 源代碼分析 suggest cdma 初始化

Android的RIL機制中的 reference-ril.c 即為廠商提供的驅動接口。這個驅動源代碼各個廠商都是有提供的,網上也有下載。我如今用的就是huawei wcdma的。最後編譯成libreference-ril.so。關於這個接口驅動在RIL中所扮演的角色可參考 Android——RIL 機制源代碼分析

android 4.2自帶pppd源代碼在/external/ppp/pppd中.相同,kernel中也是須要打開對point-to-point 的支持,在network support裏面.


撰寫不易,轉載請註明出處:http://blog.csdn.net/jscese/article/details/40340665


一.requestSetupDataCall:

這個就是在reference-ril.c 中的數據流量的request,上層的接口通過onRequest 的RIL_REQUEST_SETUP_DATA_CALL請求。這些在上面說到的源代碼分析裏有具體分析,這裏就僅僅從撥號連接分析。

static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
{
    const char *apn;
    char *cmd;
    int err;
    ATResponse *p_response = NULL;

    char ppp_dnses[(PROPERTY_VALUE_MAX * 2) + 3] = {‘\0‘}; //初始化屬性數組。用於暫時存儲撥號的屬性變量
    char ppp_local_ip[PROPERTY_VALUE_MAX] = {‘\0‘};
    char ril_pppd_tty[PROPERTY_VALUE_MAX] = {‘\0‘};
    char ppp_dns1[PROPERTY_VALUE_MAX] = {‘\0‘};
    char ppp_dns2[PROPERTY_VALUE_MAX] = {‘\0‘};
    char ppp_gw[PROPERTY_VALUE_MAX] = {‘\0‘};

    char exit_code[PROPERTY_VALUE_MAX] = {‘\0‘};

    int n = 1;
    RIL_Data_Call_Response_v6 *responses = alloca(n * sizeof(RIL_Data_Call_Response_v6)); //使用的是ipv6

    apn = ((const char **)data)[2]; //取傳進來的接入點 apn
    /*    ALOGD("jscese display in reference  APN == ‘%s‘ \n",apn);
     apn ="3gnet";*/
    ALOGD("[%s] jscese display in reference  APN ‘%s‘ ", __func__, apn);

#ifdef USE_TI_COMMANDS
    // Config for multislot class 10 (probably default anyway eh?

) err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"", NULL); err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL); #endif /* USE_TI_COMMANDS */ int fd, qmistatus; size_t cur = 0; size_t len; ssize_t written, rlen; char status[32] = {0}; int retry = 10; const char *pdp_type; ALOGD("requesting data connection to APN ‘%s‘", apn); pdp_type = "IP"; // jscese add for dial asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn); //FIXME check for error here err = at_send_command(cmd, NULL); //發送接入點的AT指令 free(cmd); #if 0 // Set required QoS params to default err = at_send_command("AT+CGQREQ=1", NULL); // Set minimum QoS params to default err = at_send_command("AT+CGQMIN=1", NULL); // packet-domain event reporting 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); #endif // Start data on PDP context 1 if (strcmp(apn, "3gnet") == 0) { ALOGD("jscese display in reference is 3gnet \n"); err = at_send_command("ATD*99***1#", &p_response); //這個就是聯通3G上網須要撥的號碼 ATD*99***1# 獲取連接 } else if (strcmp(apn, "ctnet") == 0) { ALOGD("jscese display in reference is ctnet \n"); err = at_send_command("ATD#777", &p_response); } if (err < 0 || p_response->success == 0) { goto error; } at_response_free(p_response); sleep(1); //Wait for the modem to finish property_set("net.ppp1.local-ip", ""); property_set("net.gprs.ppp-exit", ""); property_set("ctl.start", "pppd_gprs"); //假設上面的撥號AT指令成功返回,這裏就啟用之前定義好的一個pppd service 調用腳本去撥號獲取IP 等網絡參數 // Dialup sleep(3); /*jscese add try 5 times to get ip*/ int iRetry = 5; while (iRetry > 0) { property_get("net.gprs.ppp-exit", exit_code, ""); if (strcmp(exit_code, "0") != 0) { ALOGE("PPPd exit with code %s", exit_code); iRetry = 0; break; } ALOGI("Waiting For Property"); if (wait_for_property("net.ppp1.local-ip", NULL, 10) < 0) //監測 ip 地址的屬性值。這個值我放在 pppd撥號腳本裏面來進行設置,假設成功這裏就能監測到 { ALOGE("[%s]: wait for IP from ppp link at %d\n", __func__, iRetry); } else { ALOGI("[%s]: got IP from ppp link\r\n", __func__); break; } iRetry--; } if (iRetry <= 0) { ALOGE("[%s]: fail to get IP\r\n", __func__); goto error; } /* if (wait_for_property("net.ppp1.local-ip", NULL, 10) < 0) { ALOGE("Timeout waiting net.ppp1.local-ip - giving up!\n"); goto error; }*/ property_get("net.ppp1.local-ip", ppp_local_ip, NULL); property_get("net.ppp1.dns1", ppp_dns1, NULL); property_get("net.ppp1.dns2", ppp_dns2, NULL); property_get("net.ppp1.gw", ppp_gw, NULL); sprintf(ppp_dnses, "%s %s", ppp_dns1, ppp_dns2); ALOGI("Got net.ppp1.local-ip: %s\n", ppp_local_ip); responses[0].status = 0; responses[0].suggestedRetryTime = -1; responses[0].cid = 1; responses[0].active = 2; responses[0].type = "PPP"; responses[0].ifname = PPP_TTY_PATH; responses[0].addresses = ppp_local_ip; responses[0].dnses = ppp_dnses; responses[0].gateways = ppp_gw; //通過獲取到的 網絡屬性 設置這個responses 提供給上層 RIL_onRequestComplete(t, RIL_E_SUCCESS, responses, n * sizeof(RIL_Data_Call_Response_v6)); return; error: RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0); at_response_free(p_response); }



二.pppd

在上面 requestSetupDataCall 中啟動一個服務來調用pppd撥號,先在init.rc中加入:

 #jscese add for usb_switch service when 3G dongle add for serial

service ril-daemon /system/bin/rild -l /system/lib/libreference-ril.so -- -d /dev/ttyUSB2
    class main
    socket rild stream 660 root radio
    socket rild-debug stream 660 radio system
    user root
    group radio cache inet misc audio log


service pppd_gprs /system/etc/ppp/init.gprs-pppd /dev/ttyUSB0
    user root
    group radio cache inet misc
    disabled
    oneshot
## end

上面的服務是用來開機啟動rild的,載入libreference-ril動態庫,

而且指定了通信port為串口 -d /dev/ttyUSB2,另外還有 -s 代表是socket設備 -p 代表回環接口

以下的就是pppd撥號的腳本服務了,用來啟動撥號腳本,而且指定數據port.


這個pppd_gprs 服務須要設置權限。由於我是在reference-ril裏面通過屬性啟動的 在/system/core/init/property_service.c中加入例如以下:

struct {
    const char *service;
    unsigned int uid;
    unsigned int gid;
} control_perms[] = {
    { "dumpstate",AID_SHELL, AID_LOG },
    { "ril-daemon",AID_RADIO, AID_RADIO },
/*jscese add for pppd 3G*/
    { "pppd_gprs",AID_RADIO, AID_LOG },
/*end*/
     {NULL, 0, 0 }
};

struct {
    const char *prefix;
    unsigned int uid;
    unsigned int gid;
} property_perms[] = {

...

/*jscese add  pppd for 3G*/
    { "net.ppp1.",AID_RADIO,0 },
/*end*/

    { NULL, 0, 0 }
};

設置用到的net.ppp1.* 的權限。


init.gprs-pppd 設置運行權限,/system/core/include/private/android_filesystem_config.h中:

static struct fs_path_config android_files[] = {

...

/*jscese add for pppd */
    { 00777, AID_ROOT,      AID_SHELL,    "system/etc/init.gprs-pppd" },
/*    end*/

...

}

init.gprs-pppd

貼出撥號腳本

#!/system/bin/sh
 
PPPD_PID=
 
USER=`/system/bin/getprop net.gprs.user`
PASSWORD=`/system/bin/getprop net.gprs.password`
 
/system/bin/setprop "net.gprs.ppp-exit" ""

 
/system/bin/pppd $1 115200 linkname datakey unit 1 crtscts usepeerdns noauth defaultroute noipdefault ipcp-accept-local ipcp-accept-remote ipcp-max-failure 30 lcp-echo-interval 5 lcp-echo-failure 30 modem dump debug kdebug 8

PPPD_EXIT=$?
PPPD_PID=$!
 
/system/bin/log -t pppd "pppd exited with $PPPD_EXIT"
 
/system/bin/setprop "net.gprs.ppp-exit" "$PPPD_EXIT"



另外在這個撥號腳本同文件夾下 /system/etc/ppp/ 須要設置和註銷ip等參數的腳本。一個在撥號成功時調用。一個在斷開網絡時調用:

ip-up-datakey

#!/system/bin/sh
case $1 in
    ppp1)
        /android/bin/iptables --flush;
        /android/bin/iptables --table nat --flush;
        /android/bin/iptables --delete-chain;
        /android/bin/iptables --table nat --append POSTROUTING --out-interface ppp0 -j MASQUERADE;
        /android/bin/iptables --append FORWARD --in-interface ppp1 -j ACCEPT;
        echo 0 > /proc/sys/net/ipv4/ip_forward;
        echo 1 > /proc/sys/net/ipv4/ip_forward;
        ;;
    ppp0)
    /system/bin/setprop "net.interfaces.defaultroute" "ppp1";
    ;;
esac
 
/system/bin/setprop "net.ppp1.dns1" "$DNS1"
/system/bin/setprop "net.ppp1.dns2" "$DNS2"
/system/bin/setprop "net.ppp1.local-ip" "$IPLOCAL"
/system/bin/setprop "net.ppp1.remote-ip" "$IPREMOTE"
/system/bin/setprop "net.ppp1.gw" "$IPREMOTE"
/system/bin/setprop "net.ppp1.if" "$IFNAME"


ip-down-datakey:

#!/system/bin/sh
case $1 in
    ppp1)
        echo 0 > /proc/sys/net/ipv4/ip_forward;
        ;;
esac
 
/system/bin/setprop "net.ppp1.dns1" ""
/system/bin/setprop "net.ppp1.dns2" ""
/system/bin/setprop "net.ppp1.local-ip" ""
/system/bin/setprop "net.ppp1.remote-ip" ""
/system/bin/setprop "net.ppp1.gw" ""
/system/bin/setprop "net.ppp1.if" ""


貼張聯通apn為 3gnet 的撥號log:

技術分享







Android——4.2 - 3G移植之路之 reference-ril .pppd 撥號上網 (三)