Skip to content

Commit

Permalink
Added compile support for NuttX systems
Browse files Browse the repository at this point in the history
This is a basic version of compile support and only x86 simulator
lightning-APP is supported now.

The compilation method is shown below:
bash examples/lighting-app/nuttx/build_sim.sh
and compiled binaries are in the examples/lighting-app/nuttx/out directory.

Detailed introduction about NuttX system can refer to this link:
https://github.com/apache/nuttx

Signed-off-by: zhanghongyu <[email protected]>
  • Loading branch information
zhhyu7 committed Jan 4, 2024
1 parent 9279628 commit 63eeb35
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 11 deletions.
5 changes: 5 additions & 0 deletions build/config/compiler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,11 @@ config("strict_warnings") {

cflags_cc = [ "-Wnon-virtual-dtor" ]

if (current_os == "nuttx") {
cflags -= [ "-Wshadow" ]
cflags_cc -= [ "-Wnon-virtual-dtor" ]
}

configs = []
ldflags = []

Expand Down
29 changes: 29 additions & 0 deletions config/nuttx/chip-gn/.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2020 Project CHIP Authors
#
# 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.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

# The location of the build configuration file.
buildconfig = "${build_root}/config/BUILDCONFIG.gn"

# CHIP uses angle bracket includes.
check_system_includes = true

default_args = {
target_cpu = ""
target_os = "nuttx"

import("${chip_root}/config/nuttx/chip-gn/args.gni")
}
57 changes: 57 additions & 0 deletions config/nuttx/chip-gn/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2020 Project CHIP Authors
#
# 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.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/tests.gni")

assert(current_os == "nuttx")

declare_args() {
chip_build_example_providers = false
chip_example_lighting = false
}

static_library("nuttx") {
output_name = "libchipnuttx"

public_deps = [
"${chip_root}/examples/platform/linux:app-main",
"${chip_root}/src/lib",
]

if (chip_build_tests) {
public_deps += [ "${chip_root}/src:tests" ]
}

if (chip_build_example_providers) {
public_deps += [ "${chip_root}/examples/providers:device_info_provider" ]
}

if (chip_example_lighting) {
public_deps += [
"${chip_root}/examples/lighting-app/lighting-common",
"${chip_root}/examples/lighting-app/lighting-common:lighting-manager",
]
}

output_dir = "${root_out_dir}/lib"

complete_static_lib = true
}

group("default") {
deps = [ ":nuttx" ]
}
32 changes: 32 additions & 0 deletions config/nuttx/chip-gn/args.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) 2020 Project CHIP Authors
#
# 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.

import("//build_overrides/chip.gni")

import("${chip_root}/src/crypto/crypto.gni")

chip_device_platform = "nuttx"

chip_build_tests = false

chip_project_config_include = ""
chip_system_project_config_include = ""
chip_ble_project_config_include = ""

chip_crypto = "mbedtls"
chip_external_mbedtls = true

custom_toolchain = "${chip_root}/config/nuttx/chip-gn/toolchain:nuttx"

pw_build_PIP_CONSTRAINTS = [ "${chip_root}/scripts/setup/constraints.txt" ]
33 changes: 33 additions & 0 deletions config/nuttx/chip-gn/toolchain/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) 2020 Project CHIP Authors
#
# 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.

import("//build/toolchain/gcc_toolchain.gni")
import("//build_overrides/build.gni")

declare_args() {
nuttx_ar = ""
nuttx_cc = ""
nuttx_cxx = ""
}

gcc_toolchain("nuttx") {
ar = nuttx_ar
cc = nuttx_cc
cxx = nuttx_cxx

toolchain_args = {
current_os = "nuttx"
is_clang = false
}
}
2 changes: 1 addition & 1 deletion examples/lighting-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void ApplicationShutdown()
}
}

