1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
//
// Copyright 2015 Google, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once
#include <base/observer_list.h>
#include <vector>
#include "abstract_observer_list.h"
#include "service/hal/bluetooth_gatt_interface.h"
#include "types/raw_address.h"
namespace bluetooth {
namespace hal {
class FakeBluetoothGattInterface : public BluetoothGattInterface {
public:
// Handles HAL Bluetooth GATT client API calls for testing. Test code can
// provide a fake or mock implementation of this and all calls will be routed
// to it.
class TestClientHandler {
public:
virtual ~TestClientHandler() = default;
virtual bt_status_t RegisterClient(const bluetooth::Uuid& app_uuid,
bool eatt_support) = 0;
virtual bt_status_t UnregisterClient(int client_if) = 0;
virtual bt_status_t Connect(int client_if, const RawAddress& bd_addr,
bool is_direct, int transport) = 0;
virtual bt_status_t Disconnect(int client_if, const RawAddress& bd_addr,
int conn_id) = 0;
};
// Handles HAL Bluetooth GATT server API calls for testing. Test code can
// provide a fake or mock implementation of this and all calls will be routed
// to it.
class TestServerHandler {
public:
virtual ~TestServerHandler() = default;
virtual bt_status_t RegisterServer(const bluetooth::Uuid& app_uuid,
bool eatt_support) = 0;
virtual bt_status_t UnregisterServer(int server_if) = 0;
virtual bt_status_t AddService(
int server_if, std::vector<btgatt_db_element_t> service) = 0;
virtual bt_status_t DeleteService(int server_if, int srvc_handle) = 0;
virtual bt_status_t SendIndication(int server_if, int attribute_handle,
int conn_id, int confirm,
std::vector<uint8_t> value) = 0;
virtual bt_status_t SendResponse(int conn_id, int trans_id, int status,
const btgatt_response_t& response) = 0;
};
// Constructs the fake with the given handlers. Implementations can
// provide their own handlers or simply pass "nullptr" for the default
// behavior in which BT_STATUS_FAIL will be returned from all calls.
FakeBluetoothGattInterface(
std::shared_ptr<BleAdvertiserInterface> advertiser_handler,
std::shared_ptr<BleScannerInterface> scanner_handler,
std::shared_ptr<TestClientHandler> client_handler,
std::shared_ptr<TestServerHandler> server_handler);
FakeBluetoothGattInterface(const FakeBluetoothGattInterface&) = delete;
FakeBluetoothGattInterface& operator=(const FakeBluetoothGattInterface&) =
delete;
~FakeBluetoothGattInterface();
// The methods below can be used to notify observers with certain events and
// given parameters.
void NotifyRegisterScannerCallback(int status, int client_if,
const bluetooth::Uuid& app_uuid);
void NotifyScanResultCallback(const RawAddress& bda, int rssi,
std::vector<uint8_t> adv_data);
// Client callbacks:
void NotifyRegisterClientCallback(int status, int client_if,
const bluetooth::Uuid& app_uuid);
void NotifyConnectCallback(int conn_id, int status, int client_if,
const RawAddress& bda);
void NotifyDisconnectCallback(int conn_id, int status, int client_if,
const RawAddress& bda);
// Server callbacks:
void NotifyRegisterServerCallback(int status, int server_if,
const bluetooth::Uuid& app_uuid);
void NotifyServerConnectionCallback(int conn_id, int server_if, int connected,
const RawAddress& bda);
void NotifyServiceAddedCallback(int status, int server_if,
std::vector<btgatt_db_element_t> srvc);
void NotifyCharacteristicAddedCallback(int status, int server_if,
const bluetooth::Uuid& uuid,
int srvc_handle, int char_handle);
void NotifyDescriptorAddedCallback(int status, int server_if,
const bluetooth::Uuid& uuid,
int srvc_handle, int desc_handle);
void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle);
void NotifyRequestReadCharacteristicCallback(int conn_id, int trans_id,
const RawAddress& bda,
int attr_handle, int offset,
bool is_long);
void NotifyRequestReadDescriptorCallback(int conn_id, int trans_id,
const RawAddress& bda,
int attr_handle, int offset,
bool is_long);
void NotifyRequestWriteCharacteristicCallback(int conn_id, int trans_id,
const RawAddress& bda,
int attr_handle, int offset,
bool need_rsp, bool is_prep,
std::vector<uint8_t> value);
void NotifyRequestWriteDescriptorCallback(int conn_id, int trans_id,
const RawAddress& bda,
int attr_handle, int offset,
bool need_rsp, bool is_prep,
std::vector<uint8_t> value);
void NotifyRequestExecWriteCallback(int conn_id, int trans_id,
const RawAddress& bda, int exec_write);
void NotifyIndicationSentCallback(int conn_id, int status);
// BluetoothGattInterface overrides:
void AddScannerObserver(ScannerObserver* observer) override;
void RemoveScannerObserver(ScannerObserver* observer) override;
void AddClientObserver(ClientObserver* observer) override;
void RemoveClientObserver(ClientObserver* observer) override;
void AddServerObserver(ServerObserver* observer) override;
void RemoveServerObserver(ServerObserver* observer) override;
BleAdvertiserInterface* GetAdvertiserHALInterface() const override;
BleScannerInterface* GetScannerHALInterface() const override;
const btgatt_client_interface_t* GetClientHALInterface() const override;
const btgatt_server_interface_t* GetServerHALInterface() const override;
private:
btbase::AbstractObserverList<ScannerObserver> scanner_observers_;
btbase::AbstractObserverList<ClientObserver> client_observers_;
btbase::AbstractObserverList<ServerObserver> server_observers_;
std::shared_ptr<BleScannerInterface> scanner_handler_;
std::shared_ptr<TestClientHandler> client_handler_;
std::shared_ptr<TestServerHandler> server_handler_;
};
} // namespace hal
} // namespace bluetooth
|