| 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 <errno.h> | |
| 8 #include <fcntl.h> | |
| 9 #include <stdio.h> | |
| 10 #include <string> | |
| 11 #include <termios.h> | 7 #include <termios.h> |
| 12 #include <unistd.h> | |
| 13 | |
| 14 #include "base/file_path.h" | |
| 15 #include "base/file_util.h" | |
| 16 #include "base/string_util.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | 8 |
| 21 namespace extensions { | 9 namespace extensions { |
| 22 | 10 |
| 23 SerialConnection::SerialConnection(const std::string& port, | 11 bool SerialConnection::PostOpen() { |
| 24 APIResourceEventNotifier* event_notifier) | 12 struct termios options; |
| 25 : APIResource(APIResource::SerialConnectionResource, event_notifier), | |
| 26 port_(port), fd_(0) { | |
| 27 } | |
| 28 | 13 |
| 29 SerialConnection::~SerialConnection() { | 14 // Start with existing options and modify. |
| 30 } | 15 tcgetattr(file_, &options); |
| 31 | 16 |
| 32 bool SerialConnection::Open() { | 17 // Bitrate 57,600 |
| 33 fd_ = open(port_.c_str(), O_RDWR | O_NOCTTY | O_NDELAY); | 18 cfsetispeed(&options, B57600); |
| 34 return (fd_ > 0); | 19 cfsetospeed(&options, B57600); |
| 35 } | |
| 36 | 20 |
| 37 void SerialConnection::Close() { | 21 // 8N1 |
| 38 if (fd_ > 0) { | 22 options.c_cflag &= ~PARENB; |
| 39 close(fd_); | 23 options.c_cflag &= ~CSTOPB; |
| 40 fd_ = 0; | 24 options.c_cflag &= ~CSIZE; |
| 41 } | 25 options.c_cflag |= CS8; |
| 42 } | 26 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); |
| 43 | 27 |
| 44 int SerialConnection::Read(uint8* byte) { | 28 // Enable receiver and set local mode |
| 45 return read(fd_, byte, 1); | 29 // See http://www.easysw.com/~mike/serial/serial.html to understand. |
| 46 } | 30 options.c_cflag |= (CLOCAL | CREAD); |
| 47 | 31 |
| 48 int SerialConnection::Write(scoped_refptr<net::IOBuffer> io_buffer, | 32 // Write the options. |
| 49 int byte_count) { | 33 tcsetattr(file_, TCSANOW, &options); |
| 50 return write(fd_, io_buffer.get(), byte_count); | 34 |
| 35 return true; |
| 51 } | 36 } |
| 52 | 37 |
| 53 } // namespace extensions | 38 } // namespace extensions |
| OLD | NEW |