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