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请求。这些在上面说到的源代码分析里有具体分析,这里就仅仅从拨号连接分析。
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 拨号上网 (三)的更多相关文章
- Android——4.2 - 3G移植之路之 AT 通信 (四)
在前文Android--4.2 - 3G移植之路之 reference-ril .pppd 拨号上网 (三)中分析了3G连接网络的流程,当中有说道通过AT指令建立连接, 在这里记录一下3G中的AT通信 ...
- Android——4.2 - 3G移植之路之 APN (五)
APN,这东西对于刚接触的人来说并非那么好理解.对于3G移植上网不可缺少,这里记录一下. 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/detail ...
- android 3G移植【转】
本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17028383 一 开发环境简介 内容 说明 3G模块 华为EM820W(WCDMA ...
- android移植pppoe拨号上网的全过程
硬件环境:Tiny6410开发板 软件环境:fedora14 + Android 2.3.4 + linux-2.6.36 所需资源:rp-pppoe-3.11.tar.gz http://www.r ...
- 基于MT6752/32平台 Android L版本驱动移植步骤
基于MT6752/32平台 Android L版本驱动移植步骤 根据MK官网所述,在Android L 版本上Turnkey ABS 架构将会phase out,而Mediatek Turnkey架构 ...
- Android Vector曲折的兼容之路
Android Vector曲折的兼容之路 两年前写书的时候,就在研究Android L提出的Vector,可研究下来发现,完全不具备兼容性,相信这也是它没有被广泛使用的一个原因,经过Google的不 ...
- Android wifi驱动的移植 realtek 8188
Android wifi驱动的移植 一般我们拿到的android源代码中wifi应用层部分是好的, 主要是wifi芯片的驱动要移植并添加进去. wifi驱动的移植, 以realtek的8188etv为 ...
- Android程序员的进阶之路
本文主要论述的是Android程序员的进阶之路,博主本人就是一名android开发攻城狮,所以这里讲述的大多数是android开发攻城狮的技术进阶之路,如有问题请多指正. 大家都知道程序员之中有有菜鸟 ...
- ARM+llinux系统移植3G拨号上网收发短信(一)【转】
本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17099737 一. PPP移植 各项工作具体说明 向Linux内核添加3 ...
随机推荐
- PE笔记之NT头PE签名
typedef struct _IMAGE_NT_HEADERS { DWORD Signature; //PE头签名PE\0\0 IM ...
- 【Android】SQLite基本用法(转)
在Android开发中SQLite起着很重要的作用,网上SQLite的教程有很多很多,不过那些教程大多数都讲得不是很全面.本人总结了一些SQLite的常用的方法,借着论坛的大赛,跟大家分享分享的.一. ...
- Magenta源代码笔记(3) —— 内存管理【转】
转自:http://blog.csdn.net/boymax2/article/details/52550197 版权声明:本文为博主原创文章,未经博主允许不得转载. Magenta内核支持虚拟地址的 ...
- 从零开始学习OpenCL开发(一)架构【转】
转自:http://blog.csdn.net/leonwei/article/details/8880012 多谢大家关注 转载本文请注明:http://blog.csdn.net/leonwei/ ...
- javascript 移动鼠标高亮显示改行
主要用到两个事件 onmouseover onmouseout <!DOCTYPE html> <html> <head> <meta charset=&qu ...
- codevs——1154 能量项链(区间DP)
2006年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在Mars星球上,每个Mars人 ...
- SecureCRT设置和Xshell一样的快速命令集(使用快捷键输入命令和密码)
编辑想要的命令 提示:想要回车直接输入[\r]
- iOS网络交互数据格式解析之json
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式.从ios5开 始,apple提供了对json的原生支持,但为了兼容以前的ios版本,我们仍然需要使用第三方库来解析常用 ...
- 刘下记录:ImageView.scaleType 属性全解析(含视频)
0. 译序 原文:Working with the ImageView ImageView 有一个经常使用场景:ImageView 的宽度固定,高度等比例缩放.且 ImageView 在 paddin ...
- ElasticSearch的备份迁移方案
使用插件repository-hdfs插件进行测试 下载地址: https://oss.sonatype.org/content/repositories/snapshots/org/elastics ...