| 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_connection.h" | 5 #include "chrome/browser/extensions/api/serial/serial_connection.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 namespace { |
| 14 int getBaudRate(int bitrate_) { |
| 15 switch (bitrate_) { |
| 16 case 110: return CBR_110; |
| 17 case 300: return CBR_300; |
| 18 case 600: return CBR_600; |
| 19 case 1200: return CBR_1200; |
| 20 case 2400: return CBR_2400; |
| 21 case 4800: return CBR_4800; |
| 22 case 9600: return CBR_9600; |
| 23 case 14400: return CBR_14400; |
| 24 case 19200: return CBR_19200; |
| 25 case 38400: return CBR_38400; |
| 26 case 57600: return CBR_57600; |
| 27 case 115200: return CBR_115200; |
| 28 case 128000: return CBR_128000; |
| 29 case 256000: return CBR_256000; |
| 30 default: return CBR_9600; |
| 31 } |
| 32 } |
| 33 |
| 34 int getDataBit(serial::DataBit databit) { |
| 35 switch (databit) { |
| 36 case serial::DATA_BIT_SEVENBIT: |
| 37 return 7; |
| 38 case serial::DATA_BIT_EIGHTBIT: |
| 39 default: |
| 40 return 8; |
| 41 } |
| 42 } |
| 43 |
| 44 int getParity(serial::ParityBit parity) { |
| 45 switch (parity) { |
| 46 case serial::PARITY_BIT_EVENPARITY: |
| 47 return EVENPARITY; |
| 48 case serial::PARITY_BIT_ODDPARITY: |
| 49 return SPACEPARITY; |
| 50 case serial::PARITY_BIT_NOPARITY: |
| 51 default: |
| 52 return NOPARITY; |
| 53 } |
| 54 } |
| 55 |
| 56 int getStopBit(serial::StopBit stopbit) { |
| 57 switch (stopbit) { |
| 58 case serial::STOP_BIT_TWOSTOPBIT: |
| 59 return TWOSTOPBITS; |
| 60 case serial::STOP_BIT_ONESTOPBIT: |
| 61 default: |
| 62 return ONESTOPBIT; |
| 63 } |
| 64 } |
| 65 } // namespace |
| 66 |
| 13 bool SerialConnection::PostOpen() { | 67 bool SerialConnection::PostOpen() { |
| 14 // Set timeouts so that reads return immediately with whatever could be read | 68 // Set timeouts so that reads return immediately with whatever could be read |
| 15 // without blocking. | 69 // without blocking. |
| 16 COMMTIMEOUTS timeouts = { 0 }; | 70 COMMTIMEOUTS timeouts = { 0 }; |
| 17 timeouts.ReadIntervalTimeout = MAXDWORD; | 71 timeouts.ReadIntervalTimeout = MAXDWORD; |
| 18 if (!::SetCommTimeouts(file_, &timeouts)) | 72 if (!::SetCommTimeouts(file_, &timeouts)) |
| 19 return false; | 73 return false; |
| 20 | 74 |
| 21 DCB dcb = { 0 }; | 75 DCB dcb = { 0 }; |
| 22 dcb.DCBlength = sizeof(dcb); | 76 dcb.DCBlength = sizeof(dcb); |
| 23 if (!GetCommState(file_, &dcb)) | 77 if (!GetCommState(file_, &dcb)) |
| 24 return false; | 78 return false; |
| 25 | 79 |
| 26 if (bitrate_ >= 0) { | 80 dcb.BaudRate = getBaudRate(bitrate_); |
| 27 bool speed_found = true; | 81 dcb.ByteSize = getDataBit(databit_); |
| 28 DWORD speed = CBR_9600; | 82 dcb.Parity = getParity(parity_); |
| 29 switch (bitrate_) { | 83 dcb.StopBits = getStopBit(stopbit_); |
| 30 case 110: speed = CBR_110; break; | 84 |
| 31 case 300: speed = CBR_300; break; | |
| 32 case 600: speed = CBR_600; break; | |
| 33 case 1200: speed = CBR_1200; break; | |
| 34 case 2400: speed = CBR_2400; break; | |
| 35 case 4800: speed = CBR_4800; break; | |
| 36 case 9600: speed = CBR_9600; break; | |
| 37 case 14400: speed = CBR_14400; break; | |
| 38 case 19200: speed = CBR_19200; break; | |
| 39 case 38400: speed = CBR_38400; break; | |
| 40 case 57600: speed = CBR_57600; break; | |
| 41 case 115200: speed = CBR_115200; break; | |
| 42 case 128000: speed = CBR_128000; break; | |
| 43 case 256000: speed = CBR_256000; break; | |
| 44 default: speed_found = false; break; | |
| 45 } | |
| 46 if (speed_found) | |
| 47 dcb.BaudRate = speed; | |
| 48 } | |
| 49 dcb.ByteSize = 8; | |
| 50 dcb.StopBits = ONESTOPBIT; | |
| 51 dcb.Parity = NOPARITY; | |
| 52 if (!SetCommState(file_, &dcb)) | 85 if (!SetCommState(file_, &dcb)) |
| 53 return false; | 86 return false; |
| 54 | 87 |
| 55 return true; | 88 return true; |
| 56 } | 89 } |
| 57 | 90 |
| 58 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) { | 91 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) { |
| 59 DWORD dwModemStatus; | 92 DWORD dwModemStatus; |
| 60 if (!GetCommModemStatus(file_, &dwModemStatus)) | 93 if (!GetCommModemStatus(file_, &dwModemStatus)) |
| 61 return false; | 94 return false; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 81 const std::string &port_name) { | 114 const std::string &port_name) { |
| 82 // For COM numbers less than 9, CreateFile is called with a string such as | 115 // For COM numbers less than 9, CreateFile is called with a string such as |
| 83 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added. | 116 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added. |
| 84 if (port_name.length() > std::string("COM9").length()) | 117 if (port_name.length() > std::string("COM9").length()) |
| 85 return std::string("\\\\.\\").append(port_name); | 118 return std::string("\\\\.\\").append(port_name); |
| 86 | 119 |
| 87 return port_name; | 120 return port_name; |
| 88 } | 121 } |
| 89 | 122 |
| 90 } // namespace extensions | 123 } // namespace extensions |
| OLD | NEW |