1 //
2 //  Copyright 2015 Google, Inc.
3 //
4 //  Licensed under the Apache License, Version 2.0 (the "License");
5 //  you may not use this file except in compliance with the License.
6 //  You may obtain a copy of the License at:
7 //
8 //  http://www.apache.org/licenses/LICENSE-2.0
9 //
10 //  Unless required by applicable law or agreed to in writing, software
11 //  distributed under the License is distributed on an "AS IS" BASIS,
12 //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 //  See the License for the specific language governing permissions and
14 //  limitations under the License.
15 //
16 
17 #include <base/macros.h>
18 #include <base/observer_list.h>
19 
20 #include "abstract_observer_list.h"
21 #include "service/hal/bluetooth_interface.h"
22 
23 namespace bluetooth {
24 namespace hal {
25 
26 class FakeBluetoothInterface : public BluetoothInterface {
27  public:
28   // A Fake HAL Bluetooth interface. This is kept as a global singleton as the
29   // Bluetooth HAL doesn't support anything otherwise.
30   //
31   // TODO(armansito): Use an abstract "TestHandler" interface instead.
32   struct Manager {
33     Manager();
34     ~Manager() = default;
35 
36     // Values that should be returned from bt_interface_t methods.
37     bool enable_succeed;
38     bool disable_succeed;
39     bool set_property_succeed;
40   };
41 
42   // Returns the global Manager.
43   static Manager* GetManager();
44 
45   FakeBluetoothInterface() = default;
46   ~FakeBluetoothInterface() override = default;
47 
48   // Notifies the observers that the adapter state changed to |state|.
49   void NotifyAdapterStateChanged(bt_state_t state);
50 
51   // Triggers an adapter property change event.
52   void NotifyAdapterPropertiesChanged(int num_properties,
53                                       bt_property_t* properties);
54   void NotifyAdapterNamePropertyChanged(const std::string& name);
55   void NotifyAdapterAddressPropertyChanged(const RawAddress* address);
56   void NotifyAdapterLocalLeFeaturesPropertyChanged(
57       const bt_local_le_features_t* features);
58   void NotifyAclStateChangedCallback(bt_status_t status,
59                                      const RawAddress& remote_bdaddr,
60                                      bt_acl_state_t state,
61                                      bt_hci_error_code_t hci_reason);
62 
63   // hal::BluetoothInterface overrides:
64   void AddObserver(Observer* observer) override;
65   void RemoveObserver(Observer* observer) override;
66   const bt_interface_t* GetHALInterface() const override;
67   bt_callbacks_t* GetHALCallbacks() const override;
68 
69  private:
70   btbase::AbstractObserverList<Observer> observers_;
71 
72   DISALLOW_COPY_AND_ASSIGN(FakeBluetoothInterface);
73 };
74 
75 }  // namespace hal
76 }  // namespace bluetooth
77