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