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中.同样,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请求。这些在上面说到的源码分析里有详细分析,这里就只从拨号连接分析。

  1. static void requestSetupDataCall(voidvoid *data, size_t datalen, RIL_Token t)  
  2. {  
  3.     const charchar *apn;  
  4.     charchar *cmd;  
  5.     int err;  
  6.     ATResponse *p_response = NULL;  
  7.   
  8.     char ppp_dnses[(PROPERTY_VALUE_MAX * 2) + 3] = {'\0'}; //初始化属性数组,用于临时存储拨号的属性变量  
  9.     char ppp_local_ip[PROPERTY_VALUE_MAX] = {'\0'};  
  10.     char ril_pppd_tty[PROPERTY_VALUE_MAX] = {'\0'};  
  11.     char ppp_dns1[PROPERTY_VALUE_MAX] = {'\0'};  
  12.     char ppp_dns2[PROPERTY_VALUE_MAX] = {'\0'};  
  13.     char ppp_gw[PROPERTY_VALUE_MAX] = {'\0'};  
  14.   
  15.     char exit_code[PROPERTY_VALUE_MAX] = {'\0'};  
  16.   
  17.     int n = 1;  
  18.     RIL_Data_Call_Response_v6 *responses = alloca(n * sizeof(RIL_Data_Call_Response_v6)); //使用的是ipv6  
  19.   
  20.     apn = ((const charchar **)data)[2]; //取传进来的接入点 apn  
  21.     /*    ALOGD("jscese display in reference  APN == '%s' \n",apn); 
  22.      apn ="3gnet";*/  
  23.     ALOGD("[%s] jscese display in reference  APN '%s' ", __func__, apn);  
  24.   
  25. #ifdef USE_TI_COMMANDS  
  26.     // Config for multislot class 10 (probably default anyway eh?)  
  27.     err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",  
  28.             NULL);  
  29.   
  30.     err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0"NULL);  
  31. #endif /* USE_TI_COMMANDS */  
  32.   
  33.     int fd, qmistatus;  
  34.     size_t cur = 0;  
  35.     size_t len;  
  36.     ssize_t written, rlen;  
  37.     char status[32] = {0};  
  38.     int retry = 10;  
  39.     const charchar *pdp_type;  
  40.   
  41.     ALOGD("requesting data connection to APN '%s'", apn);  
  42.   
  43.     pdp_type = "IP"// jscese add for dial  
  44.   
  45.     asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);  
  46.     //FIXME check for error here  
  47.     err = at_send_command(cmd, NULL); //发送接入点的AT指令  
  48.     free(cmd);  
  49. #if 0  
  50.     // Set required QoS params to default  
  51.     err = at_send_command("AT+CGQREQ=1"NULL);  
  52.   
  53.     // Set minimum QoS params to default  
  54.     err = at_send_command("AT+CGQMIN=1"NULL);  
  55.   
  56.     // packet-domain event reporting  
  57.     err = at_send_command("AT+CGEREP=1,0"NULL);  
  58.   
  59.     // Hangup anything that's happening there now  
  60.     err = at_send_command("AT+CGACT=1,0"NULL);  
  61.   
  62. #endif  
  63.     // Start data on PDP context 1  
  64.     if (strcmp(apn, "3gnet") == 0)  
  65.     {  
  66.         ALOGD("jscese display in reference  is 3gnet \n");  
  67.         err = at_send_command("ATD*99***1#", &p_response);  //这个就是联通3G上网需要拨的号码 ATD*99***1# 获取连接  
  68.   
  69.     }  
  70.     else if (strcmp(apn, "ctnet") == 0)  
  71.     {  
  72.         ALOGD("jscese display in reference  is ctnet \n");  
  73.         err = at_send_command("ATD#777", &p_response);  
  74.     }  
  75.   
  76.     if (err < 0 || p_response->success == 0)  
  77.     {  
  78.         goto error;  
  79.     }  
  80.     at_response_free(p_response);  
  81.   
  82.     sleep(1); //Wait for the modem to finish  
  83.   
  84.     property_set("net.ppp1.local-ip""");  
  85.     property_set("net.gprs.ppp-exit""");  
  86.     property_set("ctl.start""pppd_gprs"); //如果上面的拨号AT指令成功返回,这里就启用之前定义好的一个pppd service 调用脚本去拨号获取IP 等网络参数  
  87.   
  88.     // Dialup  
  89.     sleep(3);  
  90.   
  91.   
  92.     /*jscese add try 5 times to get ip*/  
  93.     int iRetry = 5;  
  94.     while (iRetry > 0)  
  95.     {  
  96.         property_get("net.gprs.ppp-exit", exit_code, "");  
  97.   
  98.         if (strcmp(exit_code, "0") != 0)  
  99.         {  
  100.             ALOGE("PPPd exit with code %s", exit_code);  
  101.             iRetry = 0;  
  102.             break;  
  103.         }  
  104.   
  105.         ALOGI("Waiting For Property");  
  106.         if (wait_for_property("net.ppp1.local-ip"NULL10) < 0//监测 ip 地址的属性值,这个值我放在 pppd拨号脚本里面来进行设置,如果成功这里就能监测到  
  107.         {  
  108.             ALOGE("[%s]: wait for IP from ppp link at %d\n", __func__, iRetry);  
  109.         }  
  110.         else  
  111.         {  
  112.             ALOGI("[%s]: got IP from ppp link\r\n", __func__);  
  113.             break;  
  114.         }  
  115.         iRetry--;  
  116.     }  
  117.   
  118.     if (iRetry <= 0)  
  119.     {  
  120.         ALOGE("[%s]: fail to get IP\r\n", __func__);  
  121.         goto error;  
  122.     }  
  123.   
  124.     /*    if (wait_for_property("net.ppp1.local-ip", NULL, 10) < 0) { 
  125.      ALOGE("Timeout waiting net.ppp1.local-ip - giving up!\n"); 
  126.      goto error; 
  127.      }*/  
  128.   
  129.     property_get("net.ppp1.local-ip", ppp_local_ip, NULL);  
  130.     property_get("net.ppp1.dns1", ppp_dns1NULL);  
  131.     property_get("net.ppp1.dns2", ppp_dns2NULL);  
  132.     property_get("net.ppp1.gw", ppp_gw, NULL);  
  133.     sprintf(ppp_dnses, "%s %s", ppp_dns1, ppp_dns2);  
  134.   
  135.     ALOGI("Got net.ppp1.local-ip: %s\n", ppp_local_ip);  
  136.   
  137.     responses[0].status = 0;  
  138.     responses[0].suggestedRetryTime = -1;  
  139.     responses[0].cid = 1;  
  140.     responses[0].active = 2;  
  141.     responses[0].type = "PPP";  
  142.     responses[0].ifname = PPP_TTY_PATH;  
  143.     responses[0].addresses = ppp_local_ip;  
  144.     responses[0].dnses = ppp_dnses;  
  145.     responses[0].gateways = ppp_gw;  //通过获取到的 网络属性 设置这个responses 提供给上层  
  146.   
  147.     RIL_onRequestComplete(t, RIL_E_SUCCESS, responses, n * sizeof(RIL_Data_Call_Response_v6));  
  148.     return;  
  149.     error: RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL0);  
  150.     at_response_free(p_response);  
  151.   
  152. }  



