315 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intel.Management.Wsman;
using System.Collections.ObjectModel;
namespace Intel.Management.Module
{
public class StorageItem : DriveContainer
{
public class ApplicationContainer : DriveContainer
{
StorageItem _storageItem;
string _handle;
Dictionary<string, object> _props;
public ApplicationContainer(string handle,StorageItem item) : base("RegisteredApps")
{
_storageItem = item;
_handle = handle;
_props = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
_props.Add("VendorName", null);
_props.Add("ApplicationName", null);
//_blocks = new BlockContainer(item, handle);
//_items = new DriveContainer[1];
//tems[0] = _blocks;
}
public override object GetProperty( Collection<string> propList, bool force)
{
if (force)
{
IManagedInstance outObj = _storageItem.GetApplicationAttributes(_handle);
_props["VendorName"] = outObj.GetProperty("VendorName").ToString();
_props["ApplicationName"] = outObj.GetProperty("ApplicationName").ToString();
}
return GetProperties(_props, propList);
}
}
public class BlockItem : DriveItem
{
StorageItem _storageItem;
public BlockItem(String handle, StorageItem item)
: base(handle, "DataBlock")
{
_storageItem = item;
}
}
public class AppDataContainer : DriveContainer
{
StorageItem _storageItem;
string _owner;
string[] _properties = { "DefaultAction" };
public AppDataContainer(StorageItem item, string owner)
: base("AppData")
{
_storageItem = item;
_items = null;
_owner = owner;
}
public override string[] Properties
{
get { return _properties; }
}
public override object GetProperty(Collection<string> propList, bool force)
{
foreach (string propName in propList)
{
if (string.Compare(propName, _properties[0], true) != 0)
throw new ArgumentException();
}
return "Add Data Block";
}
public override DriveItem[] GetChildItems(bool force)
{
if (_items == null || force)
{
IManagedInstance outObj = _storageItem.GetAllocatedBlocks(_owner);
List<DriveItem> list = new List<DriveItem>();
IWsmanItem handles = outObj.GetProperty("BlockHandles");
if (handles != null)
{
foreach (IWsmanItem handle in handles)
{
list.Add(new BlockItem(handle.ToString(), _storageItem));
}
}
_items = list.ToArray();
}
return _items;
}
public override void Invoke(bool force)
{
// Allocate a block
//public uint32 AllocateBlock([IN]string SessionHandle[], [IN]uint32 BytesRequested, [IN]boolean BlockHidden, [IN]string BlockName, [OUT]uint32 BlockHandle)
base.Invoke(force);
}
public override object InvokeDefaultActionDynamicParameters()
{
return base.InvokeDefaultActionDynamicParameters();
}
}
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);
}
}
}
Dictionary<string, object> _props;
IManagedReference _storageRef;
private string _sessionHandle;
private string _appHandle;
string _appName;
string _vendorName;
string _entName;
string _guid;
public StorageItem(AmtDriveInfo info) : base("Storage")
{
//these will get passed in from new-drive
_appName = "foo";
_vendorName = "Microsoft";
_guid = Guid.Empty.ToString();
_entName = "Intel";
//define Admin storage
_props = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
_props.Add("Mtu", null);
_props.Add("WriteEraseLimit", null);
_props.Add("RegistrationTimeout", null);
_props.Add("LockTimeout", null);
_props.Add("BytesAvailable", null);
//Storage (StoragePropeties)
//RegisteredApplications
// 1 (application properties)
// Blocks (TotalSize)
// 1 (block properites)
// 2
//CurrentApplication (applicaton properties)
// Bocks
// 1 (block properties)
// 2
// Permissions
// 1 (Permission)bock properties)
//1 (Groups) Name, read | ReadWrite
//1 (Members) AllApps and Vendors
//
//2 (
// Block (properties)
// BlockSize, BlockHidden, BlockName
DriveItem[] storageItems = {
//new EnterpriseContainer(this),
//new ApplicationContainer(this),
};
_items = storageItems;
_storageRef = info.WsmanConnection.NewReference("AMT_ThirdPartyDataStorageService");
_sessionHandle = RegisterApplication();
_appHandle = GetCurrentApplicationHandle();
}
private uint GetMtu()
{
IManagedInstance inputObj = _storageRef.CreateMethodInput("GetMTU");
IManagedInstance outObj = _storageRef.InvokeMethod(inputObj);
StorageOperationException.CheckResult(outObj);
return uint.Parse( outObj.GetProperty("Mtu").ToString() );
}
string RegisterApplication()
{
Guid g = Guid.Empty;
string hex = BitConverter.ToString(g.ToByteArray());
string g2= hex.Replace("-", "");
IManagedInstance inputObj = _storageRef.CreateMethodInput("RegisterApplication");
inputObj.SetProperty("CallerUUID",g2 );
inputObj.SetProperty("VendorName", _vendorName);
inputObj.SetProperty("ApplicationName", _appName);
inputObj.SetProperty("EnterpriseName", _entName);
IManagedInstance outObj = _storageRef.InvokeMethod(inputObj);
StorageOperationException.CheckResult(outObj);
return outObj.GetProperty("SessionHandle").ToString();
}
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 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 string GetCurrentApplicationHandle()
{
IManagedInstance inputObj = _storageRef.CreateMethodInput("GetCurrentApplicationHandle");
inputObj.SetProperty("SessionHandle",_sessionHandle);
IManagedInstance outObj = _storageRef.InvokeMethod(inputObj);
StorageOperationException.CheckResult(outObj);
return outObj.GetProperty("ApplicationHandle").ToString();
}
private IManagedInstance GetApplicationAttributes(string handle)
{
IManagedInstance inputObj = _storageRef.CreateMethodInput("GetApplicationAttributes");
inputObj.SetProperty("SessionHandle", _sessionHandle);
inputObj.SetProperty("ApplicationBeingRequested", handle);
IManagedInstance outObj = _storageRef.InvokeMethod(inputObj);
StorageOperationException.CheckResult(outObj);
return outObj;
}
public override object GetProperty(Collection<string> propList, bool force)
{
_props["Mtu"] = GetMtu();
_props["WriteEraseLimit"] = GetWriteEraseLimit();
uint regTimeout, lockTimeout;
GetTimeoutValues(out regTimeout,out lockTimeout);
_props["RegistrationTimeout"] = regTimeout;
_props["LockTimeout"]=lockTimeout;
_props["BytesAvailable"]=GetBytesAvailable();
return GetProperties(_props, propList);
}
}
}