bt-init.sh 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/sh
  2. bt_hciattach="rtk_hciattach"
  3. start_hci_attach()
  4. {
  5. h=`ps | grep "$bt_hciattach" | grep -v grep`
  6. [ -n "$h" ] && {
  7. killall "$bt_hciattach"
  8. sleep 1
  9. }
  10. #bt rfkill init
  11. echo 0 > /sys/class/rfkill/rfkill0/state;
  12. sleep 0.3
  13. echo 1 > /sys/class/rfkill/rfkill0/state;
  14. sleep 0.3
  15. "$bt_hciattach" -n -s 115200 ttyS4 rtk_h5 >/dev/null 2>&1 &
  16. wait_hci0_count=0
  17. while true
  18. do
  19. [ -d "/sys/class/bluetooth/hci0" ] && break
  20. sleep 1
  21. let wait_hci0_count++
  22. [ $wait_hci0_count -eq 16 ] && {
  23. echo "bring up hci0 failed"
  24. exit 1
  25. }
  26. done
  27. echo "BT initial finished"
  28. }
  29. start() {
  30. if [ -d "/sys/class/bluetooth/hci0" ];then
  31. echo "Bluetooth init has been completed!!"
  32. else
  33. start_hci_attach
  34. fi
  35. # hci_is_up=`hciconfig hci0 | grep RUNNING`
  36. # [ -z "$hci_is_up" ] && {
  37. # hciconfig hci0 up
  38. # }
  39. #d=`ps | grep bluetoothd | grep -v grep`
  40. #[ -z "$d" ] && {
  41. #/etc/bluetooth/bluetoothd start
  42. #sleep 1
  43. # echo "bluetoothd should be started here..let's check later"
  44. #}
  45. }
  46. ble_start() {
  47. if [ -d "/sys/class/bluetooth/hci0" ];then
  48. echo "Bluetooth init has been completed!!"
  49. else
  50. start_hci_attach
  51. fi
  52. hci_is_up=`hciconfig hci0 | grep RUNNING`
  53. [ -z "$hci_is_up" ] && {
  54. hciconfig hci0 up
  55. }
  56. MAC_STR=`hciconfig | grep "BD Address" | awk '{print $3}'`
  57. LE_MAC=${MAC_STR/2/C}
  58. OLD_LE_MAC_T=`cat /sys/kernel/debug/bluetooth/hci0/random_address`
  59. OLD_LE_MAC=$(echo $OLD_LE_MAC_T | tr [a-z] [A-Z])
  60. if [ -n "$LE_MAC" ];then
  61. if [ "$LE_MAC" != "$OLD_LE_MAC" ];then
  62. hciconfig hci0 lerandaddr $LE_MAC
  63. else
  64. echo "the ble random_address has been set."
  65. fi
  66. fi
  67. }
  68. stop() {
  69. echo "nothing to do."
  70. }
  71. case "$1" in
  72. start|"")
  73. echo "bt_init start"
  74. start
  75. ;;
  76. stop)
  77. echo "bt_init stop"
  78. stop
  79. ;;
  80. ble_start)
  81. echo "bt_init ble_start"
  82. ble_start
  83. ;;
  84. *)
  85. echo "Usage: $0 {start|stop}"
  86. exit 1
  87. esac