1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SERVICES_DEVICE_SERIAL_SERIAL_PORT_IMPL_H_
6 #define SERVICES_DEVICE_SERIAL_SERIAL_PORT_IMPL_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "mojo/public/cpp/bindings/pending_receiver.h"
11 #include "mojo/public/cpp/bindings/pending_remote.h"
12 #include "mojo/public/cpp/bindings/receiver.h"
13 #include "mojo/public/cpp/bindings/remote.h"
14 #include "mojo/public/cpp/system/data_pipe.h"
15 #include "mojo/public/cpp/system/simple_watcher.h"
16 #include "services/device/public/mojom/serial.mojom.h"
19 class SingleThreadTaskRunner;
24 class SerialIoHandler;
26 // TODO(leonhsl): Merge this class with SerialIoHandler if/once
27 // SerialIoHandler is exposed only via the Device Service.
29 // This class must be constructed and run on IO thread.
30 class SerialPortImpl : public mojom::SerialPort {
33 base::OnceCallback<void(mojo::PendingRemote<mojom::SerialPort>)>;
36 const base::FilePath& path,
37 mojom::SerialConnectionOptionsPtr options,
38 mojo::PendingRemote<mojom::SerialPortClient> client,
39 mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
40 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
41 OpenCallback callback);
43 static void OpenForTesting(
44 scoped_refptr<SerialIoHandler> io_handler,
45 mojom::SerialConnectionOptionsPtr options,
46 mojo::PendingRemote<mojom::SerialPortClient> client,
47 mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher,
48 OpenCallback callback);
50 SerialPortImpl(const SerialPortImpl&) = delete;
51 SerialPortImpl& operator=(const SerialPortImpl&) = delete;
55 scoped_refptr<SerialIoHandler> io_handler,
56 mojo::PendingRemote<mojom::SerialPortClient> client,
57 mojo::PendingRemote<mojom::SerialPortConnectionWatcher> watcher);
58 ~SerialPortImpl() override;
60 // mojom::SerialPort methods:
61 void StartWriting(mojo::ScopedDataPipeConsumerHandle consumer) override;
62 void StartReading(mojo::ScopedDataPipeProducerHandle producer) override;
63 void Flush(mojom::SerialPortFlushMode mode, FlushCallback callback) override;
64 void Drain(DrainCallback callback) override;
65 void GetControlSignals(GetControlSignalsCallback callback) override;
66 void SetControlSignals(mojom::SerialHostControlSignalsPtr signals,
67 SetControlSignalsCallback callback) override;
68 void ConfigurePort(mojom::SerialConnectionOptionsPtr options,
69 ConfigurePortCallback callback) override;
70 void GetPortInfo(GetPortInfoCallback callback) override;
71 void Close(bool flush, CloseCallback callback) override;
73 void OpenPort(const mojom::SerialConnectionOptions& options,
74 OpenCallback callback);
75 void PortOpened(OpenCallback callback, bool success);
76 void WriteToPort(MojoResult result, const mojo::HandleSignalsState& state);
77 void OnWriteToPortCompleted(uint32_t bytes_expected,
79 mojom::SerialSendError error);
80 void ReadFromPortAndWriteOut(MojoResult result,
81 const mojo::HandleSignalsState& state);
82 void WriteToOutStream(uint32_t bytes_read, mojom::SerialReceiveError error);
83 void PortClosed(CloseCallback callback);
85 mojo::Receiver<mojom::SerialPort> receiver_{this};
87 // Underlying connection to the serial port.
88 scoped_refptr<SerialIoHandler> io_handler_;
91 mojo::Remote<mojom::SerialPortClient> client_;
92 mojo::Remote<mojom::SerialPortConnectionWatcher> watcher_;
94 // Data pipes for input and output.
95 mojo::ScopedDataPipeConsumerHandle in_stream_;
96 mojo::SimpleWatcher in_stream_watcher_;
97 mojo::ScopedDataPipeProducerHandle out_stream_;
98 mojo::SimpleWatcher out_stream_watcher_;
100 // Holds the callback for a flush or drain until pending operations have been
102 FlushCallback read_flush_callback_;
103 FlushCallback write_flush_callback_;
104 DrainCallback drain_callback_;
106 base::WeakPtrFactory<SerialPortImpl> weak_factory_{this};
109 } // namespace device
111 #endif // SERVICES_DEVICE_SERIAL_SERIAL_PORT_IMPL_H_