This is an automated email from the ASF dual-hosted git repository. jerpelea pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 24221b611f06cbf79de4e0d0dea3899915418542 Author: Laczen JMS <laczen...@gmail.com> AuthorDate: Wed Mar 5 10:17:06 2025 +0100 arch/xtensa/esp32:espnow pktradio simplify addressing Modify addressing to allow only 2 byte node address. Allow modifying the node address by modifying the ipv6 address (the ipv6 address has a direct relation with the node address) Introduce the option to add a 4 byte random number to the mac header as a preparation for ciphered data exchange. Introduce a FCB (frame control byte) as a replacement for the INFO field in the mac header. Update esp32-devkitc:espnow config to reflect address size change. Update the documentation to reflect address size changes. Signed-off-by: Laczen JMS <laczen...@gmail.com> --- .../xtensa/esp32/boards/esp32-devkitc/index.rst | 85 +++++- arch/xtensa/src/esp32/Kconfig | 98 ++----- arch/xtensa/src/esp32/esp32_espnow_pktradio.c | 300 ++++++++------------- .../esp32/esp32-devkitc/configs/espnow/defconfig | 15 +- 4 files changed, 203 insertions(+), 295 deletions(-) diff --git a/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst b/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst index ec022f1500..e239c4426d 100644 --- a/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst +++ b/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst @@ -292,7 +292,7 @@ You can run the game by using ``brick`` command:: nsh> brick Here is the sample wiring diagram that demonstrates how to wire ws2812 with buttons for brickmatch example: - + .. figure:: esp32-brickmatch-game-schematic.jpg :align: center @@ -404,26 +404,83 @@ It can be tested by executing the ``elf`` application. espnow ------ +WARNING: espnow and wifi are using the same hardware on the esp32. When a +connection to a accespoint is made while espnow is operational the espnow +connection will break if the accesspoint wants to use a different wifi +channel. + A ``espnow`` setup can be used to create a 6lowpan network of esp32 nodes. A sample configuration is found in ``esp32-devkitc:espnow``. The node -address can be changed under ``ESP32 Peripherals`` option ``Espnow``. To -test the communication using ``udpserver`` and ``udpclient`` two nodes -need to be prepared using e.g. node address ``0x0a`` and ``0x0b``. +address can be changed under ``ESP32 Peripherals`` option ``Espnow``. The +node address is direct related to the ipv6 address of the node. Changing +the ipv6 address also changes the node address. + +To test the communication using ``udpserver`` and ``udpclient`` two nodes +need to be prepared with different ipv6 address. + +The server node is assigned the node address ``0x000a`` and the udp server +is started using: + +.. code-block :: bash + + nsh> ifconfig wpan0 inet6 fe80::ff:fe00:a + nsh> ifup wpan0 + ifup wpan0..OK + nsh> udpserver & + udpserver [6:100] + +The client node can use the default node address (``0xfffe``) and the +updclient can be started using: + +.. code-block :: bash + + nsh> ifup wpan0 + ifup wpan0..OK + nsh> udpclient fe80::ff:fe00:a + client: 0. Sending 96 bytes + client: 0. Sent 96 bytes + client: 1. Sending 96 bytes + client: 1. Sent 96 bytes + +The server node will show the incoming messages: + +.. code-block :: bash + + nsh> udpserver & + udpserver [6:100] + nsh> server: 0. Receiving up 1024 bytes + server: 0. Received 96 bytes from fe80:0000:0000:0000:0000:00ff:fe00:feff port 5472 + server: 1. Receiving up 1024 bytes + server: 1. Received 96 bytes from fe80:0000:0000:0000:0000:00ff:fe00:feff port 5472 + server: 2. Receiving up 1024 bytes + +The sample configuration also allows a telnet session over espnow: + +On the server (node ``0x000a``): + +.. code-block :: bash -On node ``0x0a`` the server can be started using: + nsh> ifconfig wpan0 inet6 fe80::ff:fe00:a + nsh> ifup wpan0 + ifup wpan0..OK + nsh> telnetd -6 & -``nsh> ifup wpan0`` -``ifup wpan0..OK`` -``nsh> udpserver &`` +On the client (node ``Oxfffe``): -On node ``0x0b`` the client can be started using: +.. code-block :: bash -``nsh> ifup wpan0`` -``ifup wpan0..OK`` -``nsh> udpclient fe80::ff:fe00:a`` + nsh> ifup wpan0 + ifup wpan0..OK + nsh> telnet fe80::ff:fe00:a -The client node will show that the messages are sent while the server -shows that messages are received. + NuttShell (NSH) NuttX-12.8.0 + nsh> free + free + total used free maxused maxfree nused nfree name + 253292 65996 187296 66624 129952 185 3 Umem + nsh> exit + exit + nsh> i2schar ------- diff --git a/arch/xtensa/src/esp32/Kconfig b/arch/xtensa/src/esp32/Kconfig index b04fc2a978..1ad0c020c7 100644 --- a/arch/xtensa/src/esp32/Kconfig +++ b/arch/xtensa/src/esp32/Kconfig @@ -726,17 +726,23 @@ config ESP32_UART2 select ARCH_HAVE_SERIAL_TERMIOS config ESP32_ESPNOW_PKTRADIO - bool "ESPNOW packet radio support" - select ESP32_WIRELESS + bool "ESPNOW pktradio support" + depends on ESPRESSIF_WIFI depends on WIRELESS_PKTRADIO default n ---help--- - Enable using espnow for packet radio + Enable using espnow for pktradio, the addressing of nodes is + based on a PANID and PANADDRESS. Node communication is limited + to nodes with the same PANID. There is a direct relation + between the PANADDRESS and the ipv6 address of a node: + PANADDRESS: 0xaabb + ipv6 address: AAAA 0000 0000 0000 0000 00ff fe00 bbaa + For local link AAAA is fe80. if ESP32_ESPNOW_PKTRADIO config ESP32_ESPNOW_PKTRADIO_FRAMELEN - int "ESPNOW frame length" + int "ESPNOW pktradio frame length" range 127 250 default 196 ---help--- @@ -744,86 +750,18 @@ config ESP32_ESPNOW_PKTRADIO_FRAMELEN set equal for all nodes. config ESP32_ESPNOW_PKTRADIO_PANID - hex "ESPNOW node panid" + hex "ESPNOW pktradio node panid" range 0x0 0xffff default 0x0 ---help--- - PANID for espnow packet radio + PANID for espnow pktradio. -if PKTRADIO_ADDRLEN = 1 - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_0 - hex "ESPNOW node address" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -endif #PKTRADIO_ADDRLEN = 1 - -if PKTRADIO_ADDRLEN > 1 - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_0 - hex "ESPNOW node address[0]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_1 - hex "ESPNOW node address[1]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -if PKTRADIO_ADDRLEN > 2 - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_2 - hex "ESPNOW node address[2]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_3 - hex "ESPNOW node address[3]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_4 - hex "ESPNOW node address[4]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_5 - hex "ESPNOW node address[5]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_6 - hex "ESPNOW node address[6]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -config ESP32_ESPNOW_PKTRADIO_NODE_ADDR_7 - hex "ESPNOW node address[7]" - range 0x0 0xff - default 0x0 - ---help--- - Node address for espnow packet radio - -endif #PKTRADIO_ADDRLEN > 2 - -endif #PKTRADIO_ADDRLEN > 1 +config ESP32_ESPNOW_PKTRADIO_PANADDR + hex "ESPNOW pktradio node address" + range 0x0 0xffff + default 0xfffe + ---help--- + PANADDRESS for espnow pktradio. endif #ESP32_ESPNOW_PKTRADIO diff --git a/arch/xtensa/src/esp32/esp32_espnow_pktradio.c b/arch/xtensa/src/esp32/esp32_espnow_pktradio.c index 769ae6426e..0839080343 100644 --- a/arch/xtensa/src/esp32/esp32_espnow_pktradio.c +++ b/arch/xtensa/src/esp32/esp32_espnow_pktradio.c @@ -84,42 +84,49 @@ # error CONFIG_NET_6LOWPAN=y is required. #endif -#if (CONFIG_PKTRADIO_ADDRLEN != 1) && (CONFIG_PKTRADIO_ADDRLEN != 2) && \ - (CONFIG_PKTRADIO_ADDRLEN != 8) -# error No support for CONFIG_PKTRADIO_ADDRLEN other than {1, 2 or 8}. +#if CONFIG_PKTRADIO_ADDRLEN < 2 +#error Unsupported configuration, for espnow pktradio \ + CONFIG_PKTRADIO_ADDRLEN needs to be at least 2. #endif #if CONFIG_IOB_BUFSIZE < CONFIG_ESP32_ESPNOW_PKTRADIO_FRAMELEN -# error Unsupported configuration: reduce ESP32_ESPNOW_PKTRADIO_FRAMELEN or\ - increase IOB_BUFSIZE. +# error Unsupported configuration: reduce \ + CONFIG_ESP32_ESPNOW_PKTRADIO_FRAMELEN or increase \ + CONFIG_IOB_BUFSIZE. #endif /**************************************************************************** - * MAC HDR: 3 byte MAGIC, 2 byte PANID, 1 byte INFO, SRC addr, DST addr - * INFO: BIT0-1: address size: 00 (2^0) -> 1 BYTE address size - * 01 (2^1) -> 2 BYTE address size - * 11 (2^3) -> 8 BYTE address size - * Future expansion: INFO BIT2: cypher info added to MAC HDR + * MAC HDR: 3 byte MAGIC, 2 byte PANID, 1 byte FCB, 2 byte SRC addr, + * 2 byte DST addr, 4 byte RND (random, optional). + * + * FCB (Frame Control Byte): + * BIT0-1: frame type: 00 -> advertising frame + * 01 -> data frame + * 10 -> control frame + * BIT2-3: future expansion + * BIT4 : ciphered (when ciphered the 4 byte RND is added) + * BIT5-7: future expansion * ****************************************************************************/ #define MAC_MAGIC "6lo" #define MAC_PANIDOFFSET sizeof(MAC_MAGIC) - 1 #define MAC_PANIDLEN 2 -#define MAC_INFOOFFSET MAC_PANIDOFFSET + MAC_PANIDLEN -#define MAC_INFOLEN 1 -#define MAC_ADDRLEN CONFIG_PKTRADIO_ADDRLEN -#define MAC_SRCOFFSET MAC_INFOOFFSET + MAC_INFOLEN +#define MAC_FCBOFFSET MAC_PANIDOFFSET + MAC_PANIDLEN +#define MAC_FCBLEN 1 +#define MAC_SRCOFFSET MAC_FCBOFFSET + MAC_FCBLEN +#define MAC_ADDRLEN 2 #define MAC_DSTOFFSET MAC_SRCOFFSET + MAC_ADDRLEN -#define MAC_HDRSIZE MAC_DSTOFFSET + MAC_ADDRLEN -#if MAC_ADDRLEN == 1 -#define MAC_INFOADDRLEN 0x0 -#elif MAC_ADDRLEN == 2 -#define MAC_INFOADDRLEN 0x1 -#elif MAC_ADDRLEN == 8 -#define MAC_INFOADDRLEN 0x3 -#endif -#define MAC_INFOADDRMASK 0x3 +#define MAC_UHDRLEN MAC_DSTOFFSET + MAC_ADDRLEN /* Unciphered */ +#define MAC_RNDLEN 4 +#define MAC_CHDRLEN MAC_UHDRLEN + MAC_RNDLEN /* Ciphered */ + +#define MAC_TYPEMASK 0x03 +#define MAC_ADVFCB 0x00 +#define MAC_DATAFCB 0x01 +#define MAC_CTRLFCB 0x02 + +#define MAC_ENCMASK 0x10 /**************************************************************************** * Private Types @@ -135,7 +142,6 @@ struct espnow_driver_s { bool bifup; /* true:ifup false:ifdown */ - uint8_t mac_address[MAC_ADDRLEN]; /* node address */ uint8_t panid[MAC_PANIDLEN]; /* network id */ struct sixlowpan_reassbuf_s iobuffer; /* buffer for packet reassembly */ @@ -171,8 +177,8 @@ static struct espnow_driver_s g_espnow; /* Utility functions */ -static void espnow_addr(FAR struct net_driver_s *dev); static void espnow_addr2ip(FAR struct net_driver_s *dev); +static void espnow_ip2addr(FAR struct net_driver_s *dev); static inline void espnow_netmask(FAR struct net_driver_s *dev); /* Queue functions */ @@ -243,116 +249,70 @@ static int espnow_properties(FAR struct radio_driver_s *netdev, ****************************************************************************/ /**************************************************************************** - * Name: espnow_addr + * Name: espnow_addr2ip * * Description: - * Set the address based on config values. + * Create a MAC-based IP address from the radio address. + * + * 128 112 96 80 64 48 32 16 + * ---- ---- ---- ---- ---- ---- ---- ---- + * fe80 0000 0000 0000 0000 00ff fe00 xxxx 2-byte address * ****************************************************************************/ -static void espnow_addr(FAR struct net_driver_s *dev) +static void espnow_addr2ip(FAR struct net_driver_s *dev) { FAR struct espnow_driver_s *priv = (FAR struct espnow_driver_s *)dev->d_private; + uint8_t mac_address[MAC_ADDRLEN]; - /* Set the mac panid */ - - priv->panid[0] = CONFIG_ESP32_ESPNOW_PKTRADIO_PANID & 0xff; - priv->panid[1] = CONFIG_ESP32_ESPNOW_PKTRADIO_PANID >> 8; - -#if CONFIG_PKTRADIO_ADDRLEN == 1 - /* Set the mac address based on the 1 byte address */ + /* Get the radio address */ - priv->mac_address[0] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_0; + memcpy(mac_address, dev->d_mac.radio.nv_addr, sizeof(mac_address)); -#elif CONFIG_PKTRADIO_ADDRLEN == 2 - /* Set the mac address based on the 2 byte address */ + /* Set the IP address based on the 2 byte radio address */ - priv->mac_address[0] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_0; - priv->mac_address[1] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_1; - -#elif CONFIG_PKTRADIO_ADDRLEN == 8 - /* Set the mac address based on the 8 byte address */ - - priv->mac_address[0] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_0; - priv->mac_address[1] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_1; - priv->mac_address[2] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_2; - priv->mac_address[3] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_3; - priv->mac_address[4] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_4; - priv->mac_address[5] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_5; - priv->mac_address[6] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_6; - priv->mac_address[7] = CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_7; - -#endif + dev->d_ipv6addr[0] = HTONS(0xfe80); + dev->d_ipv6addr[1] = 0; + dev->d_ipv6addr[2] = 0; + dev->d_ipv6addr[3] = 0; + dev->d_ipv6addr[4] = 0; + dev->d_ipv6addr[5] = HTONS(0x00ff); + dev->d_ipv6addr[6] = HTONS(0xfe00); + dev->d_ipv6addr[7] = ((uint16_t)mac_address[0] << 8) + mac_address[1]; } /**************************************************************************** - * Name: espnow_addr2ip + * Name: espnow_ip2addr * * Description: - * Create a MAC-based IP address from the pktradio address assigned to the - * node. + * Create a pktradio address from the IP address assigned to the node. * * 128 112 96 80 64 48 32 16 * ---- ---- ---- ---- ---- ---- ---- ---- - * fe80 0000 0000 0000 0000 00ff fe00 00xx 1-byte pktradio address - * fe80 0000 0000 0000 0000 00ff fe00 xxxx 2-byte pktradio address - * fe80 0000 0000 0000 xxxx xxxx xxxx xxxx 8-byte pktradio address + * AAAA 0000 0000 0000 0000 00ff fe00 aabb -> radio address 0xbbaa * ****************************************************************************/ -static void espnow_addr2ip(FAR struct net_driver_s *dev) +static void espnow_ip2addr(FAR struct net_driver_s *dev) { FAR struct espnow_driver_s *priv = (FAR struct espnow_driver_s *)dev->d_private; - uint8_t mac_addr[MAC_ADDRLEN]; - - /* Copy the MAC address */ - - memcpy(mac_addr, priv->mac_address, MAC_ADDRLEN); - - /* Set the MAC address as the saddr */ - - dev->d_mac.radio.nv_addrlen = MAC_ADDRLEN; - memcpy(dev->d_mac.radio.nv_addr, mac_addr, MAC_ADDRLEN); - - /* Set the IP address */ - - dev->d_ipv6addr[0] = HTONS(0xfe80); - dev->d_ipv6addr[1] = 0; - dev->d_ipv6addr[2] = 0; - dev->d_ipv6addr[3] = 0; - -#if CONFIG_PKTRADIO_ADDRLEN == 1 - /* Set the IP address based on the 1 byte address */ + uint8_t mac_address[2]; - dev->d_ipv6addr[4] = 0; - dev->d_ipv6addr[5] = HTONS(0x00ff); - dev->d_ipv6addr[6] = HTONS(0xfe00); - dev->d_ipv6addr[7] = (uint16_t)mac_addr[0] << 8; - -#elif CONFIG_PKTRADIO_ADDRLEN == 2 - /* Set the IP address based on the 2 byte address */ + /* Make sure the IP address is correct */ dev->d_ipv6addr[4] = 0; dev->d_ipv6addr[5] = HTONS(0x00ff); dev->d_ipv6addr[6] = HTONS(0xfe00); - dev->d_ipv6addr[7] = (uint16_t)mac_addr[0] << 8 | - (uint16_t)mac_addr[1]; -#elif CONFIG_PKTRADIO_ADDRLEN == 8 - /* Set the IP address based on the 8-byte address */ + mac_address[0] = (uint8_t)(dev->d_ipv6addr[7] >> 8); + mac_address[1] = (uint8_t)(dev->d_ipv6addr[7] & 0xff); - dev->d_ipv6addr[4] = (uint16_t)mac_addr[0] << 8 | - (uint16_t)mac_addr[1]; - dev->d_ipv6addr[5] = (uint16_t)mac_addr[2] << 8 | - (uint16_t)mac_addr[3]; - dev->d_ipv6addr[6] = (uint16_t)mac_addr[4] << 8 | - (uint16_t)mac_addr[5]; - dev->d_ipv6addr[7] = (uint16_t)mac_addr[6] << 8 | - (uint16_t)mac_addr[7]; + /* Set the radio MAC address as the saddr */ -#endif + dev->d_mac.radio.nv_addrlen = MAC_ADDRLEN; + memcpy(dev->d_mac.radio.nv_addr, mac_address, sizeof(mac_address)); } /**************************************************************************** @@ -363,9 +323,7 @@ static void espnow_addr2ip(FAR struct net_driver_s *dev) * * 128 112 96 80 64 48 32 16 * ---- ---- ---- ---- ---- ---- ---- ---- - * ffff ffff ffff ffff ffff ffff ffff ff00 1-byte pktradio address - * ffff ffff ffff ffff ffff ffff ffff 0000 2-byte pktradio address - * ffff ffff ffff ffff 0000 0000 0000 0000 8-byte pktradio address + * ffff ffff ffff ffff ffff ffff ffff 0000 2-byte address * ****************************************************************************/ @@ -375,25 +333,10 @@ static inline void espnow_netmask(FAR struct net_driver_s *dev) dev->d_ipv6netmask[1] = 0xffff; dev->d_ipv6netmask[2] = 0xffff; dev->d_ipv6netmask[3] = 0xffff; - -#if CONFIG_PKTRADIO_ADDRLEN == 1 - dev->d_ipv6netmask[4] = 0xffff; - dev->d_ipv6netmask[5] = 0xffff; - dev->d_ipv6netmask[6] = 0xffff; - dev->d_ipv6netmask[7] = HTONS(0xff00); - -#elif CONFIG_PKTRADIO_ADDRLEN == 2 dev->d_ipv6netmask[4] = 0xffff; dev->d_ipv6netmask[5] = 0xffff; dev->d_ipv6netmask[6] = 0xffff; dev->d_ipv6netmask[7] = 0; - -#elif CONFIG_PKTRADIO_ADDRLEN == 8 - dev->d_ipv6netmask[4] = 0; - dev->d_ipv6netmask[5] = 0; - dev->d_ipv6netmask[6] = 0; - dev->d_ipv6netmask[7] = 0; -#endif } /**************************************************************************** @@ -416,11 +359,11 @@ static void espnow_add_header(struct espnow_driver_s *priv, FAR struct pktradio_metadata_s *pktmeta, FAR struct iob_s *iob) { - uint8_t info = MAC_INFOADDRLEN; + uint8_t fcb = MAC_DATAFCB; memcpy(&iob->io_data[0], MAC_MAGIC, sizeof(MAC_MAGIC) - 1); memcpy(&iob->io_data[MAC_PANIDOFFSET], priv->panid, MAC_PANIDLEN); - memcpy(&iob->io_data[MAC_INFOOFFSET], &info, sizeof(info)); + memcpy(&iob->io_data[MAC_FCBOFFSET], &fcb, sizeof(fcb)); memcpy(&iob->io_data[MAC_SRCOFFSET], pktmeta->pm_src.pa_addr, MAC_ADDRLEN); memcpy(&iob->io_data[MAC_DSTOFFSET], pktmeta->pm_dest.pa_addr, MAC_ADDRLEN); @@ -450,7 +393,8 @@ static void espnow_extract_pktmeta(FAR struct iob_s *iob, memcpy(pktmeta->pm_src.pa_addr, &iob->io_data[MAC_SRCOFFSET], MAC_ADDRLEN); memcpy(pktmeta->pm_dest.pa_addr, &iob->io_data[MAC_DSTOFFSET], MAC_ADDRLEN); - iob->io_offset = MAC_HDRSIZE; + + iob->io_offset = MAC_UHDRLEN; } /**************************************************************************** @@ -767,7 +711,6 @@ static void espnow_recv_cb(const esp_now_recv_info_t * esp_now_info, { FAR struct espnow_driver_s *priv = &g_espnow; FAR struct iob_s *iob; - uint8_t info; irqstate_t flags; /* Drop received data if interface not up */ @@ -779,7 +722,7 @@ static void espnow_recv_cb(const esp_now_recv_info_t * esp_now_info, /* Drop received data if header is too small */ - if (data_len < (MAC_DSTOFFSET + MAC_ADDRLEN)) + if (data_len < MAC_UHDRLEN) { return; } @@ -800,15 +743,6 @@ static void espnow_recv_cb(const esp_now_recv_info_t * esp_now_info, return; } - /* Drop received data if different address length is used */ - - memcpy(&info, &data[MAC_INFOOFFSET], 1); - - if ((info & MAC_INFOADDRMASK) != MAC_INFOADDRLEN) - { - return; - } - /* Header data seems ok, continue with processing */ iob = iob_tryalloc(false); @@ -1152,26 +1086,15 @@ static int espnow_ifup(FAR struct net_driver_s *dev) return -ENOSYS; } + espnow_ip2addr(dev); + ninfo("Bringing up: IPv6 %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", dev->d_ipv6addr[0], dev->d_ipv6addr[1], dev->d_ipv6addr[2], dev->d_ipv6addr[3], dev->d_ipv6addr[4], dev->d_ipv6addr[5], dev->d_ipv6addr[6], dev->d_ipv6addr[7]); - -#if CONFIG_PKTRADIO_ADDRLEN == 1 - ninfo("Node: %02x\n", - dev->d_mac.radio.nv_addr[0]); - -#elif CONFIG_PKTRADIO_ADDRLEN == 2 - ninfo("Node: %02x:%02x\n", - dev->d_mac.radio.nv_addr[0], dev->d_mac.radio.nv_addr[1]); - -#elif CONFIG_PKTRADIO_ADDRLEN == 8 - ninfo("Node: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x PANID=%02x:%02x\n", - dev->d_mac.radio.nv_addr[0], dev->d_mac.radio.nv_addr[1], - dev->d_mac.radio.nv_addr[2], dev->d_mac.radio.nv_addr[3], - dev->d_mac.radio.nv_addr[4], dev->d_mac.radio.nv_addr[5], - dev->d_mac.radio.nv_addr[6], dev->d_mac.radio.nv_addr[7]); -#endif + ninfo("Pan id: %02x:%02x, Pan address: %02x:%02x\n", + priv->panid[0], priv->panid[1], + dev->d_mac.radio.nv_addr[0], dev->d_mac.radio.nv_addr[1]); priv->txblocked = false; priv->bifup = true; @@ -1431,7 +1354,7 @@ static int espnow_ioctl(FAR struct net_driver_s *dev, int cmd, FAR const struct pktradio_addr_s *newaddr = (FAR const struct pktradio_addr_s *)&cmddata->pifr_hwaddr; - if (newaddr->pa_addrlen != 1) + if (newaddr->pa_addrlen != MAC_ADDRLEN) { ret = -EINVAL; } @@ -1439,11 +1362,14 @@ static int espnow_ioctl(FAR struct net_driver_s *dev, int cmd, { FAR struct netdev_varaddr_s *devaddr = &dev->d_mac.radio; - devaddr->nv_addrlen = 1; + devaddr->nv_addrlen = MAC_ADDRLEN; devaddr->nv_addr[0] = newaddr->pa_addr[0]; -#if CONFIG_PKTRADIO_ADDRLEN > 1 - memset(&devaddr->pa_addr[1], 0, CONFIG_PKTRADIO_ADDRLEN - 1); -#endif + devaddr->nv_addr[1] = newaddr->pa_addr[1]; + + /* Update the ip address */ + + espnow_addr2ip(dev); + ret = OK; } } @@ -1465,9 +1391,8 @@ static int espnow_ioctl(FAR struct net_driver_s *dev, int cmd, retaddr->pa_addrlen = devaddr->nv_addrlen; retaddr->pa_addr[0] = devaddr->nv_addr[0]; -#if CONFIG_PKTRADIO_ADDRLEN > 1 - memset(&addr->pa_addr[1], 0, CONFIG_PKTRADIO_ADDRLEN - 1); -#endif + retaddr->pa_addr[1] = devaddr->nv_addr[1]; + ret = OK; } break; @@ -1502,7 +1427,7 @@ static int espnow_ioctl(FAR struct net_driver_s *dev, int cmd, static int espnow_get_mhrlen(FAR struct radio_driver_s *netdev, FAR const void *meta) { - return MAC_HDRSIZE; + return MAC_UHDRLEN; } /**************************************************************************** @@ -1529,7 +1454,8 @@ static int espnow_req_data(FAR struct radio_driver_s *netdev, FAR struct espnow_driver_s *priv; FAR struct iob_s *iob; struct pktradio_metadata_s *pktmeta = (struct pktradio_metadata_s *)meta; - uint8_t mac[CONFIG_PKTRADIO_ADDRLEN]; + uint8_t src[MAC_ADDRLEN]; + uint8_t dst[MAC_ADDRLEN]; DEBUGASSERT(netdev != NULL && netdev->r_dev.d_private != NULL); priv = (FAR struct espnow_driver_s *)netdev->r_dev.d_private; @@ -1545,29 +1471,10 @@ static int espnow_req_data(FAR struct radio_driver_s *netdev, framelist = iob->io_flink; iob->io_flink = NULL; - ninfo("Queuing frame IOB %p [%d]\n", iob, iob->io_len); - memcpy(mac, pktmeta->pm_dest.pa_addr, CONFIG_PKTRADIO_ADDRLEN); -#if CONFIG_PKTRADIO_ADDRLEN == 1 - ninfo("MAC: %02x\n", mac[0]); - -#elif CONFIG_PKTRADIO_ADDRLEN == 2 - ninfo("MAC: %02x:%02x\n", mac[0], mac[1]); - -#elif CONFIG_PKTRADIO_ADDRLEN == 8 - ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], mac[6], mac[7]); -#endif - memcpy(mac, pktmeta->pm_src.pa_addr, CONFIG_PKTRADIO_ADDRLEN); -#if CONFIG_PKTRADIO_ADDRLEN == 1 - ninfo("MAC: %02x\n", mac[0]); - -#elif CONFIG_PKTRADIO_ADDRLEN == 2 - ninfo("MAC: %02x:%02x\n", mac[0], mac[1]); - -#elif CONFIG_PKTRADIO_ADDRLEN == 8 - ninfo("MAC: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", - mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], mac[6], mac[7]); -#endif + memcpy(src, pktmeta->pm_src.pa_addr, MAC_ADDRLEN); + memcpy(dst, pktmeta->pm_dest.pa_addr, MAC_ADDRLEN); + ninfo("Queuing frame %p size %d: %02x:%02x --> %02x:%02x\n", iob, + iob->io_len, src[0], src[1], dst[0], dst[1]); /* Add the MAC header */ @@ -1617,7 +1524,7 @@ static int espnow_properties(FAR struct radio_driver_s *netdev, /* Length of an address */ - properties->sp_addrlen = CONFIG_PKTRADIO_ADDRLEN; + properties->sp_addrlen = MAC_ADDRLEN; /* Fixed frame length */ @@ -1625,13 +1532,13 @@ static int espnow_properties(FAR struct radio_driver_s *netdev, /* Multicast address */ - properties->sp_mcast.nv_addrlen = CONFIG_PKTRADIO_ADDRLEN; - memset(properties->sp_mcast.nv_addr, 0xee, RADIO_MAX_ADDRLEN); + properties->sp_mcast.nv_addrlen = MAC_ADDRLEN; + memset(properties->sp_mcast.nv_addr, 0xee, MAC_ADDRLEN); /* Broadcast address */ - properties->sp_bcast.nv_addrlen = CONFIG_PKTRADIO_ADDRLEN; - memset(properties->sp_mcast.nv_addr, 0xff, RADIO_MAX_ADDRLEN); + properties->sp_bcast.nv_addrlen = MAC_ADDRLEN; + memset(properties->sp_mcast.nv_addr, 0xff, MAC_ADDRLEN); return OK; } @@ -1661,6 +1568,7 @@ int pktradio_espnow(void) FAR struct espnow_driver_s *priv; FAR struct radio_driver_s *radio; FAR struct net_driver_s *dev; + uint8_t mac_address[MAC_ADDRLEN]; ninfo("Initializing\n"); @@ -1686,11 +1594,23 @@ int pktradio_espnow(void) #endif dev->d_private = priv; /* Link private state to dev */ - /* Set the network mask and MAC-based IP address */ + /* Set the PANID */ + + priv->panid[0] = CONFIG_ESP32_ESPNOW_PKTRADIO_PANID >> 8; + priv->panid[1] = CONFIG_ESP32_ESPNOW_PKTRADIO_PANID & 0xff; + + /* Set the PANADDRESS */ + + mac_address[0] = CONFIG_ESP32_ESPNOW_PKTRADIO_PANADDR >> 8; + mac_address[1] = CONFIG_ESP32_ESPNOW_PKTRADIO_PANADDR & 0xff; + + dev->d_mac.radio.nv_addrlen = MAC_ADDRLEN; + memcpy(dev->d_mac.radio.nv_addr, mac_address, sizeof(mac_address)); + + /* Set the MAC-based IP address */ - espnow_netmask(dev); - espnow_addr(dev); espnow_addr2ip(dev); + espnow_netmask(dev); /* Initialize the Network frame-related callbacks */ diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/espnow/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/espnow/defconfig index 45000b2873..a3e91bddd1 100644 --- a/boards/xtensa/esp32/esp32-devkitc/configs/espnow/defconfig +++ b/boards/xtensa/esp32/esp32-devkitc/configs/espnow/defconfig @@ -28,10 +28,6 @@ CONFIG_DEV_URANDOM=y CONFIG_DRIVERS_IEEE80211=y CONFIG_DRIVERS_WIRELESS=y CONFIG_ESP32_ESPNOW_PKTRADIO=y -CONFIG_ESP32_ESPNOW_PKTRADIO_NODE_ADDR_0=0x0a -CONFIG_ESP32_SPIFLASH=y -CONFIG_ESP32_SPIFLASH_SPIFFS=y -CONFIG_ESP32_STORAGE_MTD_SIZE=0x80000 CONFIG_ESP32_UART0=y CONFIG_ESPRESSIF_WIFI=y CONFIG_EXAMPLES_UDP=y @@ -39,6 +35,7 @@ CONFIG_EXAMPLES_UDP_DEVNAME="wpan0" CONFIG_EXAMPLES_UDP_IPv6=y CONFIG_EXAMPLES_UDP_SERVER1=y CONFIG_EXAMPLES_UDP_TARGET2=y +CONFIG_FS_LARGEFILE=y CONFIG_FS_PROCFS=y CONFIG_IDLETHREAD_STACKSIZE=3072 CONFIG_INIT_ENTRYPOINT="nsh_main" @@ -51,9 +48,8 @@ CONFIG_NETDB_DNSCLIENT=y CONFIG_NETDB_DNSCLIENT_NAMESIZE=64 CONFIG_NETDEV_LATEINIT=y CONFIG_NETDEV_PHY_IOCTL=y -CONFIG_NETDEV_TELNET=y CONFIG_NETDEV_WIRELESS_IOCTL=y -CONFIG_NETUTILS_CJSON=y +CONFIG_NETUTILS_TELNETD=y CONFIG_NET_6LOWPAN=y CONFIG_NET_BROADCAST=y CONFIG_NET_ETH_PKTSIZE=1518 @@ -62,13 +58,13 @@ CONFIG_NET_IPv6=y CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32 CONFIG_NET_STATISTICS=y CONFIG_NET_TCP=y -CONFIG_NET_TCP_DELAYED_ACK=y CONFIG_NET_TCP_WRITE_BUFFERS=y CONFIG_NET_UDP=y CONFIG_NSH_ARCHINIT=y CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_READLINE=y +CONFIG_PKTRADIO_ADDRLEN=2 CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=2048 CONFIG_PREALLOC_TIMERS=4 CONFIG_PTHREAD_MUTEX_TYPES=y @@ -79,20 +75,17 @@ CONFIG_RR_INTERVAL=200 CONFIG_SCHED_LPWORK=y CONFIG_SCHED_WAITPID=y CONFIG_SIG_DEFAULT=y -CONFIG_SPI=y -CONFIG_SPIFFS_NAME_MAX=48 CONFIG_START_DAY=6 CONFIG_START_MONTH=12 CONFIG_START_YEAR=2011 CONFIG_SYSLOG_BUFFER=y CONFIG_SYSLOG_CONSOLE=y -CONFIG_SYSTEM_ARGTABLE3=y CONFIG_SYSTEM_DHCPC_RENEW=y CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_TELNET_CLIENT=y CONFIG_TLS_TASK_NELEM=4 CONFIG_UART0_SERIAL_CONSOLE=y CONFIG_WIRELESS=y CONFIG_WIRELESS_PKTRADIO=y CONFIG_WIRELESS_WAPI=y CONFIG_WIRELESS_WAPI_CMDTOOL=y -CONFIG_WIRELESS_WAPI_INITCONF=y