179f697e0SAnthony Wilson#!/bin/sh -e
279f697e0SAnthony Wilson
379f697e0SAnthony Wilsonset -euo pipefail
479f697e0SAnthony Wilson
5*3ae0a354SAnthony WilsonOPTS="bmcstate,bootprogress,chassisoff,chassison,chassisstate,hoststate,\
6*3ae0a354SAnthony 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
29*3ae0a354SAnthony Wilsonset_property ()
30*3ae0a354SAnthony Wilson{
31*3ae0a354SAnthony Wilson    busctl set-property "$@"
32*3ae0a354SAnthony Wilson}
33*3ae0a354SAnthony 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
4579f697e0SAnthony Wilsonhandle_cmd ()
4679f697e0SAnthony Wilson{
4779f697e0SAnthony Wilson    case "$1" in
48*3ae0a354SAnthony Wilson        chassisoff)
49*3ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/chassis0
50*3ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
51*3ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
52*3ae0a354SAnthony Wilson            PROPERTY=RequestedPowerTransition
53*3ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.Off
54*3ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
55*3ae0a354SAnthony Wilson            ;;
56*3ae0a354SAnthony Wilson        chassison)
57*3ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/chassis0
58*3ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
59*3ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
60*3ae0a354SAnthony Wilson            PROPERTY=RequestedPowerTransition
61*3ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.On
62*3ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
63*3ae0a354SAnthony Wilson            ;;
64*3ae0a354SAnthony Wilson        poweroff)
65*3ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/host0
66*3ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
67*3ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
68*3ae0a354SAnthony Wilson            PROPERTY=RequestedHostTransition
69*3ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.Off
70*3ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
71*3ae0a354SAnthony Wilson            ;;
72*3ae0a354SAnthony Wilson        poweron)
73*3ae0a354SAnthony Wilson            OBJECT=$STATE_OBJECT/host0
74*3ae0a354SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
75*3ae0a354SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
76*3ae0a354SAnthony Wilson            PROPERTY=RequestedHostTransition
77*3ae0a354SAnthony Wilson            VALUE=$INTERFACE.Transition.On
78*3ae0a354SAnthony Wilson            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
79*3ae0a354SAnthony Wilson            ;;
8079f697e0SAnthony Wilson        bmcstate)
8179f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/bmc0
8279f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
8379f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.BMC
8479f697e0SAnthony Wilson            PROPERTY=CurrentBMCState
8579f697e0SAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
8679f697e0SAnthony Wilson            ;;
8779f697e0SAnthony Wilson        chassisstate)
8879f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/chassis0
8979f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
9079f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Chassis
9179f697e0SAnthony Wilson            PROPERTY=CurrentPowerState
9279f697e0SAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
9379f697e0SAnthony Wilson            ;;
9479f697e0SAnthony Wilson        hoststate)
9579f697e0SAnthony Wilson            OBJECT=$STATE_OBJECT/host0
9679f697e0SAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
9779f697e0SAnthony Wilson            INTERFACE=$STATE_INTERFACE.Host
9879f697e0SAnthony Wilson            PROPERTY=CurrentHostState
9979f697e0SAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
10079f697e0SAnthony Wilson            ;;
10179f697e0SAnthony Wilson        state|status)
10279f697e0SAnthony Wilson            for query in bmcstate chassisstate hoststate
10379f697e0SAnthony Wilson            do
10479f697e0SAnthony Wilson                handle_cmd $query
10579f697e0SAnthony Wilson            done
10679f697e0SAnthony Wilson            ;;
10750c5f88dSAnthony Wilson        bootprogress)
10850c5f88dSAnthony Wilson            OBJECT=$STATE_OBJECT/host0
10950c5f88dSAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
11050c5f88dSAnthony Wilson            INTERFACE=$STATE_INTERFACE.Boot.Progress
11150c5f88dSAnthony Wilson            PROPERTY=BootProgress
11250c5f88dSAnthony Wilson            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
11350c5f88dSAnthony Wilson            ;;
1140f35983dSAnthony Wilson        power)
1150f35983dSAnthony Wilson            OBJECT=/org/openbmc/control/power0
1160f35983dSAnthony Wilson            SERVICE=$(mapper get-service $OBJECT)
1170f35983dSAnthony Wilson            INTERFACE=org.openbmc.control.Power
1180f35983dSAnthony Wilson            for property in pgood state pgood_timeout
1190f35983dSAnthony Wilson            do
1200f35983dSAnthony Wilson                # get_property can potentially return several
1210f35983dSAnthony Wilson                # different formats of values, so we do the parsing outside
1220f35983dSAnthony Wilson                # of get_property depending on the query. These queries
1230f35983dSAnthony Wilson                # return 'i VALUE' formatted strings.
1240f35983dSAnthony Wilson                STATE=$(get_property $SERVICE $OBJECT $INTERFACE $property \
1250f35983dSAnthony Wilson                    | sed 's/i[ ^I]*//')
1260f35983dSAnthony Wilson                printf "%s = %s\n" $property $STATE
1270f35983dSAnthony Wilson            done
1280f35983dSAnthony Wilson            ;;
129f3f16fa9SAnthony Wilson        -h|--help)
130f3f16fa9SAnthony Wilson            print_help
131f3f16fa9SAnthony Wilson            ;;
13279f697e0SAnthony Wilson        *)
13379f697e0SAnthony Wilson            echo "ERROR: Invalid Choice: '$1'"
13479f697e0SAnthony Wilson            echo "$USAGE"
13579f697e0SAnthony Wilson            ;;
13679f697e0SAnthony Wilson    esac
13779f697e0SAnthony Wilson}
13879f697e0SAnthony Wilson
13979f697e0SAnthony Wilsonhandle_cmd $1
140