1 /******************************************************************************
2 *
3 * Copyright (C) 2018 The Linux Foundation
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
19 #include <bluetooth/log.h>
20
21 #include <cstdint>
22 #include <cstring>
23
24 #include "bt_name.h"
25 #include "bta_sec_api.h"
26 #include "btif_storage.h"
27 #include "device/include/device_iot_conf_defs.h"
28 #include "device/include/device_iot_config.h"
29 #include "hardware/bluetooth.h"
30 #include "stack/include/btm_ble_api.h"
31 #include "stack/include/btm_client_interface.h"
32 #include "types/raw_address.h"
33
34 // TODO(b/369381361) Enfore -Wmissing-prototypes
35 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
36
37 using namespace bluetooth;
38
39 /*******************************************************************************
40 * Constants & Macros
41 ******************************************************************************/
42 #define COD_UNCLASSIFIED ((0x1F) << 8)
43
44 /*******************************************************************************
45 *
46 * Function btif_iot_save_pair_type
47 *
48 * Description Store remote pair type to iot conf file
49 *
50 * Returns void
51 *
52 *******************************************************************************/
btif_iot_save_pair_type(const RawAddress & bdaddr,bool is_ble,bool is_ssp)53 static void btif_iot_save_pair_type(const RawAddress& bdaddr, bool is_ble, bool is_ssp) {
54 if (is_ssp) {
55 if (!is_ble) {
56 DEVICE_IOT_CONFIG_ADDR_SET_INT(bdaddr, IOT_CONF_KEY_PAIRTYPE, IOT_CONF_VAL_PAIR_TYPE_SSP);
57 } else {
58 DEVICE_IOT_CONFIG_ADDR_SET_INT(bdaddr, IOT_CONF_KEY_LE_PAIRTYPE,
59 IOT_CONF_VAL_LE_PAIRTYPE_SECURE);
60 }
61 } else {
62 if (!is_ble) {
63 DEVICE_IOT_CONFIG_ADDR_SET_INT(bdaddr, IOT_CONF_KEY_PAIRTYPE, IOT_CONF_VAL_PAIR_TYPE_LEGACY);
64 } else {
65 DEVICE_IOT_CONFIG_ADDR_SET_INT(bdaddr, IOT_CONF_KEY_LE_PAIRTYPE,
66 IOT_CONF_VAL_LE_PAIRTYPE_LEGACY);
67 }
68 }
69 }
70
71 /*******************************************************************************
72 *
73 * Function btif_iot_update_remote_info
74 *
75 * Description Store remote dev info to iot conf file
76 *
77 * Returns void
78 *
79 *******************************************************************************/
btif_iot_update_remote_info(tBTA_DM_AUTH_CMPL * p_auth_cmpl,bool is_ble,bool is_ssp)80 void btif_iot_update_remote_info(tBTA_DM_AUTH_CMPL* p_auth_cmpl, bool is_ble, bool is_ssp) {
81 int name_length = 0;
82 char value[1024];
83 BD_NAME bd_name;
84 int num_properties = 0;
85 bt_property_t properties[2];
86 uint32_t cod = 0;
87 uint8_t lmp_ver = 0;
88 uint16_t lmp_subver = 0;
89 uint16_t mfct_set = 0;
90 // save remote name to iot conf file
91 if (strlen((const char*)p_auth_cmpl->bd_name)) {
92 name_length = strlen((char*)p_auth_cmpl->bd_name) > BD_NAME_LEN
93 ? BD_NAME_LEN
94 : strlen((char*)p_auth_cmpl->bd_name) + 1;
95 strncpy(value, (char*)p_auth_cmpl->bd_name, name_length);
96 DEVICE_IOT_CONFIG_ADDR_SET_STR(p_auth_cmpl->bd_addr, IOT_CONF_KEY_REMOTE_NAME, value);
97 } else {
98 if (BTM_GetRemoteDeviceName(p_auth_cmpl->bd_addr, bd_name)) {
99 DEVICE_IOT_CONFIG_ADDR_SET_STR(p_auth_cmpl->bd_addr, IOT_CONF_KEY_REMOTE_NAME,
100 (char*)bd_name);
101 }
102 }
103
104 // save remote dev class to iot conf file
105 // Try to retrieve cod from storage
106 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod),
107 &cod);
108 if (btif_storage_get_remote_device_property(&p_auth_cmpl->bd_addr, &properties[num_properties]) ==
109 BT_STATUS_SUCCESS) {
110 log::verbose("cod retrieved from storage is 0x{:06x}", cod);
111 }
112 if (cod == 0) {
113 log::verbose("cod is 0, set as unclassified");
114 cod = COD_UNCLASSIFIED;
115 }
116 DEVICE_IOT_CONFIG_ADDR_SET_INT(p_auth_cmpl->bd_addr, IOT_CONF_KEY_DEVCLASS, (int)cod);
117 num_properties++;
118
119 // save remote dev type to iot conf file
120 bt_device_type_t dev_type;
121 uint32_t remote_dev_type;
122 BTIF_STORAGE_FILL_PROPERTY(&properties[num_properties], BT_PROPERTY_TYPE_OF_DEVICE,
123 sizeof(uint32_t), &remote_dev_type);
124 if (btif_storage_get_remote_device_property(&p_auth_cmpl->bd_addr, &properties[num_properties]) ==
125 BT_STATUS_SUCCESS) {
126 log::verbose("retrieve dev type from storage");
127 dev_type = (bt_device_type_t)(remote_dev_type | p_auth_cmpl->dev_type);
128 } else {
129 dev_type = (bt_device_type_t)(p_auth_cmpl->dev_type);
130 }
131 DEVICE_IOT_CONFIG_ADDR_SET_INT(p_auth_cmpl->bd_addr, IOT_CONF_KEY_DEVTYPE, (int)dev_type);
132
133 // save remote addr type to iot conf file
134 DEVICE_IOT_CONFIG_ADDR_SET_INT(p_auth_cmpl->bd_addr, IOT_CONF_KEY_ADDRTYPE,
135 (int)p_auth_cmpl->addr_type);
136
137 // save remote versions to iot conf file
138 const bool status = get_btm_client_interface().peer.BTM_ReadRemoteVersion(
139 p_auth_cmpl->bd_addr, &lmp_ver, &mfct_set, &lmp_subver);
140
141 if (status) {
142 DEVICE_IOT_CONFIG_ADDR_SET_INT(p_auth_cmpl->bd_addr, IOT_CONF_KEY_MANUFACTURER, mfct_set);
143 DEVICE_IOT_CONFIG_ADDR_SET_INT(p_auth_cmpl->bd_addr, IOT_CONF_KEY_LMPVER, lmp_ver);
144 DEVICE_IOT_CONFIG_ADDR_SET_INT(p_auth_cmpl->bd_addr, IOT_CONF_KEY_LMPSUBVER, lmp_subver);
145 }
146
147 // save remote pair type to iot conf file
148 btif_iot_save_pair_type(p_auth_cmpl->bd_addr, is_ble, is_ssp);
149
150 device_iot_config_flush();
151 }
152