1 #ifndef __GENIE_OTA_H__
2 #define __GENIE_OTA_H__
3 
4 //For pingpong OTA
5 enum
6 {
7     DFU_IMAGE_A = 0,
8     DFU_IMAGE_B,
9     DFU_IMAGE_TOTAL = DFU_IMAGE_B,
10     DFU_IMAGE_INVALID_ID,
11 };
12 
13 #define OTA_RECV_BUF_SIZE (CONFIG_AIS_TOTAL_FRAME * 128)
14 #define OTA_RECV_MAX_ERR_COUNT 10
15 
16 #define GENIE_OTA_IMAGE_ID_MAGIC (0xA5A5A5)
17 #define GENIE_OTA_IMAGE_ID_A (GENIE_OTA_IMAGE_ID_MAGIC << 8 | DFU_IMAGE_A)
18 #define GENIE_OTA_IMAGE_ID_B (GENIE_OTA_IMAGE_ID_MAGIC << 8 | DFU_IMAGE_B)
19 
20 #define AIS_OTA_AUTH_TIMEOUT 10000    //10s
21 #define AIS_OTA_DISCONN_TIMEOUT 60000 //60s
22 #define AIS_OTA_REPORT_TIMEOUT (CONFIG_AIS_TOTAL_FRAME * 400)
23 #define AIS_OTA_REBOOT_TIMEOUT 3000 //3s
24 
25 enum
26 {
27     OTA_FLAG_SILENT = 0x02,
28     AIS_OTA_VER_REQ = 0x20,      //APP request get version info
29     AIS_OTA_VER_RESP = 0x21,     //Response the version info request
30     AIS_OTA_FIRMWARE_REQ = 0x22, //APP push firmware info
31     AIS_OTA_UPD_RESP = 0x23,     //Response to APP which can update or not and have received firmware size
32     AIS_OTA_STATUS = 0x24,       //Response to APP that last frame number and have received firmware size
33     AIS_OTA_CHECK_REQ = 0x25,    //APP download finish,then device check firmware
34     AIS_OTA_CHECK_RESP = 0x26,   //Response to APP the result of firmeware check
35     AIS_OTA_DATA = 0x2f          //This is ota firmware data
36 };
37 
38 typedef struct genie_ota_ctx_s
39 {
40     uint8_t err_count;
41 
42     uint8_t last_seq;
43     uint8_t total_frame;
44     uint8_t except_seq;
45     uint32_t rx_size;
46 
47     uint8_t image_type;
48     uint32_t image_ver;
49     uint32_t image_size;
50     uint16_t image_crc16;
51     uint8_t ota_flag;
52 
53     uint8_t flash_clean : 1;
54     uint8_t ota_ready : 1;
55     uint16_t rx_len;
56     uint8_t recv_buf[OTA_RECV_BUF_SIZE];
57 } genie_ota_ctx_t;
58 
59 typedef struct
60 {
61     uint8_t image_type;
62 } __attribute__((packed)) ais_ota_ver_req_t;
63 
64 typedef struct
65 {
66     uint8_t image_type;
67     uint32_t ver;
68 } __attribute__((packed)) ais_ota_ver_resp_t;
69 
70 typedef struct
71 {
72     uint8_t image_type;
73     uint32_t ver;
74     uint32_t fw_size;
75     uint16_t crc16;
76     uint8_t ota_flag;
77 } __attribute__((packed)) ais_ota_upd_req_t;
78 
79 typedef struct
80 {
81     uint8_t state;
82     uint32_t rx_size;
83     uint8_t total_frame;
84 } __attribute__((packed)) ais_ota_upd_resp_t;
85 
86 typedef struct
87 {
88     uint8_t last_seq : 4;
89     uint8_t total_frame : 4;
90     uint32_t rx_size;
91 } __attribute__((packed)) ais_ota_status_report_t;
92 
93 typedef struct
94 {
95     uint8_t state;
96 } __attribute__((packed)) ais_ota_check_req_t;
97 
98 typedef struct
99 {
100     uint8_t state;
101 } __attribute__((packed)) ais_ota_check_resp_t;
102 
103 bool genie_ota_handle_update_request(uint8_t msg_id, ais_ota_upd_req_t *p_ota_req, uint8_t encrypt);
104 
105 bool genie_ota_handle_version_request(uint8_t msg_id, ais_ota_ver_req_t *p_ver_req, uint8_t encrypt);
106 
107 bool genie_ota_parse_pdu(ais_pdu_t *p_msg);
108 
109 bool genie_ota_check_firmware(uint8_t msg_id, ais_ota_check_req_t *p_check_req);
110 
111 bool genie_ota_is_ready(void);
112 
113 /**
114  * @brief get the ota indication flag from flash.
115  * @return the ota indication flag.
116  */
117 bool genie_ota_get_indication(void);
118 
119 void genie_ota_status_report(void);
120 
121 bool genie_ota_is_updating(void);
122 
123 int genie_ota_report_version(void);
124 
125 int genie_ota_init(void);
126 
127 #endif
128