-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
224 lines (190 loc) · 4.87 KB
/
main.cpp
File metadata and controls
224 lines (190 loc) · 4.87 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/timeb.h>
#include <time.h>
#include "tftp_client.h"
#include <iostream>
#include "net_com.h"
#include <curses.h>
#define PORT 7
#define MAXLINE 1024
#define BUFLEN 512 //Max length of buffer
using namespace std;
char nmea_string[256];
typedef struct
{
uint8_t id;
uint8_t data[8];
} net_data;
struct sensor_data
{
uint32_t counter;
uint8_t id;
uint32_t timestamp;
int32_t sensor1;
int32_t sensor2;
int32_t sensor3;
int32_t temp1;
int32_t temp2;
int32_t temp3;
};
void mysleep_ms(int milisec)
{
struct timespec res;
res.tv_sec = milisec/1000;
res.tv_nsec = (milisec*1000000) % 1000000000;
clock_nanosleep(CLOCK_MONOTONIC, 0, &res, NULL);
}
int getMilliCount(void)
{
timeb tb;
ftime(&tb);
int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
return nCount;
}
void readValues(Net_com* net)
{
struct sensor_data rx_data;
int temp_time;
int temp_timestamp;
initscr();
cbreak();
nodelay(stdscr, TRUE);
while(getch() <= 0)
{
int r = net->net_com_receive(&rx_data, sizeof(struct sensor_data));
if(r > 0)
{
cout << (getMilliCount() - temp_time) << "ms\t"<< "timestamp: " << (rx_data.timestamp - temp_timestamp) << "\tid:" << (int)rx_data.id << " p1:" << (int)rx_data.sensor1 << " p2:" << (int)rx_data.sensor2 << " p3:" << (int)rx_data.sensor3 << "\n\r";
temp_timestamp = rx_data.timestamp;
}
temp_time = getMilliCount();
}
endwin();
}
void diag_request(Net_com* diag)
{
net_data send_data, recv_data;
cout << "check Session" << endl;
send_data.id = 0x30;
diag->net_com_sendto(&send_data, sizeof(net_data));
diag->net_com_receive(&recv_data, sizeof(net_data));
cout << "Data: " << (int)recv_data.id << endl;
cout << "Data: " << (int)recv_data.data[0] << endl;
cout << "Session: " << (int)recv_data.data[0] << "\n";
}
void flash_program(Net_com* net)
{
int tftp_port = 69;
char * ip = "192.168.0.3";
char* source = "Application.bin";
char* destination = "Application.bin";
TFTPClient client(ip, tftp_port);
net_data send_data, recv_data;
// send_data.id = 0x40;
// cout << "Starting Bootloader\n";
// net->net_com_sendto(&send_data, sizeof(net_data));
cout << "check Session" << endl;
do
{
send_data.id = 0x30;
net->net_com_sendto(&send_data, sizeof(net_data));
net->net_com_receive(&recv_data, sizeof(net_data));
cout << "Data: " << (int)recv_data.id << endl;
cout << "Data: " << (int)recv_data.data[0] << endl;
}while(recv_data.id != 0x50);
cout << "Session: " << (int)recv_data.data[0] << "\n";
if(recv_data.data[0] == 0x02)
{
cout << "Starting Bootloader" << endl;
send_data.id = 0x40;
net->net_com_sendto(&send_data, sizeof(net_data));
cout << "wait for Bootloader\n";
usleep(16000000UL);
do
{
send_data.id = 0x30;
net->net_com_sendto(&send_data, sizeof(net_data));
net->net_com_receive(&recv_data, sizeof(net_data));
cout << "Data: " << (int)recv_data.id << endl;
cout << "Data: " << (int)recv_data.data[0] << endl;
}
while((recv_data.id != 0x50) || (recv_data.data[0] != 0x01));
}
send_data.id = 0x10;
cout << "Starting TFTP Server\n";
net->net_com_sendto(&send_data, sizeof(net_data));
net->net_com_receive(&recv_data, sizeof(net_data));
cout << "Data: " << (int)recv_data.id << endl;
cout << "Starting TFTP client\n";
if (client.connectToServer() != 1)
{
cout << "Error while connecting to server " << ip << endl;
}
else
{
cout << "Trying to send file " << source << " to " << ip << " " << destination << endl;
if (client.sendFile(source, destination))
{
cout << "File sent successfully" << endl;
send_data.id = 0x20;
cout << "starting program" << endl;
net->net_com_sendto(&send_data, sizeof(net_data));
}
else
{
cout << "Error has occured in file transfer\n";
}
}
}
int main(void)
{
struct sockaddr_in si_me, si_other, server;
clock_t start, stop;
int s, s2, i, slen = sizeof(si_other) , recv_len;
char buf[BUFLEN];
struct timespec time;
float value;
net_data send_data, recv_data;
char * ip = "192.168.0.2";
int port = 69;
char* source = "Application.bin";
char* destination = "Application.bin";
TFTPClient client(ip, port);
Net_com net(2000,"192.168.0.5","192.168.0.3");
Net_com diag(8,"192.168.0.5","192.168.0.3");
send_data.id = 0x10;
char input;
net.net_com_connect();
diag.net_com_connect();
while(true)
{
cout << "Choise:" << endl;
cout << "1: read values" << endl;
cout << "2: start bootloader" << endl;
cout << "3: get Session" << endl;
input = getchar();
switch(input)
{
case '1':
readValues(&net);
break;
case '2':
flash_program(&diag);
break;
case '3':
diag_request(&diag);
break;
default:
cout << "error!" << endl;
break;
}
}
return 0;
}