summaryrefslogtreecommitdiff
path: root/system/service/hal/fake_bluetooth_av_interface.h
blob: e4a45ceeede932065e88031298c3d27b96cacf41 (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 (C) 2017 Google, Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

#pragma once

#include <base/observer_list.h>

#include "abstract_observer_list.h"
#include "service/hal/bluetooth_av_interface.h"
#include "types/raw_address.h"

namespace bluetooth {
namespace hal {

class FakeBluetoothAvInterface : public BluetoothAvInterface {
 public:
  // Handles HAL Bluetooth A2DP API calls for testing. Test code can provide a
  // fake or mock implementation of this and all calls will be routed to it.
  class TestA2dpSourceHandler {
   public:
    virtual bt_status_t Connect(RawAddress bda) = 0;
    virtual bt_status_t Disconnect(RawAddress bda) = 0;

   protected:
    virtual ~TestA2dpSourceHandler() = default;
  };
  class TestA2dpSinkHandler {
   public:
    virtual bt_status_t Connect(RawAddress bda) = 0;
    virtual bt_status_t Disconnect(RawAddress bda) = 0;
    virtual void SetAudioFocusState(int focus_state) = 0;
    virtual void SetAudioTrackGain(float gain) = 0;

   protected:
    virtual ~TestA2dpSinkHandler() = default;
  };

  // Constructs the fake with the given handlers. Implementations can
  // provide their own handlers or simply pass "nullptr" for the default
  // behavior in which BT_STATUS_FAIL will be returned from all calls.
  FakeBluetoothAvInterface(
      std::shared_ptr<TestA2dpSourceHandler> a2dp_source_handler);
  FakeBluetoothAvInterface(
      std::shared_ptr<TestA2dpSinkHandler> a2dp_sink_handler);

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

  ~FakeBluetoothAvInterface();

  // The methods below can be used to notify observers with certain events and
  // given parameters.

  // A2DP common callbacks
  void NotifyConnectionState(const RawAddress& bda,
                             btav_connection_state_t state);
  void NotifyAudioState(const RawAddress& bda, btav_audio_state_t state);
  // A2DP source callbacks
  void NotifyAudioConfig(
      const RawAddress& bda, const btav_a2dp_codec_config_t& codec_config,
      const std::vector<btav_a2dp_codec_config_t> codecs_local_capabilities,
      const std::vector<btav_a2dp_codec_config_t>
          codecs_selectable_capabilities);
  bool QueryMandatoryCodecPreferred(const RawAddress& bda);
  // A2DP sink callbacks
  void NotifyAudioConfig(const RawAddress& bda, uint32_t sample_rate,
                         uint8_t channel_count);

  // BluetoothAvInterface overrides:
  bool A2dpSourceEnable(
      std::vector<btav_a2dp_codec_config_t> codec_priorities) override;
  void A2dpSourceDisable() override;
  bool A2dpSinkEnable() override;
  void A2dpSinkDisable() override;
  void AddA2dpSourceObserver(A2dpSourceObserver* observer) override;
  void RemoveA2dpSourceObserver(A2dpSourceObserver* observer) override;
  void AddA2dpSinkObserver(A2dpSinkObserver* observer) override;
  void RemoveA2dpSinkObserver(A2dpSinkObserver* observer) override;
  const btav_source_interface_t* GetA2dpSourceHALInterface() override;
  const btav_sink_interface_t* GetA2dpSinkHALInterface() override;

 private:
  btbase::AbstractObserverList<A2dpSourceObserver> a2dp_source_observers_;
  btbase::AbstractObserverList<A2dpSinkObserver> a2dp_sink_observers_;
};

}  // namespace hal
}  // namespace bluetooth