19ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//
29ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  Copyright (C) 2015 Google, Inc.
39ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//
49ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  Licensed under the Apache License, Version 2.0 (the "License");
59ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  you may not use this file except in compliance with the License.
69ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  You may obtain a copy of the License at:
79ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//
89ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  http://www.apache.org/licenses/LICENSE-2.0
99ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//
109ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  Unless required by applicable law or agreed to in writing, software
119ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  distributed under the License is distributed on an "AS IS" BASIS,
129ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  See the License for the specific language governing permissions and
149ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//  limitations under the License.
159ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray//
169ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
179ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray#pragma once
189ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
199ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray#include <base/macros.h>
209ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray#include <base/observer_list.h>
219ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
229ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray#include "service/hal/bluetooth_gatt_interface.h"
239ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
249ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguraynamespace bluetooth {
259ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguraynamespace hal {
269ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
279ded7b6175825afdac0e2cebef8755e4d7b03103Arman Ugurayclass FakeBluetoothGattInterface : public BluetoothGattInterface {
289ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray public:
299e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // Handles HAL Bluetooth GATT client API calls for testing. Test code can
309e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // provide a fake or mock implementation of this and all calls will be routed
319e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // to it.
329e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  class TestClientHandler {
339ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray   public:
349e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray    virtual ~TestClientHandler() = default;
359ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
369ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray    virtual bt_status_t RegisterClient(bt_uuid_t* app_uuid) = 0;
379ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray    virtual bt_status_t UnregisterClient(int client_if) = 0;
383f6aa07299fd9db634274fd22821b349f18b5e0fArman Uguray
393f6aa07299fd9db634274fd22821b349f18b5e0fArman Uguray    virtual bt_status_t Scan(bool start) = 0;
40ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski    virtual bt_status_t Connect(int client_if, const bt_bdaddr_t *bd_addr,
41ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski                                bool is_direct, int transport) = 0;
42ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski    virtual bt_status_t Disconnect(int client_if, const bt_bdaddr_t *bd_addr,
43ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski                                   int conn_id) = 0;
443f6aa07299fd9db634274fd22821b349f18b5e0fArman Uguray
45d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray    virtual bt_status_t MultiAdvEnable(
46d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        int client_if, int min_interval, int max_interval, int adv_type,
47d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        int chnl_map, int tx_power, int timeout_s) = 0;
48d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray    virtual bt_status_t MultiAdvSetInstData(
49d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        int client_if, bool set_scan_rsp, bool include_name,
50d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        bool incl_txpower, int appearance,
51d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        int manufacturer_len, char* manufacturer_data,
52d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        int service_data_len, char* service_data,
53d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray        int service_uuid_len, char* service_uuid) = 0;
54d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray    virtual bt_status_t MultiAdvDisable(int client_if) = 0;
559ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  };
569ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
579e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // Handles HAL Bluetooth GATT server API calls for testing. Test code can
589e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // provide a fake or mock implementation of this and all calls will be routed
599e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // to it.
609e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  class TestServerHandler {
619e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray   public:
629e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray    virtual ~TestServerHandler() = default;
639e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray
649e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray    virtual bt_status_t RegisterServer(bt_uuid_t* app_uuid) = 0;
659e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray    virtual bt_status_t UnregisterServer(int server_if) = 0;
6678a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray    virtual bt_status_t AddService(
6778a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray        int server_if, btgatt_srvc_id_t* srvc_id, int num_handles) = 0;
6891613743c9db02dbe05d93d2afa163b78068d054Arman Uguray    virtual bt_status_t AddCharacteristic(int server_if, int srvc_handle,
6991613743c9db02dbe05d93d2afa163b78068d054Arman Uguray                                          bt_uuid_t *uuid,
7091613743c9db02dbe05d93d2afa163b78068d054Arman Uguray                                          int properties, int permissions) = 0;
71d9b0552a7f75737b5c8c60cb87f892b1b9323acfArman Uguray    virtual bt_status_t AddDescriptor(int server_if, int srvc_handle,
72d9b0552a7f75737b5c8c60cb87f892b1b9323acfArman Uguray                                      bt_uuid_t* uuid,
73d9b0552a7f75737b5c8c60cb87f892b1b9323acfArman Uguray                                      int permissions) = 0;
7478a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray    virtual bt_status_t StartService(
7578a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray        int server_if, int srvc_handle, int transport) = 0;
7678a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray    virtual bt_status_t DeleteService(int server_if, int srvc_handle) = 0;
77cd644e3c54a4321b7626b52531c59f88e24e87beArman Uguray    virtual bt_status_t SendIndication(int server_if, int attribute_handle,
78cd644e3c54a4321b7626b52531c59f88e24e87beArman Uguray                                       int conn_id, int len, int confirm,
79cd644e3c54a4321b7626b52531c59f88e24e87beArman Uguray                                       char* value) = 0;
80b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray    virtual bt_status_t SendResponse(int conn_id, int trans_id, int status,
81b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray                                     btgatt_response_t* response) = 0;
829e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  };
839e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray
849e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  // Constructs the fake with the given handlers. Implementations can
859ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  // provide their own handlers or simply pass "nullptr" for the default
869ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  // behavior in which BT_STATUS_FAIL will be returned from all calls.
879e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  FakeBluetoothGattInterface(std::shared_ptr<TestClientHandler> client_handler,
889e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray                             std::shared_ptr<TestServerHandler> server_handler);
899ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  ~FakeBluetoothGattInterface();
909ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
919ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  // The methods below can be used to notify observers with certain events and
929ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  // given parameters.
9378a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray
9478a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray  // Client callbacks:
959ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  void NotifyRegisterClientCallback(int status, int client_if,
969ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray                                    const bt_uuid_t& app_uuid);
97ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski  void NotifyConnectCallback(int conn_id, int status, int client_if,
98ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski                             const bt_bdaddr_t& bda);
99ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski  void NotifyDisconnectCallback(int conn_id, int status, int client_if,
100ce021ddaafea0cdb78005fe08bc8510f9c75aca4Jakub Pawlowski                                const bt_bdaddr_t& bda);
10182ea72f905565f83116f74d5ccbacea5d710a853Arman Uguray  void NotifyScanResultCallback(const bt_bdaddr_t& bda, int rssi,
10282ea72f905565f83116f74d5ccbacea5d710a853Arman Uguray                                uint8_t* adv_data);
103d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray  void NotifyMultiAdvEnableCallback(int client_if, int status);
104d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray  void NotifyMultiAdvDataCallback(int client_if, int status);
105d1ed1f53a5c0cce5ecb8d784acf5bef53211a20fArman Uguray  void NotifyMultiAdvDisableCallback(int client_if, int status);
1069ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
10778a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray  // Server callbacks:
1089e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  void NotifyRegisterServerCallback(int status, int server_if,
1099e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray                                    const bt_uuid_t& app_uuid);
110b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray  void NotifyServerConnectionCallback(int conn_id, int server_if,
111b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray                                      int connected,
112b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray                                      const bt_bdaddr_t& bda);
11378a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray  void NotifyServiceAddedCallback(int status, int server_if,
11478a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray                                  const btgatt_srvc_id_t& srvc_id,
11578a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray                                  int srvc_handle);
11691613743c9db02dbe05d93d2afa163b78068d054Arman Uguray  void NotifyCharacteristicAddedCallback(int status, int server_if,
11791613743c9db02dbe05d93d2afa163b78068d054Arman Uguray                                         const bt_uuid_t& uuid,
11891613743c9db02dbe05d93d2afa163b78068d054Arman Uguray                                         int srvc_handle, int char_handle);
119d9b0552a7f75737b5c8c60cb87f892b1b9323acfArman Uguray  void NotifyDescriptorAddedCallback(int status, int server_if,
120d9b0552a7f75737b5c8c60cb87f892b1b9323acfArman Uguray                                     const bt_uuid_t& uuid,
121d9b0552a7f75737b5c8c60cb87f892b1b9323acfArman Uguray                                     int srvc_handle, int desc_handle);
12278a44cdb25616892087af2c92abd382878c2ccb4Arman Uguray  void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle);
123b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray  void NotifyRequestReadCallback(int conn_id, int trans_id,
124b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray                                 const bt_bdaddr_t& bda, int attr_handle,
125b10f96fa25266b69d0d31af166e93afe14134be7Arman Uguray                                 int offset, bool is_long);
1264ebcbd9ede4006500c3ead0685befe0a8e4aadc2Arman Uguray  void NotifyRequestWriteCallback(int conn_id, int trans_id,
1274ebcbd9ede4006500c3ead0685befe0a8e4aadc2Arman Uguray                                  const bt_bdaddr_t& bda, int attr_handle,
1284ebcbd9ede4006500c3ead0685befe0a8e4aadc2Arman Uguray                                  int offset, int length,
1294ebcbd9ede4006500c3ead0685befe0a8e4aadc2Arman Uguray                                  bool need_rsp, bool is_prep, uint8_t* value);
1304ebcbd9ede4006500c3ead0685befe0a8e4aadc2Arman Uguray  void NotifyRequestExecWriteCallback(int conn_id, int trans_id,
1314ebcbd9ede4006500c3ead0685befe0a8e4aadc2Arman Uguray                                      const bt_bdaddr_t& bda, int exec_write);
132cd644e3c54a4321b7626b52531c59f88e24e87beArman Uguray  void NotifyIndicationSentCallback(int conn_id, int status);
1339e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray
1349ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  // BluetoothGattInterface overrides:
1359ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  void AddClientObserver(ClientObserver* observer) override;
1369ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  void RemoveClientObserver(ClientObserver* observer) override;
1379e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  void AddServerObserver(ServerObserver* observer) override;
1389e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  void RemoveServerObserver(ServerObserver* observer) override;
1399ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  const btgatt_client_interface_t* GetClientHALInterface() const override;
1409e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  const btgatt_server_interface_t* GetServerHALInterface() const override;
1419ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
1429ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray private:
1439ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  base::ObserverList<ClientObserver> client_observers_;
1449e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  base::ObserverList<ServerObserver> server_observers_;
1459e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  std::shared_ptr<TestClientHandler> client_handler_;
1469e5205368d61adf11e75c9e2bd9f6ff26b375bacArman Uguray  std::shared_ptr<TestServerHandler> server_handler_;
1479ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
1483f6aa07299fd9db634274fd22821b349f18b5e0fArman Uguray
1499ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray  DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattInterface);
1509ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray};
1519ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray
1529ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray}  // namespace hal
1539ded7b6175825afdac0e2cebef8755e4d7b03103Arman Uguray}  // namespace bluetooth
154