Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RTEMS TCP/UDP Support #283

Merged
merged 10 commits into from
Nov 15, 2021
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Generic")
set(UCLIENT_PLATFORM_POSIX_NOPOLL ON)
elseif(PLATFORM_NAME STREQUAL "FreeRTOS_Plus_TCP")
set(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP ON)
elseif(PLATFORM_NAME STREQUAL "RTEMS_BSD_NET")
set(UCLIENT_PLATFORM_RTEMS_BSD_NET ON)
endif()
endif()

Expand All @@ -169,6 +171,8 @@ if(UCLIENT_PROFILE_UDP)
list(APPEND _transport_src src/c/profile/transport/ip/udp/udp_transport_windows.c)
elseif(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP)
list(APPEND _transport_src src/c/profile/transport/ip/udp/udp_transport_freertos_plus_tcp.c)
elseif(UCLIENT_PLATFORM_RTEMS_BSD_NET)
list(APPEND _transport_src src/c/profile/transport/ip/udp/udp_transport_rtems_bsd_net.c)
endif()
endif()

Expand All @@ -180,6 +184,8 @@ if(UCLIENT_PROFILE_TCP)
list(APPEND _transport_src src/c/profile/transport/ip/tcp/tcp_transport_windows.c)
elseif(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP)
list(APPEND _transport_src src/c/profile/transport/ip/tcp/tcp_transport_freertos_plus_tcp.c)
elseif(UCLIENT_PLATFORM_RTEMS_BSD_NET)
list(APPEND _transport_src src/c/profile/transport/ip/tcp/tcp_transport_rtems_bsd_net.c)
endif()
endif()

Expand All @@ -204,6 +210,8 @@ if(UCLIENT_PROFILE_DISCOVERY OR UCLIENT_PROFILE_UDP OR UCLIENT_PROFILE_TCP)
list(APPEND _transport_src src/c/profile/transport/ip/ip_windows.c)
elseif(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP)
list(APPEND _transport_src src/c/profile/transport/ip/ip_freertos_plus_tcp.c)
elseif(UCLIENT_PLATFORM_RTEMS_BSD_NET)
list(APPEND _transport_src src/c/profile/transport/ip/ip_rtems_bsd_net.c)
endif()
endif()

Expand All @@ -218,6 +226,8 @@ if(UCLIENT_PROFILE_DISCOVERY)
list(APPEND _transport_src src/c/profile/discovery/transport/udp_transport_datagram_windows.c)
elseif(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP)
list(APPEND _transport_src src/c/profile/discovery/transport/udp_transport_datagram_freertos_plus_tcp.c)
elseif(UCLIENT_PLATFORM_RTEMS_BSD_NET)
list(APPEND _transport_src src/c/profile/discovery/transport/udp_transport_datagram_rtems_bsd_net.c)
endif()
endif()

Expand Down
1 change: 1 addition & 0 deletions include/uxr/client/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#cmakedefine UCLIENT_PLATFORM_POSIX_NOPOLL
#cmakedefine UCLIENT_PLATFORM_WINDOWS
#cmakedefine UCLIENT_PLATFORM_FREERTOS_PLUS_TCP
#cmakedefine UCLIENT_PLATFORM_RTEMS_BSD_NET
#cmakedefine UCLIENT_PLATFORM_ZEPHYR

#define UXR_CONFIG_MAX_OUTPUT_BEST_EFFORT_STREAMS @UCLIENT_MAX_OUTPUT_BEST_EFFORT_STREAMS@
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _UXR_CLIENT_TCP_TRANSPORT_RTEMS_H_
#define _UXR_CLIENT_TCP_TRANSPORT_RTEMS_H_

#ifdef __cplusplus
extern "C"
{
#endif // ifdef __cplusplus

#include "arpa/inet.h"
#include "sys/socket.h"
#include "sys/select.h"
#include "netinet/in.h"


typedef struct uxrTCPPlatform
{
struct sockaddr_in remote_addr;
struct fd_set select_fd;
int fd;
} uxrTCPPlatform;

#ifdef __cplusplus
}
#endif // ifdef __cplusplus

