-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataLink.cpp
68 lines (52 loc) · 1.88 KB
/
DataLink.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
// DataLink.cpp
// author: @mikesoylu
#include "DataLink.h"
#include "Hamming.h"
#include "UChannel.h"
#include <algorithm>
using std::min;
void DataLink::send(int port, char* packet, int packet_length) {
UChannel channel;
for (int i = 0; i < packet_length; ) {
// get size of the current subpacket
int subpacket_length = min(packet_length-i, SUBPACKET_SIZE);
#ifdef DEBUG
printf("alloc'ing %d bytes for encoded data\n", Hamming::getEncodedLength(subpacket_length));
#endif
// alloc space for the encoded subpacket
char *encoded = new char[Hamming::getEncodedLength(subpacket_length)];
// encode data
Hamming encoder(packet+i, encoded, subpacket_length);
encoder.encode();
// send it off
channel.u_send(port, encoded, Hamming::getEncodedLength(subpacket_length));
// clean up
delete [] encoded;
i += subpacket_length;
}
}
void DataLink::receive(int port, char* packet, int packet_length) {
UChannel channel;
for (int i = 0; i < packet_length; ) {
// get size of the current subpacket
int subpacket_length = min(packet_length-i, SUBPACKET_SIZE);
int encoded_length = Hamming::getEncodedLength(subpacket_length);
#ifdef DEBUG
printf("alloc'ing %d bytes for encoded data\n", encoded_length);
#endif
// alloc space for the encoded subpacket
char *encoded = new char[encoded_length];
char *decoded = new char[subpacket_length];
// receive encoded data
channel.u_recv(port, encoded, encoded_length);
// decode the encoded data
Hamming decoder(encoded, decoded, encoded_length);
decoder.decode();
// copy decoded data to packet
memcpy(packet+i, decoded, subpacket_length);
// clean up
delete [] encoded;
delete [] decoded;
i+= subpacket_length;
}
}