Skip to content

Commit 1eed4ea

Browse files
committed
Add auto-routing for RF24Ethernet
RF24Tun now handles ARP requests, and broacasts as required to find the IP of related devices, using RF24Network multicast - IP payloads are delivered according to MAC (RF24Network/RF24) address - This allows multiple RF24Ethernet nodes to be connected entirely using TCP/IP
1 parent 0343dca commit 1eed4ea

4 files changed

+71
-12
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

+60-8
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void radioRxTxThreadFunction() {
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) {
@@ -176,12 +176,59 @@ 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, EXTERNAL_DATA_TYPE);
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
}
@@ -203,7 +250,7 @@ void tunRxThreadFunction() {
203250

204251
fd_set socketSet;
205252
struct timeval selectTimeout;
206-
char buffer[MAX_TUN_BUF_SIZE];
253+
uint8_t buffer[MAX_TUN_BUF_SIZE];
207254
int nread;
208255

209256
while(1) {
@@ -226,8 +273,13 @@ void tunRxThreadFunction() {
226273
std::cout << "Tun: Successfully read " << nread << " bytes from tun device" << std::endl;
227274
}
228275
if (PRINT_DEBUG >= 3) {
229-
printPayload(std::string(buffer, nread),"Tun read");
276+
//printPayload(std::string(buffer, nread),"Tun read");
230277
}
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+
}*/
231283

232284
// copy received data into new Message
233285
Message msg;

rf24totun_configAndPing.sh

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

3939
INTERFACE="tun_nrf24"
40+
#MYIP="10.10.2.${1}/16"
41+
#PINGIP="10.10.2.${2}"
4042
MYIP="192.168.1.${1}/24"
4143
PINGIP="192.168.1.${2}"
4244
MTU=1500

0 commit comments

Comments
 (0)