分类: LINUX
2008-10-20 22:08:46
第一步:编写GPRS的内核驱动程序
因为我用的开发板基于ARM920T,linux内核中没有mc35i的驱动程序,所以自己写了个驱动程序,重新编译了内核。驱动程序代码如下:
编译内核后,要在文件系统中创建设备,命令如下:
mknod Mc35i c 253 0
第二步:移植ppp协议
由于arm-linux上没有提供使用ppp的pppd, chat应用程序,所以需要向开发板上移植。方法就和普通的应用程序一样,在pc机上的develop目录下面编译,然后将生成的可执行程序复制到Ramdisk文件系统中,在下次重新下载内核和文件系统时使用新的文件系统,就可以在arm-linux上运行pppd了。具体过程如下:
1,在samba.org下载ppp-2.4.3.tar.gz后安如下操作:
tar zxvf ppp-2.4.3.tar.gz
2,由于在ppp根文件夹目录下面没有makefile文件,所以要自己添加:
# PPP top-level Makefile for Linux.
DESTDIR = /usr/local
BINDIR = $(DESTDIR)/sbin
INCDIR = $(DESTDIR)/include
MANDIR = $(DESTDIR)/share/man
ETCDIR = /etc/ppp
# uid 0 = root
INSTALL= install
all:
cd chat; $(MAKE) $(MFLAGS) all
cd pppd/plugins; $(MAKE) $(MFLAGS) all
cd pppd; $(MAKE) $(MFLAGS) all
cd pppstats; $(MAKE) $(MFLAGS) all
cd pppdump; $(MAKE) $(MFLAGS) all
install: $(BINDIR) $(MANDIR)/man8 install-progs install-devel
install-progs:
cd chat; $(MAKE) $(MFLAGS) install
cd pppd/plugins; $(MAKE) $(MFLAGS) install
cd pppd; $(MAKE) $(MFLAGS) install
cd pppstats; $(MAKE) $(MFLAGS) install
cd pppdump; $(MAKE) $(MFLAGS) install
install-etcppp: $(ETCDIR) $(ETCDIR)/options $(ETCDIR)/pap-secrets
$(ETCDIR)/chap-secrets
install-devel:
cd pppd; $(MAKE) $(MFLAGS) install-devel
$(ETCDIR)/options:
$(INSTALL) -c -m 644 etc.ppp/options $@
$(ETCDIR)/pap-secrets:
$(INSTALL) -c -m 600 etc.ppp/pap-secrets $@
$(ETCDIR)/chap-secrets:
$(INSTALL) -c -m 600 etc.ppp/chap-secrets $@
$(BINDIR):
$(INSTALL) -d -m 755 $@
$(MANDIR)/man8:
$(INSTALL) -d -m 755 $@
$(ETCDIR):
$(INSTALL) -d -m 755 $@
clean:
rm -f `find . -name '*.[oas]' -print`
rm -f `find . -name 'core' -print`
rm -f `find . -name '*~' -print`
cd chat; $(MAKE) clean
cd pppd/plugins; $(MAKE) clean
cd pppd; $(MAKE) clean
cd pppstats; $(MAKE) clean
cd pppdump; $(MAKE) clean
dist-clean: clean
rm -f Makefile `find . -name Makefile -print`
#kernel:
# cd linux; ./kinstall.sh
# no tests yet, one day...
installcheck:
true
至于其他目录下的makefile可以复制该目录下的makefile.linux即可。交叉编译后得到chat、pppd、pppstats、pppdump四个应用程序,拷贝到嵌入式系统的文件系统ramdisk/target/usr/sbin目录下,权限改成755即可。
然后在ramdisk/target/dev目录下建立ppp节点:
mknod ppp -c 108,0
运行PPPD看看, 若说segment fault, 则是内核没有配置mmap, 或者把刚才的Makefile中的 HAVE_MMAP改为FALSE, 因为pppd中调用了mmap().
PPP拨号脚本有ppp-on:
#!/bin/sh
#
# Script to initiate a ppp connection. This is the first part of the
# pair of scripts. This is not a secure pair of scripts as the codes
# are visible with the 'ps' command. However, it is simple.
#
# These are the parameters. Change as needed.
TELEPHONE=*99***1# # The telephone number for the connection
ACCOUNT= # The account name for logon (as in 'George Burns')
PASSWORD= # The password for this account (and 'Gracie Allen')
LOCAL_IP=0.0.0.0 # Local IP address if known. Dynamic = 0.0.0.0
REMOTE_IP=0.0.0.0 # Remote IP address if desired. Normally 0.0.0.0
NETMASK=255.255.255.0 # The proper netmask if needed
#
# Export them so that they will be available at 'ppp-on-dialer' time.
export TELEPHONE ACCOUNT PASSWORD
#
# This is the location of the script which dials the phone and logs
# in. Please use the absolute file name as the $PATH variable is not
# used on the connect option. (To do so on a 'root' account would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=/mnt/ppp-on-dialer #这里改成自己文件所在的路径
#
# Initiate the connection
#
# I put most of the common options on this command. Please, don't
# forget the 'lock' option or some programs such as mgetty will not
# work. The asyncmap and escape will permit the PPP link to work with
# a telnet or rlogin connection. You are welcome to make any changes
# as desired. Don't use the 'defaultroute' option if you currently
# have a default route to an ethernet gateway.
#
exec /usr/sbin/pppd debug lock modem /dev/ttyS1 19200
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT
如果是Modem拨号,ppp-on为:
#!/bin/sh
#
# Script to initiate a ppp connection. This is the first part of the
# pair of scripts. This is not a secure pair of scripts as the codes
# are visible with the 'ps' command. However, it is simple.
#
# These are the parameters. Change as needed.
TELEPHONE=016300 # The telephone number for the connection
ACCOUNT=163 # The account name for logon (as in 'George Burns')
PASSWORD=163 # The password for this account (and 'Gracie Allen')
LOCAL_IP=0.0.0.0 # Local IP address if known. Dynamic = 0.0.0.0
REMOTE_IP=0.0.0.0 # Remote IP address if desired. Normally 0.0.0.0
NETMASK=255.255.255.0 # The proper netmask if needed
#
# Export them so that they will be available at 'ppp-on-dialer' time.
export TELEPHONE ACCOUNT PASSWORD
#
# This is the location of the script which dials the phone and logs
# in. Please use the absolute file name as the $PATH variable is not
# used on the connect option. (To do so on a 'root' account would be
# a security hole so don't ask.)
#
DIALER_SCRIPT=/mnt/ppp-on-dialer-modem
#
# Initiate the connection
#
# I put most of the common options on this command. Please, don't
# forget the 'lock' option or some programs such as mgetty will not
# work. The asyncmap and escape will permit the PPP link to work with
# a telnet or rlogin connection. You are welcome to make any changes
# as desired. Don't use the 'defaultroute' option if you currently
# have a default route to an ethernet gateway.
#
exec /usr/sbin/pppd debug lock modem crtscts /dev/ttyS1 115200
asyncmap 20A0000 escape FF kdebug 0 $LOCAL_IP:$REMOTE_IP
noipdefault netmask $NETMASK defaultroute connect $DIALER_SCRIPT
GPRS的ppp-on-dialer脚本如下:
#!/bin/sh
#
# This is part 2 of the ppp-on script. It will perform the connection
# protocol for the desired connection.
#
exec chat -v
TIMEOUT 3
ABORT ' BUSY '
ABORT ' NO ANSWER '
ABORT ' RINGING RINGING '
'' AT
TIMEOUT 30
OK AT+CMEE=1
OK AT+CGDCONT=1,"ip","cmnet"
OK AT+CGATT=1
OK ATDT*99***1#
CONNECT
Modem的ppp-on-dialer如下:
#!/bin/sh
#
# This is part 2 of the ppp-on script. It will perform the connection
# protocol for the desired connection.
#
exec chat -v
TIMEOUT 3
ABORT ' BUSY '
ABORT ' NO ANSWER '
ABORT ' RINGING RINGING '
'' AT
TIMEOUT 30
OK ATDT$TELEPHONE
sername:--sername: $ACCOUNT
assword: $PASSWORD第四步:C程序的实现
//GPRS初始化
int InitGPRS(char *IP)
...{
FILE *ppp0;
int status;
char buf[100];
int nIndex = 0;
char *ch=NULL,*nlch=NULL;
//判断是否存在IP
status = system("ifconfig ppp0>/ppp0");
if(status == 127||status == -1||status != 0)
...{
//打开设备
if(confData->WorkType == TYPE_GPRS)
...{
if ((mc35i = open("/dev/Mc35i",O_RDWR|O_NOCTTY))<0)
...{
printf("Warning:Open GPRS Mc35i failed! ");
return -1;
}
else
...{
printf("OK:Open GPRS is ok! ");
}
while(1) 
...{
nIndex++;
if(nIndex > 30)
return -1;
if (!GPRSPowerIsOn())
...{
printf("Warning:gprs power is not on! ");
write(mc35i,"0",1); //ignition mc35i
sleep(2);
}
else
break;
}
}
//执行ppp拨号程序
if (!vfork())
...{
//GPRS方式
if(confData->WorkType == TYPE_GPRS)
...{
printf("Exec GPRS ppp-on...... ");
if(execl("./ppp-on","ppp-on",NULL)<0)
//if (execvp("pppd",pppdScript)<0)
...{
printf("Exec ppp-on error:%s! ",strerror(errno));
return -1;
}
}
//MODEM方式
else
...{
printf("Exec MODEM ppp-on...... ");
if(execl("./ppp-on-modem","ppp-on",NULL)<0) 
...{
printf("Exec ppp-on error:%s! ",strerror(errno));
return -1;
}
}
}
sleep(40);
//获取分配的IP地址
printf("Exec ifconfig ppp0 > ppp0...... ");
status = system("ifconfig ppp0>/ppp0");
if(status == 127||status == -1||status != 0)
...{
printf("Exec ifconfig ppp0 error! ");
GPRSPoweroff();
return -1;
}
}
printf("Open file ppp0...... ");
if ((ppp0 = fopen("/ppp0","r")) <= 0)
...{
printf("Open ppp0 info file fail! ");
GPRSPoweroff();
return -1;
} 
/**//*格式:
# ifconfig ppp0
ppp0 Link encap:Point-to-Point Protocol
inet addr:211.74.48.254 P-t-P:211.74.48.1 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1492 Metric:1
RX packets:5 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
*/
if (fread(buf,sizeof(char),sizeof(buf),ppp0)>0)
...{
printf("Get ppp0 IP...... ");
ch = strchr(buf,' ');
ch = strchr(ch,':');
nlch = strchr(ch,'P');
memcpy(IP,++ch,(nlch-ch)-2);
*(IP+(nlch-ch)-2)='