OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/serial/serial_api.h" | 5 #include "chrome/browser/extensions/api/serial/serial_api.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 // But we'd like to pick something that has a chance of working, and 9600 is a | 30 // But we'd like to pick something that has a chance of working, and 9600 is a |
31 // good balance between popularity and speed. So 9600 it is. | 31 // good balance between popularity and speed. So 9600 it is. |
32 const int kDefaultBufferSize = 4096; | 32 const int kDefaultBufferSize = 4096; |
33 const int kDefaultBitrate = 9600; | 33 const int kDefaultBitrate = 9600; |
34 const serial::DataBits kDefaultDataBits = serial::DATA_BITS_EIGHT; | 34 const serial::DataBits kDefaultDataBits = serial::DATA_BITS_EIGHT; |
35 const serial::ParityBit kDefaultParityBit = serial::PARITY_BIT_NO; | 35 const serial::ParityBit kDefaultParityBit = serial::PARITY_BIT_NO; |
36 const serial::StopBits kDefaultStopBits = serial::STOP_BITS_ONE; | 36 const serial::StopBits kDefaultStopBits = serial::STOP_BITS_ONE; |
37 const int kDefaultReceiveTimeout = 0; | 37 const int kDefaultReceiveTimeout = 0; |
38 const int kDefaultSendTimeout = 0; | 38 const int kDefaultSendTimeout = 0; |
39 | 39 |
40 const char kErrorOpenFailed[] = "Failed to open the port."; | 40 const char kErrorConnectFailed[] = "Failed to connect to the port."; |
41 const char kErrorSerialConnectionNotFound[] = "Serial connection not found."; | 41 const char kErrorSerialConnectionNotFound[] = "Serial connection not found."; |
42 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; | 42 const char kErrorGetControlSignalsFailed[] = "Failed to get control signals."; |
43 | 43 |
44 template <class T> | 44 template <class T> |
45 void SetDefaultScopedPtrValue(scoped_ptr<T>& ptr, const T& value) { | 45 void SetDefaultScopedPtrValue(scoped_ptr<T>& ptr, const T& value) { |
46 if (!ptr.get()) | 46 if (!ptr.get()) |
47 ptr.reset(new T(value)); | 47 ptr.reset(new T(value)); |
48 } | 48 } |
49 | 49 |
50 } // namespace | 50 } // namespace |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 port_names.begin(); | 91 port_names.begin(); |
92 iter != port_names.end(); | 92 iter != port_names.end(); |
93 ++iter) { | 93 ++iter) { |
94 linked_ptr<serial::DeviceInfo> info(new serial::DeviceInfo); | 94 linked_ptr<serial::DeviceInfo> info(new serial::DeviceInfo); |
95 info->path = *iter; | 95 info->path = *iter; |
96 devices.push_back(info); | 96 devices.push_back(info); |
97 } | 97 } |
98 results_ = serial::GetDevices::Results::Create(devices); | 98 results_ = serial::GetDevices::Results::Create(devices); |
99 } | 99 } |
100 | 100 |
101 SerialOpenFunction::SerialOpenFunction() {} | 101 SerialConnectFunction::SerialConnectFunction() {} |
102 | 102 |
103 SerialOpenFunction::~SerialOpenFunction() {} | 103 SerialConnectFunction::~SerialConnectFunction() {} |
104 | 104 |
105 bool SerialOpenFunction::Prepare() { | 105 bool SerialConnectFunction::Prepare() { |
106 params_ = serial::Open::Params::Create(*args_); | 106 params_ = serial::Connect::Params::Create(*args_); |
107 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 107 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
108 | 108 |
109 // Fill in any omitted options to ensure a known initial configuration. | 109 // Fill in any omitted options to ensure a known initial configuration. |
110 if (!params_->options.get()) | 110 if (!params_->options.get()) |
111 params_->options.reset(new serial::ConnectionOptions()); | 111 params_->options.reset(new serial::ConnectionOptions()); |
112 serial::ConnectionOptions* options = params_->options.get(); | 112 serial::ConnectionOptions* options = params_->options.get(); |
113 | 113 |
114 SetDefaultScopedPtrValue(options->persistent, false); | 114 SetDefaultScopedPtrValue(options->persistent, false); |
115 SetDefaultScopedPtrValue(options->buffer_size, kDefaultBufferSize); | 115 SetDefaultScopedPtrValue(options->buffer_size, kDefaultBufferSize); |
116 SetDefaultScopedPtrValue(options->bitrate, kDefaultBitrate); | 116 SetDefaultScopedPtrValue(options->bitrate, kDefaultBitrate); |
117 SetDefaultScopedPtrValue(options->cts_flow_control, false); | 117 SetDefaultScopedPtrValue(options->cts_flow_control, false); |
118 SetDefaultScopedPtrValue(options->receive_timeout, kDefaultReceiveTimeout); | 118 SetDefaultScopedPtrValue(options->receive_timeout, kDefaultReceiveTimeout); |
119 SetDefaultScopedPtrValue(options->send_timeout, kDefaultSendTimeout); | 119 SetDefaultScopedPtrValue(options->send_timeout, kDefaultSendTimeout); |
120 | 120 |
121 if (options->data_bits == serial::DATA_BITS_NONE) | 121 if (options->data_bits == serial::DATA_BITS_NONE) |
122 options->data_bits = kDefaultDataBits; | 122 options->data_bits = kDefaultDataBits; |
123 if (options->parity_bit == serial::PARITY_BIT_NONE) | 123 if (options->parity_bit == serial::PARITY_BIT_NONE) |
124 options->parity_bit = kDefaultParityBit; | 124 options->parity_bit = kDefaultParityBit; |
125 if (options->stop_bits == serial::STOP_BITS_NONE) | 125 if (options->stop_bits == serial::STOP_BITS_NONE) |
126 options->stop_bits = kDefaultStopBits; | 126 options->stop_bits = kDefaultStopBits; |
127 | 127 |
128 serial_event_dispatcher_ = SerialEventDispatcher::Get(GetProfile()); | 128 serial_event_dispatcher_ = SerialEventDispatcher::Get(GetProfile()); |
129 DCHECK(serial_event_dispatcher_); | 129 DCHECK(serial_event_dispatcher_); |
130 | 130 |
131 return true; | 131 return true; |
132 } | 132 } |
133 | 133 |
134 void SerialOpenFunction::AsyncWorkStart() { | 134 void SerialConnectFunction::AsyncWorkStart() { |
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
136 connection_ = CreateSerialConnection(params_->path, extension_->id()); | 136 connection_ = CreateSerialConnection(params_->path, extension_->id()); |
137 connection_->Open(base::Bind(&SerialOpenFunction::OnOpen, this)); | 137 connection_->Open(base::Bind(&SerialConnectFunction::OnConnected, this)); |
138 } | 138 } |
139 | 139 |
140 void SerialOpenFunction::OnOpen(bool success) { | 140 void SerialConnectFunction::OnConnected(bool success) { |
141 DCHECK(connection_); | 141 DCHECK(connection_); |
142 | 142 |
143 if (success) { | 143 if (success) { |
144 if (!connection_->Configure(*params_->options.get())) { | 144 if (!connection_->Configure(*params_->options.get())) { |
145 connection_->Close(); | 145 connection_->Close(); |
146 delete connection_; | 146 delete connection_; |
147 connection_ = NULL; | 147 connection_ = NULL; |
148 } | 148 } |
149 } else { | 149 } else { |
150 delete connection_; | 150 delete connection_; |
151 connection_ = NULL; | 151 connection_ = NULL; |
152 } | 152 } |
153 | 153 |
154 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 154 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
155 base::Bind(&SerialOpenFunction::FinishOpen, this)); | 155 base::Bind(&SerialConnectFunction::FinishConnect, |
| 156 this)); |
156 } | 157 } |
157 | 158 |
158 void SerialOpenFunction::FinishOpen() { | 159 void SerialConnectFunction::FinishConnect() { |
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
160 if (!connection_) { | 161 if (!connection_) { |
161 error_ = kErrorOpenFailed; | 162 error_ = kErrorConnectFailed; |
162 } else { | 163 } else { |
163 int id = manager_->Add(connection_); | 164 int id = manager_->Add(connection_); |
164 serial_event_dispatcher_->PollConnection(extension_->id(), id); | 165 serial::ConnectionInfo info; |
165 | 166 info.connection_id = id; |
166 serial::OpenInfo open_info; | 167 if (connection_->GetInfo(&info)) { |
167 open_info.connection_id = id; | 168 serial_event_dispatcher_->PollConnection(extension_->id(), id); |
168 results_ = serial::Open::Results::Create(open_info); | 169 results_ = serial::Connect::Results::Create(info); |
| 170 } else { |
| 171 connection_->Close(); |
| 172 RemoveSerialConnection(id); |
| 173 error_ = kErrorConnectFailed; |
| 174 } |
169 } | 175 } |
170 AsyncWorkCompleted(); | 176 AsyncWorkCompleted(); |
171 } | 177 } |
172 | 178 |
173 SerialConnection* SerialOpenFunction::CreateSerialConnection( | 179 SerialConnection* SerialConnectFunction::CreateSerialConnection( |
174 const std::string& port, const std::string& extension_id) const { | 180 const std::string& port, const std::string& extension_id) const { |
175 return new SerialConnection(port, extension_id); | 181 return new SerialConnection(port, extension_id); |
176 } | 182 } |
177 | 183 |
178 SerialUpdateFunction::SerialUpdateFunction() {} | 184 SerialUpdateFunction::SerialUpdateFunction() {} |
179 | 185 |
180 SerialUpdateFunction::~SerialUpdateFunction() {} | 186 SerialUpdateFunction::~SerialUpdateFunction() {} |
181 | 187 |
182 bool SerialUpdateFunction::Prepare() { | 188 bool SerialUpdateFunction::Prepare() { |
183 params_ = serial::Update::Params::Create(*args_); | 189 params_ = serial::Update::Params::Create(*args_); |
184 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 190 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
185 | 191 |
186 return true; | 192 return true; |
187 } | 193 } |
188 | 194 |
189 void SerialUpdateFunction::Work() { | 195 void SerialUpdateFunction::Work() { |
190 SerialConnection* connection = GetSerialConnection(params_->connection_id); | 196 SerialConnection* connection = GetSerialConnection(params_->connection_id); |
191 if (!connection) { | 197 if (!connection) { |
192 error_ = kErrorSerialConnectionNotFound; | 198 error_ = kErrorSerialConnectionNotFound; |
193 return; | 199 return; |
194 } | 200 } |
195 bool success = connection->Configure(params_->options); | 201 bool success = connection->Configure(params_->options); |
196 results_ = serial::Update::Results::Create(success); | 202 results_ = serial::Update::Results::Create(success); |
197 } | 203 } |
198 | 204 |
199 SerialCloseFunction::SerialCloseFunction() {} | 205 SerialDisconnectFunction::SerialDisconnectFunction() {} |
200 | 206 |
201 SerialCloseFunction::~SerialCloseFunction() {} | 207 SerialDisconnectFunction::~SerialDisconnectFunction() {} |
202 | 208 |
203 bool SerialCloseFunction::Prepare() { | 209 bool SerialDisconnectFunction::Prepare() { |
204 params_ = serial::Close::Params::Create(*args_); | 210 params_ = serial::Disconnect::Params::Create(*args_); |
205 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 211 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
206 | 212 |
207 return true; | 213 return true; |
208 } | 214 } |
209 | 215 |
210 void SerialCloseFunction::Work() { | 216 void SerialDisconnectFunction::Work() { |
211 SerialConnection* connection = GetSerialConnection(params_->connection_id); | 217 SerialConnection* connection = GetSerialConnection(params_->connection_id); |
212 if (!connection) { | 218 if (!connection) { |
213 error_ = kErrorSerialConnectionNotFound; | 219 error_ = kErrorSerialConnectionNotFound; |
214 return; | 220 return; |
215 } | 221 } |
216 connection->Close(); | 222 connection->Close(); |
217 RemoveSerialConnection(params_->connection_id); | 223 RemoveSerialConnection(params_->connection_id); |
218 results_ = serial::Close::Results::Create(true); | 224 results_ = serial::Disconnect::Results::Create(true); |
219 } | 225 } |
220 | 226 |
221 SerialSendFunction::SerialSendFunction() {} | 227 SerialSendFunction::SerialSendFunction() {} |
222 | 228 |
223 SerialSendFunction::~SerialSendFunction() {} | 229 SerialSendFunction::~SerialSendFunction() {} |
224 | 230 |
225 bool SerialSendFunction::Prepare() { | 231 bool SerialSendFunction::Prepare() { |
226 params_ = serial::Send::Params::Create(*args_); | 232 params_ = serial::Send::Params::Create(*args_); |
227 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 233 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
228 | 234 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 return; | 408 return; |
403 } | 409 } |
404 | 410 |
405 bool success = connection->SetControlSignals(params_->signals); | 411 bool success = connection->SetControlSignals(params_->signals); |
406 results_ = serial::SetControlSignals::Results::Create(success); | 412 results_ = serial::SetControlSignals::Results::Create(success); |
407 } | 413 } |
408 | 414 |
409 } // namespace api | 415 } // namespace api |
410 | 416 |
411 } // namespace extensions | 417 } // namespace extensions |
OLD | NEW |