• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, bt_bdname_t bd_name,
43                                 uint32_t cod, 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_energy_info_cb)(bt_activity_energy_info energy_info,
62                                 bt_uid_traffic_t* uid_data);
63   void (*invoke_link_quality_report_cb)(uint64_t timestamp, int report_id,
64                                         int rssi, int snr,
65                                         int retransmission_count,
66                                         int packets_not_receive_count,
67                                         int negative_acknowledgement_count);
68 
69   EventCallbacks(const EventCallbacks&) = delete;
70   EventCallbacks& operator=(const EventCallbacks&) = delete;
71 };
72 
73 // This interface lets us query for configuration properties of the stack that
74 // could change at runtime
75 struct ConfigInterface {
76   virtual bool isA2DPOffloadEnabled() = 0;
77   virtual bool isAndroidTVDevice() = 0;
78   virtual bool isRestrictedMode() = 0;
79 
80   explicit ConfigInterface() = default;
81   ConfigInterface(const ConfigInterface&) = delete;
82   ConfigInterface& operator=(const ConfigInterface&) = delete;
83   virtual ~ConfigInterface() = default;
84 };
85 
86 // This interface lets us communicate with encoders used in profiles
87 struct CodecInterface {
88   virtual void initialize() = 0;
89   virtual void cleanup() = 0;
90 
91   virtual uint32_t encodePacket(int16_t* input, uint8_t* output) = 0;
92   virtual bool decodePacket(const uint8_t* i_buf, int16_t* o_buf,
93                             size_t out_len) = 0;
94 
95   explicit CodecInterface() = default;
96   CodecInterface(const CodecInterface&) = delete;
97   CodecInterface& operator=(const CodecInterface&) = delete;
98   virtual ~CodecInterface() = default;
99 };
100 
101 // Please DO NOT add any more methods to this interface.
102 // It is a legacy violation of the abstraction
103 // between core and profiles: the core stack should not have any
104 // profile-specific functionality or call directly into profile code, as this
105 // makes refactoring + testing much harder. Instead, create a *generic* callback
106 // that profiles can register themselves to.
107 struct HACK_ProfileInterface {
108   // HID hacks
109   bt_status_t (*btif_hh_connect)(const RawAddress* bd_addr);
110   bt_status_t (*btif_hh_virtual_unplug)(const RawAddress* bd_addr);
111   tBTA_HH_STATUS (*bta_hh_read_ssr_param)(const RawAddress& bd_addr,
112                                           uint16_t* p_max_ssr_lat,
113                                           uint16_t* p_min_ssr_tout);
114 
115   // AVDTP hacks
116   void (*btif_av_set_dynamic_audio_buffer_size)(
117       uint8_t dynamic_audio_buffer_size);
118 
119   // ASHA hacks
120   int (*GetHearingAidDeviceCount)();
121 
122   // LE Audio hacks
123   bool (*IsLeAudioClientRunning)();
124 
125   // AVRCP hacks
126   uint16_t (*AVRC_GetProfileVersion)();
127 
128   HACK_ProfileInterface(const HACK_ProfileInterface&) = delete;
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 
141   // DO NOT add any more methods here
142   HACK_ProfileInterface* profileSpecific_HACK;
143 
144   virtual void onBluetoothEnabled() = 0;
145   virtual bt_status_t toggleProfile(tBTA_SERVICE_ID service_id,
146                                     bool enable) = 0;
147   virtual void removeDeviceFromProfiles(const RawAddress& bd_addr) = 0;
148   virtual void onLinkDown(const RawAddress& bd_addr) = 0;
149 
CoreInterfaceCoreInterface150   CoreInterface(EventCallbacks* eventCallbacks,
151                 ConfigInterface* configInterface, CodecInterface* msbcCodec,
152                 HACK_ProfileInterface* profileSpecific_HACK)
153       : events{eventCallbacks},
154         config{configInterface},
155         msbcCodec{msbcCodec},
156         profileSpecific_HACK{profileSpecific_HACK} {};
157 
158   CoreInterface(const CoreInterface&) = delete;
159   CoreInterface& operator=(const CoreInterface&) = delete;
160   virtual ~CoreInterface() = default;
161 };
162 
163 }  // namespace core
164 }  // namespace bluetooth
165