1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 /* Define to prevent recursive inclusion -------------------------------------*/
6 #ifndef __YMODEM_H_
7 #define __YMODEM_H_
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdint.h>
12 
13 #include "aos/hal/uart.h"
14 #include "aos/hal/flash.h"
15 
16 /* Includes ------------------------------------------------------------------*/
17 /* Exported types ------------------------------------------------------------*/
18 /* Exported constants --------------------------------------------------------*/
19 /* Exported macro ------------------------------------------------------------*/
20 #define PACKET_SEQNO_INDEX      (1)
21 #define PACKET_SEQNO_COMP_INDEX (2)
22 
23 #define PACKET_HEADER           (3)
24 #define PACKET_TRAILER          (2)
25 #define PACKET_OVERHEAD         (PACKET_HEADER + PACKET_TRAILER)
26 #define PACKET_SIZE             (128)
27 #define PACKET_1K_SIZE          (1024)
28 
29 #define FILE_NAME_LENGTH        (256)
30 #define FILE_SIZE_LENGTH        (16)
31 
32 #define SOH                     (0x01)  /* start of 128-byte data packet */
33 #define STX                     (0x02)  /* start of 1024-byte data packet */
34 #define EOT                     (0x04)  /* end of transmission */
35 #define ACK                     (0x06)  /* acknowledge */
36 #define NAK                     (0x15)  /* negative acknowledge */
37 #define CA                      (0x18)  /* two of these in succession aborts transfer */
38 #define CRC16                   (0x43)  /* 'C' == 0x43, request 16-bit CRC */
39 
40 #define ABORT1                  (0x41)  /* 'A' == 0x41, abort by user */
41 #define ABORT2                  (0x61)  /* 'a' == 0x61, abort by user */
42 
43 #define NAK_TIMEOUT             (1000)
44 #define MAX_ERRORS              (20)
45 
46 typedef struct {
47     uint8_t crc;
48 } CRC8_Context;
49 
50 typedef struct {
51     uint16_t crc;
52 } CRC16_Context;
53 
54 /* Private define ------------------------------------------------------------*/
55 #define IS_AF(c)  ((c >= 'A') && (c <= 'F'))
56 #define IS_af(c)  ((c >= 'a') && (c <= 'f'))
57 #define IS_09(c)  ((c >= '0') && (c <= '9'))
58 #define ISVALIDHEX(c)  IS_AF(c) || IS_af(c) || IS_09(c)
59 #define ISVALIDDEC(c)  IS_09(c)
60 #define CONVERTDEC(c)  (c - '0')
61 
62 #define CONVERTHEX_alpha(c)  (IS_AF(c) ? (c - 'A'+10) : (c - 'a'+10))
63 #define CONVERTHEX(c)   (IS_09(c) ? (c - '0') : CONVERTHEX_alpha(c))
64 
65 /* Exported functions ------------------------------------------------------- */
66 int32_t Ymodem_Receive (uint8_t *buf, hal_partition_t partition);
67 uint8_t Ymodem_Transmit (hal_partition_t partition, const uint8_t* sendFileName);
68 int32_t SerialDownload(void);
69 #endif  /* __YMODEM_H_ */
70 
71 
72