在嵌入式Linux下GPRS上網(wǎng)方案
筆記1
硬/軟件環(huán)境
基于S3C2410的嵌入式系統(tǒng),COM1連接PC,COM2連接SIM300 GPRS模塊。
該系統(tǒng)運行在Linux 2.6.14操作系統(tǒng)下,使用ppp套件通過SIM300進行PPP撥號。
一。讓Linux內(nèi)核支持PPP
進入Linux內(nèi)核目錄,執(zhí)行#make menuconfig
Network Device Support à
<*> PPP (point-to-point protocol) support
[*] PPP multilink support
<*> PPP support for async serial ports
<*> PPP support for sync tty ports
<*> SLIP (serial line) support
[*] CSLIP compressed headers
二:ppp套件安裝
下載ppp:ftp://ftp.samba.org/pub/ppp ×最新版本為2.4.4
將ppp-2.4.4.tar.gz解壓至目錄
×這里默認ppp源碼目錄為$(PPP)
#tar zxvf ppp-2.4.4.tar.gz
然后交叉編譯ppp:
#cd $(PPP)
#./configure
#make CC=/usr/local/arm/3.4.1/bin/arm-linux-gcc ×這里指定交叉編譯器
將ppp套件安裝至嵌入式系統(tǒng)中:
×這里默認可執(zhí)行文件在嵌入式系統(tǒng)下的目錄為$(EMB_BIN)
#cp $(PPP)/chat/chat $(EMB_BIN)
#cp $(PPP)/pppd/pppd $(EMB_BIN)
#cp $(PPP)/pppdump/pppdump $(EMB_BIN)
#cp $(PPP)/pppstats/pppstats $(EMB_BIN)
×這里默認嵌入式系統(tǒng)的etc目錄為$(EMB_ETC)
#mkdir $(EMB_ETC)/ppp
#cp $(PPP)/etc.ppp/* $(EMB_ETC)/ppp
三:ppp套件配置
$(EMB_BIN)/dial-on.sh (GPRS啟動腳本)
- #!/bin/sh
- #define dial_on function
- dial_on()
- {
- #test if pppd is running
- pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
- if [ $pppd_stat -gt 0 ]
- then
- echo "ppp connection's already started."
- else
- #close ethernet interface
- ifconfig eth0 down
-
- #ppp start
- pppd modem /dev/ttyS1 57600 nocrtscts lock connect "chat -v -f /etc/ppp/gprs-connect" user "" noauth debug defaultroute
- # pppd配置說明:
- # ttyS1:連接GPRS模塊SIM300的串口
- # 57600:GPRS的撥號速率
- # nocrtscts:無流控
- # lock:鎖定設(shè)備
- # connect “chat –v –f /etc/ppp/gprs-connect”:GPRS連接腳本文件
- # user “”:用戶名,這里是無
- # noauth:無需認證
- # debug:輸出調(diào)試信息
- # defaultroute:此撥號連接作為默認路由
- echo "ppp is starting..."
- fi
- }
-
- #dial on gprs
- dial_on
- #wait for ppp's init
- sleep 5
- pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
- if [ $pppd_stat -eq 0 ]
- then
- echo "trying 2nd time to call ppp"
- dial_on
- sleep 5
- fi
- pppd_stat=`ifconfig|grep ppp|wc -l|cut -b 7-7`
- if [ $pppd_stat -eq 0 ]
- then
- echo "pppd error!"
- echo "please check pppd's config files
$(EMB_BIN)/dial-off.sh (關(guān)閉GPRS連接腳本)
- #!/bin/sh
- #get pppd's pid
- pid=`pidof pppd`
-
- #if pppd process is running
- if [ -n $pid ]
- then
- #kill pppd
- kill $pid
-
- #open the ethernet interface
- ifconfig eth0 up
-
- echo "ppp connection is closed."
- else
- echo "ppp connection isn't existed.
$(EMB_ETC)/ppp/gprs-connect (GPRS連接配置文件)
- #GPRS連接超時設(shè)置
- TIMEOUT 60
- #若MODEM遇到BUSY、ERROR、NO CARRIER等信息時,停止撥號
- ABORT "BUSY"
- ABORT "ERROR"
- ABORT "NO CARRIER"
- #外送“AT”指令
- '' AT
- #當?shù)玫健癘K”回應(yīng)時,外送AT+CGDCONT=1,"IP","CMNET"命令
- "OK" "AT+CGDCONT=1,/042IP/042,/042CMNET/042"
- #當?shù)玫健癘K”回應(yīng)時,外送ATDT*99***1#命令
- "OK" "ATDT*99***1#"
- #當?shù)玫健癈ONNECT”回應(yīng)時,撥號結(jié)束,程序退出
- "CONNECT"
$(EMB_ETC)/ppp/pap-secrets (GPRS認證配置文件)
- # Secrets for authentication using PAP
- # client server secret IP addresses
- '' * '' *
說明
(1)還需要在$(EMB_ETC)/ppp目錄下創(chuàng)建指向$(EMB_ETC)/resolv.conf的鏈接,用于指定PPP連接的DNS。
(2)在ppp連接時,需要關(guān)閉eth連接。在腳本中已經(jīng)設(shè)置好了,首先關(guān)閉eth連接,然后進行ppp連接,在ppp連接完成時,再開啟eth連接。
(3)最好在系統(tǒng)中開啟syslogd進程,這樣在/var/log/messages文件中會記錄GPRS進行撥號的DEBUG信息,便于調(diào)試。
(4)運行撥號腳本后,可以使用#ifconfig查看PPP連接信息。
筆記2:
arm上成功實現(xiàn)ppp撥號的腳本:
ppp-on:
- #!/bin/sh
- pppd modem -d -detach lock /dev/ttySAC0 19200 kdebug 4 file /etc/ppp/options crtscts noipdefault netmask 255.255.255.0 defaultroute connect /etc/ppp/chat-script
ppp-off:
- #!/bin/sh
- ######################################################################
- #
- # Determine the device to be terminated.
- #
- if [ "$1" = "" ]; then
- DEVICE=ppp0
- else
- DEVICE=$1
- fi
- ######################################################################
- #
- # If the ppp0 pid file is present then the program is running. Stop it.
- if [ -r /var/run/$DEVICE.pid ]; then
- kill -INT `cat /var/run/$DEVICE.pid`
- #
- # If the kill did not work then there is no process running for this
- # pid. It may also mean that the lock file will be left. You may wish
- # to delete the lock file at the same time.
- if [ ! "$?" = "0" ]; then
- rm -f /var/run/$DEVICE.pid
- echo "ERROR: Removed stale pid file"
- exit 1
- fi
- #
- # Success. Let pppd clean up its own junk.
- echo "PPP link to $DEVICE terminated."
- exit 0
- fi
- #
- # The ppp process is not running for ppp0
- echo "ERROR: PPP link is not active on $DEVICE"
- exit 1
chat-script:
- #!/bin/sh
- exec chat -v /
- TIMEOUT 5 /
- ABORT "BUSY" /
- ABORT "ERROR" /
- ABORT "NO CARRIER" /
- '' /rAT /
- OK 'AT+CGDCONT=1,"IP","CMNET"' /
- OK 'ATDT*99***1#' /
- CONNECT '' /
設(shè)置DNS的resove.conf:
- nameserver 211.136.20.203
- nameserver 211.136.17.107
筆記3:
GPRS自動撥號腳本(真正的實時監(jiān)控,斷線自動重撥):
http://www.tvrofans.org/viewthread.php?tid=8117&extra=&page=1
開機自動運行,實時監(jiān)控,斷線自動重撥
把文件傳到DM里,設(shè)置文件屬性為755,然后把啟動路徑加到init文件里即可
原設(shè)置為5秒去檢測一次,是以1字節(jié)去PING
- #!/bin/sh
- #請把dns1,dns2修改成拼得通的DNS,開機自動運行,實時監(jiān)控,斷線自動重撥
- dns1="211.95.193.97"
- dns2="211.136.20.203"
- sleep 8
- #/bin/pppd call gprs-siem &
- sleep 12
- while true
- do
- ping -s 1 -c 1 $dns1 #去PING第一個DNS
- if [ "$?" != "0" ] #假如PING不通
- then
- ping -s 1 -c 2 $dns2 #去PING第二個DNS
- if [ "$?" != "0" ] #假如PING不通
- then
- killall pppd #結(jié)束PPPD進程
- pppd call gprs-siem & #再去撥號
- sleep 12 #等待12秒
- sleep 5 #如果是PING DNS2通的話就直接等待5秒
- fi
- else
- sleep 5 #如果是PING DNS1通的話就直接等待5秒(一般要設(shè)置多長時間去PING請改這里)
- fi
- done
代碼簡明!!它相當于在后臺時時去PING一個DNS發(fā)現(xiàn)真正地掉線,它才會去重新?lián)芴枺?!此版本?jīng)測試通過才發(fā)表。
-
- **************************
- * *
- * The Gemini Project *
- * *
- **************************
- www.tvrofans.org! \' v1 Y# R2 P2 h: K
- welcome on your dreambox! - Kernel 2.6.9 (08:14:21).
- dreambox login: root
- BusyBox v1.01 (2007.10.23-19:23+0000) Built-in shell (ash)
- Enter 'help' for a list of built-in commands.
- root@dreambox:~> /bin/sh /var/etc/ppp/aa
- root@dreambox:~> AT
- OK
- ATZ
- OK
- ATH
- OK
- ATE1
- OK
- AT+CGDCONT=1,"IP","cmnet"( f$ m0 V3 U' o
- OK
- ATD*99***1#
- CONNECT
- Serial connection established.
- using channel 2
- Using interface ppp0
- Connect: ppp0 <--> /dev/tts/0
- Warning - secret file /etc/ppp/pap-secrets has world and/or group access
- sent [LCP ConfReq id=0x1
- ]
- rcvd [LCP ConfAck id=0x1
- rcvd [LCP ConfReq id=0x3 <asyncmap 0xa0000> <pcomp> <accomp> <magic 0x2ab30537>
- <auth chap MD5>]
- sent [LCP ConfNak id=0x3 <auth pap>]
- rcvd [LCP ConfReq id=0x5 <asyncmap 0xa0000> <pcomp> <accomp>
- ]
- sent [LCP ConfAck id=0x5
- <auth pap>]
- Warning - secret file /etc/ppp/pap-secrets has world and/or group access
- sent [PAP AuthReq id=0x1 user="beeline" password=<hidden>]
- rcvd [PAP AuthAck id=0x1 ""]
- PAP authentication succeeded
- sent [CCP ConfReq id=0x1 <deflate 15> <deflate(old#) 15> <bsd v1 15>]
- sent [IPCP ConfReq id=0x1 ]
- rcvd [LCP ProtRej id=0x6 80 fd 01 01 00 0f 1a 04 78 00 18 04 78]
- sent [IPCP ConfReq id=0x1 ]
- sent [IPCP ConfReq id=0x1 ]
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275420253.8 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275420253.8/275420253.8/275420253.8 ms
- sent [IPCP ConfReq id=0x1 ]
- sent [IPCP ConfReq id=0x1 ]
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275425386.3 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss9
- round-trip min/avg/max = 275425386.3/275425386.3/275425386.3 ms
- sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275430517.7 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275430517.7/275430517.7/275430517.7 ms
- sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
- sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275435653.2 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275435653.2/275435653.2/275435653.2 ms
- sent [IPCP ConfReq id=0x1 ]
- sent [IPCP ConfReq id=0x1 <addr 0.0.0.0> <ms-dns1 0.0.0.0> <ms-dns3 0.0.0.0>]
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275440784.9 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275440784.9/275440784.9/275440784.9 ms
- IPCP: timeout sending Config-Requests
- sent [LCP TermReq id=0x2 "No network protocols running"]
- rcvd [LCP TermAck id=0x2 "No network protocols running"]
- Connection terminated.
- Sending break to the modem
- PDP context detached
- Serial link disconnected.
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275445915.2 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275445915.2/275445915.2/275445915.2 ms
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275451048.9 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275451048.9/275451048.9/275451048.9 ms
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275456180.4 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275456180.4/275456180.4/275456180.4 ms
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275461314.4 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275461314.4/275461314.4/275461314.4 m
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275466446.2 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275466446.2/275466446.2/275466446.2 ms
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
- 9 bytes from 211.95.193.97: icmp_seq=0 ttl=244 time=275471577.9 ms
- --- 211.95.193.97 ping statistics ---
- 1 packets transmitted, 1 packets received, 0% packet loss
- round-trip min/avg/max = 275471577.9/275471577.9/275471577.9 ms
- PING 211.95.193.97 (211.95.193.97): 1 data bytes
大家會問這樣一直PING下去擔心流量問題,浪費一些流量是墾定的,
不過我們是以1個字節(jié)去PING 加上返回的值一共是9個字節(jié),也就是說5秒用9個字節(jié)
D1 U% ]& i
一個小時用9*12*60是一個小時6480字節(jié)=6。328125K
也就是說這樣一個小時加6.33K的流量
大家還是擔心的話可以改一下腳本,比如改60秒去PING一次啦,等等,都能有效去省流量?。?/p>
FROM:http://blog.csdn.net/seven407/article/details/4904877