int main(int argc, char * argv[])
extern "C" int main(int argc, char * argv[])
{
if (ChipLinuxAppInit(argc, argv) != 0)
{
Expand Down
32 changes: 32 additions & 0 deletions examples/lighting-app/nuttx/build_sim.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
matter_dir=$(dirname "$0")/../../..
realpath_matter=$(realpath "$matter_dir")
echo "The root of matter is $realpath_matter"
cd "$(dirname "$0")"

if [ ! -e nuttx.zip ]; then
wget -O nuttx.zip https://codeload.github.com/apache/nuttx/zip/refs/heads/master
unzip -q nuttx.zip
fi

if [ ! -e nuttx ]; then
mv nuttx-master nuttx
fi

if [ ! -e nuttx_app.zip ]; then
wget -O nuttx_app.zip https://codeload.github.com/apache/nuttx-apps/zip/refs/heads/master
unzip -q nuttx_app.zip
fi

if [ ! -e apps ]; then
mv nuttx-apps-master apps
fi

if [ ! -e apps/netutils/connectedhomeip/connectedhomeip ]; then
ln -s "$realpath_matter" apps/netutils/connectedhomeip/connectedhomeip
fi

mkdir out
cd nuttx
cmake -B build -DBOARD_CONFIG=sim:matter -GNinja
cmake --build build
cp build/nuttx ../out
3 changes: 3 additions & 0 deletions src/inet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,7 @@ static_library("inet") {
}

cflags = [ "-Wconversion" ]
if (current_os == "nuttx") {
cflags -= [ "-Wconversion" ]
}
}
8 changes: 4 additions & 4 deletions src/lib/core/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,13 @@ class ChipError
*
* This template ensures that the numeric value is constant and well-formed.
*/
template <SdkPart PART, StorageType CODE>
template <SdkPart PART, StorageType SCODE>
struct SdkErrorConstant
{
static_assert(FitsInField(kSdkPartLength, to_underlying(PART)), "part is too large");
static_assert(FitsInField(kSdkCodeLength, CODE), "code is too large");
static_assert(MakeInteger(PART, CODE) != 0, "value is zero");
static constexpr StorageType value = MakeInteger(PART, CODE);
static_assert(FitsInField(kSdkCodeLength, SCODE), "code is too large");
static_assert(MakeInteger(PART, SCODE) != 0, "value is zero");
static constexpr StorageType value = MakeInteger(PART, SCODE);
};
};

