UDP Notify Example
Overview
This application demonstrates how to use callback functions to handle incoming UDP packets in a NetBurner NNDK (NetBurner Network Development Kit) environment. The example implements an asynchronous UDP packet receiver that automatically processes incoming data through a registered callback function.
Application Details
Application Name:** UDP Notify Example
Listen Port:** 2121
Protocol:** UDP (User Datagram Protocol)
Key Features
- Callback-based UDP handling: Uses a notification callback function instead of polling
- Automatic packet processing: Incoming UDP packets trigger the callback function automatically
- Network stack initialization: Includes proper DHCP client setup and network interface management
- Packet validation: Validates received packets before processing
- Diagnostic support: Includes system diagnostics for debugging
How It Works
Network Initialization
- Initializes the network stack
- Enables system diagnostics
- Waits for active network connection (5 second timeout)
- Waits for DHCP address assignment to complete
- Displays the listening IP address and port
UDP Packet Handling
- Creates a FIFO (First-In-First-Out) buffer for incoming packets
- Registers the UDP port with a callback function using
RegisterUDPFifoWithNotify()
- When packets arrive, the callback function
UdpListenCallback() is automatically invoked
- The callback processes the packet and displays information about the sender and data
Callback Function Behavior
The UdpListenCallback() function:
- Creates a
UDPPacket object from the FIFO
- Validates the received packet
- Extracts packet size and source IP address
- Displays the packet data using
ShowData()
- Handles invalid packets gracefully
Technical Components
Key Functions
Callback Function Signature
void UdpListenCallback(OS_FIFO *pfifo, uint16_t port)
The callback function must match the typedef:
void udp_data_notify(OS_FIFO *pfifo, uint16_t port)
UDP packet arrival notification callback function type.
Definition udp.h:4002
Usage
- Compile and deploy the application to a NetBurner device
- Connect the device to your network (DHCP will assign an IP address)
- Monitor the console output to see the assigned IP address
- Send UDP packets to the displayed IP address on port 2121
- View the output as the callback function processes and displays received packets
Example Output
Application: UDP Notify Example
NNDK Revision: [version]
Listening for UDP packets on 192.168.1.100: 2121
UdpListenCallback()
Received a UDP packet with 15 bytes from : 192.168.1.50
[Packet data display]
Differences from Standard UDP Handling
This example uses callback-based notification rather than polling:
Requirements
- NetBurner NNDK development environment
- NetBurner-compatible hardware device
- Network connection with DHCP server
- UDP client application for testing (can send packets to port 2121)
Notes
- The application runs in an infinite loop, continuously listening for UDP packets
- System diagnostics are enabled for development/debugging purposes
- Remove diagnostic features for production deployment
- The callback function executes in the context of the network stack
- Packet timeout can be configured by replacing the
0 parameter in UDPPacket constructor