当前位置:编程学习 > html/css >>

高通蓝牙脚本

之前写测试程序使用脚本引发了兴趣,结合具体案例,检验学习成果。终于看懂了一个稍微复杂的shell脚本。里面还是有很多技巧性的东西存在。

声明的变量名,BLUETOOTH_SLEEP_PATH没有用到

[html] 
BLUETOOTH_SLEEP_PATH=/proc/bluetooth/sleep/proto 
LOG_TAG="qcom-bluetooth" 
LOG_NAME="${0}:" 

声明函数

$# ----传递给程序的总的参数数目
$? ----上一个代码或者shell程序在shell中退出的情况,如果正常退出则返回0,反之为非0值。
$* ----传递给程序的所有参数组成的字符串。
    $- ----在Shell启动或使用set命令时提供选项
    $? ----上一条命令执行后返回的值
    $$ ----当前shell的进程号
    $! ----上一个子进程的进程号
    $@ ----所有的参数,每个都用双括号括起
    $n ----位置参数值,n表示位置
    $0 ----当前shell名

[html] 
loge () 

  /system/bin/log -t $LOG_TAG -p e "$LOG_NAME $@" 

 
logi () 

  /system/bin/log -t $LOG_TAG -p i "$LOG_NAME $@" 

 
failed () 

  loge "$1: exit code $2" 
  exit $2 

&符号我理解是后台运行的意思,这样$!表示上一个子进程的进程号,用法可参考test2.sh

[html] 
start_hciattach () 

  /system/bin/hciattach -n $BTS_DEVICE $BTS_TYPE $BTS_BAUD & 
  hciattach_pid=$! 
  logi "start_hciattach: pid = $hciattach_pid" 
  echo 1 > $BLUETOOTH_SLEEP_PATH 

 
kill_hciattach () 

  echo 0 > $BLUETOOTH_SLEEP_PATH 
  logi "kill_hciattach: pid = $hciattach_pid" 
  ## careful not to kill zero or null! 
  kill -TERM $hciattach_pid 
  # this shell doesn't exit now -- wait returns for normal exit 

获取参数并解析,跟c语言中的类似。blnp是不带参数的,t:s:带一个冒号表示带参数、
默认输出USAGE

[html] 
while getopts "blnpt:s:" f 
do 
  case $f in 
  b | l | n | p)  opt_flags="$opt_flags -$f" ;; 
  t)      timeout=$OPTARG;; 
  s)      initial_speed=$OPTARG;; 
  \?)     echo $USAGE; exit 1;; 
  esac 
done 
shift $(($OPTIND-1)) 

涉及case的用法可以学习参考下

[html]
case $POWER_CLASS in 
  1) PWR_CLASS="-p 0" ; 
     logi "Power Class: 1";; 
  2) PWR_CLASS="-p 1" ; 
     logi "Power Class: 2";; 
  3) PWR_CLASS="-p 2" ; 
     logi "Power Class: CUSTOM";; 
  *) PWR_CLASS=""; 
     logi "Power Class: Ignored. Default(1) used (1-CLASS1/2-CLASS2/3-CUSTOM)"; 
     logi "Power Class: To override, Before turning BT ON; setprop qcom.bt.dev_power_class <1 or 2 or 3>";; 
esac 
 
case $LE_POWER_CLASS in 
  1) LE_PWR_CLASS="-P 0" ; 
     logi "LE Power Class: 1";; 
  2) LE_PWR_CLASS="-P 1" ; 
     logi "LE Power Class: 2";; 
  3) LE_PWR_CLASS="-P 2" ; 
     logi "LE Power Class: CUSTOM";; 
  *) LE_PWR_CLASS="-P 1"; 
     logi "LE Power Class: Ignored. Default(2) used (1-CLASS1/2-CLASS2/3-CUSTOM)"; 
     logi "LE Power Class: To override, Before turning BT ON; setprop qcom.bt.le_dev_pwr_class <1 or 2 or 3>";; 
esac 
eval的用法:主要是解析完参数后,将参数作为指令重新执行一次。

再变量中先解析输出参数,eval将参数作为指令执行,因此exit_code_hci_qcomm_init作为变量赋值。如果hci_qcomm_init执行成功后,exit_code_hci_qcomm_init赋值为0,失败则赋值为1。关于用法可参考test1.sh
[html]
eval $(/system/bin/hci_qcomm_init -e $PWR_CLASS $LE_PWR_CLASS && echo "exit_code_hci_qcomm_init=0" || echo "exit_code_hci_qcomm_init=1") 
 
case $exit_code_hci_qcomm_init in 
  0) logi "Bluetooth QSoC firmware download succeeded, $BTS_DEVICE $BTS_TYPE $BTS_BAUD $BTS_ADDRESS";; 
  *) failed "Bluetooth QSoC firmware download failed" $exit_code_hci_qcomm_init;; 
esac 
 

trap用于指定收到某种信号时锁执行的命令。ctrl+c组合键触发的INT信号,执行中断命令。

这句话应该是表示收到 TERM和INT两种信号就执行kill_hciattach,不知道理解的对否

忽略的写法是trap " " TERM

由于TRANSPORT为空,执行默认值,start_hciattach等待进程结束后蓝牙停止。

[html]
trap "kill_hciattach" TERM INT 
 
case $TRANSPORT in 
    "smd") 
        echo 1 > /sys/module/hci_smd/parameters/hcismd_set 
     ;; 
     *) 
        logi "start hciattach" 
        start_hciattach 
 
        wait $hciattach_pid 
        logi "Bluetooth stopped" 
     ;; 
esac 
附件实验程序:

test1.sh

[html] 
#!/bin/bash 
A=123 
eval $( [ -n "$A" ] && echo "exit_code_hci_qcomm_init=0" || echo "exit_code_hci_qcomm_init=1") 
 
echo "$exit_code_hci_qcomm_init" 
test2.sh
[html] view plaincopy
#!/bin/bash 
 
hciattach_pid="" 
 
func1 () 

    echo "test trap " 
    echo "in func1" & 
    hciattach_pid=$! 
    echo $hciattach_pid 
     

 
func1 

补充:web前端 , HTML 5 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,