![]() |
Morse Micro IoT SDK
2.9.7
|
Example app utilizing the LWIP "raw" API to handle UDP broadcast packets.
This example application supports a number of modes that can be set by writing the key udp_broadcast.mode in the config store with the relevant mode.
| Config store value | Mode |
|---|---|
| tx | Transmit Mode |
| rx | Receive Mode |
In this mode the application demonstrates how to transmit a number of UDP broadcast packets. To view the packets the application can use tcpdump on the AP.
tcpdump is due to the fact that the STA first transmits the packet to the AP and then the AP broadcasts it to the network. This expected behavior.Example output from tcpdump :
In receive mode the application demonstrates reception of UDP broadcast packets. In this mode a callback function is registered with LWIP. This callback function will get executed every time that packet is received. In this case the application have some additional logic that looks for a specific packet format but this need not be the case.
As mentioned above, the application has some additional logic to look for specific payloads in the broadcast packets. The application uses this to blink the LEDs on any connected devices. The payload has the following format:
n is the number of devices. Key is a 32-bit little-endian number.
By default the application will process the color data for DEFAULT_UDP_BROADCAST_ID. However this can be configured by setting udp_broadcast.id in the config store.
To generate this payload a python script udp_broadcast_server.py has been provided in the udp_broadcast/tools directory. You can configure your Morse Micro AP into bridge mode so that you can access devices on the HaLow network, see user guide for AP on how to do this. Once that is set up you can run the python script to start sending broadcast packets.
There is a help menu for the python script that you can view for configuration settings.
./udp_broadcast_server.py -h
See Application helper routines for Wireless LAN interface for details of WLAN and IP stack configuration. Additional configuration options for this application can be found in the config.hjson file.
Definition in file udp_broadcast.c.
#include <string.h>#include <endian.h>#include "mmosal.h"#include "mmwlan.h"#include "mmconfig.h"#include "mmipal.h"#include "lwip/icmp.h"#include "lwip/tcpip.h"#include "lwip/udp.h"#include "mm_app_common.h"
Go to the source code of this file.
Data Structures | |
| struct | udp_broadcast_rx_payload |
| UDP broadcast rx payload format. More... | |
| struct | udp_broadcast_rx_metadata |
| Struct used in rx mode for storing state. More... | |
Macros | |
| #define | DEFAULT_BROADCAST_PACKET_COUNT 10 |
| Number of broadcast packet to transmit. More... | |
| #define | DEFAULT_UDP_PORT 1337 |
| UDP port to bind too. More... | |
| #define | DEFAULT_PACKET_INTERVAL_MS 10000 |
| Interval between successive packet transmission. More... | |
| #define | BROADCAST_PACKET_MAX_TX_PAYLOAD_LEN 35 |
| Maximum length of broadcast tx packet payload. More... | |
| #define | BROADCAST_PACKET_TX_PAYLOAD_FMT "G'day World, packet no. %lu." |
| Format string to use for the tx packet payload. More... | |
| #define | DEFAULT_UDP_BROADCAST_MODE TX_MODE |
| Default mode for the application. More... | |
| #define | DEFAULT_UDP_BROADCAST_ID 0 |
| Default ID used in the rx metadata. More... | |
| #define | MMBC_KEY 0x43424d4d |
| Key used to identify received broadcast packets. More... | |
Enumerations | |
| enum | udp_broadcast_mode { TX_MODE , RX_MODE } |
| Enumeration of the various broadcast modes that can be used. More... | |
Functions | |
| static void | udp_raw_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) |
| Callback function to handle received data from the UDP pcb. More... | |
| static void | udp_broadcast_rx_start (struct udp_pcb *pcb) |
| Set a receive callback for the UDP PCB. More... | |
| static void | udp_broadcast_tx_start (struct udp_pcb *pcb) |
| Broadcast a udp packet every DEFAULT_PACKET_INTERVAL_MS until DEFAULT_BROADCAST_PACKET_COUNT packets have been sent. More... | |
| static struct udp_pcb * | init_udp_pcb (void) |
| Initialize the UDP protocol control block. More... | |
| static enum udp_broadcast_mode | get_mode (void) |
| Get the mode from config store. More... | |
| void | app_init (void) |
| Main entry point to the application. More... | |
Variables | |
| static struct udp_broadcast_rx_metadata | rx_metadata = {0} |
| Global data structure used in RX mode to record metadata. More... | |
| #define BROADCAST_PACKET_MAX_TX_PAYLOAD_LEN 35 |
Maximum length of broadcast tx packet payload.
Definition at line 123 of file udp_broadcast.c.
| #define BROADCAST_PACKET_TX_PAYLOAD_FMT "G'day World, packet no. %lu." |
Format string to use for the tx packet payload.
Definition at line 125 of file udp_broadcast.c.
| #define DEFAULT_BROADCAST_PACKET_COUNT 10 |
Number of broadcast packet to transmit.
Definition at line 117 of file udp_broadcast.c.
| #define DEFAULT_PACKET_INTERVAL_MS 10000 |
Interval between successive packet transmission.
Definition at line 121 of file udp_broadcast.c.
| #define DEFAULT_UDP_BROADCAST_ID 0 |
Default ID used in the rx metadata.
Definition at line 129 of file udp_broadcast.c.
| #define DEFAULT_UDP_BROADCAST_MODE TX_MODE |
Default mode for the application.
Definition at line 127 of file udp_broadcast.c.
| #define DEFAULT_UDP_PORT 1337 |
UDP port to bind too.
Definition at line 119 of file udp_broadcast.c.
| #define MMBC_KEY 0x43424d4d |
Key used to identify received broadcast packets.
Definition at line 132 of file udp_broadcast.c.
| enum udp_broadcast_mode |
Enumeration of the various broadcast modes that can be used.
| Enumerator | |
|---|---|
| TX_MODE | Transmit mode. Application will transmit a set amount of broadcast packets. |
| RX_MODE | Receive mode. Application will listen for any broadcast packets and process any that start with MMBC_KEY |
Definition at line 135 of file udp_broadcast.c.
| void app_init | ( | void | ) |
Main entry point to the application.
This will be invoked in a thread once operating system and hardware initialization has completed. It may return, but it does not have to.
Definition at line 373 of file udp_broadcast.c.
|
static |
Get the mode from config store.
udp_broadcast.mode into a udp_broadcast_mode, if no valid mode is set DEFAULT_UDP_BROADCAST_MODE is returned. Definition at line 346 of file udp_broadcast.c.
|
static |
Initialize the UDP protocol control block.
Binds to DEFAULT_UDP_PORT
Definition at line 304 of file udp_broadcast.c.
|
static |
Set a receive callback for the UDP PCB.
This callback will be called when receiving a datagram for the pcb.
| pcb | UDP protocol control block to register the callback for |
Definition at line 241 of file udp_broadcast.c.
|
static |
Broadcast a udp packet every DEFAULT_PACKET_INTERVAL_MS until DEFAULT_BROADCAST_PACKET_COUNT packets have been sent.
| pcb | UDP protocol control block to use for transmission |
Definition at line 258 of file udp_broadcast.c.
|
static |
Callback function to handle received data from the UDP pcb.
addr might point into the pbuf p so freeing this pbuf can make addr invalid, too.| arg | User supplied argument used to store a reference to the global rx_metadata struct. |
| pcb | The udp_pcb which received data |
| p | The packet buffer that was received |
| addr | The remote IP address from which the packet was received |
| port | The remote port from which the packet was received |
Definition at line 185 of file udp_broadcast.c.
|
static |
Global data structure used in RX mode to record metadata.
Definition at line 171 of file udp_broadcast.c.