1 // Copyright 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/callback.h"
12 #include "base/files/file.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/extensions/api/serial/serial_io_handler.h"
18 #include "chrome/common/extensions/api/serial.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "extensions/browser/api/api_resource.h"
21 #include "extensions/browser/api/api_resource_manager.h"
22 #include "net/base/file_stream.h"
24 using content::BrowserThread
;
26 namespace extensions
{
28 // Encapsulates an open serial port. Platform-specific implementations are in
29 // _win and _posix versions of the the .cc file.
30 // NOTE: Instances of this object should only be constructed on the IO thread,
31 // and all methods should only be called on the IO thread unless otherwise
33 class SerialConnection
: public ApiResource
,
34 public base::SupportsWeakPtr
<SerialConnection
> {
36 typedef base::Callback
<void(bool success
)> OpenCompleteCallback
;
38 // This is the callback type expected by Receive. Note that an error result
39 // does not necessarily imply an empty |data| string, since a receive may
40 // complete partially before being interrupted by an error condition.
41 typedef base::Callback
<
42 void(const std::string
& data
, api::serial::ReceiveError error
)>
43 ReceiveCompleteCallback
;
45 // This is the callback type expected by Send. Note that an error result
46 // does not necessarily imply 0 bytes sent, since a send may complete
47 // partially before being interrupted by an error condition.
48 typedef base::Callback
<void(int bytes_sent
, api::serial::SendError error
)>
51 SerialConnection(const std::string
& port
,
52 const std::string
& owner_extension_id
);
53 virtual ~SerialConnection();
55 // ApiResource override.
56 virtual bool IsPersistent() const OVERRIDE
;
58 void set_persistent(bool persistent
) { persistent_
= persistent
; }
59 bool persistent() const { return persistent_
; }
61 void set_name(const std::string
& name
) { name_
= name
; }
62 const std::string
& name() const { return name_
; }
64 void set_buffer_size(int buffer_size
);
65 int buffer_size() const { return buffer_size_
; }
67 void set_receive_timeout(int receive_timeout
);
68 int receive_timeout() const { return receive_timeout_
; }
70 void set_send_timeout(int send_timeout
);
71 int send_timeout() const { return send_timeout_
; }
73 void set_paused(bool paused
);
74 bool paused() const { return paused_
; }
76 // Initiates an asynchronous Open of the device. It is the caller's
77 // responsibility to ensure that this SerialConnection stays alive
78 // until |callback| is run.
79 virtual void Open(const OpenCompleteCallback
& callback
);
81 // Initiate a Close of the device. The SerialConnection instance will
82 // have its internal state reset synchronously upon calling this, but
83 // the underlying OS handle will be closed asynchronously.
86 // Begins an asynchronous receive operation. Calling this while a Receive
87 // is already pending is a no-op and returns |false| without calling
89 virtual bool Receive(const ReceiveCompleteCallback
& callback
);
91 // Begins an asynchronous send operation. Calling this while a Send
92 // is already pending is a no-op and returns |false| without calling
94 virtual bool Send(const std::string
& data
,
95 const SendCompleteCallback
& callback
);
97 // Flushes input and output buffers.
98 virtual bool Flush() const;
100 // Configures some subset of port options for this connection.
101 // Omitted options are unchanged. Returns |true| iff the configuration
102 // changes were successful.
103 virtual bool Configure(const api::serial::ConnectionOptions
& options
);
105 // Connection configuration query. Fills values in an existing
106 // ConnectionInfo. Returns |true| iff the connection's information
107 // was successfully retrieved.
108 virtual bool GetInfo(api::serial::ConnectionInfo
* info
) const;
110 // Reads current control signals (DCD, CTS, etc.) into an existing
111 // DeviceControlSignals structure. Returns |true| iff the signals were
112 // successfully read.
113 virtual bool GetControlSignals(
114 api::serial::DeviceControlSignals
* control_signals
) const;
116 // Sets one or more control signals (DTR and/or RTS). Returns |true| iff
117 // the signals were successfully set. Unininitialized flags in the
118 // HostControlSignals structure are left unchanged.
119 virtual bool SetControlSignals(
120 const api::serial::HostControlSignals
& control_signals
);
122 static const BrowserThread::ID kThreadId
= BrowserThread::IO
;
125 // Overrides |io_handler_| for testing.
126 virtual void SetIoHandlerForTest(scoped_refptr
<SerialIoHandler
> handler
);
128 // Performs platform-specific, one-time port configuration on open.
131 // Performs platform-specific port configuration. Returns |true| iff
132 // configuration was successful.
133 bool ConfigurePort(const api::serial::ConnectionOptions
& options
);
135 // Performs a platform-specific port configuration query. Fills values in an
136 // existing ConnectionInfo. Returns |true| iff port configuration was
137 // successfully retrieved.
138 bool GetPortInfo(api::serial::ConnectionInfo
* info
) const;
140 // Possibly fixes up a serial port path name in a platform-specific manner.
141 static std::string
MaybeFixUpPortName(const std::string
& port_name
);
144 friend class ApiResourceManager
<SerialConnection
>;
145 static const char* service_name() { return "SerialConnectionManager"; }
147 // Encapsulates a cancelable, delayed timeout task. Posts a delayed
148 // task upon construction and implicitly cancels the task upon
149 // destruction if it hasn't run yet.
152 TimeoutTask(const base::Closure
& closure
, const base::TimeDelta
& delay
);
158 base::WeakPtrFactory
<TimeoutTask
> weak_factory_
;
159 base::Closure closure_
;
160 base::TimeDelta delay_
;
163 // Continues an Open operation on the FILE thread.
166 // Finalizes an Open operation (continued from StartOpen) on the IO thread.
167 void FinishOpen(base::File file
);
169 // Continues a Close operation on the FILE thread.
170 static void DoClose(base::File port
);
172 // Handles a receive timeout.
173 void OnReceiveTimeout();
175 // Handles a send timeout.
176 void OnSendTimeout();
178 // Receives read completion notification from the |io_handler_|.
179 void OnAsyncReadComplete(const std::string
& data
,
180 api::serial::ReceiveError error
);
182 // Receives write completion notification from the |io_handler_|.
183 void OnAsyncWriteComplete(int bytes_sent
, api::serial::SendError error
);
185 // The pathname of the serial device.
188 // File for the opened serial device. This value is only modified from the IO
192 // Flag indicating whether or not the connection should persist when
193 // its host app is suspended.
196 // User-specified connection name.
199 // Size of the receive buffer.
202 // Amount of time (in ms) to wait for a Read to succeed before triggering a
203 // timeout response via onReceiveError.
204 int receive_timeout_
;
206 // Amount of time (in ms) to wait for a Write to succeed before triggering
207 // a timeout response.
210 // Flag indicating that the connection is paused. A paused connection will not
211 // raise new onReceive events.
214 // Callback to handle the completion of a pending Open() request.
215 OpenCompleteCallback open_complete_
;
217 // Callback to handle the completion of a pending Receive() request.
218 ReceiveCompleteCallback receive_complete_
;
220 // Callback to handle the completion of a pending Send() request.
221 SendCompleteCallback send_complete_
;
223 // Closure which will trigger a receive timeout unless cancelled. Reset on
224 // initialization and after every successful Receive().
225 scoped_ptr
<TimeoutTask
> receive_timeout_task_
;
227 // Write timeout closure. Reset on initialization and after every successful
229 scoped_ptr
<TimeoutTask
> send_timeout_task_
;
231 // Asynchronous I/O handler.
232 scoped_refptr
<SerialIoHandler
> io_handler_
;
235 } // namespace extensions
237 #endif // CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_CONNECTION_H_