25 #include <zephyr/kernel.h>
26 #include <zephyr/logging/log.h>
27 #include <zephyr/net/net_pkt.h>
28 #include <zephyr/net/net_if.h>
29 #include <zephyr/net/net_core.h>
30 #include <zephyr/net/ethernet.h>
31 #include <zephyr/net/net_ip.h>
33IPAddress RF24EthernetClass::_dnsServerAddress;
36 #include "drivers/net/rf24_netif.h"
39extern "C" int rf24_cpp_tx_frame(
const uint8_t* data,
size_t len)
50uint8_t RF24EthernetClass::networkBuffer[MAX_PAYLOAD_SIZE];
51IPAddress RF24EthernetClass::_dnsServerAddress;
78pbuf* RF24EthernetClass::readRXQueue(EthQueue* RXQueue)
90 pbuf* p = pbuf_alloc(PBUF_IP, frameLen, PBUF_RAM);
93 if (pbuf_take(p, &
RXQueue->data[
RXQueue->nRead], frameLen) == ERR_OK) {
107 return (frame & 0x01) == 0;
114 void* context = netif->state;
115 uint16_t total_len = 0;
116 alignas(4)
char buf[
Ethernet.MAX_FRAME_SIZE];
118 if (p->tot_len >
sizeof(buf))
120 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
123 pbuf_copy_partial(p, buf, p->tot_len, 0);
124 LINK_STATS_INC(link.xmit);
125 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, p->tot_len);
127 if (p->tot_len <
Ethernet.MIN_FRAME_SIZE)
129 total_len =
Ethernet.MIN_FRAME_SIZE;
133 total_len = p->tot_len;
138 MIB2_STATS_NETIF_INC(netif, ifoutucastpkts);
142 MIB2_STATS_NETIF_INC(netif, ifoutnucastpkts);
145 IPAddress gwIP =
Ethernet.gatewayIP();
146 int16_t nodeAddress = 0;
149 if (
Ethernet.mesh.mesh_address != 0) {
150 if (gwIP[3] != buf[19]) {
151 IPAddress local_ip =
Ethernet.localIP();
152 if (local_ip[0] == buf[16] && local_ip[1] == buf[17]) {
154 nodeAddress =
Ethernet.mesh.getAddress((
char)buf[19]);
155 if (nodeAddress < 0) {
162 IPAddress local_ip =
Ethernet.localIP();
163 if (local_ip[0] == buf[16] && local_ip[1] == buf[17]) {
164 nodeAddress =
Ethernet.mesh.getAddress((
char)buf[19]);
165 if (nodeAddress < 0) {
171 IF_ETH_DEBUG_L1(Serial.print(
"Net: Out "); Serial.println(nodeAddress, OCT););
173 RF24NetworkHeader headerOut(nodeAddress, EXTERNAL_DATA_TYPE);
175 if (total_len && total_len <= MAX_PAYLOAD_SIZE) {
177 if (!
RF24Ethernet.network.write(headerOut, buf, total_len)) {
190 return netif->linkoutput(netif, p);
198 myNetif->name[0] =
'e';
199 myNetif->name[1] =
'0';
202 myNetif->mtu = MAX_PAYLOAD_SIZE;
203 myNetif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6 | NETIF_FLAG_LINK_UP;
204 myNetif->hostname =
"TmrNet";
205 MIB2_INIT_NETIF(&
Ethernet.myNetif, snmp_ifType_ppp,
Ethernet.NetIF_Speed_BPS);
207 myNetif->hwaddr_len = 0;
209 netif_set_link_up(myNetif);
216#if !defined NRF52_RADIO_LIBRARY
217 #if defined(RF24_TAP)
225 isInitialized =
false;
237 isInitialized =
false;
243 #if defined(RF24_TAP)
251 isInitialized =
false;
263 isInitialized =
false;
290 const uint8_t mac[6] = {0x52, 0x46, 0x32, 0x34, (uint8_t)address, (uint8_t)(address >> 8)};
301 RF24_Channel = RF24_Channel ? RF24_Channel : 97;
302 network.begin(RF24_Channel, address);
309 RF24_Channel = channel;
311 radio.setChannel(RF24_Channel);
319 IPAddress dns = {8, 8, 8, 8};
327 IPAddress gateway = ip;
329 begin(ip, dns, gateway);
336 IPAddress subnet(255, 255, 255, 0);
337 begin(ip, dns, gateway, subnet);
344 configure(ip, dns, gateway, subnet);
349void RF24EthernetClass::configure(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet)
351#if !defined(RF24_TAP)
352 mesh.setNodeID(ip[3]);
356 uip_buf = (uint8_t*)&
network.frag_ptr->message_buffer[0];
360 uip_sethostaddr(ipaddr);
362 uip_setdraddr(ipaddr);
364 uip_setnetmask(ipaddr);
365 _dnsServerAddress = dns;
369 #if defined(RF24_TAP)
370 timer_set(&this->arp_timer, CLOCK_SECOND * 2);
374 #if defined(RF24_TAP)
382 RF24Client::clientConnectionTimeout = 0;
383 RF24Client::serverConnectionTimeout = 30000;
385 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING && defined ESP32
387 esp_err_t err = esp_wifi_get_mode(&mode);
394 #elif defined RF24ETHERNET_CORE_REQUIRES_LOCKING
398 ip4_addr_t myIp, myMask, myGateway;
399 IP4_ADDR(&myIp, ip[0], ip[1], ip[2], ip[3]);
400 IP4_ADDR(&myMask, subnet[0], subnet[1], subnet[2], subnet[3]);
401 IP4_ADDR(&myGateway, gateway[0], gateway[1], gateway[2], gateway[3]);
402 _dnsServerAddress = dns;
404 void* context =
nullptr;
405 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
407 ETHERNET_APPLY_LOCK();
410 netif_add(&
Ethernet.myNetif, &myIp, &myMask, &myGateway, context,
netif_init, ip_input);
411 netif_set_default(&
Ethernet.myNetif);
413 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
415 ETHERNET_REMOVE_LOCK();
421 struct net_if* iface = rf24_netif_get_iface();
427 struct in_addr my_ip, netmask, gw;
429 my_ip.s_addr = (uint32_t)ip;
430 netmask.s_addr = (uint32_t)subnet;
431 gw.s_addr = (uint32_t)gateway;
437 net_if_ipv4_addr_add(iface, &my_ip, NET_ADDR_MANUAL, 0);
438 net_if_ipv4_set_netmask_by_addr(iface, &my_ip, &netmask);
439 net_if_ipv4_set_gw(iface, &gw);
441 int ret = net_if_up(iface);
442 if (ret < 0 && ret != -EALREADY) {
450 _dnsServerAddress = dns;
451 isInitialized =
true;
452 RF24Client::serverConnectionTimeout = 30000;
464 uip_setdraddr(ipaddr);
467 IP4_ADDR(&new_gw, gwIP[0], gwIP[1], gwIP[2], gwIP[3]);
468 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
470 ETHERNET_APPLY_LOCK();
473 netif_set_gw(&
Ethernet.myNetif, &new_gw);
474 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
476 ETHERNET_REMOVE_LOCK();
481 struct net_if* iface = rf24_netif_get_iface();
488 net_if_ipv4_set_gw(iface, &gate);
498 uip_listen(
HTONS(port));
501 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
503 ETHERNET_APPLY_LOCK();
506 RF24Client::myPcb = tcp_new();
509 err_t err = tcp_bind(RF24Client::myPcb, IP_ADDR_ANY, port);
518 RF24Client::myPcb = tcp_listen(RF24Client::myPcb);
521 tcp_accept(RF24Client::myPcb, RF24Client::accept);
523 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
525 ETHERNET_REMOVE_LOCK();
544 const ip4_addr_t* ip_addr = netif_ip4_addr(&
myNetif);
545 return (IPAddress(ip_addr->addr));
552 return IPAddress {0, 0, 0, 0};
566 const ip4_addr_t* ip_addr = netif_ip4_netmask(&
myNetif);
567 return (IPAddress(ip_addr->addr));
569 return IPAddress {0, 0, 0, 0};
572 struct net_if* iface = rf24_netif_get_iface();
575 return IPAddress {0, 0, 0, 0};
578 struct in_addr my_ip;
580 struct in_addr mask = net_if_ipv4_get_netmask_by_addr(iface, &my_ip);
597 const ip4_addr_t* ip_addr = netif_ip4_gw(&
myNetif);
598 return (IPAddress(ip_addr->addr));
600 return IPAddress {0, 0, 0, 0};
602 struct net_if* iface = rf24_netif_get_iface();
605 return IPAddress {0, 0, 0, 0};
608 return net_if_ipv4_get_gw(iface).s_addr;
617 return _dnsServerAddress;
623void RF24EthernetClass::EthRX_Handler(
const uint8_t* ethFrame,
const uint16_t lenEthFrame)
625 LINK_STATS_INC(link.recv);
626 MIB2_STATS_NETIF_ADD(&
Ethernet.myNetif, ifinoctets, lenEthFrame);
627 if (
Ethernet.isUnicast(ethFrame[0]))
629 MIB2_STATS_NETIF_INC(&
Ethernet.myNetif, ifinucastpkts);
633 MIB2_STATS_NETIF_INC(&
Ethernet.myNetif, ifinnucastpkts);
643void RF24EthernetClass::tick()
646#if defined __ZEPHYR__
648#elif defined ARDUINO_ARCH_ESP32
649 const TickType_t xDelay = pdMS_TO_TICKS(1);
651#elif defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_NRF52) || defined ARDUINO_ARCH_RP2350 || defined ARDUINO_NRF54L15
658 if (
Ethernet.mesh.mesh_address == 0) {
662 if (
result == EXTERNAL_DATA_TYPE) {
667 else if (
result == NETWORK_CORRUPTION) {
671 #if !defined(RF24_TAP)
678 else if (timer_expired(&
Ethernet.periodic_timer)) {
679 timer_reset(&
Ethernet.periodic_timer);
680 for (
int i = 0; i < UIP_CONNS; i++) {
692 if (
BUF->type == htons(UIP_ETHTYPE_IP)) {
703 else if (
BUF->type == htons(UIP_ETHTYPE_ARP)) {
713 else if (timer_expired(&
Ethernet.periodic_timer)) {
714 timer_reset(&
Ethernet.periodic_timer);
715 for (
int i = 0; i < UIP_CONNS; i++) {
727 for (
int i = 0; i < UIP_UDP_CONNS; i++) {
735 RF24UDP::_send((uip_udp_userdata_t*)(uip_udp_conns[i].appstate));
739 #if defined(RF24_TAP)
742 if (timer_expired(&
Ethernet.arp_timer)) {
753 if (
Ethernet.mesh.mesh_address == 0) {
757 if (
result == EXTERNAL_DATA_TYPE) {
761 Ethernet.EthRX_Handler(networkBuffer, len);
766 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
768 ETHERNET_APPLY_LOCK();
771 sys_check_timeouts();
773 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
775 ETHERNET_REMOVE_LOCK();
779 pbuf* p = readRXQueue(&
RXQueue);
783 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
785 ETHERNET_APPLY_LOCK();
790 LWIP_DEBUGF(NETIF_DEBUG, (
"IP input error\r\n"));
797 #if defined RF24ETHERNET_CORE_REQUIRES_LOCKING
799 ETHERNET_REMOVE_LOCK();
808 if (
Ethernet.mesh.mesh_address == 0) {
812 if (
result == EXTERNAL_DATA_TYPE) {
814 if (len == 0 || len > MAX_PAYLOAD_SIZE) {
826void RF24EthernetClass::network_send()
830 IPAddress gwIP =
Ethernet.gatewayIP();
831 int16_t nodeAddress = 0;
834 if (
Ethernet.mesh.mesh_address != 0) {
835 if (gwIP[3] != uip_buf[19]) {
836 IPAddress local_ip =
Ethernet.localIP();
837 if (local_ip[0] == uip_buf[16] && local_ip[1] == uip_buf[17]) {
839 nodeAddress =
Ethernet.mesh.getAddress((
char)uip_buf[19]);
840 if (nodeAddress < 0) {
847 IPAddress local_ip =
Ethernet.localIP();
848 if (local_ip[0] == uip_buf[16] && local_ip[1] == uip_buf[17]) {
849 nodeAddress =
Ethernet.mesh.getAddress((
char)uip_buf[19]);
850 if (nodeAddress < 0) {
855 RF24NetworkHeader headerOut(nodeAddress, EXTERNAL_DATA_TYPE);
857 #if defined ETH_DEBUG_L1 || defined ETH_DEBUG_L2
861 Serial.print(millis());
862 Serial.println(F(
" *** RF24Ethernet Network Write Fail ***"));
868 #if defined ETH_DEBUG_L2
871 Serial.print(millis());
872 Serial.println(F(
" RF24Ethernet Network Write OK"));
882int RF24EthernetClass::sendFrame(
const uint8_t* data,
size_t len)
886 if (!data || !len || !isInitialized) {
890 IPAddress gwIP =
Ethernet.gatewayIP();
892 int16_t nodeAddress = 0;
893 const uint8_t* buf = data;
895 if (
Ethernet.mesh.mesh_address != 0) {
896 if (gwIP[3] != buf[19]) {
897 IPAddress local_ip =
Ethernet.localIP();
898 if (local_ip[0] == buf[16] && local_ip[1] == buf[17]) {
900 nodeAddress =
Ethernet.mesh.getAddress((
char)buf[19]);
901 if (nodeAddress < 0) {
908 IPAddress local_ip =
Ethernet.localIP();
910 if (local_ip[0] == buf[16] && local_ip[1] == buf[17]) {
911 nodeAddress =
Ethernet.mesh.getAddress((
char)buf[19]);
912 if (nodeAddress < 0) {
918 memcpy(outputBuffer, data, len);
919 RF24NetworkHeader headerOut(nodeAddress, EXTERNAL_DATA_TYPE);
#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)
#define uip_ip_addr(addr, ip)
#define uip_seteth_addr(eaddr)
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]