1 /* 2 * Copyright (c) 2021 Bestechnic (Shanghai) Co., Ltd. All rights reserved. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef __NVRECORD_H__ 16 #define __NVRECORD_H__ 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 #include <stdio.h> 22 #include <string.h> 23 #include "cmsis.h" 24 #include "nvrec_config.h" 25 #include "bluetooth.h" 26 #include "me_api.h" 27 #include "hal_trace.h" 28 #if defined(NEW_NV_RECORD_ENABLED) 29 #include "nvrecord_extension.h" 30 #include "nvrecord_bt.h" 31 #include "nvrecord_env.h" 32 #include "nvrecord_ble.h" 33 #include "nvrecord_dma_config.h" 34 #include "nvrecord_fp_account_key.h" 35 #include "btif_sys_config.h" 36 37 #define nvdev_tag "nvdev_tag" 38 39 #else 40 41 #define DDB_RECORD_NUM 9 42 #define section_name_ddbrec "ddbrec" 43 #define nvrecord_mem_pool_size 4096 44 #define nvrecord_tag "nvrecord" 45 #define nvdev_tag "nvdev_tag" 46 #define section_name_other "other" 47 48 49 /* 50 version|magic 16bit|16bit 0001 cb91 51 crc 32bit 52 reserve[0] 32bit 53 reserve[1] 32bit 54 config_addr 32bit 55 heap_contents 32bit 56 mempool ... 57 */ 58 #define nvrecord_struct_version 8 59 #define nvrecord_magic 0xcb91 60 61 typedef enum 62 { 63 pos_version_and_magic,// 0 64 pos_crc, // 1 65 pos_start_ram_addr, // 2 66 pos_reserv2, // 3 67 pos_config_addr, // 4 68 pos_heap_contents, // 5 69 70 }nvrec_mempool_pre_enum; 71 72 #define mempool_pre_offset (1+pos_config_addr+((sizeof(heap_handle_t)/sizeof(uint32_t))+1)) 73 74 typedef enum 75 { 76 NV_STATE_IDLE, 77 NV_STATE_MAIN_ERASING, 78 NV_STATE_MAIN_ERASED, 79 NV_STATE_MAIN_WRITTING, 80 NV_STATE_MAIN_WRITTEN, 81 NV_STATE_MAIN_DONE, 82 NV_STATE_BAK_ERASING, 83 NV_STATE_BAK_ERASED, 84 NV_STATE_BAK_WRITTING, 85 NV_STATE_BAK_WRITTEN, 86 NV_STATE_BAK_DONE, 87 }NV_STATE; 88 89 typedef struct 90 { 91 bool is_update; 92 NV_STATE state; 93 uint32_t written_size; 94 nvrec_config_t *config; 95 }nv_record_struct; 96 97 typedef enum 98 { 99 section_usrdata_ddbrecord, 100 section_none 101 }SECTIONS_ADP_ENUM; 102 103 typedef struct btdevice_local_volume{ 104 int8_t a2dp_vol; 105 int8_t hfp_vol; 106 }btdevice_volume; 107 108 typedef struct btdevice_profile{ 109 bool hfp_act; 110 uint8_t a2dp_abs_vol; 111 bool a2dp_act; 112 uint8_t a2dp_codectype; 113 }btdevice_profile; 114 115 typedef struct 116 { 117 uint16_t spec_id; 118 uint16_t vend_id; 119 uint16_t prod_id; 120 uint16_t prod_ver; 121 uint8_t prim_rec; 122 uint16_t vend_id_source; 123 } bt_dip_pnp_info_t; 124 125 typedef struct _nvrec_btdevicerecord 126 { 127 btif_device_record_t record; 128 btdevice_volume device_vol; 129 btdevice_profile device_plf; 130 #ifdef BTIF_DIP_DEVICE 131 bt_dip_pnp_info_t pnp_info; 132 #endif 133 } nvrec_btdevicerecord; 134 135 extern uint8_t nv_record_dev_rev; 136 137 void nv_record_init(void); 138 bt_status_t nv_record_open(SECTIONS_ADP_ENUM section_id); 139 bt_status_t nv_record_enum_dev_records(unsigned short index,btif_device_record_t* record); 140 bt_status_t nv_record_add(SECTIONS_ADP_ENUM type,void *record); 141 bt_status_t nv_record_ddbrec_find(const bt_bdaddr_t *bd_ddr, btif_device_record_t*record); 142 bt_status_t nv_record_ddbrec_delete(const bt_bdaddr_t *bdaddr); 143 int nv_record_btdevicerecord_find(const bt_bdaddr_t *bd_ddr, nvrec_btdevicerecord **record); 144 bool nv_record_get_pnp_info(bt_bdaddr_t *bdAddr, bt_dip_pnp_info_t *pPnpInfo); 145 int nv_record_touch_cause_flush(void); 146 147 int nv_record_get_paired_dev_count(void); 148 void nv_record_sector_clear(void); 149 void nv_record_flash_flush(void); 150 int nv_record_flash_flush_in_sleep(void); 151 void nv_record_execute_async_flush(void); 152 153 int nv_record_enum_latest_two_paired_dev(btif_device_record_t* record1,btif_device_record_t* record2); 154 void nv_record_all_ddbrec_print(void); 155 156 void nv_record_update_runtime_userdata(void); 157 bt_status_t nv_record_config_rebuild(void); 158 void nv_record_btdevicerecord_set_a2dp_vol(nvrec_btdevicerecord* pRecord, int8_t vol); 159 void nv_record_btdevicerecord_set_a2dp_abs_vol(nvrec_btdevicerecord* pRecord, int8_t vol); 160 void nv_record_btdevicerecord_set_hfp_vol(nvrec_btdevicerecord* pRecord, int8_t vol); 161 void nv_record_btdevicevolume_set_a2dp_vol(btdevice_volume* device_vol, int8_t vol); 162 void nv_record_btdevicevolume_set_hfp_vol(btdevice_volume* device_vol, int8_t vol); 163 void nv_record_btdevicerecord_set_pnp_info(nvrec_btdevicerecord* pRecord, bt_dip_pnp_info_t* pPnpInfo); 164 void nv_record_btdevicerecord_set_a2dp_profile_active_state(btdevice_profile* device_plf, bool isActive); 165 void nv_record_btdevicerecord_set_hfp_profile_active_state(btdevice_profile* device_plf, bool isActive); 166 void nv_record_btdevicerecord_set_a2dp_profile_codec(btdevice_profile* device_plf, uint8_t a2dpCodec); 167 168 #ifdef NVREC_BAIDU_DATA_SECTION 169 #define BAIDU_DATA_SECTIN ("baidu_data") 170 #define BAIDU_DATA_FM_KEY ("fmfreq") 171 #define BAIDU_DATA_RAND_KEY ("rand") 172 173 #define BAIDU_DATA_DEF_RAND "abcdefghi" 174 175 int nvrec_baidu_data_init(void); 176 int nvrec_get_fm_freq(void); 177 int nvrec_set_fm_freq(int fmfreq); 178 int nvrec_get_rand(char *rand); 179 int nvrec_set_rand(char *rand); 180 int nvrec_clear_rand(void); 181 int nvrec_dev_get_sn(char *sn); 182 #endif 183 184 uint32_t nv_record_pre_write_operation(void); 185 void nv_record_post_write_operation(uint32_t lock); 186 187 #endif // #if !defined(NEW_NV_RECORD_ENABLED) 188 189 #define nv_record_debug 190 //#define nv_record_verbose_log 191 #ifdef nv_record_debug 192 #define nvrec_trace TRACE 193 #else 194 #define nvrec_trace(...) 195 #endif 196 197 #define NVREC_DEV_NEWEST_REV 2 198 #define NVREC_DEV_VERSION_1 1 199 200 #define BLE_NAME_LEN_IN_NV 32 201 202 /* 203 this is the nvrec dev zone struct : 204 version|magic 16bit|16bit 205 crc 32bit 206 reserve[0] 32bit 207 reserv[1] 32bit 208 dev local name max 249*8bit 209 dev bt addr 64bit 210 dev ble addr 64bit 211 calib data 32bit 212 */ 213 #define nvrec_dev_version 1 214 #define nvrec_dev_magic 0xba80 215 typedef enum 216 { 217 dev_version_and_magic, //0 218 dev_crc, //1 219 dev_reserv1, //2 220 dev_reserv2, //3 221 dev_name, //[4~66] 222 dev_bt_addr = 67, //[67~68] 223 dev_ble_addr = 69, //[69~70] 224 dev_dongle_addr = 71, 225 dev_xtal_fcap = 73, //73 226 dev_data_len, 227 }nvrec_dev_enum; 228 229 // following the former nv rec dev info 230 typedef enum 231 { 232 rev2_dev_data_len = 75, //75, length of the valid content, excluding crc 233 rev2_dev_crc, //76, crc value of the following data 234 rev2_dev_section_start_reserved, //77 235 rev2_dev_reserv2, //78 236 rev2_dev_name, //[79~141] 237 rev2_dev_bt_addr = 142, //[142~143] 238 rev2_dev_ble_addr = 144, //[144~145] 239 rev2_dev_dongle_addr = 146, //[146~147] 240 rev2_dev_xtal_fcap = 148, //148 241 rev2_dev_ble_name = 149, //[149~156] 242 #ifdef NVREC_BAIDU_DATA_SECTION 243 rev2_dev_prod_sn = 157, //[157~160] 244 #endif 245 // TODO: add the new section in the future if needed 246 247 rev2_dev_section_end = 157, 248 249 }nvrec_dev_rev_2_enum; 250 251 int nvrec_dev_get_dongleaddr(bt_bdaddr_t *dongleaddr); 252 int nvrec_dev_get_btaddr(char *btaddr); 253 char* nvrec_dev_get_bt_name(void); 254 const char* nvrec_dev_get_ble_name(void); 255 256 #ifdef __cplusplus 257 } 258 #endif 259 #endif /* __NVRECORD_H__*/ 260