#endif //_UXR_CLIENT_TCP_TRANSPORT_RTEMS_H_
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef _UXR_CLIENT_UDP_TRANSPORT_RTEMS_H_
#define _UXR_CLIENT_UDP_TRANSPORT_RTEMS_H_

#ifdef __cplusplus
extern "C"
{
#endif // ifdef __cplusplus

#include "arpa/inet.h"
#include "sys/socket.h"
#include "sys/select.h"
#include "netinet/in.h"

typedef struct uxrUDPPlatform
{
struct sockaddr_in remote_addr;
struct fd_set select_fd;
int fd;
} uxrUDPPlatform;

#ifdef __cplusplus
}
#endif // ifdef __cplusplus

#endif //_UXR_CLIENT_UDP_TRANSPORT_RTEMS_H_
4 changes: 4 additions & 0 deletions include/uxr/client/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <uxr/client/profile/transport/ip/udp/udp_transport_windows.h>
#elif defined(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP)
#include <uxr/client/profile/transport/ip/udp/udp_transport_freertos_plus_tcp.h>
#elif defined(UCLIENT_PLATFORM_RTEMS_BSD_NET)
#include <uxr/client/profile/transport/ip/udp/udp_transport_rtems_bsd_net.h>
#endif // if defined(UCLIENT_EXTERNAL_UDP)
#include <uxr/client/profile/transport/ip/udp/udp_transport.h>
#endif //UCLIENT_PROFILE_UDP
Expand All @@ -35,6 +37,8 @@
#include <uxr/client/profile/transport/ip/tcp/tcp_transport_posix.h>
#elif defined(UCLIENT_PLATFORM_WINDOWS)
#include <uxr/client/profile/transport/ip/tcp/tcp_transport_windows.h>
#elif defined(UCLIENT_PLATFORM_RTEMS_BSD_NET)
#include <uxr/client/profile/transport/ip/tcp/tcp_transport_rtems_bsd_net.h>
#endif // if defined(UCLIENT_EXTERNAL_TCP)
#include <uxr/client/profile/transport/ip/tcp/tcp_transport.h>
#endif //UCLIENT_PROFILE_TCP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ extern "C"
#include "list.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#elif defined(UCLIENT_PLATFORM_RTEMS_BSD_NET)
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#endif // if defined(UCLIENT_PLATFORM_POSIX)


Expand All @@ -57,6 +61,9 @@ typedef struct uxrUDPTransportDatagram
#elif defined(UCLIENT_PLATFORM_FREERTOS_PLUS_TCP)
SocketSet_t poll_fd;
Socket_t fd;
#elif defined(UCLIENT_PLATFORM_RTEMS_BSD_NET)
struct fd_set select_fd;
int fd;
#endif // if defined(UCLIENT_PLATFORM_POSIX)

} uxrUDPTransportDatagram;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2018 eSOL Co.,Ltd.
// Copyright 2018 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "udp_transport_datagram_internal.h"
#include "arpa/inet.h"
#include "sys/socket.h"
#include "netinet/in.h"

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

bool uxr_init_udp_transport_datagram(
uxrUDPTransportDatagram* transport)
{
bool rv = false;

transport->fd = socket(AF_INET, SOCK_DGRAM, 0);

if (-1 != transport->fd)
{
/* Poll setup. */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-conversion"
FD_ZERO(&transport->select_fd);
FD_SET(transport->fd, &transport->select_fd);
#pragma GCC diagnostic pop
rv = true;
}

return rv;
}

bool uxr_close_udp_transport_datagram(
uxrUDPTransportDatagram* transport)
{
(void) close(transport->fd);

return true;
}

