21
|
1 ???using System; |
|
2 using System.IO; |
|
3 |
|
4 namespace UCIS.USBLib.Communication { |
|
5 public class UsbPipeStream : Stream { |
|
6 public IUsbInterface Device { get; private set; } |
|
7 public Byte Endpoint { get; private set; } |
|
8 public Boolean InterruptEndpoint { get; private set; } |
|
9 |
|
10 public UsbPipeStream(IUsbInterface device, Byte endpoint, Boolean interrupt) { |
|
11 this.Device = device; |
|
12 this.Endpoint = endpoint; |
|
13 this.InterruptEndpoint = interrupt; |
|
14 } |
|
15 |
|
16 public override bool CanRead { |
|
17 get { return (Endpoint & 0x80) != 0; } |
|
18 } |
|
19 |
|
20 public override bool CanSeek { |
|
21 get { return false; } |
|
22 } |
|
23 |
|
24 public override bool CanWrite { |
|
25 get { return (Endpoint & 0x80) == 0; } |
|
26 } |
|
27 |
|
28 public override void Flush() { |
|
29 } |
|
30 |
|
31 public override long Length { get { return 0; } } |
|
32 |
|
33 public override long Position { |
|
34 get { return 0; } |
|
35 set { throw new NotImplementedException(); } |
|
36 } |
|
37 |
|
38 public override int Read(byte[] buffer, int offset, int count) { |
|
39 if (InterruptEndpoint) { |
|
40 return Device.InterruptRead(Endpoint, buffer, offset, count); |
|
41 } else { |
|
42 return Device.BulkRead(Endpoint, buffer, offset, count); |
|
43 } |
|
44 } |
|
45 |
|
46 public override long Seek(long offset, SeekOrigin origin) { |
|
47 throw new NotImplementedException(); |
|
48 } |
|
49 |
|
50 public override void SetLength(long value) { |
|
51 throw new NotImplementedException(); |
|
52 } |
|
53 |
|
54 public override void Write(byte[] buffer, int offset, int count) { |
|
55 int written; |
|
56 if (InterruptEndpoint) { |
|
57 written = Device.InterruptWrite(Endpoint, buffer, offset, count); |
|
58 } else { |
|
59 written = Device.BulkWrite(Endpoint, buffer, offset, count); |
|
60 } |
|
61 if (written != count) throw new EndOfStreamException("Could not write all data"); |
|
62 } |
|
63 } |
|
64 } |