| 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 <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_path.h" |
| 10 #include "base/string_util.h" |
| 11 |
| 9 namespace extensions { | 12 namespace extensions { |
| 10 | 13 |
| 11 const char kSerialConnectionNotFoundError[] = "Serial connection not found"; | 14 const char kSerialConnectionNotFoundError[] = "Serial connection not found"; |
| 12 | 15 |
| 16 SerialConnection::SerialConnection(const std::string& port, |
| 17 APIResourceEventNotifier* event_notifier) |
| 18 : APIResource(APIResource::SerialConnectionResource, event_notifier), |
| 19 port_(port), |
| 20 file_(base::kInvalidPlatformFileValue) { |
| 21 } |
| 22 |
| 23 SerialConnection::~SerialConnection() { |
| 24 Close(); |
| 25 } |
| 26 |
| 27 bool SerialConnection::Open() { |
| 28 bool created = false; |
| 29 |
| 30 // It's the responsibility of the API wrapper around SerialConnection to |
| 31 // validate the supplied path against the set of valid port names, and |
| 32 // it is a reasonable assumption that serial port names are ASCII. |
| 33 CHECK(IsStringASCII(port_)); |
| 34 FilePath file_path(FilePath::FromUTF8Unsafe(port_)); |
| 35 |
| 36 file_ = base::CreatePlatformFile(file_path, |
| 37 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | |
| 38 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 39 base::PLATFORM_FILE_EXCLUSIVE_WRITE | |
| 40 base::PLATFORM_FILE_TERMINAL_DEVICE, &created, NULL); |
| 41 if (file_ == base::kInvalidPlatformFileValue) { |
| 42 return false; |
| 43 } |
| 44 return PostOpen(); |
| 45 } |
| 46 |
| 47 void SerialConnection::Close() { |
| 48 if (file_ != base::kInvalidPlatformFileValue) { |
| 49 base::ClosePlatformFile(file_); |
| 50 file_ = base::kInvalidPlatformFileValue; |
| 51 } |
| 52 } |
| 53 |
| 54 int SerialConnection::Read(uint8* byte) { |
| 55 DCHECK(byte); |
| 56 return base::ReadPlatformFileAtCurrentPos(file_, |
| 57 reinterpret_cast<char*>(byte), 1); |
| 58 } |
| 59 |
| 60 int SerialConnection::Write(scoped_refptr<net::IOBuffer> io_buffer, |
| 61 int byte_count) { |
| 62 DCHECK(io_buffer->data()); |
| 63 DCHECK(byte_count >= 0); |
| 64 return base::WritePlatformFileAtCurrentPos(file_, |
| 65 io_buffer->data(), byte_count); |
| 66 } |
| 67 |
| 13 } // namespace extensions | 68 } // namespace extensions |
| OLD | NEW |