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.
5 #include "device/serial/serial_connection_factory.h"
8 #include "base/location.h"
9 #include "device/serial/serial_connection.h"
10 #include "device/serial/serial_io_handler.h"
15 void FillDefaultConnectionOptions(serial::ConnectionOptions
* options
) {
16 if (!options
->bitrate
)
17 options
->bitrate
= 9600;
18 if (options
->data_bits
== serial::DATA_BITS_NONE
)
19 options
->data_bits
= serial::DATA_BITS_EIGHT
;
20 if (options
->stop_bits
== serial::STOP_BITS_NONE
)
21 options
->stop_bits
= serial::STOP_BITS_ONE
;
22 if (options
->parity_bit
== serial::PARITY_BIT_NONE
)
23 options
->parity_bit
= serial::PARITY_BIT_NO
;
24 if (!options
->has_cts_flow_control
) {
25 options
->has_cts_flow_control
= true;
26 options
->cts_flow_control
= false;
32 class SerialConnectionFactory::ConnectTask
33 : public base::RefCountedThreadSafe
<SerialConnectionFactory::ConnectTask
> {
35 ConnectTask(scoped_refptr
<SerialConnectionFactory
> factory
,
36 const std::string
& path
,
37 serial::ConnectionOptionsPtr options
,
38 mojo::InterfaceRequest
<serial::Connection
> connection_request
,
39 mojo::InterfaceRequest
<serial::DataSink
> sink
,
40 mojo::InterfaceRequest
<serial::DataSource
> source
,
41 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
);
45 friend class base::RefCountedThreadSafe
<SerialConnectionFactory::ConnectTask
>;
46 virtual ~ConnectTask();
48 void OnConnected(bool success
);
50 scoped_refptr
<SerialConnectionFactory
> factory_
;
51 const std::string path_
;
52 serial::ConnectionOptionsPtr options_
;
53 mojo::InterfaceRequest
<serial::Connection
> connection_request_
;
54 mojo::InterfaceRequest
<serial::DataSink
> sink_
;
55 mojo::InterfaceRequest
<serial::DataSource
> source_
;
56 mojo::InterfacePtr
<serial::DataSourceClient
> source_client_
;
57 scoped_refptr
<SerialIoHandler
> io_handler_
;
59 DISALLOW_COPY_AND_ASSIGN(ConnectTask
);
62 SerialConnectionFactory::SerialConnectionFactory(
63 const IoHandlerFactory
& io_handler_factory
,
64 scoped_refptr
<base::MessageLoopProxy
> connect_message_loop
)
65 : io_handler_factory_(io_handler_factory
),
66 connect_message_loop_(connect_message_loop
) {
69 void SerialConnectionFactory::CreateConnection(
70 const std::string
& path
,
71 serial::ConnectionOptionsPtr options
,
72 mojo::InterfaceRequest
<serial::Connection
> connection_request
,
73 mojo::InterfaceRequest
<serial::DataSink
> sink
,
74 mojo::InterfaceRequest
<serial::DataSource
> source
,
75 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
) {
76 scoped_refptr
<ConnectTask
> task(
77 new ConnectTask(this, path
, options
.Pass(), connection_request
.Pass(),
78 sink
.Pass(), source
.Pass(), source_client
.Pass()));
82 SerialConnectionFactory::~SerialConnectionFactory() {
85 SerialConnectionFactory::ConnectTask::ConnectTask(
86 scoped_refptr
<SerialConnectionFactory
> factory
,
87 const std::string
& path
,
88 serial::ConnectionOptionsPtr options
,
89 mojo::InterfaceRequest
<serial::Connection
> connection_request
,
90 mojo::InterfaceRequest
<serial::DataSink
> sink
,
91 mojo::InterfaceRequest
<serial::DataSource
> source
,
92 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
)
95 options_(options
.Pass()),
96 connection_request_(connection_request
.Pass()),
98 source_(source
.Pass()),
99 source_client_(source_client
.Pass()) {
101 options_
= serial::ConnectionOptions::New();
103 FillDefaultConnectionOptions(options_
.get());
106 void SerialConnectionFactory::ConnectTask::Run() {
107 factory_
->connect_message_loop_
->PostTask(
109 base::Bind(&SerialConnectionFactory::ConnectTask::Connect
, this));
112 SerialConnectionFactory::ConnectTask::~ConnectTask() {
115 void SerialConnectionFactory::ConnectTask::Connect() {
116 io_handler_
= factory_
->io_handler_factory_
.Run();
119 base::Bind(&SerialConnectionFactory::ConnectTask::OnConnected
, this));
122 void SerialConnectionFactory::ConnectTask::OnConnected(bool success
) {
123 DCHECK(io_handler_
.get());
129 new SerialConnection(io_handler_
, sink_
.Pass(), source_
.Pass(),
130 source_client_
.Pass()),
131 &connection_request_
);
134 } // namespace device