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
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242

ash / components / arc / test / fake_bluetooth_instance.h [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_COMPONENTS_ARC_TEST_FAKE_BLUETOOTH_INSTANCE_H_
#define ASH_COMPONENTS_ARC_TEST_FAKE_BLUETOOTH_INSTANCE_H_

#include <memory>
#include <vector>

#include "ash/components/arc/mojom/bluetooth.mojom.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace device {
class BluetoothUUID;
}

namespace arc {

class FakeBluetoothInstance : public mojom::BluetoothInstance {
 public:
  class GattDBResult {
   public:
    GattDBResult(mojom::BluetoothAddressPtr&& remote_addr,
                 std::vector<mojom::BluetoothGattDBElementPtr>&& db);

    GattDBResult(const GattDBResult&) = delete;
    GattDBResult& operator=(const GattDBResult&) = delete;

    ~GattDBResult();

    const mojom::BluetoothAddressPtr& remote_addr() const {
      return remote_addr_;
    }

    const std::vector<mojom::BluetoothGattDBElementPtr>& db() const {
      return db_;
    }

   private:
    mojom::BluetoothAddressPtr remote_addr_;
    std::vector<mojom::BluetoothGattDBElementPtr> db_;
  };

  class LEDeviceFoundData {
   public:
    LEDeviceFoundData(mojom::BluetoothAddressPtr addr,
                      int32_t rssi,
                      const std::vector<uint8_t>& eir);

    LEDeviceFoundData(const LEDeviceFoundData&) = delete;
    LEDeviceFoundData& operator=(const LEDeviceFoundData&) = delete;

    ~LEDeviceFoundData();

    const mojom::BluetoothAddressPtr& addr() const { return addr_; }

    int32_t rssi() const { return rssi_; }

    const std::vector<uint8_t>& eir() const { return eir_; }

   private:
    mojom::BluetoothAddressPtr addr_;
    int32_t rssi_;
    std::vector<uint8_t> eir_;
  };

  class ConnectionStateChangedData {
   public:
    ConnectionStateChangedData(mojom::BluetoothAddressPtr addr,
                               device::BluetoothTransport device_type,
                               bool connected);

    ConnectionStateChangedData(const ConnectionStateChangedData&) = delete;
    ConnectionStateChangedData& operator=(const ConnectionStateChangedData&) =
        delete;

    ~ConnectionStateChangedData();

    const mojom::BluetoothAddressPtr& addr() const { return addr_; }
    device::BluetoothTransport device_type() const { return device_type_; }
    bool connected() const { return connected_; }

   private:
    mojom::BluetoothAddressPtr addr_;
    device::BluetoothTransport device_type_;
    bool connected_;
  };

  class LEConnectionStateChangeData {
   public:
    LEConnectionStateChangeData(mojom::BluetoothAddressPtr addr,
                                bool connected);

    LEConnectionStateChangeData(const LEConnectionStateChangeData&) = delete;
    LEConnectionStateChangeData& operator=(const LEConnectionStateChangeData&) =
        delete;

    ~LEConnectionStateChangeData();

    const mojom::BluetoothAddressPtr& addr() const { return addr_; }
    bool connected() const { return connected_; }

   private:
    mojom::BluetoothAddressPtr addr_;
    bool connected_;
  };

  FakeBluetoothInstance();

  FakeBluetoothInstance(const FakeBluetoothInstance&) = delete;
  FakeBluetoothInstance& operator=(const FakeBluetoothInstance&) = delete;

  ~FakeBluetoothInstance() override;

