Skip to content

Commit 3d39692

Browse files
committed
Merge pull request #3 from TMRh20/master
Small changes to improve transfer rates. Set TAP as default device. RF24Ethernet routing
2 parents 1a5dbac + 1eed4ea commit 3d39692

5 files changed

+92
-31
lines changed

Message.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class Message {
6464
* Get the payload of the message as a char array
6565
* @return the payload as a c-string
6666
*/
67-
const char* getPayload() {
68-
const char* res = payload_.data();
67+
uint8_t* getPayload() {
68+
uint8_t* res = payload_.data();
6969
return res;
7070
};
7171

@@ -82,14 +82,14 @@ class Message {
8282
* @param buffer char array as the payload
8383
* @param bsize size of the buffer
8484
*/
85-
void setPayload(char * buffer, std::size_t bsize) {
85+
void setPayload(uint8_t * buffer, std::size_t bsize) {
8686
payload_.clear();
8787
payload_.assign(buffer, buffer + bsize);
8888
length_ = payload_.size();
8989
};
9090

9191
private:
92-
std::vector<char> payload_; /**< Internal vector data structure to save the payload*/
92+
std::vector<uint8_t> payload_; /**< Internal vector data structure to save the payload*/
9393
std::size_t length_; /**< Current length of the payload */
9494
uint8_t seqNo_; /**< Sequence number */
9595

ThreadSafeQueue.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ class ThreadSafeQueue {
3333
bool empty() {
3434
return q_.empty();
3535
}
36+
37+
size_t size() {
38+
return q_.size();
39+
}
40+
3641
};

rf24totun.cpp

+77-23
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ int configureAndSetUpTunDevice() {
5555
std::string tunTapDevice = "tun_nrf24";
5656
strcpy(tunName, tunTapDevice.c_str());
5757

58-
int flags = IFF_TUN | IFF_NO_PI | IFF_MULTI_QUEUE;
58+
//int flags = IFF_TUN | IFF_NO_PI | IFF_MULTI_QUEUE;
59+
int flags = IFF_TAP | IFF_NO_PI;// | IFF_MULTI_QUEUE;
5960
tunFd = allocateTunDevice(tunName, flags);
6061
if (tunFd >= 0) {
6162
std::cout << "Successfully attached to tun/tap device " << tunTapDevice << std::endl;
@@ -137,14 +138,13 @@ void radioRxTxThreadFunction() {
137138

138139
network.update();
139140

140-
/**
141-
* RX section
142-
*/
141+
//RX section
142+
143143
while ( network.available() ) { // Is there anything ready for us?
144144

145145
RF24NetworkHeader header; // If so, grab it and print it out
146146
Message msg;
147-
char buffer[MAX_PAYLOAD_SIZE];
147+
uint8_t buffer[MAX_PAYLOAD_SIZE];
148148

149149
unsigned int bytesRead = network.read(header,buffer,MAX_PAYLOAD_SIZE);
150150
if (bytesRead > 0) {
@@ -163,10 +163,10 @@ void radioRxTxThreadFunction() {
163163

164164
network.update();
165165

166-
/**
167-
* TX section
168-
*/
169-
if (!radioTxQueue.empty()) {
166+
167+
// TX section
168+
169+
while(!radioTxQueue.empty() && !radio.available() ) {
170170
Message msg = radioTxQueue.pop();
171171

172172
if (PRINT_DEBUG >= 1) {
@@ -176,19 +176,64 @@ void radioRxTxThreadFunction() {
176176
std::cout << std::endl; //PrintDebug == 1 does not have an endline.
177177
printPayload(msg.getPayloadStr(),"radio TX");
178178
}
179-
const uint16_t other_node = otherNodeAddr;
180-
RF24NetworkHeader header(/*to node*/ other_node);
181-
bool ok = network.write(header,msg.getPayload(),msg.getLength());
182-
179+
180+
uint8_t *tmp = msg.getPayload();
181+
/*printf("********WRITING************\n");
182+
for(int i=0; i<8; i++){
183+
//std::cout << std::hex << buffer[i] <<std::endl;
184+
//printf("%#x\n",(uint8_t)buffer[i]);
185+
//uint32_t tmp2 = 0;
186+
//tmp2 |= (uint32_t)tmp;
187+
//printf("%01x\n",tmp2);
188+
printf("0%#x\n",tmp[i]);
189+
//tmp++;
190+
}*/
191+
192+
tmp = msg.getPayload();
193+
194+
uint32_t RF24_STR = 0x34324652; //Identifies the mac as an RF24 mac
195+
uint32_t ARP_BC = 0xFFFFFFFF; //Broadcast address
196+
struct macStruct{
197+
uint16_t rf24_Addr;
198+
uint32_t rf24_Verification;
199+
};
200+
201+
//struct serialip_state *s = &(uip_conn->appstate);//Creates a pointer to the application state of the current connection, which can be used to terminate it?
202+
203+
macStruct macData;
204+
//memcpy(&macData,tmp,sizeof(macData));
205+
memcpy(&macData.rf24_Addr,tmp,2);
206+
memcpy(&macData.rf24_Verification,tmp+2,4);
207+
//const uint16_t other_node = otherNodeAddr;
208+
209+
bool ok = 0;
210+
if(macData.rf24_Verification == RF24_STR){
211+
const uint16_t other_node = macData.rf24_Addr;
212+
RF24NetworkHeader header(/*to node*/ other_node, EXTERNAL_DATA_TYPE);
213+
ok = network.write(header,msg.getPayload(),msg.getLength());
214+
printf("*************W1\n");
215+
}else
216+
if(macData.rf24_Verification == ARP_BC){
217+
const uint16_t other_node = otherNodeAddr;
218+
RF24NetworkHeader header(/*to node*/ 00, EXTERNAL_DATA_TYPE); //Set to master node, will be modified by RF24Network if multi-casting
219+
220+
if(thisNodeAddr == 00){ //Master Node
221+
ok = network.multicast(header,msg.getPayload(),msg.getLength(),1 ); //Send to Level 1
222+
}else{
223+
ok = network.write(header,msg.getPayload(),msg.getLength());
224+
}
225+
printf("*****************W2\n");
226+
}
227+
228+
printf("Addr: 0%#x\n",macData.rf24_Addr);
229+
printf("Verif: 0%#x\n",macData.rf24_Verification);
183230
if (ok) {
184-
//std::cout << "ok." << std::endl;
231+
std::cout << "ok." << std::endl;
185232
} else {
186233
std::cerr << "failed." << std::endl;
187234
}
188235
} //End Tx
189236

190-
delay(2);
191-
192237
} catch(boost::thread_interrupted&) {
193238
std::cerr << "radioRxThreadFunction is stopped" << std::endl;
194239
return;
@@ -205,7 +250,7 @@ void tunRxThreadFunction() {
205250

206251
fd_set socketSet;
207252
struct timeval selectTimeout;
208-
char buffer[MAX_TUN_BUF_SIZE];
253+
uint8_t buffer[MAX_TUN_BUF_SIZE];
209254
int nread;
210255

211256
while(1) {
@@ -228,15 +273,24 @@ void tunRxThreadFunction() {
228273
std::cout << "Tun: Successfully read " << nread << " bytes from tun device" << std::endl;
229274
}
230275
if (PRINT_DEBUG >= 3) {
231-
printPayload(std::string(buffer, nread),"Tun read");
276+
//printPayload(std::string(buffer, nread),"Tun read");
232277
}
278+
279+
/*for(int i=0; i<nread; i++){
280+
//std::cout << std::hex << buffer[i] <<std::endl;
281+
printf("%#x\n",(uint8_t)buffer[i]);
282+
}*/
233283

234284
// copy received data into new Message
235285
Message msg;
236286
msg.setPayload(buffer,nread);
237287

238288
// send downwards
239-
radioTxQueue.push(msg);
289+
if(radioTxQueue.size() < 3){
290+
radioTxQueue.push(msg);
291+
}else{
292+
//std::cout << "Tun Drop" << std::endl;
293+
}
240294

241295
} else
242296
std::cerr << "Tun: Error while reading from tun/tap interface." << std::endl;
@@ -266,7 +320,7 @@ void tunTxThreadFunction() {
266320
if (msg.getLength() > 0) {
267321

268322
size_t writtenBytes = write(tunFd, msg.getPayload(), msg.getLength());
269-
323+
if(!writtenBytes){ writtenBytes = write(tunFd, msg.getPayload(), msg.getLength()); }
270324
if (writtenBytes != msg.getLength()) {
271325
std::cerr << "Tun: Less bytes written to tun/tap device then requested." << std::endl;
272326
} else {
@@ -382,13 +436,13 @@ int main(int argc, char **argv) {
382436
std::cout << "Choose an address: Enter 0 for master, 1 for child, 2 for Master with 1 Arduino routing node (02), 3 for Child with Arduino routing node (CTRL+C to exit) \n>";
383437
std::getline(std::cin,input);
384438

385-
if(input.length() == 1) {
439+
if(input.length() > 0) {
386440
myChar = input[0];
387441
if(myChar == '0'){
388442
thisNodeAddr = 00;
389-
otherNodeAddr = 01;
443+
otherNodeAddr = 1;
390444
}else if(myChar == '1') {
391-
thisNodeAddr = 01;
445+
thisNodeAddr = 1;
392446
otherNodeAddr = 00;
393447
}else if(myChar == '2') {
394448
thisNodeAddr = 00;

rf24totun.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#include <RF24Network/RF24Network.h>
5959

6060

61-
#define PRINT_DEBUG 1
61+
#define PRINT_DEBUG 0
6262

6363
#ifndef IFF_MULTI_QUEUE
6464
#define IFF_MULTI_QUEUE 0x0100
@@ -81,7 +81,7 @@ RF24 radio(RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_24, BCM2835_SPI_SPEED_8MHZ);
8181
RF24Network network(radio);
8282
uint16_t thisNodeAddr; /**< Address of our node in Octal format (01,021, etc) */
8383
uint16_t otherNodeAddr; /**< Address of the other node */
84-
const uint8_t channel = 90;
84+
const uint8_t channel = 97;
8585
unsigned long packets_sent; /**< How many have we sent already */
8686

8787
/**

rf24totun_configAndPing.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ if [[ -z "${1##*[!0-9]*}" ]] || [[ -z "${2##*[!0-9]*}" ]]; then
3737
fi
3838

3939
INTERFACE="tun_nrf24"
40-
MYIP="10.0.0.${1}/24"
41-
PINGIP="10.0.0.${2}"
40+
#MYIP="10.10.2.${1}/16"
41+
#PINGIP="10.10.2.${2}"
42+
MYIP="192.168.1.${1}/24"
43+
PINGIP="192.168.1.${2}"
4244
MTU=1500
4345

4446
function setIP() {

0 commit comments

Comments
 (0)