179f697e0SAnthony Wilson#!/bin/sh -e
279f697e0SAnthony Wilson
379f697e0SAnthony Wilsonset -euo pipefail
479f697e0SAnthony Wilson
53ae0a354SAnthony WilsonOPTS="bmcstate,bootprogress,chassisoff,chassison,chassisstate,hoststate,\
63ae0a354SAnthony Wilsonpower,poweroff,poweron,state,status"
70f35983dSAnthony Wilson
8f3f16fa9SAnthony WilsonUSAGE="Usage: obmcutil [-h]
90f35983dSAnthony Wilson                {$OPTS}"
1079f697e0SAnthony Wilson
1179f697e0SAnthony WilsonINTERFACE_ROOT=xyz.openbmc_project
1279f697e0SAnthony WilsonSTATE_INTERFACE=$INTERFACE_ROOT.State
1379f697e0SAnthony Wilson
1479f697e0SAnthony WilsonOBJECT_ROOT=/xyz/openbmc_project
1579f697e0SAnthony WilsonSTATE_OBJECT=$OBJECT_ROOT/state
1679f697e0SAnthony Wilson
17f3f16fa9SAnthony Wilsonprint_help ()
18f3f16fa9SAnthony Wilson{
19f3f16fa9SAnthony Wilson    echo "$USAGE"
20f3f16fa9SAnthony Wilson    echo ""
21f3f16fa9SAnthony Wilson    echo "positional arguments:"
220f35983dSAnthony Wilson    echo "  {$OPTS}"
23f3f16fa9SAnthony Wilson    echo ""
24f3f16fa9SAnthony Wilson    echo "optional arguments:"
25f3f16fa9SAnthony Wilson    echo "  -h, --help          show this help message and exit"
26f3f16fa9SAnthony Wilson    exit 0
27f3f16fa9SAnthony Wilson}
28f3f16fa9SAnthony Wilson
293ae0a354SAnthony Wilsonset_property ()
303ae0a354SAnthony Wilson{
313ae0a354SAnthony Wilson    busctl set-property "$@"
323ae0a354SAnthony Wilson}
333ae0a354SAnthony Wilson
3479f697e0SAnthony Wilsonget_property ()
3579f697e0SAnthony Wilson{
3679f697e0SAnthony Wilson    busctl get-property "$@"
3779f697e0SAnthony Wilson}
3879f697e0SAnthony Wilson
3979f697e0SAnthony Wilsonstate_query ()
4079f697e0SAnthony Wilson{
4179f697e0SAnthony Wilson    local state=$(get_property "$@" | cut -d '"' -f2)
4279f697e0SAnthony Wilson    printf "%-20s: %s\n" $4 $state
4379f697e0SAnthony Wilson}
4479f697e0SAnthony Wilson
45*ea87db40SAnthony Wilsonprint_usage_err ()
46*ea87db40SAnthony Wilson{
47*ea87db40SAnthony Wilson    echo "ERROR: $1" >&2
48*ea87db40SAnthony Wilson    echo "$USAGE"
49*ea87db40SAnthony Wilson    exit 1
50*ea87db40SAnthony Wilson}
51*ea87db40SAnthony Wilson
5279f697e0SAnthony Wilsonhandle_cmd ()
5379f697e0SAnthony Wilson{
5479f697e0SAnthony Wilson    case "$1" in
553ae0a354SAnthony Wilson        chassisoff)
563ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/chassis0
573ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
583ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
593ae0a354SAnthony Wilson            PROPERTY=RequestedPowerTransition
603ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.Off
613ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
623ae0a354SAnthony Wilson            ;;
633ae0a354SAnthony Wilson        chassison)
643ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/chassis0
653ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
663ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
673ae0a354SAnthony Wilson            PROPERTY=RequestedPowerTransition
683ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.On
693ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
703ae0a354SAnthony Wilson            ;;
713ae0a354SAnthony Wilson        poweroff)
723ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/host0
733ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
743ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
753ae0a354SAnthony Wilson            PROPERTY=RequestedHostTransition
763ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.Off
773ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
783ae0a354SAnthony Wilson            ;;
793ae0a354SAnthony Wilson        poweron)
803ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/host0
813ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
823ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
833ae0a354SAnthony Wilson            PROPERTY=RequestedHostTransition
843ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.On
853ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
863ae0a354SAnthony Wilson            ;;
8779f697e0SAnthony Wilson        bmcstate)
8879f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/bmc0
8979f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
9079f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.BMC
9179f697e0SAnthony Wilson            PROPERTY=CurrentBMCState
9279f697e0SAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
9379f697e0SAnthony Wilson            ;;
9479f697e0SAnthony Wilson        chassisstate)
9579f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/chassis0
9679f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
9779f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
9879f697e0SAnthony Wilson            PROPERTY=CurrentPowerState
9979f697e0SAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
10079f697e0SAnthony Wilson            ;;
10179f697e0SAnthony Wilson        hoststate)
10279f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/host0
10379f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
10479f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
10579f697e0SAnthony Wilson            PROPERTY=CurrentHostState
10679f697e0SAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
10779f697e0SAnthony Wilson            ;;
10879f697e0SAnthony Wilson        state|status)
10979f697e0SAnthony Wilson            for query in bmcstate chassisstate hoststate
11079f697e0SAnthony Wilson            do
11179f697e0SAnthony Wilson                handle_cmd $query
11279f697e0SAnthony Wilson            done
11379f697e0SAnthony Wilson            ;;
11450c5f88dSAnthony Wilson        bootprogress)
11550c5f88dSAnthony Wilson            OBJECT=$STATE_OBJECT/host0
11650c5f88dSAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
11750c5f88dSAnthony Wilson            INTERFACE=$STATE_INTERFACE.Boot.Progress
11850c5f88dSAnthony Wilson            PROPERTY=BootProgress
11950c5f88dSAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
12050c5f88dSAnthony Wilson            ;;
1210f35983dSAnthony Wilson        power)
1220f35983dSAnthony Wilson            OBJECT=/org/openbmc/control/power0
1230f35983dSAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
1240f35983dSAnthony Wilson            INTERFACE=org.openbmc.control.Power
1250f35983dSAnthony Wilson            for property in pgood state pgood_timeout
1260f35983dSAnthony Wilson            do
1270f35983dSAnthony Wilson                # get_property can potentially return several
1280f35983dSAnthony Wilson                # different formats of values, so we do the parsing outside
1290f35983dSAnthony Wilson                # of get_property depending on the query. These queries
1300f35983dSAnthony Wilson                # return 'i VALUE' formatted strings.
1310f35983dSAnthony Wilson                STATE=$(get_property $SERVICE $OBJECT $INTERFACE $property \
1320f35983dSAnthony Wilson                    | sed 's/i[ ^I]*//')
1330f35983dSAnthony Wilson                printf "%s = %s\n" $property $STATE
1340f35983dSAnthony Wilson            done
1350f35983dSAnthony Wilson            ;;
13679f697e0SAnthony Wilson        *)
137*ea87db40SAnthony Wilson            print_usage_err "Invalid command '$1'"
13879f697e0SAnthony Wilson            ;;
13979f697e0SAnthony Wilson    esac
14079f697e0SAnthony Wilson}
14179f697e0SAnthony Wilson
142*ea87db40SAnthony Wilsonfor arg in "$@"; do
143*ea87db40SAnthony Wilson    case $arg in
144*ea87db40SAnthony Wilson        -h|--help)
145*ea87db40SAnthony Wilson            print_help
146*ea87db40SAnthony Wilson            ;;
147*ea87db40SAnthony Wilson        -*)
148*ea87db40SAnthony Wilson            print_usage_err "Unknown option: $arg"
149*ea87db40SAnthony Wilson            ;;
150*ea87db40SAnthony Wilson        *)
151*ea87db40SAnthony Wilson            handle_cmd $arg
152*ea87db40SAnthony Wilson            break
153*ea87db40SAnthony Wilson            ;;
154*ea87db40SAnthony Wilson    esac
155*ea87db40SAnthony Wilsondone
156