-
Notifications
You must be signed in to change notification settings - Fork 9
/
rf24totun.h
188 lines (164 loc) · 5.75 KB
/
rf24totun.h
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
/*
* The MIT License (MIT)
* Copyright (c) 2014 Rei <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* The TUN/TAP interface code is based code from
* http://backreference.org/2010/03/26/tuntap-interface-tutorial/
*
*/
#ifndef __RF24TOTUN_H__
#define __RF24TOTUN_H__
/**
*
* @file RF24toTUN.h
*
*/
#include <cstdlib>
#include <stdio.h>
#include <cstdint>
#include <string>
#include <cstring>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/scoped_ptr.hpp>
#include "ThreadSafeQueue.h"
#include "Message.h"
#include <RF24/RF24.h>
#include <RF24Network/RF24Network.h>
#define PRINT_DEBUG 0
#ifndef IFF_MULTI_QUEUE
#define IFF_MULTI_QUEUE 0x0100
#endif
#define MAX_TUN_BUF_SIZE (10 * 1024) // should be enough for now
#ifndef MAX_FRAME_SIZE
#define MAX_FRAME_SIZE 32 /**<The NRF24L01 frames are only 32Bytes long */
#endif
#ifndef MAX_PAYLOAD_SIZE
#define MAX_PAYLOAD_SIZE 1500 /**<The maximal payload size to be sent over the air. */
#endif
/**
* Radio configuration settings
*/
// CE Pin, CSN Pin, SPI Speed
RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
RF24Network network(radio);
uint16_t thisNodeAddr; /**< Address of our node in Octal format (01,021, etc) */
uint16_t otherNodeAddr; /**< Address of the other node */
const uint8_t channel = 97;
unsigned long packets_sent; /**< How many have we sent already */
/**
* Thread stuff
*/
ThreadSafeQueue< Message > radioRxQueue;
ThreadSafeQueue< Message > radioTxQueue;
boost::scoped_ptr< boost::thread > radioRxTxThread;
boost::scoped_ptr< boost::thread > tunRxThread;
boost::scoped_ptr< boost::thread > tunTxThread;
/**
* TUN/TAP variabled
*/
char tunName[IFNAMSIZ];
int tunFd;
/**
* Configuration and setup of the NRF24 radio
*
* @return True if the radio was configured successfully
*/
bool configureAndSetUpRadio();
/**
* Configure, set up and allocate the TUN/TAP device.
*
* If the TUN/TAP device was allocated successfully the file descriptor is returned.
* Otherwise the application closes with an error.
* @note Currently only a TUN device with multi queue is used.
*
* @return The TUN/TAP device file descriptor
*/
int configureAndSetUpTunDevice();
/**
* Allocate the TUN/TAP device (if needed)
*
* The TUN/TAP interface code is based code from
* http://backreference.org/2010/03/26/tuntap-interface-tutorial/
*
* @param *dev the name of an interface (or '\0'). MUST have enough space to hold the interface name if '\0' is passed
* @param flags interface flags (eg, IFF_TUN etc.)
* @return The file descriptor for the allocated interface or -1 is an error ocurred
*/
int allocateTunDevice(char *dev, int flags);
/**
* The thread function in charge receiving and transmitting messages with the radio.
* The received messages from RF24Network and NRF24L01 device and enqueued in the rxQueue and forwaded to the TUN/TAP device.
* The messages from the TUN/TAP device (in the txQueue) are sent to the RF24Network lib and transmited over the air.
*
* @note Optimization: Use two thread for rx and tx with the radio, but thread synchronisation and semaphores are needed.
* It may increase the throughput.
*/
void radioRxTxThreadFunction();
/**
* Thread function in charge of reading, framing and enqueuing the packets from the TUN/TAP interface in the RxQueue.
*
* This thread uses "select()" with timeout to avoid a busy waiting
*/
void tunRxThreadFunction();
/**
* This thread function waits for incoming messages from the radio and forwards them to the TUN/TAP interface.
*
* This threads blocks until a message is received avoiding busy waiting.
*/
void tunTxThreadFunction();
/**
* Projecure to wait and join all the threads.
*
* @note If an interrupt was not before called, this procedure may block for ever.
*/
void joinThreads();
/**
* This procedure is called before terminating the programm to properly close and terminate all the threads and file handlers.
*
*/
void on_exit();
/**
* Debug output of the given message buffer preceeded by the debugMsg.
*
* @param buffer The std::string with the message to debug.
* @param debugMsg The info message preceeding the buffer.
*/
void printPayload(std::string buffer, std::string debugMsg);
/**
* Debug output of the given message buffer preceeded by the debugMsg.
*
* @param buffer The char array with the message to debug.
* @param nread The length of the message buffer.
* @param debugMsg The info message preceeding the buffer.
*/
void printPayload(char *buffer, int nread, std::string debugMsg);
#endif // __RF24TOTUN_H__