Skip to content

Commit

Permalink
[OIS] Add IoT Socket support
Browse files Browse the repository at this point in the history
Add OIS IoT Socket to build system.
Implement IoT socket system layer and endpoints (UDP and TCP).
Adapting inet and system layers to IoT Socket support.
Improving unit-tests for IoT socket support.
Add IoT socket support in OIS PlatformManagerImpl.
Move initialisation of openiotsdk to after network init.

Signed-off-by: paul-szczepanek-arm <[email protected]>
  • Loading branch information
ATmobica committed May 15, 2023
1 parent f563ebf commit 71b8783
Show file tree
Hide file tree
Showing 28 changed files with 2,799 additions and 57 deletions.
1 change: 1 addition & 0 deletions config/openiotsdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ matter_add_gn_arg_string("chip_crypto" "${CONFIG_CHIP_C
matter_add_gn_arg_string("chip_openiotsdk_software_version" "${CONFIG_CHIP_OPEN_IOT_SDK_SOFTWARE_VERSION}")
matter_add_gn_arg_string("chip_openiotsdk_software_version_string" "${CONFIG_CHIP_OPEN_IOT_SDK_SOFTWARE_VERSION_STRING}")
matter_add_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OPEN_IOT_SDK_OTA_ENABLE)
matter_add_gn_arg_bool ("chip_system_config_use_iot_socket" CONFIG_CHIP_OPEN_IOT_SDK_USE_IOT_SOCKET)
if (TARGET cmsis-rtos-api)
matter_add_gn_arg_string("target_os" "cmsis-rtos")
endif()
Expand Down
1 change: 1 addition & 0 deletions config/openiotsdk/cmake/chip.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(CONFIG_CHIP_CRYPTO "mbedtls" CACHE STRING "Matter crypto backend. Mbedtls as
set(CONFIG_CHIP_OPEN_IOT_SDK_SOFTWARE_VERSION "0" CACHE STRING "Software version number")
set(CONFIG_CHIP_OPEN_IOT_SDK_SOFTWARE_VERSION_STRING ${TFM_NS_APP_VERSION} CACHE STRING "Software version in string format x.x.x")
set(CONFIG_CHIP_OPEN_IOT_SDK_OTA_ENABLE NO CACHE BOOL "Enable OTA support")
set(CONFIG_CHIP_OPEN_IOT_SDK_USE_IOT_SOCKET NO CACHE BOOL "Enable using IoT Socket API")

if(CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS AND NOT TFM_SUPPORT)
message( FATAL_ERROR "You can not use PSA Protected Storage without TF-M support" )
Expand Down
8 changes: 8 additions & 0 deletions config/openiotsdk/cmake/sdk.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ FetchContent_Declare(
PATCH_COMMAND git reset --hard --quiet && git clean --force -dx --quiet && git apply ${CMAKE_CURRENT_LIST_DIR}/tf-m.patch
)

FetchContent_Declare(
lwip
GIT_REPOSITORY https://github.com/lwip-tcpip/lwip.git
GIT_TAG STABLE-2_1_3_RELEASE
GIT_PROGRESS ON
PATCH_COMMAND git apply --ignore-space-change --ignore-whitespace ${CHIP_ROOT}/config/openiotsdk/lwip/lwip.patch || true
)

# Open IoT SDK configuration
set(IOTSDK_FETCH_LIST
mcu-driver-reference-platforms-for-arm
Expand Down
67 changes: 67 additions & 0 deletions config/openiotsdk/lwip/lwip.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
diff --git a/src/api/sockets.c b/src/api/sockets.c
index 78526350..fb6e0796 100644
--- a/src/api/sockets.c
+++ b/src/api/sockets.c
@@ -1158,23 +1158,34 @@ lwip_recvfrom_udp_raw(struct lwip_sock *sock, int flags, struct msghdr *msg, u16
#if LWIP_NETBUF_RECVINFO
/* Check if packet info was recorded */
if (buf->flags & NETBUF_FLAG_DESTADDR) {
- if (IP_IS_V4(&buf->toaddr)) {
+#if LWIP_IPV4 || LWIP_IPV6
+ if (msg->msg_controllen >= CMSG_SPACE(sizeof(struct in_pktinfo))) {
+ struct cmsghdr *chdr = CMSG_FIRSTHDR(msg); /* This will always return a header!! */
+ chdr->cmsg_level = IPPROTO_IP;
+ chdr->cmsg_type = IP_PKTINFO;
#if LWIP_IPV4
- if (msg->msg_controllen >= CMSG_SPACE(sizeof(struct in_pktinfo))) {
- struct cmsghdr *chdr = CMSG_FIRSTHDR(msg); /* This will always return a header!! */
+ if (IP_IS_V4(&buf->toaddr)) {
struct in_pktinfo *pkti = (struct in_pktinfo *)CMSG_DATA(chdr);
- chdr->cmsg_level = IPPROTO_IP;
- chdr->cmsg_type = IP_PKTINFO;
chdr->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
- pkti->ipi_ifindex = buf->p->if_idx;
- inet_addr_from_ip4addr(&pkti->ipi_addr, ip_2_ip4(netbuf_destaddr(buf)));
msg->msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
- wrote_msg = 1;
- } else {
- msg->msg_flags |= MSG_CTRUNC;
+ inet_addr_from_ip4addr(&pkti->ipi_addr, ip_2_ip4(netbuf_destaddr(buf)));
+ pkti->ipi_ifindex = buf->p->if_idx;
}
#endif /* LWIP_IPV4 */
+#if LWIP_IPV6
+ if (IP_IS_V6(&buf->toaddr)) {
+ struct in6_pktinfo *pkti6 = (struct in6_pktinfo *)CMSG_DATA(chdr);
+ chdr->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
+ msg->msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
+ inet6_addr_from_ip6addr(&pkti6->ipi6_addr, ip_2_ip6(netbuf_destaddr(buf)));
+ pkti6->ipi6_ifindex = buf->p->if_idx;
+ }
+#endif /* LWIP_IPV6 */
+ wrote_msg = 1;
+ } else {
+ msg->msg_flags |= MSG_CTRUNC;
}
+#endif /* LWIP_IPV4 || LWIP_IPV6 */
}
#endif /* LWIP_NETBUF_RECVINFO */

diff --git a/src/include/lwip/sockets.h b/src/include/lwip/sockets.h
index d70d36c4..1c225041 100644
--- a/src/include/lwip/sockets.h
+++ b/src/include/lwip/sockets.h
@@ -329,6 +329,13 @@ struct in_pktinfo {
};
#endif /* LWIP_IPV4 */

+#if LWIP_IPV6
+struct in6_pktinfo {
+ unsigned int ipi6_ifindex; /* Interface index */
+ struct in6_addr ipi6_addr; /* Destination (from header) address */
+};
+#endif /* LWIP_IPV6 */
+
#if LWIP_IPV6_MLD
/*
* Options and types related to IPv6 multicast membership
19 changes: 17 additions & 2 deletions config/openiotsdk/lwip/user_lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,24 @@
*/
#define LWIP_RAW (1)

#ifdef LWIP_DEBUG
/**
* LWIP_NETIF_API: Support netif api (in netifapi.c)
*/
#define LWIP_NETIF_API (1)

/**
* IP_SOF_BROADCAST: Use the SOF_BROADCAST field to enable broadcast
* filter per pcb on udp and raw send operations.
*/
#define IP_SOF_BROADCAST (1)

/**
* IP_SOF_BROADCAST_RECV: Enable the broadcast filter on recv operations.
*/
#define IP_SOF_BROADCAST_RECV (1)

// Debug Options
#ifdef LWIP_DEBUG
#define NETIF_DEBUG LWIP_DBG_ON
#define IP_DEBUG LWIP_DBG_ON
#define TCP_DEBUG LWIP_DBG_ON
Expand All @@ -60,7 +75,7 @@

#define LWIP_DBG_TYPES_ON LWIP_DBG_ON
#define LWIP_DBG_MIN_LEVEL LWIP_DBG_LEVEL_ALL
#endif
#endif // LWIP_DEBUG

#ifndef LWIP_PLATFORM_DIAG
#define LWIP_PLATFORM_DIAG(x) \
Expand Down
51 changes: 51 additions & 0 deletions src/inet/EndPointStateIoTSocket.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
* Copyright (c) 2015-2017 Nest Labs, Inc.
*
* 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.
*/

/**
* Shared state for socket implementations of TCPEndPoint and UDPEndPoint.
*/

#pragma once

#include <stdint.h>

namespace chip {
namespace Inet {

/**
* Definitions shared by all IoT Socket based EndPoint classes.
*/
class EndPointStateIoTSocket
{
public:
virtual void SelectCallback(void * readMask, void * writeMask, void * exceptionMask){};
int32_t GetSocketId() { return mSocket; };

static constexpr int32_t kInvalidSocketFd = -1;

protected:
EndPointStateIoTSocket() : mSocket(kInvalidSocketFd){};
virtual ~EndPointStateIoTSocket(){};

int32_t mSocket; /**< Encapsulated socket descriptor. */
int32_t mSocketFamily; /**< Protocol family, i.e. IPv4 or IPv6. */
uint16_t mBoundPort;
};

} // namespace Inet
} // namespace chip
21 changes: 20 additions & 1 deletion src/inet/IPAddress-StringFuncts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include <inet/IPAddress.h>
#include <lib/support/CodeUtils.h>

#if CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#include <iot_socket.h>
#endif // CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#include <arpa/inet.h>
#endif
Expand All @@ -43,7 +47,7 @@ namespace Inet {

char * IPAddress::ToString(char * buf, uint32_t bufSize) const
{
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT && !CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#if INET_CONFIG_ENABLE_IPV4
if (IsIPv4())
{
Expand Down Expand Up @@ -81,6 +85,21 @@ char * IPAddress::ToString(char * buf, uint32_t bufSize) const
#elif CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
otIp6Address addr = ToIPv6();
otIp6AddressToString(&addr, buf, static_cast<uint16_t>(bufSize));
#elif CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#if INET_CONFIG_ENABLE_IPV4
if (IsIPv4())
{
ip4_addr_t ip4_addr;
memcpy(&ip4_addr, &Addr[3], sizeof(ip4_addr));
ip4addr_ntoa_r(&ip4_addr, buf, (int) bufSize);
}
else
#endif // INET_CONFIG_ENABLE_IPV4
{
ip6_addr_t ip6_addr;
memcpy(&ip6_addr.addr, Addr, sizeof(ip6_addr.addr));
ip6addr_ntoa_r(&ip6_addr, buf, (int) bufSize);
}
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP

return buf;
Expand Down
19 changes: 12 additions & 7 deletions src/inet/IPAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ IPAddress::IPAddress(const ip_addr_t & addr)

#endif // INET_CONFIG_ENABLE_IPV4 || LWIP_IPV4

#if !CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#if INET_CONFIG_ENABLE_IPV4

ip4_addr_t IPAddress::ToIPv4() const
Expand Down Expand Up @@ -200,12 +201,14 @@ ip6_addr_t IPAddress::ToIPv6() const
return ipAddr;
}

#endif // !CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK || CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#if INET_CONFIG_ENABLE_IPV4
IPAddress::IPAddress(const struct in_addr & ipv4Addr)
IPAddress::IPAddress(const platform_in_addr & ipv4Addr)
{
Addr[0] = 0;
Addr[1] = 0;
Expand All @@ -214,29 +217,31 @@ IPAddress::IPAddress(const struct in_addr & ipv4Addr)
}
#endif // INET_CONFIG_ENABLE_IPV4

IPAddress::IPAddress(const struct in6_addr & ipv6Addr)
IPAddress::IPAddress(const platform_in6_addr & ipv6Addr)
{
static_assert(sizeof(*this) == sizeof(ipv6Addr), "in6_addr size mismatch");
memcpy(Addr, &ipv6Addr, sizeof(ipv6Addr));
}

#if INET_CONFIG_ENABLE_IPV4
struct in_addr IPAddress::ToIPv4() const
IPAddress::platform_in_addr IPAddress::ToIPv4() const
{
struct in_addr ipv4Addr;
platform_in_addr ipv4Addr;
ipv4Addr.s_addr = Addr[3];
return ipv4Addr;
}
#endif // INET_CONFIG_ENABLE_IPV4

struct in6_addr IPAddress::ToIPv6() const
IPAddress::platform_in6_addr IPAddress::ToIPv6() const
{
in6_addr ipAddr;
platform_in6_addr ipAddr;
static_assert(sizeof(ipAddr) == sizeof(Addr), "in6_addr size mismatch");
memcpy(&ipAddr, Addr, sizeof(ipAddr));
return ipAddr;
}
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK || CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
CHIP_ERROR IPAddress::GetIPAddressFromSockAddr(const SockAddrWithoutStorage & sockaddr, IPAddress & outIPAddress)
{
#if INET_CONFIG_ENABLE_IPV4
Expand Down
45 changes: 30 additions & 15 deletions src/inet/IPAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@

#include "inet/IANAConstants.h"

#if CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#include <iot_socket.h>
#endif // CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#include <lwip/init.h>
#include <lwip/ip_addr.h>
Expand Down Expand Up @@ -175,12 +179,23 @@ class DLL_EXPORT IPAddress
#endif // INET_CONFIG_ENABLE_IPV4
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
explicit IPAddress(const struct in6_addr & ipv6Addr);
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK || CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#if CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
using platform_in6_addr = iot_in6_addr;
using platform_in_addr = iot_in_addr;
using platform_sockaddr_in6 = iot_sockaddr_in6;
using platform_sockaddr_in = iot_sockaddr_in;
#else
using platform_in6_addr = struct in6_addr;
using platform_in_addr = struct in_addr;
using platform_sockaddr_in6 = struct sockaddr_in6;
using platform_sockaddr_in = struct sockaddr_in;
#endif // CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
explicit IPAddress(const platform_in6_addr & ipv6Addr);
#if INET_CONFIG_ENABLE_IPV4
explicit IPAddress(const struct in_addr & ipv4Addr);
explicit IPAddress(const platform_in_addr & ipv4Addr);
#endif // INET_CONFIG_ENABLE_IPV4
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK || CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
explicit IPAddress(const otIp6Address & ipv6Addr);
Expand Down Expand Up @@ -520,7 +535,7 @@ class DLL_EXPORT IPAddress
*/

#if CHIP_SYSTEM_CONFIG_USE_LWIP && !CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT

#if !CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
/**
* @fn ToLwIPAddr() const
*
Expand Down Expand Up @@ -557,17 +572,22 @@ class DLL_EXPORT IPAddress
#if INET_CONFIG_ENABLE_IPV4
ip4_addr_t ToIPv4(void) const;
#endif // INET_CONFIG_ENABLE_IPV4
#endif // !CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

struct in6_addr ToIPv6() const;

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK || CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
platform_in6_addr ToIPv6() const;
#if INET_CONFIG_ENABLE_IPV4
struct in_addr ToIPv4() const;
platform_in_addr ToIPv4() const;
#endif // INET_CONFIG_ENABLE_IPV4

static IPAddress FromSockAddr(const platform_sockaddr_in6 & sockaddr) { return IPAddress(sockaddr.sin6_addr); }
#if INET_CONFIG_ENABLE_IPV4
static IPAddress FromSockAddr(const platform_sockaddr_in & sockaddr) { return IPAddress(sockaddr.sin_addr); }
#endif // INET_CONFIG_ENABLE_IPV4
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK || CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_USE_NETWORK_FRAMEWORK
/**
* Get the IP address from a SockAddr.
*/
Expand All @@ -576,11 +596,6 @@ class DLL_EXPORT IPAddress
{
return GetIPAddressFromSockAddr(reinterpret_cast<const SockAddrWithoutStorage &>(sockaddr), outIPAddress);
}
static IPAddress FromSockAddr(const sockaddr_in6 & sockaddr) { return IPAddress(sockaddr.sin6_addr); }
#if INET_CONFIG_ENABLE_IPV4
static IPAddress FromSockAddr(const sockaddr_in & sockaddr) { return IPAddress(sockaddr.sin_addr); }
#endif // INET_CONFIG_ENABLE_IPV4

#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_USE_NETWORK_FRAMEWORK

#if CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
Expand Down
4 changes: 4 additions & 0 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
#include <lwip/tcpip.h>
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if CHIP_SYSTEM_CONFIG_USE_IOT_SOCKET
#include <iot_socket.h>
#endif

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#include <errno.h>
#include <fcntl.h>
Expand Down
Loading

0 comments on commit 71b8783

Please sign in to comment.