1 // Copyright (c) 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.
5 #ifndef DEVICE_HID_HID_CONNECTION_LINUX_H_
6 #define DEVICE_HID_HID_CONNECTION_LINUX_H_
10 #include "base/files/file.h"
11 #include "base/memory/weak_ptr.h"
12 #include "device/hid/hid_connection.h"
15 class SingleThreadTaskRunner
;
20 class HidConnectionLinux
: public HidConnection
{
23 scoped_refptr
<HidDeviceInfo
> device_info
,
24 base::File device_file
,
25 scoped_refptr
<base::SingleThreadTaskRunner
> file_thread_runner
);
28 friend class base::RefCountedThreadSafe
<HidConnectionLinux
>;
29 class FileThreadHelper
;
31 typedef base::Callback
<void(ssize_t
)> InternalWriteCallback
;
32 typedef base::Callback
<void(int)> IoctlCallback
;
34 ~HidConnectionLinux() override
;
36 // HidConnection implementation.
37 void PlatformClose() override
;
38 void PlatformRead(const ReadCallback
& callback
) override
;
39 void PlatformWrite(scoped_refptr
<net::IOBuffer
> buffer
,
41 const WriteCallback
& callback
) override
;
42 void PlatformGetFeatureReport(uint8_t report_id
,
43 const ReadCallback
& callback
) override
;
44 void PlatformSendFeatureReport(scoped_refptr
<net::IOBuffer
> buffer
,
46 const WriteCallback
& callback
) override
;
48 // Callbacks for blocking operations run on the FILE thread.
49 void FinishWrite(size_t expected_size
,
50 const WriteCallback
& callback
,
52 void FinishGetFeatureReport(uint8_t report_id
,
53 scoped_refptr
<net::IOBuffer
> buffer
,
54 const ReadCallback
& callback
,
56 void FinishSendFeatureReport(const WriteCallback
& callback
, int result
);
58 // Writes to the device. This operation may block.
59 static void BlockingWrite(
60 base::PlatformFile platform_file
,
61 scoped_refptr
<net::IOBuffer
> buffer
,
63 const InternalWriteCallback
& callback
,
64 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
65 // Performs an ioctl on the device. This operation may block.
66 static void BlockingIoctl(
67 base::PlatformFile platform_file
,
69 scoped_refptr
<net::IOBuffer
> buffer
,
70 const IoctlCallback
& callback
,
71 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
73 // Closes the device file descriptor. Must be called on the FILE thread.
74 static void CloseDevice(base::File device_file
);
76 void ProcessInputReport(scoped_refptr
<net::IOBuffer
> buffer
, size_t size
);
77 void ProcessReadQueue();
79 base::File device_file_
;
80 FileThreadHelper
* helper_
;
82 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
83 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner_
;
85 std::queue
<PendingHidReport
> pending_reports_
;
86 std::queue
<PendingHidRead
> pending_reads_
;
88 base::WeakPtrFactory
<HidConnectionLinux
> weak_factory_
;
90 DISALLOW_COPY_AND_ASSIGN(HidConnectionLinux
);
95 #endif // DEVICE_HID_HID_CONNECTION_LINUX_H_