1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 Google, Inc.
4  *  Copyright (C) 2018 The Linux Foundation
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at:
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include "osi/include/config.h"
23 
24 #define PROPERTY_ENABLE_LOGGING \
25   "persist.bluetooth.device_iot_config.enablelogging"
26 #define PROPERTY_FACTORY_RESET "persist.bluetooth.factoryreset"
27 
28 #define INFO_SECTION "Info"
29 #define VERSION_KEY "Version"
30 #define FILE_CREATED_TIMESTAMP "TimeCreated"
31 #define FILE_MODIFIED_TIMESTAMP "TimeModified"
32 #define TIME_STRING_LENGTH sizeof("YYYY-MM-DD HH:MM:SS")
33 static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S";
34 
35 #ifndef DEVICES_MAX_NUM_IN_IOT_INFO_FILE
36 #define DEVICES_MAX_NUM_IN_IOT_INFO_FILE 40
37 #endif
38 #define DEVICES_NUM_MARGIN 5
39 
40 #if (DEVICES_MAX_NUM_IN_IOT_INFO_FILE < DEVICES_NUM_MARGIN)
41 #undef DEVICES_MAX_NUM_IN_IOT_INFO_FILE
42 #define DEVICES_MAX_NUM_IN_IOT_INFO_FILE DEVICES_NUM_MARGIN
43 #endif
44 
45 #define DEVICE_IOT_INFO_CURRENT_VERSION 1
46 #define DEVICE_IOT_INFO_FIRST_VERSION 1
47 
48 #define IOT_CONFIG_FLUSH_EVT 0
49 #define IOT_CONFIG_SAVE_TIMER_FIRED_EVT 1
50 
51 #ifdef __ANDROID__
52 static const char* IOT_CONFIG_FILE_PATH =
53     "/data/misc/bluedroid/bt_remote_dev_info.conf";
54 static const char* IOT_CONFIG_BACKUP_PATH =
55     "/data/misc/bluedroid/bt_remote_dev_info.bak";
56 #else   // !__ANDROID__
57 static const char* IOT_CONFIG_FILE_PATH = "bt_remote_dev_info.conf";
58 static const char* IOT_CONFIG_BACKUP_PATH = "bt_remote_dev_info.bak";
59 #endif  // __ANDROID__
60 static const uint64_t CONFIG_SETTLE_PERIOD_MS = 12000;
61 
62 enum ConfigSource { NOT_LOADED, ORIGINAL, BACKUP, NEW_FILE, RESET };
63 
64 struct config_t;
65 struct future_t;
66 
67 typedef bool (*compare_func)(const entry_t& first, const entry_t& second);
68 
69 // config_lock is used by the callee in the following methods
70 future_t* device_iot_config_module_init(void);
71 future_t* device_iot_config_module_start_up(void);
72 future_t* device_iot_config_module_shut_down(void);
73 future_t* device_iot_config_module_clean_up(void);
74 void device_iot_config_write(uint16_t event, char* p_param);
75 
76 // config_lock is used by the caller of the following methods
77 void device_iot_config_sections_sort_by_entry_key(config_t& config,
78                                                   compare_func comp);
79 bool device_iot_config_has_key_value(const std::string& section,
80                                      const std::string& key,
81                                      const std::string& value_str);
82 void device_iot_config_save_async(void);
83 int device_iot_config_get_device_num(const config_t& config);
84 void device_iot_config_restrict_device_num(config_t& config);
85 bool device_iot_config_compare_key(const entry_t& first, const entry_t& second);
86 void device_iot_config_timer_save_cb(void* /* data */);
87 void device_iot_config_set_modified_time();
88 bool device_iot_config_is_factory_reset(void);
89 void device_iot_config_delete_files(void);
90