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 #ifndef CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/platform_file.h"
     13 #include "chrome/browser/extensions/api/api_resource.h"
     14 #include "chrome/browser/extensions/api/api_resource_manager.h"
     15 #include "content/public/browser/browser_thread.h"
     16 #include "net/base/io_buffer.h"
     17 
     18 using content::BrowserThread;
     19 
     20 namespace extensions {
     21 
     22 extern const char kSerialConnectionNotFoundError[];
     23 
     24 // Encapsulates an open serial port. Platform-specific implementations are in
     25 // _win and _posix versions of the the .cc file.
     26 class SerialConnection : public ApiResource {
     27  public:
     28   SerialConnection(const std::string& port,
     29                    int bitrate,
     30                    const std::string& owner_extension_id);
     31   virtual ~SerialConnection();
     32 
     33   virtual bool Open();
     34   virtual void Close();
     35   virtual void Flush();
     36 
     37   virtual int Read(scoped_refptr<net::IOBufferWithSize> io_buffer);
     38   virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count);
     39 
     40   struct ControlSignals {
     41     // Sent from workstation to device. The should_set_ values indicate whether
     42     // SetControlSignals should change the given signal (true) or else leave it
     43     // as-is (false).
     44     bool should_set_dtr;
     45     bool dtr;
     46     bool should_set_rts;
     47     bool rts;
     48 
     49     // Received by workstation from device. DCD (Data Carrier Detect) is
     50     // equivalent to RLSD (Receive Line Signal Detect) on some platforms.
     51     bool dcd;
     52     bool cts;
     53   };
     54 
     55   virtual bool GetControlSignals(ControlSignals &control_signals);
     56   virtual bool SetControlSignals(const ControlSignals &control_signals);
     57 
     58   static const BrowserThread::ID kThreadId = BrowserThread::FILE;
     59 
     60  protected:
     61   // Do platform-specific work after a successful Open().
     62   bool PostOpen();
     63 
     64   // Platform-specific port name adapter
     65   static std::string MaybeFixUpPortName(const std::string &port_name);
     66 
     67  private:
     68   friend class ApiResourceManager<SerialConnection>;
     69   static const char* service_name() {
     70     return "SerialConnectionManager";
     71   }
     72   std::string port_;
     73   int bitrate_;
     74   base::PlatformFile file_;
     75 };
     76 
     77 }  // namespace extensions
     78 
     79 #endif  // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_
     80