comparison USBLib/Communication/UsbInterface.cs @ 67:2d16447eff12

Simplified USB communication code, added functions to abort pipe transfers
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 16 Oct 2013 01:11:49 +0200
parents abe0d55a2201
children
comparison
equal deleted inserted replaced
66:b7bc27c6734e 67:2d16447eff12
5 namespace UCIS.USBLib.Communication { 5 namespace UCIS.USBLib.Communication {
6 public abstract class UsbInterface : IUsbInterface { 6 public abstract class UsbInterface : IUsbInterface {
7 public virtual byte Configuration { 7 public virtual byte Configuration {
8 get { 8 get {
9 byte[] buf = new byte[1]; 9 byte[] buf = new byte[1];
10 int tl = ControlRead( 10 int tl = ControlTransfer(
11 UsbControlRequestType.EndpointIn | UsbControlRequestType.TypeStandard | UsbControlRequestType.RecipDevice, 11 UsbControlRequestType.EndpointIn | UsbControlRequestType.TypeStandard | UsbControlRequestType.RecipDevice,
12 (byte)UsbStandardRequest.GetConfiguration, 0, 0, 12 (byte)UsbStandardRequest.GetConfiguration, 0, 0,
13 buf, 0, buf.Length); 13 buf, 0, buf.Length);
14 if (tl != buf.Length) throw new Exception("Read failed"); 14 if (tl != buf.Length) throw new Exception("Read failed");
15 return buf[0]; 15 return buf[0];
17 set { 17 set {
18 throw new NotImplementedException(); 18 throw new NotImplementedException();
19 } 19 }
20 } 20 }
21 public virtual int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) { 21 public virtual int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) {
22 return ControlRead( 22 return ControlTransfer(
23 UsbControlRequestType.EndpointIn | UsbControlRequestType.RecipDevice | UsbControlRequestType.TypeStandard, 23 UsbControlRequestType.EndpointIn | UsbControlRequestType.RecipDevice | UsbControlRequestType.TypeStandard,
24 (Byte)UsbStandardRequest.GetDescriptor, 24 (Byte)UsbStandardRequest.GetDescriptor,
25 (short)((descriptorType << 8) | index), langId, buffer, offset, length); 25 (short)((descriptorType << 8) | index), langId, buffer, offset, length);
26 } 26 }
27 public virtual int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index) { 27 public virtual int ControlTransfer(UsbControlRequestType requestType, byte request, short value, short index) {
28 return ControlWrite(requestType, request, value, index, null, 0, 0); 28 return ControlTransfer(requestType, request, value, index, null, 0, 0);
29 } 29 }
30 30
31 public abstract void Close(); 31 public abstract void Close();
32 32
33 public abstract int BulkWrite(Byte endpoint, Byte[] buffer, int offset, int length); 33 public abstract int PipeTransfer(Byte endpoint, Byte[] buffer, int offset, int length);
34 public abstract int BulkRead(Byte endpoint, Byte[] buffer, int offset, int length); 34 public virtual void PipeReset(Byte endpoint) { throw new NotImplementedException(); }
35 public virtual void BulkReset(Byte endpoint) { throw new NotImplementedException(); } 35 public virtual void PipeAbort(Byte endpoint) { throw new NotImplementedException(); }
36 public abstract int InterruptWrite(Byte endpoint, Byte[] buffer, int offset, int length); 36 public abstract int ControlTransfer(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
37 public abstract int InterruptRead(Byte endpoint, Byte[] buffer, int offset, int length);
38 public virtual void InterruptReset(Byte endpoint) { throw new NotImplementedException(); }
39 public abstract int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
40 public abstract int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
41 37
42 public virtual UsbPipeStream GetBulkStream(Byte endpoint) { 38 public virtual UsbPipeStream GetPipeStream(Byte endpoint) {
43 return new UsbPipeStream(this, endpoint, false); 39 return new UsbPipeStream(this, endpoint);
44 }
45 public virtual UsbPipeStream GetInterruptStream(Byte endpoint) {
46 return new UsbPipeStream(this, endpoint, true);
47 } 40 }
48 41
49 public void Dispose() { 42 public void Dispose() {
50 Close(); 43 Close();
51 GC.SuppressFinalize(this); 44 GC.SuppressFinalize(this);