Home | History | Annotate | Download | only in serial
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/extensions/api/serial/serial_connection.h"
      6 
      7 #include <string>
      8 
      9 #include "base/files/file_path.h"
     10 #include "base/lazy_instance.h"
     11 #include "base/strings/string_util.h"
     12 #include "chrome/browser/extensions/api/api_resource_manager.h"
     13 
     14 namespace extensions {
     15 
     16 const char kSerialConnectionNotFoundError[] = "Serial connection not found";
     17 
     18 static base::LazyInstance<ProfileKeyedAPIFactory<
     19     ApiResourceManager<SerialConnection> > >
     20         g_factory = LAZY_INSTANCE_INITIALIZER;
     21 
     22 // static
     23 template <>
     24 ProfileKeyedAPIFactory<ApiResourceManager<SerialConnection> >*
     25 ApiResourceManager<SerialConnection>::GetFactoryInstance() {
     26   return &g_factory.Get();
     27 }
     28 
     29 SerialConnection::SerialConnection(const std::string& port, int bitrate,
     30                                    const std::string& owner_extension_id)
     31     : ApiResource(owner_extension_id), port_(port), bitrate_(bitrate),
     32       file_(base::kInvalidPlatformFileValue) {
     33   CHECK_GE(bitrate, 0);
     34 }
     35 
     36 SerialConnection::~SerialConnection() {
     37   Close();
     38 }
     39 
     40 bool SerialConnection::Open() {
     41   bool created = false;
     42 
     43   // It's the responsibility of the API wrapper around SerialConnection to
     44   // validate the supplied path against the set of valid port names, and
     45   // it is a reasonable assumption that serial port names are ASCII.
     46   CHECK(IsStringASCII(port_));
     47   base::FilePath file_path(
     48       base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port_)));
     49 
     50   file_ = base::CreatePlatformFile(file_path,
     51     base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ |
     52     base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_READ |
     53     base::PLATFORM_FILE_EXCLUSIVE_WRITE |
     54     base::PLATFORM_FILE_TERMINAL_DEVICE, &created, NULL);
     55   if (file_ == base::kInvalidPlatformFileValue) {
     56     return false;
     57   }
     58   return PostOpen();
     59 }
     60 
     61 void SerialConnection::Close() {
     62   if (file_ != base::kInvalidPlatformFileValue) {
     63     base::ClosePlatformFile(file_);
     64     file_ = base::kInvalidPlatformFileValue;
     65   }
     66 }
     67 
     68 int SerialConnection::Read(scoped_refptr<net::IOBufferWithSize> io_buffer) {
     69   DCHECK(io_buffer->data());
     70   return base::ReadPlatformFileAtCurrentPos(file_, io_buffer->data(),
     71                                             io_buffer->size());
     72 }
     73 
     74 int SerialConnection::Write(scoped_refptr<net::IOBuffer> io_buffer,
     75                             int byte_count) {
     76   DCHECK(io_buffer->data());
     77   DCHECK_GE(byte_count, 0);
     78   return base::WritePlatformFileAtCurrentPos(file_, io_buffer->data(),
     79                                              byte_count);
     80 }
     81 
     82 void SerialConnection::Flush() {
     83   base::FlushPlatformFile(file_);
     84 }
     85 
     86 }  // namespace extensions
     87