dspbios_echo.c File Reference

DSP/BIOS TCP-Echo server. 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>
#include "dspbios_echocfg.h"
#include "nettask.h"

Macros

#define USE_TCP_CALLBACK   1
 
#define BUFFER_SIZE   (TCP_MAX_PACKET_SIZE)
 
#define KA_TIME(x)    ((uint32_t)((x)*330000/PRD1.period))
 

Functions

int32_t tcp_echo (SOCKET *so, void *data, uint32_t len, uint32_t ec)
 
void set_net_isq_interval (int32_t net_isq_interval)
 
void net_trigger (u_int32_t ms)
 
Void Create_TCP_Echo_Server (void)
 
void Echo_Task (void)
 
Void main (Void)
 

Variables

char * program_name = "Echo2"
 
SOCKET * tcp_echo_so = INVALID_SOCKET
 
unsigned char tcpRecvBuffer [BUFFER_SIZE]
 
unsigned char tcpSendBuffer [BUFFER_SIZE]
 
unsigned char * tx
 

Detailed Description

                          _         _             _
                       __| |    ___(_) ____ _ __ | |_
                      / _` |   / __| |/ _` | '_ \| __|
                     | (_| | _ \__ \ | (_| | | | | |_
                      \__,_|(_) ___/_|\__, |_| |_|\__|
                     Signalprocessing |___/ Technology
Author
D.SignT GmbH & Co. KG, Claus Hermbusche
Date
2019-06-03

To verify this program, first 'ping' the DSP:

 >ping 192.168.168.200

or

 >ping mydemo

If the settings and network connections are correct, the pings will be replied.

The TCP Echo server will re-send (echo) anything it receives. To verify this, start NetCat (nc) with the following command

 >nc 192.168.168.200 5031
            |         |
            |         +-- port 5031 = User Echo Port
            +------------ the DSP's IP address

or

 >nc mydemo 5031
       |     |
       |     +------ port 5031 = User Echo Port
       +------------ the DSP hostname

Any characters you type in the console window will be returned. NetCat doesn't send the characters immediately, it will send the entire string following a terminating CR. Please note that the maximum length of the character string is limited by the size of the buffer used for TCP echo (ECHO_BUFFER_SIZE = 1460).

The net_isq() is activated via semaphore NetSem from PRD every 1 ms.

Macro Definition Documentation

#define USE_TCP_CALLBACK   1
#define BUFFER_SIZE   (TCP_MAX_PACKET_SIZE)
#define KA_TIME (   x)    ((uint32_t)((x)*330000/PRD1.period))

Function Documentation

int32_t tcp_echo ( SOCKET *  so,
void *  data,
uint32_t  len,
uint32_t  ec 
)
Examples:
PServer.c.
224 {
225  /***************************************************************************
226  locals
227  ***************************************************************************/
228  int32_t data_length; // net_recv_event_handler() return parameter
229 
230  /***************************************************************************
231  suppress unused parameter warning
232  ***************************************************************************/
233  UNREFERENCED_PARAMETER(data); // not used
234  UNREFERENCED_PARAMETER(ec); // not used
235  UNREFERENCED_PARAMETER(len); // len is ignored, data_length is used instead
236 
237  /***************************************************************************
238  it is recommended not to use printf outputs in callback functions
239  ***************************************************************************/
240  data_length = net_recv_event_handler (so, CPrintf); /* with messages */
241  // data_length = net_recv_event_handler (so, NULL); /* without messages */
242 
243  if (data_length)
244  {
245  /***********************************************************************
246  new data was received, copy to send buffer
247  ***********************************************************************/
248  memcpy (tx, tcpRecvBuffer, data_length);
249  tx+=data_length;
250  } // if
251 
252  /***************************************************************************
253  return true, if message processed
254  ***************************************************************************/
255  return (SOCKET_CB_OK);
256 }
unsigned char * tx
Definition: dspbios_echo.c:201
unsigned char tcpRecvBuffer[BUFFER_SIZE]
Definition: dspbios_echo.c:194
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
int int32_t
Definition: stdint.h:46
#define UNREFERENCED_PARAMETER(P)
Definition: Common.h:115
#define SOCKET_CB_OK
Definition: net.h:576
void set_net_isq_interval ( int32_t  net_isq_interval)
void net_trigger ( u_int32_t  ms)
Void Create_TCP_Echo_Server ( void  )
267 {
268  /***************************************************************************
269  locals
270  ***************************************************************************/
271  /* - */
272 
273  /***************************************************************************
274  open sockets
275  ***************************************************************************/
277  ANY_PORT,
278  5031,
280  TCP_INIT_FUNC);
281  if ( tcp_echo_so == NULL )
282  {
283  CPrintf (CLRSCR
284  VT100_RED);
285  CPrintf("socket_open() failed, echo server not available\r\n");
286  CPrintf (CLRSCR
287  VT100_DEFAULT);
288  return;
289  } // if
290 
291 #ifdef USE_TCP_CALLBACK
292  /***************************************************************************
293  define callback function for received TCP packets
294  ***************************************************************************/
296 #endif
297 
298  /***************************************************************************
299  for DSP/BIOS projects the keep alive time depends on your net_isq() PRD
300  configuration. For this example the net_timer() PRD is configured for 1 ms.
301  Use macro KA_TIME(x) to specify a certain keep alive time,
302  e.g. KA_TIME(100) should result in a timeout of 100 s
303  ***************************************************************************/
304 #define KA_TIME(x) ((uint32_t)((x)*330000/PRD1.period))
306 
307 }
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.
int16_t tcp_set_keep_alive_time(SOCKET *so, uint32_t time)
Set keep alive time.
#define KA_TIME(x)
#define ANY_PORT
Definition: net.h:519
unsigned char tcpRecvBuffer[BUFFER_SIZE]
Definition: dspbios_echo.c:194
#define NULL
Definition: net.h:126
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
#define TCP_INIT_FUNC
Definition: net.h:499
#define VT100_RED
Definition: cprintf.h:143
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.
#define BUFFER_SIZE
Definition: dspbios_echo.c:164
SOCKET * tcp_echo_so
Definition: dspbios_echo.c:188
#define ANY_ADDRESS
Definition: net.h:511
#define DATATYPE_CHAR
Definition: net.h:489
#define CLRSCR
Definition: cprintf.h:164
int32_t tcp_echo(SOCKET *so, void *data, uint32_t len, uint32_t ec)
Definition: dspbios_echo.c:223
void Echo_Task ( void  )
319 {
320  /***************************************************************************
321  locals
322  ***************************************************************************/
323  int32_t data_length; // net_recv_event_handler() return parameter
324 
325  /***************************************************************************
326  create echo socket
327  ***************************************************************************/
329 
330  /***************************************************************************
331  check result
332  ***************************************************************************/
333  if ( tcp_echo_so == NULL )
334  {
335  CPrintf (CLRSCR
336  VT100_RED);
337  CPrintf(" closing echo task\r\n");
338  CPrintf (CLRSCR
339  VT100_DEFAULT);
340  return;
341  } // if
342 
343  CPrintf(" echo task running\r\n");
344 
345  /***************************************************************************
346  enter echo task
347  ***************************************************************************/
348  for (;;)
349  {
350  /***********************************************************************
351  yield to other tasks
352  ***********************************************************************/
353  TSK_yield();
354 
355  /***********************************************************************
356  reset tx buffer if not connected
357  ***********************************************************************/
359  {
360  tx = tcpSendBuffer;
361 
362  /*******************************************************************
363  set new buffer
364  *******************************************************************/
366  }
367 
368 #ifndef USE_TCP_CALLBACK
369  /***********************************************************************
370  check for received data
371  ***********************************************************************/
373  {
374  // data_length = net_recv_event_handler (tcp_echo_so, NULL); /* without messages */
375  data_length = net_recv_event_handler (tcp_echo_so, CPrintf); /* with messages */
376 
377  if (data_length)
378  {
379  /***************************************************************
380  new data was received, copy to send buffer
381  ***************************************************************/
382  memcpy (tx, tcpRecvBuffer, data_length);
383  tx+=data_length;
384  } // if
385  }
386 #endif
387 
388  /***********************************************************************
389  check for new data in output buffer
390  ***********************************************************************/
391  data_length = tx - tcpSendBuffer;
392  if ( data_length )
393  {
394 #ifdef USE_TCP_CALLBACK
395  /*******************************************************************
396  disable receive callback to avoid buffer overrun
397  *******************************************************************/
399 #endif
400  /*******************************************************************
401  echo back data, wait for completion
402  *******************************************************************/
403  net_send_safe (tcp_echo_so, tcpSendBuffer, data_length, TCP_WAIT);
404  tx = tcpSendBuffer; /* reset buffer */
405 
406 #ifdef USE_TCP_CALLBACK
407  /*******************************************************************
408  enable callback function for received TCP packets
409  *******************************************************************/
411 #endif
412 
413  } // if
414  }
415 }
int32_t set_recv_buffer(SOCKET *so, void *data, uint16_t maxdatasize)
Define a socket buffer.
unsigned char * tx
Definition: dspbios_echo.c:201
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.
unsigned char tcpRecvBuffer[BUFFER_SIZE]
Definition: dspbios_echo.c:194
int16_t tcp_get_state(SOCKET *so)
Determine tcp state.
#define NULL
Definition: net.h:126
int32_t net_recv(SOCKET *so, void *data, uint16_t maxdatasize)
Receive data via the specified socket.
int CPrintf(const char *_format,...)
Custom printf function.
Definition: cprintf.c:708
#define VT100_DEFAULT
Definition: cprintf.h:150
#define VT100_RED
Definition: cprintf.h:143
#define BUFFER_SIZE
Definition: dspbios_echo.c:164
SOCKET * tcp_echo_so
Definition: dspbios_echo.c:188
#define TCP_ESTABLISHED
Definition: net.h:693
unsigned char tcpSendBuffer[BUFFER_SIZE]
Definition: dspbios_echo.c:200
Void Create_TCP_Echo_Server(void)
Definition: dspbios_echo.c:266
#define CLRSCR
Definition: cprintf.h:164
int int32_t
Definition: stdint.h:46
int32_t tcp_echo(SOCKET *so, void *data, uint32_t len, uint32_t ec)
Definition: dspbios_echo.c:223
Void main ( Void  )
432 {
433  /***************************************************************************
434  locals
435  ***************************************************************************/
436  /* - */
437 
438  /***************************************************************************
439  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
440  (ref. \Common\Common.c)
441  ***************************************************************************/
442  AppInit (GET_CLOCK);
443 
444  /***************************************************************************
445  select output device for CPrintf (ref. \Common\cprintf.c)
446  possible settings:
447  CPRINTF_UART_OUTPUT -> output to UART
448  CPRINTF_CCS_OUTPUT -> output to CCS
449  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
450  ***************************************************************************/
451  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
452 
453  /***************************************************************************
454  print a start up message
455  ***************************************************************************/
456  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
457 
458  /***************************************************************************
459  perform HEAP check; at least 0x2000 bytes required for this app
460  ***************************************************************************/
461  CPrintfProgress (" Perform Heap check ");
462  ASSERT_HEAP (initial_heap_size, 0x2000);
464 
465  /**************************************************************************/
466  CPrintfProgress (" Initialize network ");
467  /**************************************************************************/
468  InitializeNetwork ( 64); // 64 bytes for ping
469 
470  /***************************************************************************
471  set a shorter net_isq() interval for faster retransmits
472  ***************************************************************************/
473  // set_net_isq_interval(10);
475 
476  /***************************************************************************
477  create main network task
478  ***************************************************************************/
479  create_net_task( Net_Task, "Net_Task", 3, 0x800, 0, 0, 0);
480  create_net_task( Echo_Task, "Echo_Task", 3, 0x800, 0, 0, 0);
481 
482 #ifdef USE_NET_IRQ
483  /***************************************************************************
484  map EMAC event to CPU interrupt 7 (valid: 4..15)
485  install EMAC interrupt service and enable interrupt
486  ***************************************************************************/
487  IRQ_map (IRQ_EVT_MACINT, 7);
488  HWI_dispatchPlug(7, (Fxn)inc_net_sem, -1, NULL);
489  IRQ_enable(IRQ_EVT_MACINT);
490 #endif
491 
492 }
uint16_t CPrintf_select_output(uint16_t device)
Definition: cprintf.c:206
#define ASSERT_HEAP(i, h)
Definition: Common.h:262
#define NULL
Definition: net.h:126
#define GET_CLOCK
Definition: BoardSpecific.h:258
void AppInit(uint32_t dsp_clock)
Initialize application.
Definition: Common.c:230
uint32_t initial_heap_size
Definition: Common.c:140
#define CPRINTF_DEFAULT_OUTPUT
Definition: BoardSpecific.h:129
int InitializeNetwork(uint16_t icmp_size)
Initialize MAC, sockets and protocols.
Definition: BoardSpecific.c:597
#define BLANK_REV
Definition: BoardSpecific.h:191
void Echo_Task(void)
Definition: dspbios_echo.c:318
#define START_UP_MESSAGE(rev)
Definition: BoardSpecific.h:192
#define CPrintfProgress(s)
Definition: cprintf.h:230
#define CPrintfProgressSuccess()
Definition: cprintf.h:263

Variable Documentation

char* program_name = "Echo2"
SOCKET* tcp_echo_so = INVALID_SOCKET
unsigned char tcpRecvBuffer[BUFFER_SIZE]
unsigned char tcpSendBuffer[BUFFER_SIZE]
unsigned char* tx