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