chat.c File Reference

UDP chat 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>

Functions

int main (void)
 

Variables

char * program_name = "chat"
 
char * udp_data
 
SOCKET * udp_tx
 
SOCKET * udp_rx
 

Detailed Description

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

Function Documentation

int main ( void  )
145 {
146  /***************************************************************************
147  locals
148  ***************************************************************************/
149  int main_loop = 1; /* main loop switch, set to 0 to exit */
150  timeval stamp1, stamp2, delta; /* used to determine startup time */
151  int32_t len; /* received data length */
152 
153  /***************************************************************************
154  initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
155  (ref. \Common\Common.c)
156  ***************************************************************************/
157  AppInit (GET_CLOCK);
158 
159  /***************************************************************************
160  select output device for CPrintf (ref. \Common\cprintf.c)
161  possible settings:
162  CPRINTF_UART_OUTPUT -> output to UART
163  CPRINTF_CCS_OUTPUT -> output to CCS
164  CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
165  ***************************************************************************/
166  CPrintf_select_output (CPRINTF_DEFAULT_OUTPUT); /* default outputs */
167 
168  /***************************************************************************
169  print a start up message
170  ***************************************************************************/
171  START_UP_MESSAGE (BLANK_REV NETLIB_REV);
172 
173  /**************************************************************************/
174  // CPrintfProgress (" Heap check ");
175  // at least 0x2000 bytes required for this app
176  /**************************************************************************/
177  ASSERT_HEAP (initial_heap_size, 0x2000);
178  // CPrintfProgressSuccess();
179 
180  /***************************************************************************
181  malloc space for data
182  ***************************************************************************/
183  CPrintfProgress (" Try to allocate data buffer ");
184  udp_data = (char *) malloc (UDP_MAX_PACKET_SIZE * sizeof(char));
185  if ( udp_data == NULL )
186  {
187  prg_exit ("out of memory error");
188  }
190 
191  /**************************************************************************/
192  CPrintfProgress (" Setup system time ");
193  // 1 milli seconds resolution
194  /**************************************************************************/
197  CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
199 
200  /**************************************************************************/
201  CPrintfProgress (" Enable interrupts ");
202  /**************************************************************************/
205 
206  /**************************************************************************/
207  CPrintfProgress (" Start system timer ");
208  /**************************************************************************/
209  StartSystemTimer ();
211  CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
212 
213  /***************************************************************************
214  measure network initialization time
215  ***************************************************************************/
216  stamp1 = GetTimeStamp();
217 
218  /**************************************************************************/
219  CPrintfProgress (" Initialize network ");
220  /**************************************************************************/
221  InitializeNetwork ( 64); // 64 bytes for ping
222 
223  /**************************************************************************
224  open sockets:
225  **************************************************************************/
227  5061,
228  5031,
230  UDP_INIT_FUNC);
231  if ( udp_tx == INVALID_SOCKET )
232  {
233  prg_exit ("socket_open() error"); /* possibly insufficient heap */
234  } // if
235 
237  5062,
238  5032,
240  UDP_INIT_FUNC);
241  if ( udp_rx == INVALID_SOCKET )
242  {
243  prg_exit ("socket_open() error"); /* possibly insufficient heap */
244  } // if
245 
246  /***************************************************************************
247  set receive buffer
248  ***************************************************************************/
251 
252  stamp2 = GetTimeStamp();
253 
254  tv_interval (&delta, &stamp1, &stamp2);
255  CPuts (" network startup time [sec]: ");
257  "%"PRId32".%03"PRId32"\r\n"
259  delta.tv_sec,
260  delta.tv_usec/1000);
261 
262 
263  /***************************************************************************
264  main program loop: set main_loop to 0 to exit loop
265  ***************************************************************************/
266  CPuts ("\r\n Entering main loop ...");
267  while ( main_loop )
268  {
269  /***********************************************************************
270  process net_isq()
271  ***********************************************************************/
272  net_isq ();
273 
274  /***********************************************************************
275  monitor link status
276  ***********************************************************************/
278 
279  /***********************************************************************
280  try to detect IP assignment
281  if DHCP is used, the assigned IP address may change
282  ***********************************************************************/
284 
286  {
287  // len = net_recv_event_handler (udp_rx, CPrintf); /* with messages */
288  len = net_recv_event_handler (udp_rx, NULL); /* without messages */
289  if (len)
290  {
291  /***************************************************************
292  udp_tx is configured for "ANY_ADDRESS", that means, the
293  destination address is unknown. If you want to send the
294  received message on udp_rx back to sender, you have to
295  copy the destination address information from udp_rx to udp_tx:
296  ***************************************************************/
297  udp_tx-> dest_addr = udp_rx-> dest_addr;
298 
299  /***************************************************************
300  send message back to sender
301  ***************************************************************/
302  net_send_safe (udp_tx, udp_data, len, 0);
303  }
304  }
305 
306  /***********************************************************************
307  show that the program is running, perform symbol animation
308  ***********************************************************************/
310  }
311 
312  /***************************************************************************
313  exit program, shut down peripherals
314  ***************************************************************************/
315  return (0);
316 }
#define prg_exit(s)
Definition: Common.h:267
int32_t set_recv_buffer(SOCKET *so, void *data, uint16_t maxdatasize)
Define a socket buffer.
void BoardEnableInterrupts(void)
global enable interrupts
Definition: BoardSpecific.c:365
#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 NULL
Definition: net.h:126
int32_t net_recv(SOCKET *so, void *data, uint16_t maxdatasize)
Receive data via the specified socket.
time_t GetSystemTimerRes(void)
Definition: timer.c:160
#define GET_CLOCK
Definition: BoardSpecific.h:258
#define UDP_INIT_FUNC
Definition: net.h:500
SOCKET * udp_tx
Definition: chat.c:125
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 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
int InitializeNetwork(uint16_t icmp_size)
Initialize MAC, sockets and protocols.
Definition: BoardSpecific.c:597
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.
Definition: timer.h:75
#define UDP_MAX_PACKET_SIZE
Definition: net.h:671
#define GetTimeStamp()
Definition: timer.h:81
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
SOCKET * udp_rx
Definition: chat.c:126
int int32_t
Definition: stdint.h:46
#define INVALID_SOCKET
Definition: net.h:540
uint16_t monitor_ip_address(tpOutputFunc pLog)
monitor IP assignment
Definition: BoardSpecific.c:545
#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
char * udp_data
Definition: chat.c:123
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 = "chat"
char* udp_data
Examples:
Chat.c, Multicast.c, and Send.c.
SOCKET* udp_tx
Examples:
Chat.c.
SOCKET* udp_rx
Examples:
Chat.c.