Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: device/serial/serial_connection_factory.cc

Issue 401563002: Add a partial Mojo serial connection interface and implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/serial/serial_connection_factory.h ('k') | device/serial/serial_connection_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 #include "device/serial/serial_connection_factory.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "device/serial/serial_connection.h"
10 #include "device/serial/serial_io_handler.h"
11
12 namespace device {
13 namespace {
14
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;
27 }
28 }
29
30 } // namespace
31
32 class SerialConnectionFactory::ConnectTask
33 : public base::RefCountedThreadSafe<SerialConnectionFactory::ConnectTask> {
34 public:
35 ConnectTask(scoped_refptr<SerialConnectionFactory> factory,
36 const std::string& path,
37 serial::ConnectionOptionsPtr options,
38 mojo::InterfaceRequest<serial::Connection> connection_request);
39 void Run();
40
41 private:
42 friend class base::RefCountedThreadSafe<SerialConnectionFactory::ConnectTask>;
43 virtual ~ConnectTask();
44 void Connect();
45 void OnConnected(bool success);
46
47 scoped_refptr<SerialConnectionFactory> factory_;
48 const std::string path_;
49 serial::ConnectionOptionsPtr options_;
50 mojo::InterfaceRequest<serial::Connection> connection_request_;
51 scoped_refptr<SerialIoHandler> io_handler_;
52
53 DISALLOW_COPY_AND_ASSIGN(ConnectTask);
54 };
55
56 SerialConnectionFactory::SerialConnectionFactory(
57 const IoHandlerFactory& io_handler_factory,
58 scoped_refptr<base::MessageLoopProxy> connect_message_loop)
59 : io_handler_factory_(io_handler_factory),
60 connect_message_loop_(connect_message_loop) {
61 }
62
63 void SerialConnectionFactory::CreateConnection(
64 const std::string& path,
65 serial::ConnectionOptionsPtr options,
66 mojo::InterfaceRequest<serial::Connection> connection_request) {
67 scoped_refptr<ConnectTask> task(
68 new ConnectTask(this, path, options.Pass(), connection_request.Pass()));
69 task->Run();
70 }
71
72 SerialConnectionFactory::~SerialConnectionFactory() {
73 }
74
75 SerialConnectionFactory::ConnectTask::ConnectTask(
76 scoped_refptr<SerialConnectionFactory> factory,
77 const std::string& path,
78 serial::ConnectionOptionsPtr options,
79 mojo::InterfaceRequest<serial::Connection> connection_request)
80 : factory_(factory),
81 path_(path),
82 options_(options.Pass()),
83 connection_request_(connection_request.Pass()) {
84 }
85
86 void SerialConnectionFactory::ConnectTask::Run() {
87 factory_->connect_message_loop_->PostTask(
88 FROM_HERE,
89 base::Bind(&SerialConnectionFactory::ConnectTask::Connect, this));
90 }
91
92 SerialConnectionFactory::ConnectTask::~ConnectTask() {
93 }
94
95 void SerialConnectionFactory::ConnectTask::Connect() {
96 io_handler_ = factory_->io_handler_factory_.Run();
97 io_handler_->Open(
98 path_,
99 base::Bind(&SerialConnectionFactory::ConnectTask::OnConnected, this));
100 }
101
102 void SerialConnectionFactory::ConnectTask::OnConnected(bool success) {
103 DCHECK(io_handler_);
104 if (!success)
105 return;
106 if (!options_)
107 options_ = serial::ConnectionOptions::New();
108 FillDefaultConnectionOptions(options_.get());
109 if (!io_handler_->ConfigurePort(*options_))
110 return;
111 mojo::BindToRequest(new SerialConnection(io_handler_), &connection_request_);
112 }
113
114 } // namespace device
OLDNEW
« no previous file with comments | « device/serial/serial_connection_factory.h ('k') | device/serial/serial_connection_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698