hearing_aid.cc 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. /******************************************************************************
  2. *
  3. * Copyright 2018 The Android Open Source Project
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #include "bta_hearing_aid_api.h"
  19. #include "bta_gatt_api.h"
  20. #include "bta_gatt_queue.h"
  21. #include "btm_int.h"
  22. #include "device/include/controller.h"
  23. #include "embdrv/g722/g722_enc_dec.h"
  24. #include "gap_api.h"
  25. #include "gatt_api.h"
  26. #include "osi/include/properties.h"
  27. #include <base/bind.h>
  28. #include <base/logging.h>
  29. #include <base/strings/string_number_conversions.h>
  30. #include <hardware/bt_hearing_aid.h>
  31. #include <vector>
  32. using base::Closure;
  33. using bluetooth::Uuid;
  34. using bluetooth::hearing_aid::ConnectionState;
  35. // The MIN_CE_LEN parameter for Connection Parameters based on the current
  36. // Connection Interval
  37. constexpr uint16_t MIN_CE_LEN_10MS_CI = 0x0006;
  38. constexpr uint16_t MIN_CE_LEN_20MS_CI = 0x000C;
  39. constexpr uint16_t CONNECTION_INTERVAL_10MS_PARAM = 0x0008;
  40. constexpr uint16_t CONNECTION_INTERVAL_20MS_PARAM = 0x0010;
  41. void btif_storage_add_hearing_aid(const HearingDevice& dev_info);
  42. bool btif_storage_get_hearing_aid_prop(
  43. const RawAddress& address, uint8_t* capabilities, uint64_t* hi_sync_id,
  44. uint16_t* render_delay, uint16_t* preparation_delay, uint16_t* codecs);
  45. constexpr uint8_t CODEC_G722_16KHZ = 0x01;
  46. constexpr uint8_t CODEC_G722_24KHZ = 0x02;
  47. // audio control point opcodes
  48. constexpr uint8_t CONTROL_POINT_OP_START = 0x01;
  49. constexpr uint8_t CONTROL_POINT_OP_STOP = 0x02;
  50. constexpr uint8_t CONTROL_POINT_OP_STATE_CHANGE = 0x03;
  51. constexpr uint8_t STATE_CHANGE_OTHER_SIDE_DISCONNECTED = 0x00;
  52. constexpr uint8_t STATE_CHANGE_OTHER_SIDE_CONNECTED = 0x01;
  53. constexpr uint8_t STATE_CHANGE_CONN_UPDATE = 0x02;
  54. // used to mark current_volume as not yet known, or possibly old
  55. constexpr int8_t VOLUME_UNKNOWN = 127;
  56. constexpr int8_t VOLUME_MIN = -127;
  57. // audio type
  58. constexpr uint8_t AUDIOTYPE_UNKNOWN = 0x00;
  59. // Status of the other side Hearing Aids device
  60. constexpr uint8_t OTHER_SIDE_NOT_STREAMING = 0x00;
  61. constexpr uint8_t OTHER_SIDE_IS_STREAMING = 0x01;
  62. // This ADD_RENDER_DELAY_INTERVALS is the number of connection intervals when
  63. // the audio data packet is send by Audio Engine to when the Hearing Aids device
  64. // received it from the air. We assumed that there is 2 data buffer queued from
  65. // audio subsystem to bluetooth chip. Then the estimated OTA delay is two
  66. // connnection intervals.
  67. constexpr uint16_t ADD_RENDER_DELAY_INTERVALS = 4;
  68. namespace {
  69. // clang-format off
  70. Uuid HEARING_AID_UUID = Uuid::FromString("FDF0");
  71. Uuid READ_ONLY_PROPERTIES_UUID = Uuid::FromString("6333651e-c481-4a3e-9169-7c902aad37bb");
  72. Uuid AUDIO_CONTROL_POINT_UUID = Uuid::FromString("f0d4de7e-4a88-476c-9d9f-1937b0996cc0");
  73. Uuid AUDIO_STATUS_UUID = Uuid::FromString("38663f1a-e711-4cac-b641-326b56404837");
  74. Uuid VOLUME_UUID = Uuid::FromString("00e4ca9e-ab14-41e4-8823-f9e70c7e91df");
  75. Uuid LE_PSM_UUID = Uuid::FromString("2d410339-82b6-42aa-b34e-e2e01df8cc1a");
  76. // clang-format on
  77. void hearingaid_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data);
  78. void encryption_callback(const RawAddress*, tGATT_TRANSPORT, void*,
  79. tBTM_STATUS);
  80. void read_rssi_cb(void* p_void);
  81. inline BT_HDR* malloc_l2cap_buf(uint16_t len) {
  82. BT_HDR* msg = (BT_HDR*)osi_malloc(BT_HDR_SIZE + L2CAP_MIN_OFFSET +
  83. len /* LE-only, no need for FCS here */);
  84. msg->offset = L2CAP_MIN_OFFSET;
  85. msg->len = len;
  86. return msg;
  87. }
  88. inline uint8_t* get_l2cap_sdu_start_ptr(BT_HDR* msg) {
  89. return (uint8_t*)(msg) + BT_HDR_SIZE + L2CAP_MIN_OFFSET;
  90. }
  91. class HearingAidImpl;
  92. HearingAidImpl* instance;
  93. HearingAidAudioReceiver* audioReceiver;
  94. class HearingDevices {
  95. public:
  96. void Add(HearingDevice device) {
  97. if (FindByAddress(device.address) != nullptr) return;
  98. devices.push_back(device);
  99. }
  100. void Remove(const RawAddress& address) {
  101. for (auto it = devices.begin(); it != devices.end();) {
  102. if (it->address != address) {
  103. ++it;
  104. continue;
  105. }
  106. it = devices.erase(it);
  107. return;
  108. }
  109. }
  110. HearingDevice* FindByAddress(const RawAddress& address) {
  111. auto iter = std::find_if(devices.begin(), devices.end(),
  112. [&address](const HearingDevice& device) {
  113. return device.address == address;
  114. });
  115. return (iter == devices.end()) ? nullptr : &(*iter);
  116. }
  117. HearingDevice* FindByConnId(uint16_t conn_id) {
  118. auto iter = std::find_if(devices.begin(), devices.end(),
  119. [&conn_id](const HearingDevice& device) {
  120. return device.conn_id == conn_id;
  121. });
  122. return (iter == devices.end()) ? nullptr : &(*iter);
  123. }
  124. HearingDevice* FindByGapHandle(uint16_t gap_handle) {
  125. auto iter = std::find_if(devices.begin(), devices.end(),
  126. [&gap_handle](const HearingDevice& device) {
  127. return device.gap_handle == gap_handle;
  128. });
  129. return (iter == devices.end()) ? nullptr : &(*iter);
  130. }
  131. bool IsAnyConnectionUpdateStarted() {
  132. for (const auto& d : devices) {
  133. if (d.connection_update_status == STARTED) return true;
  134. }
  135. return false;
  136. }
  137. void StartRssiLog() {
  138. int read_rssi_start_interval_count = 0;
  139. for (auto& d : devices) {
  140. VLOG(1) << __func__ << ": device=" << d.address << ", read_rssi_count=" << d.read_rssi_count;
  141. // Reset the count
  142. if (d.read_rssi_count <= 0) {
  143. d.read_rssi_count = READ_RSSI_NUM_TRIES;
  144. d.num_intervals_since_last_rssi_read = read_rssi_start_interval_count;
  145. // Spaced apart the Read RSSI commands to the BT controller.
  146. read_rssi_start_interval_count += PERIOD_TO_READ_RSSI_IN_INTERVALS / 2;
  147. read_rssi_start_interval_count %= PERIOD_TO_READ_RSSI_IN_INTERVALS;
  148. std::deque<rssi_log>& rssi_logs = d.audio_stats.rssi_history;
  149. if (rssi_logs.size() >= MAX_RSSI_HISTORY) {
  150. rssi_logs.pop_front();
  151. }
  152. rssi_logs.emplace_back();
  153. }
  154. }
  155. }
  156. size_t size() { return (devices.size()); }
  157. std::vector<HearingDevice> devices;
  158. };
  159. static void write_rpt_ctl_cfg_cb(uint16_t conn_id, tGATT_STATUS status,
  160. uint16_t handle, void* data) {
  161. if (status != GATT_SUCCESS) {
  162. LOG(ERROR) << __func__ << ": handle=" << handle << ", conn_id=" << conn_id
  163. << ", status=" << loghex(status);
  164. }
  165. }
  166. g722_encode_state_t* encoder_state_left = nullptr;
  167. g722_encode_state_t* encoder_state_right = nullptr;
  168. inline void encoder_state_init() {
  169. if (encoder_state_left != nullptr) {
  170. LOG(WARNING) << __func__ << ": encoder already initialized";
  171. return;
  172. }
  173. encoder_state_left = g722_encode_init(nullptr, 64000, G722_PACKED);
  174. encoder_state_right = g722_encode_init(nullptr, 64000, G722_PACKED);
  175. }
  176. inline void encoder_state_release() {
  177. if (encoder_state_left != nullptr) {
  178. g722_encode_release(encoder_state_left);
  179. encoder_state_left = nullptr;
  180. g722_encode_release(encoder_state_right);
  181. encoder_state_right = nullptr;
  182. }
  183. }
  184. class HearingAidImpl : public HearingAid {
  185. private:
  186. // Keep track of whether the Audio Service has resumed audio playback
  187. bool audio_running;
  188. // For Testing: overwrite the MIN_CE_LEN during connection parameter updates
  189. uint16_t overwrite_min_ce_len;
  190. public:
  191. virtual ~HearingAidImpl() = default;
  192. HearingAidImpl(bluetooth::hearing_aid::HearingAidCallbacks* callbacks,
  193. Closure initCb)
  194. : audio_running(false),
  195. overwrite_min_ce_len(0),
  196. gatt_if(0),
  197. seq_counter(0),
  198. current_volume(VOLUME_UNKNOWN),
  199. callbacks(callbacks),
  200. codec_in_use(0) {
  201. default_data_interval_ms = (uint16_t)osi_property_get_int32(
  202. "persist.bluetooth.hearingaid.interval", (int32_t)HA_INTERVAL_20_MS);
  203. if ((default_data_interval_ms != HA_INTERVAL_10_MS) &&
  204. (default_data_interval_ms != HA_INTERVAL_20_MS)) {
  205. LOG(ERROR) << __func__
  206. << ": invalid interval=" << default_data_interval_ms
  207. << "ms. Overwriting back to default";
  208. default_data_interval_ms = HA_INTERVAL_20_MS;
  209. }
  210. VLOG(2) << __func__
  211. << ", default_data_interval_ms=" << default_data_interval_ms;
  212. overwrite_min_ce_len = (uint16_t)osi_property_get_int32(
  213. "persist.bluetooth.hearingaidmincelen", 0);
  214. if (overwrite_min_ce_len) {
  215. LOG(INFO) << __func__
  216. << ": Overwrites MIN_CE_LEN=" << overwrite_min_ce_len;
  217. }
  218. BTA_GATTC_AppRegister(
  219. hearingaid_gattc_callback,
  220. base::Bind(
  221. [](Closure initCb, uint8_t client_id, uint8_t status) {
  222. if (status != GATT_SUCCESS) {
  223. LOG(ERROR) << "Can't start Hearing Aid profile - no gatt "
  224. "clients left!";
  225. return;
  226. }
  227. instance->gatt_if = client_id;
  228. initCb.Run();
  229. },
  230. initCb));
  231. }
  232. uint16_t UpdateBleConnParams(const RawAddress& address) {
  233. /* List of parameters that depends on the chosen Connection Interval */
  234. uint16_t min_ce_len;
  235. uint16_t connection_interval;
  236. switch (default_data_interval_ms) {
  237. case HA_INTERVAL_10_MS:
  238. min_ce_len = MIN_CE_LEN_10MS_CI;
  239. connection_interval = CONNECTION_INTERVAL_10MS_PARAM;
  240. break;
  241. case HA_INTERVAL_20_MS:
  242. min_ce_len = MIN_CE_LEN_20MS_CI;
  243. connection_interval = CONNECTION_INTERVAL_20MS_PARAM;
  244. break;
  245. default:
  246. LOG(ERROR) << __func__ << ":Error: invalid default_data_interval_ms="
  247. << default_data_interval_ms;
  248. min_ce_len = MIN_CE_LEN_10MS_CI;
  249. connection_interval = CONNECTION_INTERVAL_10MS_PARAM;
  250. }
  251. if (overwrite_min_ce_len != 0) {
  252. VLOG(2) << __func__ << ": min_ce_len=" << min_ce_len
  253. << " is overwritten to " << overwrite_min_ce_len;
  254. min_ce_len = overwrite_min_ce_len;
  255. }
  256. L2CA_UpdateBleConnParams(address, connection_interval, connection_interval,
  257. 0x000A, 0x0064 /*1s*/, min_ce_len, min_ce_len);
  258. return connection_interval;
  259. }
  260. void Connect(const RawAddress& address) override {
  261. DVLOG(2) << __func__ << " " << address;
  262. hearingDevices.Add(HearingDevice(address, true));
  263. BTA_GATTC_Open(gatt_if, address, true, GATT_TRANSPORT_LE, false);
  264. }
  265. void AddToWhiteList(const RawAddress& address) override {
  266. VLOG(2) << __func__ << " address: " << address;
  267. hearingDevices.Add(HearingDevice(address, true));
  268. BTA_GATTC_Open(gatt_if, address, false, GATT_TRANSPORT_LE, false);
  269. }
  270. void AddFromStorage(const HearingDevice& dev_info, uint16_t is_white_listed) {
  271. DVLOG(2) << __func__ << " " << dev_info.address
  272. << ", hiSyncId=" << loghex(dev_info.hi_sync_id)
  273. << ", isWhiteListed=" << is_white_listed;
  274. if (is_white_listed) {
  275. hearingDevices.Add(dev_info);
  276. // TODO: we should increase the scanning window for few seconds, to get
  277. // faster initial connection, same after hearing aid disconnects, i.e.
  278. // BTM_BleSetConnScanParams(2048, 1024);
  279. /* add device into BG connection to accept remote initiated connection */
  280. BTA_GATTC_Open(gatt_if, dev_info.address, false, GATT_TRANSPORT_LE,
  281. false);
  282. }
  283. callbacks->OnDeviceAvailable(dev_info.capabilities, dev_info.hi_sync_id,
  284. dev_info.address);
  285. }
  286. int GetDeviceCount() { return (hearingDevices.size()); }
  287. void OnGattConnected(tGATT_STATUS status, uint16_t conn_id,
  288. tGATT_IF client_if, RawAddress address,
  289. tBTA_TRANSPORT transport, uint16_t mtu) {
  290. VLOG(2) << __func__ << ": address=" << address << ", conn_id=" << conn_id;
  291. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  292. if (!hearingDevice) {
  293. /* When Hearing Aid is quickly disabled and enabled in settings, this case
  294. * might happen */
  295. LOG(WARNING) << "Closing connection to non hearing-aid device, address="
  296. << address;
  297. BTA_GATTC_Close(conn_id);
  298. return;
  299. }
  300. if (status != GATT_SUCCESS) {
  301. if (!hearingDevice->connecting_actively) {
  302. // whitelist connection failed, that's ok.
  303. return;
  304. }
  305. LOG(INFO) << "Failed to connect to Hearing Aid device";
  306. hearingDevices.Remove(address);
  307. callbacks->OnConnectionState(ConnectionState::DISCONNECTED, address);
  308. return;
  309. }
  310. hearingDevice->connecting_actively = false;
  311. hearingDevice->conn_id = conn_id;
  312. /* We must update connection parameters one at a time, otherwise anchor
  313. * point (start of connection event) for two devices can be too close to
  314. * each other. Here, by setting min_ce_len=max_ce_len=X, we force controller
  315. * to move anchor point of both connections away from each other, to make
  316. * sure we'll be able to fit all the data we want in one connection event.
  317. */
  318. bool any_update_pending = hearingDevices.IsAnyConnectionUpdateStarted();
  319. // mark the device as pending connection update. If we don't start the
  320. // update now, it'll be started once current device finishes.
  321. if (!any_update_pending) {
  322. hearingDevice->connection_update_status = STARTED;
  323. hearingDevice->requested_connection_interval =
  324. UpdateBleConnParams(address);
  325. } else {
  326. hearingDevice->connection_update_status = AWAITING;
  327. }
  328. tACL_CONN* p_acl = btm_bda_to_acl(address, BT_TRANSPORT_LE);
  329. if (p_acl != nullptr && controller_get_interface()->supports_ble_2m_phy() &&
  330. HCI_LE_2M_PHY_SUPPORTED(p_acl->peer_le_features)) {
  331. LOG(INFO) << address << " set preferred PHY to 2M";
  332. BTM_BleSetPhy(address, PHY_LE_2M, PHY_LE_2M, 0);
  333. }
  334. // Set data length
  335. // TODO(jpawlowski: for 16khz only 87 is required, optimize
  336. BTM_SetBleDataLength(address, 167);
  337. tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(address);
  338. if (p_dev_rec) {
  339. if (p_dev_rec->sec_state == BTM_SEC_STATE_ENCRYPTING ||
  340. p_dev_rec->sec_state == BTM_SEC_STATE_AUTHENTICATING) {
  341. /* if security collision happened, wait for encryption done
  342. * (BTA_GATTC_ENC_CMPL_CB_EVT) */
  343. return;
  344. }
  345. }
  346. /* verify bond */
  347. uint8_t sec_flag = 0;
  348. BTM_GetSecurityFlagsByTransport(address, &sec_flag, BT_TRANSPORT_LE);
  349. if (sec_flag & BTM_SEC_FLAG_ENCRYPTED) {
  350. /* if link has been encrypted */
  351. OnEncryptionComplete(address, true);
  352. return;
  353. }
  354. if (sec_flag & BTM_SEC_FLAG_LKEY_KNOWN) {
  355. /* if bonded and link not encrypted */
  356. sec_flag = BTM_BLE_SEC_ENCRYPT;
  357. BTM_SetEncryption(address, BTA_TRANSPORT_LE, encryption_callback, nullptr,
  358. sec_flag);
  359. return;
  360. }
  361. /* otherwise let it go through */
  362. OnEncryptionComplete(address, true);
  363. }
  364. void OnConnectionUpdateComplete(uint16_t conn_id, tBTA_GATTC* p_data) {
  365. HearingDevice* hearingDevice = hearingDevices.FindByConnId(conn_id);
  366. if (!hearingDevice) {
  367. DVLOG(2) << "Skipping unknown device, conn_id=" << loghex(conn_id);
  368. return;
  369. }
  370. if (p_data) {
  371. if (p_data->conn_update.status == 0) {
  372. bool same_conn_interval =
  373. (hearingDevice->requested_connection_interval ==
  374. p_data->conn_update.interval);
  375. switch (hearingDevice->connection_update_status) {
  376. case COMPLETED:
  377. if (!same_conn_interval) {
  378. LOG(WARNING) << __func__
  379. << ": Unexpected change. Redo. connection interval="
  380. << p_data->conn_update.interval << ", expected="
  381. << hearingDevice->requested_connection_interval
  382. << ", conn_id=" << conn_id
  383. << ", connection_update_status="
  384. << hearingDevice->connection_update_status;
  385. // Redo this connection interval change.
  386. hearingDevice->connection_update_status = AWAITING;
  387. }
  388. break;
  389. case STARTED:
  390. if (same_conn_interval) {
  391. LOG(INFO) << __func__
  392. << ": Connection update completed. conn_id=" << conn_id
  393. << ", device=" << hearingDevice->address;
  394. hearingDevice->connection_update_status = COMPLETED;
  395. } else {
  396. LOG(WARNING) << __func__
  397. << ": Ignored. Different connection interval="
  398. << p_data->conn_update.interval << ", expected="
  399. << hearingDevice->requested_connection_interval
  400. << ", conn_id=" << conn_id
  401. << ", connection_update_status="
  402. << hearingDevice->connection_update_status;
  403. // Wait for the right Connection Update Completion.
  404. return;
  405. }
  406. break;
  407. case AWAITING:
  408. case NONE:
  409. break;
  410. }
  411. // Inform this side and other side device (if any) of Connection
  412. // Updates.
  413. std::vector<uint8_t> conn_update(
  414. {CONTROL_POINT_OP_STATE_CHANGE, STATE_CHANGE_CONN_UPDATE,
  415. (uint8_t)p_data->conn_update.interval});
  416. send_state_change_to_other_side(hearingDevice, conn_update);
  417. send_state_change(hearingDevice, conn_update);
  418. } else {
  419. LOG(INFO) << __func__
  420. << ": error status=" << loghex(p_data->conn_update.status)
  421. << ", conn_id=" << conn_id
  422. << ", device=" << hearingDevice->address
  423. << ", connection_update_status="
  424. << hearingDevice->connection_update_status;
  425. if (hearingDevice->connection_update_status == STARTED) {
  426. // Redo this connection interval change.
  427. LOG(ERROR) << __func__ << ": Redo Connection Interval change";
  428. hearingDevice->connection_update_status = AWAITING;
  429. }
  430. }
  431. } else {
  432. hearingDevice->connection_update_status = NONE;
  433. }
  434. for (auto& device : hearingDevices.devices) {
  435. if (device.conn_id && (device.connection_update_status == AWAITING)) {
  436. device.connection_update_status = STARTED;
  437. device.requested_connection_interval =
  438. UpdateBleConnParams(device.address);
  439. return;
  440. }
  441. }
  442. }
  443. // Completion Callback for the RSSI read operation.
  444. void OnReadRssiComplete(const RawAddress& address, int8_t rssi_value) {
  445. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  446. if (!hearingDevice) {
  447. LOG(INFO) << "Skipping unknown device" << address;
  448. return;
  449. }
  450. VLOG(1) << __func__ << ": device=" << address << ", rssi=" << (int)rssi_value;
  451. if (hearingDevice->read_rssi_count <= 0) {
  452. LOG(ERROR) << __func__ << ": device=" << address
  453. << ", invalid read_rssi_count=" << hearingDevice->read_rssi_count;
  454. return;
  455. }
  456. rssi_log& last_log_set = hearingDevice->audio_stats.rssi_history.back();
  457. if (hearingDevice->read_rssi_count == READ_RSSI_NUM_TRIES) {
  458. // Store the timestamp only for the first one after packet flush
  459. clock_gettime(CLOCK_REALTIME, &last_log_set.timestamp);
  460. LOG(INFO) << __func__ << ": store time. device=" << address << ", rssi=" << (int)rssi_value;
  461. }
  462. last_log_set.rssi.emplace_back(rssi_value);
  463. hearingDevice->read_rssi_count--;
  464. }
  465. void OnEncryptionComplete(const RawAddress& address, bool success) {
  466. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  467. if (!hearingDevice) {
  468. DVLOG(2) << "Skipping unknown device" << address;
  469. return;
  470. }
  471. if (!success) {
  472. LOG(ERROR) << "encryption failed";
  473. BTA_GATTC_Close(hearingDevice->conn_id);
  474. if (hearingDevice->first_connection) {
  475. callbacks->OnConnectionState(ConnectionState::DISCONNECTED, address);
  476. }
  477. return;
  478. }
  479. DVLOG(2) << __func__ << " " << address;
  480. if (hearingDevice->audio_control_point_handle &&
  481. hearingDevice->audio_status_handle &&
  482. hearingDevice->audio_status_ccc_handle &&
  483. hearingDevice->volume_handle && hearingDevice->read_psm_handle) {
  484. // Use cached data, jump to read PSM
  485. ReadPSM(hearingDevice);
  486. } else {
  487. hearingDevice->first_connection = true;
  488. BTA_GATTC_ServiceSearchRequest(hearingDevice->conn_id, &HEARING_AID_UUID);
  489. }
  490. }
  491. void OnServiceChangeEvent(const RawAddress& address) {
  492. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  493. if (!hearingDevice) {
  494. VLOG(2) << "Skipping unknown device" << address;
  495. return;
  496. }
  497. LOG(INFO) << __func__ << ": address=" << address;
  498. hearingDevice->first_connection = true;
  499. hearingDevice->service_changed_rcvd = true;
  500. BtaGattQueue::Clean(hearingDevice->conn_id);
  501. if (hearingDevice->gap_handle) {
  502. GAP_ConnClose(hearingDevice->gap_handle);
  503. hearingDevice->gap_handle = 0;
  504. }
  505. }
  506. void OnServiceDiscDoneEvent(const RawAddress& address) {
  507. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  508. if (!hearingDevice) {
  509. VLOG(2) << "Skipping unknown device" << address;
  510. return;
  511. }
  512. if (hearingDevice->service_changed_rcvd) {
  513. BTA_GATTC_ServiceSearchRequest(hearingDevice->conn_id, &HEARING_AID_UUID);
  514. }
  515. }
  516. void OnServiceSearchComplete(uint16_t conn_id, tGATT_STATUS status) {
  517. HearingDevice* hearingDevice = hearingDevices.FindByConnId(conn_id);
  518. if (!hearingDevice) {
  519. DVLOG(2) << "Skipping unknown device, conn_id=" << loghex(conn_id);
  520. return;
  521. }
  522. // Known device, nothing to do.
  523. if (!hearingDevice->first_connection) return;
  524. if (status != GATT_SUCCESS) {
  525. /* close connection and report service discovery complete with error */
  526. LOG(ERROR) << "Service discovery failed";
  527. if (hearingDevice->first_connection) {
  528. callbacks->OnConnectionState(ConnectionState::DISCONNECTED,
  529. hearingDevice->address);
  530. }
  531. return;
  532. }
  533. const std::list<gatt::Service>* services = BTA_GATTC_GetServices(conn_id);
  534. const gatt::Service* service = nullptr;
  535. for (const gatt::Service& tmp : *services) {
  536. if (tmp.uuid == Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER)) {
  537. LOG(INFO) << "Found UUID_SERVCLASS_GATT_SERVER, handle="
  538. << loghex(tmp.handle);
  539. const gatt::Service* service_changed_service = &tmp;
  540. find_server_changed_ccc_handle(conn_id, service_changed_service);
  541. } else if (tmp.uuid == HEARING_AID_UUID) {
  542. LOG(INFO) << "Found Hearing Aid service, handle=" << loghex(tmp.handle);
  543. service = &tmp;
  544. }
  545. }
  546. if (!service) {
  547. LOG(ERROR) << "No Hearing Aid service found";
  548. callbacks->OnConnectionState(ConnectionState::DISCONNECTED,
  549. hearingDevice->address);
  550. return;
  551. }
  552. for (const gatt::Characteristic& charac : service->characteristics) {
  553. if (charac.uuid == READ_ONLY_PROPERTIES_UUID) {
  554. if (!btif_storage_get_hearing_aid_prop(
  555. hearingDevice->address, &hearingDevice->capabilities,
  556. &hearingDevice->hi_sync_id, &hearingDevice->render_delay,
  557. &hearingDevice->preparation_delay, &hearingDevice->codecs)) {
  558. VLOG(2) << "Reading read only properties "
  559. << loghex(charac.value_handle);
  560. BtaGattQueue::ReadCharacteristic(
  561. conn_id, charac.value_handle,
  562. HearingAidImpl::OnReadOnlyPropertiesReadStatic, nullptr);
  563. }
  564. } else if (charac.uuid == AUDIO_CONTROL_POINT_UUID) {
  565. hearingDevice->audio_control_point_handle = charac.value_handle;
  566. // store audio control point!
  567. } else if (charac.uuid == AUDIO_STATUS_UUID) {
  568. hearingDevice->audio_status_handle = charac.value_handle;
  569. hearingDevice->audio_status_ccc_handle =
  570. find_ccc_handle(conn_id, charac.value_handle);
  571. if (!hearingDevice->audio_status_ccc_handle) {
  572. LOG(ERROR) << __func__ << ": cannot find Audio Status CCC descriptor";
  573. continue;
  574. }
  575. LOG(INFO) << __func__
  576. << ": audio_status_handle=" << loghex(charac.value_handle)
  577. << ", ccc=" << loghex(hearingDevice->audio_status_ccc_handle);
  578. } else if (charac.uuid == VOLUME_UUID) {
  579. hearingDevice->volume_handle = charac.value_handle;
  580. } else if (charac.uuid == LE_PSM_UUID) {
  581. hearingDevice->read_psm_handle = charac.value_handle;
  582. } else {
  583. LOG(WARNING) << "Unknown characteristic found:" << charac.uuid;
  584. }
  585. }
  586. if (hearingDevice->service_changed_rcvd) {
  587. hearingDevice->service_changed_rcvd = false;
  588. }
  589. ReadPSM(hearingDevice);
  590. }
  591. void ReadPSM(HearingDevice* hearingDevice) {
  592. if (hearingDevice->read_psm_handle) {
  593. LOG(INFO) << "Reading PSM " << loghex(hearingDevice->read_psm_handle)
  594. << ", device=" << hearingDevice->address;
  595. BtaGattQueue::ReadCharacteristic(
  596. hearingDevice->conn_id, hearingDevice->read_psm_handle,
  597. HearingAidImpl::OnPsmReadStatic, nullptr);
  598. }
  599. }
  600. void OnNotificationEvent(uint16_t conn_id, uint16_t handle, uint16_t len,
  601. uint8_t* value) {
  602. HearingDevice* device = hearingDevices.FindByConnId(conn_id);
  603. if (!device) {
  604. LOG(INFO) << __func__
  605. << ": Skipping unknown device, conn_id=" << loghex(conn_id);
  606. return;
  607. }
  608. if (device->audio_status_handle != handle) {
  609. LOG(INFO) << __func__ << ": Mismatched handle, "
  610. << loghex(device->audio_status_handle)
  611. << "!=" << loghex(handle);
  612. return;
  613. }
  614. if (len < 1) {
  615. LOG(ERROR) << __func__ << ": Data Length too small, len=" << len
  616. << ", expecting at least 1";
  617. return;
  618. }
  619. if (value[0] != 0) {
  620. LOG(INFO) << __func__
  621. << ": Invalid returned status. data=" << loghex(value[0]);
  622. return;
  623. }
  624. LOG(INFO) << __func__
  625. << ": audio status success notification. command_acked="
  626. << device->command_acked;
  627. device->command_acked = true;
  628. }
  629. void OnReadOnlyPropertiesRead(uint16_t conn_id, tGATT_STATUS status,
  630. uint16_t handle, uint16_t len, uint8_t* value,
  631. void* data) {
  632. HearingDevice* hearingDevice = hearingDevices.FindByConnId(conn_id);
  633. if (!hearingDevice) {
  634. DVLOG(2) << __func__ << "unknown conn_id=" << loghex(conn_id);
  635. return;
  636. }
  637. VLOG(2) << __func__ << " " << base::HexEncode(value, len);
  638. uint8_t* p = value;
  639. uint8_t version;
  640. STREAM_TO_UINT8(version, p);
  641. if (version != 0x01) {
  642. LOG(WARNING) << "Unknown version: " << loghex(version);
  643. return;
  644. }
  645. // version 0x01 of read only properties:
  646. if (len < 17) {
  647. LOG(WARNING) << "Read only properties too short: " << loghex(len);
  648. return;
  649. }
  650. uint8_t capabilities;
  651. STREAM_TO_UINT8(capabilities, p);
  652. hearingDevice->capabilities = capabilities;
  653. bool side = capabilities & CAPABILITY_SIDE;
  654. bool standalone = capabilities & CAPABILITY_BINAURAL;
  655. VLOG(2) << __func__ << " capabilities: " << (side ? "right" : "left")
  656. << ", " << (standalone ? "binaural" : "monaural");
  657. if (capabilities & CAPABILITY_RESERVED) {
  658. LOG(WARNING) << __func__ << " reserved capabilities are set";
  659. }
  660. STREAM_TO_UINT64(hearingDevice->hi_sync_id, p);
  661. VLOG(2) << __func__ << " hiSyncId: " << loghex(hearingDevice->hi_sync_id);
  662. uint8_t feature_map;
  663. STREAM_TO_UINT8(feature_map, p);
  664. STREAM_TO_UINT16(hearingDevice->render_delay, p);
  665. VLOG(2) << __func__
  666. << " render delay: " << loghex(hearingDevice->render_delay);
  667. STREAM_TO_UINT16(hearingDevice->preparation_delay, p);
  668. VLOG(2) << __func__ << " preparation delay: "
  669. << loghex(hearingDevice->preparation_delay);
  670. uint16_t codecs;
  671. STREAM_TO_UINT16(codecs, p);
  672. hearingDevice->codecs = codecs;
  673. VLOG(2) << __func__ << " supported codecs: " << loghex(codecs);
  674. if (codecs & (1 << CODEC_G722_16KHZ)) VLOG(2) << "\tG722@16kHz";
  675. if (codecs & (1 << CODEC_G722_24KHZ)) VLOG(2) << "\tG722@24kHz";
  676. if (!(codecs & (1 << CODEC_G722_16KHZ))) {
  677. LOG(WARNING) << __func__ << " Mandatory codec, G722@16kHz not supported";
  678. }
  679. }
  680. uint16_t CalcCompressedAudioPacketSize(uint16_t codec_type,
  681. int connection_interval) {
  682. int sample_rate;
  683. const int sample_bit_rate = 16; /* 16 bits per sample */
  684. const int compression_ratio = 4; /* G.722 has a 4:1 compression ratio */
  685. if (codec_type == CODEC_G722_24KHZ) {
  686. sample_rate = 24000;
  687. } else {
  688. sample_rate = 16000;
  689. }
  690. // compressed_data_packet_size is the size in bytes of the compressed audio
  691. // data buffer that is generated for each connection interval.
  692. uint32_t compressed_data_packet_size =
  693. (sample_rate * connection_interval * (sample_bit_rate / 8) /
  694. compression_ratio) /
  695. 1000;
  696. return ((uint16_t)compressed_data_packet_size);
  697. }
  698. void ChooseCodec(const HearingDevice& hearingDevice) {
  699. if (codec_in_use) return;
  700. // use the best codec available for this pair of devices.
  701. uint16_t codecs = hearingDevice.codecs;
  702. if (hearingDevice.hi_sync_id != 0) {
  703. for (const auto& device : hearingDevices.devices) {
  704. if (device.hi_sync_id != hearingDevice.hi_sync_id) continue;
  705. codecs &= device.codecs;
  706. }
  707. }
  708. if ((codecs & (1 << CODEC_G722_24KHZ)) &&
  709. controller_get_interface()->supports_ble_2m_phy() &&
  710. default_data_interval_ms == HA_INTERVAL_10_MS) {
  711. codec_in_use = CODEC_G722_24KHZ;
  712. } else if (codecs & (1 << CODEC_G722_16KHZ)) {
  713. codec_in_use = CODEC_G722_16KHZ;
  714. }
  715. }
  716. void OnAudioStatus(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
  717. uint16_t len, uint8_t* value, void* data) {
  718. LOG(INFO) << __func__ << " " << base::HexEncode(value, len);
  719. }
  720. void OnPsmRead(uint16_t conn_id, tGATT_STATUS status, uint16_t handle,
  721. uint16_t len, uint8_t* value, void* data) {
  722. HearingDevice* hearingDevice = hearingDevices.FindByConnId(conn_id);
  723. if (!hearingDevice) {
  724. DVLOG(2) << "Skipping unknown read event, conn_id=" << loghex(conn_id);
  725. return;
  726. }
  727. if (status != GATT_SUCCESS) {
  728. LOG(ERROR) << "Error reading PSM for device" << hearingDevice->address;
  729. return;
  730. }
  731. if (len > 2) {
  732. LOG(ERROR) << "Bad PSM length";
  733. return;
  734. }
  735. uint16_t psm = *((uint16_t*)value);
  736. VLOG(2) << "read psm:" << loghex(psm);
  737. ConnectSocket(hearingDevice, psm);
  738. }
  739. void ConnectSocket(HearingDevice* hearingDevice, uint16_t psm) {
  740. tL2CAP_CFG_INFO cfg_info = tL2CAP_CFG_INFO{.mtu = 512};
  741. SendEnableServiceChangedInd(hearingDevice);
  742. uint8_t service_id = hearingDevice->isLeft()
  743. ? BTM_SEC_SERVICE_HEARING_AID_LEFT
  744. : BTM_SEC_SERVICE_HEARING_AID_RIGHT;
  745. uint16_t gap_handle = GAP_ConnOpen(
  746. "", service_id, false, &hearingDevice->address, psm, 514 /* MPS */,
  747. &cfg_info, nullptr, BTM_SEC_NONE /* TODO: request security ? */,
  748. L2CAP_FCR_LE_COC_MODE, HearingAidImpl::GapCallbackStatic,
  749. BT_TRANSPORT_LE);
  750. if (gap_handle == GAP_INVALID_HANDLE) {
  751. LOG(ERROR) << "UNABLE TO GET gap_handle";
  752. return;
  753. }
  754. hearingDevice->gap_handle = gap_handle;
  755. LOG(INFO) << "Successfully sent GAP connect request";
  756. }
  757. static void OnReadOnlyPropertiesReadStatic(uint16_t conn_id,
  758. tGATT_STATUS status,
  759. uint16_t handle, uint16_t len,
  760. uint8_t* value, void* data) {
  761. if (instance)
  762. instance->OnReadOnlyPropertiesRead(conn_id, status, handle, len, value,
  763. data);
  764. }
  765. static void OnAudioStatusStatic(uint16_t conn_id, tGATT_STATUS status,
  766. uint16_t handle, uint16_t len, uint8_t* value,
  767. void* data) {
  768. if (instance)
  769. instance->OnAudioStatus(conn_id, status, handle, len, value, data);
  770. }
  771. static void OnPsmReadStatic(uint16_t conn_id, tGATT_STATUS status,
  772. uint16_t handle, uint16_t len, uint8_t* value,
  773. void* data) {
  774. if (instance)
  775. instance->OnPsmRead(conn_id, status, handle, len, value, data);
  776. }
  777. /* CoC Socket is ready */
  778. void OnGapConnection(const RawAddress& address) {
  779. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  780. if (!hearingDevice) {
  781. LOG(INFO) << "Device not connected to profile" << address;
  782. return;
  783. }
  784. if (hearingDevice->first_connection) {
  785. /* add device into BG connection to accept remote initiated connection */
  786. BTA_GATTC_Open(gatt_if, address, false, GATT_TRANSPORT_LE, false);
  787. btif_storage_add_hearing_aid(*hearingDevice);
  788. hearingDevice->first_connection = false;
  789. }
  790. LOG(INFO) << __func__ << ": audio_status_handle="
  791. << loghex(hearingDevice->audio_status_handle)
  792. << ", audio_status_ccc_handle="
  793. << loghex(hearingDevice->audio_status_ccc_handle);
  794. /* Register and enable the Audio Status Notification */
  795. tGATT_STATUS register_status;
  796. register_status = BTA_GATTC_RegisterForNotifications(
  797. gatt_if, address, hearingDevice->audio_status_handle);
  798. if (register_status != GATT_SUCCESS) {
  799. LOG(ERROR) << __func__
  800. << ": BTA_GATTC_RegisterForNotifications failed, status="
  801. << loghex(register_status);
  802. return;
  803. }
  804. std::vector<uint8_t> value(2);
  805. uint8_t* ptr = value.data();
  806. UINT16_TO_STREAM(ptr, GATT_CHAR_CLIENT_CONFIG_NOTIFICATION);
  807. BtaGattQueue::WriteDescriptor(
  808. hearingDevice->conn_id, hearingDevice->audio_status_ccc_handle,
  809. std::move(value), GATT_WRITE, write_rpt_ctl_cfg_cb, nullptr);
  810. ChooseCodec(*hearingDevice);
  811. SendStart(hearingDevice);
  812. if (audio_running) {
  813. // Inform the other side (if any) of this connection
  814. std::vector<uint8_t> inform_conn_state(
  815. {CONTROL_POINT_OP_STATE_CHANGE, STATE_CHANGE_OTHER_SIDE_CONNECTED});
  816. send_state_change_to_other_side(hearingDevice, inform_conn_state);
  817. }
  818. hearingDevice->accepting_audio = true;
  819. LOG(INFO) << __func__ << ": address=" << address
  820. << ", hi_sync_id=" << loghex(hearingDevice->hi_sync_id)
  821. << ", codec_in_use=" << loghex(codec_in_use)
  822. << ", audio_running=" << audio_running;
  823. StartSendingAudio(*hearingDevice);
  824. callbacks->OnDeviceAvailable(hearingDevice->capabilities,
  825. hearingDevice->hi_sync_id, address);
  826. callbacks->OnConnectionState(ConnectionState::CONNECTED, address);
  827. }
  828. void StartSendingAudio(const HearingDevice& hearingDevice) {
  829. VLOG(0) << __func__ << ": device=" << hearingDevice.address;
  830. if (encoder_state_left == nullptr) {
  831. encoder_state_init();
  832. seq_counter = 0;
  833. // use the best codec avaliable for this pair of devices.
  834. uint16_t codecs = hearingDevice.codecs;
  835. if (hearingDevice.hi_sync_id != 0) {
  836. for (const auto& device : hearingDevices.devices) {
  837. if (device.hi_sync_id != hearingDevice.hi_sync_id) continue;
  838. codecs &= device.codecs;
  839. }
  840. }
  841. CodecConfiguration codec;
  842. if (codec_in_use == CODEC_G722_24KHZ) {
  843. codec.sample_rate = 24000;
  844. } else {
  845. codec.sample_rate = 16000;
  846. }
  847. codec.bit_rate = 16;
  848. codec.data_interval_ms = default_data_interval_ms;
  849. uint16_t delay_report_ms = 0;
  850. if (hearingDevice.render_delay != 0) {
  851. delay_report_ms =
  852. hearingDevice.render_delay +
  853. (ADD_RENDER_DELAY_INTERVALS * default_data_interval_ms);
  854. }
  855. HearingAidAudioSource::Start(codec, audioReceiver, delay_report_ms);
  856. }
  857. }
  858. void OnAudioSuspend(const std::function<void()>& stop_audio_ticks) {
  859. CHECK(stop_audio_ticks) << "stop_audio_ticks is empty";
  860. if (!audio_running) {
  861. LOG(WARNING) << __func__ << ": Unexpected audio suspend";
  862. } else {
  863. LOG(INFO) << __func__ << ": audio_running=" << audio_running;
  864. }
  865. audio_running = false;
  866. stop_audio_ticks();
  867. std::vector<uint8_t> stop({CONTROL_POINT_OP_STOP});
  868. for (auto& device : hearingDevices.devices) {
  869. if (!device.accepting_audio) continue;
  870. if (!device.playback_started) {
  871. LOG(WARNING) << __func__
  872. << ": Playback not started, skip send Stop cmd, device="
  873. << device.address;
  874. } else {
  875. LOG(INFO) << __func__ << ": send Stop cmd, device=" << device.address;
  876. device.playback_started = false;
  877. device.command_acked = false;
  878. BtaGattQueue::WriteCharacteristic(device.conn_id,
  879. device.audio_control_point_handle,
  880. stop, GATT_WRITE, nullptr, nullptr);
  881. }
  882. }
  883. }
  884. void OnAudioResume(const std::function<void()>& start_audio_ticks) {
  885. CHECK(start_audio_ticks) << "start_audio_ticks is empty";
  886. if (audio_running) {
  887. LOG(ERROR) << __func__ << ": Unexpected Audio Resume";
  888. } else {
  889. LOG(INFO) << __func__ << ": audio_running=" << audio_running;
  890. }
  891. for (auto& device : hearingDevices.devices) {
  892. if (!device.accepting_audio) continue;
  893. audio_running = true;
  894. SendStart(&device);
  895. }
  896. if (!audio_running) {
  897. LOG(INFO) << __func__ << ": No device (0/" << GetDeviceCount()
  898. << ") ready to start";
  899. return;
  900. }
  901. // TODO: shall we also reset the encoder ?
  902. encoder_state_release();
  903. encoder_state_init();
  904. seq_counter = 0;
  905. start_audio_ticks();
  906. }
  907. uint8_t GetOtherSideStreamStatus(HearingDevice* this_side_device) {
  908. for (auto& device : hearingDevices.devices) {
  909. if ((device.address == this_side_device->address) ||
  910. (device.hi_sync_id != this_side_device->hi_sync_id)) {
  911. continue;
  912. }
  913. if (audio_running && (device.conn_id != 0)) {
  914. return (OTHER_SIDE_IS_STREAMING);
  915. } else {
  916. return (OTHER_SIDE_NOT_STREAMING);
  917. }
  918. }
  919. return (OTHER_SIDE_NOT_STREAMING);
  920. }
  921. void SendEnableServiceChangedInd(HearingDevice* device) {
  922. VLOG(2) << __func__ << " Enable " << device->address
  923. << "service changed ind.";
  924. std::vector<uint8_t> value(2);
  925. uint8_t* ptr = value.data();
  926. UINT16_TO_STREAM(ptr, GATT_CHAR_CLIENT_CONFIG_INDICTION);
  927. BtaGattQueue::WriteDescriptor(
  928. device->conn_id, device->service_changed_ccc_handle, std::move(value),
  929. GATT_WRITE, nullptr, nullptr);
  930. }
  931. void SendStart(HearingDevice* device) {
  932. std::vector<uint8_t> start({CONTROL_POINT_OP_START, codec_in_use,
  933. AUDIOTYPE_UNKNOWN, (uint8_t)current_volume,
  934. OTHER_SIDE_NOT_STREAMING});
  935. if (!audio_running) {
  936. if (!device->playback_started) {
  937. LOG(INFO) << __func__
  938. << ": Skip Send Start since audio is not running, device="
  939. << device->address;
  940. } else {
  941. LOG(ERROR) << __func__
  942. << ": Audio not running but Playback has started, device="
  943. << device->address;
  944. }
  945. return;
  946. }
  947. if (current_volume == VOLUME_UNKNOWN) start[3] = (uint8_t)VOLUME_MIN;
  948. if (device->playback_started) {
  949. LOG(ERROR) << __func__
  950. << ": Playback already started, skip send Start cmd, device="
  951. << device->address;
  952. } else {
  953. start[4] = GetOtherSideStreamStatus(device);
  954. LOG(INFO) << __func__ << ": send Start cmd, volume=" << loghex(start[3])
  955. << ", audio type=" << loghex(start[2])
  956. << ", device=" << device->address
  957. << ", other side streaming=" << loghex(start[4]);
  958. device->playback_started = true;
  959. device->command_acked = false;
  960. BtaGattQueue::WriteCharacteristic(device->conn_id,
  961. device->audio_control_point_handle,
  962. start, GATT_WRITE, nullptr, nullptr);
  963. }
  964. }
  965. void OnAudioDataReady(const std::vector<uint8_t>& data) {
  966. /* For now we assume data comes in as 16bit per sample 16kHz PCM stereo */
  967. DVLOG(2) << __func__;
  968. int num_samples =
  969. data.size() / (2 /*bytes_per_sample*/ * 2 /*number of channels*/);
  970. // The G.722 codec accept only even number of samples for encoding
  971. if (num_samples % 2 != 0)
  972. LOG(FATAL) << "num_samples is not even: " << num_samples;
  973. // TODO: we should cache left/right and current state, instad of recomputing
  974. // it for each packet, 100 times a second.
  975. HearingDevice* left = nullptr;
  976. HearingDevice* right = nullptr;
  977. for (auto& device : hearingDevices.devices) {
  978. if (!device.accepting_audio) continue;
  979. if (device.isLeft())
  980. left = &device;
  981. else
  982. right = &device;
  983. }
  984. if (left == nullptr && right == nullptr) {
  985. LOG(WARNING) << __func__ << ": No more (0/" << GetDeviceCount()
  986. << ") devices ready";
  987. DoDisconnectAudioStop();
  988. return;
  989. }
  990. std::vector<uint16_t> chan_left;
  991. std::vector<uint16_t> chan_right;
  992. if (left == nullptr || right == nullptr) {
  993. for (int i = 0; i < num_samples; i++) {
  994. const uint8_t* sample = data.data() + i * 4;
  995. int16_t left = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
  996. sample += 2;
  997. int16_t right = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
  998. uint16_t mono_data = (int16_t)(((uint32_t)left + (uint32_t)right) >> 1);
  999. chan_left.push_back(mono_data);
  1000. chan_right.push_back(mono_data);
  1001. }
  1002. } else {
  1003. for (int i = 0; i < num_samples; i++) {
  1004. const uint8_t* sample = data.data() + i * 4;
  1005. uint16_t left = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
  1006. chan_left.push_back(left);
  1007. sample += 2;
  1008. uint16_t right = (int16_t)((*(sample + 1) << 8) + *sample) >> 1;
  1009. chan_right.push_back(right);
  1010. }
  1011. }
  1012. // TODO: monural, binarual check
  1013. // divide encoded data into packets, add header, send.
  1014. // TODO: make those buffers static and global to prevent constant
  1015. // reallocations
  1016. // TODO: this should basically fit the encoded data, tune the size later
  1017. std::vector<uint8_t> encoded_data_left;
  1018. if (left) {
  1019. // TODO: instead of a magic number, we need to figure out the correct
  1020. // buffer size
  1021. encoded_data_left.resize(4000);
  1022. int encoded_size =
  1023. g722_encode(encoder_state_left, encoded_data_left.data(),
  1024. (const int16_t*)chan_left.data(), chan_left.size());
  1025. encoded_data_left.resize(encoded_size);
  1026. uint16_t cid = GAP_ConnGetL2CAPCid(left->gap_handle);
  1027. uint16_t packets_to_flush = L2CA_FlushChannel(cid, L2CAP_FLUSH_CHANS_GET);
  1028. if (packets_to_flush) {
  1029. VLOG(2) << left->address << " skipping " << packets_to_flush
  1030. << " packets";
  1031. left->audio_stats.packet_flush_count += packets_to_flush;
  1032. left->audio_stats.frame_flush_count++;
  1033. hearingDevices.StartRssiLog();
  1034. }
  1035. // flush all packets stuck in queue
  1036. L2CA_FlushChannel(cid, 0xffff);
  1037. check_and_do_rssi_read(left);
  1038. }
  1039. std::vector<uint8_t> encoded_data_right;
  1040. if (right) {
  1041. // TODO: instead of a magic number, we need to figure out the correct
  1042. // buffer size
  1043. encoded_data_right.resize(4000);
  1044. int encoded_size =
  1045. g722_encode(encoder_state_right, encoded_data_right.data(),
  1046. (const int16_t*)chan_right.data(), chan_right.size());
  1047. encoded_data_right.resize(encoded_size);
  1048. uint16_t cid = GAP_ConnGetL2CAPCid(right->gap_handle);
  1049. uint16_t packets_to_flush = L2CA_FlushChannel(cid, L2CAP_FLUSH_CHANS_GET);
  1050. if (packets_to_flush) {
  1051. VLOG(2) << right->address << " skipping " << packets_to_flush
  1052. << " packets";
  1053. right->audio_stats.packet_flush_count += packets_to_flush;
  1054. right->audio_stats.frame_flush_count++;
  1055. hearingDevices.StartRssiLog();
  1056. }
  1057. // flush all packets stuck in queue
  1058. L2CA_FlushChannel(cid, 0xffff);
  1059. check_and_do_rssi_read(right);
  1060. }
  1061. size_t encoded_data_size =
  1062. std::max(encoded_data_left.size(), encoded_data_right.size());
  1063. uint16_t packet_size =
  1064. CalcCompressedAudioPacketSize(codec_in_use, default_data_interval_ms);
  1065. for (size_t i = 0; i < encoded_data_size; i += packet_size) {
  1066. if (left) {
  1067. left->audio_stats.packet_send_count++;
  1068. SendAudio(encoded_data_left.data() + i, packet_size, left);
  1069. }
  1070. if (right) {
  1071. right->audio_stats.packet_send_count++;
  1072. SendAudio(encoded_data_right.data() + i, packet_size, right);
  1073. }
  1074. seq_counter++;
  1075. }
  1076. if (left) left->audio_stats.frame_send_count++;
  1077. if (right) right->audio_stats.frame_send_count++;
  1078. }
  1079. void SendAudio(uint8_t* encoded_data, uint16_t packet_size,
  1080. HearingDevice* hearingAid) {
  1081. if (!hearingAid->playback_started || !hearingAid->command_acked) {
  1082. VLOG(2) << __func__
  1083. << ": Playback stalled, device=" << hearingAid->address
  1084. << ", cmd send=" << hearingAid->playback_started
  1085. << ", cmd acked=" << hearingAid->command_acked;
  1086. return;
  1087. }
  1088. BT_HDR* audio_packet = malloc_l2cap_buf(packet_size + 1);
  1089. uint8_t* p = get_l2cap_sdu_start_ptr(audio_packet);
  1090. *p = seq_counter;
  1091. p++;
  1092. memcpy(p, encoded_data, packet_size);
  1093. DVLOG(2) << hearingAid->address << " : " << base::HexEncode(p, packet_size);
  1094. uint16_t result = GAP_ConnWriteData(hearingAid->gap_handle, audio_packet);
  1095. if (result != BT_PASS) {
  1096. LOG(ERROR) << " Error sending data: " << loghex(result);
  1097. }
  1098. }
  1099. void GapCallback(uint16_t gap_handle, uint16_t event, tGAP_CB_DATA* data) {
  1100. HearingDevice* hearingDevice = hearingDevices.FindByGapHandle(gap_handle);
  1101. if (!hearingDevice) {
  1102. LOG(INFO) << "Skipping unknown device, gap_handle=" << gap_handle;
  1103. return;
  1104. }
  1105. switch (event) {
  1106. case GAP_EVT_CONN_OPENED: {
  1107. RawAddress address = *GAP_ConnGetRemoteAddr(gap_handle);
  1108. uint16_t tx_mtu = GAP_ConnGetRemMtuSize(gap_handle);
  1109. LOG(INFO) << "GAP_EVT_CONN_OPENED " << address << ", tx_mtu=" << tx_mtu;
  1110. OnGapConnection(address);
  1111. break;
  1112. }
  1113. // TODO: handle properly!
  1114. case GAP_EVT_CONN_CLOSED:
  1115. LOG(INFO) << __func__
  1116. << ": GAP_EVT_CONN_CLOSED: " << hearingDevice->address
  1117. << ", playback_started=" << hearingDevice->playback_started;
  1118. hearingDevice->accepting_audio = false;
  1119. hearingDevice->gap_handle = 0;
  1120. hearingDevice->playback_started = false;
  1121. hearingDevice->command_acked = false;
  1122. break;
  1123. case GAP_EVT_CONN_DATA_AVAIL: {
  1124. DVLOG(2) << "GAP_EVT_CONN_DATA_AVAIL";
  1125. // only data we receive back from hearing aids are some stats, not
  1126. // really important, but useful now for debugging.
  1127. uint32_t bytes_to_read = 0;
  1128. GAP_GetRxQueueCnt(gap_handle, &bytes_to_read);
  1129. std::vector<uint8_t> buffer(bytes_to_read);
  1130. uint16_t bytes_read = 0;
  1131. // TODO:GAP_ConnReadData should accpet uint32_t for length!
  1132. GAP_ConnReadData(gap_handle, buffer.data(), buffer.size(), &bytes_read);
  1133. if (bytes_read < 4) {
  1134. LOG(WARNING) << " Wrong data length";
  1135. return;
  1136. }
  1137. uint8_t* p = buffer.data();
  1138. DVLOG(1) << "stats from the hearing aid:";
  1139. for (size_t i = 0; i + 4 <= buffer.size(); i += 4) {
  1140. uint16_t event_counter, frame_index;
  1141. STREAM_TO_UINT16(event_counter, p);
  1142. STREAM_TO_UINT16(frame_index, p);
  1143. DVLOG(1) << "event_counter=" << event_counter
  1144. << " frame_index: " << frame_index;
  1145. }
  1146. break;
  1147. }
  1148. case GAP_EVT_TX_EMPTY:
  1149. DVLOG(2) << "GAP_EVT_TX_EMPTY";
  1150. break;
  1151. case GAP_EVT_CONN_CONGESTED:
  1152. DVLOG(2) << "GAP_EVT_CONN_CONGESTED";
  1153. // TODO: make it into function
  1154. HearingAidAudioSource::Stop();
  1155. // TODO: kill the encoder only if all hearing aids are down.
  1156. // g722_encode_release(encoder_state);
  1157. // encoder_state_left = nulllptr;
  1158. // encoder_state_right = nulllptr;
  1159. break;
  1160. case GAP_EVT_CONN_UNCONGESTED:
  1161. DVLOG(2) << "GAP_EVT_CONN_UNCONGESTED";
  1162. break;
  1163. case GAP_EVT_LE_COC_CREDITS: {
  1164. auto& tmp = data->coc_credits;
  1165. DVLOG(2) << "GAP_EVT_LE_COC_CREDITS, for device: "
  1166. << hearingDevice->address << " added" << tmp.credits_received
  1167. << " credit_count: " << tmp.credit_count;
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. static void GapCallbackStatic(uint16_t gap_handle, uint16_t event,
  1173. tGAP_CB_DATA* data) {
  1174. if (instance) instance->GapCallback(gap_handle, event, data);
  1175. }
  1176. void DumpRssi(int fd, const HearingDevice& device) {
  1177. const struct AudioStats* stats = &device.audio_stats;
  1178. if (stats->rssi_history.size() <= 0) {
  1179. dprintf(fd, " No RSSI history for %s:\n", device.address.ToString().c_str());
  1180. return;
  1181. }
  1182. dprintf(fd, " RSSI history for %s:\n", device.address.ToString().c_str());
  1183. dprintf(fd, " Time of RSSI 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9\n");
  1184. for (auto& rssi_logs : stats->rssi_history) {
  1185. if (rssi_logs.rssi.size() <= 0) {
  1186. break;
  1187. }
  1188. char eventtime[20];
  1189. char temptime[20];
  1190. struct tm* tstamp = localtime(&rssi_logs.timestamp.tv_sec);
  1191. if (!strftime(temptime, sizeof(temptime), "%H:%M:%S", tstamp)) {
  1192. LOG(ERROR) << __func__ << ": strftime fails. tm_sec=" << tstamp->tm_sec << ", tm_min=" << tstamp->tm_min
  1193. << ", tm_hour=" << tstamp->tm_hour;
  1194. strlcpy(temptime, "UNKNOWN TIME", sizeof(temptime));
  1195. }
  1196. snprintf(eventtime, sizeof(eventtime), "%s.%03ld", temptime, rssi_logs.timestamp.tv_nsec / 1000000);
  1197. dprintf(fd, " %s: ", eventtime);
  1198. for (auto rssi_value : rssi_logs.rssi) {
  1199. dprintf(fd, " %04d", rssi_value);
  1200. }
  1201. dprintf(fd, "\n");
  1202. }
  1203. }
  1204. void Dump(int fd) {
  1205. std::stringstream stream;
  1206. for (const auto& device : hearingDevices.devices) {
  1207. bool side = device.capabilities & CAPABILITY_SIDE;
  1208. bool standalone = device.capabilities & CAPABILITY_BINAURAL;
  1209. stream << " " << device.address.ToString() << " "
  1210. << (device.accepting_audio ? "" : "not ") << "connected"
  1211. << "\n " << (standalone ? "binaural" : "monaural") << " "
  1212. << (side ? "right" : "left") << " " << loghex(device.hi_sync_id)
  1213. << std::endl;
  1214. stream
  1215. << " Packet counts (enqueued/flushed) : "
  1216. << device.audio_stats.packet_send_count << " / "
  1217. << device.audio_stats.packet_flush_count
  1218. << "\n Frame counts (enqueued/flushed) : "
  1219. << device.audio_stats.frame_send_count << " / "
  1220. << device.audio_stats.frame_flush_count << std::endl;
  1221. DumpRssi(fd, device);
  1222. }
  1223. dprintf(fd, "%s", stream.str().c_str());
  1224. }
  1225. void Disconnect(const RawAddress& address) override {
  1226. DVLOG(2) << __func__;
  1227. HearingDevice* hearingDevice = hearingDevices.FindByAddress(address);
  1228. if (!hearingDevice) {
  1229. LOG(INFO) << "Device not connected to profile" << address;
  1230. return;
  1231. }
  1232. VLOG(2) << __func__ << ": " << address;
  1233. bool connected = hearingDevice->accepting_audio;
  1234. LOG(INFO) << "GAP_EVT_CONN_CLOSED: " << hearingDevice->address
  1235. << ", playback_started=" << hearingDevice->playback_started
  1236. << ", accepting_audio=" << hearingDevice->accepting_audio;
  1237. if (hearingDevice->connecting_actively) {
  1238. // cancel pending direct connect
  1239. BTA_GATTC_CancelOpen(gatt_if, address, true);
  1240. }
  1241. // Removes all registrations for connection.
  1242. BTA_GATTC_CancelOpen(0, address, false);
  1243. // Inform the other side (if any) of this disconnection
  1244. std::vector<uint8_t> inform_disconn_state(
  1245. {CONTROL_POINT_OP_STATE_CHANGE, STATE_CHANGE_OTHER_SIDE_DISCONNECTED});
  1246. send_state_change_to_other_side(hearingDevice, inform_disconn_state);
  1247. DoDisconnectCleanUp(hearingDevice);
  1248. hearingDevices.Remove(address);
  1249. if (!connected) {
  1250. return;
  1251. }
  1252. callbacks->OnConnectionState(ConnectionState::DISCONNECTED, address);
  1253. for (const auto& device : hearingDevices.devices) {
  1254. if (device.accepting_audio) return;
  1255. }
  1256. LOG(INFO) << __func__ << ": No more (0/" << GetDeviceCount()
  1257. << ") devices ready";
  1258. DoDisconnectAudioStop();
  1259. }
  1260. void OnGattDisconnected(tGATT_STATUS status, uint16_t conn_id,
  1261. tGATT_IF client_if, RawAddress remote_bda,
  1262. tBTA_GATT_REASON reason) {
  1263. HearingDevice* hearingDevice = hearingDevices.FindByConnId(conn_id);
  1264. if (!hearingDevice) {
  1265. VLOG(2) << "Skipping unknown device disconnect, conn_id="
  1266. << loghex(conn_id);
  1267. return;
  1268. }
  1269. VLOG(2) << __func__ << ": conn_id=" << loghex(conn_id)
  1270. << ", reason=" << loghex(reason) << ", remote_bda=" << remote_bda;
  1271. // Inform the other side (if any) of this disconnection
  1272. std::vector<uint8_t> inform_disconn_state(
  1273. {CONTROL_POINT_OP_STATE_CHANGE, STATE_CHANGE_OTHER_SIDE_DISCONNECTED});
  1274. send_state_change_to_other_side(hearingDevice, inform_disconn_state);
  1275. DoDisconnectCleanUp(hearingDevice);
  1276. // Keep this hearing aid in the list, and allow to reconnect back.
  1277. callbacks->OnConnectionState(ConnectionState::DISCONNECTED, remote_bda);
  1278. for (const auto& device : hearingDevices.devices) {
  1279. if (device.accepting_audio) return;
  1280. }
  1281. LOG(INFO) << __func__ << ": No more (0/" << GetDeviceCount()
  1282. << ") devices ready";
  1283. DoDisconnectAudioStop();
  1284. }
  1285. void DoDisconnectCleanUp(HearingDevice* hearingDevice) {
  1286. if (hearingDevice->connection_update_status != COMPLETED) {
  1287. LOG(INFO) << __func__ << ": connection update not completed. Current="
  1288. << hearingDevice->connection_update_status
  1289. << ", device=" << hearingDevice->address;
  1290. if (hearingDevice->connection_update_status == STARTED) {
  1291. OnConnectionUpdateComplete(hearingDevice->conn_id, NULL);
  1292. }
  1293. }
  1294. hearingDevice->connection_update_status = NONE;
  1295. if (hearingDevice->conn_id) {
  1296. BtaGattQueue::Clean(hearingDevice->conn_id);
  1297. BTA_GATTC_Close(hearingDevice->conn_id);
  1298. hearingDevice->conn_id = 0;
  1299. }
  1300. if (hearingDevice->gap_handle) {
  1301. GAP_ConnClose(hearingDevice->gap_handle);
  1302. hearingDevice->gap_handle = 0;
  1303. }
  1304. hearingDevice->accepting_audio = false;
  1305. LOG(INFO) << __func__ << ": device=" << hearingDevice->address
  1306. << ", playback_started=" << hearingDevice->playback_started;
  1307. hearingDevice->playback_started = false;
  1308. hearingDevice->command_acked = false;
  1309. }
  1310. void DoDisconnectAudioStop() {
  1311. HearingAidAudioSource::Stop();
  1312. audio_running = false;
  1313. encoder_state_release();
  1314. current_volume = VOLUME_UNKNOWN;
  1315. }
  1316. void SetVolume(int8_t volume) override {
  1317. VLOG(2) << __func__ << ": " << +volume;
  1318. current_volume = volume;
  1319. for (HearingDevice& device : hearingDevices.devices) {
  1320. if (!device.accepting_audio) continue;
  1321. std::vector<uint8_t> volume_value({static_cast<unsigned char>(volume)});
  1322. BtaGattQueue::WriteCharacteristic(device.conn_id, device.volume_handle,
  1323. volume_value, GATT_WRITE_NO_RSP,
  1324. nullptr, nullptr);
  1325. }
  1326. }
  1327. void CleanUp() {
  1328. BTA_GATTC_AppDeregister(gatt_if);
  1329. for (HearingDevice& device : hearingDevices.devices) {
  1330. DoDisconnectCleanUp(&device);
  1331. }
  1332. hearingDevices.devices.clear();
  1333. encoder_state_release();
  1334. }
  1335. private:
  1336. uint8_t gatt_if;
  1337. uint8_t seq_counter;
  1338. /* current volume gain for the hearing aids*/
  1339. int8_t current_volume;
  1340. bluetooth::hearing_aid::HearingAidCallbacks* callbacks;
  1341. /* currently used codec */
  1342. uint8_t codec_in_use;
  1343. uint16_t default_data_interval_ms;
  1344. HearingDevices hearingDevices;
  1345. void find_server_changed_ccc_handle(uint16_t conn_id,
  1346. const gatt::Service* service) {
  1347. HearingDevice* hearingDevice = hearingDevices.FindByConnId(conn_id);
  1348. if (!hearingDevice) {
  1349. DVLOG(2) << "Skipping unknown device, conn_id=" << loghex(conn_id);
  1350. return;
  1351. }
  1352. for (const gatt::Characteristic& charac : service->characteristics) {
  1353. if (charac.uuid == Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD)) {
  1354. hearingDevice->service_changed_ccc_handle =
  1355. find_ccc_handle(conn_id, charac.value_handle);
  1356. if (!hearingDevice->service_changed_ccc_handle) {
  1357. LOG(ERROR) << __func__
  1358. << ": cannot find service changed CCC descriptor";
  1359. continue;
  1360. }
  1361. LOG(INFO) << __func__ << " service_changed_ccc="
  1362. << loghex(hearingDevice->service_changed_ccc_handle);
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. // Find the handle for the client characteristics configuration of a given
  1368. // characteristics
  1369. uint16_t find_ccc_handle(uint16_t conn_id, uint16_t char_handle) {
  1370. const gatt::Characteristic* p_char =
  1371. BTA_GATTC_GetCharacteristic(conn_id, char_handle);
  1372. if (!p_char) {
  1373. LOG(WARNING) << __func__ << ": No such characteristic: " << char_handle;
  1374. return 0;
  1375. }
  1376. for (const gatt::Descriptor& desc : p_char->descriptors) {
  1377. if (desc.uuid == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG))
  1378. return desc.handle;
  1379. }
  1380. return 0;
  1381. }
  1382. void send_state_change(HearingDevice* device, std::vector<uint8_t> payload) {
  1383. if (device->conn_id != 0) {
  1384. if (device->service_changed_rcvd) {
  1385. LOG(INFO)
  1386. << __func__
  1387. << ": service discover is in progress, skip send State Change cmd.";
  1388. return;
  1389. }
  1390. // Send the data packet
  1391. LOG(INFO) << __func__ << ": Send State Change. device=" << device->address
  1392. << ", status=" << loghex(payload[1]);
  1393. BtaGattQueue::WriteCharacteristic(
  1394. device->conn_id, device->audio_control_point_handle, payload,
  1395. GATT_WRITE_NO_RSP, nullptr, nullptr);
  1396. }
  1397. }
  1398. void send_state_change_to_other_side(HearingDevice* this_side_device,
  1399. std::vector<uint8_t> payload) {
  1400. for (auto& device : hearingDevices.devices) {
  1401. if ((device.address == this_side_device->address) ||
  1402. (device.hi_sync_id != this_side_device->hi_sync_id)) {
  1403. continue;
  1404. }
  1405. send_state_change(&device, payload);
  1406. }
  1407. }
  1408. void check_and_do_rssi_read(HearingDevice* device) {
  1409. if (device->read_rssi_count > 0) {
  1410. device->num_intervals_since_last_rssi_read++;
  1411. if (device->num_intervals_since_last_rssi_read >= PERIOD_TO_READ_RSSI_IN_INTERVALS) {
  1412. device->num_intervals_since_last_rssi_read = 0;
  1413. VLOG(1) << __func__ << ": device=" << device->address;
  1414. BTM_ReadRSSI(device->address, read_rssi_cb);
  1415. }
  1416. }
  1417. }
  1418. };
  1419. void read_rssi_cb(void* p_void) {
  1420. tBTM_RSSI_RESULT* p_result = (tBTM_RSSI_RESULT*)p_void;
  1421. if (!p_result) return;
  1422. if ((instance) && (p_result->status == BTM_SUCCESS)) {
  1423. instance->OnReadRssiComplete(p_result->rem_bda, p_result->rssi);
  1424. }
  1425. }
  1426. void hearingaid_gattc_callback(tBTA_GATTC_EVT event, tBTA_GATTC* p_data) {
  1427. VLOG(2) << __func__ << " event = " << +event;
  1428. if (p_data == nullptr) return;
  1429. switch (event) {
  1430. case BTA_GATTC_DEREG_EVT:
  1431. break;
  1432. case BTA_GATTC_OPEN_EVT: {
  1433. if (!instance) return;
  1434. tBTA_GATTC_OPEN& o = p_data->open;
  1435. instance->OnGattConnected(o.status, o.conn_id, o.client_if, o.remote_bda,
  1436. o.transport, o.mtu);
  1437. break;
  1438. }
  1439. case BTA_GATTC_CLOSE_EVT: {
  1440. if (!instance) return;
  1441. tBTA_GATTC_CLOSE& c = p_data->close;
  1442. instance->OnGattDisconnected(c.status, c.conn_id, c.client_if,
  1443. c.remote_bda, c.reason);
  1444. } break;
  1445. case BTA_GATTC_SEARCH_CMPL_EVT:
  1446. if (!instance) return;
  1447. instance->OnServiceSearchComplete(p_data->search_cmpl.conn_id,
  1448. p_data->search_cmpl.status);
  1449. break;
  1450. case BTA_GATTC_NOTIF_EVT:
  1451. if (!instance) return;
  1452. if (!p_data->notify.is_notify || p_data->notify.len > GATT_MAX_ATTR_LEN) {
  1453. LOG(ERROR) << __func__ << ": rejected BTA_GATTC_NOTIF_EVT. is_notify="
  1454. << p_data->notify.is_notify
  1455. << ", len=" << p_data->notify.len;
  1456. break;
  1457. }
  1458. instance->OnNotificationEvent(p_data->notify.conn_id,
  1459. p_data->notify.handle, p_data->notify.len,
  1460. p_data->notify.value);
  1461. break;
  1462. case BTA_GATTC_ENC_CMPL_CB_EVT:
  1463. if (!instance) return;
  1464. instance->OnEncryptionComplete(p_data->enc_cmpl.remote_bda, true);
  1465. break;
  1466. case BTA_GATTC_CONN_UPDATE_EVT:
  1467. if (!instance) return;
  1468. instance->OnConnectionUpdateComplete(p_data->conn_update.conn_id, p_data);
  1469. break;
  1470. case BTA_GATTC_SRVC_CHG_EVT:
  1471. if (!instance) return;
  1472. instance->OnServiceChangeEvent(p_data->remote_bda);
  1473. break;
  1474. case BTA_GATTC_SRVC_DISC_DONE_EVT:
  1475. if (!instance) return;
  1476. instance->OnServiceDiscDoneEvent(p_data->remote_bda);
  1477. break;
  1478. default:
  1479. break;
  1480. }
  1481. }
  1482. void encryption_callback(const RawAddress* address, tGATT_TRANSPORT, void*,
  1483. tBTM_STATUS status) {
  1484. if (instance) {
  1485. instance->OnEncryptionComplete(*address,
  1486. status == BTM_SUCCESS ? true : false);
  1487. }
  1488. }
  1489. class HearingAidAudioReceiverImpl : public HearingAidAudioReceiver {
  1490. public:
  1491. void OnAudioDataReady(const std::vector<uint8_t>& data) override {
  1492. if (instance) instance->OnAudioDataReady(data);
  1493. }
  1494. void OnAudioSuspend(const std::function<void()>& stop_audio_ticks) override {
  1495. if (instance) instance->OnAudioSuspend(stop_audio_ticks);
  1496. }
  1497. void OnAudioResume(const std::function<void()>& start_audio_ticks) override {
  1498. if (instance) instance->OnAudioResume(start_audio_ticks);
  1499. }
  1500. };
  1501. HearingAidAudioReceiverImpl audioReceiverImpl;
  1502. } // namespace
  1503. void HearingAid::Initialize(
  1504. bluetooth::hearing_aid::HearingAidCallbacks* callbacks, Closure initCb) {
  1505. if (instance) {
  1506. LOG(ERROR) << "Already initialized!";
  1507. }
  1508. audioReceiver = &audioReceiverImpl;
  1509. instance = new HearingAidImpl(callbacks, initCb);
  1510. HearingAidAudioSource::Initialize();
  1511. }
  1512. bool HearingAid::IsHearingAidRunning() { return instance; }
  1513. HearingAid* HearingAid::Get() {
  1514. CHECK(instance);
  1515. return instance;
  1516. };
  1517. void HearingAid::AddFromStorage(const HearingDevice& dev_info,
  1518. uint16_t is_white_listed) {
  1519. if (!instance) {
  1520. LOG(ERROR) << "Not initialized yet";
  1521. }
  1522. instance->AddFromStorage(dev_info, is_white_listed);
  1523. };
  1524. int HearingAid::GetDeviceCount() {
  1525. if (!instance) {
  1526. LOG(INFO) << __func__ << ": Not initialized yet";
  1527. return 0;
  1528. }
  1529. return (instance->GetDeviceCount());
  1530. }
  1531. void HearingAid::CleanUp() {
  1532. // Must stop audio source to make sure it doesn't call any of callbacks on our
  1533. // soon to be null instance
  1534. HearingAidAudioSource::Stop();
  1535. HearingAidImpl* ptr = instance;
  1536. instance = nullptr;
  1537. HearingAidAudioSource::CleanUp();
  1538. ptr->CleanUp();
  1539. delete ptr;
  1540. };
  1541. void HearingAid::DebugDump(int fd) {
  1542. dprintf(fd, "Hearing Aid Manager:\n");
  1543. if (instance) instance->Dump(fd);
  1544. HearingAidAudioSource::DebugDump(fd);
  1545. dprintf(fd, "\n");
  1546. }