Untitled diff

Created Diff never expires
168 removals
342 lines
48 additions
222 lines
/**
******************************************************************************
* @file network.c
* @author MCD Application Team
* @version V1.0.1
* @date 12-April-2017
* @brief network interface implementation for STM32F769-Discovery ethernet.
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
* All rights reserved.</center></h2>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted, provided that the following conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. 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.
* 3. Neither the name of STMicroelectronics nor the names of other
* contributors to this software may be used to endorse or promote products
* derived from this software without specific written permission.
* 4. This software, including modifications and/or derivative works of this
* software, must execute solely and exclusively on microcontroller or
* microprocessor devices manufactured by or for STMicroelectronics.
* 5. Redistribution and use of this software other than as permitted under
* this license is void and will automatically terminate your rights under
* this license.
*
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
* SHALL STMICROELECTRONICS OR CONTRIBUTORS 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.
*
******************************************************************************
*/

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include <string.h>
#include <stdint.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdlib.h>








#include "lwip/dhcp.h"
#include "lwip/dhcp.h"
#include "lwip/tcpip.h"
#include "lwip/tcpip.h"
#include "lwip/netdb.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "lwip/sockets.h"
#include "netif/ethernet.h"
#include "netif/ethernet.h"


#include "ethernetif.h"
#include "ethernetif.h"
#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal.h"


#include "main.h"
#include "main.h"
#include "network.h"
#include "network.h"
#include "msg.h"
#include "msg.h"


//#define USE_DHCP
#ifdef USE_DHCP


#define IP_ADDR0 0
extern struct netif gnetif;
#define IP_ADDR1 0
#define IP_ADDR2 0
#define IP_ADDR3 0

#define GW_ADDR0 0
#define GW_ADDR1 0
#define GW_ADDR2 0
#define GW_ADDR3 0

#define MASK_ADDR0 0
#define MASK_ADDR1 0
#define MASK_ADDR2 0
#define MASK_ADDR3 0

#else

#define IP_ADDR0 192
#define IP_ADDR1 168
#define IP_ADDR2 0
#define IP_ADDR3 10

#define GW_ADDR0 192
#define GW_ADDR1 168
#define GW_ADDR2 0
#define GW_ADDR3 1

#define MASK_ADDR0 255
#define MASK_ADDR1 255
#define MASK_ADDR2 255
#define MASK_ADDR3 0

#endif /* USE_DHCP */

static struct netif Netif;

