OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_IMPL_H_ | 5 #ifndef TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_IMPL_H_ |
6 #define TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_IMPL_H_ | 6 #define TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_IMPL_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 BattOrConnection::Listener* listener, | 35 BattOrConnection::Listener* listener, |
36 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 36 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
37 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | 37 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
38 ~BattOrConnectionImpl() override; | 38 ~BattOrConnectionImpl() override; |
39 | 39 |
40 void Open() override; | 40 void Open() override; |
41 void Close() override; | 41 void Close() override; |
42 void SendBytes(BattOrMessageType type, | 42 void SendBytes(BattOrMessageType type, |
43 const void* buffer, | 43 const void* buffer, |
44 size_t bytes_to_send) override; | 44 size_t bytes_to_send) override; |
45 void ReadBytes(size_t bytes_to_read) override; | 45 void ReadMessage(size_t max_data_bytes_to_read) override; |
46 void Flush() override; | 46 void Flush() override; |
47 | 47 |
48 protected: | 48 protected: |
49 // Overridden by the test to use a fake serial connection. | 49 // Overridden by the test to use a fake serial connection. |
50 virtual scoped_refptr<device::SerialIoHandler> CreateIoHandler(); | 50 virtual scoped_refptr<device::SerialIoHandler> CreateIoHandler(); |
51 | 51 |
52 // IO handler capable of reading and writing from the serial connection. | 52 // IO handler capable of reading and writing from the serial connection. |
53 scoped_refptr<device::SerialIoHandler> io_handler_; | 53 scoped_refptr<device::SerialIoHandler> io_handler_; |
54 | 54 |
55 private: | 55 private: |
56 void OnOpened(bool success); | 56 void OnOpened(bool success); |
57 | 57 |
58 // Reads the specified number of additional bytes and adds them to the pending | 58 // Reads the specified number of additional bytes and adds them to the pending |
59 // read buffer. | 59 // read buffer. |
60 void ReadMoreBytes(size_t bytes_to_read); | 60 void BeginReadBytes(size_t bytes_to_read); |
61 | 61 |
62 // Internal callback for when bytes are read. This method may trigger | 62 // Internal callback for when bytes are read. This method may trigger |
63 // additional reads if any newly read bytes are escape bytes. | 63 // additional reads if any newly read bytes are escape bytes. |
64 void OnBytesRead(int bytes_read, device::serial::ReceiveError error); | 64 void OnBytesRead(int bytes_read, device::serial::ReceiveError error); |
65 | 65 |
| 66 void EndReadBytes(bool success, |
| 67 BattOrMessageType type, |
| 68 scoped_ptr<std::vector<char>> data); |
| 69 |
| 70 // Pulls off the next complete message from already_read_buffer_, returning |
| 71 // its type and contents (via out parameters) and whether a complete message |
| 72 // was able to be read (via the return value). |
| 73 bool ParseMessage(BattOrMessageType* type, std::vector<char>* data); |
| 74 |
66 // Internal callback for when bytes are sent. | 75 // Internal callback for when bytes are sent. |
67 void OnBytesSent(int bytes_sent, device::serial::SendError error); | 76 void OnBytesSent(int bytes_sent, device::serial::SendError error); |
68 | 77 |
69 // The path of the BattOr. | 78 // The path of the BattOr. |
70 std::string path_; | 79 std::string path_; |
71 | 80 |
72 // All bytes that have been read since the user requested a read. If multiple | 81 // All bytes that have already been read from the serial stream, but have not |
73 // reads are required due to the presence of escape bytes, | 82 // yet been given to the listener as a complete message. |
74 // pending_read_buffer_ grows with each read. | 83 std::vector<char> already_read_buffer_; |
75 scoped_ptr<std::vector<char>> pending_read_buffer_; | 84 // The bytes that were read in the last read. |
76 // The bytes that were read in just the last read. If multiple reads are | 85 scoped_refptr<net::IOBuffer> pending_read_buffer_; |
77 // required due to the presence of escape bytes, last_read_buffer_ only | 86 // The maximum number of bytes for the last read. |
78 // contains the results of the last read. | 87 size_t pending_read_max_length_; |
79 scoped_refptr<net::IOBuffer> last_read_buffer_; | |
80 // The number of bytes that we requested in the last read. | |
81 size_t pending_read_length_; | |
82 // The number of escape bytes that have already been read. | |
83 size_t pending_read_escape_byte_count_; | |
84 | 88 |
85 // The total number of bytes that we're expecting to send. | 89 // The total number of bytes that we're expecting to send. |
86 size_t pending_write_length_; | 90 size_t pending_write_length_; |
87 | 91 |
88 // Threads needed for serial communication. | 92 // Threads needed for serial communication. |
89 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; | 93 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; |
90 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | 94 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
91 | 95 |
92 DISALLOW_COPY_AND_ASSIGN(BattOrConnectionImpl); | 96 DISALLOW_COPY_AND_ASSIGN(BattOrConnectionImpl); |
93 }; | 97 }; |
94 | 98 |
95 } // namespace battor | 99 } // namespace battor |
96 | 100 |
97 #endif // TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_IMPL_H_ | 101 #endif // TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_IMPL_H_ |
OLD | NEW |