Morse Micro IoT SDK  2.9.7
udp_broadcast.c File Reference

Detailed Description

Example app utilizing the LWIP "raw" API to handle UDP broadcast packets.

Note
It is assumed that you have followed the steps in the Getting Started guide and are therefore familiar with how to build, flash, and monitor an application using the MM-IoT-SDK framework.

Operating Modes

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

Transmit 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.

Note
The reason for two copies of the packet in 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 :

root@morsemicro:~ $ tcpdump -A -i wlan0 -n "broadcast"
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on wlan0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
01:51:49.865347 0c:bf:74:00:01:29 > ff:ff:ff:ff:ff:ff Null Unnumbered, xid, Flags [Response], length 6: 01 00
...
01:51:49.902936 ARP, Request who-has 192.168.1.2 tell 192.168.1.2, length 28
..........t..)..............
01:51:49.902975 ARP, Request who-has 192.168.1.2 tell 192.168.1.2, length 28
..........t..)..............
01:51:51.432422 IP 192.168.1.2.1337 > 0.0.0.0.0: UDP, length 28
E..8.......
.........9...$..G'day World, packet no. 00..
01:51:51.432440 IP 192.168.1.2.1337 > 0.0.0.0.0: UDP, length 28
E..8.......
.........9...$..G'day World, packet no. 00..
01:52:01.309673 IP 192.168.1.2.1337 > 0.0.0.0.0: UDP, length 28
E..8....... .........9...$..G'day World, packet no. 01..
01:52:01.309700 IP 192.168.1.2.1337 > 0.0.0.0.0: UDP, length 28
E..8....... .........9...$..G'day World, packet no. 01..
01:52:11.186521 IP 192.168.1.2.1337 > 0.0.0.0.0: UDP, length 28
E..8.................9...$..G'day World, packet no. 02..
01:52:11.186549 IP 192.168.1.2.1337 > 0.0.0.0.0: UDP, length 28
E..8.................9...$..G'day World, packet no. 02..

Receive Mode

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:

+-----+--------------+--------------+ +----------------+
| Key | Color data 0 | Color data 1 | ..... | Color data n-1 |
+-----+--------------+--------------+ +----------------+

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.

./udp_broadcast_server.py

‍There is a help menu for the python script that you can view for configuration settings.

./udp_broadcast_server.py -h

Configuration

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"
Include dependency graph for udp_broadcast.c:

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...
 

Macro Definition Documentation

◆ BROADCAST_PACKET_MAX_TX_PAYLOAD_LEN

#define BROADCAST_PACKET_MAX_TX_PAYLOAD_LEN   35

Maximum length of broadcast tx packet payload.

Definition at line 123 of file udp_broadcast.c.

◆ BROADCAST_PACKET_TX_PAYLOAD_FMT

#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.

◆ DEFAULT_BROADCAST_PACKET_COUNT

#define DEFAULT_BROADCAST_PACKET_COUNT   10

Number of broadcast packet to transmit.

Definition at line 117 of file udp_broadcast.c.

◆ DEFAULT_PACKET_INTERVAL_MS

#define DEFAULT_PACKET_INTERVAL_MS   10000

Interval between successive packet transmission.

Definition at line 121 of file udp_broadcast.c.

◆ DEFAULT_UDP_BROADCAST_ID

#define DEFAULT_UDP_BROADCAST_ID   0

Default ID used in the rx metadata.

Definition at line 129 of file udp_broadcast.c.

◆ DEFAULT_UDP_BROADCAST_MODE

#define DEFAULT_UDP_BROADCAST_MODE   TX_MODE

Default mode for the application.

Definition at line 127 of file udp_broadcast.c.

◆ DEFAULT_UDP_PORT

#define DEFAULT_UDP_PORT   1337

UDP port to bind too.

Definition at line 119 of file udp_broadcast.c.

◆ MMBC_KEY

#define MMBC_KEY   0x43424d4d

Key used to identify received broadcast packets.

Definition at line 132 of file udp_broadcast.c.

Enumeration Type Documentation

◆ 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.

Function Documentation

◆ app_init()

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.

◆ get_mode()

static enum udp_broadcast_mode get_mode ( void  )
static

Get the mode from config store.

Returns
translates the value of 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.

◆ init_udp_pcb()

static struct udp_pcb * init_udp_pcb ( void  )
static

Initialize the UDP protocol control block.

Binds to DEFAULT_UDP_PORT

Note
If the parameters are set in the config store they will be used.
Returns
Reference to the pcb is successfully initialized else NULL

Definition at line 304 of file udp_broadcast.c.

◆ udp_broadcast_rx_start()

static void udp_broadcast_rx_start ( struct udp_pcb *  pcb)
static

Set a receive callback for the UDP PCB.

This callback will be called when receiving a datagram for the pcb.

Parameters
pcbUDP protocol control block to register the callback for

Definition at line 241 of file udp_broadcast.c.

◆ udp_broadcast_tx_start()

static void udp_broadcast_tx_start ( struct udp_pcb *  pcb)
static

Broadcast a udp packet every DEFAULT_PACKET_INTERVAL_MS until DEFAULT_BROADCAST_PACKET_COUNT packets have been sent.

Note
If the parameters are set in the config store they will be used.
Parameters
pcbUDP protocol control block to use for transmission

Definition at line 258 of file udp_broadcast.c.

◆ udp_raw_recv()

static void udp_raw_recv ( void *  arg,
struct udp_pcb *  pcb,
struct pbuf *  p,
const ip_addr_t *  addr,
u16_t  port 
)
static

Callback function to handle received data from the UDP pcb.

Warning
Be aware that addr might point into the pbuf p so freeing this pbuf can make addr invalid, too.
Parameters
argUser supplied argument used to store a reference to the global rx_metadata struct.
pcbThe udp_pcb which received data
pThe packet buffer that was received
addrThe remote IP address from which the packet was received
portThe remote port from which the packet was received

Definition at line 185 of file udp_broadcast.c.

Variable Documentation

◆ rx_metadata

struct udp_broadcast_rx_metadata rx_metadata = {0}
static

Global data structure used in RX mode to record metadata.

Definition at line 171 of file udp_broadcast.c.