I am trying to use mbedtls on my lwip modbus application with stm32f4. I am working non-rtos systems.
I am using the lwip-altcp library. I need to change my function with altcp function,
This is old function:
BOOL
xMBTCPPortInit( USHORT usTCPPort )
{
struct tcp_pcb *pxPCBListenNew, *pxPCBListenOld;
BOOL bOkay = (BOOL)FALSE;
USHORT usPort;
if( usTCPPort == 0 )
{
usPort = MB_TCP_DEFAULT_PORT;
}
else
{
usPort = ( USHORT ) usTCPPort;
}
if( ( pxPCBListenNew = pxPCBListenOld = tcp_new( ) ) == NULL )
{
/* Can't create TCP socket. */
bOkay = (BOOL)FALSE;
}
else if( tcp_bind( pxPCBListenNew, IP_ADDR_ANY, ( u16_t ) usPort ) != ERR_OK )
{
/* Bind failed - Maybe illegal port value or in use. */
( void )tcp_close( pxPCBListenOld );
bOkay = (BOOL)FALSE;
}
else if( ( pxPCBListenNew = tcp_listen( pxPCBListenNew ) ) == NULL )
{
( void )tcp_close( pxPCBListenOld );
bOkay = (BOOL)FALSE;
}
else
{
/* Register callback function for new clients. */
tcp_accept( pxPCBListenNew, prvxMBTCPPortAccept );
/* Everything okay. Set global variable. */
pxPCBListen = pxPCBListenNew;
#ifdef MB_TCP_DEBUG
vMBPortLog( MB_LOG_DEBUG, "MBTCP-ACCEPT", "Protocol stack ready.\r\n" );
#endif
}
bOkay = (BOOL)TRUE;
return bOkay;
}
I am trying to change like this:
BOOL
xMBTCPPortInit( USHORT usTCPPort )
{
struct altcp_pcb *pxPCBListenNew, *pxPCBListenOld;
BOOL bOkay = (BOOL)FALSE;
USHORT usPort;
if( usTCPPort == 0 )
{
usPort = MB_TCP_DEFAULT_PORT;
}
else
{
usPort = ( USHORT ) usTCPPort;
}
if( ( pxPCBListenNew = pxPCBListenOld = altcp_new( NULL ) ) == NULL )
{
/* Can't create TCP socket. */
bOkay = (BOOL)FALSE;
}
else
if( altcp_bind( pxPCBListenNew, IP_ADDR_ANY, ( u16_t ) usPort ) != ERR_OK )
{
/* Bind failed - Maybe illegal port value or in use. */
( void )altcp_close( pxPCBListenOld );
bOkay = (BOOL)FALSE;
}
else if( ( pxPCBListenNew = altcp_listen( pxPCBListenNew ) ) == NULL )
{
( void )altcp_close( pxPCBListenOld );
bOkay = (BOOL)FALSE;
}
else
{
/* altcp_tls_new(pxPCBListenNew, IP_GET_TYPE(ip_addr))*/;
/* Register callback function for new clients. */
altcp_accept( pxPCBListenNew, prvxMBTCPPortAccept );
/* Everything okay. Set global variable. */
pxPCBListen = pxPCBListenNew;
#ifdef MB_TCP_DEBUG
vMBPortLog( MB_LOG_DEBUG, "MBTCP-ACCEPT", "Protocol stack ready.\r\n" );
#endif
SerialPrint("MBTCTP-ACCEPT");
}
altcp_tls_create_config_client(certfile, certlen);
bOkay = (BOOL)TRUE;
return bOkay;
}
I think I've a mistake with the altcp_new( NULL ) function because, when I interest another example code with using mbedtls-altcp, I see this line https://github.com/straight-coding/LPC407x-NoOS-LWIP-MBEDTLS-HTTPD-KEIL/blob/f3943f7487a296a16ddff51885c9c8d0ca07562a/LambdaIOT/httpd/http_core.c#L419
They used altcp_tls_new function, but this function needs altcp_tls_config parameters, but I have just tcp pcb parameters. How can I change my modbus-tcp connections functions ?
struct altcp_pcb *
altcp_tls_new(struct altcp_tls_config *config, u8_t ip_type)
{
struct altcp_pcb *inner_conn, *ret;
LWIP_UNUSED_ARG(ip_type);
inner_conn = altcp_tcp_new_ip_type(ip_type);
if (inner_conn == NULL) {
printf("altcp_tls_new inner_conn = nULL\r\n");
return NULL;
}
ret = altcp_tls_wrap(config, inner_conn);
if (ret == NULL) {
printf("altcp_tls_new altcp_tls_wrap return null\r\n");
altcp_close(inner_conn);
}
return ret;
}