• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2022 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 #define LOG_TAG "bt_btif_profile_storage"
19 
20 #include "btif_profile_storage.h"
21 
22 #include <alloca.h>
23 #include <base/logging.h>
24 #include <ctype.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 
29 #include <unordered_set>
30 #include <vector>
31 
32 #include "bta_csis_api.h"
33 #include "bta_groups.h"
34 #include "bta_has_api.h"
35 #include "bta_hd_api.h"
36 #include "bta_hearing_aid_api.h"
37 #include "bta_hh_api.h"
38 #include "bta_le_audio_api.h"
39 #include "btif_api.h"
40 #include "btif_config.h"
41 #include "btif_hd.h"
42 #include "btif_hh.h"
43 #include "btif_storage.h"
44 #include "btif_util.h"
45 #include "core_callbacks.h"
46 #include "device/include/controller.h"
47 #include "gd/common/init_flags.h"
48 #include "osi/include/allocator.h"
49 #include "osi/include/compat.h"
50 #include "osi/include/config.h"
51 #include "osi/include/log.h"
52 #include "osi/include/osi.h"
53 #include "stack/include/bt_octets.h"
54 #include "stack/include/btu.h"
55 #include "stack_manager.h"
56 #include "types/bluetooth/uuid.h"
57 #include "types/raw_address.h"
58 
59 using base::Bind;
60 using bluetooth::Uuid;
61 using bluetooth::csis::CsisClient;
62 using bluetooth::groups::DeviceGroups;
63 
64 /*******************************************************************************
65  *  Constants & Macros
66  ******************************************************************************/
67 #define BTIF_STORAGE_PATH_REMOTE_DEVCLASS "DevClass"
68 
69 #define BTIF_STORAGE_CSIS_AUTOCONNECT "CsisAutoconnect"
70 #define BTIF_STORAGE_CSIS_SET_INFO_BIN "CsisSetInfoBin"
71 #define BTIF_STORAGE_LEAUDIO_AUTOCONNECT "LeAudioAutoconnect"
72 #define BTIF_STORAGE_LEAUDIO_HANDLES_BIN "LeAudioHandlesBin"
73 #define BTIF_STORAGE_LEAUDIO_SINK_PACS_BIN "SinkPacsBin"
74 #define BTIF_STORAGE_LEAUDIO_SOURCE_PACS_BIN "SourcePacsBin"
75 #define BTIF_STORAGE_LEAUDIO_ASES_BIN "AsesBin"
76 #define BTIF_STORAGE_LEAUDIO_SINK_AUDIOLOCATION "SinkAudioLocation"
77 #define BTIF_STORAGE_LEAUDIO_SOURCE_AUDIOLOCATION "SourceAudioLocation"
78 #define BTIF_STORAGE_LEAUDIO_SINK_SUPPORTED_CONTEXT_TYPE \
79   "SinkSupportedContextType"
80 #define BTIF_STORAGE_LEAUDIO_SOURCE_SUPPORTED_CONTEXT_TYPE \
81   "SourceSupportedContextType"
82 #define BTIF_STORAGE_DEVICE_GROUP_BIN "DeviceGroupBin"
83 
84 #define STORAGE_HID_ATRR_MASK_SIZE (4)
85 #define STORAGE_HID_SUB_CLASS_SIZE (2)
86 #define STORAGE_HID_APP_ID_SIZE (2)
87 #define STORAGE_HID_VENDOR_ID_SIZE (4)
88 #define STORAGE_HID_PRODUCT_ID_SIZE (4)
89 #define STORAGE_HID_VERSION_SIZE (4)
90 #define STORAGE_HID_CTRY_CODE_SIZE (2)
91 #define STORAGE_HID_DESC_LEN_SIZE (4)
92 #define STORAGE_HID_DESC_MAX_SIZE (2 * 512)
93 
94 /* <18 char bd addr> <space>LIST <attr_mask> <space> > <sub_class> <space>
95    <app_id> <space>
96                                 <vendor_id> <space> > <product_id> <space>
97    <version> <space>
98                                 <ctry_code> <space> > <desc_len> <space>
99    <desc_list> <space> */
100 #define BTIF_HID_INFO_ENTRY_SIZE_MAX                                  \
101   (STORAGE_BDADDR_STRING_SZ + 1 + STORAGE_HID_ATRR_MASK_SIZE + 1 +    \
102    STORAGE_HID_SUB_CLASS_SIZE + 1 + STORAGE_HID_APP_ID_SIZE + 1 +     \
103    STORAGE_HID_VENDOR_ID_SIZE + 1 + STORAGE_HID_PRODUCT_ID_SIZE + 1 + \
104    STORAGE_HID_VERSION_SIZE + 1 + STORAGE_HID_CTRY_CODE_SIZE + 1 +    \
105    STORAGE_HID_DESC_LEN_SIZE + 1 + STORAGE_HID_DESC_MAX_SIZE + 1)
106 
107 /*******************************************************************************
108  *
109  * Function         btif_storage_add_hid_device_info
110  *
111  * Description      BTIF storage API - Adds the hid information of bonded hid
112  *                  devices-to NVRAM
113  *
114  * Returns          BT_STATUS_SUCCESS if the store was successful,
115  *                  BT_STATUS_FAIL otherwise
116  *
117  ******************************************************************************/
118 
btif_storage_add_hid_device_info(RawAddress * remote_bd_addr,uint16_t attr_mask,uint8_t sub_class,uint8_t app_id,uint16_t vendor_id,uint16_t product_id,uint16_t version,uint8_t ctry_code,uint16_t ssr_max_latency,uint16_t ssr_min_tout,uint16_t dl_len,uint8_t * dsc_list)119 bt_status_t btif_storage_add_hid_device_info(
120     RawAddress* remote_bd_addr, uint16_t attr_mask, uint8_t sub_class,
121     uint8_t app_id, uint16_t vendor_id, uint16_t product_id, uint16_t version,
122     uint8_t ctry_code, uint16_t ssr_max_latency, uint16_t ssr_min_tout,
123     uint16_t dl_len, uint8_t* dsc_list) {
124   BTIF_TRACE_DEBUG("btif_storage_add_hid_device_info:");
125   std::string bdstr = remote_bd_addr->ToString();
126   btif_config_set_int(bdstr, "HidAttrMask", attr_mask);
127   btif_config_set_int(bdstr, "HidSubClass", sub_class);
128   btif_config_set_int(bdstr, "HidAppId", app_id);
129   btif_config_set_int(bdstr, "HidVendorId", vendor_id);
130   btif_config_set_int(bdstr, "HidProductId", product_id);
131   btif_config_set_int(bdstr, "HidVersion", version);
132   btif_config_set_int(bdstr, "HidCountryCode", ctry_code);
133   btif_config_set_int(bdstr, "HidSSRMaxLatency", ssr_max_latency);
134   btif_config_set_int(bdstr, "HidSSRMinTimeout", ssr_min_tout);
135   if (dl_len > 0) btif_config_set_bin(bdstr, "HidDescriptor", dsc_list, dl_len);
136   return BT_STATUS_SUCCESS;
137 }
138 
139 /*******************************************************************************
140  *
141  * Function         btif_storage_load_bonded_hid_info
142  *
143  * Description      BTIF storage API - Loads hid info for all the bonded devices
144  *                  from NVRAM and adds those devices  to the BTA_HH.
145  *
146  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
147  *
148  ******************************************************************************/
btif_storage_load_bonded_hid_info(void)149 bt_status_t btif_storage_load_bonded_hid_info(void) {
150   for (const auto& bd_addr : btif_config_get_paired_devices()) {
151     auto name = bd_addr.ToString();
152 
153     BTIF_TRACE_DEBUG("Remote device:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
154 
155     int value;
156     if (!btif_config_get_int(name, "HidAttrMask", &value)) continue;
157     uint16_t attr_mask = (uint16_t)value;
158 
159     if (btif_in_fetch_bonded_device(name) != BT_STATUS_SUCCESS) {
160       btif_storage_remove_hid_info(bd_addr);
161       continue;
162     }
163 
164     tBTA_HH_DEV_DSCP_INFO dscp_info;
165     memset(&dscp_info, 0, sizeof(dscp_info));
166 
167     btif_config_get_int(name, "HidSubClass", &value);
168     uint8_t sub_class = (uint8_t)value;
169 
170     btif_config_get_int(name, "HidAppId", &value);
171     uint8_t app_id = (uint8_t)value;
172 
173     btif_config_get_int(name, "HidVendorId", &value);
174     dscp_info.vendor_id = (uint16_t)value;
175 
176     btif_config_get_int(name, "HidProductId", &value);
177     dscp_info.product_id = (uint16_t)value;
178 
179     btif_config_get_int(name, "HidVersion", &value);
180     dscp_info.version = (uint8_t)value;
181 
182     btif_config_get_int(name, "HidCountryCode", &value);
183     dscp_info.ctry_code = (uint8_t)value;
184 
185     value = 0;
186     btif_config_get_int(name, "HidSSRMaxLatency", &value);
187     dscp_info.ssr_max_latency = (uint16_t)value;
188 
189     value = 0;
190     btif_config_get_int(name, "HidSSRMinTimeout", &value);
191     dscp_info.ssr_min_tout = (uint16_t)value;
192 
193     size_t len = btif_config_get_bin_length(name, "HidDescriptor");
194     if (len > 0) {
195       dscp_info.descriptor.dl_len = (uint16_t)len;
196       dscp_info.descriptor.dsc_list = (uint8_t*)alloca(len);
197       btif_config_get_bin(name, "HidDescriptor",
198                           (uint8_t*)dscp_info.descriptor.dsc_list, &len);
199     }
200 
201     // add extracted information to BTA HH
202     if (btif_hh_add_added_dev(bd_addr, attr_mask)) {
203       BTA_HhAddDev(bd_addr, attr_mask, sub_class, app_id, dscp_info);
204     }
205   }
206 
207   return BT_STATUS_SUCCESS;
208 }
209 
210 /*******************************************************************************
211  *
212  * Function         btif_storage_remove_hid_info
213  *
214  * Description      BTIF storage API - Deletes the bonded hid device info from
215  *                  NVRAM
216  *
217  * Returns          BT_STATUS_SUCCESS if the deletion was successful,
218  *                  BT_STATUS_FAIL otherwise
219  *
220  ******************************************************************************/
btif_storage_remove_hid_info(const RawAddress & remote_bd_addr)221 bt_status_t btif_storage_remove_hid_info(const RawAddress& remote_bd_addr) {
222   std::string bdstr = remote_bd_addr.ToString();
223 
224   btif_config_remove(bdstr, "HidAttrMask");
225   btif_config_remove(bdstr, "HidSubClass");
226   btif_config_remove(bdstr, "HidAppId");
227   btif_config_remove(bdstr, "HidVendorId");
228   btif_config_remove(bdstr, "HidProductId");
229   btif_config_remove(bdstr, "HidVersion");
230   btif_config_remove(bdstr, "HidCountryCode");
231   btif_config_remove(bdstr, "HidSSRMaxLatency");
232   btif_config_remove(bdstr, "HidSSRMinTimeout");
233   btif_config_remove(bdstr, "HidDescriptor");
234   return BT_STATUS_SUCCESS;
235 }
236 
237 // Check if a given profile is supported.
btif_device_supports_profile(const std::string & device,const Uuid & profile)238 static bool btif_device_supports_profile(const std::string& device,
239                                          const Uuid& profile) {
240   int size = STORAGE_UUID_STRING_SIZE * BT_MAX_NUM_UUIDS;
241   char uuid_str[size];
242   if (btif_config_get_str(device, BTIF_STORAGE_PATH_REMOTE_SERVICE, uuid_str,
243                           &size)) {
244     Uuid p_uuid[BT_MAX_NUM_UUIDS];
245     size_t num_uuids =
246         btif_split_uuids_string(uuid_str, p_uuid, BT_MAX_NUM_UUIDS);
247     for (size_t i = 0; i < num_uuids; i++) {
248       if (p_uuid[i] == profile) {
249         return true;
250       }
251     }
252   }
253 
254   return false;
255 }
256 
btif_device_supports_hogp(const std::string & device)257 static bool btif_device_supports_hogp(const std::string& device) {
258   return btif_device_supports_profile(device,
259                                       Uuid::From16Bit(UUID_SERVCLASS_LE_HID));
260 }
261 
btif_device_supports_classic_hid(const std::string & device)262 static bool btif_device_supports_classic_hid(const std::string& device) {
263   return btif_device_supports_profile(
264       device, Uuid::From16Bit(UUID_SERVCLASS_HUMAN_INTERFACE));
265 }
266 
267 /*******************************************************************************
268  *
269  * Function         btif_storage_get_le_hid_devices
270  *
271  * Description      BTIF storage API - Finds all bonded LE HID devices
272  *
273  * Returns          std::vector of (RawAddress, AddressType)
274  *
275  ******************************************************************************/
276 
277 bool btif_get_address_type(const RawAddress& bda, tBLE_ADDR_TYPE* p_addr_type);
278 
btif_storage_get_le_hid_devices(void)279 std::vector<std::pair<RawAddress, uint8_t>> btif_storage_get_le_hid_devices(
280     void) {
281   std::vector<std::pair<RawAddress, uint8_t>> hid_addresses;
282   for (const auto& bd_addr : btif_config_get_paired_devices()) {
283     auto name = bd_addr.ToString();
284     if (btif_device_supports_hogp(name)) {
285       tBLE_ADDR_TYPE type = BLE_ADDR_PUBLIC;
286       btif_get_address_type(bd_addr, &type);
287 
288       hid_addresses.push_back({bd_addr, type});
289       LOG_DEBUG("Remote device: %s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
290     }
291   }
292 
293   return hid_addresses;
294 }
295 
btif_storage_get_wake_capable_classic_hid_devices(void)296 std::vector<RawAddress> btif_storage_get_wake_capable_classic_hid_devices(
297     void) {
298   std::vector<RawAddress> hid_addresses;
299   for (const auto& bd_addr : btif_config_get_paired_devices()) {
300     auto name = bd_addr.ToString();
301     if (btif_device_supports_classic_hid(name)) {
302       // Filter out devices that aren't keyboards or pointing devices.
303       // 0x500 = HID Major
304       // 0x080 = Pointing device
305       // 0x040 = Keyboard
306       constexpr int kHidMask = COD_HID_MAJOR;
307       constexpr int kKeyboardMouseMask = COD_HID_COMBO & ~COD_HID_MAJOR;
308       int cod_value;
309       if (!btif_config_get_int(name, BTIF_STORAGE_PATH_REMOTE_DEVCLASS,
310                                &cod_value) ||
311           (cod_value & kHidMask) != kHidMask ||
312           (cod_value & kKeyboardMouseMask) == 0) {
313         continue;
314       }
315 
316       hid_addresses.push_back(bd_addr);
317       LOG_DEBUG("Remote device: %s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
318     }
319   }
320 
321   return hid_addresses;
322 }
323 
324 constexpr char HEARING_AID_READ_PSM_HANDLE[] = "HearingAidReadPsmHandle";
325 constexpr char HEARING_AID_CAPABILITIES[] = "HearingAidCapabilities";
326 constexpr char HEARING_AID_CODECS[] = "HearingAidCodecs";
327 constexpr char HEARING_AID_AUDIO_CONTROL_POINT[] =
328     "HearingAidAudioControlPoint";
329 constexpr char HEARING_AID_VOLUME_HANDLE[] = "HearingAidVolumeHandle";
330 constexpr char HEARING_AID_AUDIO_STATUS_HANDLE[] =
331     "HearingAidAudioStatusHandle";
332 constexpr char HEARING_AID_AUDIO_STATUS_CCC_HANDLE[] =
333     "HearingAidAudioStatusCccHandle";
334 constexpr char HEARING_AID_SERVICE_CHANGED_CCC_HANDLE[] =
335     "HearingAidServiceChangedCccHandle";
336 constexpr char HEARING_AID_SYNC_ID[] = "HearingAidSyncId";
337 constexpr char HEARING_AID_RENDER_DELAY[] = "HearingAidRenderDelay";
338 constexpr char HEARING_AID_PREPARATION_DELAY[] = "HearingAidPreparationDelay";
339 constexpr char HEARING_AID_IS_ACCEPTLISTED[] = "HearingAidIsAcceptlisted";
340 
btif_storage_add_hearing_aid(const HearingDevice & dev_info)341 void btif_storage_add_hearing_aid(const HearingDevice& dev_info) {
342   do_in_jni_thread(
343       FROM_HERE,
344       Bind(
345           [](const HearingDevice& dev_info) {
346             std::string bdstr = dev_info.address.ToString();
347             VLOG(2) << "saving hearing aid device: "
348                     << ADDRESS_TO_LOGGABLE_STR(dev_info.address);
349             btif_config_set_int(bdstr, HEARING_AID_SERVICE_CHANGED_CCC_HANDLE,
350                                 dev_info.service_changed_ccc_handle);
351             btif_config_set_int(bdstr, HEARING_AID_READ_PSM_HANDLE,
352                                 dev_info.read_psm_handle);
353             btif_config_set_int(bdstr, HEARING_AID_CAPABILITIES,
354                                 dev_info.capabilities);
355             btif_config_set_int(bdstr, HEARING_AID_CODECS, dev_info.codecs);
356             btif_config_set_int(bdstr, HEARING_AID_AUDIO_CONTROL_POINT,
357                                 dev_info.audio_control_point_handle);
358             btif_config_set_int(bdstr, HEARING_AID_VOLUME_HANDLE,
359                                 dev_info.volume_handle);
360             btif_config_set_int(bdstr, HEARING_AID_AUDIO_STATUS_HANDLE,
361                                 dev_info.audio_status_handle);
362             btif_config_set_int(bdstr, HEARING_AID_AUDIO_STATUS_CCC_HANDLE,
363                                 dev_info.audio_status_ccc_handle);
364             btif_config_set_uint64(bdstr, HEARING_AID_SYNC_ID,
365                                    dev_info.hi_sync_id);
366             btif_config_set_int(bdstr, HEARING_AID_RENDER_DELAY,
367                                 dev_info.render_delay);
368             btif_config_set_int(bdstr, HEARING_AID_PREPARATION_DELAY,
369                                 dev_info.preparation_delay);
370             btif_config_set_int(bdstr, HEARING_AID_IS_ACCEPTLISTED, true);
371           },
372           dev_info));
373 }
374 
375 /** Loads information about bonded hearing aid devices */
btif_storage_load_bonded_hearing_aids()376 void btif_storage_load_bonded_hearing_aids() {
377   for (const auto& bd_addr : btif_config_get_paired_devices()) {
378     const std::string& name = bd_addr.ToString();
379 
380     int size = STORAGE_UUID_STRING_SIZE * BT_MAX_NUM_UUIDS;
381     char uuid_str[size];
382     bool isHearingaidDevice = false;
383     if (btif_config_get_str(name, BTIF_STORAGE_PATH_REMOTE_SERVICE, uuid_str,
384                             &size)) {
385       Uuid p_uuid[BT_MAX_NUM_UUIDS];
386       size_t num_uuids =
387           btif_split_uuids_string(uuid_str, p_uuid, BT_MAX_NUM_UUIDS);
388       for (size_t i = 0; i < num_uuids; i++) {
389         if (p_uuid[i] == Uuid::FromString("FDF0")) {
390           isHearingaidDevice = true;
391           break;
392         }
393       }
394     }
395     if (!isHearingaidDevice) {
396       continue;
397     }
398 
399     BTIF_TRACE_DEBUG("Remote device:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
400 
401     if (btif_in_fetch_bonded_device(name) != BT_STATUS_SUCCESS) {
402       btif_storage_remove_hearing_aid(bd_addr);
403       continue;
404     }
405 
406     int value;
407     uint8_t capabilities = 0;
408     if (btif_config_get_int(name, HEARING_AID_CAPABILITIES, &value))
409       capabilities = value;
410 
411     uint16_t codecs = 0;
412     if (btif_config_get_int(name, HEARING_AID_CODECS, &value)) codecs = value;
413 
414     uint16_t audio_control_point_handle = 0;
415     if (btif_config_get_int(name, HEARING_AID_AUDIO_CONTROL_POINT, &value))
416       audio_control_point_handle = value;
417 
418     uint16_t audio_status_handle = 0;
419     if (btif_config_get_int(name, HEARING_AID_AUDIO_STATUS_HANDLE, &value))
420       audio_status_handle = value;
421 
422     uint16_t audio_status_ccc_handle = 0;
423     if (btif_config_get_int(name, HEARING_AID_AUDIO_STATUS_CCC_HANDLE, &value))
424       audio_status_ccc_handle = value;
425 
426     uint16_t service_changed_ccc_handle = 0;
427     if (btif_config_get_int(name, HEARING_AID_SERVICE_CHANGED_CCC_HANDLE,
428                             &value))
429       service_changed_ccc_handle = value;
430 
431     uint16_t volume_handle = 0;
432     if (btif_config_get_int(name, HEARING_AID_VOLUME_HANDLE, &value))
433       volume_handle = value;
434 
435     uint16_t read_psm_handle = 0;
436     if (btif_config_get_int(name, HEARING_AID_READ_PSM_HANDLE, &value))
437       read_psm_handle = value;
438 
439     uint64_t lvalue;
440     uint64_t hi_sync_id = 0;
441     if (btif_config_get_uint64(name, HEARING_AID_SYNC_ID, &lvalue))
442       hi_sync_id = lvalue;
443 
444     uint16_t render_delay = 0;
445     if (btif_config_get_int(name, HEARING_AID_RENDER_DELAY, &value))
446       render_delay = value;
447 
448     uint16_t preparation_delay = 0;
449     if (btif_config_get_int(name, HEARING_AID_PREPARATION_DELAY, &value))
450       preparation_delay = value;
451 
452     uint16_t is_acceptlisted = 0;
453     if (btif_config_get_int(name, HEARING_AID_IS_ACCEPTLISTED, &value))
454       is_acceptlisted = value;
455 
456     // add extracted information to BTA Hearing Aid
457     do_in_main_thread(
458         FROM_HERE,
459         Bind(&HearingAid::AddFromStorage,
460              HearingDevice(bd_addr, capabilities, codecs,
461                            audio_control_point_handle, audio_status_handle,
462                            audio_status_ccc_handle, service_changed_ccc_handle,
463                            volume_handle, read_psm_handle, hi_sync_id,
464                            render_delay, preparation_delay),
465              is_acceptlisted));
466   }
467 }
468 
469 /** Deletes the bonded hearing aid device info from NVRAM */
btif_storage_remove_hearing_aid(const RawAddress & address)470 void btif_storage_remove_hearing_aid(const RawAddress& address) {
471   std::string addrstr = address.ToString();
472   btif_config_remove(addrstr, HEARING_AID_READ_PSM_HANDLE);
473   btif_config_remove(addrstr, HEARING_AID_CAPABILITIES);
474   btif_config_remove(addrstr, HEARING_AID_CODECS);
475   btif_config_remove(addrstr, HEARING_AID_AUDIO_CONTROL_POINT);
476   btif_config_remove(addrstr, HEARING_AID_VOLUME_HANDLE);
477   btif_config_remove(addrstr, HEARING_AID_AUDIO_STATUS_HANDLE);
478   btif_config_remove(addrstr, HEARING_AID_AUDIO_STATUS_CCC_HANDLE);
479   btif_config_remove(addrstr, HEARING_AID_SERVICE_CHANGED_CCC_HANDLE);
480   btif_config_remove(addrstr, HEARING_AID_SYNC_ID);
481   btif_config_remove(addrstr, HEARING_AID_RENDER_DELAY);
482   btif_config_remove(addrstr, HEARING_AID_PREPARATION_DELAY);
483   btif_config_remove(addrstr, HEARING_AID_IS_ACCEPTLISTED);
484 }
485 
486 /** Set/Unset the hearing aid device HEARING_AID_IS_ACCEPTLISTED flag. */
btif_storage_set_hearing_aid_acceptlist(const RawAddress & address,bool add_to_acceptlist)487 void btif_storage_set_hearing_aid_acceptlist(const RawAddress& address,
488                                              bool add_to_acceptlist) {
489   std::string addrstr = address.ToString();
490 
491   btif_config_set_int(addrstr, HEARING_AID_IS_ACCEPTLISTED, add_to_acceptlist);
492 }
493 
494 /** Get the hearing aid device properties. */
btif_storage_get_hearing_aid_prop(const RawAddress & address,uint8_t * capabilities,uint64_t * hi_sync_id,uint16_t * render_delay,uint16_t * preparation_delay,uint16_t * codecs)495 bool btif_storage_get_hearing_aid_prop(
496     const RawAddress& address, uint8_t* capabilities, uint64_t* hi_sync_id,
497     uint16_t* render_delay, uint16_t* preparation_delay, uint16_t* codecs) {
498   std::string addrstr = address.ToString();
499 
500   int value;
501   if (btif_config_get_int(addrstr, HEARING_AID_CAPABILITIES, &value)) {
502     *capabilities = value;
503   } else {
504     return false;
505   }
506 
507   if (btif_config_get_int(addrstr, HEARING_AID_CODECS, &value)) {
508     *codecs = value;
509   } else {
510     return false;
511   }
512 
513   if (btif_config_get_int(addrstr, HEARING_AID_RENDER_DELAY, &value)) {
514     *render_delay = value;
515   } else {
516     return false;
517   }
518 
519   if (btif_config_get_int(addrstr, HEARING_AID_PREPARATION_DELAY, &value)) {
520     *preparation_delay = value;
521   } else {
522     return false;
523   }
524 
525   uint64_t lvalue;
526   if (btif_config_get_uint64(addrstr, HEARING_AID_SYNC_ID, &lvalue)) {
527     *hi_sync_id = lvalue;
528   } else {
529     return false;
530   }
531 
532   return true;
533 }
534 
535 /** Set autoconnect information for LeAudio device */
btif_storage_set_leaudio_autoconnect(const RawAddress & addr,bool autoconnect)536 void btif_storage_set_leaudio_autoconnect(const RawAddress& addr,
537                                           bool autoconnect) {
538   do_in_jni_thread(FROM_HERE, Bind(
539                                   [](const RawAddress& addr, bool autoconnect) {
540                                     std::string bdstr = addr.ToString();
541                                     VLOG(2) << "saving le audio device: "
542                                             << ADDRESS_TO_LOGGABLE_CSTR(addr);
543                                     btif_config_set_int(
544                                         bdstr, BTIF_STORAGE_LEAUDIO_AUTOCONNECT,
545                                         autoconnect);
546                                   },
547                                   addr, autoconnect));
548 }
549 
550 /** Store ASEs information */
btif_storage_leaudio_update_handles_bin(const RawAddress & addr)551 void btif_storage_leaudio_update_handles_bin(const RawAddress& addr) {
552   std::vector<uint8_t> handles;
553 
554   if (LeAudioClient::GetHandlesForStorage(addr, handles)) {
555     do_in_jni_thread(
556         FROM_HERE,
557         Bind(
558             [](const RawAddress& bd_addr, std::vector<uint8_t> handles) {
559               auto bdstr = bd_addr.ToString();
560               btif_config_set_bin(bdstr, BTIF_STORAGE_LEAUDIO_HANDLES_BIN,
561                                   handles.data(), handles.size());
562             },
563             addr, std::move(handles)));
564   }
565 }
566 
567 /** Store PACs information */
btif_storage_leaudio_update_pacs_bin(const RawAddress & addr)568 void btif_storage_leaudio_update_pacs_bin(const RawAddress& addr) {
569   std::vector<uint8_t> sink_pacs;
570 
571   if (LeAudioClient::GetSinkPacsForStorage(addr, sink_pacs)) {
572     do_in_jni_thread(
573         FROM_HERE,
574         Bind(
575             [](const RawAddress& bd_addr, std::vector<uint8_t> sink_pacs) {
576               auto bdstr = bd_addr.ToString();
577               btif_config_set_bin(bdstr, BTIF_STORAGE_LEAUDIO_SINK_PACS_BIN,
578                                   sink_pacs.data(), sink_pacs.size());
579             },
580             addr, std::move(sink_pacs)));
581   }
582 
583   std::vector<uint8_t> source_pacs;
584   if (LeAudioClient::GetSourcePacsForStorage(addr, source_pacs)) {
585     do_in_jni_thread(
586         FROM_HERE,
587         Bind(
588             [](const RawAddress& bd_addr, std::vector<uint8_t> source_pacs) {
589               auto bdstr = bd_addr.ToString();
590               btif_config_set_bin(bdstr, BTIF_STORAGE_LEAUDIO_SOURCE_PACS_BIN,
591                                   source_pacs.data(), source_pacs.size());
592             },
593             addr, std::move(source_pacs)));
594   }
595 }
596 
597 /** Store ASEs information */
btif_storage_leaudio_update_ase_bin(const RawAddress & addr)598 void btif_storage_leaudio_update_ase_bin(const RawAddress& addr) {
599   std::vector<uint8_t> ases;
600 
601   if (LeAudioClient::GetAsesForStorage(addr, ases)) {
602     do_in_jni_thread(
603         FROM_HERE,
604         Bind(
605             [](const RawAddress& bd_addr, std::vector<uint8_t> ases) {
606               auto bdstr = bd_addr.ToString();
607               btif_config_set_bin(bdstr, BTIF_STORAGE_LEAUDIO_ASES_BIN,
608                                   ases.data(), ases.size());
609             },
610             addr, std::move(ases)));
611   }
612 }
613 
614 /** Store Le Audio device audio locations */
btif_storage_set_leaudio_audio_location(const RawAddress & addr,uint32_t sink_location,uint32_t source_location)615 void btif_storage_set_leaudio_audio_location(const RawAddress& addr,
616                                              uint32_t sink_location,
617                                              uint32_t source_location) {
618   do_in_jni_thread(
619       FROM_HERE,
620       Bind(
621           [](const RawAddress& addr, int sink_location, int source_location) {
622             std::string bdstr = addr.ToString();
623             LOG_DEBUG("saving le audio device: %s",
624                       ADDRESS_TO_LOGGABLE_CSTR(addr));
625             btif_config_set_int(bdstr, BTIF_STORAGE_LEAUDIO_SINK_AUDIOLOCATION,
626                                 sink_location);
627             btif_config_set_int(bdstr,
628                                 BTIF_STORAGE_LEAUDIO_SOURCE_AUDIOLOCATION,
629                                 source_location);
630           },
631           addr, sink_location, source_location));
632 }
633 
634 /** Store Le Audio device context types */
btif_storage_set_leaudio_supported_context_types(const RawAddress & addr,uint16_t sink_supported_context_type,uint16_t source_supported_context_type)635 void btif_storage_set_leaudio_supported_context_types(
636     const RawAddress& addr, uint16_t sink_supported_context_type,
637     uint16_t source_supported_context_type) {
638   do_in_jni_thread(
639       FROM_HERE,
640       Bind(
641           [](const RawAddress& addr, int sink_supported_context_type,
642              int source_supported_context_type) {
643             std::string bdstr = addr.ToString();
644             LOG_DEBUG("saving le audio device: %s",
645                       ADDRESS_TO_LOGGABLE_CSTR(addr));
646             btif_config_set_int(
647                 bdstr, BTIF_STORAGE_LEAUDIO_SINK_SUPPORTED_CONTEXT_TYPE,
648                 sink_supported_context_type);
649             btif_config_set_int(
650                 bdstr, BTIF_STORAGE_LEAUDIO_SOURCE_SUPPORTED_CONTEXT_TYPE,
651                 source_supported_context_type);
652           },
653           addr, sink_supported_context_type, source_supported_context_type));
654 }
655 
656 /** Loads information about bonded Le Audio devices */
btif_storage_load_bonded_leaudio()657 void btif_storage_load_bonded_leaudio() {
658   for (const auto& bd_addr : btif_config_get_paired_devices()) {
659     auto name = bd_addr.ToString();
660 
661     int size = STORAGE_UUID_STRING_SIZE * BT_MAX_NUM_UUIDS;
662     char uuid_str[size];
663     bool isLeAudioDevice = false;
664     if (btif_config_get_str(name, BTIF_STORAGE_PATH_REMOTE_SERVICE, uuid_str,
665                             &size)) {
666       Uuid p_uuid[BT_MAX_NUM_UUIDS];
667       size_t num_uuids =
668           btif_split_uuids_string(uuid_str, p_uuid, BT_MAX_NUM_UUIDS);
669       for (size_t i = 0; i < num_uuids; i++) {
670         if (p_uuid[i] == Uuid::FromString("184E")) {
671           isLeAudioDevice = true;
672           break;
673         }
674       }
675     }
676     if (!isLeAudioDevice) {
677       continue;
678     }
679 
680     BTIF_TRACE_DEBUG("Remote device:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
681 
682     int value;
683     bool autoconnect = false;
684     if (btif_config_get_int(name, BTIF_STORAGE_LEAUDIO_AUTOCONNECT, &value))
685       autoconnect = !!value;
686 
687     int sink_audio_location = 0;
688     if (btif_config_get_int(name, BTIF_STORAGE_LEAUDIO_SINK_AUDIOLOCATION,
689                             &value))
690       sink_audio_location = value;
691 
692     int source_audio_location = 0;
693     if (btif_config_get_int(name, BTIF_STORAGE_LEAUDIO_SOURCE_AUDIOLOCATION,
694                             &value))
695       source_audio_location = value;
696 
697     int sink_supported_context_type = 0;
698     if (btif_config_get_int(
699             name, BTIF_STORAGE_LEAUDIO_SINK_SUPPORTED_CONTEXT_TYPE, &value))
700       sink_supported_context_type = value;
701 
702     int source_supported_context_type = 0;
703     if (btif_config_get_int(
704             name, BTIF_STORAGE_LEAUDIO_SOURCE_SUPPORTED_CONTEXT_TYPE, &value))
705       source_supported_context_type = value;
706 
707     size_t buffer_size =
708         btif_config_get_bin_length(name, BTIF_STORAGE_LEAUDIO_HANDLES_BIN);
709     std::vector<uint8_t> handles(buffer_size);
710     if (buffer_size > 0) {
711       btif_config_get_bin(name, BTIF_STORAGE_LEAUDIO_HANDLES_BIN,
712                           handles.data(), &buffer_size);
713     }
714 
715     buffer_size =
716         btif_config_get_bin_length(name, BTIF_STORAGE_LEAUDIO_SINK_PACS_BIN);
717     std::vector<uint8_t> sink_pacs(buffer_size);
718     if (buffer_size > 0) {
719       btif_config_get_bin(name, BTIF_STORAGE_LEAUDIO_SINK_PACS_BIN,
720                           sink_pacs.data(), &buffer_size);
721     }
722 
723     buffer_size =
724         btif_config_get_bin_length(name, BTIF_STORAGE_LEAUDIO_SOURCE_PACS_BIN);
725     std::vector<uint8_t> source_pacs(buffer_size);
726     if (buffer_size > 0) {
727       btif_config_get_bin(name, BTIF_STORAGE_LEAUDIO_SOURCE_PACS_BIN,
728                           source_pacs.data(), &buffer_size);
729     }
730 
731     buffer_size =
732         btif_config_get_bin_length(name, BTIF_STORAGE_LEAUDIO_ASES_BIN);
733     std::vector<uint8_t> ases(buffer_size);
734     if (buffer_size > 0) {
735       btif_config_get_bin(name, BTIF_STORAGE_LEAUDIO_ASES_BIN, ases.data(),
736                           &buffer_size);
737     }
738 
739     do_in_main_thread(
740         FROM_HERE,
741         Bind(&LeAudioClient::AddFromStorage, bd_addr, autoconnect,
742              sink_audio_location, source_audio_location,
743              sink_supported_context_type, source_supported_context_type,
744              std::move(handles), std::move(sink_pacs), std::move(source_pacs),
745              std::move(ases)));
746   }
747 }
748 
749 /** Remove the Le Audio device from storage */
btif_storage_remove_leaudio(const RawAddress & address)750 void btif_storage_remove_leaudio(const RawAddress& address) {
751   std::string addrstr = address.ToString();
752   btif_config_set_int(addrstr, BTIF_STORAGE_LEAUDIO_AUTOCONNECT, false);
753 }
754 
755 constexpr char HAS_IS_ACCEPTLISTED[] = "LeAudioHasIsAcceptlisted";
756 constexpr char HAS_FEATURES[] = "LeAudioHasFlags";
757 constexpr char HAS_ACTIVE_PRESET[] = "LeAudioHasActivePreset";
758 constexpr char HAS_SERIALIZED_PRESETS[] = "LeAudioHasSerializedPresets";
759 
btif_storage_add_leaudio_has_device(const RawAddress & address,std::vector<uint8_t> presets_bin,uint8_t features,uint8_t active_preset)760 void btif_storage_add_leaudio_has_device(const RawAddress& address,
761                                          std::vector<uint8_t> presets_bin,
762                                          uint8_t features,
763                                          uint8_t active_preset) {
764   do_in_jni_thread(
765       FROM_HERE,
766       Bind(
767           [](const RawAddress& address, std::vector<uint8_t> presets_bin,
768              uint8_t features, uint8_t active_preset) {
769             const std::string& name = address.ToString();
770 
771             btif_config_set_int(name, HAS_FEATURES, features);
772             btif_config_set_int(name, HAS_ACTIVE_PRESET, active_preset);
773             btif_config_set_bin(name, HAS_SERIALIZED_PRESETS,
774                                 presets_bin.data(), presets_bin.size());
775 
776             btif_config_set_int(name, HAS_IS_ACCEPTLISTED, true);
777           },
778           address, std::move(presets_bin), features, active_preset));
779 }
780 
btif_storage_set_leaudio_has_active_preset(const RawAddress & address,uint8_t active_preset)781 void btif_storage_set_leaudio_has_active_preset(const RawAddress& address,
782                                                 uint8_t active_preset) {
783   do_in_jni_thread(FROM_HERE,
784                    Bind(
785                        [](const RawAddress& address, uint8_t active_preset) {
786                          const std::string& name = address.ToString();
787 
788                          btif_config_set_int(name, HAS_ACTIVE_PRESET,
789                                              active_preset);
790                        },
791                        address, active_preset));
792 }
793 
btif_storage_get_leaudio_has_features(const RawAddress & address,uint8_t & features)794 bool btif_storage_get_leaudio_has_features(const RawAddress& address,
795                                            uint8_t& features) {
796   std::string name = address.ToString();
797 
798   int value;
799   if (!btif_config_get_int(name, HAS_FEATURES, &value)) return false;
800 
801   features = value;
802   return true;
803 }
804 
btif_storage_set_leaudio_has_features(const RawAddress & address,uint8_t features)805 void btif_storage_set_leaudio_has_features(const RawAddress& address,
806                                            uint8_t features) {
807   do_in_jni_thread(FROM_HERE,
808                    Bind(
809                        [](const RawAddress& address, uint8_t features) {
810                          const std::string& name = address.ToString();
811 
812                          btif_config_set_int(name, HAS_FEATURES, features);
813                        },
814                        address, features));
815 }
816 
btif_storage_load_bonded_leaudio_has_devices()817 void btif_storage_load_bonded_leaudio_has_devices() {
818   for (const auto& bd_addr : btif_config_get_paired_devices()) {
819     const std::string& name = bd_addr.ToString();
820 
821     if (!btif_config_exist(name, HAS_IS_ACCEPTLISTED) &&
822         !btif_config_exist(name, HAS_FEATURES))
823       continue;
824 
825 #ifndef TARGET_FLOSS
826     int value;
827     uint16_t is_acceptlisted = 0;
828     if (btif_config_get_int(name, HAS_IS_ACCEPTLISTED, &value))
829       is_acceptlisted = value;
830 
831     uint8_t features = 0;
832     if (btif_config_get_int(name, HAS_FEATURES, &value)) features = value;
833 
834     do_in_main_thread(FROM_HERE, Bind(&le_audio::has::HasClient::AddFromStorage,
835                                       bd_addr, features, is_acceptlisted));
836 #else
837     ASSERT_LOG(false, "TODO - Fix LE audio build.");
838 #endif
839   }
840 }
841 
btif_storage_remove_leaudio_has(const RawAddress & address)842 void btif_storage_remove_leaudio_has(const RawAddress& address) {
843   std::string addrstr = address.ToString();
844   btif_config_remove(addrstr, HAS_IS_ACCEPTLISTED);
845   btif_config_remove(addrstr, HAS_FEATURES);
846   btif_config_remove(addrstr, HAS_ACTIVE_PRESET);
847   btif_config_remove(addrstr, HAS_SERIALIZED_PRESETS);
848 }
849 
btif_storage_set_leaudio_has_acceptlist(const RawAddress & address,bool add_to_acceptlist)850 void btif_storage_set_leaudio_has_acceptlist(const RawAddress& address,
851                                              bool add_to_acceptlist) {
852   std::string addrstr = address.ToString();
853 
854   btif_config_set_int(addrstr, HAS_IS_ACCEPTLISTED, add_to_acceptlist);
855 }
856 
btif_storage_set_leaudio_has_presets(const RawAddress & address,std::vector<uint8_t> presets_bin)857 void btif_storage_set_leaudio_has_presets(const RawAddress& address,
858                                           std::vector<uint8_t> presets_bin) {
859   do_in_jni_thread(
860       FROM_HERE,
861       Bind(
862           [](const RawAddress& address, std::vector<uint8_t> presets_bin) {
863             const std::string& name = address.ToString();
864 
865             btif_config_set_bin(name, HAS_SERIALIZED_PRESETS,
866                                 presets_bin.data(), presets_bin.size());
867           },
868           address, std::move(presets_bin)));
869 }
870 
btif_storage_get_leaudio_has_presets(const RawAddress & address,std::vector<uint8_t> & presets_bin,uint8_t & active_preset)871 bool btif_storage_get_leaudio_has_presets(const RawAddress& address,
872                                           std::vector<uint8_t>& presets_bin,
873                                           uint8_t& active_preset) {
874   std::string name = address.ToString();
875 
876   int value;
877   if (!btif_config_get_int(name, HAS_ACTIVE_PRESET, &value)) return false;
878   active_preset = value;
879 
880   auto bin_sz = btif_config_get_bin_length(name, HAS_SERIALIZED_PRESETS);
881   presets_bin.resize(bin_sz);
882   if (!btif_config_get_bin(name, HAS_SERIALIZED_PRESETS, presets_bin.data(),
883                            &bin_sz))
884     return false;
885 
886   return true;
887 }
888 
889 /** Adds the bonded Le Audio device grouping info into the NVRAM */
btif_storage_add_groups(const RawAddress & addr)890 void btif_storage_add_groups(const RawAddress& addr) {
891   std::vector<uint8_t> group_info;
892   auto not_empty = DeviceGroups::GetForStorage(addr, group_info);
893 
894   if (not_empty)
895     do_in_jni_thread(
896         FROM_HERE,
897         Bind(
898             [](const RawAddress& bd_addr, std::vector<uint8_t> group_info) {
899               auto bdstr = bd_addr.ToString();
900               btif_config_set_bin(bdstr, BTIF_STORAGE_DEVICE_GROUP_BIN,
901                                   group_info.data(), group_info.size());
902             },
903             addr, std::move(group_info)));
904 }
905 
906 /** Deletes the bonded Le Audio device grouping info from the NVRAM */
btif_storage_remove_groups(const RawAddress & address)907 void btif_storage_remove_groups(const RawAddress& address) {
908   std::string addrstr = address.ToString();
909   btif_config_remove(addrstr, BTIF_STORAGE_DEVICE_GROUP_BIN);
910 }
911 
912 /** Loads information about bonded group devices */
btif_storage_load_bonded_groups(void)913 void btif_storage_load_bonded_groups(void) {
914   for (const auto& bd_addr : btif_config_get_paired_devices()) {
915     auto name = bd_addr.ToString();
916     size_t buffer_size =
917         btif_config_get_bin_length(name, BTIF_STORAGE_DEVICE_GROUP_BIN);
918     if (buffer_size == 0) continue;
919 
920     BTIF_TRACE_DEBUG("Grouped device:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
921 
922     std::vector<uint8_t> in(buffer_size);
923     if (btif_config_get_bin(name, BTIF_STORAGE_DEVICE_GROUP_BIN, in.data(),
924                             &buffer_size)) {
925       do_in_main_thread(FROM_HERE, Bind(&DeviceGroups::AddFromStorage, bd_addr,
926                                         std::move(in)));
927     }
928   }
929 }
930 
btif_storage_set_csis_autoconnect(const RawAddress & addr,bool autoconnect)931 void btif_storage_set_csis_autoconnect(const RawAddress& addr,
932                                        bool autoconnect) {
933   do_in_jni_thread(FROM_HERE, Bind(
934                                   [](const RawAddress& addr, bool autoconnect) {
935                                     std::string bdstr = addr.ToString();
936                                     VLOG(2) << "Storing CSIS device: "
937                                             << ADDRESS_TO_LOGGABLE_CSTR(addr);
938                                     btif_config_set_int(
939                                         bdstr, BTIF_STORAGE_CSIS_AUTOCONNECT,
940                                         autoconnect);
941                                   },
942                                   addr, autoconnect));
943 }
944 
945 /** Stores information about the bonded CSIS device */
btif_storage_update_csis_info(const RawAddress & addr)946 void btif_storage_update_csis_info(const RawAddress& addr) {
947   std::vector<uint8_t> set_info;
948   auto not_empty = CsisClient::GetForStorage(addr, set_info);
949 
950   if (not_empty)
951     do_in_jni_thread(
952         FROM_HERE,
953         Bind(
954             [](const RawAddress& bd_addr, std::vector<uint8_t> set_info) {
955               auto bdstr = bd_addr.ToString();
956               btif_config_set_bin(bdstr, BTIF_STORAGE_CSIS_SET_INFO_BIN,
957                                   set_info.data(), set_info.size());
958             },
959             addr, std::move(set_info)));
960 }
961 
962 /** Loads information about the bonded CSIS device */
btif_storage_load_bonded_csis_devices(void)963 void btif_storage_load_bonded_csis_devices(void) {
964   for (const auto& bd_addr : btif_config_get_paired_devices()) {
965     auto name = bd_addr.ToString();
966 
967     BTIF_TRACE_DEBUG("Loading CSIS device:%s",
968                      ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
969 
970     int value;
971     bool autoconnect = false;
972     if (btif_config_get_int(name, BTIF_STORAGE_CSIS_AUTOCONNECT, &value))
973       autoconnect = !!value;
974 
975     size_t buffer_size =
976         btif_config_get_bin_length(name, BTIF_STORAGE_CSIS_SET_INFO_BIN);
977     std::vector<uint8_t> in(buffer_size);
978     if (buffer_size != 0)
979       btif_config_get_bin(name, BTIF_STORAGE_CSIS_SET_INFO_BIN, in.data(),
980                           &buffer_size);
981 
982     if (buffer_size != 0 || autoconnect)
983       do_in_main_thread(FROM_HERE, Bind(&CsisClient::AddFromStorage, bd_addr,
984                                         std::move(in), autoconnect));
985   }
986 }
987 
988 /** Removes information about the bonded CSIS device */
btif_storage_remove_csis_device(const RawAddress & address)989 void btif_storage_remove_csis_device(const RawAddress& address) {
990   std::string addrstr = address.ToString();
991   btif_config_remove(addrstr, BTIF_STORAGE_CSIS_AUTOCONNECT);
992   btif_config_remove(addrstr, BTIF_STORAGE_CSIS_SET_INFO_BIN);
993 }
994 
995 /*******************************************************************************
996  * Function         btif_storage_load_hidd
997  *
998  * Description      Loads hidd bonded device and "plugs" it into hidd
999  *
1000  * Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
1001  *
1002  ******************************************************************************/
btif_storage_load_hidd(void)1003 bt_status_t btif_storage_load_hidd(void) {
1004   for (const auto& bd_addr : btif_config_get_paired_devices()) {
1005     auto name = bd_addr.ToString();
1006 
1007     BTIF_TRACE_DEBUG("Remote device:%s", ADDRESS_TO_LOGGABLE_CSTR(bd_addr));
1008     int value;
1009     if (btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS) {
1010       if (btif_config_get_int(name, "HidDeviceCabled", &value)) {
1011         BTA_HdAddDevice(bd_addr);
1012         break;
1013       }
1014     }
1015   }
1016 
1017   return BT_STATUS_SUCCESS;
1018 }
1019 
1020 /*******************************************************************************
1021  *
1022  * Function         btif_storage_set_hidd
1023  *
1024  * Description      Stores currently used HIDD device info in nvram and remove
1025  *                  the "HidDeviceCabled" flag from unused devices
1026  *
1027  * Returns          BT_STATUS_SUCCESS
1028  *
1029  ******************************************************************************/
btif_storage_set_hidd(const RawAddress & remote_bd_addr)1030 bt_status_t btif_storage_set_hidd(const RawAddress& remote_bd_addr) {
1031   std::string remote_device_address_string = remote_bd_addr.ToString();
1032   for (const auto& bd_addr : btif_config_get_paired_devices()) {
1033     auto name = bd_addr.ToString();
1034     if (bd_addr == remote_bd_addr) continue;
1035     if (btif_in_fetch_bonded_device(name) == BT_STATUS_SUCCESS) {
1036       btif_config_remove(name, "HidDeviceCabled");
1037     }
1038   }
1039 
1040   btif_config_set_int(remote_device_address_string, "HidDeviceCabled", 1);
1041   return BT_STATUS_SUCCESS;
1042 }
1043 
1044 /*******************************************************************************
1045  *
1046  * Function         btif_storage_remove_hidd
1047  *
1048  * Description      Removes hidd bonded device info from nvram
1049  *
1050  * Returns          BT_STATUS_SUCCESS
1051  *
1052  ******************************************************************************/
btif_storage_remove_hidd(RawAddress * remote_bd_addr)1053 bt_status_t btif_storage_remove_hidd(RawAddress* remote_bd_addr) {
1054   btif_config_remove(remote_bd_addr->ToString(), "HidDeviceCabled");
1055 
1056   return BT_STATUS_SUCCESS;
1057 }
1058 
1059 /*******************************************************************************
1060  *
1061  *Function : btif_storage_set_pce_profile_version
1062  *
1063  * Description :
1064  *    This function store remote PCE profile version in config file
1065  *
1066  ******************************************************************************/
btif_storage_set_pce_profile_version(const RawAddress & remote_bd_addr,uint16_t peer_pce_version)1067 void btif_storage_set_pce_profile_version(const RawAddress& remote_bd_addr,
1068                                           uint16_t peer_pce_version) {
1069   BTIF_TRACE_DEBUG("peer_pce_version : 0x%x", peer_pce_version);
1070 
1071   if (btif_config_set_bin(
1072           remote_bd_addr.ToString(), BT_CONFIG_KEY_PBAP_PCE_VERSION,
1073           (const uint8_t*)&peer_pce_version, sizeof(peer_pce_version))) {
1074   } else {
1075     BTIF_TRACE_WARNING("Failed to store  peer_pce_version for %s",
1076                        ADDRESS_TO_LOGGABLE_CSTR(remote_bd_addr));
1077   }
1078 }
1079 
1080 /*******************************************************************************
1081  *
1082  * Function        btif_storage_is_pce_version_102
1083  *
1084  * Description     checks if remote supports PBAP 1.2
1085  *
1086  * Returns         true/false depending on remote PBAP version support found in
1087  *file.
1088  *
1089  ******************************************************************************/
btif_storage_is_pce_version_102(const RawAddress & remote_bd_addr)1090 bool btif_storage_is_pce_version_102(const RawAddress& remote_bd_addr) {
1091   bool entry_found = false;
1092   // Read and restore the PBAP PCE version from local storage
1093   uint16_t pce_version = 0;
1094   size_t version_value_size = sizeof(pce_version);
1095   if (!btif_config_get_bin(remote_bd_addr.ToString(),
1096                            BT_CONFIG_KEY_PBAP_PCE_VERSION,
1097                            (uint8_t*)&pce_version, &version_value_size)) {
1098     BTIF_TRACE_DEBUG("Failed to read cached peer PCE version for %s",
1099                      ADDRESS_TO_LOGGABLE_CSTR(remote_bd_addr));
1100     return entry_found;
1101   }
1102 
1103   if (pce_version == 0x0102) {
1104     entry_found = true;
1105   }
1106 
1107   BTIF_TRACE_DEBUG("read cached peer PCE version 0x%04x for %s", pce_version,
1108                    ADDRESS_TO_LOGGABLE_CSTR(remote_bd_addr));
1109 
1110   return entry_found;
1111 }
1112