using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; using System.Management.Automation; using System.Management.Automation.Provider; using Intel.Management.Wsman; namespace Intel.Management.Module.Amt { class StorageManager { class RegistrationsContainer : DriveContainer { StorageManager _stgManager; public RegistrationsContainer(StorageManager stgManager) : base("Registrations") { _stgManager = stgManager; _items = null; } public override ManagedItem[] GetChildItems(bool force) { if (_items == null) { List list = new List(); IManagedInstance outObj=_stgManager.GetRegisteredApplications(); //_stgManager.get //list.Add(new AppDataContainer(_stgManager)); //list.Add(new EnterpriseContainer(_stgAdmin)); //list.Add(new PartnerContainer(_stgAdmin)); //list.Add(new RegistrationsContainer(_stgAdmin)); list.Sort(_defaultComparer); _items = list.ToArray(); } return _items; } } class BlockItem : DriveItem { StorageManager _stgManager; Dictionary _props; public BlockItem( StorageManager stgManager,string handle) : base(handle, ManagedValueType.Resource) { _stgManager = stgManager; IManagedInstance outObj= _stgManager.GetBlockAttributes(handle); _name=outObj.GetProperty("BlockHidden").ToString(); _props = new Dictionary(StringComparer.OrdinalIgnoreCase); _props.Add("BlockSize", uint.Parse( outObj.GetProperty("BlockSize").ToString() )); _props.Add("BlockHidden", bool.Parse(outObj.GetProperty("BlockHidden").ToString())); _props.Add("BlockHandle", uint.Parse(handle)); _name = outObj.GetProperty("BlockName").ToString(); } public override string[] Properties { get { return _props.Keys.ToArray(); } } public override object GetProperty(Collection propList, bool force) { return GetProperties(_props, propList); } } class BlockContainer : DriveContainer { StorageManager _stgManager; uint _owner; public BlockContainer(StorageManager stgManager,uint owner) : base("Blocks") { _stgManager = stgManager; _owner = owner; _items = null; } public override ManagedItem[] GetChildItems(bool force) { if (_items == null || force) { List list = new List(); IManagedInstance outObj = _stgManager.GetAllocatedBlocks(_owner.ToString()); IWsmanItem handles = outObj.GetProperty("BlockHandles"); if (handles != null) { foreach (IWsmanItem handle in handles) { list.Add(new BlockItem(_stgManager, handle.ToString())); } } list.Sort(_defaultComparer); _items = list.ToArray(); } return _items; } } public class StorageWriter : IContentWriter { public void Dispose() { } public void Close() { } public void Seek(long offset, System.IO.SeekOrigin origin) { //Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding. } public System.Collections.IList Write(System.Collections.IList content) { object b=content[0]; if (b is PSObject) { PSObject bb = b as PSObject; //bb. bb.ToString(); } //content. return content; } } class SessionContainer : DriveContainer { StorageManager _stgManager; static string[] _propKeys = { "BytesAvailable", "SessionHandle", "ApplicationHandle" , "VendorName", "ApplicationName", }; public SessionContainer(StorageManager stgManager, string name) : base(name) { _stgManager = stgManager; _items = null; } public override string[] Properties { get { return _propKeys; } } public override ManagedItem[] GetChildItems(bool force) { if (_items == null) { List list = new List(); list.Add(new BlockContainer(_stgManager,_stgManager.ApplicationHandle)); list.Add(new RegistrationsContainer(_stgManager)); list.Sort(_defaultComparer); _items = list.ToArray(); } return _items; } public override object GetProperty(Collection propList, bool force) { Dictionary props = new Dictionary(StringComparer.OrdinalIgnoreCase); props.Add("SessionHandle",_stgManager.SessionHandle); props.Add("ApplicationHandle",_stgManager.ApplicationHandle); if (HasProperty(propList,"BytesAvailable")) props.Add("BytesAvailable",_stgManager.GetBytesAvailable()); if (HasProperty(propList, "VendorName") || HasProperty(propList, "ApplicationName")) { IManagedInstance outObj = _stgManager.GetApplicationAttributes(); props.Add("VendorName", outObj.GetProperty("VendorName").ToString()); props.Add("ApplicationName", outObj.GetProperty("ApplicationName").ToString()); } return GetProperties(props, propList); } public override void RemoveItem(object dynamicParams, bool force) { _stgManager.UnregisterApplication(); _stgManager.RemoveItem(this); } public override IContentWriter GetContentWriter(object dynamicParams, bool force) { return new StorageWriter(); } /* public override g { return null; } public IContentWriter GetContentWriter(string path) { return null; }*/ //public override set } class SetContentDynamicParameters { public SetContentDynamicParameters() { _encoding = Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding.Unknown; } [Parameter(Mandatory = false, HelpMessage = "Encoding type of the content")] public Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding Encoding { get { return _encoding; } set { _encoding = value; } } [Parameter(Mandatory = false, HelpMessage = "Encoding type of the content")] public uint BlockHandle { get { return _blockHandle; } set { _blockHandle = value; } } [Parameter(Mandatory = false, HelpMessage = "Encoding type of the content")] public uint AllocationSize { get { return _allocationSize; } set { _allocationSize = value; } } Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding _encoding; uint _blockHandle; uint _allocationSize; } class SessionDynamicParameters { public SessionDynamicParameters() { _uuid=Guid.Empty; } [Parameter(Mandatory = true, Position = 0, HelpMessage = "Vendor Name of the Storage ACL")] public string VendorName { get { return _vendorName;} set {_vendorName=value; } } [Parameter(Mandatory = true, Position = 1, HelpMessage = "Application Name of the Storage ACL")] public string ApplicationName { get { return _appName;} set { _appName=value;} } [Parameter(Mandatory = true, Position = 2, HelpMessage = "Enterprise Storage ACL")] public string EnterpriseName { get { return _entName;} set {_entName=value;} } [Parameter(Mandatory = false, Position = 3, HelpMessage = "UUID of the comupter accessing storage")] public Guid UUID { get {return _uuid;} set{_uuid=value;} } string _appName; string _vendorName; string _entName; Guid _uuid; } class StorageRoot : DriveContainer { StorageManager _stgManager; static string[] _propNames = { "Mtu", "WriteEraseLimit", "RegistrationTimeout", "LockTimeout" }; public StorageRoot(StorageManager stgManager) : base("Third Party Data Storage") { _stgManager = stgManager; } public override string[] Properties { get { return _propNames; } } public override object GetProperty(Collection propList, bool force) { Dictionary props = new Dictionary(StringComparer.OrdinalIgnoreCase); props.Add("Mtu", _stgManager.Mtu); props.Add("WriteEraseLimit",_stgManager.GetWriteEraseLimit()); uint regTimeout, lockTimeout; _stgManager.GetTimeoutValues(out regTimeout, out lockTimeout); props.Add("RegistrationTimeout",regTimeout); props.Add("LockTimeout", lockTimeout); return GetProperties(props, propList); } public override ManagedItem NewItem(string name, string type, object newItem, object dynamicParams, bool force) { SessionDynamicParameters sessionParams = dynamicParams as SessionDynamicParameters; StorageManager stgManager = new StorageManager(_stgManager); stgManager.RegisterApplication(sessionParams.VendorName, sessionParams.ApplicationName, sessionParams.EnterpriseName, sessionParams.UUID); List list = new List(_items); ManagedItem itemToAdd = new SessionContainer(stgManager,name); list.Add(itemToAdd); _items = list.ToArray(); return (DriveItem)itemToAdd; } public override object NewItemDynamicParameters(string itemTypeName, object newItemValue) { return new SessionDynamicParameters(); } } public class StorageOperationException : Exception { public StorageOperationException(string message) : base(message) { } public static void CheckResult(IManagedInstance resultObj) { string returnValue = resultObj.GetProperty("ReturnValue").ToString(); if (!returnValue.Equals("0")) { throw new StorageOperationException(resultObj.SimpleName + ".ReturnValue=" + returnValue); } } } // private data IManagedReference _storageRef; string _sessionHandle; uint _appHandle; uint _mtu; DriveContainer _root; public StorageManager(WsmanConnection conn) { _storageRef = conn.NewReference("AMT_ThirdPartyDataStorageService"); _root = new StorageRoot(this); } // copy constuctor public StorageManager(StorageManager stgManager) { _storageRef = stgManager._storageRef; _appHandle = stgManager._appHandle; _sessionHandle = stgManager.SessionHandle; _root = stgManager._root; _mtu = stgManager._mtu; } public ManagedItem RootFolder { get { return _root; } } private bool RemoveItem(DriveItem item) { return _root.RemoveChildItem(item, true); } private uint Mtu { get { if (_mtu == 0) _mtu = GetMtu(); return Mtu; } } private string SessionHandle { get { return _sessionHandle; } set { _sessionHandle = value; } } private uint ApplicationHandle { get { return _appHandle; } set {_appHandle = value; } } private uint GetMtu() { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetMTU"); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return uint.Parse( outObj.GetProperty("Mtu").ToString() ); } void RegisterApplication(string appName, string vendorName,string entName, Guid uuid) { string uuidString = BitConverter.ToString(uuid.ToByteArray()); uuidString = uuidString.Replace("-", ""); IManagedInstance inputObj = _storageRef.CreateMethodInput("RegisterApplication"); inputObj.SetProperty("CallerUUID",uuidString ); inputObj.SetProperty("VendorName", vendorName); inputObj.SetProperty("ApplicationName", appName); inputObj.SetProperty("EnterpriseName", entName); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); _sessionHandle= outObj.GetProperty("SessionHandle").ToString(); GetCurrentApplicationHandle(); } void UnregisterApplication() { IManagedInstance inputObj = _storageRef.CreateMethodInput("UnregisterApplication"); inputObj.SetProperty("SessionHandle", _sessionHandle); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); } private uint GetWriteEraseLimit() { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetWriteEraseLimit"); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return uint.Parse(outObj.GetProperty("WriteEraseLimit").ToString()); } private void GetTimeoutValues(out uint regTimeout, out uint lockTimeout) { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetTimeoutValues"); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); regTimeout = uint.Parse(outObj.GetProperty("RegistrationTimeout").ToString()); lockTimeout = uint.Parse(outObj.GetProperty("LockTimeout").ToString()); } private uint GetBytesAvailable() { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetBytesAvailable"); inputObj.SetProperty("SessionHandle", _sessionHandle); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return uint.Parse(outObj.GetProperty("BytesAvailable").ToString()); } private IManagedInstance AllocateBlock(uint size,bool hidden,string name) { IManagedInstance inputObj = _storageRef.CreateMethodInput("AllocateBlock"); inputObj.SetProperty("SessionHandle", _sessionHandle); inputObj.SetProperty("BytesRequested", size.ToString()); inputObj.SetProperty("BlockHidden", hidden.ToString().ToLower()); //inputObj.SetProperty("BlockName", name); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetAllocatedBlocks(string owner) { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetAllocatedBlocks"); inputObj.SetProperty("SessionHandle",_sessionHandle); inputObj.SetProperty("BlockOwnerApplication", owner); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetBlockAttributes(string handle) { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetBlockAttributes"); inputObj.SetProperty("SessionHandle", _sessionHandle); inputObj.SetProperty("BlockHandle", handle); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return outObj; } private uint WriteBlock(string handle,uint offset,byte[] data) { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetBlockAttributes"); inputObj.SetProperty("SessionHandle", _sessionHandle); inputObj.SetProperty("BlockHandle", handle); inputObj.SetProperty("ByteOffset",offset.ToString()); inputObj.SetProperty("Data", Convert.ToBase64String(data)); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return uint.Parse(outObj.GetProperty("BytesWritten").ToString()); } private uint GetCurrentApplicationHandle() { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetCurrentApplicationHandle"); inputObj.SetProperty("SessionHandle",_sessionHandle); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return _appHandle= uint.Parse(outObj.GetProperty("ApplicationHandle").ToString()); } private IManagedInstance GetRegisteredApplications() { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetRegisteredApplications"); inputObj.SetProperty("SessionHandle", _sessionHandle); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return outObj; } private IManagedInstance GetApplicationAttributes() { IManagedInstance inputObj = _storageRef.CreateMethodInput("GetApplicationAttributes"); inputObj.SetProperty("SessionHandle", _sessionHandle); inputObj.SetProperty("ApplicationBeingRequested", _appHandle.ToString()); IManagedInstance outObj = _storageRef.InvokeMethod(inputObj); StorageOperationException.CheckResult(outObj); return outObj; } } }