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 "base/callback_forward.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "device/serial/serial.mojom.h" | |
13 #include "tools/battor_agent/battor_error.h" | |
14 #include "tools/battor_agent/battor_protocol_types.h" | |
15 | |
16 namespace device { | |
17 class SerialIoHandler; | |
18 } | |
19 namespace net { | |
20 class IOBuffer; | |
21 } | |
22 | |
23 namespace battor { | |
24 | |
25 // A BattOrConnection is a wrapper around the serial connection to the BattOr | |
26 // that handles conversion of a message to and from the byte-level BattOr | |
27 // 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.
| |
28 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'
| |
29 public: | |
30 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
| |
31 typedef base::Callback<void(bool success, | |
32 BattOrMessageType type, | |
33 scoped_ptr<std::vector<char>>)> ReadCallback; | |
34 | |
35 BattOrConnection( | |
36 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
37 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | |
38 virtual ~BattOrConnection(); | |
39 | |
40 // Initializes the serial connection and calls the callback when complete. | |
41 void Connect(const std::string& path, | |
42 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
| |
43 | |
44 // Sends the specified message over the serial connection and calls the | |
45 // callback when complete. Note that the number of bytes requested should | |
46 // not include the start, end, or type bytes required by the BattOr protocol. | |
47 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,
| |
48 BattOrMessageType type, | |
49 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.
| |
50 uint16_t bytes_to_send); | |
51 | |
52 // Reads the specified number of bytes from the serial connection and calls | |
53 // the callback when complete. Note that the number of bytes requested should | |
54 // not include the start, end, or type bytes required by the BattOr protocol, | |
55 // and that this method may issue multiple read read requests if the message | |
56 // contains escape characters. | |
57 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)
| |
58 | |
59 // Flushes the serial connection with the BattOr. | |
60 void Flush(); | |
61 | |
62 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.
| |
63 virtual scoped_refptr<device::SerialIoHandler> CreateIoHandler(); | |
64 | |
65 // IO handler capable of reading and writing from the serial connection. | |
66 scoped_refptr<device::SerialIoHandler> io_handler_; | |
67 | |
68 private: | |
69 // A version of ReadMoreBytes that passes along a vector of bytes that have | |
70 // already been read. This is useful when we need to issue multiple reads | |
71 // because there were escape bytes that caused us to not read enough data | |
72 // in the first read. | |
73 void ReadMoreBytes(const ReadCallback& callback, | |
74 uint16_t bytes_to_read, | |
75 scoped_ptr<std::vector<char>> bytes_already_read); | |
76 // Internal callback for when bytes are sent. | |
77 void OnBytesSent(const BattOrConnection::SendCallback& callback, | |
78 int expected_bytes_sent, | |
79 int bytes_sent, | |
80 device::serial::SendError error); | |
81 | |
82 // Internal callback for when bytes are read. This method may trigger | |
83 // additional reads if any newly read bytes are escape bytes. | |
84 void OnBytesRead(const BattOrConnection::ReadCallback& callback, | |
85 scoped_ptr<std::vector<char>> bytes_already_read, | |
86 scoped_refptr<net::IOBuffer> buffer, | |
87 int expected_bytes_read, | |
88 int bytes_read, | |
89 device::serial::ReceiveError error); | |
90 | |
91 // Threads needed for serial communication. | |
92 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner_; | |
93 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(BattOrConnection); | |
96 }; | |
97 | |
98 } // namespace battor | |
99 | |
100 #endif // TOOLS_BATTOR_AGENT_BATTOR_CONNECTION_H_ | |
OLD | NEW |