hs2, I didn’t know that. Here it is again.
uint8_t ucMACAddress[6] = { 0x00, 0x0A, 0x35, 0xE5, 0xE5, 0xE5 };
uint8_t ucIPAddress[4] = { 192, 168, 1, 69 };
uint8_t ucNetMask[4] = { 255, 255, 255, 0 };
uint8_t ucGatewayAddress[4] = { 10, 10, 10, 1 };
uint8_t ucDNSServerAddress[4] = { 208, 67, 222, 222 };
uint16_t usPort = 45678;
const BaseType_t xBacklog = 20;
NetworkInterface_t xInterfaces[ 1 ];
NetworkEndPoint_t xEndPoints[ 4 ];
Socket_t xListeningSocket;
Socket_t xConnectedSocket;
int main()
{
pxZynq_FillInterfaceDescriptor(0, &( xInterfaces[0]));
FreeRTOS_FillEndPoint( &( xInterfaces[ 0 ] ), &( xEndPoints[ 0 ] ), ucIPAddress,
ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
FreeRTOS_IPInit_Multi();
vTaskStartScheduler();
}
void vApplicationIPNetworkEventHook_Multi( eIPCallbackEvent_t eNetworkEvent, struct xNetworkEndPoint * pxEndPoint )
{
static BaseType_t xTasksAlreadyCreated = pdFALSE;
printf("vApplicationIPNetworkEventHook_Multi\n\r");
/* Check this was a network up event, as opposed to a network down event. */
if( eNetworkEvent == eNetworkUp ) {
/* Create the tasks that use the TCP/IP stack if they have not already been created. */
if( xTasksAlreadyCreated == pdFALSE ) {
/* Create the tasks here. */
xTaskCreate( NetworkUpTask, /* The function that implements the task. */
"NetworkUpTask", /* Just a text name for the task to aid debugging. */
ipconfigIP_TASK_STACK_SIZE_WORDS, /* The stack size is defined in FreeRTOSIPConfig.h. */
NULL, /* The task parameter, not used in this case. */
ipconfigIP_TASK_PRIORITY, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
NULL ); /* The task handle is not used. */
xTasksAlreadyCreated = pdTRUE;
FreeRTOS_printf( ( "Application idle hook network up\n" ) );
}
}
else {
FreeRTOS_printf( ( "Application idle hook network down\n" ) );
}
}
static void NetworkUpTask( void * pvParameters )
{
struct freertos_sockaddr xBindAddress;
struct freertos_sockaddr xClient;
socklen_t xSize = sizeof( xClient );
static const TickType_t xReceiveTimeOut = portMAX_DELAY;
const BaseType_t xBacklog = 20;
static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 2000 );
static char cRXData[ RX_IP_BUFFER_SIZE ];
BaseType_t lBytesReceived;
BaseType_t rv;
printf("NetworkUpTask\n\r");
xListeningSocket = FreeRTOS_socket( FREERTOS_AF_INET4,
FREERTOS_SOCK_STREAM,
FREERTOS_IPPROTO_TCP );
configASSERT( xListeningSocket != FREERTOS_INVALID_SOCKET );
if(xListeningSocket != FREERTOS_INVALID_SOCKET) {
printf("Listening Socket created!\n\r");
}
else {
printf("ERROR: Listening Socket could not be created!\n\r");
}
rv = FreeRTOS_setsockopt( xListeningSocket,
0,
FREERTOS_SO_RCVTIMEO,
&xReceiveTimeOut,
sizeof( xReceiveTimeOut ) );
if(rv == 0) {
printf("FreeRTOS_setsockopt FREERTOS_SO_RCVTIMEO set.\n\r");
}
else {
printf("ERROR: FreeRTOS_setsockopt FREERTOS_SO_RCVTIMEO failed.\n\r");
}
rv = FreeRTOS_setsockopt( xListeningSocket,
0,
FREERTOS_SO_SNDTIMEO,
&xSendTimeOut,
sizeof( xSendTimeOut ) );
if(rv == 0) {
printf("FreeRTOS_setsockopt FREERTOS_SO_SNDTIMEO set.\n\r");
}
else {
printf("ERROR: FreeRTOS_setsockopt FREERTOS_SO_SNDTIMEO failed.\n\r");
}
memset( &xBindAddress, 0 , sizeof(xBindAddress) );
xBindAddress.sin_len = sizeof(xBindAddress);
xBindAddress.sin_family = FREERTOS_AF_INET;
xBindAddress.sin_flowinfo = 0; // not used?
xBindAddress.sin_port = ( uint16_t ) usPort;
xBindAddress.sin_port = FreeRTOS_htons( xBindAddress.sin_port );
xBindAddress.sin_address.ulIP_IPv4 = FreeRTOS_inet_addr_quick(ucIPAddress[0], ucIPAddress[1], ucIPAddress[2], ucIPAddress[3]);
rv = FreeRTOS_bind( xListeningSocket, &xBindAddress, sizeof( xBindAddress ) );
if(rv == 0) {
printf("FreeRTOS_bind good.\n\r");
}
else {
printf("ERROR: FreeRTOS_bind failed.\n\r");
}
rv = FreeRTOS_listen( xListeningSocket, xBacklog );
printf("FreeRTOS_listen: %ld\n\r", rv);\
for( ; ; )
{
xConnectedSocket = FreeRTOS_accept( xListeningSocket, &xClient, &xSize );
configASSERT( xConnectedSocket != FREERTOS_INVALID_SOCKET );
if(xConnectedSocket != FREERTOS_INVALID_SOCKET) {
printf("FreeRTOS_accept good.\n\r");
}
else {
printf("ERROR: FreeRTOS_accept failed.\n\r");
}
lBytesReceived = FreeRTOS_recv( xConnectedSocket, &cRXData, RX_IP_BUFFER_SIZE, 0 );
if( lBytesReceived > 0 )
{
/* Data was received, process it here. */
printf("TCP RX: %s\n\r", cRXData);
}
else if( lBytesReceived == 0 )
{
/* No data was received, but FreeRTOS_recv() did not return an error.
Timeout? */
}
else
{
/* Error (maybe the connected socket already shut down the socket?).
Attempt graceful shutdown. */
FreeRTOS_shutdown( xListeningSocket, FREERTOS_SHUT_RDWR );
break;
}
}
}