Chromium Code Reviews| Index: tools/battor_agent/battor_connection.h |
| diff --git a/tools/battor_agent/battor_connection.h b/tools/battor_agent/battor_connection.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..bdc0a2287ce646275154e16c6cb6a185fe5fc8cb |
| --- /dev/null |
| +++ b/tools/battor_agent/battor_connection.h |
| @@ -0,0 +1,100 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ |
| +#define TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "device/serial/serial.mojom.h" |
| +#include "tools/battor_agent/battor_error.h" |
| +#include "tools/battor_agent/battor_protocol_types.h" |
| + |
| +namespace device { |
| +class SerialIoHandler; |
| +} |
| +namespace net { |
| +class IOBuffer; |
| +} |
| + |
| +namespace battor { |
| + |
| +// A BattOrConnection is a wrapper around the serial connection to the BattOr |
| +// that handles conversion of a message to and from the byte-level BattOr |
| +// protocol. |
|
Zhen Wang
2015/12/14 23:39:46
Is there a description of BattOr protocol and how
charliea (OOO until 10-5)
2015/12/15 23:50:04
Done.
|
| +class BattOrConnection : public base::SupportsWeakPtr<BattOrConnection> { |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
Mostly a design thing here:
If you used the same L
charliea (OOO until 10-5)
2015/12/15 23:50:04
I thought about this for a while (obviously, that'
|
| + public: |
| + typedef base::Callback<void(bool success)> SendCallback; |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
in C++ using "using" is the preferred way for this
charliea (OOO until 10-5)
2015/12/15 23:50:04
Ack.
This actually went away after implementing
|
| + typedef base::Callback<void(bool success, |
| + BattOrMessageType type, |
| + scoped_ptr<std::vector<char>>)> ReadCallback; |
| + |
| + BattOrConnection( |
| + scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
| + virtual ~BattOrConnection(); |
| + |
| + // Initializes the serial connection and calls the callback when complete. |
| + void Connect(const std::string& path, |
| + const base::Callback<void(bool success)> callback); |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
Ironically this has the same signature of SendCall
charliea (OOO until 10-5)
2015/12/15 23:50:04
Acknowledged. Not completely sure what my reasonin
|
| + |
| + // Sends the specified message over the serial connection and calls the |
| + // callback when complete. Note that the number of bytes requested should |
| + // not include the start, end, or type bytes required by the BattOr protocol. |
| + void SendBytes(const SendCallback& callback, |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
Small nit: I'd move the callback to be the last ar
charliea (OOO until 10-5)
2015/12/15 23:50:04
Ack. I ended up going with the Listener interface,
|
| + BattOrMessageType type, |
| + const char* bytes, |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
typically I see this as "const void*".
It typicall
charliea (OOO until 10-5)
2015/12/15 23:50:04
Done.
|
| + uint16_t bytes_to_send); |
| + |
| + // Reads the specified number of bytes from the serial connection and calls |
| + // the callback when complete. Note that the number of bytes requested should |
| + // not include the start, end, or type bytes required by the BattOr protocol, |
| + // and that this method may issue multiple read read requests if the message |
| + // contains escape characters. |
| + void ReadBytes(const ReadCallback& callback, uint16_t bytes_to_read); |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
ditto here about ordering
charliea (OOO until 10-5)
2015/12/15 23:50:04
(see above)
|
| + |
| + // Flushes the serial connection with the BattOr. |
| + void Flush(); |
| + |
| + protected: |
|
Primiano Tucci (use gerrit)
2015/12/15 11:07:20
why protected? Is there anything extending this cl
charliea (OOO until 10-5)
2015/12/15 23:50:04
In battor_connection_unittest, we override CreateI
Primiano Tucci (use gerrit)
2015/12/16 16:47:43
ah got it, thanks
Maybe add a one line comment say
charliea (OOO until 10-5)
2015/12/16 18:27:55
Done.
|
| + virtual scoped_refptr<device::SerialIoHandler> CreateIoHandler(); |
| + |
| + // IO handler capable of reading and writing from the serial connection. |
| + scoped_refptr<device::SerialIoHandler> io_handler_; |
| + |
| + private: |
| + // A version of ReadMoreBytes that passes along a vector of bytes that have |
| + // already been read. This is useful when we need to issue multiple reads |
| + // because there were escape bytes that caused us to not read enough data |
| + // in the first read. |
| + void ReadMoreBytes(const ReadCallback& callback, |
| + uint16_t bytes_to_read, |
| + scoped_ptr<std::vector<char>> bytes_already_read); |
| + // Internal callback for when bytes are sent. |
| + void OnBytesSent(const BattOrConnection::SendCallback& callback, |
| + int expected_bytes_sent, |
| + int bytes_sent, |
| + device::serial::SendError error); |
| + |
| + // Internal callback for when bytes are read. This method may trigger |
| + // additional reads if any newly read bytes are escape bytes. |
| + void OnBytesRead(const BattOrConnection::ReadCallback& callback, |
| + scoped_ptr<std::vector<char>> bytes_already_read, |
| + scoped_refptr<net::IOBuffer> buffer, |
| + int expected_bytes_read, |
| + int bytes_read, |
| + device::serial::ReceiveError error); |
| + |
| + // Threads needed for serial communication. |
| + scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; |
| + scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BattOrConnection); |
| +}; |
| + |
| +} // namespace battor |
| + |
| +#endif // TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ |