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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_
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"
19 using content::BrowserThread;
21 namespace serial = extensions::api::serial;
23 namespace extensions {
25 extern const char kSerialConnectionNotFoundError[];
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 {
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();
41 virtual int Read(scoped_refptr<net::IOBufferWithSize> io_buffer);
42 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count);
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
53 // Received by workstation from device. DCD (Data Carrier Detect) is
54 // equivalent to RLSD (Receive Line Signal Detect) on some platforms.
59 virtual bool GetControlSignals(ControlSignals &control_signals);
60 virtual bool SetControlSignals(const ControlSignals &control_signals);
62 static const BrowserThread::ID kThreadId = BrowserThread::FILE;
65 // Do platform-specific work after a successful Open().
68 // Platform-specific port name adapter
69 static std::string MaybeFixUpPortName(const std::string &port_name);
72 friend class ApiResourceManager<SerialConnection>;
73 static const char* service_name() {
74 return "SerialConnectionManager";
78 serial::DataBit databit_;
79 serial::ParityBit parity_;
80 serial::StopBit stopbit_;
81 base::PlatformFile file_;
84 } // namespace extensions
86 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_