/**
* Initialize LwIP stack and get a dynamic IP address.
*/
int network_init(void)
{
ip4_addr_t addr;
ip4_addr_t netmask;
ip4_addr_t gw;
uint32_t start;
tcpip_init(NULL, NULL);

/* IP default settings, to be overridden by DHCP */

IP4_ADDR(&addr, IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
IP4_ADDR(&netmask, MASK_ADDR0, MASK_ADDR1, MASK_ADDR2, MASK_ADDR3);
/* add the network interface */
netif_add(&Netif, &addr, &netmask, &gw, NULL, , );

/* register the default network interface */
netif_set_default(&Netif);
netif_set_up(&Netif);

#ifdef USE_DHCP
dhcp_start(&Netif);
#endif

start = HAL_GetTick();
while((Netif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
{
}
if (Netif.ip_addr.addr == 0)
{
msg_info(" Failed to get IP address! Please check your network configuration.\n");
Error_Handler();
}
else
{
msg_info("\nIpAdress = %lu.%lu.%lu.%lu\n", (Netif.ip_addr.addr & 0xff), ((Netif.ip_addr.addr >> 8) & 0xff)
, ((Netif.ip_addr.addr >> 16) & 0xff), ((Netif.ip_addr.addr >> 24)& 0xff));
#ifdef USE_DHCP
dhcp_stop(&Netif);
#endif
}
return 0;
}

/**
* @brief Only used in case of wifi.
*/
void network_credential(void)
{
}

/**
* @brief Get IP address.
* @param ipAddress pointer to a table of 4 bytes containing IP address
* @return 0 in case of success
*/
int network_get_ip_address(uint8_t *ipAddress)
int network_get_ip_address(uint8_t *ipAddress)
{
{
if (!ipAddress)
if (!ipAddress)
{
{
return -1;
return -1;
}
}
if (!Netif.ip_addr.addr)
if (!gnetif.ip_addr.addr)
{
{
return -1;
return -1;
}
}
ipAddress[0] = Netif.ip_addr.addr & 0xff;
ipAddress[0] = gnetif.ip_addr.addr & 0xff;
ipAddress[1] = (Netif.ip_addr.addr >> 8) & 0xff;
ipAddress[1] = (gnetif.ip_addr.addr >> 8) & 0xff;
ipAddress[2] = (Netif.ip_addr.addr >> 16) & 0xff;
ipAddress[2] = (gnetif.ip_addr.addr >> 16) & 0xff;
ipAddress[3] = (Netif.ip_addr.addr >> 24) & 0xff;
ipAddress[3] = (gnetif.ip_addr.addr >> 24) & 0xff;


return 0;
return 0;
}
}



int network_socket_recv(void *ctx, unsigned char *buf, size_t len)
int network_socket_recv(void *ctx, unsigned char *buf, size_t len)
{
{
int ret;
int ret;
int fd = (int) ctx;
int fd = (int) ctx;


ret = SUCCESS;
ret = SUCCESS;
if( fd < 0 )
if( fd < 0 )
{
{
printf("\n invalid context \n");
printf("\r\n invalid context \r\n");
ret = -1;
ret = -1;
return ret;
return ret;
}
}
/* ret = (int32_t) read( fd, buf, len ); */
/* ret = (int32_t) read( fd, buf, len ); */
ret = (int32_t) recv( fd, buf, len, MSG_DONTWAIT);
ret = (int32_t) recv( fd, buf, len, MSG_DONTWAIT);
if( ret < 0 )
if( ret < 0 )
{
{
if(errno == EWOULDBLOCK)
if(errno == EWOULDBLOCK)
{
{
ret = -1;
ret = -1;
return ret;
return ret;
}
}
if(errno == EPIPE || errno == ECONNRESET)
if(errno == EPIPE || errno == ECONNRESET)
{
{
ret = -1;
ret = -1;
return ret;
return ret;
}
}


if(errno == EINTR)
if(errno == EINTR)
{
{
ret = -1;
ret = -1;
return ret;
return ret;
}
}
printf("\n network_socket_recv() failed. \n");
printf("\r\n network_socket_recv() failed. \r\n");
ret = -1;
ret = -1;
return ret;
return ret;
}
}


return ret;
return ret;
}
}


int network_socket_send(void *ctx, const unsigned char *buf, size_t len)
int network_socket_send(void *ctx, const unsigned char *buf, size_t len)
{
{
int32_t ret;
int32_t ret;
int fd = (int) ctx;
int fd = (int) ctx;


ret = SUCCESS;
ret = SUCCESS;
if( fd < 0 )
if( fd < 0 )
{
{
ret = -1;
ret = -1;
return ret;
return ret;
}
}
ret = (int32_t) write(fd, buf, len);
ret = (int32_t) write(fd, buf, len);


if( ret < 0 )
if( ret < 0 )
{
{
/*
/*
if(net_would_block(ctx) != 0)
if(net_would_block(ctx) != 0)
{
{
return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_SSL_WANT_WRITE;
}
}
*/
*/
if(errno == EPIPE || errno == ECONNRESET)
if(errno == EPIPE || errno == ECONNRESET)
{
{
ret = -1;
ret = -1;
return ret;
return ret;
}
}
if(errno == EINTR)
if(errno == EINTR)
{
{
ret = -1;
ret = -1;
return ret;
return ret;
}
}
printf("\n network_socket_send() failed. \n");
printf("\r\n network_socket_send() failed. \r\n");
ret = -1;
ret = -1;
return ret;
return ret;
}
}


return ret;
return ret;


}
}






int network_connect( void *ctx, const char *hostname, int dstport )
int network_connect( void *ctx, const char *hostname, int dstport )
{
{
int ret;
int ret;
struct addrinfo hints;
struct addrinfo hints;
struct addrinfo *list;
struct addrinfo *list;
struct addrinfo *current;
struct addrinfo *current;
int socket = -1;
int socket = -1;
// int proto = MBEDTLS_NET_PROTO_TCP;
// int proto = MBEDTLS_NET_PROTO_TCP;
char portBuffer[7];
char portBuffer[7];


/* Do name resolution */
/* Do name resolution */
memset(&hints, 0, sizeof(hints));
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_protocol = IPPROTO_TCP;
snprintf(portBuffer, 6, "%d", dstport);
snprintf(portBuffer, 6, "%d", dstport);


if(getaddrinfo(hostname, portBuffer, &hints, &list) != 0)
if(getaddrinfo(hostname, portBuffer, &hints, &list) != 0)
{
{
printf("\n unknown host \n");
printf("\r\n unknown host \r\n");
ret = -1;
ret = -1;
return ret;
return ret;
}
}


/* Try the sockaddrs until a connection succeeds */
/* Try the sockaddrs until a connection succeeds */
ret = -1;
ret = -1;
for( current = list; current != NULL; current = current->ai_next)
for( current = list; current != NULL; current = current->ai_next)
{
{
socket = (int) socket(current->ai_family, current->ai_socktype, current->ai_protocol);
socket = (int) socket(current->ai_family, current->ai_socktype, current->ai_protocol);
if(socket < 0)
if(socket < 0)
{
{
printf("\n socket failed \n");
printf("\r\n socket failed \r\n");
ret = -1;
ret = -1;
continue;
continue;
}
}
*(int *)ctx = socket;
*(int *)ctx = socket;


if(connect(socket, current->ai_addr, (uint32_t)current->ai_addrlen) == 0)
if(connect(socket, current->ai_addr, (uint32_t)current->ai_addrlen) == 0)
{
{
ret = 0;
ret = 0;
break;
break;
}
}


close( socket );
close( socket );
printf("\n socket connect failed \n");
printf("\r\n socket connect failed \r\n");
ret = -1;
ret = -1;
return ret;
return ret;
}
}


freeaddrinfo(list);
freeaddrinfo(list);


return ret;
return ret;
}
}


int network_socket_close(uint32_t Socket)
int network_socket_close(uint32_t Socket)
{
{
shutdown( Socket, 2 );
shutdown( Socket, 2 );
close( Socket );
close( Socket );
return 0;
return 0;
}
}


int network_init(void)
{
uint32_t start;
start = HAL_GetTick();
while((gnetif.ip_addr.addr == 0) && (HAL_GetTick() - start < 10000))
{
}
if (gnetif.ip_addr.addr == 0)
{
msg_info(" Failed to get IP address! Please check your network configuration.\r\n");
Error_Handler();
}
return 0;
}

/**
* @brief Only used in case of wifi.
*/
void network_credential(void)
{
}




/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/