#!/bin/sh
# CPU adapter: mu300-cpu for the panel's CPU page (mu300dash cpu_get, cpu_set). Every reply is one JSON object.
#   cpu get             {"ok":1,"profile":...,"profiles":[...],"throttling":0|1,"policies":[...]}
#   cpu set NAME        {"ok":1,...} (the new status), or {"ok":0,"error":...}; NAME checked here again
#   cpu core N on|off   CPU N online or offline, saved (never CPU0); the reply is the new status
#   cpu lowlatency on|off  CPU0 without its idle states (lower Wi-Fi latency, more power), saved; the new status
#   cpu voltage-get     the CPU voltage offset (EXPERIMENTAL): mu300-cpu voltage status --json
#   cpu voltage-set set A,B,C WORD | off | stress MINUTES
#                       the request for the next boot, back to the stock voltages, or the CPU stress test in the
#                       background; every value checked here again; the reply is the new voltage status
set -u
export PATH="$PATH:/opt/mu300/bin:/usr/sbin:/sbin"
LIB=${0%/*}/../../share/unisoc-modem/lib.sh
[ -f "$LIB" ] || LIB=/usr/share/unisoc-modem/lib.sh
. "$LIB"

CPU_CMD=${MU300_CPU_BIN:-/opt/mu300/bin/mu300-cpu}

error() { printf '{"ok":0,"error":%s}\n' "$(json_str "$1")"; exit 0; }

do_get() {
    out=$("$CPU_CMD" status --json 2>/dev/null) || error 'The CPU profile could not be read'
    case $out in '{'*) printf '%s\n' "$out" ;; *) error 'The CPU profile could not be read' ;; esac
}

do_set() {
    case ${1:-} in saving|balanced|performance) ;; *) error 'Invalid CPU profile' ;; esac
    if out=$("$CPU_CMD" profile "$1" --json 2>/dev/null); then
        case $out in '{'*) printf '%s\n' "$out"; return ;; esac
    fi
    error 'The CPU profile could not be set'
}

do_core() {
    case ${1:-} in [1-9]|[1-5][0-9]|6[0-3]) ;; *) error 'Invalid CPU core' ;; esac
    case ${2:-} in on|off) ;; *) error 'Invalid CPU core' ;; esac
    if ! out=$("$CPU_CMD" core "$1" "$2" 2>&1 >/dev/null); then
        printf '{"ok":0,"error":"The CPU core could not be switched","detail":%s}\n' "$(json_str "$(why "$out")")"
        exit 0
    fi
    do_get
}

do_lowlat() {
    case ${1:-} in on|off) ;; *) error 'Invalid low latency setting' ;; esac
    if ! out=$("$CPU_CMD" lowlatency "$1" 2>&1 >/dev/null); then
        printf '{"ok":0,"error":"The low latency setting could not be applied","detail":%s}\n' "$(json_str "$(why "$out")")"
        exit 0
    fi
    do_get
}

do_vget() {
    out=$("$CPU_CMD" voltage status --json 2>/dev/null) || error 'The CPU voltage offset could not be read'
    case $out in '{'*) printf '%s\n' "$out" ;; *) error 'The CPU voltage offset could not be read' ;; esac
}

# the reason mu300-cpu gave (its last "mu300-cpu: ..." line), for the detail of a refusal
why() { printf '%s\n' "$1" | sed -n 's/^mu300-cpu: //p' | tail -n1 | cut -c1-200; }

do_vset() {
    case ${1:-} in
        set)
            printf '%s\n' "${2:-}" | grep -Eqx -- '-?[0-9]{1,6},-?[0-9]{1,6},-?[0-9]{1,6}' || error 'Invalid CPU voltage offset'
            [ "${3:-}" = EXPERIMENTAL ] || error 'Type the confirmation word to apply a voltage offset'
            if ! out=$("$CPU_CMD" voltage set "$2" --confirm EXPERIMENTAL 2>&1 >/dev/null); then
                printf '{"ok":0,"error":"The CPU voltage offset was refused","detail":%s}\n' "$(json_str "$(why "$out")")"
                exit 0
            fi ;;
        off)
            [ $# = 1 ] || error 'Invalid CPU voltage request'
            "$CPU_CMD" voltage off >/dev/null 2>&1 || error 'The CPU voltage offset could not be turned off' ;;
        stress)
            case ${2:-} in [1-9]|[1-9][0-9]|1[01][0-9]|120) ;; *) error 'Invalid stress test length' ;; esac
            if ! out=$("$CPU_CMD" voltage stress "$2" --background 2>&1 >/dev/null); then
                printf '{"ok":0,"error":"The stress test could not be started","detail":%s}\n' "$(json_str "$(why "$out")")"
                exit 0
            fi ;;
        *) error 'Unknown CPU voltage operation' ;;
    esac
    do_vget
}

case ${1:-} in
    get) do_get ;;
    set) do_set "${2:-}" ;;
    core) do_core "${2:-}" "${3:-}" ;;
    lowlatency) do_lowlat "${2:-}" ;;
    voltage-get) do_vget ;;
    voltage-set) shift; do_vset "$@" ;;
    *) error 'Unknown CPU operation' ;;
esac
