嵌入式Linux系统实现3G网卡拨号
http://blog.chinaunix.net/uid-9525959-id-3998519.html
http://hi.baidu.com/backtrackwin/item/63bbe432756fbec71b9696a1
本文介绍在嵌入式Linux中,实现3G联网的基本方法。包括驱动配置,和联网的过程。也对在PC上实现3G的过程进行了介绍。硬件:3g usb模块(华为ce189的3g网卡)+一张sim卡(电信cdma2000)软件系统:Linux主要内容:一、在pc端确认拨号功能可用二、在嵌入式端的实现三、其它一、在pc端确认拨号功能可用==================================在windows下直接插入网卡即可自动安装其驱动,比较简单直观,其实linux下连接也很简单,这里给出linux下的连接方式。系统:Ubuntu 12.041、将sim卡插入3g网卡,再将3g网卡插入pc。2、对3g网卡进行网络配置这里有图形和命令行两种方式。后面在嵌入式系统中会采用命令行方式配置3g信息。2.1、图形方式通过图形设置比较直观,可大致了解3g配置所需的信息。编辑链接->移动宽带->添加(新连接名为china telecom 1)具体配置:(1)IP配置:自动(即ppp自动)(2)移动宽带:编号:#777用户名:ctnet@mycdma.cn密码:vnet.mobi(3)ppp设置:默认。即chap、pap等鉴权方式都选中。(4)连接保存上述配置后,即可通过点击新建的连接"china telecom 1"连上。(5)确认联网情况ifconfig可见网络的名字为ppp0通过firefox即可上网。2.2命令行脚本方式:通过pppd拨号上网,可以对连接过程有更深一步的了解,且使用预先配置好的信息,无需用户干预。如下(1)pppd对cdma2000的拨号脚本#cat /etc/ppp/peers/cdma2000ce189#This is pppd script used by huawei ce189./dev/ttyUSB0115200crtsctsmodemdebuglocknodetachusepeerdnsdefaultroutenoipdefaultnoauthuser "ctnet@mycdma.cn"password "vnet.mobi"#lcp-echo-failure 5#lcp-echo-interval 30#persist0.0.0.0:0.0.0.0connect '/usr/sbin/chat -s -v -f /etc/ppp/cdma2000-connect-chat'这里,注意,noauth选项一定要有,这里"lcp-echo-failure 5"表示5次心跳确认失败后会终止连接、"lcp-echo-interval 30"表示心跳确认时间为30秒,"persist"表示基于前面心跳失败后,不会终止连接而是重新拨号。(2)pppd拨号脚本调用的chat拨号脚本#cat /etc/ppp/cdma2000-connect-chatTIMEOUT 5ABORT "DELAYED"ABORT "BUSY"ABORT "ERROR"ABORT "NO DIALTONE"ABORT "NO CARRIER""" AT"OK-+++c-OK" ATH0TIMEOUT 40"" ATOK ATDT#777CONNECT ""这里,脚本含义参见"man chat",基本每行是"<期望接收的字符> <发送字符>"。(3)调用pppd拨号脚本:#pppd call cdma2000ce189这里,即调用前面的脚本配置并连接上网。结果和图形的一样。通过"Ctrl-C"即可断开连接。至此,即可通过图形或命令行方式实现3g上网。二、在嵌入式端的实现==================================在嵌入式板子上实现3G连接过程类似,不过需要可运行在嵌入式板上的工具。内核版本:2.6.181、交叉编译必要的工具(1)libusb-1.0.9:用于编译usb-modeswitch-2.0.1http://sourceforge.net/projects/libusb/postdownload?source=dlp(2)usb-modeswitch-2.0.1:用于切换网卡的模式http://www.draisberghof.de/usb_modeswitch/#download(3)usb-modeswitch-data-20130807用于切换时指定切换的型号。http://www.draisberghof.de/usb_modeswitch/#download(4)ppp-2.4.4用于拨号的pppd,和发送拨号命令的chat程序(可单独编译)。ftp://ftp.samba.org/pub/ppp以上程序可参考如下Makefile进行编译:#cat MakefileCROSS_TOOL :=/usr/local/montavista/pro/devkit/arm/v5t_le/bin/arm_v5t_le-CC=$(CROSS_TOOL)gccAR=$(CROSS_TOOL)arLD=$(CROSS_TOOL)g++STRIP=$(CROSS_TOOL)stripCPLUSPLUS=$(CROSS_TOOL)g++LIBRARY_LINK = $(CROSS_TOOL)ld -oCUR_DIR:=$(PWD)THE_SRC:=usb-modeswitch-2.0.1THE_BUILD:=build_pathTHE_INSTALL:=install_pathdepend:mkdir -p ${THE_BUILD}/${THE_BUILD}mkdir -p ${CUR_DIR}/${THE_INSTALL}cd ${CUR_DIR}/${THE_BUILD} && ../${THE_SRC}/configure --build=i686 --host=arm-linux --prefix=${CUR_DIR}/${THE_INSTALL}build:cd ${CUR_DIR}/${THE_BUILD} && makeinstall:cd ${CUR_DIR}/${THE_BUILD} && make installclean:cd ${CUR_DIR}/${THE_BUILD} && make cleanrm -rf ${CUR_DIR}/${THE_INSTALL}rm -rf ${CUR_DIR}/${THE_BUILD}.PHONY:depend libs clean关键在于CROSS_TOOL和THE_SRC的设定,以及代码中的Makefile。具体过程省略,请参考参考资料。2、配置内核打开ppp相关配置:Device Drivers ---> [*] Network device support ---> <*> PPP (point-to-point protocol) support [*] PPP multilink support (EXPERIMENTAL) [*] PPP filtering <*> PPP support for async serial ports <*> PPP support for sync tty ports 打开usb串口相关配置:[*] USB support ---> <*> USB Serial Converter support ---> [*] USB Serial Console device support [*] USB Generic Serial Driver <*> USB driver for GSM and CDMA modems3、确定产品型号3.1、配置好的内核,先编译后,启动系统,插入模块usbserial.ko、option.ko。3.2、插入3g网卡,#lsusb这样会显示相应信息:12d1:1505这里,若无lsusb命令,可以查看/sys/bus/usb/devices/下有相应的设备信息,其中:# cat /sys/bus/usb/devices/1-1/idProduct 1505# cat /sys/bus/usb/devices/1-1/idVendor 12d1这里假设是1-1。3.3、切换网卡模式可理解为以前usb网卡都被识别为usb存储类型,这里切换相当于将usb网卡拔掉,再改变模式再插上,这样识别成合适的usb类型。#usb_modeswitch -c /etc/usb_modswitch.d/12d1:1505切换后查看切换后的设备信息:# cat /sys/bus/usb/devices/1-1/idProduct 140c# cat /sys/bus/usb/devices/1-1/idVendor 12d1这里,12d1实际是厂商号“华为”,140c实际是该厂商的设备产品号。4、修改内核代码,添加相应设备:#vim kernel/drivers/usb/serial/option.c添加内容:...#define HUAWEI_VENDOR_ID 0x12D1...#define HUAWEI_PRODUCT_CE189 0x140c...static struct usb_device_id option_ids[] = {...{ USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_CE189) },...};...static struct usb_device_id option_ids1[] = {...{ USB_DEVICE(HUAWEI_VENDOR_ID, HUAWEI_PRODUCT_CE189) },...};...5、拨号5.1、对修改好的内核模块编译后,重启系统并插入模块:#insmod usbserial.ko#insmod option.ko5.2、切换3g网卡的usb模式#usb_modeswitch -c /etc/usb_modswitch.d/12d1:1505...[ 124.770000] option 1-1:1.0: GSM modem (1-port) converter detected[ 124.780000] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB0[ 124.790000] option 1-1:1.1: GSM modem (1-port) converter detected[ 124.800000] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB1[ 124.810000] option 1-1:1.2: GSM modem (1-port) converter detected[ 124.810000] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB2[ 124.830000] option 1-1:1.3: GSM modem (1-port) converter detected[ 124.830000] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB3[ 124.850000] option 1-1:1.4: GSM modem (1-port) converter detected[ 124.860000] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB4...#mknod /dev/ttyUSB0 c 188 0#mknod /dev/ttyUSB1 c 188 1#mknod /dev/ttyUSB2 c 188 2#mknod /dev/ttyUSB3 c 188 3#mknod /dev/ttyUSB4 c 188 4这里,根据切换后的提醒,建立相应的通讯设备节点。5.3、拨号按照和pc一样的方式,建立pppd拨号脚本和chat脚本,并拨号:#pppd cdma2000ce189最后会连接成功。5.4、联网这里,拔掉网线,确保只有3g网络# ifconfigeth0 Link encap:Ethernet HWaddr 74:37:2F:00:00:00 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:338 errors:0 dropped:0 overruns:0 frame:0 TX packets:304 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:166139 (162.2 KiB) TX bytes:39884 (38.9 KiB) Interrupt:52 Base address:0x7000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:12 errors:0 dropped:0 overruns:0 frame:0 TX packets:12 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1669 (1.6 KiB) TX bytes:1669 (1.6 KiB)ppp0 Link encap:Point-to-Point Protocol inet addr:183.41.122.220 P-t-P:183.41.0.1 Mask:255.255.255.255 UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1 RX packets:3 errors:0 dropped:0 overruns:0 frame:0 TX packets:11 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:3 RX bytes:66 (66.0 B) TX bytes:3164 (3.0 KiB)路由:# routeKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface183.41.0.1 * 255.255.255.255 UH 0 0 0 ppp0default * 0.0.0.0 U 0 0 0 ppp05.5、ping百度:# ping www.baidu.comping: bad address 'www.baidu.com'# ping 115.239.210.27PING 115.239.210.27 (115.239.210.27): 56 data bytes64 bytes from 115.239.210.27: seq=0 ttl=56 time=2260.913 ms