1 // Copyright 2014 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.
5 #include "device/serial/data_receiver.h"
10 #include "base/message_loop/message_loop.h"
14 // Represents a receive that is not yet fulfilled.
15 class DataReceiver::PendingReceive {
17 PendingReceive(DataReceiver* receiver,
18 const ReceiveDataCallback& callback,
19 const ReceiveErrorCallback& error_callback,
20 int32_t fatal_error_value);
22 // Dispatches |data| to |receive_callback_|. Returns whether this
23 // PendingReceive is finished by this call.
24 bool DispatchDataFrame(DataReceiver::DataFrame* data);
26 // Reports |fatal_error_value_| to |receive_error_callback_|.
27 void DispatchFatalError();
29 bool buffer_in_use() { return buffer_in_use_; }
34 // Invoked when the user is finished with the ReadOnlyBuffer provided to
35 // |receive_callback_|.
36 void Done(uint32_t num_bytes);
38 // The DataReceiver that owns this.
39 DataReceiver* receiver_;
41 // The callback to dispatch data.
42 ReceiveDataCallback receive_callback_;
44 // The callback to report errors.
45 ReceiveErrorCallback receive_error_callback_;
47 // The error value to report when DispatchFatalError() is called.
48 const int32_t fatal_error_value_;
50 // True if the user owns a buffer passed to |receive_callback_| as part of
51 // DispatchDataFrame().
55 // A ReadOnlyBuffer implementation that provides a view of a buffer owned by a
57 class DataReceiver::PendingReceive::Buffer : public ReadOnlyBuffer {
59 Buffer(scoped_refptr<DataReceiver> pipe,
60 PendingReceive* receive,
62 uint32_t buffer_size);
65 // ReadOnlyBuffer overrides.
66 const char* GetData() override;
67 uint32_t GetSize() override;
68 void Done(uint32_t bytes_consumed) override;
69 void DoneWithError(uint32_t bytes_consumed, int32_t error) override;
72 // The DataReceiver of whose buffer we are providing a view.
73 scoped_refptr<DataReceiver> receiver_;
75 // The PendingReceive to which this buffer has been created in response.
76 PendingReceive* pending_receive_;
79 uint32_t buffer_size_;
82 // A buffer of data or an error received from the DataSource.
83 struct DataReceiver::DataFrame {
84 explicit DataFrame(mojo::Array<uint8_t> data)
91 explicit DataFrame(int32_t error)
92 : is_error(true), offset(0), error(error), dispatched(false) {}
94 // Whether this DataFrame represents an error.
97 // The data received from the DataSource.
98 mojo::Array<uint8_t> data;
100 // The offset within |data| at which the next read should begin.
103 // The value of the error that occurred.
106 // Whether the error has been dispatched to the user.
110 DataReceiver::DataReceiver(mojo::InterfacePtr<serial::DataSource> source,
111 uint32_t buffer_size,
112 int32_t fatal_error_value)
113 : source_(source.Pass()),
114 fatal_error_value_(fatal_error_value),
116 weak_factory_(this) {
117 source_.set_client(this);
118 source_.set_error_handler(this);
119 source_->Init(buffer_size);
122 bool DataReceiver::Receive(const ReceiveDataCallback& callback,
123 const ReceiveErrorCallback& error_callback) {
124 DCHECK(!callback.is_null() && !error_callback.is_null());
125 if (pending_receive_ || shut_down_)
127 // When the DataSource encounters an error, it pauses transmission. When the
128 // user starts a new receive following notification of the error (via
129 // |error_callback| of the previous Receive call) of the error we can tell the
130 // DataSource to resume transmission of data.
131 if (!pending_data_frames_.empty() && pending_data_frames_.front()->is_error &&
132 pending_data_frames_.front()->dispatched) {
134 pending_data_frames_.pop();
137 pending_receive_.reset(
138 new PendingReceive(this, callback, error_callback, fatal_error_value_));
143 DataReceiver::~DataReceiver() {
147 void DataReceiver::OnError(int32_t error) {
151 pending_data_frames_.push(linked_ptr<DataFrame>(new DataFrame(error)));
152 if (pending_receive_)
156 void DataReceiver::OnData(mojo::Array<uint8_t> data) {
157 pending_data_frames_.push(linked_ptr<DataFrame>(new DataFrame(data.Pass())));
158 if (pending_receive_)
162 void DataReceiver::OnConnectionError() {
166 void DataReceiver::Done(uint32_t bytes_consumed) {
170 DCHECK(pending_receive_);
171 DataFrame& pending_data = *pending_data_frames_.front();
172 pending_data.offset += bytes_consumed;
173 DCHECK_LE(pending_data.offset, pending_data.data.size());
174 if (pending_data.offset == pending_data.data.size()) {
175 source_->ReportBytesReceived(
176 static_cast<uint32_t>(pending_data.data.size()));
177 pending_data_frames_.pop();
179 pending_receive_.reset();
182 void DataReceiver::ReceiveInternal() {
185 DCHECK(pending_receive_);
186 if (pending_receive_->buffer_in_use())
189 if (!pending_data_frames_.empty() &&
190 pending_receive_->DispatchDataFrame(pending_data_frames_.front().get())) {
191 pending_receive_.reset();
195 void DataReceiver::ShutDown() {
197 if (pending_receive_)
198 pending_receive_->DispatchFatalError();
201 DataReceiver::PendingReceive::PendingReceive(
202 DataReceiver* receiver,
203 const ReceiveDataCallback& callback,
204 const ReceiveErrorCallback& error_callback,
205 int32_t fatal_error_value)
206 : receiver_(receiver),
207 receive_callback_(callback),
208 receive_error_callback_(error_callback),
209 fatal_error_value_(fatal_error_value),
210 buffer_in_use_(false) {
213 bool DataReceiver::PendingReceive::DispatchDataFrame(
214 DataReceiver::DataFrame* data) {
215 DCHECK(!buffer_in_use_);
216 DCHECK(!data->dispatched);
218 if (data->is_error) {
219 data->dispatched = true;
220 base::MessageLoop::current()->PostTask(
221 FROM_HERE, base::Bind(receive_error_callback_, data->error));
224 buffer_in_use_ = true;
225 base::MessageLoop::current()->PostTask(
229 base::Passed(scoped_ptr<ReadOnlyBuffer>(new Buffer(
232 reinterpret_cast<char*>(&data->data[0]) + data->offset,
233 static_cast<uint32_t>(data->data.size() - data->offset))))));
237 void DataReceiver::PendingReceive::DispatchFatalError() {
238 receive_error_callback_.Run(fatal_error_value_);
241 void DataReceiver::PendingReceive::Done(uint32_t bytes_consumed) {
242 DCHECK(buffer_in_use_);
243 buffer_in_use_ = false;
244 receiver_->Done(bytes_consumed);
247 DataReceiver::PendingReceive::Buffer::Buffer(
248 scoped_refptr<DataReceiver> receiver,
249 PendingReceive* receive,
251 uint32_t buffer_size)
252 : receiver_(receiver),
253 pending_receive_(receive),
255 buffer_size_(buffer_size) {
258 DataReceiver::PendingReceive::Buffer::~Buffer() {
259 if (pending_receive_)
260 pending_receive_->Done(0);
263 const char* DataReceiver::PendingReceive::Buffer::GetData() {
267 uint32_t DataReceiver::PendingReceive::Buffer::GetSize() {
271 void DataReceiver::PendingReceive::Buffer::Done(uint32_t bytes_consumed) {
272 pending_receive_->Done(bytes_consumed);
273 pending_receive_ = NULL;
279 void DataReceiver::PendingReceive::Buffer::DoneWithError(
280 uint32_t bytes_consumed,
282 Done(bytes_consumed);
285 } // namespace device