1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 #ifndef __NVRECORD_H__
5 #define __NVRECORD_H__
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 #include <stdio.h>
11 #include <string.h>
12 #include "cmsis.h"
13 #include "nvrec_config.h"
14 #include "bluetooth.h"
15 #include "me_api.h"
16 #include "hal_trace.h"
17 #if defined(NEW_NV_RECORD_ENALBED)
18 #include "nvrecord_extension.h"
19 #include "nvrecord_bt.h"
20 #include "nvrecord_env.h"
21 #include "nvrecord_ble.h"
22 #include "nvrecord_dma_config.h"
23 #include "nvrecord_fp_account_key.h"
24 
25 #define nvdev_tag       "nvdev_tag"
26 
27 #else
28 
29 #define DDB_RECORD_NUM  9
30 #define section_name_ddbrec     "ddbrec"
31 #define nvrecord_mem_pool_size      4096
32 #define nvrecord_tag    "nvrecord"
33 #define nvdev_tag       "nvdev_tag"
34 #define section_name_other     "other"
35 
36 
37 /*
38     version|magic           16bit|16bit             0001 cb91
39     crc                            32bit
40     reserve[0]                 32bit
41     reserve[1]                 32bit
42     config_addr                32bit
43     heap_contents            32bit
44     mempool                   ...
45 */
46 #define nvrecord_struct_version             8
47 #define nvrecord_magic                      0xcb91
48 
49 typedef enum
50 {
51     pos_version_and_magic,// 0
52     pos_crc,                            // 1
53     pos_start_ram_addr,                    // 2
54     pos_reserv2,                    // 3
55     pos_config_addr,            // 4
56     pos_heap_contents,      // 5
57 
58 }nvrec_mempool_pre_enum;
59 
60 #define mempool_pre_offset       (1+pos_config_addr+((sizeof(heap_handle_t)/sizeof(uint32_t))+1))
61 
62 typedef enum
63 {
64     NV_STATE_IDLE,
65     NV_STATE_MAIN_ERASING,
66     NV_STATE_MAIN_ERASED,
67     NV_STATE_MAIN_WRITTING,
68     NV_STATE_MAIN_WRITTEN,
69     NV_STATE_MAIN_DONE,
70     NV_STATE_BAK_ERASING,
71     NV_STATE_BAK_ERASED,
72     NV_STATE_BAK_WRITTING,
73     NV_STATE_BAK_WRITTEN,
74     NV_STATE_BAK_DONE,
75 }NV_STATE;
76 
77 typedef struct
78 {
79     bool is_update;
80     NV_STATE state;
81     uint32_t written_size;
82     nvrec_config_t *config;
83 }nv_record_struct;
84 
85 typedef enum
86 {
87     section_usrdata_ddbrecord,
88     section_none
89 }SECTIONS_ADP_ENUM;
90 
91 typedef  struct btdevice_volume{
92     int8_t a2dp_vol;
93     int8_t hfp_vol;
94 }btdevice_volume;
95 
96 typedef  struct btdevice_profile{
97     bool hfp_act;
98     bool hsp_act;
99     bool a2dp_act;
100     uint8_t a2dp_codectype;
101 }btdevice_profile;
102 
103 typedef struct _nvrec_btdevicerecord
104 {
105     btif_device_record_t record;
106     btdevice_volume device_vol;
107     btdevice_profile device_plf;
108 } nvrec_btdevicerecord;
109 
110 extern uint8_t nv_record_dev_rev;
111 
112 void nv_record_init(void);
113 bt_status_t nv_record_open(SECTIONS_ADP_ENUM section_id);
114 bt_status_t nv_record_enum_dev_records(unsigned short index,btif_device_record_t* record);
115 bt_status_t nv_record_add(SECTIONS_ADP_ENUM type,void *record);
116 bt_status_t nv_record_ddbrec_find(const bt_bdaddr_t *bd_ddr, btif_device_record_t*record);
117 bt_status_t nv_record_ddbrec_delete(const bt_bdaddr_t *bdaddr);
118 int nv_record_btdevicerecord_find(const bt_bdaddr_t *bd_ddr, nvrec_btdevicerecord **record);
119 int nv_record_touch_cause_flush(void);
120 
121 int nv_record_get_paired_dev_count(void);
122 void nv_record_sector_clear(void);
123 void nv_record_flash_flush(void);
124 int nv_record_flash_flush_in_sleep(void);
125 
126 
127 int nv_record_enum_latest_two_paired_dev(btif_device_record_t* record1,btif_device_record_t* record2);
128 void nv_record_all_ddbrec_print(void);
129 
130 void nv_record_update_runtime_userdata(void);
131 bt_status_t nv_record_config_rebuild(void);
132 void nv_record_btdevicerecord_set_a2dp_vol(nvrec_btdevicerecord* pRecord, int8_t vol);
133 void nv_record_btdevicerecord_set_hfp_vol(nvrec_btdevicerecord* pRecord, int8_t vol);
134 void nv_record_btdevicevolume_set_a2dp_vol(btdevice_volume* device_vol, int8_t vol);
135 void nv_record_btdevicevolume_set_hfp_vol(btdevice_volume* device_vol, int8_t vol);
136 void nv_record_btdevicerecord_set_a2dp_profile_active_state(btdevice_profile* device_plf, bool isActive);
137 void nv_record_btdevicerecord_set_hfp_profile_active_state(btdevice_profile* device_plf, bool isActive);
138 void nv_record_btdevicerecord_set_hsp_profile_active_state(btdevice_profile* device_plf, bool isActive);
139 void nv_record_btdevicerecord_set_a2dp_profile_codec(btdevice_profile* device_plf, uint8_t a2dpCodec);
140 
141 #ifdef NVREC_BAIDU_DATA_SECTION
142 #define BAIDU_DATA_SECTIN ("baidu_data")
143 #define BAIDU_DATA_FM_KEY ("fmfreq")
144 #define BAIDU_DATA_RAND_KEY ("rand")
145 
146 #define BAIDU_DATA_DEF_RAND "abcdefghi"
147 
148 int nvrec_baidu_data_init(void);
149 int nvrec_get_fm_freq(void);
150 int nvrec_set_fm_freq(int fmfreq);
151 int nvrec_get_rand(char *rand);
152 int nvrec_set_rand(char *rand);
153 int nvrec_clear_rand(void);
154 int nvrec_dev_get_sn(char *sn);
155 #endif
156 
157 uint32_t nv_record_pre_write_operation(void);
158 void nv_record_post_write_operation(uint32_t lock);
159 #include "nvrecord_wifi.h"
160 #endif // #if !defined(NEW_NV_RECORD_ENALBED)
161 
162 //#define nv_record_debug
163 #ifdef nv_record_debug
164 #define nvrec_trace     TRACE
165 #else
166 #define nvrec_trace(...)
167 #endif
168 
169 #define NVREC_DEV_NEWEST_REV        2
170 #define NVREC_DEV_VERSION_1         1
171 
172 #define BLE_NAME_LEN_IN_NV  32
173 
174 /*
175     this is the nvrec dev zone struct :
176     version|magic   16bit|16bit
177     crc         32bit
178     reserve[0]      32bit
179     reserv[1]       32bit
180     dev local name  max 249*8bit
181     dev bt addr         64bit
182     dev ble addr        64bit
183     calib data      32bit
184 */
185 #define nvrec_dev_version   1
186 #define nvrec_dev_magic      0xba80
187 typedef enum
188 {
189     dev_version_and_magic,      //0
190     dev_crc,                    //1
191     dev_reserv1,                //2
192     dev_reserv2,                //3
193     dev_name,                   //[4~66]
194     dev_bt_addr = 67,           //[67~68]
195     dev_ble_addr = 69,          //[69~70]
196     dev_dongle_addr = 71,
197     dev_xtal_fcap = 73,         //73
198     dev_data_len,
199 }nvrec_dev_enum;
200 
201 // following the former nv rec dev info
202 typedef enum
203 {
204     rev2_dev_data_len = 75,             //75, length of the valid content, excluding crc
205     rev2_dev_crc,                       //76, crc value of the following data
206     rev2_dev_section_start_reserved,    //77
207     rev2_dev_reserv2,                   //78
208     rev2_dev_name,                      //[79~141]
209     rev2_dev_bt_addr = 142,             //[142~143]
210     rev2_dev_ble_addr = 144,            //[144~145]
211     rev2_dev_dongle_addr = 146,         //[146~147]
212     rev2_dev_xtal_fcap = 148,           //148
213     rev2_dev_ble_name = 149,            //[149~156]
214 #ifdef NVREC_BAIDU_DATA_SECTION
215     rev2_dev_prod_sn = 157,             //[157~160]
216 #endif
217     // TODO: add the new section in the future if needed
218 
219     rev2_dev_section_end = 157,
220 
221 }nvrec_dev_rev_2_enum;
222 
223 int nvrec_dev_get_dongleaddr(bt_bdaddr_t *dongleaddr);
224 int nvrec_dev_get_btaddr(char *btaddr);
225 char* nvrec_dev_get_bt_name(void);
226 const char* nvrec_dev_get_ble_name(void);
227 size_t nv_record_get_bt_records_num(void);
228 #ifdef __cplusplus
229 }
230 #endif
231 #endif /* __NVRECORD_H__*/
232