bool uxr_udp_send_datagram_to(
uxrUDPTransportDatagram* transport,
const uint8_t* buf,
size_t len,
const TransportLocator* locator)
{
bool rv = true;

struct sockaddr_in remote_addr;
memcpy(&remote_addr.sin_addr.s_addr, &locator->_.medium_locator.address, sizeof(locator->_.medium_locator.address));
if (0 != remote_addr.sin_addr.s_addr)
{
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(locator->_.medium_locator.locator_port);

int32_t bytes_sent = sendto(transport->fd, (void*)buf, len, 0,
(struct sockaddr*)&remote_addr, sizeof(remote_addr));

if (0 >= bytes_sent)
{
rv = false;
}
}

return rv;
}

bool uxr_udp_recv_datagram(
uxrUDPTransportDatagram* transport,
uint8_t** buf,
size_t* len,
int timeout)
{
bool rv = false;

struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;


int32_t poll_rv = select(transport->fd + 1, &transport->select_fd, NULL, NULL, &tv);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The same FD_ZERO FD_SET treatment before select() should be done here. Will post a new PR soon.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks. We will handle it in a new PR.

Copy link
Contributor

@Acuadros95 Acuadros95 Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! We missed that the set object is modified.

We also use a select approach on the multi-serial agent, where it is enough to copy the set on a local variable, check here: link (read_fds is equivalent to your transport->select_fd).

With this approach you avoid modifications on external data.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, cleaner! I will also then update the other two usages of select() like this.

if (0 < poll_rv)
{
int32_t bytes_received = recvfrom(transport->fd, (void*)transport->buffer, sizeof(transport->buffer),
0, NULL, NULL);
if (0 < bytes_received)
{
*len = (size_t)bytes_received;
*buf = transport->buffer;
rv = true;
}
}
else if (0 == poll_rv)
{
errno = ETIME;
}

return rv;
}

void uxr_bytes_to_ip(
const uint8_t* bytes,
char* ip)
{
uint32_t addr;
addr = (uint32_t)(*bytes + (*(bytes + 1) << 8) + (*(bytes + 2) << 16) + (*(bytes + 3) << 24));
struct in_addr saddr;
saddr.s_addr = addr;
char* res = inet_ntoa(saddr);
strncpy(ip, res, 16); // 16 == MAX_IPv4_LEN
}
75 changes: 75 additions & 0 deletions src/c/profile/transport/ip/ip_rtems_bsd_net.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2017-present Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <uxr/client/profile/transport/ip/ip.h>
#include <uxr/client/config.h>

#include "sys/socket.h"
#include "arpa/inet.h"
#include <netinet/in.h>
#include <string.h>

bool uxr_ip_to_locator(
char const* ip,
uint16_t port,
uxrIpProtocol ip_protocol,
TransportLocator* locator)
{
bool result = false;
switch (ip_protocol)
{
case UXR_IPv4:
locator->format = ADDRESS_FORMAT_MEDIUM;
locator->_.medium_locator.locator_port = port;
uint32_t addr = inet_addr(ip);
memcpy(&locator->_.medium_locator.address, &addr, sizeof(locator->_.medium_locator.address));
result = locator->_.medium_locator.address != 0;
break;
case UXR_IPv6:
break;
default:
break;
}
return result;
}

bool uxr_locator_to_ip(
TransportLocator const* locator,
char* ip,
size_t size,
uint16_t* port,
uxrIpProtocol* ip_protocol)
{
bool result = false;
(void)size;
switch (locator->format)
{
case ADDRESS_FORMAT_MEDIUM:
*port = locator->_.medium_locator.locator_port;
*ip_protocol = UXR_IPv4;
struct in_addr saddr;
uint32_t addr;
memcpy(&addr, &locator->_.medium_locator.address, sizeof(locator->_.medium_locator.address));
saddr.s_addr = addr;
char* res = inet_ntoa(saddr);
strncpy(ip, res, 16); // 16 == MAX_IPv4_LEN
result = true;
break;
case ADDRESS_FORMAT_LARGE:
break;
default:
break;
}
return result;
}
Loading