使用GPRS模块拨号上网之前需要一直pppd和chat
首先配置内核中的ppp选项,
Make menuconfig
Device dirver --->Network device
│ │ *** CAIF transport drivers *** │ │
│ │ <*> PPP (point-to-point protocol) support │ │
│ │ [*] PPP multilink support (EXPERIMENTAL) │ │
│ │ [*] PPP filtering │ │
│ │ <*> PPP support for async serial ports │ │
│ │ <*> PPP support for sync tty ports │ │
│ │ <*> PPP Deflate compression │ │
│ │ <*> PPP BSD-Compress compression │ │
│ │ <*> PPP MPPE compression (encryption) (EXPERIMENTAL) │ │
│ │ <*> PPP over Ethernet (EXPERIMENTAL)
[guanlei@centos6 GPRS]$ tar -xzf ppp-2.4.4.tar.gz
[guanlei@centos6 GPRS]$ ls
ppp-2.4.4 ppp-2.4.4.tar.gz
[guanlei@centos6 GPRS]$ cd ppp-2.4.4
[guanlei@centos6 ppp-2.4.4]$ ls
Changes-2.3 configure FAQ modules pppdump README.cbcp README.MPPE README.pppoe scripts
chat contrib include PLUGINS pppstats README.eap-srp README.MSCHAP80 README.pwfd SETUP
common etc.ppp linux pppd README README.linux README.MSCHAP81 README.sol2 solaris
[guanlei@centos6 ppp-2.4.4]$ ./configure
Configuring for Linux
Creating Makefiles.
Makefile <= linux/Makefile.top
pppd/Makefile <= pppd/Makefile.linux
pppstats/Makefile <= pppstats/Makefile.linux
chat/Makefile <= chat/Makefile.linux
pppdump/Makefile <= pppdump/Makefile.linux
pppd/plugins/Makefile <= pppd/plugins/Makefile.linux
pppd/plugins/rp-pppoe/Makefile <= pppd/plugins/rp-pppoe/Makefile.linux
pppd/plugins/radius/Makefile <= pppd/plugins/radius/Makefile.linux
pppd/plugins/pppoatm/Makefile <= pppd/plugins/pppoatm/Makefile.linux
[guanlei@centos6 ppp-2.4.4]$ ls
Changes-2.3 contrib linux pppd README.cbcp README.MSCHAP80 README.sol2
chat etc.ppp Makefile pppdump README.eap-srp README.MSCHAP81 scripts
common FAQ modules pppstats README.linux README.pppoe SETUP
configure include PLUGINS README README.MPPE README.pwfd solaris
[guanlei@centos6 ppp-2.4.4]$ cd chat/
[guanlei@centos6 chat]$ ls
chat.8 chat.c Makefile Makefile.linux Makefile.sol2
[guanlei@centos6 chat]$ vim chat.c
[guanlei@centos6 chat]$ vim Makefile
[guanlei@centos6 chat]$ cd ../pppd
[guanlei@centos6 pppd]$ vim auth.c
[guanlei@centos6 pppd]$ vim Makefile
CC =/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc#add by guanlei 2016.1.21
[guanlei@centos6 ppp-2.4.4]$ make
过程省略。。。。
[guanlei@centos6 ppp-2.4.4]$ cd chat/
[guanlei@centos6 chat]$ ls
chat chat.8 chat.c chat.o Makefile Makefile.linux Makefile.sol2
[guanlei@centos6 chat]$ file chat
chat: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
[guanlei@centos6 pppd]$ file pppd
pppd: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
把生成的pppd 和chat 下载到自己的s3c2440开发板上,放在/usr/sbin中
使用gprs上网的时候需要如下几个脚本
一:编写gprs脚本为PPP连接指定了连接使用的设备、使用的控制字符传输速率、指定了硬件加速、溢出控制等。
#/etc/ppp/peers/gprs
# Usage: root>pppd call gprs
#set seriral
/dev/ttyS1
# set baudrate
115200
# set flowdate
nocrtscts
#加上参数-v 告诉 chat命令将其所有的输出/入拷贝到系统记录里(通常是 #/var/log/messages)。-t 6 指定了执行chat该命令的时间为6s。chat脚本的位置可以位##于/etc/目录下,也可以位于/var下,这个可以更加需要灵活设置。
connect '/usr/sbin/chat -v -f /etc/ppp/chat/gprs-connect-chat'
#set debug ,send message to /var/log/messages
debug
#To keep pppd on the terminal
nodetach
#hide-password
# Accept the peer's idea of our local IP address
ipcp-accept-local
# Accept the peer's idea of its (remote) IP address
ipcp-accept-remote
#dial up connetion as the default route
defaultroute
usepeerdns
Noipdefault
具体查看的话,man 8 pppd
二, CHAT脚本
一个简单的chat脚本有下面的结构:
"" AT
OK ATDT dialnumber
CONNECT ""
1) MODEM期望空字符串。这句话的意思直接理解就是MODEM不管收到什么字符串,先发出字符串AT;
2) 期望收到“OK”字符串,然后发送字符串“ATDT dialnumber”
3) 如果收到“CONNECT”,就不再发送,认为数据链路已建立连接。
4)如果需要进行超时控制,就可以加入如下字段:
TIMEOUT 10
5)如果要增加对特殊情况的处理,就加入下面字段:
ABORT BUSY
ABORT NO ANSWER
ABORT RINGING
意思是:如果收到字符串“BUSY”、“NO ANSWER”、“RINGING”就退出执行。
编写自己的chat脚本
>vi gprs-connect-chat
#/etc/ppp/gprs-connect-chat
#Copyright (c) 2016 guanlei [email protected]
# This is second part of the ppp dial script. It will perform the connection
#
ABORT 'BUSY'
ABORT 'NO ANSWER'
ABORT 'NO CARRIER'
ABORT 'NO DIALTONE'
ABORT 'ERROR'
ABORT '\nRING\r\n\r\nRING\r'
TIMEOUT 20
'' \rAT
#set apn
OK AT+CGDCONT=1,"IP","cmnet"
# 拨号上网
OK ATDT*99***1#
SAY " + requesting data connection\n"
CONNECT ''
SAY " + connected\n"
开始测试:
>: ifconfig eth0 down
>: ifconfig
要程序一直在后台运行,如果在前台运行的话,没办法ping,不能测试到底能不能上网
>: pppd call gprs &
>: + requesting data connection
+ connected
Serial connection established.
using channel 2
iUsing interface ppp0
Connect: ppp0 <--> /dev/ttyS1
fconfsent [LCP ConfReq id=0x1
rcvd [LCP ConfReq id=0x1
sent [LCP ConfAck id=0x1
rcvd [LCP ConfRej id=0x1
sent [LCP ConfReq id=0x2
rcvd [LCP ConfAck id=0x2
No secret found for PAP login
sent [PAP AuthReq id=0x1 user="guanlei" password=
rcvd [PAP AuthAck id=0x1 "Login OK"]
Remote message: Login OK
PAP authentication succeeded
sent [CCP ConfReq id=0x1
sent [IPCP ConfReq id=0x1
rcvd [LCP ProtRej id=0x2 80 fd 01 01 00 0f 1a 04 78 00 18 04 78 00 15 03 2f]
Protocol-Reject for 'Compression Control Protocol' (0x80fd) received
consent [IPCP ConfReq id=0x1
rcvd [IPCP ConfReq id=0x1
sent [IPCP ConfAck id=0x1
rcvd [IPCP ConfRej id=0x1
sent [IPCP ConfReq id=0x2
rcvd [IPCP ConfNak id=0x2
sent [IPCP ConfReq id=0x3
rcvd [IPCP ConfAck id=0x3
local IP address 10.110.1.110
remote IP address 192.200.1.21
primary DNS address 211.137.58.20
secondary DNS address 211.137.64.163
>: ifconfig
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.110.1.110 P-t-P:192.200.1.21 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:64 (64.0 B) TX bytes:125 (125.0 B)
>: ping www.baidu.com
PING www.baidu.com (103.235.46.39): 56 data bytes
64 bytes from 103.235.46.39: seq=0 ttl=48 time=555.719 ms
64 bytes from 103.235.46.39: seq=1 ttl=48 time=273.481 ms
--- www.baidu.com ping statistics ---
3 packets transmitted, 2 packets received, 33% packet loss
round-trip min/avg/max = 273.481/414.600/555.719 ms
>: ifconfig
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.110.1.110 P-t-P:192.200.1.21 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:10 errors:0 dropped:0 overruns:0 frame:0
TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:686 (686.0 B) TX bytes:557 (557.0 B)