Chromium Code Reviews| 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 // Callback for when the connection is opened. | |
|
Primiano Tucci (use gerrit)
2015/12/16 16:47:43
same thing here: probably these are self describin
charliea (OOO until 10-5)
2015/12/16 18:27:55
Done.
| |
| 46 virtual void OnConnectionOpened(bool success) = 0; | |
| 47 // Callback for when bytes are sent over the connection. | |
| 48 virtual void OnBytesSent(bool success) = 0; | |
| 49 // Callback for when bytes are read from the connection. | |
| 50 virtual void OnBytesRead(bool success, | |
| 51 BattOrMessageType type, | |
| 52 scoped_ptr<std::vector<char>> bytes) = 0; | |
| 53 }; | |
| 54 | |
| 55 BattOrConnection( | |
| 56 const std::string& path, | |
| 57 base::WeakPtr<Listener> listener, | |
|
Primiano Tucci (use gerrit)
2015/12/16 16:47:43
Maybe you want to add a comment to say why the lis
charliea (OOO until 10-5)
2015/12/16 18:27:55
Hmmm... I don't think that's quite right, but you
Primiano Tucci (use gerrit)
2015/12/16 19:41:51
Ok yeah let's clarify a bit this, let me see if I
| |
| 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 callback when complete. | |
| 63 // This function must be called before using the BattOrConnection. | |
| 64 void Open(); | |
| 65 bool IsOpen(); | |
| 66 // Closes the serial connection and releases any handles being held. | |
| 67 void Close(); | |
| 68 | |
| 69 // Sends the specified buffer over the serial connection and calls the | |
| 70 // listener's OnBytesSent() when complete. Note that bytes_to_send should not | |
| 71 // include the start, end, type, or escape bytes required by the BattOr | |
| 72 // protocol. | |
| 73 void SendBytes(BattOrMessageType type, | |
| 74 const void* buffer, | |
| 75 size_t bytes_to_send); | |
| 76 | |
| 77 // Reads the specified number of bytes from the serial connection and calls | |
| 78 // the listener's OnBytesRead() when complete. Note that the number of bytes | |
| 79 // requested should not include the start, end, or type bytes required by the | |
| 80 // BattOr protocol, and that this method may issue multiple read read requests | |
| 81 // if the message contains escape characters. | |
| 82 void ReadBytes(size_t bytes_to_read); | |
| 83 | |
| 84 // Flushes the serial connection to the BattOr. | |
| 85 void Flush(); | |
| 86 | |
| 87 protected: | |
| 88 virtual scoped_refptr<device::SerialIoHandler> CreateIoHandler(); | |
| 89 | |
| 90 // IO handler capable of reading and writing from the serial connection. | |
| 91 scoped_refptr<device::SerialIoHandler> io_handler_; | |
| 92 | |
| 93 private: | |
| 94 void OnOpened(bool success); | |
| 95 | |
| 96 // Reads the specified number of additional bytes and adds them to the pending | |
| 97 // read buffer. | |
| 98 void ReadMoreBytes(size_t bytes_to_read); | |
| 99 | |
| 100 // Internal callback for when bytes are sent. | |
| 101 void OnBytesSent(int bytes_sent, device::serial::SendError error); | |
| 102 | |
| 103 // Internal callback for when bytes are read. This method may trigger | |
| 104 // additional reads if any newly read bytes are escape bytes. | |
| 105 void OnBytesRead(int bytes_read, device::serial::ReceiveError error); | |
| 106 | |
| 107 // The path of the BattOr. | |
| 108 std::string path_; | |
| 109 | |
| 110 // The listener receiving the results of the commands being executed. | |
| 111 base::WeakPtr<Listener> listener_; | |
| 112 | |
| 113 // All bytes that have been read since the user requested a read. If multiple | |
| 114 // reads are required due to the presence of escape bytes, | |
| 115 // pending_read_buffer_ grows with each read. | |
| 116 scoped_ptr<std::vector<char>> pending_read_buffer_; | |
| 117 // The bytes that were read in just the last read. If multiple reads are | |
| 118 // required due to the presence of escape bytes, last_read_buffer_ only | |
| 119 // contains the results of the last read. | |
| 120 scoped_refptr<net::IOBuffer> last_read_buffer_; | |
| 121 // The number of bytes that we requested in the last read. | |
| 122 size_t pending_read_length_; | |
| 123 // The number of escape bytes that have already been read. | |
| 124 size_t pending_read_escape_byte_count_; | |
| 125 | |
| 126 // The total number of bytes that we're expecting to send. | |
| 127 size_t pending_write_length_; | |
| 128 | |
| 129 // Threads needed for serial communication. | |
| 130 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; | |
| 131 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | |
| 132 | |
| 133 DISALLOW_COPY_AND_ASSIGN(BattOrConnection); | |
| 134 }; | |
| 135 | |
| 136 } // namespace battor | |
| 137 | |
| 138 #endif // TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ | |
| OLD | NEW |