1 /* 2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., 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 _FLASH_API_H_ 16 #define _FLASH_API_H_ 17 #include <stdint.h> 18 #include "plf.h" 19 20 #ifdef CFG_BLE_STACK 21 #include "hal_gap.h" 22 #include "hal_gapc_task.h" 23 #include "bt_common_defines.h" 24 #endif //CFG_BLE_STACK 25 26 #if PLF_BT_STACK == 1 27 #include "cs_target_config.h" 28 #include "bt_types_def.h" 29 #define MAX_BD_MUN 5 30 #endif 31 32 /** 33 * Structs 34 */ 35 /* calib info */ 36 typedef struct { 37 unsigned char cap; 38 unsigned char cap_fine; 39 unsigned char PADDING0[2]; 40 } xtal_cap_info_t; 41 42 typedef struct { 43 signed char dsss_txpwr_ofst; 44 signed char ofdm_txpwr_ofst; 45 unsigned char PADDING0[2]; 46 } wifi_txpwr_info_t; 47 48 typedef struct { 49 unsigned int magic_num; /* "CALI" */ 50 unsigned int info_flag; 51 unsigned int RESERVED_08; 52 unsigned int RESERVED_0C; 53 xtal_cap_info_t xtal; 54 wifi_txpwr_info_t wifi2g4_txpwr[3]; // for chan 1/7/13 55 wifi_txpwr_info_t wifi5g_txpwr[5]; // for chan 36/68/100/132/164 56 } calib_info_t; 57 58 /* wifi info */ 59 typedef struct { 60 char ssid[48]; 61 char passwd[64]; 62 } wifi_ssidpw_t; 63 64 typedef struct { 65 unsigned int config; 66 unsigned char mac_addr[6]; 67 unsigned char PADDING0[2]; 68 wifi_ssidpw_t ssidpw; 69 //unsigned char pmk[32]; 70 } wifi_sta_info_t; 71 72 typedef struct { 73 unsigned int config; 74 unsigned char mac_addr[6]; 75 unsigned char PADDING0[2]; 76 wifi_ssidpw_t ssidpw; 77 unsigned int channel; 78 } wifi_ap_info_t; 79 80 typedef struct { 81 unsigned int lease_time; 82 unsigned int dhcp_start; 83 unsigned int dhcp_end; 84 } wifi_lwip_dhcps_t; 85 86 typedef struct { 87 signed int time_zone; 88 } wifi_lwip_sntp_t; 89 90 typedef struct { 91 unsigned int config; 92 wifi_lwip_dhcps_t dhcps; 93 unsigned int dns_server; 94 wifi_lwip_sntp_t sntp; 95 } wifi_lwip_info_t; 96 97 /* wifi sta config */ 98 #define WIFI_STA_CONFIG_STA_AUTO_CONNECT_EN CO_BIT(0) 99 100 /* wifi ap config */ 101 #define WIFI_AP_CONFIG_FORCED_AP_MODE_EN CO_BIT(0) 102 103 /* wifi lwip config */ 104 #define WIFI_LWIP_CONFIG_SNTP_CLIENT_AUTO_CONNECT_EN CO_BIT(0) 105 106 typedef struct { 107 unsigned int magic_num; /* "WIFI" */ 108 unsigned int info_flag; 109 unsigned int RESERVED_08; 110 unsigned int RESERVED_0C; 111 wifi_sta_info_t sta_info; 112 wifi_ap_info_t ap_info; 113 wifi_lwip_info_t lwip_info; 114 } wifi_info_t; 115 116 /* btdm info */ 117 typedef struct 118 { 119 uint8_t bt_flash_erased; 120 uint8_t factory_mode_setting; 121 uint8_t local_bt_addr[6]; 122 uint8_t local_ble_addr[6]; 123 uint16_t reserved; 124 char local_dev_name[32]; 125 uint8_t pincode[16]; 126 } bt_factory_info_t; 127 128 #if PLF_BT_STACK == 1 129 #ifndef BTDEVRECORD 130 #define BTDEVRECORD 131 typedef struct _BtDeviceRecord 132 { 133 BT_ADDR bdAddr; 134 bool trusted; 135 uint8_t linkKey[16]; 136 uint8_t keyType; 137 uint8_t pinLen; 138 } BtDeviceRecord; 139 #endif 140 typedef struct list_node_t { 141 struct list_node_t *prev; 142 struct list_node_t *next; 143 void *data; 144 } list_node_t; 145 146 typedef struct { 147 list_node_t *head; 148 uint8_t length; 149 } bt_dev_list; 150 151 typedef struct { 152 bool hf_state; 153 bool a2dp_state; 154 uint8_t a2dp_role; 155 uint8_t a2dp_codectype; 156 int8_t peer_a2dp_vol; 157 int8_t peer_hfp_vol; 158 uint32_t cod; 159 }btdev_info; 160 161 typedef struct { 162 list_node_t node; 163 BtDeviceRecord record; 164 btdev_info device_info; 165 } bt_dev_info_t; 166 167 typedef struct { 168 uint8_t addr_type; 169 BT_ADDR bleaddr; 170 } ble_dev_info_t; 171 172 typedef struct { 173 uint8_t init_need; // need init when value not equal to 0xEE 174 uint8_t flush_need; 175 #if APP_SUPPORT_TWS == 1 176 uint8_t flash_tws_role; 177 ble_dev_info_t ble_dev_info; 178 #endif 179 bt_dev_list bt_list; 180 bt_dev_info_t bt_dev_info[MAX_BD_MUN]; 181 } bt_flash_info_t; 182 #endif 183 184 #ifdef CFG_BLE_STACK 185 // Local key length 186 #define LOC_KEY_LEN (0x10) 187 typedef struct gapc_ltk gapc_ltk_t; 188 #endif // (CFG_BLE_STACK) 189 190 typedef struct { 191 unsigned int magic_num; /* "BTDM" */ 192 unsigned int info_flag; 193 unsigned int RESERVED_08; 194 unsigned int RESERVED_0C; 195 bt_factory_info_t bt_factory; 196 #if PLF_BT_STACK == 1 197 bt_flash_info_t bt_info; 198 #endif 199 #ifdef CFG_BLE_STACK 200 gapc_ltk_t le_ltk; 201 uint8_t loc_irk[LOC_KEY_LEN]; 202 uint8_t bond_status; 203 uint16_t ntf_cfg; 204 uint8_t ble_rand_add[BD_ADDR_LEN]; 205 #endif 206 } btdm_info_t; 207 208 /** 209 * Enums 210 */ 211 typedef enum { 212 INFO_FLAG_INVALID = 1, 213 INFO_READ_DONE = 0, 214 MAGIC_NUM_ERR = -1, 215 INFO_LEN_ERR = -2, 216 } INFO_READ_STATUS_T; 217 218 /** 219 * Get chip size as bytes 220 */ 221 unsigned int flash_chip_size_get(void); 222 223 /** 224 * Erase all flash except for reserved area 225 */ 226 void flash_chip_erase(void); 227 228 /** 229 * Erase flash 230 */ 231 int flash_erase(void *a4k, unsigned int len); 232 233 /** 234 * Write flash 235 */ 236 int flash_write(void *adr, unsigned int len, void *buf); 237 238 /** 239 * Read flash 240 */ 241 int flash_read(void *adr, unsigned int len, void *buf); 242 243 /** 244 * Invalid all flash cache 245 */ 246 void flash_cache_invalid_all(void); 247 248 /** 249 * Invalid the range of flash cache 250 */ 251 void flash_cache_invalid_range(void *adr, unsigned int len); 252 unsigned int flash_crc32(void *adr, unsigned int len); 253 254 255 /** 256 * calib info api(read only) 257 */ 258 int flash_calib_xtal_cap_read(xtal_cap_info_t *xtal_cap); 259 int flash_calib_wifi2g4_txpwr_read(wifi_txpwr_info_t *txpwr); 260 int flash_calib_wifi5g_txpwr_read(wifi_txpwr_info_t *txpwr); 261 262 /** 263 * wifi info api 264 */ 265 int flash_wifi_sta_config_read(unsigned int *config); 266 void flash_wifi_sta_config_write(unsigned int *config); 267 int flash_wifi_sta_macaddr_read(unsigned char *addr); 268 void flash_wifi_sta_macaddr_write(unsigned char *addr); 269 int flash_wifi_sta_ssidpw_read(char *ssid, char *pass); 270 void flash_wifi_sta_ssidpw_write(char *ssid, char *pass); 271 int flash_wifi_ap_config_read(unsigned int *config); 272 void flash_wifi_ap_config_write(unsigned int *config); 273 int flash_wifi_ap_macaddr_read(unsigned char *addr); 274 void flash_wifi_ap_macaddr_write(unsigned char *addr); 275 int flash_wifi_ap_ssidpw_read(char *ssid, char *pass); 276 void flash_wifi_ap_ssidpw_write(char *ssid, char *pass); 277 int flash_wifi_ap_channel_read(unsigned int *channel); 278 void flash_wifi_ap_channel_write(unsigned int channel); 279 int flash_wifi_lwip_config_read(unsigned int *config); 280 void flash_wifi_lwip_config_write(unsigned int *config); 281 int flash_wifi_lwip_dhcps_read(unsigned int *lease_time, unsigned int *dhcp_start, unsigned int *dhcp_end); 282 void flash_wifi_lwip_dhcps_write(unsigned int lease_time, unsigned int dhcp_start, unsigned int dhcp_end); 283 int flash_wifi_lwip_dnsserv_read(unsigned int *dns_server); 284 void flash_wifi_lwip_dnsserv_write(unsigned int dns_server); 285 int flash_wifi_lwip_sntp_read(signed int *time_zone); 286 void flash_wifi_lwip_sntp_write(signed int time_zone); 287 void flash_wifi_info_remove_all(void); 288 void flash_wifi_info_remove_ssidpw(void); 289 290 unsigned int flash_strlen(unsigned char *str,unsigned char max_len); 291 292 /** 293 * btdm info api 294 */ 295 int flash_btdm_bt_factory_read(void *buf, unsigned int len); 296 void flash_btdm_bt_factory_write(const void *buf, unsigned int len); 297 298 #if PLF_BT_STACK == 1 299 /** 300 **************************************************************************************** 301 * @brief * Doubly-linked list initialize . 302 * 303 * @param Doubly-linked list Entry. 304 * 305 **************************************************************************************** 306 */ 307 void bt_initializeList(list_node_t* list); 308 /** 309 **************************************************************************************** 310 * @brief * Doubly-linked list insert node in Tail . 311 * 312 * @param tail: Doubly-linked list. entry: node which want to be inserted in tail of Doubly-linked list 313 * 314 **************************************************************************************** 315 */ 316 void bt_insertTailList(list_node_t* tail, list_node_t* entry); 317 /** 318 **************************************************************************************** 319 * @brief * Doubly-linked list insert node in Head . 320 * 321 * @param head: Doubly-linked list. entry: node which want to be inserted in head of Doubly-linked list 322 * 323 **************************************************************************************** 324 */ 325 void bt_insertHeadList(list_node_t* head, list_node_t* entry); 326 /** 327 **************************************************************************************** 328 * @brief * remove current Node form a Doubly-linked list . 329 * 330 * @param Node. 331 * 332 **************************************************************************************** 333 */ 334 void bt_removeNode(list_node_t* node); 335 /** 336 **************************************************************************************** 337 * @brief * remove current Node form a Doubly-linked list ,and return next Node's ptr . 338 * 339 * @param 340 * 341 * @return Node which is next after removed Node. 342 **************************************************************************************** 343 */ 344 list_node_t* bt_removeHeadNode(list_node_t* head); 345 /** 346 **************************************************************************************** 347 * @brief * Get and remove Node form the Head of a Doubly-linked list . 348 * 349 * @param Doubly-linked list. 350 * 351 * @return Head of a Doubly-linked list 352 **************************************************************************************** 353 */ 354 list_node_t* bt_removeHeadList(list_node_t* head); 355 /** 356 **************************************************************************************** 357 * @brief * Doubly-linked list is Empty . 358 * 359 * @param Doubly-linked list. 360 * 361 * @return true: empty false: not empty 362 **************************************************************************************** 363 */ 364 bool bt_listIsEmpty(list_node_t *head); 365 366 bt_flash_info_t * flash_btdm_bt_dev_read(void); 367 void flash_btdm_bt_dev_write(void); 368 #endif 369 370 #ifdef CFG_BLE_STACK 371 int flash_btdm_le_ltk_read(gapc_ltk_t *le_ltk); 372 void flash_btdm_le_ltk_write(gapc_ltk_t *le_ltk); 373 void flash_btdm_le_ltk_remove(void); 374 375 int flash_btdm_le_loc_irk_read(uint8_t *loc_irk); 376 void flash_btdm_le_loc_irk_write(uint8_t *loc_irk); 377 void flash_btdm_le_loc_irk_remove(void); 378 379 int flash_btdm_le_periph_bonded_read(uint8_t *bond_status); 380 void flash_btdm_le_periph_bonded_write(uint8_t *bond_status); 381 void flash_btdm_le_periph_bonded_remove(void); 382 383 int flash_btdm_le_mouse_ntf_cfg_read(uint8_t *ntf_cfg); 384 void flash_btdm_le_mouse_ntf_cfg_write(uint8_t *ntf_cfg); 385 void flash_btdm_le_mouse_ntf_cfg_remove(void); 386 387 int flash_btdm_le_rand_addr_read(uint8_t *le_addr); 388 void flash_btdm_le_rand_addr_write(uint8_t *le_addr); 389 void flash_btdm_le_rand_addr_remove(void); 390 391 void flash_btdm_le_info_remove_all(void); 392 #endif // CFG_BLE_STACK 393 394 int flash_user_data_addr_length_set(unsigned int addr, unsigned int len); 395 int flash_user_data_read(void *buf, unsigned int len); 396 int flash_user_data_write(void *buf, unsigned int len); 397 398 #endif /* _FLASH_API_H_ */ 399