二.pppd

     在上面 requestSetupDataCall 中启动一个服务来调用pppd拨号,先在init.rc中添加:

  1.  #jscese add for usb_switch service when 3G dongle add for serial  
  2.   
  3. service ril-daemon /system/bin/rild -l /system/lib/libreference-ril.so -- -d /dev/ttyUSB2  
  4.     class main  
  5.     socket rild stream 660 root radio  
  6.     socket rild-debug stream 660 radio system  
  7.     user root  
  8.     group radio cache inet misc audio log  
  9.   
  10.   
  11. service pppd_gprs /system/etc/ppp/init.gprs-pppd /dev/ttyUSB0  
  12.     user root  
  13.     group radio cache inet misc  
  14.     disabled  
  15.     oneshot  
  16. ## end  

上面的服务是用来开机启动rild的,加载libreference-ril动态库,

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

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


这个pppd_gprs 服务需要设置权限,因为我是在reference-ril里面通过属性启动的 在/system/core/init/property_service.c中添加如下:

  1. struct {  
  2.     const charchar *service;  
  3.     unsigned int uid;  
  4.     unsigned int gid;  
  5. } control_perms[] = {  
  6.     { "dumpstate",AID_SHELL, AID_LOG },  
  7.     { "ril-daemon",AID_RADIO, AID_RADIO },  
  8. /*jscese add for pppd 3G*/  
  9.     { "pppd_gprs",AID_RADIO, AID_LOG },  
  10. /*end*/  
  11.      {NULL00 }  
  12. };  
  1. struct {  
  2.     const charchar *prefix;  
  3.     unsigned int uid;  
  4.     unsigned int gid;  
  5. } property_perms[] = {  
  6.   
  7. ...  
  8.   
  9. /*jscese add  pppd for 3G*/  
  10.     { "net.ppp1.",AID_RADIO,0 },  
  11. /*end*/  
  12.   
  13.     { NULL00 }  
  14. };  

