203 lines
7.9 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2011 - 2014 All Rights Reserved.
//
// File: PowerPackageSample.cs
//
// Contents: Sample code for Intel(R) Active Management Technology
// (Intel® AMT) PowerPackage Sample.
//
// Notes: This sample demonstrates how to use various commands of
// the PowerPackage service.
//
//----------------------------------------------------------------------------
using System;
using Utils;
using Intel.Management.Wsman;
using System.Runtime.InteropServices;
namespace PowerPackage
{
class PowerPackageSample
{
#region CONSTANTS
public const string S0_ONLY = "S0Only";
public const string SX_AC = "SxAC";
private const ushort MAX_IDLE_TIMEOUT = 65535;
// function options
private const string ENUMERATE_POWER_PACKAGES = "enumerate the power packages";
private const string GET_CURRENT_POWER_PACKAGE = "get the current active power package";
private const string SET_POWER_PACKAGE_BASED_ON_GUID = "set power package based on guid";
private const string GET_IDLE_TIMEOUT = "get the idle wake timeout";
private const string SET_IDLE_TIMEOUT = "set the idle wake timeout (minutes)";
private const string WAKE_ON = "Use -" + WAKE_ON_FLAG + " on|off to enable / disable power package which support wake on lan.";
// function option flags
private const string ENUMERATE_POWER_PACKAGES_FLAG = "enumerate";
private const string GET_CURRENT_POWER_PACKAGE_FLAG = "getcurrent";
private const string SET_POWER_PACKAGE_BASED_ON_GUID_FLAG = "applyguid";
private const string GET_IDLE_TIMEOUT_FLAG = "getidletimeout";
private const string SET_IDLE_TIMEOUT_FLAG = "setidletimeout";
private const string AFTER_POWER_LOSS_FLAG = "allowHostWakeAfterPowerLoss";
private const string IDLE_TIMEOUT_FLAG = "idleWakeTimeout";
private const string FORCE_MOVE_TO_S0_FLAG = "force";
private const string WAKE_ON_FLAG = "preferWakeOn";
// Exit Codes Types
private enum exitCodes
{
EXIT_SUCCESS = 0,
EXIT_FAILURE,
EXIT_USAGE,
EXIT_COMMUNICATION_ERROR,
EXIT_ARGUMENT_ERROR,
}
#endregion CONSTANTS
#region PRIVATE_DATA_MEMBERS
// WSManClient
private WsmanConnection wsmanCient;
// User parameters
private static CmdLineArguments Params = new CmdLineArguments();
#endregion PRIVATE_DATA_MEMBERS
#region MAIN
[DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetDefaultDllDirectories(int directoryFlags);
static int Main(string[] args)
{
// set default dll lookup directory to system
SetDefaultDllDirectories(0x00000800); //LOAD_LIBRARY_SEARCH_SYSTEM32
exitCodes exitCode = exitCodes.EXIT_SUCCESS;
PowerPackageApi api = null;
// Add command line argument options
Params.init_functions();
// Add options to activate
Params.AddArg(GET_IDLE_TIMEOUT_FLAG, false, false, GET_IDLE_TIMEOUT);
Params.AddArg(GET_CURRENT_POWER_PACKAGE_FLAG, false, false, GET_CURRENT_POWER_PACKAGE);
Params.AddArg(SET_POWER_PACKAGE_BASED_ON_GUID_FLAG, true, false, SET_POWER_PACKAGE_BASED_ON_GUID);
Params.AddArg(SET_IDLE_TIMEOUT_FLAG, true, false, SET_IDLE_TIMEOUT);
Params.AddArg(ENUMERATE_POWER_PACKAGES_FLAG, false, false, ENUMERATE_POWER_PACKAGES);
// Creates usage string
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
string usage = string.Empty;
// If no params were given, just print the usage
if (args.Length == 0)
{
usage = Params.CreateUsage(assembly, false);
Console.WriteLine("\n" + usage);
return (int)exitCodes.EXIT_SUCCESS;
}
try
{
try
{
// Verify command line arguments.
Params.Parse(args);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(Params.CreateUsage(assembly, false));
return 0;
}
bool verbose = Params.Selected(CmdLineArguments.OPT_VERBOSE);
//Create the WSMan Connection.
//Check if TLS option is selected or not.
if (Params.Selected(CmdLineArguments.OPT_SECURE) == false)
{
api = new PowerPackageApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
else
{
api = new PowerPackageApi(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], Params[CmdLineArguments.OPT_CERT], Params.Selected(CmdLineArguments.OPT_KRB), Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
// Main flow
if (!validateFlagsLogic())
{
throw new CmdLineArguments.Exception("Illegal value for " + SET_IDLE_TIMEOUT_FLAG);
}
//check for only 1 parameter
switch (args[0].Trim('-'))
{
//enumerate Power Packages
case ENUMERATE_POWER_PACKAGES_FLAG:
api.enumerate();
break;
//get powerpackage state
case GET_CURRENT_POWER_PACKAGE_FLAG:
api.getActivePowerSchema();
break;
//set powerpackage based on GUID
case SET_POWER_PACKAGE_BASED_ON_GUID_FLAG:
api.setPowerPackageBasedOnGuid(Params[SET_POWER_PACKAGE_BASED_ON_GUID_FLAG]);
break;
//set Idletimeout
case SET_IDLE_TIMEOUT_FLAG:
uint val;
if (!(uint.TryParse(Params[SET_IDLE_TIMEOUT_FLAG], out val)) || !(val >= 1 && val <= MAX_IDLE_TIMEOUT))
{
throw new CmdLineArguments.Exception("Illegal value for " + SET_IDLE_TIMEOUT_FLAG);
}
api.setIdleWakeTimeout(val);
break;
//get idle timeout
case GET_IDLE_TIMEOUT_FLAG:
api.getIdleWakeTimeout();
break;
default: Console.WriteLine("No option was selected");
break;
}
//end switch
}
//end Try
catch (Exception e)
{
//Check for the Type of Exception created during execution.
exitCode = (exitCodes)Params.catchType(e, null);
}
finally
{
api?.Dispose();
}
return (int)exitCode;
}
private static bool validateFlagsLogic()
{
bool setAvailabilitySx = (Params.Selected(AFTER_POWER_LOSS_FLAG) || Params.Selected(IDLE_TIMEOUT_FLAG) || Params.Selected(WAKE_ON_FLAG));
bool setAvailabilityS0 = Params.Selected(FORCE_MOVE_TO_S0_FLAG);
return true;
}
#endregion MAIN
}
}