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