1#!/bin/sh -e
2
3set -euo pipefail
4
5OPTS="bmcstate,bootprogress,chassisoff,chassison,chassisstate,hoststate,\
6power,poweroff,poweron,state,status"
7
8USAGE="Usage: obmcutil [-h]
9                {$OPTS}"
10
11INTERFACE_ROOT=xyz.openbmc_project
12STATE_INTERFACE=$INTERFACE_ROOT.State
13
14OBJECT_ROOT=/xyz/openbmc_project
15STATE_OBJECT=$OBJECT_ROOT/state
16
17print_help ()
18{
19    echo "$USAGE"
20    echo ""
21    echo "positional arguments:"
22    echo "  {$OPTS}"
23    echo ""
24    echo "optional arguments:"
25    echo "  -h, --help          show this help message and exit"
26    exit 0
27}
28
29set_property ()
30{
31    busctl set-property "$@"
32}
33
34get_property ()
35{
36    busctl get-property "$@"
37}
38
39state_query ()
40{
41    local state=$(get_property "$@" | cut -d '"' -f2)
42    printf "%-20s: %s\n" $4 $state
43}
44
45print_usage_err ()
46{
47    echo "ERROR: $1" >&2
48    echo "$USAGE"
49    exit 1
50}
51
52handle_cmd ()
53{
54    case "$1" in
55        chassisoff)
56            OBJECT=$STATE_OBJECT/chassis0
57            SERVICE=$(mapper get-service $OBJECT)
58            INTERFACE=$STATE_INTERFACE.Chassis
59            PROPERTY=RequestedPowerTransition
60            VALUE=$INTERFACE.Transition.Off
61            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
62            ;;
63        chassison)
64            OBJECT=$STATE_OBJECT/chassis0
65            SERVICE=$(mapper get-service $OBJECT)
66            INTERFACE=$STATE_INTERFACE.Chassis
67            PROPERTY=RequestedPowerTransition
68            VALUE=$INTERFACE.Transition.On
69            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
70            ;;
71        poweroff)
72            OBJECT=$STATE_OBJECT/host0
73            SERVICE=$(mapper get-service $OBJECT)
74            INTERFACE=$STATE_INTERFACE.Host
75            PROPERTY=RequestedHostTransition
76            VALUE=$INTERFACE.Transition.Off
77            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
78            ;;
79        poweron)
80            OBJECT=$STATE_OBJECT/host0
81            SERVICE=$(mapper get-service $OBJECT)
82            INTERFACE=$STATE_INTERFACE.Host
83            PROPERTY=RequestedHostTransition
84            VALUE=$INTERFACE.Transition.On
85            set_property $SERVICE $OBJECT $INTERFACE $PROPERTY "s" $VALUE
86            ;;
87        bmcstate)
88            OBJECT=$STATE_OBJECT/bmc0
89            SERVICE=$(mapper get-service $OBJECT)
90            INTERFACE=$STATE_INTERFACE.BMC
91            PROPERTY=CurrentBMCState
92            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
93            ;;
94        chassisstate)
95            OBJECT=$STATE_OBJECT/chassis0
96            SERVICE=$(mapper get-service $OBJECT)
97            INTERFACE=$STATE_INTERFACE.Chassis
98            PROPERTY=CurrentPowerState
99            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
100            ;;
101        hoststate)
102            OBJECT=$STATE_OBJECT/host0
103            SERVICE=$(mapper get-service $OBJECT)
104            INTERFACE=$STATE_INTERFACE.Host
105            PROPERTY=CurrentHostState
106            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
107            ;;
108        state|status)
109            for query in bmcstate chassisstate hoststate
110            do
111                handle_cmd $query
112            done
113            ;;
114        bootprogress)
115            OBJECT=$STATE_OBJECT/host0
116            SERVICE=$(mapper get-service $OBJECT)
117            INTERFACE=$STATE_INTERFACE.Boot.Progress
118            PROPERTY=BootProgress
119            state_query $SERVICE $OBJECT $INTERFACE $PROPERTY
120            ;;
121        power)
122            OBJECT=/org/openbmc/control/power0
123            SERVICE=$(mapper get-service $OBJECT)
124            INTERFACE=org.openbmc.control.Power
125            for property in pgood state pgood_timeout
126            do
127                # get_property can potentially return several
128                # different formats of values, so we do the parsing outside
129                # of get_property depending on the query. These queries
130                # return 'i VALUE' formatted strings.
131                STATE=$(get_property $SERVICE $OBJECT $INTERFACE $property \
132                    | sed 's/i[ ^I]*//')
133                printf "%s = %s\n" $property $STATE
134            done
135            ;;
136        *)
137            print_usage_err "Invalid command '$1'"
138            ;;
139    esac
140}
141
142for arg in "$@"; do
143    case $arg in
144        -h|--help)
145            print_help
146            ;;
147        -*)
148            print_usage_err "Unknown option: $arg"
149            ;;
150        *)
151            handle_cmd $arg
152            break
153            ;;
154    esac
155done
156