1*7c0e43dbSPeter Yin#!/bin/bash
2*7c0e43dbSPeter Yin# Usage of this utility
3*7c0e43dbSPeter Yinfunction usage() {
4*7c0e43dbSPeter Yin    echo "usage: power-util mb [on|off|status|cycle|reset]";
5*7c0e43dbSPeter Yin    echo "       power-util sled-cycle"
6*7c0e43dbSPeter Yin}
7*7c0e43dbSPeter Yin
8*7c0e43dbSPeter Yinpower_off() {
9*7c0e43dbSPeter Yin    echo "Shutting down Server"
10*7c0e43dbSPeter Yin    busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Off
11*7c0e43dbSPeter Yin}
12*7c0e43dbSPeter Yin
13*7c0e43dbSPeter Yinpower_on() {
14*7c0e43dbSPeter Yin    echo "Powering on Server"
15*7c0e43dbSPeter Yin    busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.On
16*7c0e43dbSPeter Yin}
17*7c0e43dbSPeter Yin
18*7c0e43dbSPeter Yinpower_status() {
19*7c0e43dbSPeter Yin    st=$(busctl get-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis CurrentPowerState | cut -d"." -f6)
20*7c0e43dbSPeter Yin    if [ "$st" == "On\"" ]; then
21*7c0e43dbSPeter Yin        echo "on"
22*7c0e43dbSPeter Yin    else
23*7c0e43dbSPeter Yin        echo "off"
24*7c0e43dbSPeter Yin    fi
25*7c0e43dbSPeter Yin}
26*7c0e43dbSPeter Yin
27*7c0e43dbSPeter Yinpower_reset() {
28*7c0e43dbSPeter Yin    echo "Reset on server"
29*7c0e43dbSPeter Yin    busctl set-property xyz.openbmc_project.State.Chassis /xyz/openbmc_project/state/chassis0 xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.Reset
30*7c0e43dbSPeter Yin}
31*7c0e43dbSPeter Yin
32*7c0e43dbSPeter Yinsled_cycle() {
33*7c0e43dbSPeter Yin    i2cset -y 7 0x45 0xd9 c
34*7c0e43dbSPeter Yin}
35*7c0e43dbSPeter Yin
36*7c0e43dbSPeter Yinif [ "$1" == "sled-cycle" ]; then
37*7c0e43dbSPeter Yin    echo "SLED_CYCLE starting at $(date)"
38*7c0e43dbSPeter Yin    sled_cycle
39*7c0e43dbSPeter Yin    exit 0;
40*7c0e43dbSPeter Yinfi
41*7c0e43dbSPeter Yin
42*7c0e43dbSPeter Yinif [ $# -lt 2 ]; then
43*7c0e43dbSPeter Yin    echo "Total number of parameter=$#"
44*7c0e43dbSPeter Yin    echo "Insufficient parameter"
45*7c0e43dbSPeter Yin    usage;
46*7c0e43dbSPeter Yin    exit 0;
47*7c0e43dbSPeter Yinfi
48*7c0e43dbSPeter Yin
49*7c0e43dbSPeter Yinif [ "$1" != "mb" ]; then
50*7c0e43dbSPeter Yin    echo "Invalid parameter1=$1"
51*7c0e43dbSPeter Yin    usage;
52*7c0e43dbSPeter Yin    exit 0;
53*7c0e43dbSPeter Yinfi
54*7c0e43dbSPeter Yin
55*7c0e43dbSPeter Yinif [ "$2" = "on" ]; then
56*7c0e43dbSPeter Yin  if [ "$(power_status)" == "off" ]; then
57*7c0e43dbSPeter Yin      power_on
58*7c0e43dbSPeter Yin  fi
59*7c0e43dbSPeter Yinelif [ "$2" = "off" ]; then
60*7c0e43dbSPeter Yin  if [ "$(power_status)" == "on" ]; then
61*7c0e43dbSPeter Yin      power_off
62*7c0e43dbSPeter Yin  fi
63*7c0e43dbSPeter Yinelif [ "$2" == "cycle" ]; then
64*7c0e43dbSPeter Yin  if [ "$(power_status)" == "on" ]; then
65*7c0e43dbSPeter Yin      power_off
66*7c0e43dbSPeter Yin  else
67*7c0e43dbSPeter Yin    echo "WARNING: Powering on server"
68*7c0e43dbSPeter Yin  fi
69*7c0e43dbSPeter Yin    power_on
70*7c0e43dbSPeter Yinelif [ "$2" == "reset" ]; then
71*7c0e43dbSPeter Yin  if [ "$(power_status)" == "on" ]; then
72*7c0e43dbSPeter Yin      power_reset
73*7c0e43dbSPeter Yin  else
74*7c0e43dbSPeter Yin    echo "ERROR: Server not powered on"
75*7c0e43dbSPeter Yin  fi
76*7c0e43dbSPeter Yinelif [ "$2" == "status" ]; then
77*7c0e43dbSPeter Yin    power_status
78*7c0e43dbSPeter Yinelse
79*7c0e43dbSPeter Yin    echo "Invalid parameter2=$2"
80*7c0e43dbSPeter Yin    usage;
81*7c0e43dbSPeter Yinfi
82*7c0e43dbSPeter Yin
83*7c0e43dbSPeter Yinexit 0;
84