ARM上的linux如何实现无线网卡的冷插拔和热插拔(3)
- UID
- 1029342
- 性别
- 男
|
ARM上的linux如何实现无线网卡的冷插拔和热插拔(3)
#由于配置文件中的WORKMODE的选项很肯使用小写的sta或是ap,也有可能是Sta和Ap书写方法,通过下面的第一个语句就可以将这些选项统统装化为大写模式,toupper()是一个转换为大写的函数。如果不是STA(station)模式,就赋值为AP模式。
WORKMODE=$(echo $WORKMODE | awk '{ print toupper($0) }')
if [ $WORKMODE != "STA" ] ; then
WORKMODE="AP"
fi
echo "Enable network interface $DEVICE[$NAME] work on $WORKMODE mode." >&2
#下面主要的是理解eval命令,可以通过在shell中拆解来理解,如下
/apps/etc/network >: export WORKMODE=STA
/apps/etc/network >: export IPADDR_STA=192.168.1.166
/apps/etc/network >: echo $WORKMODE
STA
/apps/etc/network >: echo \$IPADDR_$WORKMODE
$IPADDR_STA
/apps/etc/network >: eval "echo \$IPADDR_$WORKMODE"
192.168.1.166
/apps/etc/network >:
也就是把192.168.1.166赋值给ipaddr,同理netmask=255.255.255.0
parser_ip()
{
unset ipaddr netmask
ipaddr=$(eval "echo \$IPADDR_$WORKMODE")
netmask=$(eval "echo \$NETMASK_$WORKMODE")
}
#下面这一个是关掉wifi适配器的函数。
stop_wifi_worker()
{
#stop DHCP work on this NIC
#分解运行分析如下:
/apps/etc/network >: ps | grep dhcp
5385 root 1332 S udhcpc -i eth0
10678 root 1324 S udhcpc -i wlan0
11598 root 1316 S grep dhcp
/apps/etc/network >: ps | grep dhcp | grep -v grep
5385 root 1332 S udhcpc -i eth0
10678 root 1324 S udhcpc -i wlan0
/apps/etc/network >: ps | grep dhcp | grep -v grep | grep wlan
10678 root 1324 S udhcpc -i wlan0
/apps/etc/network >: ps | grep dhcp | grep -v grep | grep wlan | awk '{print $1}'
10678
/apps/etc/network >: ps | grep dhcp | grep -v grep | grep wlan | awk '{print $2}'
root 这样可以获得udhcpc -i wlan0的进程id,并将其杀死。下同。
dhcp_pid=`ps | grep -v grep | grep "dhcp" | grep $DEVICE | awk '{print $1;}'`
if [ -n "$dhcp_pid" ] ; then
kill $dhcp_pid
fi
ifconfig $DEVICE 0.0.0.0
#Stop wpa_supplicant work on STA mode
pid=`ps | grep -v grep | grep "wpa_supplicant" | grep $DEVICE | awk '{print $1;}'`
if [ -n "$pid" ] ; then
kill $pid
sleep 1
fi
if [ -d /var/run/wpa_supplicant ] ; then
rm -rf /var/run/wpa_supplicant
fi
#Stop hostapd work on AP mode
pid=`ps | grep -v grep | grep "hostapd" | awk '{print $1;}'`
if [ -n "$pid" ] ; then
kill $pid
sleep 1
fi
if [ -d /var/run/hostapd ] ; then
rm -rf /var/run/hostapd
fi
}
#配置wifi为station模式。
configure_wifi_sta()
{
parser_ip
#If enable DHCP configure or IP address not configured, then use DHCP get IP address and exit
#man test 可以查看下面判断选项的意思
-n STRING
the length of STRING is nonzero
字符串的长度不为0为真
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
字符串的长度为0为真
-o选项是或运算
if [ -n "$BOOTPROTO" -o -z "$ipaddr" ]; then
BOOTPROTO=$(echo $BOOTPROTO | awk '{ print toupper($0) }')
if [ "$BOOTPROTO" = "DHCP" ] ; then
ifconfig $DEVICE up
#Start wpa_supplicant to work now
#连接路由命令,这里使用的配置文件是apps/etc/network/目录下的wpa_supplicant.conf
/apps/tools/wpa_supplicant -B -Dwext -i$DEVICE -c${network_cfg_dir}/wpa_supplicant.conf
#再为我们的wifi动态分配ip地址,并且是后台运行。
udhcpc -i $DEVICE &
exit
fi
fi |
|
|
|
|
|