Skip to content

Commit

Permalink
treehouses bluetooth refactor (fixes #1582) (#1583)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <dogi@users.noreply.github.com>
  • Loading branch information
sarahrams and dogi authored Jul 3, 2020
1 parent 0890158 commit af1613c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 84 deletions.
177 changes: 94 additions & 83 deletions modules/bluetooth.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,111 @@ function bluetooth {
checkargn $# 2
status=$1

if [ -z "$status" ]; then
if [[ "$(service rpibluetooth status | grep "Active:")" =~ "running" ]]; then
echo "on"
else
echo "off"
fi
case $status in
"")
if [[ "$(service rpibluetooth status | grep "Active:")" =~ "running" ]]; then
echo "on"
else
echo "off"
fi
;;

elif [ "$status" = "status" ]; then
if [[ "$(service bluetooth status | grep "Active:")" =~ "running" ]]; then
"status")
if [[ "$(service bluetooth status | grep "Active:")" =~ "running" ]]; then
echo "bluetooth service status: on"
else
echo "bluetooth service status: off"
fi
if [[ "$(service rpibluetooth status | grep "Active:")" =~ "running" ]]; then
echo "rpibluetooth service status: on"
else
echo "rpibluetooth service status: off"
fi
else
echo "bluetooth service status: off"
fi
if [[ "$(service rpibluetooth status | grep "Active:")" =~ "running" ]]; then
echo "rpibluetooth service status: on"
else
echo "rpibluetooth service status: off"
fi
;;

elif [ "$status" = "on" ]; then
checkargn $# 1
cp "$TEMPLATES/bluetooth/hotspot" /etc/systemd/system/dbus-org.bluez.service
enable_service rpibluetooth
restart_service bluetooth
restart_service rpibluetooth
sleep 5 # wait 5 seconds for bluetooth to be completely up
echo "Success: the bluetooth service has been started."
"on")
checkargn $# 1
cp "$TEMPLATES/bluetooth/hotspot" /etc/systemd/system/dbus-org.bluez.service
enable_service rpibluetooth
restart_service bluetooth
restart_service rpibluetooth
sleep 5 # wait 5 seconds for bluetooth to be completely up
echo "Success: the bluetooth service has been started."
;;

elif [ "$status" = "off" ] || [ "$status" = "pause" ]; then
checkargn $# 1
cp "$TEMPLATES/bluetooth/default" /etc/systemd/system/dbus-org.bluez.service
disable_service rpibluetooth
stop_service rpibluetooth
restart_service bluetooth
if [ "$status" = "off" ]; then
rm -rf /etc/bluetooth-id
fi
sleep 3 # Wait few seconds for bluetooth to start
restart_service bluealsa # restart the bluetooth audio service
echo "Success: the bluetooth service has been switched to default, and the service has been stopped."
"off")
checkargn $# 1
cp "$TEMPLATES/bluetooth/default" /etc/systemd/system/dbus-org.bluez.service
disable_service rpibluetooth
stop_service rpibluetooth
restart_service bluetooth
if [ "$status" = "off" ]; then
rm -rf /etc/bluetooth-id
fi
sleep 3 # Wait few seconds for bluetooth to start
restart_service bluealsa # restart the bluetooth audio service
echo "Success: the bluetooth service has been switched to default, and the service has been stopped."
;;

elif [ "$status" = "mac" ]; then
checkargn $# 1
macfile=/sys/kernel/debug/bluetooth/hci0/identity
macadd=$(cat ${macfile})
echo "${macadd:0:17}"
"mac")
checkargn $# 1
macfile=/sys/kernel/debug/bluetooth/hci0/identity
macadd=$(cat ${macfile})
echo "${macadd:0:17}"
;;

elif [ "$status" = "id" ]; then
btidfile=/etc/bluetooth-id
if [ ! -f "${btidfile}" ]; then
echo "No ID. Bluetooth service is not on."
exit 0
fi
"id")
btidfile=/etc/bluetooth-id
if [ ! -f "${btidfile}" ]; then
echo "No ID. Bluetooth service is not on."
exit 0
fi

bid=$(cat ${btidfile})
nname=$(uname -n)
bid=$(cat ${btidfile})
nname=$(uname -n)

case "$2" in
"")
echo "${nname}-${bid}"
;;
"number")
echo "${bid}"
;;
*)
echo "Argument not valid; leave blank or use \"number\""
exit 1
;;
esac

elif [ "$status" = "button" ]; then
checkargn $# 1
button bluetooth
case "$2" in
"")
echo "${nname}-${bid}"
;;
"number")
echo "${bid}"
;;
*)
echo "Argument not valid; leave blank or use \"number\""
exit 1
;;
esac
;;

"button")
checkargn $# 1
button bluetooth
;;

elif [ "$status" = "log" ]; then
if [ "$2" = "" ]; then
checkargn $# 1
journalctl -u rpibluetooth -u bluetooth --no-pager
elif [ "$2" = "follow" ]; then
echo "press (ctrl + c) to exit"
journalctl -u rpibluetooth -u bluetooth -f
else
echo "Argument not valid; leave blank or use \"follow\""
exit 1
fi
"log")
if [ "$2" = "" ]; then
checkargn $# 1
journalctl -u rpibluetooth -u bluetooth --no-pager
elif [ "$2" = "follow" ]; then
echo "press (ctrl + c) to exit"
journalctl -u rpibluetooth -u bluetooth -f
else
echo "Argument not valid; leave blank or use \"follow\""
exit 1
fi
;;

elif [ "$status" = "restart" ]; then
bluetooth off &>"$LOGFILE"
bluetooth on &>"$LOGFILE"
echo "Success: the bluetooth service has been restarted."
"restart")
bluetooth off &>"$LOGFILE"
bluetooth on &>"$LOGFILE"
echo "Success: the bluetooth service has been restarted."
;;

else
echo "Error: only 'on', 'off', 'pause', 'restart', 'mac', 'id', 'button', 'log', and 'status' options are supported";
fi
*)
echo "Error: only 'on', 'off', 'pause', 'restart', 'mac', 'id', 'button', 'log', and 'status' options are supported";
;;
esac
}

function bluetooth_help {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@treehouses/cli",
"version": "1.22.48",
"version": "1.23.0",
"remote": "2346",
"description": "Thin command-line interface for Raspberry Pi low level configuration.",
"main": "cli.sh",
Expand Down

0 comments on commit af1613c

Please sign in to comment.