//---------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2009-2014 All Rights Reserved. // // File: FunctionalityPower.cs // // Contents: High Level API definition: CLI DLL for Power // // Notes: // //---------------------------------------------------------------------------- using System; using Intel.Manageability; using Intel.Manageability.Power; using Intel.Manageability.Exceptions; namespace PowerSample { public class PowerFunctionality { public enum PowerOption { PowerUp, PowerCycle, PowerDown, Reset, Sleep, Hibernate, GracefulOff, GracefulReset, NMI } public static OSPowerState GetPowerState(IAMTInstance amt, bool verbose = true) { OSPowerState state; try { state = amt.Power.CurrentOSPowerState; if (verbose) { Console.WriteLine("Current OS Power state = " + state.PowerState); Console.WriteLine("Current Power Saving state = " + state.PowerSavingState); } return state; } catch (Exception ex) { Console.WriteLine("Fail to get OS Power state."); Console.WriteLine("\n" + ex.Message); state.PowerState = PowerState.Unknown; state.PowerSavingState = PowerSavingState.Unknown; return state; } } public static void WakeFromPowerSavingState(IAMTInstance amt) { try { amt.Power.WakeFromPowerSavingState(); Console.WriteLine("\nWake from OS Power Saving state completed successfully."); } catch (ManageabilityException ex) { Console.WriteLine("Fail to wake from OS Power Saving state."); Console.WriteLine("\n" + ex.Message); } } /// /// Print the transitions that are allowed from the current state. /// /// The Intel AMT instance /// OS power state public static void PrintAllowedCommands(IAMTInstance amt, OSPowerState state) { try { Console.WriteLine("Current power state = " + state.PowerState); PowerTransition[] transitions; amt.Power.AllowedTransitions(state.PowerState, out transitions); if (transitions != null) { Console.Write("Allowed commands are: "); foreach (PowerTransition t in transitions) { Console.Write(" {0}", t); } Console.WriteLine(); } else { Console.WriteLine("No commands allowed."); } } catch (ManageabilityException ex) { Console.WriteLine("Print allowed commands failed"); Console.WriteLine("\n" + ex.Message); } } /// /// Perform a power operation /// /// The Intel AMT instance /// The operation to perform public static void PerformPowerOperation(IAMTInstance amt, PowerOption powerOption) { try { // Deprecated: // PowerState currentState = amt.Power.CurrentPowerState; OSPowerState currentState = amt.Power.CurrentOSPowerState; if (IsCommandAllowed(amt, currentState.PowerState, powerOption)) { switch (powerOption) { case PowerOption.PowerUp: amt.Power.Powerup(); break; case PowerOption.PowerDown: amt.Power.PowerDown(); break; case PowerOption.PowerCycle: amt.Power.PowerCycle(); break; case PowerOption.Reset: amt.Power.Reset(); break; case PowerOption.GracefulReset: amt.Power.GracefulReset(); break; case PowerOption.GracefulOff: amt.Power.GracefulShutDown(); break; case PowerOption.Sleep: amt.Power.Sleep(); break; case PowerOption.Hibernate: amt.Power.Hibernate(); break; case PowerOption.NMI: amt.Power.NMI(); break; } Console.WriteLine("\n" + powerOption + " operation completed successfully."); } else throw new Exception(powerOption + ": Requested operation is not allowed in AMT system's current state."); } catch (ManageabilityException ex) { Console.WriteLine("Perform power operation failed"); Console.WriteLine("\n" + ex.Message); } catch (Exception ex) { Console.WriteLine("Perform power operation failed"); Console.WriteLine("\n" + ex.Message); } } public static void WaitForStateChange(IAMTInstance amt, OSPowerState state) { for (int i = 0; i < 6; ++i) { var currentState = GetPowerState(amt, false); if (state.PowerState != currentState.PowerState) return; System.Threading.Thread.Sleep(500); } } /// /// Check if a power command is allowed according to the current Intel AMT power state. /// /// The Intel AMT instance /// The current Intel AMT power state /// The operation to perform /// True if the command is allowed, otherwise false. private static bool IsCommandAllowed(IAMTInstance amt, PowerState currentState, PowerOption action) { PowerTransition[] transitions; amt.Power.AllowedTransitions(currentState, out transitions); if (transitions == null) return false; switch (action) { case PowerOption.PowerCycle: return CommandInList(PowerTransition.PowerCycle, transitions); case PowerOption.PowerDown: return CommandInList(PowerTransition.PowerDown, transitions); case PowerOption.PowerUp: return CommandInList(PowerTransition.PowerUp, transitions); case PowerOption.Reset: return CommandInList(PowerTransition.Reset, transitions); case PowerOption.GracefulOff: return CommandInList(PowerTransition.GracefulOff, transitions); case PowerOption.GracefulReset: return CommandInList(PowerTransition.GracefulReset, transitions); case PowerOption.Sleep: return CommandInList(PowerTransition.Sleep, transitions); case PowerOption.Hibernate: return CommandInList(PowerTransition.Hibernate, transitions); case PowerOption.NMI: return CommandInList(PowerTransition.NMI, transitions); } return false; } /// /// Check if the desired command is in the PowerTransition list /// /// The desired command /// The PowerTransition list /// True if the command is in the list, otherwise false. private static bool CommandInList(PowerTransition wanted, PowerTransition[] list) { foreach (PowerTransition t in list) { if (t == wanted) { return true; } } return false; } } }