254 lines
9.3 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2011 - 2015 All Rights Reserved.
//
// File: AlarmClockSample.cs
//
// Contents: Sample code for Intel(R) Active Management Technology
// (Intel® AMT) AlarmClock Sample.
//
// Notes: This sample demonstrates how to use some of the features and
// capabilities provided by the AlarmClock service of Intel® AMT,
// over WS-Management.
//
//----------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using Utils;
namespace AlarmClock
{
class AlarmClock_Sample
{
#region CONSTANTS
/// <summary>
/// User input flags.
/// </summary>
public enum YesNoFlag
{
YES,
NO
};
public const string SAMPLE_ALARM_NAME = "SampleAlarmName";
public const string OPT_ENUMERATE = "getallcontexts";
public const string OPT_SUBSCRIBE = "subscribe";
public const string OPT_UNSUBSCRIBE = "unsubscribe";
public const string OPT_IS_MACHINE_CONNECTED = "ismachineconnected";
// Command line flags
private const string OPT_SINGLE_WAKE_ON = "singlewakeon";
private const string OPT_PERIODIC_WAKE_ON = "periodicwakeon";
private const string OPT_DISABLE_WAKE_ON = "disablewakeon";
private const string OPT_WAKE_ON_INFO = "wakeoninfo";
private const string OPT_RUN = "api";
// Command line description
private const string SINGLE_WAKE_ON_DESCRIPTION = "Configure a single wake on";
private const string PERIODIC_WAKE_ON_DESCRIPTION = "Configure a periodic wake on";
private const string DISABLE_WAKE_ON_DESCRIPTION = "Disable the alarm clock";
private const string WAKE_ON_INFO_DESCRIPTION = "Display the alarm clock settings";
private const string RUN_DESCRIPTION = "Run without user parameters";
// Exit Codes Types
private enum exitCodes
{
EXIT_SUCCESS = 0
}
#endregion CONSTANTS
#region PRIVATE_DATA_MEMBERS
// WS-Management session
//private IWsmanConnection wsmanClient = null;
// User parameters
private static CmdLineArguments Params = new CmdLineArguments();
#endregion PRIVATE_DATA_MEMBERS
#region PRIVATE FUNCTIONS
public static void periodicWakeOn(AlarmClock_Api api)
{
DateTimeInterval AMTInterval;
DateTime NextAMTAlarmTime = DateTime.Now;
NextAMTAlarmTime = NextAMTAlarmTime.AddSeconds(60 - NextAMTAlarmTime.Second);// the seconds must be 0.
Console.WriteLine("\nCurrent Date time is:{0}\n", NextAMTAlarmTime);
//Auto set Date time to 2days from current date and time...
NextAMTAlarmTime = NextAMTAlarmTime.AddDays(2);
Console.WriteLine("Date time to be Set:{0}", NextAMTAlarmTime);
//Auto set interval to once a day...
AMTInterval = new DateTimeInterval(1, 0, 0);
Console.WriteLine("Date time interval to be set:{0}:{1}:{2}", AMTInterval.Days, AMTInterval.Hours, AMTInterval.Minutes);
// Configure a periodic wake on
api.PeriodicWakeOn(NextAMTAlarmTime, AMTInterval);
}
public static void singleWakeOn(AlarmClock_Api api)
{
DateTime NextAMTAlarmTime = DateTime.Now;
NextAMTAlarmTime = NextAMTAlarmTime.AddSeconds(60 - NextAMTAlarmTime.Second);// the seconds must be 0.
Console.WriteLine("\nCurrent Date time is:{0}\n", NextAMTAlarmTime);
//Auto set Date time to 2days from current date and time...
NextAMTAlarmTime = NextAMTAlarmTime.AddDays(2);
Console.WriteLine("Date time to be Set:{0}", NextAMTAlarmTime);
// Configure a single wake on
api.SingleWakeOn(NextAMTAlarmTime);
}
#endregion PRIVATE FUNCTIONS
#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;
AlarmClock_Api api = null;
int i = 0;
#region INIT_COMMAND_LINE_ARGUMENTS
// Add command line argument options.
Params.init_functions();
//Add options to activate
Params.AddArg(OPT_SINGLE_WAKE_ON, false, false, SINGLE_WAKE_ON_DESCRIPTION);
Params.AddArg(OPT_PERIODIC_WAKE_ON, false, false, PERIODIC_WAKE_ON_DESCRIPTION);
Params.AddArg(OPT_DISABLE_WAKE_ON, false, false, DISABLE_WAKE_ON_DESCRIPTION);
Params.AddArg(OPT_WAKE_ON_INFO, false, false, WAKE_ON_INFO_DESCRIPTION);
Params.AddArg(OPT_RUN, false, false, RUN_DESCRIPTION);
#endregion
try
{
// 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)
{
Params.MessageDisplay_Color("This feature is supported from Intel AMT FW version 5.1 and above.\n", ConsoleColor.Yellow);
usage = Params.CreateUsage(assembly, false);
Console.WriteLine(usage);
return (int)exitCodes.EXIT_SUCCESS;
}
try
{
// Verify command line arguments.
Params.Parse(args);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Params.MessageDisplay_Color("This feature is supported from Intel AMT FW version 5.1 and above.\n", ConsoleColor.Yellow);
usage = Params.CreateUsage(assembly, false);
Console.WriteLine(usage);
return 0;
}
bool krb = (Params.Selected(CmdLineArguments.OPT_KRB) == false) ? false : true;
//Create the WSMan Connection Object based on TLS/Non-TLS option.
if (Params.Selected(CmdLineArguments.OPT_SECURE) == false)
{
api = new AlarmClock_Api(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], krb, Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));
}
else
{
api = new AlarmClock_Api(Params[CmdLineArguments.OPT_HOST], Params[CmdLineArguments.OPT_USER], Params[CmdLineArguments.OPT_PASS], Params[CmdLineArguments.OPT_CERT], krb, Params.GetWebProxy(), Params.Selected(CmdLineArguments.ACCEPT_SELF_SIGNED_CERTIFICATE));//Params[OPT_HOST], Params[OPT_USER], Params[OPT_PASS]);
}
// Main flow
//parse through all the args passed in command line to call the requested function
while (i < args.Length)
{
int flag;
switch (args[i])
{
//Call RunAPI Test.
case "-" + OPT_RUN:
api.RunAPITest();
flag = 1;
break;
//Set SingleWake On.
case "-" + OPT_SINGLE_WAKE_ON:
singleWakeOn(api);
flag = 1;
break;
//Set Periodic Wake On.
case "-" + OPT_PERIODIC_WAKE_ON:
periodicWakeOn(api);
flag = 1;
break;
// Disable the alarm clock.
case "-" + OPT_DISABLE_WAKE_ON:
api.DisableWakeOn();
flag = 1;
break;
// Display the alarm clock settings.
case "-" + OPT_WAKE_ON_INFO:
api.WakeOnInfo();
flag = 1;
break;
//Default, check the next arg value.
default: i++;
flag = 0;
break;
}
//end switch
if (flag == 1)
break;
}
//end while
}
//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;
}
//end MAIN
#endregion MAIN
}
}