1 /* 2 * Copyright (C) 2018-2020 Alibaba Group Holding Limited 3 */ 4 5 #ifndef __GENIE_STORAGE_H__ 6 #define __GENIE_STORAGE_H__ 7 8 #include <bluetooth/bluetooth.h> 9 10 #include <api/mesh.h> 11 #include "net.h" 12 //#include "flash.h" 13 14 #define GENIE_KV_SEQ_KEY "seq" 15 #define GENIE_SEQ_MAGIC_NUMBER (0xA8) 16 #define GENIE_SEQ_SAVE_INTERVAL (100) 17 18 enum { 19 GENIE_STORAGE_INDEX_START = 0, 20 GFI_MESH_PARA, 21 GFI_MESH_TRITUPLE, 22 GFI_MESH_SADDR, 23 GFI_MESH_SEQ, 24 GFI_MESH_RESET_CNT, 25 GFI_MESH_POWERUP, 26 GFI_MESH_HB, 27 GFI_MESH_DEVKEY, 28 GFI_MESH_NETKEY, 29 GFI_MESH_APPKEY, 30 GFI_OTA_INDICAT, 31 GFI_OTA_IMAGE_ID, 32 GFI_USERDATA_START, 33 #ifdef CONFIG_BT_MESH_CTRL_RELAY 34 GFI_MESH_CTRL_RELAY, 35 #endif 36 }; 37 38 typedef enum 39 { 40 GENIE_STORAGE_SUCCESS = 0, 41 GENIE_STORAGE_INIT_FAIL, 42 GENIE_STORAGE_MALLOC_FAIL, 43 GENIE_STORAGE_EARSE_FAIL, 44 GENIE_STORAGE_DATA_INVALID, 45 GENIE_STORAGE_ERASE_FAIL, 46 GENIE_STORAGE_READ_FAIL, 47 GENIE_STORAGE_WRITE_FAIL, 48 GENIE_STORAGE_DELETE_FAIL, 49 GENIE_STORAGE_INVALID_INDEX, 50 GENIE_STORAGE_SIZE_ERROR, 51 GENIE_STORAGE_CHECK_CRC_FAIL 52 } genie_storage_status_e; 53 54 typedef struct{ 55 uint16_t dst; 56 uint8_t count; 57 uint8_t period; 58 uint8_t ttl; 59 uint16_t feat; 60 uint16_t net_idx; 61 } mesh_hb_para_t; 62 63 typedef struct{ 64 uint16_t net_index; 65 uint8_t flag; 66 uint32_t ivi; 67 uint8_t key[16]; 68 } mesh_netkey_para_t; 69 70 typedef struct{ 71 uint16_t net_index; 72 uint16_t key_index; 73 uint8_t flag; 74 uint8_t key[16]; 75 } mesh_appkey_para_t; 76 77 #ifdef CONFIG_BT_MESH_CTRL_RELAY 78 typedef struct 79 { 80 uint8_t enable; 81 uint8_t trd_n; 82 uint8_t rssi; 83 uint8_t sta_period; 84 uint8_t chk_period; 85 uint8_t req_period; 86 } mesh_ctrl_relay_para_t; 87 #endif 88 89 /** 90 * @brief iniatialize the flash for further operation 91 * @return the status of iniatializetion, 0 for successed. 92 */ 93 genie_storage_status_e genie_storage_init(void); 94 95 /** 96 * @brief read the date from flash and decrypt it. 97 * @param[in] index refers to the flash partition to be read. 98 * @param[out] p_buff: the data. 99 * @param[in] data_size: the size of the data. 100 * @return the status of reading operation. 101 */ 102 genie_storage_status_e genie_storage_read_reliable(uint16_t index, uint8_t *p_buff, uint16_t data_size); 103 104 /** 105 * @brief encrypt the data and write it into flash 106 * @param[in] index refers to the flash partition to be read. 107 * @param[out] p_buff: the data. 108 * @param[in] data_size: the size of the data. 109 * @return the status of writing operation. 110 */ 111 genie_storage_status_e genie_storage_write_reliable(uint16_t index, uint8_t *p_buff, uint16_t data_size); 112 113 /** 114 * @brief delete the contents saved at the partition refered by index. 115 * @param[in] index refers to the partition 116 * @return the status of the operation. 117 */ 118 genie_storage_status_e genie_storage_delete_reliable(uint16_t index); 119 120 /** 121 * @brief read the user raw date from flash 122 * @param[in] index refers to the flash partition to be read. 123 * @param[out] p_buff: the data. 124 * @param[in] data_size: the size of the data. 125 * @return the status of reading operation. 126 */ 127 genie_storage_status_e genie_storage_read_userdata(uint16_t index, uint8_t *p_buff, uint16_t data_size); 128 129 /** 130 * @brief write the date into flash 131 * @param[in] index refers to the flash partition to be read. 132 * @param[out] p_buff: the data. 133 * @param[in] data_size: the size of the data. 134 * @return the status of writing operation. 135 */ 136 genie_storage_status_e genie_storage_write_userdata(uint16_t index, uint8_t *p_buff, uint16_t data_size); 137 138 /** 139 * @brief delete the contents saved at the partition refered by index. 140 * @param[in] index refers to the partition 141 * @return the status of the operation. 142 */ 143 genie_storage_status_e genie_storage_delete_userdata(uint16_t index); 144 145 genie_storage_status_e genie_triple_write(uint32_t *p_pid, uint8_t *p_mac, uint8_t *p_key); 146 genie_storage_status_e genie_triple_read(uint32_t *p_pid, uint8_t *p_mac, uint8_t *p_key); 147 148 /** 149 * @brief save the subscription address to flash 150 * @param[in] p_sub: the list of subscription address 151 * @return the status of operation, 0 means successed. 152 */ 153 genie_storage_status_e genie_storage_write_sub(uint16_t *p_sub); 154 155 /** 156 * @brief read the subscription address from flash 157 * @param[out] p_sub: the list of subscription address 158 * @return the status of operation, 0 means successed. 159 */ 160 genie_storage_status_e genie_storage_read_sub(uint16_t *p_sub); 161 162 /** 163 * @brief save the parameters for mesh to flash 164 * @param[in] mesh_data: the parameters for mesh 165 * @return the status of operation, 0 means successed. 166 */ 167 genie_storage_status_e genie_storage_write_para(struct bt_mesh_net *mesh_data); 168 169 /** 170 * @brief read the parameters for mesh from flash 171 * @param[in] mesh_data: the parameters for mesh 172 * @return the status of operation, 0 means successed. 173 */ 174 genie_storage_status_e genie_storage_read_para(struct bt_mesh_net *mesh_data); 175 176 /** 177 * @brief save the address of peer mesh devices to flash 178 * @param[in] p_addr: address 179 * @return the status of operation, 0 means successed. 180 */ 181 genie_storage_status_e genie_storage_write_addr(uint16_t *p_addr); 182 183 /** 184 * @brief read the address of peer mesh devices from flash 185 * @param[out] p_addr: address 186 * @return the status of operation, 0 means successed. 187 */ 188 genie_storage_status_e genie_storage_read_addr(uint16_t *p_addr); 189 190 /** 191 * @brief write the heartbeat parameters to flash 192 * @param[in] p_para: heartbeat parameters 193 * @return the status of operation, 0 means successed. 194 */ 195 genie_storage_status_e genie_storage_write_hb(mesh_hb_para_t *p_para); 196 197 /** 198 * @brief read the heartbeat parameters from flash 199 * @param[out] p_para: heartbeat parameters 200 * @return the status of operation, 0 means successed. 201 */ 202 genie_storage_status_e genie_storage_read_hb(mesh_hb_para_t *p_para); 203 204 #ifdef CONFIG_BT_MESH_CTRL_RELAY 205 /** 206 * @brief write the ctrl relay parameters to flash 207 * @param[in] p_para: ctrl relay parameters 208 * @return the status of operation, 0 means successed. 209 */ 210 genie_storage_status_e genie_storage_write_ctrl_relay(mesh_ctrl_relay_para_t *p_para); 211 212 /** 213 * @brief read the ctrl relay parameters from flash 214 * @param[out] p_para: ctrl relay parameters 215 * @return the status of operation, 0 means successed. 216 */ 217 genie_storage_status_e genie_storage_read_ctrl_relay(mesh_ctrl_relay_para_t *p_para); 218 #endif 219 220 /** 221 * @brief save the devices key to flash 222 * @param[in] status: devcie key 223 * @return the status of operation, 0 means successed. 224 */ 225 genie_storage_status_e genie_storage_write_devkey(uint8_t *p_devkey); 226 227 /** 228 * @brief read the devices key from flash 229 * @param[out] status: devcie key 230 * @return the status of operation, 0 means successed. 231 */ 232 genie_storage_status_e genie_storage_read_devkey(uint8_t *p_devkey); 233 234 /** 235 * @brief save the net key to flash 236 * @param[in] status: net key 237 * @return the status of operation, 0 means successed. 238 */ 239 genie_storage_status_e genie_storage_write_netkey(mesh_netkey_para_t *p_netkey); 240 241 /** 242 * @brief read the net key from flash 243 * @param[out] status: net key 244 * @return the status of operation, 0 means successed. 245 */ 246 genie_storage_status_e genie_storage_read_netkey(mesh_netkey_para_t *p_netkey); 247 248 /** 249 * @brief save the app key to flash 250 * @param[in] status: app key 251 * @return the status of operation, 0 means successed. 252 */ 253 genie_storage_status_e genie_storage_write_appkey(mesh_appkey_para_t *p_appkey); 254 255 /** 256 * @brief read the app key from flash 257 * @param[out] status: app key 258 * @return the status of operation, 0 means successed. 259 */ 260 genie_storage_status_e genie_storage_read_appkey(mesh_appkey_para_t *p_appkey); 261 262 /** 263 * @brief save the seq number to flash 264 * @param[in] status: seq number 265 * @return the status of operation, 0 means successed. 266 */ 267 genie_storage_status_e genie_storage_write_seq(uint32_t *p_seq, bool force_write); 268 269 /** 270 * @brief read the seq number from flash 271 * @param[out] status: seq number 272 * @return the status of operation, 0 means successed. 273 */ 274 genie_storage_status_e genie_storage_read_seq(uint32_t *p_seq); 275 276 /** 277 * @brief delete the seq number from flash 278 * @return the status of operation, 0 means successed. 279 */ 280 genie_storage_status_e genie_storage_delete_seq(void); 281 282 /** 283 * @brief erase everyting on flash except the trituple info 284 * @return the status of operation, 0 means successed. 285 */ 286 genie_storage_status_e genie_storage_reset_system(void); 287 288 #endif/*--_GENIE_STORAGE_H_--*/ 289