1 /****************************************************************************** 2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK") 3 * All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 *****************************************************************************/ 18 #ifndef OTA_H_ 19 #define OTA_H_ 20 21 #include <types.h> 22 23 /** 24 * @brief Legacy OTA command 25 */ 26 #define CMD_OTA_VERSION 0xFF00 // client -> server 27 #define CMD_OTA_START 0xFF01 // client -> server 28 #define CMD_OTA_END 0xFF02 // client -> server 29 30 /** 31 * @brief Extended OTA command, optional 32 */ 33 #define CMD_OTA_START_EXT 0xFF03 // client -> server 34 #define CMD_OTA_FW_VERSION_REQ 0xFF04 // client -> server 35 #define CMD_OTA_FW_VERSION_RSP 0xFF05 // server -> client 36 #define CMD_OTA_RESULT 0xFF06 // server -> client 37 38 /** 39 * @brief Multiple boot address enumeration 40 */ 41 typedef enum { 42 MULTI_BOOT_ADDR_0x20000 = 0x20000, // 128 K 43 MULTI_BOOT_ADDR_0x40000 = 0x40000, // 256 K 44 MULTI_BOOT_ADDR_0x80000 = 0x80000, // 512 K 45 } multi_boot_addr_e; 46 47 /** 48 * @brief OTA result 49 */ 50 enum { 51 // 0x00 52 OTA_SUCCESS = 0, // success 53 OTA_DATA_PACKET_SEQ_ERR, // OTA data packet sequence number error: repeated OTA PDU or lost some OTA PDU 54 OTA_PACKET_INVALID, // invalid OTA packet: 55 // 1. invalid OTA command 56 // 2. addr_index out of range 57 // 3.not standard OTA PDU length 58 OTA_DATA_CRC_ERR, // packet PDU CRC err 59 60 // 0x04 61 OTA_WRITE_FLASH_ERR, // write OTA data to flash ERR 62 OTA_DATA_UNCOMPLETE, // lost last one or more OTA PDU 63 OTA_FLOW_ERR, // peer device send OTA command or OTA data not in correct flow 64 OTA_FW_CHECK_ERR, // firmware CRC check error 65 66 // 0x08 67 OTA_VERSION_COMPARE_ERR, // the version number to be update is lower than the current version 68 OTA_PDU_LEN_ERR, // PDU length error: not 16*n, or not equal to the value it declare in "CMD_OTA_START_EXT" packet 69 OTA_FIRMWARE_MARK_ERR, // firmware mark error: not generated by telink's BLE SDK 70 OTA_FW_SIZE_ERR, // firmware size error: no firmware_size; firmware size too small or too big 71 72 // 0x0C 73 OTA_DATA_PACKET_TIMEOUT, // time interval between two consequent packet exceed a value(user can adjust this value) 74 OTA_TIMEOUT, // OTA flow total timeout 75 OTA_FAIL_DUE_TO_CONNECTION_TERMIANTE, // OTA fail due to current connection terminate 76 // (maybe connection timeout or local/peer device terminate connection) 77 }; 78 79 /** 80 * @brief data structure of OTA command "CMD_OTA_START" 81 */ 82 typedef struct { 83 u16 ota_cmd; 84 } ota_start_t; 85 86 /** 87 * @brief data structure of OTA command "CMD_OTA_START_EXT" 88 */ 89 typedef struct { 90 u16 ota_cmd; 91 u8 pdu_length; // must be: 16*n(n is in range of 1 ~ 15); pdu_length: 16,32,48,...240 92 u8 version_compare; // 0: no version compare; 1: only higher version can replace lower version 93 } ota_startExt_t; 94 95 /** 96 * @brief data structure of OTA command "CMD_OTA_END" 97 */ 98 typedef struct { 99 u16 ota_cmd; 100 u16 adr_index_max; 101 u16 adr_index_max_xor; 102 } ota_end_t; 103 104 /** 105 * @brief data structure of OTA command "CMD_OTA_RESULT" 106 */ 107 typedef struct { 108 u16 ota_cmd; 109 u8 result; 110 } ota_result_t; 111 112 /** 113 * @brief data structure of OTA command "CMD_OTA_FW_VERSION_REQ" 114 */ 115 typedef struct { 116 u16 ota_cmd; 117 u16 version_num; 118 u8 version_compare; // 1: only higher version can replace lower version 119 } ota_versionReq_t; 120 121 /** 122 * @brief data structure of OTA command "CMD_OTA_FW_VERSION_RSP" 123 */ 124 typedef struct { 125 u16 ota_cmd; 126 u16 version_num; 127 u8 version_accept; // 1: accept firmware update; 0: reject firmware update 128 // (version compare enable, and compare result: fail) 129 } ota_versionRsp_t; 130 131 typedef struct { 132 u16 adr_index; 133 u8 data[16]; 134 u16 crc_16; 135 } ota_pdu16_t; 136 137 unsigned long crc32_half_cal(unsigned long crc, unsigned char *input, unsigned long *table, int len); 138 unsigned long crc32_cal(unsigned long crc, unsigned char *input, unsigned long *table, int len); 139 140 #endif /* OTA_H_ */ 141