diff options
Diffstat (limited to 'bin/blum')
| -rwxr-xr-x | bin/blum | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/bin/blum b/bin/blum new file mode 100755 index 0000000..19c2aef --- /dev/null +++ b/bin/blum @@ -0,0 +1,100 @@ +#!/bin/bash +set -eu + +description="bluetooth manager" + +export FUZL_PROMPT=blum +export FUZL_ICON= +. libfuzl.sh + +main() { + local connected + + if ! bt_enabled; then + fuzl_action_add "enable" "" "Enable Bluetooth" + else + connected=$(bt_devices Connected | bt_devices_getname) + if [ -n "$connected" ]; then + fuzl_action_add "disconnect" "" "Connected to $connected" + fi + fuzl_action_add "devices" "" "Available devices" + fuzl_action_add "scan" "" "Scan" + fuzl_action_add "repl" "" "Run REPL" + fuzl_action_add "disable" "" "Disable Bluetooth" + fuzl_action_add "tether_amagiri" "" "Tether amagiri" + fi + + fuzl_action_menu || exit 1 +} + +bt_enabled() { + pgrep -cx bluetoothd >/dev/null 2>&1 +} + +bt_devices() { + echo "devices" "${1:-}" | bluetoothctl | + awk '/^Device /{match($0, /[0-9A-F:]{17}/); mac=substr($0,RSTART,RLENGTH); name=substr($0,RSTART+RLENGTH+1); print mac "\t" name}' +} + +bt_devices_getmac() { + awk -F '\t' '{print $1}' +} + +bt_devices_getname() { + awk -F '\t' '{print $2}' +} + +# +# actions +# + +blum_enable() { + fuzl_try -- doas -n rc-service bluetooth start + fuzl_notification "enabled" "-" + main +} + +blum_disable() { + fuzl_try -- doas -n rc-service bluetooth stop + fuzl_notification "disabled" "-" +} + +blum_disconnect() { + echo "disconnect" | fuzl_try -- bluetoothctl + fuzl_notification "disconnected" "-" +} + +blum_devices() { + device=$(bt_devices | + FUZL_PROMPT='devices' fuzl_menu $FUZL_MENU_FILTER) + + [ -n "$device" ] || main + device_mac=$(printf %s "$device" | bt_devices_getmac) + + echo "connect" "$device_mac" | fuzl_try -- bluetoothctl + fuzl_notification "connected" "$device" + +} + +blum_scan() { + fuzl_loading "scanning" + echo "scan" "on" | bluetoothctl || return + sleep 2 + fuzl_loading_cancel # TODO: this kills everything + main +} + +blum_repl() { + riverctl spawn 'footclient -- bluetoothctl' +} + +blum_tether_amagiri() { + local amagiri_mac='3C:38:F4:5D:3D:49' + local amagiri_net='e4e75ff7-b907-43b7-98b7-e0bf94a56d08' + echo "connect" "$amagiri_mac" | fuzl_try -- bluetoothctl + fuzl_notification "connected" "amagiri" + fuzl_try -- nmcli connection up "$amagiri_net" + fuzl_notification "network up" "amagiri" +} + +main |