// Author: NiuJiuRu
// Email: niujiuru@qq.com
// Date: 2025-12-20
#ifndef __YMODEM_H__
#define __YMODEM_H__

#define PACKET_SEQNO_INDEX      (1)
#define PACKET_SEQNO_COMP_INDEX (2)

#define PACKET_HEADER           (3)     /* start, block, block-complement */
#define PACKET_TRAILER          (2)     /* CRC bytes */
#define PACKET_OVERHEAD         (PACKET_HEADER + PACKET_TRAILER)
#define PACKET_SIZE             (128)
#define PACKET_1K_SIZE          (1024)
#define PACKET_TIMEOUT          (1000)  /* milliseconds */

#define FILE_NAME_LENGTH (64)
#define FILE_SIZE_LENGTH (16)

/* ASCII control codes: */
#define SOH (0x01)  /* start of 128-byte data packet */
#define STX (0x02)  /* start of 1024-byte data packet */
#define EOT (0x04)  /* end of transmission */
#define ACK (0x06)  /* receive OK */
#define NAK (0x15)  /* receiver error; retry */
#define CAN (0x18)  /* two of these in succession aborts transfer */
#define CRC (0x43)  /* use in place of first NAK for CRC mode */

/* Number of consecutive receive errors before giving up: */
#define MAX_ERRORS      (5)

/* Number of startup wait attempts before giving up: */
#define MAX_START_WAITS (30)

#define UART_MODULE_NAME "YMODEM"         /* 串口模块名称 */
#define UART_DEVICE_NAME  "/dev/ttymxc2"  /* 串口设备名称 */
#define UART_BAUD_RATE    115200          /* 串口的波特率 */
#define UART_PARITY_CHECK "none"          /* 串口的校验位 */

#include "../swapi/include_swapiLib.h"

#ifdef __cplusplus
extern "C"
{
#endif

// 通过串口使用 YMODEM 协议接收多个文件, >=0 实际成功接收的文件数, <0 接收过程中发生的错误
int ymodem_recv_files(const char *dir);

// 通过串口使用 YMODEM 协议发送一个文件, >=0 实际成功发送的字节数, <0 发送过程中发生的错误
int ymodem_send_file(const char *path);

#ifdef __cplusplus
}
#endif

#endif /* __YMODEM_H__ */
