summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/serial/serial_api.cc
blob: 8ffb83403454dbe54be50216c0b8e107a0f41d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/serial/serial_api.h"

#include "base/values.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/api/serial/serial_connection.h"
#include "chrome/browser/extensions/api/serial/serial_port_enumerator.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

namespace extensions {

const char kConnectionIdKey[] = "connectionId";
const char kPortsKey[] = "ports";
const char kDataKey[] = "data";
const char kBytesReadKey[] = "bytesRead";
const char kBytesToReadKey[] = "bytesToRead";
const char kBytesWrittenKey[] = "bytesWritten";
const char kBitrateKey[] = "bitrate";
const char kOptionsKey[] = "options";
const char kSuccessKey[] = "success";
const char kDtrKey[] = "dtr";
const char kRtsKey[] = "rts";
const char kDcdKey[] = "dcd";
const char kCtsKey[] = "cts";

const char kErrorGetControlSignalsFailed[] = "Failed to get control signals.";
const char kErrorSetControlSignalsFailed[] = "Failed to set control signals.";
const char kSerialReadInvalidBytesToRead[] = "Number of bytes to read must "
    "be a positive number less than 1,048,576.";

SerialAsyncApiFunction::SerialAsyncApiFunction()
    : manager_(NULL) {
}

SerialAsyncApiFunction::~SerialAsyncApiFunction() {
}

bool SerialAsyncApiFunction::PrePrepare() {
  manager_ = ExtensionSystem::Get(profile())->serial_connection_manager();
  return manager_ != NULL;
}

SerialConnection* SerialAsyncApiFunction::GetSerialConnection(
    int api_resource_id) {
  return manager_->Get(extension_->id(), api_resource_id);
}

void SerialAsyncApiFunction::RemoveSerialConnection(int api_resource_id) {
  manager_->Remove(extension_->id(), api_resource_id);
}

SerialGetPortsFunction::SerialGetPortsFunction() {}

bool SerialGetPortsFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);
  return true;
}

void SerialGetPortsFunction::Work() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  ListValue* ports = new ListValue();
  SerialPortEnumerator::StringSet port_names =
      SerialPortEnumerator::GenerateValidSerialPortNames();
  SerialPortEnumerator::StringSet::const_iterator i = port_names.begin();
  while (i != port_names.end()) {
    ports->Append(Value::CreateStringValue(*i++));
  }

  SetResult(ports);
}

bool SerialGetPortsFunction::Respond() {
  return true;
}

// It's a fool's errand to come up with a default bitrate, because we don't get
// to control both sides of the communication. Unless the other side has
// implemented auto-bitrate detection (rare), if we pick the wrong rate, then
// you're gonna have a bad time. Close doesn't count.
//
// But we'd like to pick something that has a chance of working, and 9600 is a
// good balance between popularity and speed. So 9600 it is.
SerialOpenFunction::SerialOpenFunction()
    : src_id_(-1),
      bitrate_(9600),
      event_notifier_(NULL) {
}

SerialOpenFunction::~SerialOpenFunction() {
}

bool SerialOpenFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::Open::Params::Create(*args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());

  if (params_->options.get()) {
    scoped_ptr<DictionaryValue> options = params_->options->ToValue();
    if (options->HasKey(kBitrateKey))
      EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kBitrateKey, &bitrate_));

    src_id_ = ExtractSrcId(options.get());
    event_notifier_ = CreateEventNotifier(src_id_);
  }

  return true;
}

void SerialOpenFunction::AsyncWorkStart() {
  Work();
}

void SerialOpenFunction::Work() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  const SerialPortEnumerator::StringSet name_set(
    SerialPortEnumerator::GenerateValidSerialPortNames());
  if (DoesPortExist(params_->port)) {
    SerialConnection* serial_connection = CreateSerialConnection(
      params_->port,
      bitrate_,
      extension_->id(),
      event_notifier_);
    CHECK(serial_connection);
    int id = manager_->Add(serial_connection);
    CHECK(id);

    bool open_result = serial_connection->Open();
    if (!open_result) {
      serial_connection->Close();
      RemoveSerialConnection(id);
      id = -1;
    }

    DictionaryValue* result = new DictionaryValue();
    result->SetInteger(kConnectionIdKey, id);
    SetResult(result);
    AsyncWorkCompleted();
  } else {
    DictionaryValue* result = new DictionaryValue();
    result->SetInteger(kConnectionIdKey, -1);
    SetResult(result);
    AsyncWorkCompleted();
  }
}

SerialConnection* SerialOpenFunction::CreateSerialConnection(
    const std::string& port,
    int bitrate,
    const std::string& owner_extension_id,
    ApiResourceEventNotifier* event_notifier) {
  return new SerialConnection(port, bitrate, owner_extension_id,
                              event_notifier);
}

bool SerialOpenFunction::DoesPortExist(const std::string& port) {
  const SerialPortEnumerator::StringSet name_set(
    SerialPortEnumerator::GenerateValidSerialPortNames());
  return SerialPortEnumerator::DoesPortExist(name_set, params_->port);
}

bool SerialOpenFunction::Respond() {
  return true;
}

SerialCloseFunction::SerialCloseFunction() {
}

SerialCloseFunction::~SerialCloseFunction() {
}

bool SerialCloseFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::Close::Params::Create(*args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());

  return true;
}

void SerialCloseFunction::Work() {
  bool close_result = false;
  SerialConnection* serial_connection = GetSerialConnection(
      params_->connection_id);
  if (serial_connection) {
    serial_connection->Close();
    RemoveSerialConnection(params_->connection_id);
    close_result = true;
  }

  SetResult(Value::CreateBooleanValue(close_result));
}

