1 /*
2  * Copyright 2022 The Android Open Source Project
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 "bta/include/bta_api.h"
20 #include "bta/include/bta_hh_api.h"
21 #include "include/hardware/bluetooth.h"
22 #include "stack/include/btm_ble_api_types.h"
23 #include "types/raw_address.h"
24 
25 namespace bluetooth {
26 namespace core {
27 
28 // These callbacks are not profile specific (e.g. connection complete, bond
29 // complete, etc) and are what go to the Java layer.
30 struct EventCallbacks {
31   void (*invoke_adapter_state_changed_cb)(bt_state_t state);
32   void (*invoke_adapter_properties_cb)(bt_status_t status, int num_properties,
33                                        bt_property_t* properties);
34   void (*invoke_remote_device_properties_cb)(bt_status_t status,
35                                              RawAddress bd_addr,
36                                              int num_properties,
37                                              bt_property_t* properties);
38   void (*invoke_device_found_cb)(int num_properties, bt_property_t* properties);
39   void (*invoke_discovery_state_changed_cb)(bt_discovery_state_t state);
40   void (*invoke_pin_request_cb)(RawAddress bd_addr, bt_bdname_t bd_name,
41                                 uint32_t cod, bool min_16_digit);
42   void (*invoke_ssp_request_cb)(RawAddress bd_addr,
43                                 bt_ssp_variant_t pairing_variant,
44                                 uint32_t pass_key);
45   void (*invoke_oob_data_request_cb)(tBT_TRANSPORT t, bool valid, Octet16 c,
46                                      Octet16 r, RawAddress raw_address,
47                                      uint8_t address_type);
48   void (*invoke_bond_state_changed_cb)(bt_status_t status, RawAddress bd_addr,
49                                        bt_bond_state_t state, int fail_reason);
50   void (*invoke_address_consolidate_cb)(RawAddress main_bd_addr,
51                                         RawAddress secondary_bd_addr);
52   void (*invoke_le_address_associate_cb)(RawAddress main_bd_addr,
53                                          RawAddress secondary_bd_addr);
54   void (*invoke_acl_state_changed_cb)(bt_status_t status, RawAddress bd_addr,
55                                       bt_acl_state_t state,
56                                       int transport_link_type,
57                                       bt_hci_error_code_t hci_reason,
58                                       bt_conn_direction_t direction,
59                                       uint16_t acl_handle);
60   void (*invoke_thread_evt_cb)(bt_cb_thread_evt event);
61   void (*invoke_le_test_mode_cb)(bt_status_t status, uint16_t count);
62   void (*invoke_energy_info_cb)(bt_activity_energy_info energy_info,
63                                 bt_uid_traffic_t* uid_data);
64   void (*invoke_link_quality_report_cb)(uint64_t timestamp, int report_id,
65                                         int rssi, int snr,
66                                         int retransmission_count,
67                                         int packets_not_receive_count,
68                                         int negative_acknowledgement_count);
69   void (*invoke_key_missing_cb)(RawAddress bd_addr);
70 
71   EventCallbacks& operator=(const EventCallbacks&) = delete;
72 };
73 
74 // This interface lets us query for configuration properties of the stack that
75 // could change at runtime
76 struct ConfigInterface {
77   virtual bool isA2DPOffloadEnabled() = 0;
78   virtual bool isAndroidTVDevice() = 0;
79   virtual bool isRestrictedMode() = 0;
80 
81   explicit ConfigInterface() = default;
82   ConfigInterface(const ConfigInterface&) = delete;
83   ConfigInterface& operator=(const ConfigInterface&) = delete;
84   virtual ~ConfigInterface() = default;
85 };
86 
87 // This interface lets us communicate with encoders used in profiles
88 struct CodecInterface {
89   virtual void initialize() = 0;
90   virtual void cleanup() = 0;
91 
92   virtual uint32_t encodePacket(int16_t* input, uint8_t* output) = 0;
93   virtual bool decodePacket(const uint8_t* i_buf, int16_t* o_buf,
94                             size_t out_len) = 0;
95 
96   explicit CodecInterface() = default;
97   CodecInterface(const CodecInterface&) = delete;
98   CodecInterface& operator=(const CodecInterface&) = delete;
99   virtual ~CodecInterface() = default;
100 };
101 
102 // Please DO NOT add any more methods to this interface.
103 // It is a legacy violation of the abstraction
104 // between core and profiles: the core stack should not have any
105 // profile-specific functionality or call directly into profile code, as this
106 // makes refactoring + testing much harder. Instead, create a *generic* callback
107 // that profiles can register themselves to.
108 struct HACK_ProfileInterface {
109   // HID hacks
110   bt_status_t (*btif_hh_connect)(const tAclLinkSpec& link_spec);
111   bt_status_t (*btif_hh_virtual_unplug)(const tAclLinkSpec& link_spec);
112   tBTA_HH_STATUS (*bta_hh_read_ssr_param)(const tAclLinkSpec& link_spec,
113                                           uint16_t* p_max_ssr_lat,
114                                           uint16_t* p_min_ssr_tout);
115 
116   // AVDTP hacks
117   void (*btif_av_set_dynamic_audio_buffer_size)(
118       uint8_t dynamic_audio_buffer_size);
119 
120   // ASHA hacks
121   int (*GetHearingAidDeviceCount)();
122 
123   // LE Audio hacks
124   bool (*IsLeAudioClientRunning)();
125 
126   // AVRCP hacks
127   uint16_t (*AVRC_GetProfileVersion)();
128 
129   HACK_ProfileInterface& operator=(const HACK_ProfileInterface&) = delete;
130 };
131 
132 // This class defines the overall interface expected by bluetooth::core.
133 struct CoreInterface {
134   // generic interface
135   EventCallbacks* events;
136   ConfigInterface* config;
137 
138   // codecs
139   CodecInterface* msbcCodec;
140   CodecInterface* lc3Codec;
141 
142   // DO NOT add any more methods here
143   HACK_ProfileInterface* profileSpecific_HACK;
144 
145   virtual void onBluetoothEnabled() = 0;
146   virtual bt_status_t toggleProfile(tBTA_SERVICE_ID service_id,
147                                     bool enable) = 0;
148   virtual void removeDeviceFromProfiles(const RawAddress& bd_addr) = 0;
149   virtual void onLinkDown(const RawAddress& bd_addr) = 0;
150 
CoreInterfaceCoreInterface151   CoreInterface(EventCallbacks* eventCallbacks,
152                 ConfigInterface* configInterface, CodecInterface* msbcCodec,
153                 CodecInterface* lc3Codec,
154                 HACK_ProfileInterface* profileSpecific_HACK)
155       : events{eventCallbacks},
156         config{configInterface},
157         msbcCodec{msbcCodec},
158         lc3Codec{lc3Codec},
159         profileSpecific_HACK{profileSpecific_HACK} {};
160 
161   CoreInterface(const CoreInterface&) = delete;
162   CoreInterface& operator=(const CoreInterface&) = delete;
163   virtual ~CoreInterface() = default;
164 };
165 
166 }  // namespace core
167 }  // namespace bluetooth
168