Ymodem ESP32 1.0.0
Loading...
Searching...
No Matches
YmodemPaquets.cpp
Go to the documentation of this file.
1
14#include "YmodemPaquets.h"
15
16void Ymodem_PrepareIntialPacket(uint8_t* data, char* fileName, uint32_t length)
17{
18 uint16_t tempCRC;
19
20 memset(data, 0, PACKET_SIZE + PACKET_HEADER);
21 // Make first three packet
22 data[0] = SOH;
23 data[1] = 0x00;
24 data[2] = 0xff;
25
26 // add filename
27 sprintf((char*)(data + PACKET_HEADER), "%s", fileName);
28
29 // add file site
30 sprintf((char*)(data + PACKET_HEADER + strlen((char*)(data + PACKET_HEADER)) + 1), "%d", length);
31 data[PACKET_HEADER + strlen((char*)(data + PACKET_HEADER)) + 1 +
32 strlen((char*)(data + PACKET_HEADER + strlen((char*)(data + PACKET_HEADER)) + 1))] = ' ';
33
34 // add crc
35 tempCRC = crc16(&data[PACKET_HEADER], PACKET_SIZE);
36 data[PACKET_SIZE + PACKET_HEADER] = tempCRC >> 8;
37 data[PACKET_SIZE + PACKET_HEADER + 1] = tempCRC & 0xFF;
38}
39
40void Ymodem_PrepareLastPacket(uint8_t* data)
41{
42 uint16_t tempCRC;
43
44 memset(data, 0, PACKET_SIZE + PACKET_HEADER);
45 data[0] = SOH;
46 data[1] = 0x00;
47 data[2] = 0xff;
48 tempCRC = crc16(&data[PACKET_HEADER], PACKET_SIZE);
49 // tempCRC = crc16_le(0, &data[PACKET_HEADER], PACKET_SIZE);
50 data[PACKET_SIZE + PACKET_HEADER] = tempCRC >> 8;
51 data[PACKET_SIZE + PACKET_HEADER + 1] = tempCRC & 0xFF;
52}
53
54void Ymodem_PreparePacket(uint8_t* data, uint8_t pktNo, uint32_t sizeBlk, fs::File& ffd)
55{
56 uint16_t i, size;
57 uint16_t tempCRC;
58
59 data[0] = STX;
60 data[1] = (pktNo & 0x000000ff);
61 data[2] = (~(pktNo & 0x000000ff));
62
63 size = sizeBlk < PACKET_1K_SIZE ? sizeBlk : PACKET_1K_SIZE;
64 // Read block from file
65 if (size > 0) {
66 size = ffd.read(data + PACKET_HEADER, size);
67 }
68
69 if (size < PACKET_1K_SIZE) {
70 for (i = size + PACKET_HEADER; i < PACKET_1K_SIZE + PACKET_HEADER; i++) {
71 data[i] = 0x00; // EOF (0x1A) or 0x00
72 }
73 }
74 tempCRC = crc16(&data[PACKET_HEADER], PACKET_1K_SIZE);
75 // tempCRC = crc16_le(0, &data[PACKET_HEADER], PACKET_1K_SIZE);
76 data[PACKET_1K_SIZE + PACKET_HEADER] = tempCRC >> 8;
77 data[PACKET_1K_SIZE + PACKET_HEADER + 1] = tempCRC & 0xFF;
78}
79
80uint8_t Ymodem_WaitResponse(uint8_t ackchr, uint8_t tmo)
81{
82 unsigned char receivedC;
83 uint32_t errors = 0;
84
85 do {
86 if (Receive_Byte(&receivedC, NAK_TIMEOUT) == 0) {
87 if (receivedC == ackchr) {
88 return 1;
89 }
90 else if (receivedC == CA) {
91 send_CA();
92 return 2; // CA received, Sender abort
93 }
94 else if (receivedC == NAK) {
95 return 3;
96 }
97 else {
98 return 4;
99 }
100 }
101 else {
102 errors++;
103 }
104 } while (errors < tmo);
105 return 0;
106}
#define PACKET_HEADER
Definition YmodemDef.h:29
#define NAK_TIMEOUT
Definition YmodemDef.h:47
#define PACKET_1K_SIZE
Definition YmodemDef.h:33
#define NAK
Definition YmodemDef.h:40
#define STX
Definition YmodemDef.h:37
#define SOH
Definition YmodemDef.h:36
#define CA
Definition YmodemDef.h:41
#define PACKET_SIZE
Definition YmodemDef.h:32
void Ymodem_PrepareIntialPacket(uint8_t *data, char *fileName, uint32_t length)
Prepares the initial packet for Ymodem transmission.
void Ymodem_PreparePacket(uint8_t *data, uint8_t pktNo, uint32_t sizeBlk, fs::File &ffd)
Prepares a Ymodem packet with the given data.
uint8_t Ymodem_WaitResponse(uint8_t ackchr, uint8_t tmo)
Waits for a specific response character within a given timeout period.
void Ymodem_PrepareLastPacket(uint8_t *data)
Prepares the last packet for Ymodem transmission.
Ymodem Packet preparation functions.
unsigned short crc16(const unsigned char *buf, unsigned long count)
Computes the CRC-16 checksum for a given buffer.
void send_CA()
Sends the CA (Cancel) signal.
int32_t Receive_Byte(unsigned char *c, uint32_t timeout)
Receives a byte from a communication interface.