forked from Othernet-Project/Dreamcatcher-Packet-Tester
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
584 lines (478 loc) · 15.2 KB
/
main.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* Copyright 2018 Fausto Annicchiarico Petruzzelli <[email protected]>
* Copyright 2018 Luigi Zevola <[email protected]>
*
* This file is part of DreamcatcherChat.
*
* This program 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 2, or (at your option)
* any later version.
*
* This program 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 program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "mbed.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "rffc5071.h"
#include "rffc5071_spi.h"
#include "radio.h"
#include "sx1280-hal.h"
#include <pthread.h>
#include <poll.h>
#define MODE_LORA
/*!
* \brief Defines the nominal frequency
*/
#define RF_FREQUENCY 2400000000UL // Hz
/*!
* \brief Defines the output power in dBm
*
* \remark The range of the output power is [-18..+13] dBm
*/
#define TX_OUTPUT_POWER 13
/*!
* \brief Defines the states of the application
*/
typedef enum
{
APP_LOWPOWER,
APP_RX,
APP_RX_TIMEOUT,
APP_RX_ERROR,
APP_TX,
APP_TX_TIMEOUT,
}AppStates_t;
/*!
* \brief Defines the buffer size, i.e. the payload size
*/
#define BUFFER_SIZE 255
#define TOKENSIZE 1
#define HEADER_SIZE TOKENSIZE+2
/*!
* \brief Define the possible message type for this application
*/
const uint8_t MasterToken[] = "M";
const uint8_t SlaveToken[] = "S";
/*!
* \brief The size of the buffer
*/
uint8_t BufferSize = BUFFER_SIZE;
/*!
* \brief The buffer
*/
uint8_t Buffer[BUFFER_SIZE];
/*!
* \brief The State of the application
*/
AppStates_t AppState = APP_LOWPOWER;
int8_t RssiValue = 0;
int8_t SnrValue = 0;
/*!
* \brief Function to be executed on Radio Tx Done event
*/
void OnTxDone( void );
/*!
* \brief Function to be executed on Radio Rx Done event
*/
void OnRxDone( void );
/*!
* \brief Function executed on Radio Tx Timeout event
*/
void OnTxTimeout( void );
/*!
* \brief Function executed on Radio Rx Timeout event
*/
void OnRxTimeout( void );
/*!
* \brief Function executed on Radio Rx Error event
*/
void OnRxError( IrqErrorCode_t );
/*!
* \brief All the callbacks are stored in a structure
*/
RadioCallbacks_t callbacks =
{
&OnTxDone, // txDone
&OnRxDone, // rxDone
NULL, // syncWordDone
NULL, // headerDone
&OnTxTimeout, // txTimeout
&OnRxTimeout, // rxTimeout
&OnRxError, // rxError
NULL, // rangingDone
NULL, // cadDone
};
// mosi, miso, sclk, nss, busy, dio1, dio2, dio3, rst, callbacks...
SX1280Hal Radio( 999, 999, 999, 999, 36, 35, 999, 999,115, &callbacks );
DigitalOut ANT_SW( 999 );
DigitalOut TxLed( 101 );
DigitalOut RxLed( 999 );
DigitalOut DirectLed( 116 );
/*!
* \brief Define IO for Unused Pin
*/
DigitalOut F_CS( 999 ); // MBED description of pin
DigitalOut SD_CS( 999 ); // MBED description of pin
/*!
* \brief Number of tick size steps for tx timeout
*/
#define TX_TIMEOUT_VALUE 1000 // ms
/*!
* \brief Number of tick size steps for rx timeout
*/
#define RX_TIMEOUT_VALUE 1000 // ms
/*!
* \brief Size of ticks (used for Tx and Rx timeout)
*/
#define RX_TIMEOUT_TICK_SIZE RADIO_TICK_SIZE_1000_US
/*!
* \brief Mask of IRQs to listen to in rx mode
*/
uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT;
/*!
* \brief Mask of IRQs to listen to in tx mode
*/
uint16_t TxIrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT;
/*!
* \brief Locals parameters and status for radio API
* NEED TO BE OPTIMIZED, COPY OF STUCTURE ALREADY EXISTING
*/
PacketParams_t PacketParams;
PacketStatus_t PacketStatus;
gpio_t_rffc gpio_rffc5072_select;
gpio_t_rffc gpio_rffc5072_clock;
gpio_t_rffc gpio_rffc5072_data;
gpio_t_rffc gpio_rffc5072_reset;
rffc5071_spi_config_t rffc5071_spi_config;/* = {
.gpio_select = gpio_rffc5072_select,
.gpio_clock = gpio_rffc5072_clock,
.gpio_data = gpio_rffc5072_data,
};*/
spi_bus_t spi_bus_rffc5071
{
.obj=NULL,
.config = &rffc5071_spi_config,
.start = rffc5071_spi_start,
.stop = rffc5071_spi_stop,
.transfer = rffc5071_spi_transfer,
.transfer_gather = rffc5071_spi_transfer_gather,
};
rffc5071_driver_t mixer= {
.bus = &spi_bus_rffc5071,
.gpio_reset = gpio_rffc5072_reset,
};
uint8_t broadcast_msg[BUFFER_SIZE-HEADER_SIZE];
uint8_t bcast_msg_len = 0;
uint16_t bcast_frame_no = 0;
uint8_t my_msg[BUFFER_SIZE-HEADER_SIZE];
uint8_t my_msg_len = 0;
struct threadargs
{
int fd;
};
int pipefd_term[2];
int pipefd_radio[2];
void broadcast_message()
{
#ifdef DEBUG
printf( "Sending message to slaves (frame %hu)\r\n", bcast_frame_no);
#endif
int ret = read(pipefd_radio[0], broadcast_msg, BUFFER_SIZE-HEADER_SIZE);
if (ret != -1)
{
bcast_frame_no = bcast_frame_no + 1;
bcast_msg_len = ret;
broadcast_msg[bcast_msg_len-1] = '\0';
#ifdef DEBUG_COM
printf("%d,%d:%s\r\n",ret,strlen((char*)broadcast_msg),broadcast_msg);
#endif
}
TxLed = 1;
memcpy( Buffer, MasterToken, TOKENSIZE );
memcpy( Buffer+TOKENSIZE, &bcast_frame_no, 2);
memcpy( Buffer+HEADER_SIZE, broadcast_msg, bcast_msg_len);
Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
Radio.SendPayload( Buffer, HEADER_SIZE+bcast_msg_len, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
}
void send_message()
{
// A master already exists then become a slave
#ifdef DEBUG
printf( "Sending message to master\r\n" );
#endif
int ret = read(pipefd_radio[0], my_msg, BUFFER_SIZE-HEADER_SIZE);
if (ret != -1)
{
bcast_frame_no = bcast_frame_no + 1;
my_msg_len = ret;
my_msg[my_msg_len-1] = '\0';
#ifdef DEBUG_COM
printf("%d,%d:%s\r\n",ret,strlen((char*)my_msg),my_msg);
#endif
}
TxLed = 1;
memcpy( Buffer, SlaveToken, TOKENSIZE );
memcpy( Buffer+TOKENSIZE , &bcast_frame_no, 2);
memcpy( Buffer+HEADER_SIZE, my_msg, my_msg_len);
Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
Radio.SendPayload( Buffer, HEADER_SIZE+my_msg_len, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
}
void receive_message()
{
TxLed = 0;
Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
}
void * consumer(void *p)
{
int fd = reinterpret_cast<struct threadargs *>(p)->fd;
fcntl(fd, F_SETFL, O_NONBLOCK);
uint8_t buffer[BUFFER_SIZE-HEADER_SIZE];
for (;;) {
if (read(fd, buffer, BUFFER_SIZE-HEADER_SIZE)!=-1)
{
printf(">%s\r\n",buffer);
}
}
}
void * producer(void *p)
{
int fd = reinterpret_cast<struct threadargs *>(p)->fd;
char buffer[BUFFER_SIZE-HEADER_SIZE];
for (;;) {
fgets(buffer, BUFFER_SIZE-HEADER_SIZE, stdin);
write(fd, (void *)buffer, strlen(buffer));
}
}
void process_message(uint16_t frame_no)
{
int recv_str_len = strlen((char *)(Buffer+HEADER_SIZE));
#ifdef DEBUG_COM
printf("String length is %d, Recv buffer size is %d",recv_str_len,BufferSize);
#endif
write(pipefd_term[1], Buffer+HEADER_SIZE, recv_str_len+1);
bcast_frame_no = frame_no;
}
int main()
{
bool isMaster = true;
ModulationParams_t modulationParams;
uint64_t mixer_freq = 0;
pthread_t p,c;
struct threadargs p_args,c_args;
printf( "\n\n\r Dreamcatcher Chat (v.%s)\n\n\r", "0.0 alpha" );
DirectLed = 1;
gpio_rffc5072_select=(gpio_t_rffc)malloc(sizeof(struct gpio_rffc));
if (gpio_rffc5072_select==NULL)
printf("Pointer is NULL\n");
gpio_rffc5072_select->pin=135;
gpio_rffc5072_clock=(gpio_t_rffc)malloc(sizeof(struct gpio_rffc));
if (gpio_rffc5072_clock==NULL)
printf("Pointer is NULL\n");
gpio_rffc5072_clock->pin=134;
gpio_rffc5072_data=(gpio_t_rffc)malloc(sizeof(struct gpio_rffc));
if (gpio_rffc5072_data==NULL)
printf("Pointer is NULL\n");
gpio_rffc5072_data->pin=133;
gpio_rffc5072_reset=(gpio_t_rffc)malloc(sizeof(struct gpio_rffc));
if (gpio_rffc5072_reset==NULL)
printf("Pointer is NULL\n");
gpio_rffc5072_reset->pin=136;
rffc5071_spi_config.gpio_select=gpio_rffc5072_select;
rffc5071_spi_config.gpio_clock= gpio_rffc5072_clock;
rffc5071_spi_config.gpio_data= gpio_rffc5072_data;
mixer.gpio_reset=gpio_rffc5072_reset;
spi_bus_start(&spi_bus_rffc5071, &rffc5071_spi_config);
rffc5071_setup(&mixer);
// wait for on board DC/DC start-up time
usleep( 500000 );
Radio.Init( );
Radio.SetRegulatorMode( USE_DCDC );
memset( &Buffer, 0x00, BufferSize );
modulationParams.PacketType = PACKET_TYPE_LORA;
modulationParams.Params.LoRa.SpreadingFactor = LORA_SF12;
modulationParams.Params.LoRa.Bandwidth = LORA_BW_0400;
modulationParams.Params.LoRa.CodingRate = LORA_CR_4_5;
PacketParams.PacketType = PACKET_TYPE_LORA;
PacketParams.Params.LoRa.PreambleLength = 0x08;
PacketParams.Params.LoRa.HeaderType = LORA_PACKET_VARIABLE_LENGTH;
PacketParams.Params.LoRa.PayloadLength = 50;
PacketParams.Params.LoRa.Crc = LORA_CRC_ON;
PacketParams.Params.LoRa.InvertIQ = LORA_IQ_INVERTED;
while (!mixer_freq)
{
printf("Select MIXER frequency (Hz):");
if ((scanf("%llu",&mixer_freq)==1)&&(mixer_freq>=40000000llu)&&(mixer_freq<=6000000000llu))
{
printf("Selected mixer frequency: %f MHz\r\n",((double)mixer_freq)/1000000.0);
rffc5071_set_frequency(&mixer, mixer_freq);
}else{
printf("Invalid choice. Range is 40 MHz to 6 GHz\r\n");
mixer_freq=0;
}
}
if (pipe(pipefd_radio) != 0) {
perror("pipe");
exit(1);
}
if (pipe(pipefd_term) != 0) {
perror("pipe");
exit(1);
}
fcntl(pipefd_radio[0], F_SETFL, O_NONBLOCK);
c_args.fd = pipefd_term[0];
p_args.fd = pipefd_radio[1];
pthread_create(&p,NULL,&producer,(void *) &p_args);
pthread_create(&c,NULL,&consumer,(void *) &c_args);
Radio.SetStandby( STDBY_RC );
Radio.SetPacketType( modulationParams.PacketType );
Radio.SetModulationParams( &modulationParams );
Radio.SetPacketParams( &PacketParams );
Radio.SetRfFrequency( RF_FREQUENCY );
Radio.SetBufferBaseAddresses( 0x00, 0x00 );
Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_20_US );
receive_message();
AppState = APP_LOWPOWER;
while(1)
{
switch( AppState )
{
case APP_RX:
AppState = APP_LOWPOWER;
Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE );
if ((isMaster)&&(BufferSize>0)){
if( strncmp( (const char*)Buffer, (const char*)SlaveToken, TOKENSIZE ) == 0 )
{
uint16_t frame_no;
memcpy(&frame_no,Buffer+TOKENSIZE,2);
if (frame_no > bcast_frame_no)
{
// A new message has been received from slave.
process_message(frame_no);
}
broadcast_message();
} else if( strncmp( ( const char* )Buffer, ( const char* )MasterToken, TOKENSIZE ) == 0 ) {
// Found another master, becoming slave
isMaster = false;
uint16_t frame_no;
memcpy(&frame_no,Buffer+TOKENSIZE,2);
// A new message has been received. Synchronize frame number
process_message(frame_no);
send_message();
} else { // Set device as master ans start again
#ifdef DEBUG_COM
for (int i =0;i<BufferSize;i++)
{
printf("%02x ",Buffer[i]);
}
printf( "...no valid token received (%d)\r\n",BufferSize );
#endif
isMaster = true;
receive_message();
}
}else if ((!isMaster)&&(BufferSize>0)){
if( strncmp( ( const char* )Buffer, ( const char* )MasterToken, TOKENSIZE ) == 0 )
{
#ifdef DEBUG
printf( "Received message from Master\r\n" );
#endif
uint16_t frame_no;
memcpy(&frame_no,Buffer+TOKENSIZE,2);
if (frame_no > bcast_frame_no)
{
// A new message has been received from master.
process_message(frame_no);
}
send_message();
}
else // valid reception but not a PING as expected
{
#ifdef DEBUG
printf( "waiting for new message\r\n" );
#endif
isMaster = true;
receive_message();
}
}
break;
case APP_TX:
AppState = APP_LOWPOWER;
receive_message();
break;
case APP_RX_TIMEOUT:
AppState = APP_LOWPOWER;
if( isMaster == true )
{
// Send the message again
broadcast_message();
}else{
receive_message();
}
break;
case APP_RX_ERROR:
AppState = APP_LOWPOWER;
// We have received a Packet with a CRC error, send reply as if packet was correct
if(isMaster)
broadcast_message();
else
send_message();
break;
case APP_TX_TIMEOUT:
AppState = APP_LOWPOWER;
receive_message();
break;
case APP_LOWPOWER:
break;
default:
// Set low power
AppState = APP_LOWPOWER;
break;
}
}
}
void OnTxDone( void )
{
AppState = APP_TX;
}
void OnRxDone( void )
{
AppState = APP_RX;
#ifdef PRINT_RSSI_ON_RX
Radio.GetPacketStatus(&packetStatus);
RssiValue = packetStatus.LoRa.RssiPkt;
SnrValue = packetStatus.LoRa.SnrPkt;
printf("rssi: %d; snr: %d\n\r", RssiValue, SnrValue );
#endif
}
void OnTxTimeout( void )
{
AppState = APP_TX_TIMEOUT;
printf( "Transmission Timeout\r\n" );
}
void OnRxTimeout( void )
{
AppState = APP_RX_TIMEOUT;
//printf( "No Response\r\n" );
}
void OnRxError( IrqErrorCode_t errorCode )
{
AppState = APP_RX_ERROR;
printf( "RX packet with CRC error\r\n" );
}
void OnRangingDone( IrqRangingCode_t val )
{
}
void OnCadDone( bool channelActivityDetected )
{
}