OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ | |
6 #define TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ | |
7 | |
8 #include <vector> | |
9 | |
10 #include "base/callback_forward.h" | |
11 #include "base/macros.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "device/serial/serial.mojom.h" | |
15 #include "tools/battor_agent/battor_error.h" | |
16 #include "tools/battor_agent/battor_protocol_types.h" | |
17 | |
18 namespace device { | |
19 class SerialIoHandler; | |
20 } | |
21 namespace net { | |
22 class IOBuffer; | |
23 } | |
24 | |
25 namespace battor { | |
26 | |
27 // A BattOrConnection is a wrapper around the serial connection to the BattOr | |
28 // that handles conversion of a message to and from the byte-level BattOr | |
29 // protocol. | |
30 // | |
31 // At a high-level, all BattOr messages consist of: | |
32 // | |
33 // 0x00 (1 byte start marker) | |
34 // uint8_t (1 byte header indicating the message type) | |
35 // data (message data, with 0x00s and 0x01s escaped with 0x02) | |
36 // 0x01 (1 byte end marker) | |
37 // | |
38 // For a more in-depth description of the protocol, see http://bit.ly/1NvNVc3. | |
39 class BattOrConnection : public base::SupportsWeakPtr<BattOrConnection> { | |
40 public: | |
41 // The listener interface that must be implemented in order to interact with | |
42 // the BattOrConnection. | |
43 class Listener { | |
44 public: | |
45 virtual void OnConnectionOpened(bool success) = 0; | |
46 virtual void OnBytesSent(bool success) = 0; | |
47 virtual void OnBytesRead(bool success, | |
48 BattOrMessageType type, | |
49 scoped_ptr<std::vector<char>> bytes) = 0; | |
50 }; | |
51 | |
52 // Constructs a new BattOrConnection. We use a WeakPtr for the listener | |
53 // because it's possible for the SerialIoHandler's connect, read, or write to | |
54 // complete after we've been destroyed, in which case we just want to cancel | |
55 BattOrConnection( | |
56 const std::string& path, | |
57 Listener* listener, | |
58 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
59 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | |
60 virtual ~BattOrConnection(); | |
61 | |
62 // Initializes the serial connection and calls the listener's | |
63 // OnConnectionOpened() when complete. This function must be called before | |
64 // using the BattOrConnection. | |
65 void Open(); | |
66 bool IsOpen(); | |
67 // Closes the serial connection and releases any handles being held. | |
68 void Close(); | |
69 | |
70 // Sends the specified buffer over the serial connection and calls the | |
71 // listener's OnBytesSent() when complete. Note that bytes_to_send should not | |
72 // include the start, end, type, or escape bytes required by the BattOr | |
73 // protocol. | |
74 void SendBytes(BattOrMessageType type, | |
75 const void* buffer, | |
76 size_t bytes_to_send); | |
77 | |
78 // Reads the specified number of bytes from the serial connection and calls | |
79 // the listener's OnBytesRead() when complete. Note that the number of bytes | |
80 // requested should not include the start, end, or type bytes required by the | |
81 // BattOr protocol, and that this method may issue multiple read read requests | |
82 // if the message contains escape characters. | |
83 void ReadBytes(size_t bytes_to_read); | |
84 | |
85 // Flushes the serial connection to the BattOr. | |
86 void Flush(); | |
87 | |
88 protected: | |
89 // Overridden by the test to use a fake serial connection. | |
90 virtual scoped_refptr<device::SerialIoHandler> CreateIoHandler(); | |
91 | |
92 // IO handler capable of reading and writing from the serial connection. | |
93 scoped_refptr<device::SerialIoHandler> io_handler_; | |
94 | |
95 private: | |
96 void OnOpened(bool success); | |
97 | |
98 // Reads the specified number of additional bytes and adds them to the pending | |
99 // read buffer. | |
100 void ReadMoreBytes(size_t bytes_to_read); | |
101 | |
102 // Internal callback for when bytes are read. This method may trigger | |
103 // additional reads if any newly read bytes are escape bytes. | |
104 void OnBytesRead(int bytes_read, device::serial::ReceiveError error); | |
105 | |
106 // Internal callback for when bytes are sent. | |
107 void OnBytesSent(int bytes_sent, device::serial::SendError error); | |
108 | |
109 // The path of the BattOr. | |
110 std::string path_; | |
111 | |
112 // The listener receiving the results of the commands being executed. | |
113 Listener* listener_; | |
114 | |
115 // All bytes that have been read since the user requested a read. If multiple | |
116 // reads are required due to the presence of escape bytes, | |
117 // pending_read_buffer_ grows with each read. | |
118 scoped_ptr<std::vector<char>> pending_read_buffer_; | |
119 // The bytes that were read in just the last read. If multiple reads are | |
120 // required due to the presence of escape bytes, last_read_buffer_ only | |
121 // contains the results of the last read. | |
122 scoped_refptr<net::IOBuffer> last_read_buffer_; | |
123 // The number of bytes that we requested in the last read. | |
124 size_t pending_read_length_; | |
125 // The number of escape bytes that have already been read. | |
126 size_t pending_read_escape_byte_count_; | |
127 | |
128 // The total number of bytes that we're expecting to send. | |
129 size_t pending_write_length_; | |
130 | |
131 // Threads needed for serial communication. | |
132 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; | |
133 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | |
134 | |
135 DISALLOW_COPY_AND_ASSIGN(BattOrConnection); | |
136 }; | |
137 | |
138 } // namespace battor | |
139 | |
140 #endif // TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ | |
OLD | NEW |