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