  // mojom::BluetoothInstance overrides:
  void Init(mojo::PendingRemote<mojom::BluetoothHost> host_remote,
            InitCallback callback) override;
  void OnAdapterProperties(
      mojom::BluetoothStatus status,
      std::vector<mojom::BluetoothPropertyPtr> properties) override;
  void OnDeviceFound(
      std::vector<mojom::BluetoothPropertyPtr> properties) override;
  void OnDevicePropertiesChanged(
      mojom::BluetoothAddressPtr remote_addr,
      std::vector<mojom::BluetoothPropertyPtr> properties) override;
  void OnDiscoveryStateChanged(mojom::BluetoothDiscoveryState state) override;
  void OnBondStateChanged(mojom::BluetoothStatus status,
                          mojom::BluetoothAddressPtr remote_addr,
                          mojom::BluetoothBondState state) override;
  void OnConnectionStateChanged(mojom::BluetoothAddressPtr remote_addr,
                                device::BluetoothTransport device_type,
                                bool connected) override;
  void OnLEDeviceFound(mojom::BluetoothAddressPtr addr,
                       int32_t rssi,
                       const std::vector<uint8_t>& eir) override;
  void OnLEConnectionStateChange(mojom::BluetoothAddressPtr remote_addr,
                                 bool connected) override;
  void OnLEDeviceAddressChange(mojom::BluetoothAddressPtr old_addr,
                               mojom::BluetoothAddressPtr new_addr) override;
  void OnSearchComplete(mojom::BluetoothAddressPtr remote_addr,
                        mojom::BluetoothGattStatus status) override;
  void OnGetGattDB(mojom::BluetoothAddressPtr remote_addr,
                   std::vector<mojom::BluetoothGattDBElementPtr> db) override;

  void OnGattNotify(mojom::BluetoothAddressPtr remote_addr,
                    mojom::BluetoothGattServiceIDPtr service_id,
                    mojom::BluetoothGattIDPtr char_id,
                    bool is_notify,
                    const std::vector<uint8_t>& value) override;

  void RequestGattRead(mojom::BluetoothAddressPtr address,
                       int32_t attribute_handle,
                       int32_t offset,
                       bool is_long,
                       mojom::BluetoothGattDBAttributeType attribute_type,
                       RequestGattReadCallback callback) override;

  void RequestGattWrite(mojom::BluetoothAddressPtr address,
                        int32_t attribute_handle,
                        int32_t offset,
                        const std::vector<uint8_t>& value,
                        mojom::BluetoothGattDBAttributeType attribute_type,
                        bool is_prepare,
                        RequestGattWriteCallback callback) override;

  void RequestGattExecuteWrite(
      mojom::BluetoothAddressPtr address,
      bool execute,
      RequestGattExecuteWriteCallback callback) override;

  void OnGetSdpRecords(
      mojom::BluetoothStatus status,
      mojom::BluetoothAddressPtr remote_addr,
      const device::BluetoothUUID& target_uuid,
      std::vector<mojom::BluetoothSdpRecordPtr> records) override;

  void OnMTUReceived(mojom::BluetoothAddressPtr remote_addr,
                     uint16_t mtu) override;

  void OnServiceChanged(mojom::BluetoothAddressPtr remote_addr) override;

  const std::vector<std::vector<mojom::BluetoothPropertyPtr>>&
  device_found_data() const {
    return device_found_data_;
  }

  const std::vector<std::vector<mojom::BluetoothPropertyPtr>>&
  device_properties_changed_data() const {
    return device_properties_changed_data_;
  }

  const std::vector<std::unique_ptr<LEDeviceFoundData>>& le_device_found_data()
      const {
    return le_device_found_data_;
  }

  const std::vector<std::unique_ptr<ConnectionStateChangedData>>&
  connection_state_changed_data() const {
    return connection_state_changed_data_;
  }

  const std::vector<std::unique_ptr<LEConnectionStateChangeData>>&
  le_connection_state_change_data() {
    return le_connection_state_change_data_;
  }

  const std::vector<std::unique_ptr<GattDBResult>>& gatt_db_result() const {
    return gatt_db_result_;
  }

  bool get_service_changed_flag() {
    return service_changed_flag_;
  }

  void reset_service_changed_flag() {
    service_changed_flag_ = false;
  }

 private:
  std::vector<std::vector<mojom::BluetoothPropertyPtr>> device_found_data_;
  std::vector<std::vector<mojom::BluetoothPropertyPtr>>
      device_properties_changed_data_;
  std::vector<std::unique_ptr<LEDeviceFoundData>> le_device_found_data_;
  std::vector<std::unique_ptr<ConnectionStateChangedData>>
      connection_state_changed_data_;
  std::vector<std::unique_ptr<LEConnectionStateChangeData>>
      le_connection_state_change_data_;
  std::vector<std::unique_ptr<GattDBResult>> gatt_db_result_;

  // Keeps the binding alive so that calls to this class can be correctly
  // routed.
  mojo::Remote<mojom::BluetoothHost> host_remote_;

  // To indicate whether OnServiceChanged is called.
  bool service_changed_flag_;
};

}  // namespace arc

#endif  // ASH_COMPONENTS_ARC_TEST_FAKE_BLUETOOTH_INSTANCE_H_