fake_bluetooth_gatt_interface.h revision b10f96fa25266b69d0d31af166e93afe14134be7
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
24namespace bluetooth {
25namespace hal {
26
27class 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    virtual bt_status_t MultiAdvEnable(
39        int client_if, int min_interval, int max_interval, int adv_type,
40        int chnl_map, int tx_power, int timeout_s) = 0;
41    virtual bt_status_t MultiAdvSetInstData(
42        int client_if, bool set_scan_rsp, bool include_name,
43        bool incl_txpower, int appearance,
44        int manufacturer_len, char* manufacturer_data,
45        int service_data_len, char* service_data,
46        int service_uuid_len, char* service_uuid) = 0;
47    virtual bt_status_t MultiAdvDisable(int client_if) = 0;
48  };
49
50  // Handles HAL Bluetooth GATT server API calls for testing. Test code can
51  // provide a fake or mock implementation of this and all calls will be routed
52  // to it.
53  class TestServerHandler {
54   public:
55    virtual ~TestServerHandler() = default;
56
57    virtual bt_status_t RegisterServer(bt_uuid_t* app_uuid) = 0;
58    virtual bt_status_t UnregisterServer(int server_if) = 0;
59    virtual bt_status_t AddService(
60        int server_if, btgatt_srvc_id_t* srvc_id, int num_handles) = 0;
61    virtual bt_status_t AddCharacteristic(int server_if, int srvc_handle,
62                                          bt_uuid_t *uuid,
63                                          int properties, int permissions) = 0;
64    virtual bt_status_t AddDescriptor(int server_if, int srvc_handle,
65                                      bt_uuid_t* uuid,
66                                      int permissions) = 0;
67    virtual bt_status_t StartService(
68        int server_if, int srvc_handle, int transport) = 0;
69    virtual bt_status_t DeleteService(int server_if, int srvc_handle) = 0;
70    virtual bt_status_t SendResponse(int conn_id, int trans_id, int status,
71                                     btgatt_response_t* response) = 0;
72  };
73
74  // Constructs the fake with the given handlers. Implementations can
75  // provide their own handlers or simply pass "nullptr" for the default
76  // behavior in which BT_STATUS_FAIL will be returned from all calls.
77  FakeBluetoothGattInterface(std::shared_ptr<TestClientHandler> client_handler,
78                             std::shared_ptr<TestServerHandler> server_handler);
79  ~FakeBluetoothGattInterface();
80
81  // The methods below can be used to notify observers with certain events and
82  // given parameters.
83
84  // Client callbacks:
85  void NotifyRegisterClientCallback(int status, int client_if,
86                                    const bt_uuid_t& app_uuid);
87  void NotifyMultiAdvEnableCallback(int client_if, int status);
88  void NotifyMultiAdvDataCallback(int client_if, int status);
89  void NotifyMultiAdvDisableCallback(int client_if, int status);
90
91  // Server callbacks:
92  void NotifyRegisterServerCallback(int status, int server_if,
93                                    const bt_uuid_t& app_uuid);
94  void NotifyServerConnectionCallback(int conn_id, int server_if,
95                                      int connected,
96                                      const bt_bdaddr_t& bda);
97  void NotifyServiceAddedCallback(int status, int server_if,
98                                  const btgatt_srvc_id_t& srvc_id,
99                                  int srvc_handle);
100  void NotifyCharacteristicAddedCallback(int status, int server_if,
101                                         const bt_uuid_t& uuid,
102                                         int srvc_handle, int char_handle);
103  void NotifyDescriptorAddedCallback(int status, int server_if,
104                                     const bt_uuid_t& uuid,
105                                     int srvc_handle, int desc_handle);
106  void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle);
107  void NotifyRequestReadCallback(int conn_id, int trans_id,
108                                 const bt_bdaddr_t& bda, int attr_handle,
109                                 int offset, bool is_long);
110
111  // BluetoothGattInterface overrides:
112  void AddClientObserver(ClientObserver* observer) override;
113  void RemoveClientObserver(ClientObserver* observer) override;
114  void AddClientObserverUnsafe(ClientObserver* observer) override;
115  void RemoveClientObserverUnsafe(ClientObserver* observer) override;
116  void AddServerObserver(ServerObserver* observer) override;
117  void RemoveServerObserver(ServerObserver* observer) override;
118  void AddServerObserverUnsafe(ServerObserver* observer) override;
119  void RemoveServerObserverUnsafe(ServerObserver* observer) override;
120  const btgatt_client_interface_t* GetClientHALInterface() const override;
121  const btgatt_server_interface_t* GetServerHALInterface() const override;
122
123 private:
124  base::ObserverList<ClientObserver> client_observers_;
125  base::ObserverList<ServerObserver> server_observers_;
126  std::shared_ptr<TestClientHandler> client_handler_;
127  std::shared_ptr<TestServerHandler> server_handler_;
128
129  DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattInterface);
130};
131
132}  // namespace hal
133}  // namespace bluetooth
134