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