| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef DEVICE_HID_HID_CONNECTION_H_ | 5 #ifndef DEVICE_HID_HID_CONNECTION_H_ |
| 6 #define DEVICE_HID_HID_CONNECTION_H_ | 6 #define DEVICE_HID_HID_CONNECTION_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "device/hid/hid_device_info.h" | 13 #include "device/hid/hid_device_info.h" |
| 14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 15 | 15 |
| 16 namespace device { | 16 namespace device { |
| 17 | 17 |
| 18 class HidConnection : public base::RefCountedThreadSafe<HidConnection> { | 18 class HidConnection : public base::RefCountedThreadSafe<HidConnection> { |
| 19 public: | 19 public: |
| 20 enum SpecialReportIds { | 20 enum SpecialReportIds { |
| 21 kNullReportId = 0x00, | 21 kNullReportId = 0x00, |
| 22 kAnyReportId = 0xFF, | 22 kAnyReportId = 0xFF, |
| 23 }; | 23 }; |
| 24 | 24 |
| 25 typedef base::Callback<void(bool success, size_t size)> IOCallback; | 25 typedef base::Callback< |
| 26 void(bool success, scoped_refptr<net::IOBuffer> buffer, size_t size)> |
| 27 ReadCallback; |
| 28 typedef base::Callback<void(bool success)> WriteCallback; |
| 26 | 29 |
| 27 const HidDeviceInfo& device_info() const { return device_info_; } | 30 const HidDeviceInfo& device_info() const { return device_info_; } |
| 28 bool has_protected_collection() const { return has_protected_collection_; } | 31 bool has_protected_collection() const { return has_protected_collection_; } |
| 29 const base::ThreadChecker& thread_checker() const { return thread_checker_; } | 32 const base::ThreadChecker& thread_checker() const { return thread_checker_; } |
| 30 | 33 |
| 31 void Read(scoped_refptr<net::IOBufferWithSize> buffer, | 34 // The report ID (or 0 if report IDs are not supported by the device) is |
| 32 const IOCallback& callback); | 35 // always returned in the first byte of the buffer. |
| 33 void Write(uint8_t report_id, | 36 void Read(const ReadCallback& callback); |
| 34 scoped_refptr<net::IOBufferWithSize> buffer, | 37 |
| 35 const IOCallback& callback); | 38 // The report ID (or 0 if report IDs are not supported by the device) is |
| 36 void GetFeatureReport(uint8_t report_id, | 39 // always expected in the first byte of the buffer. |
| 37 scoped_refptr<net::IOBufferWithSize> buffer, | 40 void Write(scoped_refptr<net::IOBuffer> buffer, |
| 38 const IOCallback& callback); | 41 size_t size, |
| 39 void SendFeatureReport(uint8_t report_id, | 42 const WriteCallback& callback); |
| 40 scoped_refptr<net::IOBufferWithSize> buffer, | 43 |
| 41 const IOCallback& callback); | 44 // The report ID is not returned in the buffer. |
| 45 void GetFeatureReport(uint8_t report_id, const ReadCallback& callback); |
| 46 |
| 47 // The report ID (or 0 if report IDs are not supported by the device) is |
| 48 // always expected in the first byte of the buffer. |
| 49 void SendFeatureReport(scoped_refptr<net::IOBuffer> buffer, |
| 50 size_t size, |
| 51 const WriteCallback& callback); |
| 42 | 52 |
| 43 protected: | 53 protected: |
| 44 friend class base::RefCountedThreadSafe<HidConnection>; | 54 friend class base::RefCountedThreadSafe<HidConnection>; |
| 45 | 55 |
| 46 explicit HidConnection(const HidDeviceInfo& device_info); | 56 explicit HidConnection(const HidDeviceInfo& device_info); |
| 47 virtual ~HidConnection(); | 57 virtual ~HidConnection(); |
| 48 | 58 |
| 49 virtual void PlatformRead(scoped_refptr<net::IOBufferWithSize> buffer, | 59 virtual void PlatformRead(const ReadCallback& callback) = 0; |
| 50 const IOCallback& callback) = 0; | 60 virtual void PlatformWrite(scoped_refptr<net::IOBuffer> buffer, |
| 51 virtual void PlatformWrite(uint8_t report_id, | 61 size_t size, |
| 52 scoped_refptr<net::IOBufferWithSize> buffer, | 62 const WriteCallback& callback) = 0; |
| 53 const IOCallback& callback) = 0; | 63 virtual void PlatformGetFeatureReport(uint8_t report_id, |
| 54 virtual void PlatformGetFeatureReport( | 64 const ReadCallback& callback) = 0; |
| 55 uint8_t report_id, | 65 virtual void PlatformSendFeatureReport(scoped_refptr<net::IOBuffer> buffer, |
| 56 scoped_refptr<net::IOBufferWithSize> buffer, | 66 size_t size, |
| 57 const IOCallback& callback) = 0; | 67 const WriteCallback& callback) = 0; |
| 58 virtual void PlatformSendFeatureReport( | |
| 59 uint8_t report_id, | |
| 60 scoped_refptr<net::IOBufferWithSize> buffer, | |
| 61 const IOCallback& callback) = 0; | |
| 62 | 68 |
| 63 // PlatformRead implementation must call this method on read | 69 // PlatformRead implementation must call this method on read |
| 64 // success, rather than directly running the callback. | 70 // success, rather than directly running the callback. |
| 65 // In case incoming buffer is empty or protected, it is filtered | 71 // In case incoming buffer is empty or protected, it is filtered |
| 66 // and this method returns false. Otherwise it runs the callback | 72 // and this method returns false. Otherwise it runs the callback |
| 67 // and returns true. | 73 // and returns true. |
| 68 bool CompleteRead(scoped_refptr<net::IOBufferWithSize> buffer, | 74 bool CompleteRead(scoped_refptr<net::IOBuffer> buffer, |
| 69 int bytes_read, | 75 size_t size, |
| 70 const IOCallback& callback); | 76 const ReadCallback& callback); |
| 71 | 77 |
| 72 private: | 78 private: |
| 73 bool IsReportIdProtected(const uint8_t report_id); | 79 bool IsReportIdProtected(uint8_t report_id); |
| 74 | 80 |
| 75 const HidDeviceInfo device_info_; | 81 const HidDeviceInfo device_info_; |
| 76 bool has_protected_collection_; | 82 bool has_protected_collection_; |
| 77 base::ThreadChecker thread_checker_; | 83 base::ThreadChecker thread_checker_; |
| 78 | 84 |
| 79 DISALLOW_COPY_AND_ASSIGN(HidConnection); | 85 DISALLOW_COPY_AND_ASSIGN(HidConnection); |
| 80 }; | 86 }; |
| 81 | 87 |
| 82 struct PendingHidReport { | 88 struct PendingHidReport { |
| 83 PendingHidReport(); | 89 PendingHidReport(); |
| 84 ~PendingHidReport(); | 90 ~PendingHidReport(); |
| 85 | 91 |
| 86 scoped_refptr<net::IOBufferWithSize> buffer; | 92 scoped_refptr<net::IOBuffer> buffer; |
| 93 size_t size; |
| 87 }; | 94 }; |
| 88 | 95 |
| 89 struct PendingHidRead { | 96 struct PendingHidRead { |
| 90 PendingHidRead(); | 97 PendingHidRead(); |
| 91 ~PendingHidRead(); | 98 ~PendingHidRead(); |
| 92 | 99 |
| 93 scoped_refptr<net::IOBufferWithSize> buffer; | 100 HidConnection::ReadCallback callback; |
| 94 HidConnection::IOCallback callback; | |
| 95 }; | 101 }; |
| 96 | 102 |
| 97 } // namespace device | 103 } // namespace device |
| 98 | 104 |
| 99 #endif // DEVICE_HID_HID_CONNECTION_H_ | 105 #endif // DEVICE_HID_HID_CONNECTION_H_ |
| OLD | NEW |