#!/bin/bash

set -e
set -u

usage() {
    echo "Usage: $0 {start|stop|status}" >&2
}

if [ $# -eq 0 ]; then
    echo "Error: No command provided." >&2
    usage
    exit 1
fi

source "/etc/lwr/static-config/lwr-usb-gadget-configurator/common-values"
source "/etc/lwr/static-config/lwr-usb-gadget-configurator/hid-common-values"

start() {
    report_descriptor=""
    report_descriptor+="060fff"     # USAGE_PAGE (Generic Desktop)
    report_descriptor+="0910"       # USAGE (Peripheral Switcher)
    report_descriptor+="a101"       # COLLECTION (Application)
    report_descriptor+="8501"       #   REPORT_ID (1)
    report_descriptor+="1500"       #   LOGICAL_MINIMUM (0)
    report_descriptor+="2501"       #   LOGICAL_MAXIMUM (1)
    report_descriptor+="7501"       #   REPORT_SIZE (1)
    report_descriptor+="9502"       #   REPORT_COUNT (2)
    report_descriptor+="0911"       #   USAGE (Peripheral Switcher Switching State)
    report_descriptor+="0913"       #   USAGE (Peripheral Switcher BYOD Host Sensing State)
    report_descriptor+="8102"       #   INPUT (Data,Var,Abs)
    report_descriptor+="9506"       #   REPORT_COUNT (6)
    report_descriptor+="8101"       #   INPUT (Cnst,Ary,Abs)
    report_descriptor+="9508"       #   REPORT_COUNT (8)
    report_descriptor+="8101"       #   INPUT (Cnst,Ary,Abs)
    report_descriptor+="850b"       #   REPORT_ID (11)
    report_descriptor+="7501"       #   REPORT_SIZE (1)
    report_descriptor+="9501"       #   REPORT_COUNT (1)
    report_descriptor+="0912"       #   USAGE (Peripheral Switcher Toggle)
    report_descriptor+="9106"       #   OUTPUT (Data,Var,Rel)
    report_descriptor+="0914"       #   USAGE (Peripheral Switcher Mode Lock)
    report_descriptor+="9122"       #   OUTPUT (Data,Var,Abs,NPrf)
    report_descriptor+="9506"       #   REPORT_COUNT (6)
    report_descriptor+="9101"       #   OUTPUT (Cnst,Ary,Abs)
    report_descriptor+="9508"       #   REPORT_COUNT (8)
    report_descriptor+="9101"       #   OUTPUT (Cnst,Ary,Abs)
    report_descriptor+="0901"       #   USAGE (Peripheral Health)
    report_descriptor+="a102"       #   COLLECTION (Logical)
    report_descriptor+="8502"       #     REPORT_ID (2)
    report_descriptor+="7501"       #     REPORT_SIZE (1)
    report_descriptor+="9501"       #     REPORT_COUNT (1)
    report_descriptor+="0902"       #     USAGE (Peripheral Health Status)
    report_descriptor+="8102"       #     INPUT (Data,Var,Abs)
    report_descriptor+="9507"       #     REPORT_COUNT (7)
    report_descriptor+="8101"       #     INPUT (Cnst,Ary,Abs)
    report_descriptor+="26ff00"     #     LOGICAL_MAXIMUM (255)
    report_descriptor+="7508"       #     REPORT_SIZE (8)
    report_descriptor+="9501"       #     REPORT_COUNT (1)
    report_descriptor+="0903"       #     USAGE (Peripheral Health Error Code)
    report_descriptor+="8102"       #     INPUT (Data,Var,Abs)
    report_descriptor+="c0"         #   END_COLLECTION
    report_descriptor+="c0"         # END_COLLECTION
    report_descriptor+="0501"       # USAGE_PAGE (Generic Desktop)
    report_descriptor+="0913"       # USAGE (Call State Management Control)
    report_descriptor+="a101"       # COLLECTION (Application)
    report_descriptor+="8503"       #   REPORT_ID (3)
    report_descriptor+="2501"       #   LOGICAL_MAXIMUM (1)
    report_descriptor+="7501"       #   REPORT_SIZE (1)
    report_descriptor+="9501"       #   REPORT_COUNT (1)
    report_descriptor+="0be1000100" #   USAGE (Generic Desktop:Call Mute Toggle)
    report_descriptor+="8106"       #   INPUT (Data,Var,Rel)
    report_descriptor+="9507"       #   REPORT_COUNT (7)
    report_descriptor+="8101"       #   INPUT (Cnst,Ary,Abs)
    report_descriptor+="9508"       #   REPORT_COUNT (8)
    report_descriptor+="8101"       #   INPUT (Cnst,Ary,Abs)
    report_descriptor+="850c"       #   REPORT_ID (12)
    report_descriptor+="7501"       #   REPORT_SIZE (1)
    report_descriptor+="9502"       #   REPORT_COUNT (2)
    report_descriptor+="0be0000100" #   USAGE (Generic Desktop:Call Active LED)
    report_descriptor+="0be2000100" #   USAGE (Generic Desktop:Call Mute LED)
    report_descriptor+="9122"       #   OUTPUT (Data,Var,Abs,NPrf)
    report_descriptor+="9506"       #   REPORT_COUNT (6)
    report_descriptor+="9101"       #   OUTPUT (Cnst,Ary,Abs)
    report_descriptor+="9508"       #   REPORT_COUNT (8)
    report_descriptor+="9101"       #   OUTPUT (Cnst,Ary,Abs)
    report_descriptor+="c0"         # END_COLLECTION

    echo -en ${report_descriptor} | xxd -r -p > "${HID_FUNCTION_DIR}/report_desc"

    # Including leading REPORT_ID byte
    echo 3 > "${HID_FUNCTION_DIR}/report_length"
}

stop() {
    return
}

status() {
    return # Do not report status
}

case "$1" in
    start|stop|status)
        "$@"
        ;;
    *)
        echo "Error: Unknown command '$1'." >&2
        usage
        exit 1
        ;;
esac
