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