bool SerialCloseFunction::Respond() {
  return true;
}

SerialReadFunction::SerialReadFunction() {
}

SerialReadFunction::~SerialReadFunction() {
}

bool SerialReadFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::Read::Params::Create(*args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());
  if (params_->bytes_to_read <= 0 || params_->bytes_to_read >= 1024 * 1024) {
    error_ = kSerialReadInvalidBytesToRead;
    return false;
  }

  return true;
}

void SerialReadFunction::Work() {
  int bytes_read = -1;
  scoped_refptr<net::IOBufferWithSize> io_buffer(
      new net::IOBufferWithSize(params_->bytes_to_read));
  SerialConnection* serial_connection(GetSerialConnection(
      params_->connection_id));

  if (serial_connection)
    bytes_read = serial_connection->Read(io_buffer);

  DictionaryValue* result = new DictionaryValue();

  // The API is defined to require a 'data' value, so we will always
  // create a BinaryValue, even if it's zero-length.
  if (bytes_read < 0)
    bytes_read = 0;
  result->SetInteger(kBytesReadKey, bytes_read);
  result->Set(kDataKey, base::BinaryValue::CreateWithCopiedBuffer(
      io_buffer->data(), bytes_read));
  SetResult(result);
}

bool SerialReadFunction::Respond() {
  return true;
}

SerialWriteFunction::SerialWriteFunction()
    : io_buffer_(NULL), io_buffer_size_(0) {
}

SerialWriteFunction::~SerialWriteFunction() {
}

bool SerialWriteFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::Write::Params::Create(*args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());

  io_buffer_size_ = params_->data.size();
  io_buffer_ = new net::WrappedIOBuffer(params_->data.data());

  return true;
}

void SerialWriteFunction::Work() {
  int bytes_written = -1;
  SerialConnection* serial_connection = GetSerialConnection(
      params_->connection_id);
  if (serial_connection)
    bytes_written = serial_connection->Write(io_buffer_, io_buffer_size_);
  else
    error_ = kSerialConnectionNotFoundError;

  DictionaryValue* result = new DictionaryValue();
  result->SetInteger(kBytesWrittenKey, bytes_written);
  SetResult(result);
}

bool SerialWriteFunction::Respond() {
  return true;
}

SerialFlushFunction::SerialFlushFunction() {
}

SerialFlushFunction::~SerialFlushFunction() {
}

bool SerialFlushFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::Flush::Params::Create(*args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());
  return true;
}

void SerialFlushFunction::Work() {
  bool flush_result = false;
  SerialConnection* serial_connection = GetSerialConnection(
      params_->connection_id);
  if (serial_connection) {
    serial_connection->Flush();
    flush_result = true;
  }

  SetResult(Value::CreateBooleanValue(flush_result));
}

bool SerialFlushFunction::Respond() {
  return true;
}

SerialGetControlSignalsFunction::SerialGetControlSignalsFunction()
    : api_response_(false) {
}

SerialGetControlSignalsFunction::~SerialGetControlSignalsFunction() {
}

bool SerialGetControlSignalsFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::GetControlSignals::Params::Create(
      *args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());

  return true;
}

void SerialGetControlSignalsFunction::Work() {
  DictionaryValue *result = new DictionaryValue();
  SerialConnection* serial_connection = GetSerialConnection(
      params_->connection_id);
  if (serial_connection) {
    SerialConnection::ControlSignals control_signals = { 0 };
    if (serial_connection->GetControlSignals(control_signals)) {
      api_response_ = true;
      result->SetBoolean(kDcdKey, control_signals.dcd);
      result->SetBoolean(kCtsKey, control_signals.cts);
    } else {
      error_ = kErrorGetControlSignalsFailed;
    }
  } else {
    error_ = kSerialConnectionNotFoundError;
    result->SetBoolean(kSuccessKey, false);
  }

  SetResult(result);
}

bool SerialGetControlSignalsFunction::Respond() {
  return api_response_;
}

SerialSetControlSignalsFunction::SerialSetControlSignalsFunction() {
}

SerialSetControlSignalsFunction::~SerialSetControlSignalsFunction() {
}

bool SerialSetControlSignalsFunction::Prepare() {
  set_work_thread_id(BrowserThread::FILE);

  params_ = api::serial::SetControlSignals::Params::Create(
      *args_);
  EXTENSION_FUNCTION_VALIDATE(params_.get());

  return true;
}

void SerialSetControlSignalsFunction::Work() {
  SerialConnection* serial_connection = GetSerialConnection(
      params_->connection_id);
  if (serial_connection) {
    SerialConnection::ControlSignals control_signals = { 0 };
    control_signals.should_set_dtr = params_->options.dtr.get() != NULL;
    if (control_signals.should_set_dtr)
      control_signals.dtr = *(params_->options.dtr);
    control_signals.should_set_rts = params_->options.rts.get() != NULL;
    if (control_signals.should_set_rts)
      control_signals.rts = *(params_->options.rts);
    if (serial_connection->SetControlSignals(control_signals)) {
      SetResult(Value::CreateBooleanValue(true));
    } else {
      error_ = kErrorSetControlSignalsFailed;
      SetResult(Value::CreateBooleanValue(false));
    }
  } else {
    error_ = kSerialConnectionNotFoundError;
    SetResult(Value::CreateBooleanValue(false));
  }
}

bool SerialSetControlSignalsFunction::Respond() {
  return true;
}

}  // namespace extensions