//----------------------------------------------------------------------------
//
// Copyright © 2009-2014, Intel Corporation. All rights reserved.
//
// File: TargetSystem.cs
//
//----------------------------------------------------------------------------
using System;
using UCT.Utils;
using System.Threading;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Intel.Management.Wsman;
namespace UCT
{
#region - Enums -
///
/// Indicate the wireless profile state.
/// Wireless has IT profiles or / and User profiles.
/// If user has no user profiles wireless flow cannot continue
///
public enum WirelessProfilesState
{
///
/// No profiles at all
///
None,
///
/// Just IT Profiles
///
ITProfiles,
///
/// Just user profiles
///
UserProfiles,
///
/// Both user and IT profiles
///
BothITAndUserProfiles
}
///
/// Link interface Wired/Wireless
///
public enum LinkInterface
{
///
/// no link interface detected
///
None = 0,
///
/// wired link interface
///
Wired,
///
/// wireless link interface
///
Wireless
};
///
/// Link control
///
public enum LinkControl
{
///
/// no link control detected
///
None,
///
/// ME control
///
ME = 1,
///
/// Host control
///
Host = 2
};
public enum TimerStatus
{
///
/// The timer is diable (00:00)
///
DISABLE,
///
/// The timer is freeze (stand on 14:59)
///
FREEZE,
///
/// Timer should start running
///
RUN
}
public enum PowerState
{
[Description("Unknown")]
Unknown = 0,
[Description("Unknown")]
Other = 1,
[Description("On")]
On,
[Description("Sleep")]
LightSleep,
[Description("Sleep")]
DeepSleep,
[Description("Power Cycle")]
SoftPowerCycle,
[Description("Off")]
HardOff,
[Description("Hibernate")]
Hibernate,
[Description("Off")]
SoftOff,
[Description("Power Cycle")]
HardPowerCycle,
[Description("Reset")]
MasterBusReset,
[Description("Diagnostic Interrupt")]
DiagnosticInterrupt,
[Description("GracefulOff")]
GracefulOff,
[Description("GracefulReset")]
GracefulReset = 14
};
public enum DefaultScreen
{
[Description("PrimaryScreen")]
PrimaryScreen,
[Description("SecondaryScreen")]
SecondaryScreen,
[Description("ThirdScreen")]
ThirdScreen,
[Description("FourthScreen")]
FourthScreen,
[Description("FifthScreen")]
FifthScreen,
[Description("SixthScreen")]
SixthScreen,
};
public enum OptInPolicy : uint
{
NONE,
KVM,
ALL = UInt32.MaxValue
};
///
/// Indicates the state of OptIn (User Consent)
///
public enum OptInState : uint
{
[Description("Not Started")]
NotStarted,
///
/// A console has required an optIn code, but it was not displayed to the user yet
///
[Description("Requested")]
Requested,
///
/// The optIn code was displayed to the user
///
[Description("Displayed")]
Displayed,
///
/// optIn code was successfully entered by the IT
///
[Description("Received")]
Received,
///
/// The console has an open session
///
[Description("In Session")]
InSession,
///
/// The optInState is unknown
///
[Description("Unknown")]
Unknown
};
public enum MessageType
{
GENERAL,
INFO,
WARNING,
ERROR,
QUESTION
}
public enum EnabledState : ushort
{
[Description("Unknown")]
Unknown,
[Description("Other")]
Other,
[Description("Enabled")]
Enabled,
[Description("Disabled")]
Disabled,
[Description("Shutting Down")]
ShuttingDown,
[Description("Not Applicable")]
NotApplicable,
[Description("Enabled But Offline")]
EnabledButOffline,
[Description("In Test")]
InTest,
[Description("Deferred")]
Deferred,
[Description("Quiesce")]
Quiesce,
[Description("Starting")]
Starting,
[Description("Reserved")]
Reserved
};
///
/// WS-Mangement status codes
///
public enum WSMAN_KVM_STATUS_CODE
{
///
/// the operation succeeded
///
[Description("success")]
OK = 0,
///
/// KVM Disabled in the MEBX
///
[Description("KVM is Disabled in the MEBx")]
KVM_DISABLED_IN_MEBX,
///
/// Failed to perform requested state change
///
[Description("Failed to perform requested state change")]
KVM_REQUEST_STATE_CHANGE_FAILED,
///
/// KVM general error
///
[Description("KVM internal error")]
KVM_ERROR
};
#endregion
///
/// Describe the target AMT system
///
public class TargetSystem : IDisposable
{
#region - Extern Methods -
[DllImport("AdvAPI32.dll")]
private static extern bool InitiateSystemShutdown(string lpMachineName,
string lpMessage,
int dwTimeout,
bool bForceAppsClosed,
bool bRebootAfterShutdown);
#endregion
#region - Consts -
private const uint LinkTimeoutSeconds = 30;
private const string SupportOpInTimeOut = "6.2.0";
private const string Support3Displays = "8.0";
private const string SupportLinkProtection = "8.1.0";
#endregion
#region - Members -
private WsmanConnection _wsmanConnection;
private bool _requested = true;
private LogicLayer _logicLayer;
public ManualResetEvent _manualResetEvent = new ManualResetEvent(true);
public PowerOperations PowerOperation { set; get; }
public UCTOperations UctOperation { set; get; }
public WirelessOperations WirelessOperation { set; get; }
#endregion
#region - Constructor -
public TargetSystem(LogicLayer logicLayer)
{
_wsmanConnection = Configuration._wsmanConnection;
PowerOperation = new PowerOperations(this);
UctOperation = new UCTOperations(this);
WirelessOperation = new WirelessOperations(this);
_logicLayer = logicLayer;
UctOperation.OptInChangedEvent += new StateChanged_D(this.OptInChangedEvent);
PowerOperation.PowerStateChangedEvent += new PowerStateChanged_D(this.PowerStateChangedEvent);
WirelessOperation.WirelessLinkChangedEvent += new WirelessLinkChanged_D(_wirelessOperation_WirelessLinkChangedEvent);
PowerOperation.StartThread();
UctOperation.StartThread();
if (Configuration.InterfaceSettings.GetInstance().LinkInterface == LinkInterface.Wireless)
WirelessOperation.StartThread();
}
void _wirelessOperation_WirelessLinkChangedEvent()
{
_logicLayer.UpdateWirelessDetails(WirelessOperation.LinkControl, WirelessOperation.LinkPreference);
}
#endregion
#region - Methods -
#region - Public Functions -
public Dictionary GetConnectionParameters()
{
try
{
_wsmanConnection = Configuration._wsmanConnection;
Dictionary connectionParams = Merge(PowerOperation.GetPowerParameters(), UctOperation.GetUCTParameters());
_logicLayer.WriteLog(LogLevel.DETAILED, "Getting system properties.", LogInteraction.FWOperation);
// Add FW Version
IManagedReference softwareIdentityRef = _wsmanConnection.NewReference("SELECT * FROM CIM_SoftwareIdentity WHERE InstanceID='AMT FW Core Version'");
IManagedInstance softwareIdentityInstance = softwareIdentityRef.Get();
connectionParams.Add("CodeVersion", softwareIdentityInstance.GetProperty("VersionString").ToString());
// Add build number
softwareIdentityRef = _wsmanConnection.NewReference("SELECT * FROM CIM_SoftwareIdentity WHERE InstanceID='Build Number'");
softwareIdentityInstance = softwareIdentityRef.Get();
connectionParams.Add("VersionString", softwareIdentityInstance.GetProperty("VersionString").ToString());
// Add AMT host name property
IManagedReference generalSettingsRef = _wsmanConnection.NewReference("AMT_GeneralSettings");
IManagedInstance generalSettingsInstance = generalSettingsRef.Get();
connectionParams.Add("HostName", generalSettingsInstance.GetProperty("HostName").ToString());
return connectionParams;
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
public void PowerStateChangedEvent()
{
_logicLayer.UpdatePowerState(PowerOperation.PowerState);
}
public void OptInChangedEvent()
{
_logicLayer.UpdateOptInState(UctOperation.OptInState);
}
#endregion
#region - Private Functions -
private static Dictionary Merge(params Dictionary[] dictionaries)
{
Dictionary result = new Dictionary();
foreach (Dictionary dictionary in dictionaries)
foreach (KeyValuePair pair in dictionary)
result[pair.Key] = pair.Value;
return result;
}
#endregion
#region - Logic Functions -
///
/// Set the OptIn state to its default value
///
internal void SetDefaultOptInState()
{
if (null != UctOperation)
{
UctOperation.OptInState = OptInState.Unknown;
}
}
///
/// Set the power state to its default value
///
internal void SetDefaultPowerState()
{
if (null != UctOperation)
PowerOperation.PowerState = PowerState.Unknown;
}
internal OptInPolicy GetOptInPolicy()
{
_logicLayer.WriteLog(LogLevel.DETAILED,
"Invoke IPS_OptInService.Get( OptInRequired property )", LogInteraction.FWOperation);
return (OptInPolicy)UctOperation.GetOptInPolicy();
}
internal OptInState GetOptInState()
{
return (OptInState)UctOperation.OptInState;
}
internal uint StartConsent()
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke IPS_OptInService.StartOptIn", LogInteraction.FWOperation);
return UctOperation.StartOptIn();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal uint SendCode(uint code)
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke IPS_OptInService.SendOptInCode( code = " + code + " )", LogInteraction.FWOperation);
return UctOperation.SendCode(code);
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal uint CancelConsent()
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke IPS_OptInService.CancelOptIn", LogInteraction.FWOperation);
return UctOperation.CancelOptIn();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal void SetDefaultMonitor()
{
try
{
UctOperation.ChangeDefaultScreen();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal void SetDefaultMonitor(DefaultScreen screen)
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED,
"Invoke IPS_SecIOService.Put( DefaultScreen = " + Utilities.GetEnumDescription(screen) + " )", LogInteraction.FWOperation);
UctOperation.SetNewScreen((Byte)(screen));
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal void SetDefaultTimeout(ushort minutes)
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED,
"Invoke IPS_OptInService.Put( OptInDisplayTimeout = " + minutes.ToString() + " )", LogInteraction.FWOperation);
UctOperation.SetDefaultTimeout(minutes * 60);
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal PowerState GetPowerState()
{
_logicLayer.WriteLog(LogLevel.DETAILED,
"Invoke CIM_PowerManagementService.Get()", LogInteraction.FWOperation);
return PowerOperation.PowerState;
}
internal uint ChangePowerState(PowerState powerState)
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED,
"Invoke CIM_PowerManagementService.RequestPowerStateChange( state = " + Utilities.GetEnumDescription(powerState) + " )", LogInteraction.FWOperation);
return PowerOperation.ChangePowerState(powerState);
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal void SetOptInTimeout(uint time)
{
_logicLayer.WriteLog(LogLevel.DETAILED,
"Invoke IPS_OptInService.Put( OptInCodeTimeout = " + time + " )", LogInteraction.FWOperation);
UctOperation.SetOptInTimeout(time);
}
internal bool PerfomGracefulShutDown(bool reboot)
{
try
{
return PowerOperation.GreacfulShutDown(reboot);
}
catch (Exception e)
{
throw new UCTException(e.Message);
}
}
internal void StartPolling(bool connected)
{
UctOperation.connected = WirelessOperation.connected = PowerOperation.connected = connected;
if (connected && Configuration.InterfaceSettings.GetInstance().LinkInterface == LinkInterface.Wireless)
{
WirelessOperation.InitWirelessService();
WirelessOperation.StartThread();
}
}
#endregion
#region - Wireless Functions -
internal uint SetLinkPreference(LinkControl link, uint timeout)
{
try
{
return WirelessOperation.SetLinkPreference(link, timeout);
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal bool CheckAMTWirelessLink()
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke AMT_EthernetPortSettings.Get( LinkControl property )", LogInteraction.FWOperation);
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke AMT_EthernetPortSettings.Get( LinkPreference property )", LogInteraction.FWOperation);
return WirelessOperation.CheckAMTWirelessLink();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal bool CheckConnectionBetweenProfiles()
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke CIM_WiFiEndpoint.Get( LANID property )", LogInteraction.FWOperation);
return WirelessOperation.CheckConnectionBetweenProfiles();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal WirelessProfilesState GetWirelessProfilesState()
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke CIM_WiFiEndpointSettings.Enumerate()", LogInteraction.FWOperation);
return WirelessOperation.GetWirelessProfilesState();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
internal uint GetLinkProtectionLevel()
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke AMT_EthernetPortSettings.Get()", LogInteraction.FWOperation);
return WirelessOperation.GetLinkProtectionLevel();
}
catch (Exception e)
{
throw new UCTException(e.InnerException.Message);
}
}
///
/// Start polling the optIn state and set re-set the link preference
///
public void StartLinkPreferenceThread(bool start)
{
_requested = start;
if (start)
ThreadPool.QueueUserWorkItem(new WaitCallback(this.SetLinkPreference));
}
///
/// Polling the machine state in order to check if SetLinkPreference is still needed
///
///
private void SetLinkPreference(object stateInfo)
{
while (_requested)
{
if (UctOperation.OptInState == OptInState.Requested)
{
try
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke AMT_EthernetPortSettings.SetLinkPreference()", LogInteraction.FWOperation);
WirelessOperation.SetLinkPreference(LinkControl.ME, LinkTimeoutSeconds);
Thread.Sleep(20000);
}
catch (Exception e)
{
_requested = false;
_logicLayer.WriteLog(LogLevel.DETAILED, String.Format(Properties.Resources.EXCEPTION_OCCURED_ERROR_CODE, e.Message, e.TargetSite.ToString()), LogInteraction.Error);
}
}
else
{
_requested = false;
}
}
}
internal uint RestoreLinkProtection()
{
_logicLayer.WriteLog(LogLevel.DETAILED, "Invoke AMT_EthernetPortSettings.RestoreLinkProtection()", LogInteraction.FWOperation);
return WirelessOperation.RestoreLinkProtection();
}
#endregion
#endregion
#region - Inner Classes -
///
/// Describe various power operations
///
public class PowerOperations
{
#region MEMBERS
public event PowerStateChanged_D PowerStateChangedEvent;
public TargetSystem _parent;
public PowerState PowerState { set; get; }
public bool connected { set; private get; }
private IManagedReference associatedPowerManagementServiceRef;
private Mutex _powerMutex;
private IWsmanConnection _wsmanConnection;
private IManagedInstance associatedPowerManagementServiceInstance;
#endregion
#region CONSTRUCTOR
public PowerOperations(TargetSystem targetSystem)
{
_parent = targetSystem;
connected = true;
_wsmanConnection = Configuration._wsmanConnection;
associatedPowerManagementServiceRef = _wsmanConnection.NewReference("CIM_AssociatedPowerManagementService");
associatedPowerManagementServiceInstance = associatedPowerManagementServiceRef.Get();
_powerMutex = new Mutex();
PowerState = PowerState.Unknown;
}
public void StartThread()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.GetPowerState));
}
#endregion
#region METHODS
///
/// Polling the power state
///
///
public void GetPowerState(Object stateinfo)
{
uint counter = 0;
while (true)
{
if (connected)
{
_powerMutex.WaitOne();
try
{
_parent._manualResetEvent.WaitOne();
IManagedReference powerManagementServiceRef = _wsmanConnection.NewReference("CIM_AssociatedPowerManagementService");
associatedPowerManagementServiceInstance = powerManagementServiceRef.Get();
counter = (counter == 0) ? counter : counter--;
string realPowerStateStr = associatedPowerManagementServiceInstance.GetProperty("PowerState").ToString();
uint realPowerStateUint = Convert.ToUInt32(realPowerStateStr);
PowerState realPowerState = (PowerState)(realPowerStateUint);
if (PowerState != realPowerState)
{
PowerState s = realPowerState;
PowerState = s;
if (PowerStateChangedEvent != null)
{
PowerStateChangedEvent();
}
}
}
catch (Exception)
{
if (++counter == 3)
{
PowerState = PowerState.Unknown;
counter = 0;
}
}
_powerMutex.ReleaseMutex();
Thread.Sleep(500);
}
Thread.Sleep(500);
}
}
///
/// Changing the machine power state (AMT force shutdown)
///
///
public uint ChangePowerState(PowerState state)
{
IManagedReference powerManagementServiceRef = _wsmanConnection.NewReference("SELECT * FROM CIM_PowerManagementService WHERE Name='Intel(r) AMT Power Management Service'");
IManagedInstance powerManagementServiceInstance = powerManagementServiceRef.Get();
IManagedReference computerSystemRef = _wsmanConnection.NewReference("SELECT * FROM CIM_ComputerSystem WHERE Name='ManagedSystem'");
powerManagementServiceInstance = powerManagementServiceRef.CreateMethodInput("RequestPowerStateChange");
powerManagementServiceInstance.SetProperty("PowerState", (Convert.ToUInt32(state)).ToString());
powerManagementServiceInstance.SetProperty("ManagedElement", computerSystemRef);
powerManagementServiceInstance = powerManagementServiceRef.InvokeMethod(powerManagementServiceInstance);
uint response = uint.Parse(powerManagementServiceInstance.GetProperty("ReturnValue").ToString());
_parent._manualResetEvent.Reset();
Thread.Sleep(20000);
_parent._manualResetEvent.Set();
return response;
}
///
/// ShutDown the machine gracefully
///
///
public bool GreacfulShutDown(bool reboot)
{
LogManager.WriteOperation(LogLevel.DETAILED, "Attempting graceful shutdown", LogInteraction.FWOperation);
PowerState desired = reboot ? PowerState.GracefulReset : PowerState.GracefulOff;
uint result = ChangePowerState(desired);
return result == 0;
}
///
/// Return the advanced UserConsent properties
///
///
public Dictionary GetPowerParameters()
{
Dictionary powerParams = new Dictionary();
powerParams.Add("PowerState", Utilities.GetEnumDescription(((PowerState)(Convert.ToUInt32(associatedPowerManagementServiceInstance.GetProperty("PowerState").ToString())))));
return powerParams;
}
#endregion
}
///
/// Describe various user consent operations
///
public class UCTOperations
{
#region MEMBERS
public event StateChanged_D OptInChangedEvent;
public bool connected { set; private get; }
public OptInState OptInState { set; get; }
public TargetSystem _parent;
private IWsmanConnection _wsmanConnection;
private IManagedReference optInServiceRef;
private IManagedReference secIOServiceRef;
private IManagedInstance optInServiceInstance;
private IManagedInstance secIOServiceInstance;
private Mutex _optInMutex;
#endregion
#region CONSTRUCTORS
public UCTOperations(TargetSystem targetSystem)
{
_parent = targetSystem;
connected = true;
_wsmanConnection = Configuration._wsmanConnection;
secIOServiceRef = _wsmanConnection.NewReference("SELECT * FROM IPS_SecIOService WHERE Name='SecIO'");
secIOServiceInstance = secIOServiceRef.Get();
optInServiceRef = _wsmanConnection.NewReference("SELECT * FROM IPS_OptInService WHERE Name='Intel(r) AMT OptIn Service'");
optInServiceInstance = optInServiceRef.Get();
OptInState = OptInState.Unknown;
_optInMutex = new Mutex();
}
public void StartThread()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.GetOptInState));
}
#endregion
#region METHODS
public uint CancelOptIn()
{
optInServiceInstance = optInServiceRef.CreateMethodInput("CancelOptIn");
optInServiceInstance = optInServiceRef.InvokeMethod(optInServiceInstance);
return uint.Parse(optInServiceInstance.GetProperty("ReturnValue").ToString());
}
public uint StartOptIn()
{
optInServiceInstance = optInServiceRef.CreateMethodInput("StartOptIn");
optInServiceInstance = optInServiceRef.InvokeMethod(optInServiceInstance);
return uint.Parse(optInServiceInstance.GetProperty("ReturnValue").ToString());
}
public void ChangeDefaultScreen(byte currentScreen)
{
byte newScreen;
if (Utilities.CompareVersions(GetCoreVersion(), "8.0") < 0)
{
newScreen = (currentScreen == (byte)DefaultScreen.PrimaryScreen) ? (byte)DefaultScreen.SecondaryScreen : (byte)DefaultScreen.PrimaryScreen;
}
else
{
newScreen = (byte)((currentScreen + 1) % GetNumberOfScreens());
}
SetNewScreen(newScreen);
}
public void SetNewScreen(byte newScreen)
{
secIOServiceInstance.SetProperty("DefaultScreen", newScreen.ToString());
secIOServiceRef.Put(secIOServiceInstance);
}
public uint SendCode(uint code)
{
optInServiceInstance = optInServiceRef.CreateMethodInput("SendOptInCode");
optInServiceInstance.SetProperty("OptInCode", code.ToString());
optInServiceInstance = optInServiceRef.InvokeMethod(optInServiceInstance);
return uint.Parse(optInServiceInstance.GetProperty("ReturnValue").ToString());
}
public Dictionary GetUCTParameters()
{
optInServiceInstance = optInServiceRef.Get();
secIOServiceInstance = secIOServiceRef.Get();
Dictionary UCTParams = new Dictionary();
UCTParams.Add("DisplayTimeout", optInServiceInstance.GetProperty("OptInDisplayTimeout").ToString());
UCTParams.Add("CodeTimeout", optInServiceInstance.GetProperty("OptInCodeTimeout").ToString());
UCTParams.Add("OptInPolicy", Utilities.GetEnumDescription((OptInPolicy)(Convert.ToUInt32(optInServiceInstance.GetProperty("OptInRequired").ToString()))));
UCTParams.Add("OptInState", Utilities.GetEnumDescription((OptInState)(Convert.ToUInt32(optInServiceInstance.GetProperty("OptInState").ToString()))));
UCTParams.Add("DefaultMonitor", Utilities.GetEnumDescription((DefaultScreen)(Convert.ToUInt32(secIOServiceInstance.GetProperty("DefaultScreen").ToString()))));
return UCTParams;
}
public void GetOptInState(Object stateinfo)
{
uint counter = 0;
while (true)
{
if (connected)
{
_optInMutex.WaitOne();
try
{
_parent._manualResetEvent.WaitOne();
optInServiceInstance = optInServiceRef.Get();
counter = (counter == 0) ? counter : counter--;
if (OptInState != (OptInState)(Convert.ToUInt32(optInServiceInstance.GetProperty("OptInState").ToString())))
{
OptInState = (OptInState)Convert.ToUInt32(optInServiceInstance.GetProperty("OptInState").ToString());
if (OptInChangedEvent != null)
{
OptInChangedEvent();
}
}
}
catch (Exception ex)
{
if (++counter == 3 || ex.Message == "Unable to connect to the remote server")
{
OptInState = OptInState.Unknown;
counter = 0;
if (OptInChangedEvent != null)
{
OptInChangedEvent();
}
}
}
_optInMutex.ReleaseMutex();
Thread.Sleep(500);
}
Thread.Sleep(500);
}
}
public uint GetOptInState()
{
return Convert.ToUInt32(optInServiceInstance.GetProperty("OptInState").ToString());
}
public uint GetOptInPolicy()
{
return Convert.ToUInt32(optInServiceInstance.GetProperty("OptInRequired").ToString());
}
public uint GetOptInTimeout()
{
optInServiceInstance = optInServiceRef.Get();
return Convert.ToUInt32(optInServiceInstance.GetProperty("OptInCodeTimeout").ToString());
}
public bool CheckOptInTimeoutSupport()
{
return Utilities.CompareVersions(GetCoreVersion(), SupportOpInTimeOut) >= 0;
}
public bool CheckThirdScreenSupport()
{
return Utilities.CompareVersions(GetCoreVersion(), Support3Displays) >= 0;
}
internal void SetOptInTimeout(uint time)
{
optInServiceInstance.SetProperty("OptInCodeTimeout", time.ToString());
optInServiceRef.Put(optInServiceInstance);
}
internal void ChangeDefaultScreen()
{
DefaultScreen currentScreen = (DefaultScreen)Convert.ToUInt32(secIOServiceInstance.GetProperty("DefaultScreen").ToString());
ChangeDefaultScreen((Byte)currentScreen);
}
internal void SetDefaultTimeout(int minutes)
{
optInServiceInstance.SetProperty("OptInDisplayTimeout", (uint)minutes);
optInServiceRef.Put(optInServiceInstance);
}
private string GetCoreVersion()
{
IManagedReference softwareIdentityRef =
_wsmanConnection.NewReference("SELECT * FROM CIM_SoftwareIdentity WHERE InstanceID='AMT FW Core Version'");
IManagedInstance softwareIdentityInstance = softwareIdentityRef.Get();
return softwareIdentityInstance.GetProperty("VersionString").ToString();
}
private int GetNumberOfScreens()
{
IWsmanItem numberOfScreens= secIOServiceInstance.GetProperty("NumberOfScreens");
if (numberOfScreens.IsNull)// this property is available only in AMT Release 8.0 HF1 and later.
{
return Utilities.CompareVersions(GetCoreVersion(),"8.0" )< 0 ? 2 : 3;
}
return Int32.Parse(numberOfScreens.ToString());
}
#endregion
}
///
/// Describe the wireless operation
/// (used to set the link preference when the connection interface is wireless and
/// machine does not support sprite)
///
public class WirelessOperations
{
#region Consts
private const string InstanceIDUser = "WiFi Endpoint User Settings";
#endregion
#region Members
public event WirelessLinkChanged_D WirelessLinkChangedEvent;
public TargetSystem _parent;
public bool connected { set; private get; }
public LinkControl LinkControl { private set; get; }
public LinkControl LinkPreference { private set; get; }
private Mutex _wirelessMutex;
IWsmanConnection _wsmanConnection;
IManagedInstance _ethernetPortService;
IManagedReference _ethernetPortEPR;
private bool _changed = false;
#endregion
#region Constructors
public WirelessOperations(TargetSystem targetSystem)
{
_parent = targetSystem;
_wsmanConnection = Configuration._wsmanConnection;
_wirelessMutex = new Mutex();
}
#endregion
#region Methods
public bool InitWirelessService()
{
if (Configuration.InterfaceSettings.GetInstance().LinkInterface == LinkInterface.Wireless)
{
_ethernetPortEPR = Configuration.InterfaceSettings.GetInstance().EthernetPortServiceEPR;
_ethernetPortService = _ethernetPortEPR.Get();
LinkPreference = LinkControl.None;
LinkControl = LinkControl.None;
return true;
}
return false;
}
public void StartThread()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(GetWirelessLinkState));
}
///
/// Check if AMT’s wireless link is active or passive
///
/// true - wireless link is active, false - the link is passive
public bool CheckAMTWirelessLink()
{
// If LinkProtection is supported and wasn't overrieded, no need to notify the user about LinkPreference, the switch between the links will be automatically.
if (CheckLinkProtectionSupport())
return true;
_ethernetPortService = _ethernetPortEPR.Get();
return (LinkControl)Convert.ToUInt32(_ethernetPortService.GetProperty("LinkControl").ToString()) == LinkControl.ME &&
(LinkControl)Convert.ToUInt32(_ethernetPortService.GetProperty("LinkPreference").ToString()) == LinkControl.ME;
}
///
/// Check if AMT is connected to one of its defined profiles
///
///
public bool CheckConnectionBetweenProfiles()
{
return !String.IsNullOrEmpty(Configuration.InterfaceSettings.GetInstance().LanID);
}
///
/// Set the Link Preference
///
///
public uint SetLinkPreference(LinkControl link, UInt32 timeout)
{
_ethernetPortService = _ethernetPortEPR.CreateMethodInput("SetLinkPreference");
_ethernetPortService.SetProperty("LinkPreference", (Convert.ToUInt16(link)).ToString());
_ethernetPortService.SetProperty("Timeout", timeout.ToString());
_ethernetPortService = _ethernetPortEPR.InvokeMethod(_ethernetPortService);
return Convert.ToUInt32(_ethernetPortService.GetProperty("ReturnValue").ToString());
}
///
/// Return the wireless profiles state
///
///
public WirelessProfilesState GetWirelessProfilesState()
{
// Count the number of user and admin profiles instances
int AdminProfiles = 0, UserProfiles = 0;
IManagedReference wiFiEndpointSettingsRef = _wsmanConnection.NewReference("CIM_WiFiEndpointSettings");
IWsmanEnumeration wiFiEndpointSettingsCollection = wiFiEndpointSettingsRef.Enumerate("http://schemas.dmtf.org/wbem/wsman/1/wsman/SelectorFilter", null);
// No profiles exist.
if(!wiFiEndpointSettingsCollection.HasNext)
return WirelessProfilesState.None;
// Go over the instances and count the user and admin instances
foreach (IWsmanItem wiFiEndpointSettingsItem in wiFiEndpointSettingsCollection)
{
IManagedInstance wiFiEndpointSettingsObject = wiFiEndpointSettingsItem.Object;
// The difference between user profiles and admin profiles is that the user profiles
// don't have SSID property. (name is the only property the user profiles has)
string instanceID = wiFiEndpointSettingsObject.GetProperty("InstanceID").ToString();
if (instanceID.Contains(InstanceIDUser))
UserProfiles++;
else AdminProfiles++;
}
// No admin profiles - indicate there are only user profiles
if (AdminProfiles == 0)
return WirelessProfilesState.UserProfiles;
// No user profiles - indicate there are only admin profiles
return UserProfiles == 0 ? WirelessProfilesState.ITProfiles : WirelessProfilesState.BothITAndUserProfiles;
}
///
/// Polling the wireless properties
///
///
public void GetWirelessLinkState(Object stateinfo)
{
uint counter = 0;
while (true)
{
_changed = false;
if (connected)
{
_wirelessMutex.WaitOne();
try
{
_parent._manualResetEvent.WaitOne();
_ethernetPortService = _ethernetPortEPR.Get();
counter = (counter == 0) ? counter : counter--;
if (LinkControl != (LinkControl)Convert.ToUInt32(_ethernetPortService.GetProperty("LinkControl").ToString()))
{
LinkControl = (LinkControl)Convert.ToUInt32(_ethernetPortService.GetProperty("LinkControl").ToString());
_changed = true;
}
if (LinkPreference != (LinkControl)Convert.ToUInt32(_ethernetPortService.GetProperty("LinkPreference").ToString()))
{
LinkPreference = (LinkControl)Convert.ToUInt32(_ethernetPortService.GetProperty("LinkPreference").ToString());
_changed = true;
}
if (_changed)
{
if (WirelessLinkChangedEvent != null)
{
WirelessLinkChangedEvent();
}
}
}
catch (Exception)
{
if (++counter == 3)
{
LinkControl = LinkControl.None;
LinkPreference = LinkControl.None;
counter = 0;
}
}
_wirelessMutex.ReleaseMutex();
}
Thread.Sleep(80000);
}
}
internal bool CheckLinkProtectionSupport()
{
if (Utilities.CompareVersions(SupportLinkProtection, GetCoreVersion()) > 0)// version earlier than 8.1
return false;
uint linkProtectionLevel = GetLinkProtectionLevel();
if (linkProtectionLevel != 2)// Not Passive.
return false;
return true;
}
internal uint GetLinkProtectionLevel()
{
const string NOT_EXIST_EXCEPTION = "No route can be determined to reach the destination role defined by the WSAddressing To.";
const string ETHERNET_PORT_INSTANCE_ID = "Intel(r) AMT Ethernet Port Settings 1";
if (Utilities.CompareVersions(SupportLinkProtection, GetCoreVersion()) > 0)// version earlier than 8.1
return 1;// none
if(_ethernetPortEPR == null)
_ethernetPortEPR =
_wsmanConnection.NewReference("SELECT * FROM AMT_EthernetPortSettings WHERE InstanceID='" + ETHERNET_PORT_INSTANCE_ID + "'");
try
{
_ethernetPortService = _ethernetPortEPR.Get();
if (_ethernetPortService == null)
return 1;// Wired connection, return 1 (none).
IWsmanItem linkProtectionLevel = _ethernetPortService.GetProperty("WLANLinkProtectionLevel");
return uint.Parse(linkProtectionLevel.ToString());//override, none or passive
}
catch (Exception ex)
{
if(ex.Message.Contains(NOT_EXIST_EXCEPTION))
return 1;// Wired connection, return 1 (none).
throw;
}
}
internal uint RestoreLinkProtection()
{
_ethernetPortService = _ethernetPortEPR.CreateMethodInput("RestoreLinkProtection");
_ethernetPortService = _ethernetPortEPR.InvokeMethod(_ethernetPortService);
return Convert.ToUInt32(_ethernetPortService.GetProperty("ReturnValue").ToString());
}
#endregion
#region Private Methods
private string GetCoreVersion()
{
IManagedReference softwareIdentityRef =
_wsmanConnection.NewReference("SELECT * FROM CIM_SoftwareIdentity WHERE InstanceID='AMT FW Core Version'");
IManagedInstance softwareIdentityInstance = softwareIdentityRef.Get();
return softwareIdentityInstance.GetProperty("VersionString").ToString();
}
#endregion
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
UctOperation.OptInChangedEvent -= new StateChanged_D(this.OptInChangedEvent);
PowerOperation.PowerStateChangedEvent -= new PowerStateChanged_D(this.PowerStateChangedEvent);
WirelessOperation.WirelessLinkChangedEvent -= new WirelessLinkChanged_D(_wirelessOperation_WirelessLinkChangedEvent);
}
}
#endregion
}
}