108#include "lwip/icmp.h"
109#include "lwip/tcpip.h"
117#define DEFAULT_BROADCAST_PACKET_COUNT 10
119#define DEFAULT_UDP_PORT 1337
121#define DEFAULT_PACKET_INTERVAL_MS 10000
123#define BROADCAST_PACKET_MAX_TX_PAYLOAD_LEN 35
125#define BROADCAST_PACKET_TX_PAYLOAD_FMT "G'day World, packet no. %lu."
127#define DEFAULT_UDP_BROADCAST_MODE TX_MODE
129#define DEFAULT_UDP_BROADCAST_ID 0
132#define MMBC_KEY 0x43424d4d
185static void udp_raw_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
186 const ip_addr_t *addr, u16_t port)
188 LWIP_UNUSED_ARG(pcb);
189 LWIP_UNUSED_ARG(addr);
190 LWIP_UNUSED_ARG(port);
202 uint32_t min_payload_len =
203 sizeof(payload->
key) + (
sizeof(payload->
data[0]) * (metadata->
id + 1));
205 if (p->len < min_payload_len)
207 printf(
"Payload length to short. Len: %u. Min len: %lu\n", p->len, min_payload_len);
213 printf(
"Invalid payload received.\n");
217 printf(
"Valid payload received. \n"
218 " Time since last: %lums\n"
219 " Data recieved: 0x%02x%02x%02x\n\n",
270 for (count = 0; (count < packet_count) || (packet_count == 0); count++)
272 printf(
"Sending Broadcast UDP packet no. %lu.\n", count);
277 printf(
"Failed to allocate pbuf for transmit\n");
283 err = udp_send(pcb, p);
286 printf(
"Failed to send, err:%d.\n", err);
306 struct udp_pcb *pcb = NULL;
311 if (port_num > UINT16_MAX)
314 printf(
"Specified port number is too large. Falling back to %lu\n", port_num);
322 printf(
"Error creating PCB.\n");
326 err = udp_bind(pcb, IP_ADDR_ANY, (uint16_t)port_num);
329 printf(
"Failed to bind, err:%d.\n", err);
352 if (strcasecmp(mode_str,
"tx") == 0)
356 else if (strcasecmp(mode_str,
"rx") == 0)
362 printf(
"Unknown mode: %s. Reverting to default.\n", mode_str);
378 printf(
"\n\nMorse UDP broadcast Demo (Built " __DATE__
" " __TIME__
")\n\n");
int mmconfig_read_string(const char *key, char *buffer, int bufsize)
Returns the persistent store string value identified by the key.
int mmconfig_read_uint32(const char *key, uint32_t *value)
Returns the unsigned integer stored in persistent store identified by the key.
void mmhal_set_led(uint8_t led, uint8_t level)
Set the specified LED to the requested level.
void mmosal_task_sleep(uint32_t duration_ms)
Sleep for a period of time, yielding during that time.
uint32_t mmosal_get_time_ms(void)
Get the system time in milliseconds.
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_stop(void)
Disconnects from Wi-Fi and de-initializes the WLAN interface.
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.
UDP broadcast rx payload format.
uint8_t blue
Blue intensity.
uint8_t red
Red intensity.
uint32_t key
Key used to identify payload.
uint8_t green
Green intensity.
struct udp_broadcast_rx_payload::@0 data[]
Flexible array member used to access color data for each ID.
static struct udp_pcb * init_udp_pcb(void)
Initialize the UDP protocol control block.
#define BROADCAST_PACKET_MAX_TX_PAYLOAD_LEN
Maximum length of broadcast tx packet payload.
#define DEFAULT_UDP_PORT
UDP port to bind too.
#define BROADCAST_PACKET_TX_PAYLOAD_FMT
Format string to use for the tx packet payload.
static enum udp_broadcast_mode get_mode(void)
Get the mode from config store.
static void udp_broadcast_rx_start(struct udp_pcb *pcb)
Set a receive callback for the UDP PCB.
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 ...
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.
udp_broadcast_mode
Enumeration of the various broadcast modes that can be used.
#define DEFAULT_PACKET_INTERVAL_MS
Interval between successive packet transmission.
#define DEFAULT_UDP_BROADCAST_MODE
Default mode for the application.
void app_init(void)
Main entry point to the application.
static struct udp_broadcast_rx_metadata rx_metadata
Global data structure used in RX mode to record metadata.
#define MMBC_KEY
Key used to identify received broadcast packets.
#define DEFAULT_BROADCAST_PACKET_COUNT
Number of broadcast packet to transmit.