Expand Down
8 changes: 8 additions & 0 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
} else if (chip_device_platform == "stm32") {
device_layer_target_define = "STM32"
defines += [ "CHIP_DEVICE_LAYER_TARGET=stm32" ]
} else if (chip_device_platform == "nuttx") {
device_layer_target_define = "LINUX"
defines += [
"CHIP_DEVICE_LAYER_TARGET=Linux",
"CHIP_DEVICE_CONFIG_ENABLE_WIFI=${chip_enable_wifi}",
]
} else {
device_layer_target_define = ""
}
Expand Down Expand Up @@ -572,6 +578,8 @@ if (chip_device_platform != "none") {
_platform_target = "ASR"
} else if (chip_device_platform == "stm32") {
_platform_target = "stm32"
} else if (chip_device_platform == "nuttx") {
_platform_target = "Linux"
} else {
assert(false, "Unknown chip_device_platform: ${chip_device_platform}")
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import("${build_root}/config/linux/pkg_config.gni")
import("${chip_root}/src/lib/core/core.gni")
import("${chip_root}/src/platform/device.gni")

assert(chip_device_platform == "linux")
assert(chip_device_platform == "linux" || chip_device_platform == "nuttx")

if (chip_use_pw_logging) {
import("//build_overrides/pigweed.gni")
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Linux/CHIPLinuxStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ CHIP_ERROR ChipLinuxStorage::WriteValue(const char * key, uint32_t val)
{
char buf[32];

snprintf(buf, sizeof(buf), "%d", val);
snprintf(buf, sizeof(buf), "%" PRIu32, val);

return WriteValueStr(key, buf);
}
Expand Down
7 changes: 6 additions & 1 deletion src/platform/Linux/ConnectivityUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@

#include <arpa/inet.h>
#include <ifaddrs.h>
#ifdef __NuttX__
#include <nuttx/ethtool.h>
#include <nuttx/wireless/wireless.h>
#else
#include <linux/ethtool.h>
#include <linux/if_link.h>
#include <linux/sockios.h>
#include <linux/wireless.h>
#endif
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -687,7 +692,7 @@ CHIP_ERROR ConnectivityUtils::GetEthPHYRate(const char * ifname, app::Clusters::
pHYRate = app::Clusters::EthernetNetworkDiagnostics::PHYRateEnum::kRate400G;
break;
default:
ChipLogError(DeviceLayer, "Undefined speed! (%d)\n", speed);
ChipLogError(DeviceLayer, "Undefined speed! (%" PRIu32 ")\n", speed);
err = CHIP_ERROR_READ_FAILED;
break;
};
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Linux/ConnectivityUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
#include <platform/DiagnosticDataProvider.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>

#ifdef __NuttX__
#include <nuttx/wireless/wireless.h>
#else
#include <linux/types.h> /* for "caddr_t" et al */
#include <linux/wireless.h>
#endif

namespace chip {
namespace DeviceLayer {
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Linux/DiagnosticDataProviderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
#include <arpa/inet.h>
#include <dirent.h>
#include <ifaddrs.h>
#ifdef __NuttX__
#include <netpacket/netlink.h>
#else
#include <linux/ethtool.h>
#include <linux/if_link.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#endif
#include <malloc.h>
#include <net/if.h>
#include <netinet/in.h>
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Linux/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ void ENFORCE_FORMAT(3, 0) LogV(const char * module, uint8_t category, const char
flockfile(stdout);

printf("[%" PRIu64 ".%06" PRIu64 "][%lld:%lld] CHIP:%s: ", static_cast<uint64_t>(tv.tv_sec), static_cast<uint64_t>(tv.tv_usec),
#ifdef __NuttX__
static_cast<long long>(getpid()), static_cast<long long>(gettid()), module);
#else
static_cast<long long>(syscall(SYS_getpid)), static_cast<long long>(syscall(SYS_gettid)), module);
#endif
vprintf(msg, v);
printf("\n");
fflush(stdout);
Expand Down
4 changes: 4 additions & 0 deletions src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#ifdef __NuttX__
#include <netpacket/netlink.h>
#else
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#endif
#include <net/if.h>
#include <netinet/in.h>
#include <unistd.h>
Expand Down
12 changes: 9 additions & 3 deletions src/platform/device.gni
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ if (chip_device_platform == "auto") {
chip_device_platform = "webos"
} else if (current_os == "zephyr") {
chip_device_platform = "zephyr"
} else if (current_os == "nuttx") {
chip_device_platform = "nuttx"
} else {
chip_device_platform = "none"
}
Expand Down Expand Up @@ -94,7 +96,8 @@ declare_args() {
chip_device_platform == "ameba" || chip_device_platform == "webos" ||
chip_device_platform == "cc32xx" || chip_device_platform == "mw320" ||
chip_device_platform == "beken" || chip_device_platform == "mt793x" ||
chip_device_platform == "asr" || chip_device_platform == "openiotsdk") {
chip_device_platform == "asr" || chip_device_platform == "openiotsdk" ||
chip_device_platform == "nuttx") {
chip_mdns = "minimal"
} else if (chip_device_platform == "darwin" ||
chip_device_platform == "cc13x2_26x2" ||
Expand All @@ -109,7 +112,8 @@ declare_args() {
# Enable Subscription persistence / resumption for CI and supported platforms
if (chip_device_platform == "darwin" || chip_device_platform == "linux" ||
chip_device_platform == "esp32" || chip_device_platform == "fake" ||
chip_device_platform == "efr32" || chip_device_platform == "SiWx917") {
chip_device_platform == "efr32" || chip_device_platform == "SiWx917" ||
chip_device_platform == "nuttx") {
chip_persist_subscriptions = true
} else {
chip_persist_subscriptions = false
Expand Down Expand Up @@ -190,6 +194,8 @@ if (chip_device_platform == "cc13x2_26x2") {
_chip_device_layer = "openiotsdk"
} else if (chip_device_platform == "asr") {
_chip_device_layer = "ASR"
} else if (chip_device_platform == "nuttx") {
_chip_device_layer = "Linux"
} else if (chip_device_platform == "stm32") {
_chip_device_layer = "stm32"
}
Expand Down Expand Up @@ -262,5 +268,5 @@ assert(
chip_device_platform == "bl702" || chip_device_platform == "bl702l" ||
chip_device_platform == "mt793x" || chip_device_platform == "SiWx917" ||
chip_device_platform == "openiotsdk" || chip_device_platform == "asr" ||
chip_device_platform == "stm32",
chip_device_platform == "stm32" || chip_device_platform == "nuttx",
"Please select a valid value for chip_device_platform")

0 comments on commit 63eeb35

Please sign in to comment.