Mercurial > hg > ucis.core
comparison USBLib/Communication/UsbPipeStream.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 | dcfec2be27c9 |
children |
comparison
equal
deleted
inserted
replaced
66:b7bc27c6734e | 67:2d16447eff12 |
---|---|
3 | 3 |
4 namespace UCIS.USBLib.Communication { | 4 namespace UCIS.USBLib.Communication { |
5 public class UsbPipeStream : Stream { | 5 public class UsbPipeStream : Stream { |
6 public IUsbInterface Device { get; private set; } | 6 public IUsbInterface Device { get; private set; } |
7 public Byte Endpoint { get; private set; } | 7 public Byte Endpoint { get; private set; } |
8 public Boolean InterruptEndpoint { get; private set; } | |
9 | 8 |
10 public UsbPipeStream(IUsbInterface device, Byte endpoint, Boolean interrupt) { | 9 public UsbPipeStream(IUsbInterface device, Byte endpoint) { |
11 this.Device = device; | 10 this.Device = device; |
12 this.Endpoint = endpoint; | 11 this.Endpoint = endpoint; |
13 this.InterruptEndpoint = interrupt; | |
14 } | 12 } |
15 | 13 |
16 public override bool CanRead { | 14 public override bool CanRead { |
17 get { return (Endpoint & 0x80) != 0; } | 15 get { return (Endpoint & 0x80) != 0; } |
18 } | 16 } |
33 public override long Position { | 31 public override long Position { |
34 get { return 0; } | 32 get { return 0; } |
35 set { throw new NotImplementedException(); } | 33 set { throw new NotImplementedException(); } |
36 } | 34 } |
37 | 35 |
36 public void Abort() { | |
37 Device.PipeAbort(Endpoint); | |
38 } | |
39 | |
40 public void ClearHalt() { | |
41 Device.PipeReset(Endpoint); | |
42 } | |
43 | |
38 public override int Read(byte[] buffer, int offset, int count) { | 44 public override int Read(byte[] buffer, int offset, int count) { |
39 if (InterruptEndpoint) { | 45 if (!CanRead) throw new InvalidOperationException("Can not read from an output endpoint"); |
40 return Device.InterruptRead(Endpoint, buffer, offset, count); | 46 return Device.PipeTransfer(Endpoint, buffer, offset, count); |
41 } else { | |
42 return Device.BulkRead(Endpoint, buffer, offset, count); | |
43 } | |
44 } | 47 } |
45 | 48 |
46 public override long Seek(long offset, SeekOrigin origin) { | 49 public override long Seek(long offset, SeekOrigin origin) { |
47 throw new NotImplementedException(); | 50 throw new NotImplementedException(); |
48 } | 51 } |
50 public override void SetLength(long value) { | 53 public override void SetLength(long value) { |
51 throw new NotImplementedException(); | 54 throw new NotImplementedException(); |
52 } | 55 } |
53 | 56 |
54 public override void Write(byte[] buffer, int offset, int count) { | 57 public override void Write(byte[] buffer, int offset, int count) { |
55 int written; | 58 if (!CanWrite) throw new InvalidOperationException("Can not write to an input endpoint"); |
56 if (InterruptEndpoint) { | 59 int written = Device.PipeTransfer(Endpoint, buffer, offset, count); |
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"); | 60 if (written != count) throw new EndOfStreamException("Could not write all data"); |
61 } | |
62 | |
63 protected override void Dispose(bool disposing) { | |
64 if (disposing) try { Abort(); } catch { } | |
65 base.Dispose(disposing); | |
62 } | 66 } |
63 } | 67 } |
64 } | 68 } |