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 const HidDeviceInfo
& device_info
,
24 base::File device_file
,
25 scoped_refptr
<base::SingleThreadTaskRunner
> file_thread_runner
);
30 friend class base::RefCountedThreadSafe
<HidConnectionLinux
>;
32 typedef base::Callback
<void(ssize_t
)> InternalWriteCallback
;
33 typedef base::Callback
<void(int)> IoctlCallback
;
35 ~HidConnectionLinux() override
;
37 // HidConnection implementation.
38 void PlatformClose() override
;
39 void PlatformRead(const ReadCallback
& callback
) override
;
40 void PlatformWrite(scoped_refptr
<net::IOBuffer
> buffer
,
42 const WriteCallback
& callback
) override
;
43 void PlatformGetFeatureReport(uint8_t report_id
,
44 const ReadCallback
& callback
) override
;
45 void PlatformSendFeatureReport(scoped_refptr
<net::IOBuffer
> buffer
,
47 const WriteCallback
& callback
) override
;
49 // Callbacks for blocking operations run on the FILE thread.
50 void FinishWrite(size_t expected_size
,
51 const WriteCallback
& callback
,
53 void FinishGetFeatureReport(uint8_t report_id
,
54 scoped_refptr
<net::IOBuffer
> buffer
,
55 const ReadCallback
& callback
,
57 void FinishSendFeatureReport(const WriteCallback
& callback
, int result
);
59 // Writes to the device. This operation may block.
60 static void BlockingWrite(
61 base::PlatformFile platform_file
,
62 scoped_refptr
<net::IOBuffer
> buffer
,
64 const InternalWriteCallback
& callback
,
65 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
66 // Performs an ioctl on the device. This operation may block.
67 static void BlockingIoctl(
68 base::PlatformFile platform_file
,
70 scoped_refptr
<net::IOBuffer
> buffer
,
71 const IoctlCallback
& callback
,
72 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
74 // Closes the device file descriptor. Must be called on the FILE thread.
75 static void CloseDevice(base::File device_file
);
77 void ProcessInputReport(scoped_refptr
<net::IOBuffer
> buffer
, size_t size
);
78 void ProcessReadQueue();
80 base::File device_file_
;
83 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
84 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner_
;
86 std::queue
<PendingHidReport
> pending_reports_
;
87 std::queue
<PendingHidRead
> pending_reads_
;
89 base::WeakPtrFactory
<HidConnectionLinux
> weak_factory_
;
91 DISALLOW_COPY_AND_ASSIGN(HidConnectionLinux
);
96 #endif // DEVICE_HID_HID_CONNECTION_LINUX_H_