-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecg.c
179 lines (143 loc) · 4.48 KB
/
ecg.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "radio.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#define TIMEOUT_SEC 10
int snd_addr = 2132;
int rcv_addr = 2135;
int main(){
int err, last, addr;
char type;
char data[FRAME_PAYLOAD_SIZE];
// User input for sender or reciever
printf("Type of radio (s or r):\n");
scanf("%c", &type);
// Set address
if (type == 's'){
addr = snd_addr;
}
else{
addr = rcv_addr;
}
// Initialize node
if ((err=radio_init(addr)) != ERR_OK) {
printf("Radio could not be initialized: %d\n", err);
return 1;
}
// FSM as sender
if (type == 's'){
char sender = 'i';
while(1){
memset((char *) &data, 0 , FRAME_PAYLOAD_SIZE);
// preamble for the dataframe
char *pre = (char*) malloc(20);
pre = "AAAAAAAAAAAAAAAAAAAA";
strcpy(data, pre);
char *magickey = (char*) malloc(8);
magickey = "1337F542";
strcat(data, magickey);
switch(sender){
case 'i': // Idle
printf("case (send): idle\n");
// Payload
char *payload = (char*) malloc(144);
printf("Type the payload (integer):\n");
scanf("%s",payload);
printf("payload: %s\n", payload);
// Header
char *header = (char*) malloc(12);
sprintf(header, "%02x", strlen(payload));
printf("header: %s\n", header);
int checksum;
char *hex_sum = (char*) malloc(8);
checksum = 0;
sprintf(hex_sum, "%08x", checksum);
strcat(header, hex_sum);
printf("checksum: %s\n", header);
int id;
char *hex_id = (char*) malloc(1);
id = 1;
sprintf(hex_id, "%01x", id);
strcat(header, hex_id);
printf("id: %s\n", header);
int flag;
char *hex_flag = (char*) malloc(1);
flag = 0;
sprintf(hex_flag, "%01x", flag);
strcat(header, hex_flag);
printf("flag: %s\n", header);
strcat(data, header);
strcat(data, payload);
printf("data: %s\n", data);
sender = 's';
case 's': // Send
printf("case (send): start\n");
if ( (err=radio_send(rcv_addr, data, FRAME_PAYLOAD_SIZE)) != ERR_OK) {
printf("radio_send failed with %d\n", err);
return 1;
}
sender = 'i';
/* case 'send_data':
case 'done':
case 'end':
break;
*/ }
}
}
// FSM as reciever
if (type == 'r'){
char reciever = 'r';
char buf[FRAME_PAYLOAD_SIZE + 1];
int err, len;
int source;
while(1){
switch(reciever){
case 'r': // Ready
memset((char *) buf, 0, FRAME_PAYLOAD_SIZE+1);
printf("case (rec): ready\n");
if ( (len=radio_recv(&source, buf, TIMEOUT_SEC * 1000)) < 0) {
if (len == ERR_TIMEOUT) {
printf("radio_recv timed out\n");
continue;
}
printf("radio_recv failed with %d\n", len);
return 1;
}
printf("Received message: %s\n", buf);
char *preamble = (char*) malloc(20);
strncpy(preamble, buf, 20);
printf("Received preamble (hex): %s\n", preamble);
char *magickey = (char*) malloc(8);
strncpy(magickey, buf+20, 8);
printf("Received magickey (hex): %s\n", magickey);
char *header = (char*) malloc(12);
strncpy(header, buf+28, 12);
printf("Received header (hex): %s\n", header);
char *pay_len = (char*) malloc(2);
strncpy(pay_len, header, 2);
//int pay_lenint = (int)strtol(&pay_len, NULL, 16);
printf("Received pay_len : %s\n", pay_len);
//printf("Received pay_lenint : %s\n", pay_lenint);
char *checksum = (char*) malloc(8);
strncpy(checksum, header+2, 8);
printf("Received checksum (hex): %s\n", checksum);
char *id = (char*) malloc(1);
strncpy(id, header+10, 1);
printf("Received id: %s\n", id);
char *flag = (char*) malloc(1);
strncpy(flag, header+11, 1);
printf("Received flag: %s\n", flag);
char *pakage = (char*) malloc(144);
strncpy(pakage, buf+40, 144);
//int pakage_int = (int)strtol(&pakage, NULL, 16);
printf("Received pakage (int): %s\n", pakage);
/* case 'recieve_data':
case 'ack':
break;
*/
}
}
}
}