summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/serial/serial_connection_win.cc
blob: 06c8b59307be538cb14d46577a2213d71951a15c (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
// 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_connection.h"

#include <string>
#include <windows.h>

namespace extensions {

bool SerialConnection::PostOpen() {
  // Set timeouts so that reads return immediately with whatever could be read
  // without blocking.
  COMMTIMEOUTS timeouts = { 0 };
  timeouts.ReadIntervalTimeout = MAXDWORD;
  if (!::SetCommTimeouts(file_, &timeouts))
    return false;

  DCB dcb = { 0 };
  dcb.DCBlength = sizeof(dcb);
  if (!GetCommState(file_, &dcb))
    return false;

  if (bitrate_ >= 0) {
    bool speed_found = true;
    DWORD speed = CBR_9600;
    switch (bitrate_) {
      case 110: speed = CBR_110; break;
      case 300: speed = CBR_300; break;
      case 600: speed = CBR_600; break;
      case 1200: speed = CBR_1200; break;
      case 2400: speed = CBR_2400; break;
      case 4800: speed = CBR_4800; break;
      case 9600: speed = CBR_9600; break;
      case 14400: speed = CBR_14400; break;
      case 19200: speed = CBR_19200; break;
      case 38400: speed = CBR_38400; break;
      case 57600: speed = CBR_57600; break;
      case 115200: speed = CBR_115200; break;
      case 128000: speed = CBR_128000; break;
      case 256000: speed = CBR_256000; break;
      default: speed_found = false; break;
    }
    if (speed_found)
      dcb.BaudRate = speed;
  }
  dcb.ByteSize = 8;
  dcb.StopBits = ONESTOPBIT;
  dcb.Parity = NOPARITY;
  if (!SetCommState(file_, &dcb))
    return false;

  return true;
}

bool SerialConnection::GetControlSignals(ControlSignals &control_signals) {
  DWORD dwModemStatus;
  if (!GetCommModemStatus(file_, &dwModemStatus))
    return false;
  control_signals.dcd = (MS_RLSD_ON & dwModemStatus) != 0;
  control_signals.cts = (MS_CTS_ON & dwModemStatus) != 0;
  return true;
}

bool SerialConnection::
SetControlSignals(const ControlSignals &control_signals) {
  if (control_signals.should_set_dtr) {
    if (!EscapeCommFunction(file_, control_signals.dtr ? SETDTR : CLRDTR))
      return false;
  }
  if (control_signals.should_set_rts) {
    if (!EscapeCommFunction(file_, control_signals.rts ? SETRTS : CLRRTS))
      return false;
  }
  return true;
}

}  // namespace extensions