comparison USBLib/Communication/UsbInterface.cs @ 21:dcfec2be27c9

Added USBLib
author Ivo Smits <Ivo@UCIS.nl>
date Mon, 15 Apr 2013 01:04:59 +0200
parents
children 6fcedb1030bf
comparison
equal deleted inserted replaced
20:c873e3dd73fe 21:dcfec2be27c9
1 using System;
2 using System.Text;
3 using UCIS.USBLib.Descriptor;
4
5 namespace UCIS.USBLib.Communication {
6 public abstract class UsbInterface : IUsbInterface {
7 public virtual byte Configuration {
8 get {
9 byte[] buf = new byte[1];
10 int tl = ControlRead(
11 UsbControlRequestType.EndpointIn | UsbControlRequestType.TypeStandard | UsbControlRequestType.RecipDevice,
12 (byte)UsbStandardRequest.GetConfiguration, 0, 0,
13 buf, 0, buf.Length);
14 if (tl != buf.Length) throw new Exception("Read failed");
15 return buf[0];
16 }
17 set {
18 throw new NotImplementedException();
19 }
20 }
21 public unsafe virtual string GetString(short langId, byte stringIndex) {
22 Byte[] buffer = new Byte[256];
23 int tl = GetDescriptor((byte)UsbDescriptorType.String, stringIndex, langId, buffer, 0, buffer.Length);
24 if (tl < 2) return null;
25 return UsbStringDescriptor.GetString(buffer, 0, tl);
26 }
27 public virtual int GetDescriptor(byte descriptorType, byte index, short langId, byte[] buffer, int offset, int length) {
28 return ControlRead(
29 UsbControlRequestType.EndpointIn | UsbControlRequestType.RecipDevice | UsbControlRequestType.TypeStandard,
30 (Byte)UsbStandardRequest.GetDescriptor,
31 (short)((descriptorType << 8) | index), langId, buffer, offset, length);
32 }
33 public virtual int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index) {
34 return ControlWrite(requestType, request, value, index, null, 0, 0);
35 }
36
37 public abstract void Close();
38
39 public abstract int BulkWrite(Byte endpoint, Byte[] buffer, int offset, int length);
40 public abstract int BulkRead(Byte endpoint, Byte[] buffer, int offset, int length);
41 public abstract int InterruptWrite(Byte endpoint, Byte[] buffer, int offset, int length);
42 public abstract int InterruptRead(Byte endpoint, Byte[] buffer, int offset, int length);
43 public abstract int ControlWrite(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
44 public abstract int ControlRead(UsbControlRequestType requestType, byte request, short value, short index, byte[] buffer, int offset, int length);
45
46 public UsbPipeStream GetBulkStream(Byte endpoint) {
47 return new UsbPipeStream(this, endpoint, false);
48 }
49 public UsbPipeStream GetInterruptStream(Byte endpoint) {
50 return new UsbPipeStream(this, endpoint, true);
51 }
52
53 public void Dispose() {
54 Close();
55 GC.SuppressFinalize(this);
56 }
57 ~UsbInterface() {
58 Close();
59 }
60 }
61 }