/** * 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() { while(1) { try { //Wait for Message from radio Message msg = radioRxQueue.pop(); assert(msg.getLength() <= MAX_TUN_BUF_SIZE); if (msg.getLength() > 0) { size_t writtenBytes = write(tunFd, msg.getPayload(), msg.getLength()); if(!writtenBytes){ writtenBytes = write(tunFd, msg.getPayload(), msg.getLength()); } if (writtenBytes != msg.getLength()) { std::cerr << "Tun: Less bytes written to tun/tap device then requested." << std::endl; } else { if (PRINT_DEBUG >= 1) { std::cout << "Tun: Successfully wrote " << writtenBytes << " bytes to tun device" << std::endl; } } if (PRINT_DEBUG >= 3) { printPayload(msg.getPayloadStr(),"tun write"); } } } catch(boost::thread_interrupted&) { std::cerr << "tunTxThreadFunction is stopped" << std::endl; return; } } }
/******************************************************************************* * @author : Rohan Jyoti * @name : printMembershipList * @param : Membership List to print, optional message to print * @return : void * @purpose : Print assoc. params of membership list ******************************************************************************/ void printMembershipList(queue_t *incomingML, char *opt_message) { printf("%s\n", opt_message); unsigned int i; for(i=0; i<queue_size(incomingML); i++) { mPayload_t *tPayload = (mPayload_t *)queue_at(incomingML, i); char message[64]; sprintf(message, "Payload[%d]---------->", i); printPayload(tPayload, message); } }
/** * 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() { while(1) { try { network.update(); //RX section while ( network.available() ) { // Is there anything ready for us? RF24NetworkHeader header; // If so, grab it and print it out Message msg; uint8_t buffer[MAX_PAYLOAD_SIZE]; unsigned int bytesRead = network.read(header,buffer,MAX_PAYLOAD_SIZE); if (bytesRead > 0) { msg.setPayload(buffer,bytesRead); if (PRINT_DEBUG >= 1) { std::cout << "Radio: Received "<< bytesRead << " bytes ... " << std::endl; } if (PRINT_DEBUG >= 3) { printPayload(msg.getPayloadStr(),"radio RX"); } radioRxQueue.push(msg); } else { std::cerr << "Radio: Error reading data from radio. Read '" << bytesRead << "' Bytes." << std::endl; } } //End RX network.update(); // TX section while(!radioTxQueue.empty() && !radio.available() ) { Message msg = radioTxQueue.pop(); if (PRINT_DEBUG >= 1) { std::cout << "Radio: Sending "<< msg.getLength() << " bytes ... "; } if (PRINT_DEBUG >= 3) { std::cout << std::endl; //PrintDebug == 1 does not have an endline. printPayload(msg.getPayloadStr(),"radio TX"); } uint8_t *tmp = msg.getPayload(); /*printf("********WRITING************\n"); for(int i=0; i<8; i++){ //std::cout << std::hex << buffer[i] <<std::endl; //printf("%#x\n",(uint8_t)buffer[i]); //uint32_t tmp2 = 0; //tmp2 |= (uint32_t)tmp; //printf("%01x\n",tmp2); printf("0%#x\n",tmp[i]); //tmp++; }*/ tmp = msg.getPayload(); uint32_t RF24_STR = 0x34324652; //Identifies the mac as an RF24 mac uint32_t ARP_BC = 0xFFFFFFFF; //Broadcast address struct macStruct{ uint16_t rf24_Addr; uint32_t rf24_Verification; }; //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? macStruct macData; //memcpy(&macData,tmp,sizeof(macData)); memcpy(&macData.rf24_Addr,tmp,2); memcpy(&macData.rf24_Verification,tmp+2,4); //const uint16_t other_node = otherNodeAddr; bool ok = 0; if(macData.rf24_Verification == RF24_STR){ const uint16_t other_node = macData.rf24_Addr; RF24NetworkHeader header(/*to node*/ other_node, EXTERNAL_DATA_TYPE); ok = network.write(header,msg.getPayload(),msg.getLength()); printf("*************W1\n"); }else if(macData.rf24_Verification == ARP_BC){ const uint16_t other_node = otherNodeAddr; RF24NetworkHeader header(/*to node*/ 00, EXTERNAL_DATA_TYPE); //Set to master node, will be modified by RF24Network if multi-casting if(thisNodeAddr == 00){ //Master Node ok = network.multicast(header,msg.getPayload(),msg.getLength(),1 ); //Send to Level 1 }else{ ok = network.write(header,msg.getPayload(),msg.getLength()); } printf("*****************W2\n"); } printf("Addr: 0%#x\n",macData.rf24_Addr); printf("Verif: 0%#x\n",macData.rf24_Verification); if (ok) { std::cout << "ok." << std::endl; } else { std::cerr << "failed." << std::endl; } } //End Tx } catch(boost::thread_interrupted&) { std::cerr << "radioRxThreadFunction is stopped" << std::endl; return; } } }