首页 > 代码库 > Android——4.2 - 3G移植之路之 reference-ril .pppd 拨号上网 (三)

Android——4.2 - 3G移植之路之 reference-ril .pppd 拨号上网 (三)

       Android的RIL机制中的 reference-ril.c 即为厂商提供的驱动接口,这个驱动源码各个厂商都是有提供的,网上也有下载,我现在用的就是huawei wcdma的,最后编译成libreference-ril.so,关于这个接口驱动在RIL中所扮演的角色可参考 Android——RIL 机制源码分析

android 4.2自带pppd源码在/external/ppp/pppd中.


                                              撰写不易,转载请注明出处: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=http://www.mamicode.com/2,/"UART/",1,,/"SER/",/"UART/",0", NULL);>



二.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动态库,

并且指定了通信端口为串口 -d /dev/ttyUSB2,另外还有 -s 代表是socket设备 -p 代表回环接口

下面的就是pppd拨号的脚本服务了,用来启动拨号脚本,并且指定数据端口.


这个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 拨号上网 (三)