#!/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 | 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