117 lines
3.8 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2010-2014 All Rights Reserved.
//
//----------------------------------------------------------------------------
using System;
using System.Text.RegularExpressions;
using Intel.Manageability;
using Intel.Manageability.Exceptions;
using Intel.Manageability.Power;
using Common.Utils;
using System.IO;
using System.Runtime.InteropServices;
namespace PowerSample
{
class Program
{
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultDllDirectories(int directoryFlags);
static void Main(string[] args)
{
// set default dll lookup directory to system
SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32
IAMTInstance amt = null;
ConnectionInfoEX ci = null;
try
{
// Check if JSON path was provided as an argument. If not, default path would be used.
try
{
ci = ProgramInput.DeserializeJsonToStruct(args.Length > 0 ? args[0] : null);
}
catch (IOException e)
{
Console.WriteLine($"Could not read argument file: {e.Message}");
return;
}
amt = AMTInstanceFactory.CreateEX(ci);
}
catch (ManageabilityException e)
{
ci?.Dispose();
Console.WriteLine(e.Message);
return;
}
try
{
// Get current power state
OSPowerState state = PowerFunctionality.GetPowerState(amt);
// Wake up host if currently power saving
if (state.PowerSavingState == PowerSavingState.OSPowerSaving)
PowerFunctionality.WakeFromPowerSavingState(amt);
do
{
// Update state
state = PowerFunctionality.GetPowerState(amt);
// Print the transitions that are allowed from the current state.
PowerFunctionality.PrintAllowedCommands(amt, state);
Console.WriteLine("Press enter to exit or enter requested Power Operation name and than enter:");
var input = Console.ReadLine();
// Breaks on empty line
if (string.IsNullOrEmpty(input))
{
break;
}
// Remove all whitespaces
input = Regex.Replace(input, @"\s", "");
// Check input validity
if (!Enum.TryParse<PowerFunctionality.PowerOption>(input, true, out var powerOption))
{
Console.WriteLine("Invalid power operation.");
continue;
}
try
{
// Perform requested power operation
PowerFunctionality.PerformPowerOperation(amt, powerOption);
}
catch (PowerManagabilityException e)
{
Console.Error.WriteLine(e.Message);
}
Console.WriteLine();
// Wait for changes to take effect
PowerFunctionality.WaitForStateChange(amt, state);
} while (true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
amt?.Dispose();
}
}
}
}