-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_obb.c
149 lines (128 loc) · 4.11 KB
/
test_obb.c
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
146
147
148
149
// runs on patmos
// minimal test file to get started on obb
// the server receives a request and responds (i.e., acknowledges)
// with the same request msg id and increments the flag
// more to be done...getting started...
#include <stdio.h>
#include <string.h> //memset
#include <machine/patmos.h>
#include "slip.h"
#include "tpip.h"
static unsigned char bufin[2000];
static unsigned char bufout[2000];
__attribute__((noinline))
int receive(unsigned char *pbuf)
{
int cnt = 0;
int slipcnt = 0;
static unsigned char c;
do{
while(!tpip_slip_getchar(&c));
printf("slip %02d = 0x%02x\n", slipcnt, c);
slipcnt++;
if (tpip_slip_is_end(c)){
if(cnt == 0)
continue; // slip frames starting with END
else
break;
} else if (tpip_slip_is_esc(c)){
while(!tpip_slip_getchar(&c));
printf("slip %02d = 0x%02x\n", slipcnt, c);
slipcnt++;
c = tpip_slip_was_esc(c);
}
*pbuf = c;
pbuf++;
cnt++;
} while(1);
printf("patmos receive: %d data bytes (%d slip bytes):\n", cnt, slipcnt);
return cnt;
}
// this function should be changed to slip
// modify this to encode and send over slip
__attribute__((noinline))
void xmitslip(unsigned char *pbuf, int cnt)
{
// cnt is the number of bytes to transmit
printf("xmit ip datagram:\n");
bufprint(pbuf, cnt);
printf("total %d bytes\n", cnt);
printf("\n");
printf("slip send:\n");
int i = 0;
int j = 0;
tpip_slip_put_end(); j++;
for (i = 0; i < cnt; i++, j++)
{
unsigned char c = *(pbuf + i);
printf("%02d:data byte 0x%02x\n", i, c);
if(tpip_slip_is_esc(c)){
tpip_slip_put_esc();
tpip_slip_put_esc_esc();
j++;
}
else if (tpip_slip_is_end(c)){
tpip_slip_put_esc();
tpip_slip_put_esc_end();
j++;
}
else {
tpip_slip_putchar(c);
}
}
tpip_slip_put_end(); j++;
printf("xmit total %d bytes\n", j);
}
int main(int argc, char *argv[])
{
memset(bufin, 0, sizeof(bufin));
memset(bufout, 0, sizeof(bufout));
printf("sizeof(unsigned char) =%lu\n", sizeof(unsigned char));
printf("sizeof(unsigned short)=%lu\n", sizeof(unsigned short));
printf("sizeof(unsigned int) =%lu\n", sizeof(unsigned int));
printf("patmos step 1: an UDP packet will now be sent to the PC with 4th byte set in the obb flag struct\n");
obb_t obb_msg = (obb_t){.flags = 1};
// prepare sending
// patmos, 10.0.0.2, 10002
// server, 10.0.0.3, 10003
ip_t ipout = {.verhdl = (0x4 << 4) | 0x5,
.tos = 0x03, //0x00,
.length = 20 + 8 + 4, // 5 + 2 + 1 words
.id = 1,
.ff = 0x4000,
.ttl = 0x30,
.prot = 0x11, // UDP
.checksum = 0x0000,
.srcip = (10 << 24) | (0 << 16) | (0 << 8) | 2,
.dstip = (10 << 24) | (0 << 16) | (0 << 8) | 3,
.udp.srcport = 10002, // 0x2712
.udp.dstport = 10003, // 0x2713
.udp.length = 8 + 4,
.udp.data = (unsigned char[]){0, 0, 0, 1}};//(unsigned char)obb_msg.flags}};
printf("ipout:\n");
printipdatagram(&ipout);
int len = packip(bufout, &ipout);
printf("bufout before checksum:\n");
bufprint(bufout, len);
ipchecksum(bufout);
printf("bufout after checksum:\n");
bufprint(bufout, len);
printf("\n");
printf("patmos sending: \n");
xmitslip(bufout, len);
printf("\n");
printf("patmos step 2: an UDP packet will now be received from the PC with 1st byte set in the obb flag struct\n");
unsigned char recbuf[2000];
memset(recbuf, 0, sizeof(recbuf));
int reccnt = receive(recbuf);
printf("ipin mem \"raw\" (%d bytes):\n", reccnt);
bufprint(recbuf, reccnt);
printf("\n");
ip_t *ipin = (ip_t*) recbuf;
unpackip(ipin, recbuf);
printf("ip datagram from pc:\n");
printipdatagram(ipin);
printf("obb flag test completed on patmos...\n");
tpip_slip_run();
return 0;
}