-
Notifications
You must be signed in to change notification settings - Fork 48
/
XModem.h
78 lines (68 loc) · 2.46 KB
/
XModem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// This code was taken from: https://code.google.com/archive/p/arduino-xmodem
// (https://code.google.com/archive/p/arduino-xmodem)
// which was released under GPL V3:
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -----------------------------------------------------------------------------
typedef enum {
Crc,
ChkSum
} transfer_t;
class XModem {
private:
//delay when receive bytes in frame - 7 secs
static const int receiveDelay;
//retry limit when receiving
static const int rcvRetryLimit;
//holds readed byte (due to dataAvail())
int byte;
//expected block number
unsigned char blockNo;
//extended block number, send to dataHandler()
unsigned long blockNoExt;
//retry counter for NACK
int retries;
//buffer
char buffer[133];
//repeated block flag
bool repeatedBlock;
int (*recvChar)(int);
void (*sendData)(const char *data, int len);
bool (*dataHandler)(unsigned long number, char *buffer, int len);
unsigned short crc16_ccitt(char *buf, int size);
bool dataAvail(int delay);
int dataRead(int delay);
void dataWrite(char symbol);
bool receiveFrameNo(void);
bool receiveData(void);
bool checkCrc(void);
bool checkChkSum(void);
bool receiveFrames(transfer_t transfer);
bool sendNack(void);
void init(void);
bool transmitFrames(transfer_t);
unsigned char generateChkSum(const char *buffer, int len);
public:
static const unsigned char NACK;
static const unsigned char ACK;
static const unsigned char SOH;
static const unsigned char EOT;
static const unsigned char CAN;
XModem(int (*recvChar)(int), void (*sendData)(const char *data, int len));
XModem(int (*recvChar)(int), void (*sendData)(const char *data, int len),
bool (*dataHandler)(unsigned long, char*, int));
bool receive();
bool transmit();
};