Split-out and refactor bluetooth status parser
[khatus.git] / bin / khatus_parse_bluetoothctl_show
diff --git a/bin/khatus_parse_bluetoothctl_show b/bin/khatus_parse_bluetoothctl_show
new file mode 100755 (executable)
index 0000000..08a8708
--- /dev/null
@@ -0,0 +1,36 @@
+#! /usr/bin/awk -f
+
+/^Controller / {
+    controller = $2
+    controllers[++ctrl_count] = controller
+}
+
+/^\t[A-Z][A-Za-z]+:/ {
+    key = $1
+    sub(":$", "", key)
+    sub("^" $1, "")
+    val = $0
+    data[controller, key] = val
+}
+
+END {
+    # Using the 1st seen controller. Should we select specific instead?
+    power_status = data[controllers[1], "Powered"]
+    if (ctrl_count > 0) {
+        if (power_status == "no") {
+            show = "off"
+        } else if (power_status == "yes") {
+            show = "on"
+        } else {
+            print_error("Unexpected bluetooth power status: " power_status)
+            show = "ERROR"
+        }
+    } else {
+        show = "off"  # TODO: Perhaps use differentiated marker?
+    }
+    print(show)
+}
+
+function print_error(msg) {
+    print(msg) > "/dev/stderr"
+}
This page took 0.028525 seconds and 4 git commands to generate.