设置用到的net.ppp1.* 的权限。


init.gprs-pppd 设置执行权限,/system/core/include/private/android_filesystem_config.h中:

  1. static struct fs_path_config android_files[] = {  
  2.   
  3. ...  
  4.   
  5. /*jscese add for pppd */  
  6.     { 00777, AID_ROOT,      AID_SHELL,    "system/etc/init.gprs-pppd" },  
  7. /*    end*/  
  8.   
  9. ...  
  10.   
  11. }  

init.gprs-pppd

贴出拨号脚本

  1. #!/system/bin/sh  
  2.    
  3. PPPD_PID=  
  4.    
  5. USER=`/system/bin/getprop net.gprs.user`  
  6. PASSWORD=`/system/bin/getprop net.gprs.password`  
  7.    
  8. /system/bin/setprop "net.gprs.ppp-exit" ""  
  9.   
  10.    
  11. /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  
  12.   
  13. PPPD_EXIT=$?  
  14. PPPD_PID=$!  
  15.    
  16. /system/bin/log -t pppd "pppd exited with $PPPD_EXIT"  
  17.    
  18. /system/bin/setprop "net.gprs.ppp-exit" "$PPPD_EXIT"  



另外在这个拨号脚本同目录下 /system/etc/ppp/ 需要设置和注销ip等参数的脚本,一个在拨号成功时调用,一个在断开网络时调用:

ip-up-datakey

  1. #!/system/bin/sh  
  2. case $1 in  
  3.     ppp1)  
  4.         /android/bin/iptables --flush;  
  5.         /android/bin/iptables --table nat --flush;  
  6.         /android/bin/iptables --delete-chain;  
  7.         /android/bin/iptables --table nat --append POSTROUTING --out-interface ppp0 -j MASQUERADE;  
  8.         /android/bin/iptables --append FORWARD --in-interface ppp1 -j ACCEPT;  
  9.         echo 0 > /proc/sys/net/ipv4/ip_forward;  
  10.         echo 1 > /proc/sys/net/ipv4/ip_forward;  
  11.         ;;  
  12.     ppp0)  
  13.     /system/bin/setprop "net.interfaces.defaultroute" "ppp1";  
  14.     ;;  
  15. esac  
  16.    
  17. /system/bin/setprop "net.ppp1.dns1" "$DNS1"  
  18. /system/bin/setprop "net.ppp1.dns2" "$DNS2"  
  19. /system/bin/setprop "net.ppp1.local-ip" "$IPLOCAL"  
  20. /system/bin/setprop "net.ppp1.remote-ip" "$IPREMOTE"  
  21. /system/bin/setprop "net.ppp1.gw" "$IPREMOTE"  
  22. /system/bin/setprop "net.ppp1.if" "$IFNAME"  


ip-down-datakey:

  1. #!/system/bin/sh  
  2. case $1 in  
  3.     ppp1)  
  4.         echo 0 > /proc/sys/net/ipv4/ip_forward;  
  5.         ;;  
  6. esac  
  7.    
  8. /system/bin/setprop "net.ppp1.dns1" ""  
  9. /system/bin/setprop "net.ppp1.dns2" ""  
  10. /system/bin/setprop "net.ppp1.local-ip" ""  
  11. /system/bin/setprop "net.ppp1.remote-ip" ""  
  12. /system/bin/setprop "net.ppp1.gw" ""  
  13. /system/bin/setprop "net.ppp1.if" ""  


贴张联通apn为 3gnet 的拨号log:





  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值