controll 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/bin/bash
  2. __DIR__=$(cd $(dirname $0); pwd)
  3. log_dir=/var/log/kbm
  4. mkdir -p ${log_dir}
  5. # 安装必要的依赖
  6. function install()
  7. {
  8. sudo apt-get install bluez bluez-tools -y
  9. sudo apt-get install bluez-firmware python-bluez python-dev python-pip -y
  10. sudo pip install evdev
  11. sudo apt install git python python3 python-dev python3-dev python3-dbus python3-pyudev python3-evdev -y
  12. sudo apt-get install python-dbus -y
  13. sudo apt-get install tmux -y
  14. sudo cp dbus/org.thanhle.btkbservice.conf /etc/dbus-1/system.d
  15. sudo cp /lib/systemd/system/bluetooth.service ./bluetooth.service.bk
  16. sudo cp bluetooth.service /lib/systemd/system/bluetooth.service
  17. sudo systemctl daemon-reload
  18. sudo /etc/init.d/bluetooth start
  19. sudo apt-get install python3-gi -y
  20. }
  21. # 卸载
  22. function uninstall()
  23. {
  24. if [ -f "bluetooth.service.bk" ] ; then
  25. sudo cp bluetooth.service.bk /lib/systemd/system/bluetooth.service
  26. sudo systemctl daemon-reload
  27. sudo /etc/init.d/bluetooth start
  28. fi
  29. }
  30. # 帮助信息
  31. function help()
  32. {
  33. echo "./controll [ install | uninstall | config-address | config-name <name> | boot | start-keyboard | start-mouse ]"
  34. }
  35. # 自动更新服务器的蓝牙地址
  36. function update_bluetooth_address()
  37. {
  38. address=$(hciconfig hci0 | awk '/BD Address: /{print $3}')
  39. if [[ ! -z "${address}" ]] ; then
  40. sed -i -e "s/BLUETOOTH_ADDRESS_PLACEHOLDER/${address}/" server/btk_server.py
  41. fi
  42. }
  43. # 设置服务器的蓝牙地址
  44. function set_bluetooth_name()
  45. {
  46. bluetooth_name=$1
  47. sed -i -e "s/DEVICE_NAME_PLACEHOLDER/${bluetooth_name}/" ${__DIR__}/server/btk_server.py
  48. }
  49. # 启动蓝牙系统服务
  50. function start_bluetooth()
  51. {
  52. # 关闭蓝牙设备
  53. sudo hciconfig hci0 down
  54. # 启动蓝牙系统服务
  55. sudo /etc/init.d/bluetooth start
  56. }
  57. function stop_bluetooth()
  58. {
  59. sudo hciconfig hci0 down
  60. sudo /etc/init.d/bluetooth stop
  61. }
  62. # 启动蓝牙连接及事件发送服务
  63. function start_main_service()
  64. {
  65. ${__DIR__}/server/btk_server.py 2>&1 >> ${log_dir}/main.log &
  66. }
  67. function stop_main_service()
  68. {
  69. stop btk_server.py
  70. }
  71. # 启动键盘事件监听及发送服务
  72. function start_keyboard()
  73. {
  74. ${__DIR__}/keyboard/kb_client.py 2>&1 >> ${log_dir}/keyboard.log &
  75. }
  76. function stop_keyboard()
  77. {
  78. stop kb_client.py
  79. }
  80. # 启动鼠标事件监听及发送服务
  81. function start_mouse()
  82. {
  83. ${__DIR__}/mouse/mouse_client.py 2>&1 >> ${log_dir}/mouse.log &
  84. }
  85. function stop_mouse()
  86. {
  87. stop mouse_client.py
  88. }
  89. function stop() {
  90. service=$1
  91. pid=$(ps aux | grep ${service} | grep -v grep | awk '{print $2}')
  92. if [[ ! -z "${pid}" ]]; then
  93. sudo kill ${pid}
  94. fi
  95. }
  96. case $1 in
  97. install)
  98. install
  99. ;;
  100. uninstall)
  101. uninstall
  102. ;;
  103. config-address)
  104. update_bluetooth_address
  105. ;;
  106. config-name)
  107. set_bluetooth_name $2
  108. ;;
  109. boot)
  110. start_bluetooth
  111. start_main_service
  112. ;;
  113. start-keyboard)
  114. start_keyboard
  115. ;;
  116. start-mouse)
  117. start_mouse
  118. ;;
  119. stop-all)
  120. stop_mouse
  121. stop_keyboard
  122. stop_main_service
  123. stop_bluetooth
  124. ;;
  125. *)
  126. help
  127. ;;
  128. esac