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 #pragma once
18 
19 #include <vector>
20 
21 #include <base/macros.h>
22 #include <base/observer_list.h>
23 
24 #include "abstract_observer_list.h"
25 #include "service/hal/bluetooth_gatt_interface.h"
26 
27 namespace bluetooth {
28 namespace hal {
29 
30 class FakeBluetoothGattInterface : public BluetoothGattInterface {
31  public:
32   // Handles HAL Bluetooth GATT client API calls for testing. Test code can
33   // provide a fake or mock implementation of this and all calls will be routed
34   // to it.
35   class TestClientHandler {
36    public:
37     virtual ~TestClientHandler() = default;
38 
39     virtual bt_status_t RegisterClient(const bluetooth::Uuid& app_uuid,
40                                        bool eatt_support) = 0;
41     virtual bt_status_t UnregisterClient(int client_if) = 0;
42 
43     virtual bt_status_t Connect(int client_if, const RawAddress& bd_addr,
44                                 bool is_direct, int transport) = 0;
45     virtual bt_status_t Disconnect(int client_if, const RawAddress& bd_addr,
46                                    int conn_id) = 0;
47   };
48 
49   // Handles HAL Bluetooth GATT server API calls for testing. Test code can
50   // provide a fake or mock implementation of this and all calls will be routed
51   // to it.
52   class TestServerHandler {
53    public:
54     virtual ~TestServerHandler() = default;
55 
56     virtual bt_status_t RegisterServer(const bluetooth::Uuid& app_uuid,
57                                        bool eatt_support) = 0;
58     virtual bt_status_t UnregisterServer(int server_if) = 0;
59     virtual bt_status_t AddService(
60         int server_if, std::vector<btgatt_db_element_t> service) = 0;
61     virtual bt_status_t DeleteService(int server_if, int srvc_handle) = 0;
62     virtual bt_status_t SendIndication(int server_if, int attribute_handle,
63                                        int conn_id, int confirm,
64                                        std::vector<uint8_t> value) = 0;
65     virtual bt_status_t SendResponse(int conn_id, int trans_id, int status,
66                                      const btgatt_response_t& response) = 0;
67   };
68 
69   // Constructs the fake with the given handlers. Implementations can
70   // provide their own handlers or simply pass "nullptr" for the default
71   // behavior in which BT_STATUS_FAIL will be returned from all calls.
72   FakeBluetoothGattInterface(
73       std::shared_ptr<BleAdvertiserInterface> advertiser_handler,
74       std::shared_ptr<BleScannerInterface> scanner_handler,
75       std::shared_ptr<TestClientHandler> client_handler,
76       std::shared_ptr<TestServerHandler> server_handler);
77   ~FakeBluetoothGattInterface();
78 
79   // The methods below can be used to notify observers with certain events and
80   // given parameters.
81 
82   void NotifyRegisterScannerCallback(int status, int client_if,
83                                      const bluetooth::Uuid& app_uuid);
84   void NotifyScanResultCallback(const RawAddress& bda, int rssi,
85                                 std::vector<uint8_t> adv_data);
86 
87   // Client callbacks:
88   void NotifyRegisterClientCallback(int status, int client_if,
89                                     const bluetooth::Uuid& app_uuid);
90   void NotifyConnectCallback(int conn_id, int status, int client_if,
91                              const RawAddress& bda);
92   void NotifyDisconnectCallback(int conn_id, int status, int client_if,
93                                 const RawAddress& bda);
94 
95   // Server callbacks:
96   void NotifyRegisterServerCallback(int status, int server_if,
97                                     const bluetooth::Uuid& app_uuid);
98   void NotifyServerConnectionCallback(int conn_id, int server_if, int connected,
99                                       const RawAddress& bda);
100   void NotifyServiceAddedCallback(int status, int server_if,
101                                   std::vector<btgatt_db_element_t> srvc);
102   void NotifyCharacteristicAddedCallback(int status, int server_if,
103                                          const bluetooth::Uuid& uuid,
104                                          int srvc_handle, int char_handle);
105   void NotifyDescriptorAddedCallback(int status, int server_if,
106                                      const bluetooth::Uuid& uuid,
107                                      int srvc_handle, int desc_handle);
108   void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle);
109   void NotifyRequestReadCharacteristicCallback(int conn_id, int trans_id,
110                                                const RawAddress& bda,
111                                                int attr_handle, int offset,
112                                                bool is_long);
113   void NotifyRequestReadDescriptorCallback(int conn_id, int trans_id,
114                                            const RawAddress& bda,
115                                            int attr_handle, int offset,
116                                            bool is_long);
117   void NotifyRequestWriteCharacteristicCallback(int conn_id, int trans_id,
118                                                 const RawAddress& bda,
119                                                 int attr_handle, int offset,
120                                                 bool need_rsp, bool is_prep,
121                                                 std::vector<uint8_t> value);
122   void NotifyRequestWriteDescriptorCallback(int conn_id, int trans_id,
123                                             const RawAddress& bda,
124                                             int attr_handle, int offset,
125                                             bool need_rsp, bool is_prep,
126                                             std::vector<uint8_t> value);
127   void NotifyRequestExecWriteCallback(int conn_id, int trans_id,
128                                       const RawAddress& bda, int exec_write);
129   void NotifyIndicationSentCallback(int conn_id, int status);
130 
131   // BluetoothGattInterface overrides:
132   void AddScannerObserver(ScannerObserver* observer) override;
133   void RemoveScannerObserver(ScannerObserver* observer) override;
134   void AddClientObserver(ClientObserver* observer) override;
135   void RemoveClientObserver(ClientObserver* observer) override;
136   void AddServerObserver(ServerObserver* observer) override;
137   void RemoveServerObserver(ServerObserver* observer) override;
138   BleAdvertiserInterface* GetAdvertiserHALInterface() const override;
139   BleScannerInterface* GetScannerHALInterface() const override;
140   const btgatt_client_interface_t* GetClientHALInterface() const override;
141   const btgatt_server_interface_t* GetServerHALInterface() const override;
142 
143  private:
144   btbase::AbstractObserverList<ScannerObserver> scanner_observers_;
145   btbase::AbstractObserverList<ClientObserver> client_observers_;
146   btbase::AbstractObserverList<ServerObserver> server_observers_;
147   std::shared_ptr<BleScannerInterface> scanner_handler_;
148   std::shared_ptr<TestClientHandler> client_handler_;
149   std::shared_ptr<TestServerHandler> server_handler_;
150 
151   DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattInterface);
152 };
153 
154 }  // namespace hal
155 }  // namespace bluetooth
156