Send.c

Send.c example details
UDP details

/***************************************************************************//**
@file send.c
@brief UDP send test program
@verbatim
_ _ _
__| | ___(_) ____ _ __ | |_
/ _` | / __| |/ _` | '_ \| __|
| (_| | _ \__ \ | (_| | | | | |_
\__,_|(_) ___/_|\__, |_| |_|\__|
Signalprocessing |___/ Technology
@endverbatim
@author D.SignT GmbH & Co. KG, Claus Hermbusche
@date 2019-06-03
@anchor SENDEX
@details
This is a simple DSP program for sending a time stamp to the specified pc
address and port using UDP. For proper function you have to specify your
PC IP- address by definition:
#define PC_IP_ADDR "192.168.168.8"
Or if you want to send broadcast messages use
#define PC_IP_ADDR BROADCAST
@cond Software License Agreement
Copyright (C) 2001-2019 D.SignT GmbH & Co. KG - http://www.dsignt.de
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the
distribution.
Neither the name of D.SignT GmbH & Co. KG nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
Disclaimer
THIS SOFTWARE IS PROVIDED BY D.SIGNT GMBH & CO. KG "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL D.SIGNT GMBH & CO. KG BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@endcond
*******************************************************************************/
/*******************************************************************************
include stdtypes.h to avoid data type mismatch
*******************************************************************************/
/*******************************************************************************
include Runtime Source
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
/*******************************************************************************
network support functions
*******************************************************************************/
#include <Libs/NETlib/net.h> /* D.Module network support */
/*******************************************************************************
board specific functions
*******************************************************************************/
#include <BoardSupport/inc/BoardSpecific.h> /* board support functions */
/*******************************************************************************
common support functions
*******************************************************************************/
#include <Common/Common.h> /* on exit function */
#include <Common/uartio.h> /* UART support */
#include <Common/timer.h> /* timer setting */
#include <Common/CPrintf.h> /* CPrintf defines */
/*******************************************************************************
network configuration
*******************************************************************************/
#include <BoardSupport/config/netconfig.c> /* network configuration */
#ifdef __cplusplus
extern "C" {
#endif /* !__cplusplus */
/*******************************************************************************
local prototypes
*******************************************************************************/
#pragma CODE_SECTION(SendTimeStamp , ".commontext")
void SendTimeStamp (void);
#ifdef __cplusplus
} // extern "C"
#endif
/*******************************************************************************
* *
* DEFINES *
* *
*******************************************************************************/
/*******************************************************************************
IP and port addresses:
*******************************************************************************/
// #define PC_IP_ADDR "192.168.168.8" /* this must match your PC IP address */
// #define PC_IP_ADDR BROADCAST /* activate, if you want to send broadcast messages*/
#define PC_IP_ADDR "192.168.168.255" /* activate, if you want to send broadcast messages*/
#define PC_PORT 5062
#define DSP_PORT 5032
/*******************************************************************************
* *
* GLOBALS *
* *
*******************************************************************************/
/*******************************************************************************
program name
*******************************************************************************/
char *program_name = "send";
SOCKET *udp_socket; /* socket descriptor */
char udp_data[12];
/*******************************************************************************
* *
* FUNCTIONS *
* *
*******************************************************************************/
/*******************************************************************************
@brief Send a time stamp
@param -
@return -
*******************************************************************************/
void SendTimeStamp (void)
{
/***************************************************************************
locals
***************************************************************************/
//~ char udp_data[12]; /* !!data buffer must not reside on the stack !! */
int32_t ret;
struct tm *local_time; /* structure to hold time and date */
if ( udp_socket-> error_code || (get_ip_address (0) == UINT32_C(0)) || !_link)
{
/***********************************************************************
in case of errors or no link do nothing
***********************************************************************/
return;
} // if
/***************************************************************************
generate a new time stamp each second
***************************************************************************/
local_time = localtime ((const time_t *)&RecentEpoch);
/***************************************************************************
fill send buffer with time stamp data
***************************************************************************/
udp_data[0] = (local_time->tm_hour / 10) + '0';
udp_data[1] = (local_time->tm_hour % 10) + '0';
udp_data[3] = (local_time->tm_min / 10) + '0';
udp_data[4] = (local_time->tm_min % 10) + '0';
udp_data[6] = (local_time->tm_sec / 10) + '0';
udp_data[7] = (local_time->tm_sec % 10) + '0';
/***************************************************************************
send time stamp to PC
***************************************************************************/
ret = net_send (udp_socket, /* socket */
udp_data, /* data to send */
10); /* length of data */
if (ret == INT32_C(0))
{
if (( udp_socket-> error_code & SO_ERROR_MASK) == SO_NO_ARP_ADDRESS)
{
net_print_error (udp_socket-> error_code, CPrintf);
// Destination Address not resolved. Packet was passed to the TCP/IP stack
// and will be transmitted automatically when address resolution
// in background was successful. This happens always with the first packet
// to a new destination or when the ARP cache is cleared after a certain time.
// Make sure to trigger the net_isq() for address resolution.
// Note: don't change data content until this packet was transmitted
// by the TCP/IP stack. This constraint exists due to the zero-copy mechanism;
// the stack needs to access the data pointer later.
// This is also the reason for the globally defined array udp_data[]. If this
// buffer would have been defined locally, the data content is invalid after
// return from this function, since all local data is stored on the stack
}
}
}
/*******************************************************************************
@brief Main application
@param -
@return never
*******************************************************************************/
#pragma CODE_SECTION(main , ".commontext");
int main ( void )
{
/***************************************************************************
locals
***************************************************************************/
int main_loop = 1; /* main loop switch, set to 0 to exit */
timeval stamp1, stamp2, delta; /* used to determine startup time */
time_t seconds;
char buffer[20]; /* small buffer for ip-address conversion */
struct tm *local_time; /* structure to hold time and date */
/***************************************************************************
initialize application (e.g. timer clocks, PLL settings, EMIF etc.)
(ref. \Common\Common.c)
***************************************************************************/
/***************************************************************************
select output device for CPrintf (ref. \Common\cprintf.c)
possible settings:
CPRINTF_UART_OUTPUT -> output to UART
CPRINTF_CCS_OUTPUT -> output to CCS
CPRINTF_UART_OUTPUT | CPRINTF_CCS_OUTPUT -> output to UART and CCS
***************************************************************************/
/***************************************************************************
print a start up message
***************************************************************************/
/**************************************************************************/
// CPrintfProgress (" Heap check ");
// at least 0x2000 bytes required for this app
/**************************************************************************/
// CPrintfProgressSuccess();
/**************************************************************************/
CPrintfProgress (" Setup system time ");
// 1 milli seconds resolution
/**************************************************************************/
CPrintf (" *** timer %d mapped to CPU int %d ***\r\n",
/**************************************************************************/
CPrintfProgress (" Enable interrupts ");
/**************************************************************************/
/**************************************************************************/
CPrintfProgress (" Start system timer ");
/**************************************************************************/
CPrintf (" *** timer %d running at %"PRId32" Hz ***\r\n", SystemTimerDev, RES_SECONDS/GetSystemTimerRes());
/***************************************************************************
measure network initialization time
***************************************************************************/
stamp1 = GetTimeStamp();
/**************************************************************************/
CPrintfProgress (" Initialize network ");
/**************************************************************************/
InitializeNetwork ( 64); // 64 bytes for ping
/***************************************************************************
open socket
***************************************************************************/
udp_socket = socket_open (PC_IP_ADDR, /* fix pc address */
PC_PORT, /* destination port */
DSP_PORT, /* source port */
DATATYPE_CHAR, /* data type char */
UDP_INIT_FUNC); /* udp protocol */
{
prg_exit ("socket_open() failed"); /* possibly insufficient heap */
} // if
stamp2 = GetTimeStamp();
tv_interval (&delta, &stamp1, &stamp2);
CPuts (" network startup time [sec]: ");
"%"PRId32".%03"PRId32"\r\n"
delta.tv_sec,
delta.tv_usec/1000);
udp_data[2] = ':';
udp_data[5] = ':';
udp_data[8] = '\r';
udp_data[9] = '\n';
udp_data[10] = '\0';
CPrintf (" Sending time stamp to address %s on port %d\r\n", inet_ntoa (udp_socket->dest_addr, buffer), udp_socket->dest_port);
local_time = localtime ((const time_t *)&RecentEpoch);
CPrintf (" System start time: %s\r", asctime (local_time));
LED_on (0);
LED_off (1);
/***************************************************************************
main program loop: set main_loop to 0 to exit loop
***************************************************************************/
CPuts ("\r\n Entering main loop ...\r\n");
while ( main_loop )
{
/***********************************************************************
process net_isq()
***********************************************************************/
net_isq (); // process ISQ
/***********************************************************************
monitor link status
***********************************************************************/
/***********************************************************************
try to detect IP assignment
if DHCP is used, the assigned IP address may change
***********************************************************************/
/***********************************************************************
since no receive function is used in this example, some error codes
for receive events may block the send process.
***********************************************************************/
udp_socket -> error_code = UINT32_C(0);
if (seconds != RecentEpoch.tv_sec)
{
seconds = RecentEpoch.tv_sec;
/*******************************************************************
send time stamp each second
*******************************************************************/
}
/***********************************************************************
show that the program is running, perform symbol animation
***********************************************************************/
}
/***************************************************************************
exit program, shut down peripherals
***************************************************************************/
return(0);
}