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 #define LOG_TAG "btm_iot"
20 
21 #include <bluetooth/log.h>
22 
23 #include "btif/include/btif_storage.h"
24 #include "btm_ble_api.h"
25 #include "device/include/device_iot_config.h"
26 #include "stack/acl/acl.h"
27 #include "stack/include/btm_client_interface.h"
28 
29 // TODO(b/369381361) Enfore -Wmissing-prototypes
30 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
31 
32 using namespace bluetooth;
33 
34 /*******************************************************************************
35  *
36  * Function         btm_iot_save_remote_properties
37  *
38  * Description      Store remote basic properties to iot conf file
39  *
40  * Returns          void
41  *
42  *******************************************************************************/
btm_iot_save_remote_properties(tACL_CONN * p_acl_cb)43 void btm_iot_save_remote_properties(tACL_CONN* p_acl_cb) {
44   BD_NAME bd_name;
45   bt_property_t prop_name;
46   uint32_t cod = 0;
47   tBT_DEVICE_TYPE dev_type;
48   tBLE_ADDR_TYPE addr_type;
49 
50   // save remote name to iot conf file
51   if (BTM_GetRemoteDeviceName(p_acl_cb->remote_addr, bd_name)) {
52     std::string name_str{(char*)bd_name};
53     DEVICE_IOT_CONFIG_ADDR_SET_STR(p_acl_cb->remote_addr, IOT_CONF_KEY_REMOTE_NAME, name_str);
54   }
55 
56   /* Try to retrieve cod from storage */
57   BTIF_STORAGE_FILL_PROPERTY(&prop_name, BT_PROPERTY_CLASS_OF_DEVICE, sizeof(cod), &cod);
58   if (btif_storage_get_remote_device_property(&p_acl_cb->remote_addr, &prop_name) ==
59       BT_STATUS_SUCCESS) {
60     log::verbose("cod retrieved from storage is 0x{:06x}", cod);
61   }
62   if (cod == 0) {
63     log::verbose("cod is 0, set as unclassified");
64     cod = (0x1F) << 8;
65   }
66 
67   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_DEVCLASS, (int)cod);
68 
69   get_btm_client_interface().peer.BTM_ReadDevInfo(p_acl_cb->remote_addr, &dev_type, &addr_type);
70 
71   // save remote dev type to iot conf file
72   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_DEVTYPE, (int)dev_type);
73 
74   // save remote addr type to iot conf file
75   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_ADDRTYPE, (int)addr_type);
76 
77   // save default recorded value to iot conf file
78   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_RECORDED,
79                                  IOT_CONF_VAL_RECORDED_DEFAULT);
80 }
81 
82 /*******************************************************************************
83  *
84  * Function         btm_iot_save_remote_versions
85  *
86  * Description      Store remote versions to iot conf file
87  *
88  * Returns          void
89  *
90  *******************************************************************************/
btm_iot_save_remote_versions(tACL_CONN * p_acl_cb)91 void btm_iot_save_remote_versions(tACL_CONN* p_acl_cb) {
92   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_MANUFACTURER,
93                                  p_acl_cb->remote_version_info.manufacturer);
94   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_LMPVER,
95                                  p_acl_cb->remote_version_info.lmp_version);
96   DEVICE_IOT_CONFIG_ADDR_SET_INT(p_acl_cb->remote_addr, IOT_CONF_KEY_LMPSUBVER,
97                                  p_acl_cb->remote_version_info.lmp_subversion);
98 }
99