summaryrefslogtreecommitdiffstats
path: root/device/serial/data_receiver.h
blob: 7393517f47c86aed5d3f0d60013e117c5ae3a617 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2014 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 DEVICE_SERIAL_DATA_RECEIVER_H_
#define DEVICE_SERIAL_DATA_RECEIVER_H_

#include <stdint.h>

#include <queue>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "device/serial/buffer.h"
#include "device/serial/data_stream.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/system/data_pipe.h"

namespace device {

// A DataReceiver receives data from a DataSource.
class DataReceiver : public base::RefCounted<DataReceiver>,
                     public serial::DataSourceClient {
 public:
  typedef base::Callback<void(scoped_ptr<ReadOnlyBuffer>)> ReceiveDataCallback;
  typedef base::Callback<void(int32_t error)> ReceiveErrorCallback;

  // Constructs a DataReceiver to receive data from |source|, using a buffer
  // size of |buffer_size|, with connection errors reported as
  // |fatal_error_value|.
  DataReceiver(mojo::InterfacePtr<serial::DataSource> source,
               mojo::InterfaceRequest<serial::DataSourceClient> client,
               uint32_t buffer_size,
               int32_t fatal_error_value);

  // Begins a receive operation. If this returns true, exactly one of |callback|
  // and |error_callback| will eventually be called. If there is already a
  // receive in progress or there has been a connection error, this will have no
  // effect and return false.
  bool Receive(const ReceiveDataCallback& callback,
               const ReceiveErrorCallback& error_callback);

 private:
  class PendingReceive;
  struct DataFrame;
  friend class base::RefCounted<DataReceiver>;

  ~DataReceiver() override;

  // serial::DataSourceClient overrides.
  // Invoked by the DataSource to report errors.
  void OnError(int32_t error) override;
  // Invoked by the DataSource transmit data.
  void OnData(mojo::Array<uint8_t> data) override;

  // mojo error handler
  void OnConnectionError();

  // Invoked by the PendingReceive to report that the user is done with the
  // receive buffer, having read |bytes_read| bytes from it.
  void Done(uint32_t bytes_read);

  // The implementation of Receive(). If a |pending_error_| is ready to
  // dispatch, it does so. Otherwise, this attempts to read from |handle_| and
  // dispatches the contents to |pending_receive_|. If |handle_| is not ready,
  // |waiter_| is used to wait until it is ready. If an error occurs in reading
  // |handle_|, ShutDown() is called.
  void ReceiveInternal();

  // Called when we encounter a fatal error. If a receive is in progress,
  // |fatal_error_value_| will be reported to the user.
  void ShutDown();

  // The control connection to the data source.
  mojo::InterfacePtr<serial::DataSource> source_;
  mojo::Binding<serial::DataSourceClient> client_;

  // The error value to report in the event of a fatal error.
  const int32_t fatal_error_value_;

  // Whether we have encountered a fatal error and shut down.
  bool shut_down_;

  // The current pending receive operation if there is one.
  scoped_ptr<PendingReceive> pending_receive_;

  // The queue of pending data frames (data or an error) received from the
  // DataSource.
  std::queue<linked_ptr<DataFrame>> pending_data_frames_;

  base::WeakPtrFactory<DataReceiver> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(DataReceiver);
};

}  // namespace device

#endif  // DEVICE_SERIAL_DATA_RECEIVER_H_