123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- #!/bin/bash
- __DIR__=$(cd $(dirname $0); pwd)
- log_dir=/var/log/kbm
- mkdir -p ${log_dir}
- # 安装必要的依赖
- function install()
- {
- sudo apt-get install bluez bluez-tools -y
- sudo apt-get install bluez-firmware python-bluez python-dev python-pip -y
- sudo pip install evdev
- sudo apt install git python python3 python-dev python3-dev python3-dbus python3-pyudev python3-evdev -y
- sudo apt-get install python-dbus -y
- sudo apt-get install tmux -y
- sudo cp dbus/org.thanhle.btkbservice.conf /etc/dbus-1/system.d
- sudo cp /lib/systemd/system/bluetooth.service ./bluetooth.service.bk
- sudo cp bluetooth.service /lib/systemd/system/bluetooth.service
- sudo systemctl daemon-reload
- sudo /etc/init.d/bluetooth start
- sudo apt-get install python3-gi -y
- }
- # 卸载
- function uninstall()
- {
- if [ -f "bluetooth.service.bk" ] ; then
- sudo cp bluetooth.service.bk /lib/systemd/system/bluetooth.service
- sudo systemctl daemon-reload
- sudo /etc/init.d/bluetooth start
- fi
- }
- # 帮助信息
- function help()
- {
- echo "./controll [ install | uninstall | config-address | config-name <name> | boot | start-keyboard | start-mouse ]"
- }
- # 自动更新服务器的蓝牙地址
- function update_bluetooth_address()
- {
- address=$(hciconfig hci0 | awk '/BD Address: /{print $3}')
- if [[ ! -z "${address}" ]] ; then
- sed -i -e "s/BLUETOOTH_ADDRESS_PLACEHOLDER/${address}/" server/btk_server.py
- fi
- }
- # 设置服务器的蓝牙地址
- function set_bluetooth_name()
- {
- bluetooth_name=$1
- sed -i -e "s/DEVICE_NAME_PLACEHOLDER/${bluetooth_name}/" ${__DIR__}/server/btk_server.py
- }
- # 启动蓝牙系统服务
- function start_bluetooth()
- {
- # 关闭蓝牙设备
- sudo hciconfig hci0 down
- # 启动蓝牙系统服务
- sudo /etc/init.d/bluetooth start
- }
- function stop_bluetooth()
- {
- sudo hciconfig hci0 down
- sudo /etc/init.d/bluetooth stop
- }
- # 启动蓝牙连接及事件发送服务
- function start_main_service()
- {
- ${__DIR__}/server/btk_server.py 2>&1 >> ${log_dir}/main.log &
- }
- function stop_main_service()
- {
- stop btk_server.py
- }
- # 启动键盘事件监听及发送服务
- function start_keyboard()
- {
- ${__DIR__}/keyboard/kb_client.py 2>&1 >> ${log_dir}/keyboard.log &
- }
- function stop_keyboard()
- {
- stop kb_client.py
- }
- # 启动鼠标事件监听及发送服务
- function start_mouse()
- {
- ${__DIR__}/mouse/mouse_client.py 2>&1 >> ${log_dir}/mouse.log &
- }
- function stop_mouse()
- {
- stop mouse_client.py
- }
- function stop() {
- service=$1
- pid=$(ps aux | grep ${service} | grep -v grep | awk '{print $2}')
- if [[ ! -z "${pid}" ]]; then
- sudo kill ${pid}
- fi
- }
- case $1 in
- install)
- install
- ;;
- uninstall)
- uninstall
- ;;
- config-address)
- update_bluetooth_address
- ;;
- config-name)
- set_bluetooth_name $2
- ;;
- boot)
- start_bluetooth
- start_main_service
- ;;
- start-keyboard)
- start_keyboard
- ;;
- start-mouse)
- start_mouse
- ;;
- stop-all)
- stop_mouse
- stop_keyboard
- stop_main_service
- stop_bluetooth
- ;;
- *)
- help
- ;;
- esac
|