-
Notifications
You must be signed in to change notification settings - Fork 2
/
vara.c
320 lines (257 loc) · 10 KB
/
vara.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* Rhizo-connector: A connector to different HF modems
* Copyright (C) 2018 Rhizomatica
* Author: Rafael Diniz <[email protected]>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*
* Vara support routines
*/
/**
* @file vara.c
* @author Rafael Diniz
* @date 12 Apr 2018
* @brief VARA modem support functions
*
* All the specific code for supporting VARA.
*
*/
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sched.h>
#include "common.h"
#include "net.h"
#include "spool.h"
#include "vara.h"
#include "serial.h"
void *vara_data_worker_thread_tx(void *conn)
{
rhizo_conn *connector = (rhizo_conn *) conn;
uint8_t *buffer;
uint32_t buf_size;
while(connector->tcp_ret_ok){
// check if we are connected, otherwise, wait
connector->safe_state++;
while (connector->connected == false || ring_buffer_count_bytes(&connector->in_buffer.buf) == 0){
if (connector->tcp_ret_ok == false){
connector->safe_state--;
goto exit_local;
}
sleep(1);
}
connector->safe_state--;
connector->timeout_counter = 0;
// read header
read_buffer(&connector->in_buffer, (uint8_t *) &buf_size, sizeof(buf_size)); // \todo if the two parties in a connection have different endianess, we are in trouble
buffer = (uint8_t *) malloc (buf_size);
memset(buffer, 0, buf_size);
read_buffer(&connector->in_buffer, buffer, buf_size);
connector->tcp_ret_ok &= tcp_write(connector->data_socket, (uint8_t *) &buf_size, sizeof(buf_size) );
connector->tcp_ret_ok &= tcp_write(connector->data_socket, buffer, buf_size);
free(buffer);
}
exit_local:
return EXIT_SUCCESS;
}
void *vara_data_worker_thread_rx(void *conn)
{
rhizo_conn *connector = (rhizo_conn *) conn;
uint8_t *buffer;
uint32_t buf_size;
while(connector->tcp_ret_ok){
connector->safe_state++;
while (connector->connected == false) {
if (connector->tcp_ret_ok == false){
connector->safe_state--;
goto exit_local;
}
sleep(1);
}
buf_size = 0;
connector->tcp_ret_ok &= tcp_read(connector->data_socket, (uint8_t *) &buf_size, sizeof(buf_size));
connector->safe_state--;
connector->timeout_counter = 0;
buffer = (uint8_t *) malloc (buf_size);
memset(buffer, 0, buf_size);
connector->tcp_ret_ok &= tcp_read(connector->data_socket, buffer, buf_size);
fprintf(stderr,"Message of size: %u received.\n", buf_size);
write_buffer(&connector->out_buffer, (uint8_t *) &buf_size, sizeof(buf_size));
write_buffer(&connector->out_buffer, buffer, buf_size);
connector->timeout_counter = 0;
free(buffer);
}
exit_local:
return EXIT_SUCCESS;
}
void *vara_control_worker_thread_rx(void *conn)
{
rhizo_conn *connector = (rhizo_conn *) conn;
uint8_t rcv_byte;
uint8_t buffer[1024];
int counter = 0;
bool new_cmd = false;
while(connector->tcp_ret_ok){
connector->tcp_ret_ok &= tcp_read(connector->control_socket, &rcv_byte, 1);
if (rcv_byte == '\r'){
buffer[counter] = 0;
counter = 0;
new_cmd = true;
}
else{
buffer[counter] = rcv_byte;
counter++;
new_cmd = false;
}
if (new_cmd){
if (!strcmp((char *) buffer, "DISCONNECTED")){
fprintf(stderr, "TNC: %s\n", buffer);
connector->connected = false;
connector->waiting_for_connection = false;
} else
// other commands here
if (!memcmp(buffer, "CONNECTED", strlen("CONNECTED"))){
fprintf(stderr, "TNC: %s\n", buffer);
connector->connected = true;
connector->waiting_for_connection = false;
} else
if (!memcmp(buffer, "BUFFER", strlen("BUFFER"))){
uint32_t buf_size;
sscanf( (char *) buffer, "BUFFER %u", &buf_size);
fprintf(stderr, "BUFFER: %u\n", buf_size);
if (buf_size != 0)
connector->timeout_counter = 0;
// our delete messages mechanism
if (buf_size == 0 &&
ring_buffer_count_bytes(&connector->in_buffer.buf) == 0 &&
connector->connected == true){
fprintf(stderr, "Messages successfully sent. Erasing messages...\n");
remove_all_msg_path_queue(connector);
}
} else
{
if (connector->serial_keying == true)
{
if (!memcmp(buffer, "PTT ON", strlen("PTT ON")))
{
key_on(connector->serial_fd, connector->radio_type);
}
if (!memcmp(buffer, "PTT OFF", strlen("PTT OFF"))){
key_off(connector->serial_fd, connector->radio_type);
}
}
fprintf(stderr, "%s\n", buffer);
}
}
}
return EXIT_SUCCESS;
}
void *vara_control_worker_thread_tx(void *conn)
{
rhizo_conn *connector = (rhizo_conn *) conn;
char buffer[1024];
// We set a call sign
memset(buffer,0,sizeof(buffer));
sprintf(buffer, "MYCALL %s\r", connector->call_sign);
connector->tcp_ret_ok &= tcp_write(connector->control_socket, (uint8_t *) buffer, strlen(buffer));
memset(buffer,0,sizeof(buffer));
strcpy(buffer,"LISTEN ON\r");
connector->tcp_ret_ok &= tcp_write(connector->control_socket, (uint8_t *) buffer, strlen(buffer));
memset(buffer,0,sizeof(buffer));
strcpy(buffer,"PUBLIC OFF\r");
connector->tcp_ret_ok &= tcp_write(connector->control_socket, (uint8_t *) buffer, strlen(buffer));
memset(buffer,0,sizeof(buffer));
strcpy(buffer,"BW2300\r");
connector->tcp_ret_ok &= tcp_write(connector->control_socket, (uint8_t *) buffer, strlen(buffer));
// 1Hz function
while(connector->tcp_ret_ok){
// Logic to start a connection
// condition for connection: no connection AND something to transmitt AND we did not issue a CONNECT recently
if (connector->connected == false &&
ring_buffer_count_bytes(&connector->in_buffer.buf) > 0 &&
!connector->waiting_for_connection){
// \todo try to add some entropy in order to avoid on air clashes
memset(buffer,0,sizeof(buffer));
sprintf(buffer,"CONNECT %s %s\r", connector->call_sign,
connector->remote_call_sign);
connector->tcp_ret_ok &= tcp_write(connector->control_socket, (uint8_t *)buffer, strlen(buffer));
fprintf(stderr, "CONNECTING... %s\n", buffer);
connector->waiting_for_connection = true;
}
// Logic to disconnect on timeout
if (connector->timeout_counter >= connector->timeout &&
connector->connected == true){
connector->connected = false;
while (connector->safe_state != 2){ // this means we have both data threads in the safe zone
sched_yield();
}
fprintf(stderr, "DISCONNECTING BY TIMEOUT...\n");
memset(buffer,0,sizeof(buffer));
sprintf(buffer,"DISCONNECT\r");
connector->tcp_ret_ok &= tcp_write(connector->control_socket, (uint8_t *)buffer, strlen(buffer));
}
sleep(1);
}
return EXIT_SUCCESS;
}
bool initialize_modem_vara(rhizo_conn *connector)
{
connector->tcp_ret_ok &= tcp_connect(connector->ip_address, connector->tcp_base_port, &connector->control_socket);
connector->tcp_ret_ok &= tcp_connect(connector->ip_address, connector->tcp_base_port+1, &connector->data_socket);
if (connector->tcp_ret_ok == false)
{
fprintf(stderr, "Connection to TNC failure.\n");
return false;
}
if (connector->serial_keying == true)
{
connector->serial_fd = open_serial_port(connector->serial_path);
if (connector->serial_fd == -1)
{
fprintf(stderr, "Could not open serial device.\n");
return false;
}
if (connector->radio_type == RADIO_TYPE_ICOM)
set_fixed_baudrate("19200", connector->serial_fd);
if (connector->radio_type == RADIO_TYPE_UBITX){
set_fixed_baudrate("38400", connector->serial_fd);
sleep(2); // ubitx workaround
}
}
// we start our control thread
pthread_t tid1;
pthread_create(&tid1, NULL, vara_control_worker_thread_rx, (void *) connector);
// we start our control tx thread
pthread_t tid2;
pthread_create(&tid2, NULL, vara_control_worker_thread_tx, (void *) connector);
pthread_t tid3;
pthread_create(&tid3, NULL, vara_data_worker_thread_tx, (void *) connector);
pthread_t tid4;
pthread_create(&tid4, NULL, vara_data_worker_thread_rx, (void *) connector);
pthread_t tid5;
pthread_create(&tid5, NULL, connection_timeout_thread, (void *) connector);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_join(tid3, NULL);
pthread_join(tid4, NULL);
pthread_join(tid5, NULL);
return true;
}