protected byte[] SendAndReturnReport(InputReport returnReportType, TimeSpan timeout) { byte[] report = null; using (IReportInterceptor reportInterceptor = CreateReportInterceptor()) { SendReport(); DateTime start = DateTime.Now; while ((report = reportInterceptor.Intercept()) != null) { if (report[0] == (byte)returnReportType) { break; } if (DateTime.Now - start > timeout) { report = null; break; } } } if (report == null) { throw new TimeoutException("Could not retrieve result-report."); } return(report); }
public void ReadMemory(uint address, byte[] buffer, int offset, short count) { if (count + offset > buffer.Length) { throw new ArgumentException("The specified buffer cannot hold the requested amount of bytes.", "buffer"); } CreateReport(OutputReport.ReadMemory); // Write the address. OutputBuffer[1] = (byte)(((address & 0xff000000) >> 24)); OutputBuffer[2] = (byte)((address & 0x00ff0000) >> 16); OutputBuffer[3] = (byte)((address & 0x0000ff00) >> 8); OutputBuffer[4] = (byte)(address & 0x000000ff); // Write the byte-count. OutputBuffer[5] = (byte)((count & 0xff00) >> 8); OutputBuffer[6] = (byte)(count & 0x00ff); int i = 0; using (IReportInterceptor reportInterceptor = CreateReportInterceptor()) { SendReport(); byte[] report = null; // Since ReadMemory can have multiple response-reports we have to intercept // all ReadDataResult-reports that follow the ReadMemory-report. while ((report = reportInterceptor.Intercept()) != null) { if (report[0] == (byte)InputReport.ReadDataResult) { int size = (report[3] >> 4) + 1; // The data from ReadDataResult is added to the buffer. Array.Copy(report, 6, buffer, offset + i * 16, size); i++; if (size != 16 || i * 16 == count) { break; } byte errorCode = (byte)(report[3] & 0x0f); switch (errorCode) { case 0: break; case 8: throw new ArgumentException("The specified range to read contains non-existent addresses.", "address"); case 7: throw new ArgumentException("The specified range to read contains write-only registers.", "address"); default: throw new InvalidDataException("The wiimote returned an unknown errorcode."); } } } } }