internal static IpmiResponse ConvertResponse(byte[] data, Type responseType) { // Create the response based on the provided type. ConstructorInfo constructorInfo = responseType.GetConstructor(Type.EmptyTypes); IpmiResponse ipmiResponse = (IpmiResponse)constructorInfo.Invoke(new Object[0]); foreach (PropertyInfo propertyInfo in responseType.GetProperties()) { IpmiMessageDataAttribute[] attributes = (IpmiMessageDataAttribute[])propertyInfo.GetCustomAttributes(typeof(IpmiMessageDataAttribute), true); if (attributes.Length > 0) { if (propertyInfo.PropertyType == typeof(byte)) { if (attributes[0].Offset < data.Length) { propertyInfo.SetValue(ipmiResponse, data[attributes[0].Offset], null); } } else if (propertyInfo.PropertyType == typeof(ushort)) { propertyInfo.SetValue(ipmiResponse, BitConverter.ToUInt16(data, attributes[0].Offset), null); } else if (propertyInfo.PropertyType == typeof(uint)) { propertyInfo.SetValue(ipmiResponse, BitConverter.ToUInt32(data, attributes[0].Offset), null); } else if (propertyInfo.PropertyType == typeof(byte[])) { int propertyLength = attributes[0].Length; if (propertyLength == 0) { propertyLength = data.Length - attributes[0].Offset; } if (attributes[0].Offset < data.Length) { byte[] propertyData = new byte[propertyLength]; Buffer.BlockCopy(data, attributes[0].Offset, propertyData, 0, propertyData.Length); propertyInfo.SetValue(ipmiResponse, propertyData, null); } } else { // May need to add other types. Debug.Assert(false); } } } return(ipmiResponse); }
/// <summary> /// Send Receive Ipmi messages /// </summary> internal override IpmiResponse IpmiSendReceive(IpmiRequest ipmiRequest, Type responseType, bool allowRetry = true) { byte[] message = ipmiRequest.GetBytes(IpmiTransport.Wmi, 0x00); // Create the response based on the provided type ConstructorInfo constructorInfo = responseType.GetConstructor(Type.EmptyTypes); IpmiResponse ipmiResponse = (IpmiResponse)constructorInfo.Invoke(new Object[0]); // Serialize the IPMI request into bytes. ManagementBaseObject ipmiRequestMessage = this.GetManagementObject(ipmiRequest.ToString(), message); // invoke new method options InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.FromMilliseconds(base.Timeout)); // management return object ManagementBaseObject ipmiResponseMessage = null; // get instance and invoke RequestResponse method foreach (ManagementObject mo in ipmi_Instance) { ipmiResponseMessage = mo.InvokeMethod(ipmi_Method, wmiPacket, methodOptions); } if (ipmiResponseMessage == null) { // Assume the request timed out. ipmiResponse.CompletionCode = 0xA3; } else { ipmiResponse.CompletionCode = (byte)ipmiResponseMessage["CompletionCode"]; if (ipmiResponse.CompletionCode == 0) { try { uint dataLenght = (uint)ipmiResponseMessage["ResponseDataSize"]; // expected to be true, as ResponseDataSize includes completionCode if (dataLenght != 0) { // extract response data array byte[] responseData = (byte[])ipmiResponseMessage["ResponseData"]; // extract response message lenght if (responseData != null) { int lenght = responseData.Length; if (this.debugEnabled) { string cmd = ipmiRequest.GetType().ToString(); IpmiSharedFunc.WriteTrace(string.Format("Command: {0} Request: {1}", cmd, IpmiSharedFunc.ByteArrayToHexString(message))); if (responseData != null) { IpmiSharedFunc.WriteTrace(string.Format("Command: {0} Response: {1}", cmd, IpmiSharedFunc.ByteArrayToHexString(responseData))); } else { IpmiSharedFunc.WriteTrace(string.Format("Request: {0} Response: null", cmd)); } } // initialize the response to set the paramaters. ipmiResponse.Initialize(IpmiTransport.Wmi, responseData, lenght, 0x00); ipmiResponseMessage = null; } else { // IpmiCannotReturnRequestedDataBytes, data lenght is greater than zero // but responseData is null. ipmiResponse.CompletionCode = 0xCA; if (this.debugEnabled) { IpmiSharedFunc.WriteTrace(string.Format("Response Lenght: {0} Response: null. Asserting 0xCA CompletionCode ", dataLenght)); } } } else { // Asserting IpmiResponseNotProvided ipmiResponse.CompletionCode = 0xCE; if (this.debugEnabled) { IpmiSharedFunc.WriteTrace(string.Format("Unable to obtain Response Data Lenght: {0} Response: null. Asserting 0xCE CompletionCode ", dataLenght)); } } } catch (Exception ex) { // Response data Invalid, data convertion failed. // unexpected error, return: ipmiResponse.CompletionCode = 0xAD; if (this.debugEnabled) { IpmiSharedFunc.WriteTrace(string.Format("Exception Source: {0} Message{1}", ex.Source.ToString(), ex.Message.ToString())); } } } else { if (this.debugEnabled) { // throw ipmi/dcmi response exception with a custom string message and the ipmi completion code IpmiSharedFunc.WriteTrace(string.Format("Completion Code: " + IpmiSharedFunc.ByteToHexString(ipmiResponse.CompletionCode))); if (ipmiResponseMessage == null) { IpmiSharedFunc.WriteTrace(string.Format("Request Type: {0} Response Packet: null Completion Code {1}", ipmiRequest.GetType().ToString(), IpmiSharedFunc.ByteToHexString(ipmiResponse.CompletionCode))); } } } } // Response to the IPMI request message. return(ipmiResponse); }
/// <summary> /// Send Receive Ipmi messages /// </summary> /// <param name="request"></param> /// <param name="responseType"></param> /// <returns></returns> internal override IpmiResponse IpmiSendReceive(IpmiRequest ipmiRequest, Type responseType, bool allowRetry = true) { byte[] message = ipmiRequest.GetBytes(IpmiTransport.Wmi, 0x00); // Serialize the IPMI request into bytes. ManagementBaseObject ipmiRequestMessage = this.GetManagementObject(message); // invoke new method options InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.FromMilliseconds(base.Timeout)); // management return object ManagementBaseObject ipmiResponseMessage = null; // get instance and invoke RequestResponse method foreach (ManagementObject mo in _ipmi_Instance) { ipmiResponseMessage = mo.InvokeMethod(_ipmi_Method, wmiPacket, methodOptions); } if (ipmiResponseMessage == null) { // throw ipmi/dcmi response exception with a custom string message and the ipmi completion code throw new IpmiResponseException(); } // ipmi response completion code byte completionCode = (byte)ipmiResponseMessage["CompletionCode"]; // Create the response based on the provided type and message bytes. ConstructorInfo constructorInfo = responseType.GetConstructor(Type.EmptyTypes); IpmiResponse ipmiResponse = (IpmiResponse)constructorInfo.Invoke(new Object[0]); if (completionCode == 0) { try { // extract response data array byte[] responseData = (byte[])ipmiResponseMessage["ResponseData"]; // extract response message lenght int lenght = (int)ipmiResponseMessage["ResponseDataSize"]; // initialize the response to set the paramaters. ipmiResponse.Initialize(IpmiTransport.Wmi, responseData, lenght, 0x00); ipmiResponseMessage = null; } catch (Exception ex) { Debug.WriteLine("Exception Source: {0} Message{1}", ex.Source.ToString(), ex.Message.ToString()); } } else { // throw ipmi/dcmi response exception with a custom string message and the ipmi completion code Debug.WriteLine("Completion Code: " + IpmiSharedFunc.ByteToHexString(ipmiResponse.CompletionCode)); if (ipmiResponseMessage == null) { Debug.WriteLine("Request Type: {0} Response Packet: {1} Completion Code {2}", ipmiRequest.GetType().ToString(), IpmiSharedFunc.ByteArrayToHexString((byte[])ipmiResponseMessage["ResponseData"]), IpmiSharedFunc.ByteToHexString(ipmiResponse.CompletionCode)); } } // Response to the IPMI request message. return(ipmiResponse); }