-
Notifications
You must be signed in to change notification settings - Fork 6
/
xmodem.cpp
145 lines (115 loc) · 3.14 KB
/
xmodem.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* XMODEM-CRC implementation according to:
* http://web.mit.edu/6.115/www/miscfiles/amulet/amulet-help/xmodem.htm
*/
#include "xmodem.h"
#include <QDebug>
#include <QSerialPort>
#include <QFile>
#include <QEventLoop>
#include <QTimer>
XMODEM::XMODEM(QSerialPort *sp, QObject *parent) :
QObject(parent)
{
m_serialPort = sp;
}
bool XMODEM::sendFile(const QString &filePath)
{
QFile file(filePath);
if(!file.open(QFile::ReadOnly))
{
qDebug() << "failed to open file" << filePath;
return false;
}
file.size();
char dataBuffer[128];
m_packetNumber = 1;
int totalBytesRead = 0;
m_serialPort->readAll();
while(!file.atEnd())
{
int bytesRead = file.read(dataBuffer, 128);
totalBytesRead += bytesRead;
for(int i = bytesRead; i < 128; i++)
dataBuffer[i] = 0xFF; // pad buffer with 0xFF
qDebug() << "packet:" << m_packetNumber << "bytes transfered:" << QString().sprintf("%10lu / %10lu", totalBytesRead, file.size());
emit output(QString().sprintf("[%6lu / %6lu]", totalBytesRead, file.size()));
sendPacket(dataBuffer, 128);
waitACK(1000);
}
dataBuffer[0] = (char) XMODEM::EOT;
m_serialPort->write(dataBuffer, 1);
waitACK(1000);
qDebug() << "file sent";
qDebug() << m_serialPort->readAll();
file.close();
return true;
}
void XMODEM::sendPacket(char *data, int count)
{
QSerialPort *sp = m_serialPort;
QByteArray packet;
int crc = calculateCRC(data, count);
packet.append(XMODEM::SOH);
packet.append(m_packetNumber);
packet.append(0xFF - m_packetNumber);
packet.append(data, count);
packet.append((char) ((crc >> 8) & 0xFF));
packet.append((char) (crc & 0xFF));
m_packetNumber++;
if(m_packetNumber > 255)
m_packetNumber = 0;
sp->write(packet);
}
int XMODEM::waitACK(int timeout)
{
int retries = 4;
QByteArray rxData;
QTimer timer;
QEventLoop eventLoop;
timer.setInterval(3500);
connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
connect(m_serialPort, SIGNAL(readyRead()), &eventLoop, SLOT(quit()));
while(retries-- > 0)
{
eventLoop.exec();
rxData = m_serialPort->readAll();
if(rxData.size() > 0)
break;
}
if(retries == 0 && rxData.size() == 0)
{
qDebug() << "failed to get ack";
emit output(tr("XMODEM: Failed to get ACK"));
return -1;
}
int code = rxData.at(0);
switch(code)
{
case XMODEM::ACK: qDebug() << "ACK"; break;
case XMODEM::NAK: qDebug() << "NAK"; break;
default:
qDebug() << "unknown ACK code:" << QString().sprintf("%02X",code);
emit output(tr("XMODEM: Unknown ACK code") + QString().sprintf("%02X",code));
}
return code;
}
int XMODEM::calculateCRC(char *ptr, int count)
{
int crc;
char i;
crc = 0;
while (--count >= 0)
{
crc = crc ^ (int) *ptr++ << 8;
i = 8;
do
{
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
} while(--i);
}
return (crc);
}