multicast.c File Reference

UDP multicast test program. More...

#include <BoardSupport/inc/stdtypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <Libs/NETlib/net.h>
#include <BoardSupport/inc/BoardSpecific.h>
#include <Common/Common.h>
#include <Common/uartio.h>
#include <Common/timer.h>
#include <Common/CPrintf.h>
#include <BoardSupport/config/netconfig.c>

Macros

#define MCAST_IP_ADDR   "239.255.1.1" /* organisations local multicast */
 
#define MCAST_IP_ADDR2   "239.255.1.2" /* organisations local multicast */
 
#define MCAST_PORT   5555
 
#define MCAST_PORT2   5556
 
#define DSP_PORT   5031
 
#define CMD_LED_INIT   1
 
#define CMD_LED_ON   2
 
#define CMD_LED_OFF   3
 

Functions

int32_t udp_call_back (SOCKET *so, void *data, uint32_t len, uint32_t ec)
 
int main (void)
 

Variables

char * program_name = "multicast"
 
SOCKET * udp_socket
 
char * udp_data
 

Detailed Description

                          _         _             _
                       __| |    ___(_) ____ _ __ | |_
                      / _` |   / __| |/ _` | '_ \| __|
                     | (_| | _ \__ \ | (_| | | | | |_
                      \__,_|(_) ___/_|\__, |_| |_|\__|
                     Signalprocessing |___/ Technology
Author
D.SignT GmbH & Co. KG, Claus Hermbusche
Date
2019-06-03 Simple DSP program for receiving multicast UDP - packets. The received data is used to switch the LED's on and off. The DSP IP address is configured to 192.168.168.200. The multicast group is 239.255.1.1 port 5555.

Macro Definition Documentation

#define MCAST_IP_ADDR   "239.255.1.1" /* organisations local multicast */
Examples:
Multicast.c.
#define MCAST_IP_ADDR2   "239.255.1.2" /* organisations local multicast */
#define MCAST_PORT   5555
Examples:
Multicast.c.
#define MCAST_PORT2   5556
Examples:
Multicast.c.
#define DSP_PORT   5031
Examples:
Multicast.c, and Send.c.
#define CMD_LED_INIT   1
Examples:
Multicast.c.
#define CMD_LED_ON   2
Examples:
Multicast.c.
#define CMD_LED_OFF   3
Examples:
Multicast.c.

Function Documentation

int32_t udp_call_back ( SOCKET *  so,
void *  data,
uint32_t  len,
uint32_t  ec 
)
169 {
170  /***************************************************************************
171  locals
172  ***************************************************************************/
173  char *udp_data = (char *)data;
174  int32_t data_length; // net_recv_event_handler() return parameter
175  char buffer[20]; /* small buffer for ip-address conversion */
176 
177  /***************************************************************************
178  suppress unused parameter warning
179  ***************************************************************************/
181  UNREFERENCED_PARAMETER(ec); // handled in net_recv_event_handler()
182 
183  /***************************************************************************
184  Check events
185  Use net_recv_event_handler() to determine the data length waiting in the
186  receive buffer
187  If NULL is passed as pLog parameter, no message is printed to output.
188  CPrintf is used here for demonstration purpose only. You shouldn't use
189  long blocking operation in callback functions.
190  ***************************************************************************/
191  data_length = net_recv_event_handler (so, CPrintf); /* with messages */
192  // data_length = net_recv_event_handler (so, NULL); /* without messages */
193 
194  /***************************************************************************
195  net_recv_event_handler() returns the amount of data waiting in the buffer
196  ***************************************************************************/
197  if (data_length)
198  {
199  /*******************************************************************
200  a new command received:
201  1. byte: command
202  2. byte: led number
203  *******************************************************************/
204  switch ( udp_data[0] )
205  {
206  case CMD_LED_INIT:
207  LED_init();
208  break;
209 
210  case CMD_LED_ON:
211  LED_on(udp_data[1]);
212  break;
213 
214  case CMD_LED_OFF:
215  LED_off(udp_data[1]);
216  break;
217 
218  default :
219  /* no more commands defined*/
220  break;
221  } // switch
222 
223  /*******************************************************************
224  print some more details on received data
225  CPrintf is used here for demonstration purpose only. You shouldn't
226  use long blocking operation in callback functions.
227  *******************************************************************/
228  if (IS_MULTICAST(so-> mc_group_addr))
229  {
230  /***************************************************************
231  when this is a multicast UDP packet, so-> dest_addr, so-> src_addr
232  and so-> src_port, so-> dest_port are invalid! Use a new offset
233  to access valid connection data:
234  ***************************************************************/
235  CPuts (" New multicast data received\r\n");
236  CPrintf (" Destination Address: %s\r\n", inet_ntoa ( so-> mc_dest_addr, buffer));
237  CPrintf (" Multicast Group Address: %s\r\n", inet_ntoa ( so-> mc_group_addr, buffer));
238  CPrintf (" Destination Port: %d\r\n", so-> mc_dest_port);
239  CPrintf (" Source Multicast Port: %d\r\n", so-> mc_src_port);
240  }
241  else
242  {
243  CPrintf (" New data received\r\n"
244  " Destination Address: %s\r\n"
245  " Source Address: %s\r\n"
246  " Destination Port: %d\r\n"
247  " Source Port: %d\r\n\n",
248  inet_ntoa ( so-> dest_addr, buffer),
249  inet_ntoa ( so-> src_addr, buffer),
250  so-> dest_port,
251  so-> src_port);
252  }
253 #ifdef USE_UART_CONTROL
254  udp_data[len] = 0x0; /* string termination */
255  CPuts ( udp_data); /* send string back over UART */
256  CPuts ( CRLF);
257 #endif
258 
259  }
260 
261  /***************************************************************************
262  return true, if message processed
263  ***************************************************************************/
264  return (SOCKET_CB_OK);
265 }
#define CMD_LED_OFF
Definition: multicast.c:131
#define CMD_LED_INIT
Definition: multicast.c:129
char * inet_ntoa(uint32_t i_addr, char *s)
Convert IP-address from 0xbbaaddcc to "aaa.bbb.ccc.ddd".
void LED_on(unsigned int ledNum)
Definition: BoardSpecific.c:1302
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define CRLF
Definition: cprintf.h:214
#define CMD_LED_ON
Definition: multicast.c:130
void LED_off(unsigned int ledNum)
Definition: BoardSpecific.c:1284
static char buffer[100]
Definition: blocksend.c:158
char * udp_data
Definition: multicast.c:147
void LED_init()
Definition: BoardSpecific.c:1270
int int32_t
Definition: stdint.h:46
#define UNREFERENCED_PARAMETER(P)
Definition: Common.h:115
#define SOCKET_CB_OK
Definition: net.h:576
int CPuts(const char *_ptr)
Definition: cprintf.c:399
#define IS_MULTICAST(a)
Definition: net.h:761
int main ( void  )
276 {
277  /***************************************************************************
278  locals
279  ***************************************************************************/
280  int main_loop = 1; /* main loop switch, set to 0 to exit */
281  timeval stamp1, stamp2, delta; /* used to determine startup time */
282 
283  /***************************************************************************
284  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
285  (ref. \Common\Common.c)
286  ***************************************************************************/
287  AppInit (GET_CLOCK);
288 
289  /***************************************************************************
290  select output device for CPrintf (ref. \Common\cprintf.c)
291  possible settings:
292  CPRINTF_UART_OUTPUT -> output to UART
293  CPRINTF_CCS_OUTPUT -> output to CCS
294  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
295  ***************************************************************************/
296  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
297 
298  /***************************************************************************
299  print a start up message
300  ***************************************************************************/
301  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
302 
303  /**************************************************************************/
304  // CPrintfProgress (" Heap check ");
305  // at least 0x2000 bytes required for this app
306  /**************************************************************************/
307  ASSERT_HEAP (initial_heap_size, 0x2000);
308  // CPrintfProgressSuccess();
309 
310  /***************************************************************************
311  malloc space for data
312  ***************************************************************************/
313  CPrintfProgress (" Try to allocate data buffer ");
314  udp_data = (char *) malloc (UDP_MAX_PACKET_SIZE * sizeof(char));
315  if ( udp_data == NULL )
316  {
317  prg_exit ("out of memory error");
318  }
320 
321  /**************************************************************************/
322  CPrintfProgress (" Setup system time ");
323  // 1 milli seconds resolution
324  /**************************************************************************/
327  CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
329 
330  /**************************************************************************/
331  CPrintfProgress (" Enable interrupts ");
332  /**************************************************************************/
335 
336  /**************************************************************************/
337  CPrintfProgress (" Start system timer ");
338  /**************************************************************************/
339  StartSystemTimer ();
341  CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
342 
343  /***************************************************************************
344  measure network initialization time
345  ***************************************************************************/
346  stamp1 = GetTimeStamp();
347 
348  /**************************************************************************/
349  CPrintfProgress (" Initialize network ");
350  /**************************************************************************/
351  InitializeNetwork ( 64); // 64 bytes for ping
352 
353 
354  /***************************************************************************
355  open socket: receive data from any address and port
356  ***************************************************************************/
357  udp_socket = socket_open (ANY_ADDRESS, /* all addresses */
358  ANY_PORT, /* destination port */
359  DSP_PORT, /* source port */
360  DATATYPE_CHAR, /* data type char */
361  UDP_INIT_FUNC); /* udp protocol */
362  if ( udp_socket == INVALID_SOCKET )
363  {
364  prg_exit ("socket_open() error"); /* possibly insufficient heap */
365  } // if
366 
367  /***************************************************************************
368  define callback function for received UDP packets
369  ***************************************************************************/
372 
373  stamp2 = GetTimeStamp();
374 
375  tv_interval (&delta, &stamp1, &stamp2);
376  CPuts (" network startup time [sec]: ");
378  "%"PRId32".%03"PRId32"\r\n"
380  delta.tv_sec,
381  delta.tv_usec/1000);
382 
383  /***************************************************************************
384  join a multicast group
385  ***************************************************************************/
387 
388  /***************************************************************************
389  join another multicast group
390  ***************************************************************************/
392 // multicast_join_group (udp_socket, MCAST_IP_ADDR2, MCAST_PORT2);
393 
394  /***************************************************************************
395  main program loop: set main_loop to 0 to exit loop
396  ***************************************************************************/
397  CPuts ("\r\n Entering main loop ...\r\n");
398  while ( main_loop )
399  {
400  /***********************************************************************
401  process net_isq()
402  ***********************************************************************/
403  net_isq (); // process ISQ
404 
405  /***********************************************************************
406  monitor link status
407  ***********************************************************************/
409 
410  /***********************************************************************
411  try to detect IP assignment
412  if DHCP is used, the assigned IP address may change
413  ***********************************************************************/
415 
416  /***********************************************************************
417  show that the program is running, perform symbol animation
418  ***********************************************************************/
420  }
421 
422  /***************************************************************************
423  exit program, shut down peripherals
424  ***************************************************************************/
425  return (0);
426 }
#define prg_exit(s)
Definition: Common.h:267
void BoardEnableInterrupts(void)
global enable interrupts
Definition: BoardSpecific.c:365
void socket_define_callback(SOCKET *so, int32_t(*call_back_function)(SOCKET *, void *, uint32_t, uint32_t), void *data, uint16_t maxdata)
Install a user callback function for a specific socket.
#define MCAST_IP_ADDR
Definition: multicast.c:120
#define ANIMATE_SYMBOLS_COUNT
Definition: BoardSpecific.h:268
void StartSystemTimer(void)
start system timer
Definition: timer.c:447
#define RES_MSECONDS
Definition: timer.h:67
uint16_t CPrintf_select_output(uint16_t device)
Definition: cprintf.c:206
#define ASSERT_HEAP(i, h)
Definition: Common.h:262
#define ANY_PORT
Definition: net.h:519
#define NULL
Definition: net.h:126
time_t GetSystemTimerRes(void)
Definition: timer.c:160
#define GET_CLOCK
Definition: BoardSpecific.h:258
#define UDP_INIT_FUNC
Definition: net.h:500
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
void SetupSystemTime(int32_t cpuint, int port, time_t resolution)
Setup System Time.
Definition: timer.c:392
void AppInit(uint32_t dsp_clock)
Initialize application.
Definition: Common.c:230
uint32_t initial_heap_size
Definition: Common.c:140
#define DSP_PORT
Definition: multicast.c:124
#define CPRINTF_DEFAULT_OUTPUT
Definition: BoardSpecific.h:129
#define VT100_RED
Definition: cprintf.h:143
uint16_t CPrintAnimatedSymbol(char *c, time_t period, size_t size)
Definition: cprintf.c:816
#define MCAST_PORT2
Definition: multicast.c:123
int InitializeNetwork(uint16_t icmp_size)
Initialize MAC, sockets and protocols.
Definition: BoardSpecific.c:597
#define MCAST_PORT
Definition: multicast.c:122
char sym_animate[]
Definition: BoardSpecific.c:129
SOCKET * socket_open(char *dest_addr, uint16_t dest_port, uint16_t src_port, uint8_t data_type, int32_t(*init_func)(SOCKET *))
Create a new socket.
char * udp_data
Definition: multicast.c:147
Definition: timer.h:75
#define UDP_MAX_PACKET_SIZE
Definition: net.h:671
#define GetTimeStamp()
Definition: timer.h:81
int32_t udp_call_back(SOCKET *so, void *data, uint32_t len, uint32_t ec)
Definition: multicast.c:168
void tv_interval(timeval *e, timeval *t1, timeval *t2)
compute elapsed time
Definition: timer.c:336
#define ANY_ADDRESS
Definition: net.h:511
#define SYSTEM_TIMER_INT
Definition: BoardSpecific.h:174
#define SYSTEM_TIMER
Definition: BoardSpecific.h:167
#define DATATYPE_CHAR
Definition: net.h:489
void net_isq(void)
The main polling function for processing sockets, must be periodically called in the main application...
#define BLANK_REV
Definition: BoardSpecific.h:191
Uint16 SystemTimerDev
#define INVALID_SOCKET
Definition: net.h:540
uint16_t monitor_ip_address(tpOutputFunc pLog)
monitor IP assignment
Definition: BoardSpecific.c:545
int32_t multicast_join_group(SOCKET *so, char *maddr, uint16_t mport)
Join a multicast group.
#define START_UP_MESSAGE(rev)
Definition: BoardSpecific.h:192
#define RES_SECONDS
Definition: timer.h:70
uint16_t monitor_link_status(tpOutputFunc pLog)
monitor link status change
Definition: BoardSpecific.c:497
int CPuts(const char *_ptr)
Definition: cprintf.c:399
SOCKET * udp_socket
Definition: multicast.c:146
time_t tv_sec
Definition: timer.h:77
#define CPrintfProgress(s)
Definition: cprintf.h:230
time_t tv_usec
Definition: timer.h:78
#define CPrintfProgressSuccess()
Definition: cprintf.h:263

Variable Documentation

char* program_name = "multicast"
SOCKET* udp_socket
Examples:
Multicast.c, and Send.c.
char* udp_data