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 "persist.bluetooth.device_iot_config.enablelogging"
25 #define PROPERTY_FACTORY_RESET "persist.bluetooth.factoryreset"
26 
27 #define INFO_SECTION "Info"
28 #define VERSION_KEY "Version"
29 #define FILE_CREATED_TIMESTAMP "TimeCreated"
30 #define FILE_MODIFIED_TIMESTAMP "TimeModified"
31 #define TIME_STRING_LENGTH sizeof("YYYY-MM-DD HH:MM:SS")
32 static const char* TIME_STRING_FORMAT = "%Y-%m-%d %H:%M:%S";
33 
34 #ifndef DEVICES_MAX_NUM_IN_IOT_INFO_FILE
35 #define DEVICES_MAX_NUM_IN_IOT_INFO_FILE 40
36 #endif
37 #define DEVICES_NUM_MARGIN 5
38 
39 #if (DEVICES_MAX_NUM_IN_IOT_INFO_FILE < DEVICES_NUM_MARGIN)
40 #undef DEVICES_MAX_NUM_IN_IOT_INFO_FILE
41 #define DEVICES_MAX_NUM_IN_IOT_INFO_FILE DEVICES_NUM_MARGIN
42 #endif
43 
44 #define DEVICE_IOT_INFO_CURRENT_VERSION 1
45 #define DEVICE_IOT_INFO_FIRST_VERSION 1
46 
47 #define IOT_CONFIG_FLUSH_EVT 0
48 #define IOT_CONFIG_SAVE_TIMER_FIRED_EVT 1
49 
50 #ifdef __ANDROID__
51 static const char* IOT_CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_remote_dev_info.conf";
52 static const char* IOT_CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_remote_dev_info.bak";
53 #else   // !__ANDROID__
54 static const char* IOT_CONFIG_FILE_PATH = "bt_remote_dev_info.conf";
55 static const char* IOT_CONFIG_BACKUP_PATH = "bt_remote_dev_info.bak";
56 #endif  // __ANDROID__
57 static const uint64_t CONFIG_SETTLE_PERIOD_MS = 12000;
58 
59 enum ConfigSource { NOT_LOADED, ORIGINAL, BACKUP, NEW_FILE, RESET };
60 
61 struct config_t;
62 struct future_t;
63 
64 typedef bool (*compare_func)(const entry_t& first, const entry_t& second);
65 
66 // config_lock is used by the callee in the following methods
67 future_t* device_iot_config_module_init(void);
68 future_t* device_iot_config_module_start_up(void);
69 future_t* device_iot_config_module_shut_down(void);
70 future_t* device_iot_config_module_clean_up(void);
71 void device_iot_config_write(uint16_t event, char* p_param);
72 
73 // config_lock is used by the caller of the following methods
74 void device_iot_config_sections_sort_by_entry_key(config_t& config, compare_func comp);
75 bool device_iot_config_has_key_value(const std::string& section, const std::string& key,
76                                      const std::string& value_str);
77 void device_iot_config_save_async(void);
78 int device_iot_config_get_device_num(const config_t& config);
79 void device_iot_config_restrict_device_num(config_t& config);
80 bool device_iot_config_compare_key(const entry_t& first, const entry_t& second);
81 void device_iot_config_timer_save_cb(void* /* data */);
82 void device_iot_config_set_modified_time();
83 bool device_iot_config_is_factory_reset(void);
84 void device_iot_config_delete_files(void);
85