27uint8_t RF24EthernetClass::networkBuffer[MAX_PAYLOAD_SIZE];
28IPAddress RF24EthernetClass::_dnsServerAddress;
55pbuf* RF24EthernetClass::readRXQueue(EthQueue* RXQueue)
67 pbuf* p = pbuf_alloc(PBUF_IP, frameLen, PBUF_RAM);
70 if (pbuf_take(p, &
RXQueue->data[
RXQueue->nRead], frameLen) == ERR_OK) {
84 return (frame & 0x01) == 0;
91 void* context = netif->state;
92 uint16_t total_len = 0;
93 alignas(4)
char buf[
Ethernet.MAX_FRAME_SIZE];
95 if (p->tot_len >
sizeof(buf))
97 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
100 pbuf_copy_partial(p, buf, p->tot_len, 0);
101 LINK_STATS_INC(link.xmit);
102 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
104 if (p->tot_len <
Ethernet.MIN_FRAME_SIZE)
106 total_len =
Ethernet.MIN_FRAME_SIZE;
110 total_len = p->tot_len;
115 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
119 MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
122 IPAddress gwIP =
Ethernet.gatewayIP();
123 int16_t nodeAddress = 0;
126 if (
Ethernet.mesh.mesh_address != 0) {
127 if (gwIP[3] != buf[19]) {
128 IPAddress local_ip =
Ethernet.localIP();
129 if (local_ip[0] == buf[16] && local_ip[1] == buf[17]) {
131 nodeAddress =
Ethernet.mesh.getAddress((
char)buf[19]);
132 if (nodeAddress < 0) {
139 IPAddress local_ip =
Ethernet.localIP();
140 if (local_ip[0] == buf[16] && local_ip[1] == buf[17]) {
141 nodeAddress =
Ethernet.mesh.getAddress((
char)buf[19]);
142 if (nodeAddress < 0) {
148 IF_ETH_DEBUG_L1(Serial.print(
"Net: Out "); Serial.println(nodeAddress, OCT););
150 RF24NetworkHeader headerOut(nodeAddress, EXTERNAL_DATA_TYPE);
152 if (total_len && total_len <= MAX_PAYLOAD_SIZE) {
153 if (!
RF24Ethernet.network.write(headerOut, buf, total_len)) {
166 return netif->linkoutput(netif, p);
174 myNetif->name[0] =
'e';
175 myNetif->name[1] =
'0';
178 myNetif->mtu = MAX_PAYLOAD_SIZE;
179 myNetif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 | NETIF_FLAG_LINK_UP;
180 myNetif->hostname =
"TmrNet";
181 MIB2_INIT_NETIF(&
Ethernet.myNetif, snmp_ifType_ppp,
Ethernet.NetIF_Speed_BPS);
183 myNetif->hwaddr_len = 0;
185 netif_set_link_up(myNetif);
192#if !defined NRF52_RADIO_LIBRARY
193 #if defined(RF24_TAP)
213 #if defined(RF24_TAP)
254 const uint8_t mac[6] = {0x52, 0x46, 0x32, 0x34, (uint8_t)address, (uint8_t)(address >> 8)};
258 uip_seteth_addr(mac);
265 RF24_Channel = RF24_Channel ? RF24_Channel : 97;
266 network.begin(RF24_Channel, address);
273 RF24_Channel = channel;
275 radio.setChannel(RF24_Channel);
283 IPAddress dns = {8, 8, 8, 8};
291 IPAddress gateway = ip;
293 begin(ip, dns, gateway);
300 IPAddress subnet(255, 255, 255, 0);
301 begin(ip, dns, gateway, subnet);
308 configure(ip, dns, gateway, subnet);
313void RF24EthernetClass::configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet)
315#if !defined(RF24_TAP)
316 mesh.setNodeID(ip[3]);
320 uip_buf = (uint8_t*)&
network.frag_ptr->message_buffer[0];
323 uip_ip_addr(ipaddr, ip);
324 uip_sethostaddr(ipaddr);
325 uip_ip_addr(ipaddr, gateway);
326 uip_setdraddr(ipaddr);
327 uip_ip_addr(ipaddr, subnet);
328 uip_setnetmask(ipaddr);
329 _dnsServerAddress = dns;
333 #if defined(RF24_TAP)
334 timer_set(&this->arp_timer, CLOCK_SECOND * 2);
338 #if defined(RF24_TAP)
346 RF24Client::clientConnectionTimeout = 0;
347 RF24Client::serverConnectionTimeout = 30000;
349 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING && defined ESP32
351 esp_err_t err = esp_wifi_get_mode(&mode);
358 #elif defined RF24ETHERNET_CORE_REQUIRES_LOCKING
362 ip4_addr_t myIp, myMask, myGateway;
363 IP4_ADDR(&myIp, ip[0], ip[1], ip[2], ip[3]);
364 IP4_ADDR(&myMask, subnet[0], subnet[1], subnet[2], subnet[3]);
365 IP4_ADDR(&myGateway, gateway[0], gateway[1], gateway[2], gateway[3]);
366 _dnsServerAddress = dns;
368 void* context =
nullptr;
369 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
371 ETHERNET_APPLY_LOCK();
374 netif_add(&
Ethernet.myNetif, &myIp, &myMask, &myGateway, context,
netif_init, ip_input);
375 netif_set_default(&
Ethernet.myNetif);
377 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
379 ETHERNET_REMOVE_LOCK();
392 uip_ip_addr(ipaddr, gwIP);
393 uip_setdraddr(ipaddr);
396 IP4_ADDR(&new_gw, gwIP[0], gwIP[1], gwIP[2], gwIP[3]);
397 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
399 ETHERNET_APPLY_LOCK();
402 netif_set_gw(&
Ethernet.myNetif, &new_gw);
403 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
405 ETHERNET_REMOVE_LOCK();
416 uip_listen(
HTONS(port));
419 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
421 ETHERNET_APPLY_LOCK();
424 RF24Client::myPcb = tcp_new();
427 err_t err = tcp_bind(RF24Client::myPcb, IP_ADDR_ANY, port);
437 RF24Client::myPcb = tcp_listen(RF24Client::myPcb);
440 tcp_accept(RF24Client::myPcb, RF24Client::accept);
442 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
444 ETHERNET_REMOVE_LOCK();
457 return ip_addr_uip(a);
461 const ip4_addr_t* ip_addr = netif_ip4_addr(&
myNetif);
462 return (IPAddress(ip_addr->addr));
464 return IPAddress {0, 0, 0, 0};
475 return ip_addr_uip(a);
479 const ip4_addr_t* ip_addr = netif_ip4_netmask(&
myNetif);
480 return (IPAddress(ip_addr->addr));
482 return IPAddress {0, 0, 0, 0};
493 return ip_addr_uip(a);
497 const ip4_addr_t* ip_addr = netif_ip4_gw(&
myNetif);
498 return (IPAddress(ip_addr->addr));
500 return IPAddress {0, 0, 0, 0};
508 return _dnsServerAddress;
514void RF24EthernetClass::EthRX_Handler(
const uint8_t* ethFrame,
const uint16_t lenEthFrame)
516 LINK_STATS_INC(link.recv);
517 MIB2_STATS_NETIF_ADD(&
Ethernet.myNetif, ifinoctets, lenEthFrame);
518 if (
Ethernet.isUnicast(ethFrame[0]))
520 MIB2_STATS_NETIF_INC(&
Ethernet.myNetif, ifinucastpkts);
524 MIB2_STATS_NETIF_INC(&
Ethernet.myNetif, ifinnucastpkts);
534void RF24EthernetClass::tick()
537#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_NRF52) || defined ARDUINO_ARCH_RP2350
539#elif defined(ARDUINO_ARCH_ESP32)
540 const TickType_t xDelay = pdMS_TO_TICKS(1);
547 if (
Ethernet.mesh.mesh_address == 0) {
551 if (
result == EXTERNAL_DATA_TYPE) {
556 else if (
result == NETWORK_CORRUPTION) {
560 #if !defined(RF24_TAP)
567 else if (timer_expired(&
Ethernet.periodic_timer)) {
568 timer_reset(&
Ethernet.periodic_timer);
569 for (
int i = 0; i < UIP_CONNS; i++) {
581 if (BUF->type == htons(UIP_ETHTYPE_IP)) {
592 else if (BUF->type == htons(UIP_ETHTYPE_ARP)) {
602 else if (timer_expired(&
Ethernet.periodic_timer)) {
603 timer_reset(&
Ethernet.periodic_timer);
604 for (
int i = 0; i < UIP_CONNS; i++) {
616 for (
int i = 0; i < UIP_UDP_CONNS; i++) {
624 RF24UDP::_send((uip_udp_userdata_t*)(uip_udp_conns[i].appstate));
628 #if defined(RF24_TAP)
631 if (timer_expired(&
Ethernet.arp_timer)) {
642 if (
Ethernet.mesh.mesh_address == 0) {
646 if (
result == EXTERNAL_DATA_TYPE) {
650 Ethernet.EthRX_Handler(networkBuffer, len);
655 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
657 ETHERNET_APPLY_LOCK();
660 sys_check_timeouts();
662 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
664 ETHERNET_REMOVE_LOCK();
668 pbuf* p = readRXQueue(&
RXQueue);
672 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
674 ETHERNET_APPLY_LOCK();
679 LWIP_DEBUGF(NETIF_DEBUG, (
"IP input error\r\n"));
686 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
688 ETHERNET_REMOVE_LOCK();
698void RF24EthernetClass::network_send()
702 IPAddress gwIP =
Ethernet.gatewayIP();
703 int16_t nodeAddress = 0;
706 if (
Ethernet.mesh.mesh_address != 0) {
707 if (gwIP[3] != uip_buf[19]) {
708 IPAddress local_ip =
Ethernet.localIP();
709 if (local_ip[0] == uip_buf[16] && local_ip[1] == uip_buf[17]) {
711 nodeAddress =
Ethernet.mesh.getAddress((
char)uip_buf[19]);
712 if (nodeAddress < 0) {
719 IPAddress local_ip =
Ethernet.localIP();
720 if (local_ip[0] == uip_buf[16] && local_ip[1] == uip_buf[17]) {
721 nodeAddress =
Ethernet.mesh.getAddress((
char)uip_buf[19]);
722 if (nodeAddress < 0) {
727 RF24NetworkHeader headerOut(nodeAddress, EXTERNAL_DATA_TYPE);
729 #if defined ETH_DEBUG_L1 || defined ETH_DEBUG_L2
733 Serial.print(millis());
734 Serial.println(F(
" *** RF24Ethernet Network Write Fail ***"));
740 #if defined ETH_DEBUG_L2
743 Serial.print(millis());
744 Serial.println(F(
" RF24Ethernet Network Write OK"));
#define INCOMING_DATA_SIZE
err_t tun_netif_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
err_t netif_init(struct netif *myNetif)
err_t netif_output(struct netif *netif, struct pbuf *p)
RF24EthernetClass RF24Ethernet
static void error_callback(void *arg, err_t err)
static ConnectState * gState[2]
uint32_t networkCorruption
void setMac(uint16_t address)
void setChannel(uint8_t channel)
static void writeRXQueue(EthQueue *RXQueue, const uint8_t *ethFrame, uint16_t lenEthFrame)
static void initRXQueue(EthQueue *RXQueue)
void listen(uint16_t port)
void set_gateway(IPAddress gwIP)
static constexpr unsigned MAX_FRAME_SIZE
static bool isUnicast(const uint8_t frame)
static constexpr unsigned MAX_RX_QUEUE
void begin(IP_ADDR myIP, IP_ADDR subnet)
static bool useCoreLocking
#define IF_RF24ETHERNET_DEBUG_CLIENT(x)
#define IF_ETH_DEBUG_L1(x)
#define UIP_TIMER_DIVISOR
Adjust the rate at which the IP stack performs periodic processing.
uint16_t len[MAX_RX_QUEUE]