From 3a600aff9c5ee6aba8efbc2aa73ffabddcf0631f Mon Sep 17 00:00:00 2001 From: Justin Morton Date: Mon, 22 Jan 2024 16:05:38 -0800 Subject: [PATCH] add coap wifi support Signed-off-by: Justin Morton --- .../nrf_cloud_multi_service/CMakeLists.txt | 5 +- .../overlay_nrf7002ek_wifi_coap_no_lte.conf | 222 ++++++++++++++++++ .../src/cloud_connection.c | 7 + 3 files changed, 232 insertions(+), 2 deletions(-) create mode 100644 samples/cellular/nrf_cloud_multi_service/overlay_nrf7002ek_wifi_coap_no_lte.conf diff --git a/samples/cellular/nrf_cloud_multi_service/CMakeLists.txt b/samples/cellular/nrf_cloud_multi_service/CMakeLists.txt index ed6b6dd35786..def1b7845199 100644 --- a/samples/cellular/nrf_cloud_multi_service/CMakeLists.txt +++ b/samples/cellular/nrf_cloud_multi_service/CMakeLists.txt @@ -10,8 +10,9 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(nrf_cloud_multi_service) zephyr_compile_definitions(PROJECT_NAME=${PROJECT_NAME}) -# Include certs files if enabled. -zephyr_include_directories_ifdef(CONFIG_NRF_CLOUD_PROVISION_CERTIFICATES certs) +if(CONFIG_NRF_CLOUD_COAP_WIFI_ADD_CREDS_FROM_FILE OR CONFIG_NRF_CLOUD_PROVISION_CERTIFICATES) + zephyr_include_directories(certs) +endif() # NORDIC SDK APP START target_sources(app PRIVATE src/main.c) diff --git a/samples/cellular/nrf_cloud_multi_service/overlay_nrf7002ek_wifi_coap_no_lte.conf b/samples/cellular/nrf_cloud_multi_service/overlay_nrf7002ek_wifi_coap_no_lte.conf new file mode 100644 index 000000000000..fd76e9c49b97 --- /dev/null +++ b/samples/cellular/nrf_cloud_multi_service/overlay_nrf7002ek_wifi_coap_no_lte.conf @@ -0,0 +1,222 @@ +# +# Copyright (c) 2023 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# Note that this overlay is not compatible with nrf91 Series! Use something such as the nrf5340 as +# the host microcontroller. + +## Disable Modem/LTE/nrf91-specific features +CONFIG_NRF_MODEM_LIB=n +CONFIG_MODEM_INFO=n +CONFIG_MODEM_INFO_ADD_NETWORK=n +CONFIG_AT_HOST_LIBRARY=n +CONFIG_LTE_LINK_CONTROL=n +CONFIG_LTE_PSM_REQ=n + +## Disable offloaded sockets (we are going to switch to the native networking stack) +CONFIG_NET_SOCKETS_OFFLOAD=n + +## Disable cloud features reliant on or specific to nrf91 modem features +CONFIG_NRF_CLOUD_AGNSS=n +CONFIG_NRF_CLOUD_PGPS=n +CONFIG_NRF_CLOUD_PGPS_REQUEST_UPON_INIT=n +CONFIG_LOCATION_METHOD_GNSS=n +CONFIG_LOCATION_METHOD_CELLULAR=n + +## Disable FOTA to conserve memory +CONFIG_NRF_CLOUD_FOTA=n +CONFIG_BOOTLOADER_MCUBOOT=n +CONFIG_IMG_MANAGER=n +CONFIG_MCUBOOT_IMG_MANAGER=n + +## Disable LTE-event-driven date_time updates (the date_time library will still periodically +## refresh its timestamp, this setting only controls whether LTE-event-driven date_time updates +## are enabled) +CONFIG_DATE_TIME_AUTO_UPDATE=n +CONFIG_DATE_TIME_MODEM=n +CONFIG_DATE_TIME_NTP=y +CONFIG_DATE_TIME_LOG_LEVEL_DBG=y + +## Disable LED patterns since enabling WiFi shield takes control of two LEDs +CONFIG_LED_INDICATION_DISABLED=y + +## Disable LTE conn_mgr bindings +CONFIG_NRF_MODEM_LIB_NET_IF=n + +## These had to be disabled for LTE with conn_mgr, but there is no reason to disable them for +## wifi with conn_mgr, so re-enable them. +CONFIG_DNS_RESOLVER=y + +## Enable Wi-Fi drivers, (and the native NET stack so that the location library can access them) +CONFIG_WIFI=y +CONFIG_WIFI_NRF700X=y +CONFIG_WIFI_NRF700X_SKIP_LOCAL_ADMIN_MAC=y + +## Enable Wi-Fi conn_mgr bindings +CONFIG_L2_WIFI_CONNECTIVITY=y +CONFIG_NET_CONNECTION_MANAGER_MONITOR_STACK_SIZE=8192 + +## Enable WIFI_MGMT_EXT and WIFI_CREDENTIALS for simplified Wi-Fi connection setup +CONFIG_WIFI_CREDENTIALS=y +CONFIG_WIFI_MGMT_EXT=y + +## Use compile-time client ID for nRF Cloud +CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME=y + +## Enable flash and NVS settings, required by WIFI_CREDENTIALS +CONFIG_SETTINGS=y +CONFIG_SETTINGS_NVS=y +CONFIG_NVS=y +CONFIG_FLASH=y +CONFIG_FLASH_PAGE_LAYOUT=y +CONFIG_FLASH_MAP=y + +## Enable Wi-Fi networking and native networking stack +CONFIG_WPA_SUPP=y +CONFIG_NET_L2_ETHERNET=y +CONFIG_NET_NATIVE=y +CONFIG_NRF_SECURITY=y +CONFIG_NET_SOCKETS_SOCKOPT_TLS=y +CONFIG_NET_CONTEXT_SNDTIMEO=y +CONFIG_NET_TCP=y +CONFIG_NET_TCP_WORKQ_STACK_SIZE=4096 +CONFIG_NET_DHCPV4=y + +## Configure native MBEDTLS to support nRF Cloud MQTT +#CONFIG_MQTT_LIB_TLS=y +CONFIG_MBEDTLS=y +CONFIG_MBEDTLS_ENABLE_HEAP=y +CONFIG_MBEDTLS_RSA_C=y +CONFIG_MBEDTLS_SSL_SERVER_NAME_INDICATION=y +CONFIG_MBEDTLS_HEAP_SIZE=120000 + +## Enable Wi-Fi location tracking +CONFIG_LOCATION_TRACKING_WIFI=y +CONFIG_LOCATION_METHOD_WIFI=y +# Align this with CONFIG_LOCATION_METHOD_WIFI_SCANNING_RESULTS_MAX_CNT. +# Also see comments for CONFIG_HEAP_MEM_POOL_SIZE. +# We set this to 30 in this overlay so that +# CONFIG_HEAP_MEM_POOL_SIZE can be small enough to leave at least 24kB of unused RAM in the final +# build, which is required by WPA_supp +CONFIG_NRF_WIFI_SCAN_MAX_BSS_CNT=30 +CONFIG_LOCATION_METHOD_WIFI_SCANNING_RESULTS_MAX_CNT=30 + +## Enable shell and WIFI_CREDENTIALS shell +CONFIG_SHELL=y +CONFIG_WIFI_CREDENTIALS_SHELL=y +# Increased stack size needed for wifi_cred auto_connect command +CONFIG_SHELL_STACK_SIZE=4096 + +## Resource allocation tweaks needed to support Wi-Fi. +CONFIG_MAIN_STACK_SIZE=8192 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192 +CONFIG_NET_MGMT_EVENT_STACK_SIZE=8192 +CONFIG_NRF_CLOUD_CONNECTION_POLL_THREAD_STACK_SIZE=8192 +CONFIG_APPLICATION_THREAD_STACK_SIZE=8192 +CONFIG_MESSAGE_THREAD_STACK_SIZE=3072 +CONFIG_DATE_TIME_THREAD_STACK_SIZE=2048 +CONFIG_NET_TX_STACK_SIZE=4096 +CONFIG_NET_RX_STACK_SIZE=4096 +CONFIG_POSIX_MAX_FDS=16 +CONFIG_NET_SOCKETS_POLL_MAX=8 +# Heap allocation should be changed when CONFIG_LOCATION_METHOD_WIFI_SCANNING_RESULTS_MAX_CNT +# and CONFIG_NRF_WIFI_SCAN_MAX_BSS_CNT (which should be the same value) are changed. With the limit at 30, +# this could be set as low as 130000. Add 256 bytes for each additional scanning result, assuming +# sane SSID lengths. We set the heap much higher in case of long SSIDs. +CONFIG_HEAP_MEM_POOL_SIZE=130000 + +# Disable PICOLIBC since hostap currently is not compatible with it +CONFIG_NEWLIB_LIBC=y +CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y +CONFIG_PICOLIBC=n + + +# ---------- COAP STUFF -----------------# +# nRF Cloud +CONFIG_NRF_CLOUD_MQTT=n +CONFIG_NRF_CLOUD_COAP=y +CONFIG_NRF_CLOUD_ALERT=y +CONFIG_NRF_CLOUD_FOTA=n +CONFIG_NRF_CLOUD_LOCATION=n + +# General config +CONFIG_FPU=y +CONFIG_NEWLIB_LIBC_NANO=n + +# Rate of cloud interactions +# These are faster than one normally would use in a low power device. +# This is strictly for demo purposes. +CONFIG_SENSOR_SAMPLE_INTERVAL_SECONDS=120 +CONFIG_LOCATION_TRACKING_SAMPLE_INTERVAL_SECONDS=360 + +# Logs +CONFIG_LOG=y +CONFIG_NET_LOG=y +CONFIG_LOG_PRINTK=y +CONFIG_COAP_LOG_LEVEL_INF=y +CONFIG_LOCATION_LOG_LEVEL_INF=y +CONFIG_MULTI_SERVICE_LOG_LEVEL_INF=y + +# LTE link control - used by PGPS and main application +CONFIG_LTE_LINK_CONTROL=n +#CONFIG_NET_L2_VIRTUAL=y + +# Modem +CONFIG_MODEM_KEY_MGMT=y +CONFIG_MODEM_JWT=n +CONFIG_MODEM_INFO_ADD_DEVICE=n +CONFIG_MODEM_INFO_ADD_DATE_TIME=n +CONFIG_MODEM_INFO_ADD_SIM=n +CONFIG_MODEM_INFO_ADD_SIM_ICCID=n +CONFIG_MODEM_INFO_ADD_SIM_IMSI=n + +# Network +CONFIG_NET_SOCKETS_POSIX_NAMES=y +CONFIG_NET_SOCKETS_TLS_SET_MAX_FRAGMENT_LENGTH=y +CONFIG_NET_SOCKETS_OFFLOAD_PRIORITY=40 + +# CoAP Client +CONFIG_COAP_CLIENT_BLOCK_SIZE=1024 +CONFIG_COAP_CLIENT_STACK_SIZE=6144 +CONFIG_COAP_CLIENT_THREAD_PRIORITY=0 +CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE=40 + +# Location Services configuration +CONFIG_LOCATION_DATA_DETAILS=n + +CONFIG_APPLICATION_THREAD_STACK_SIZE=8192 +CONFIG_TEMP_ALERT_LIMIT=24 + +CONFIG_AT_MONITOR=n + +# -------- TESTING STUFF +CONFIG_NRF_CLOUD_PROVISION_CERTIFICATES=n +CONFIG_NRF_CLOUD_COAP_WIFI_ADD_CREDS_FROM_FILE=y + +CONFIG_NRF_JWT=y +CONFIG_NRF_JWT_DER=y +CONFIG_NRF_CLOUD_JWT_GEN_METHOD_DER=y + +CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME=y +CONFIG_NRF_CLOUD_CLIENT_ID="justin_53DK_wifi" + +CONFIG_CONNECTION_THREAD_STACK_SIZE=4096 + +CONFIG_NRF_JWT_LOG_LEVEL_DBG=y +CONFIG_NRF_CLOUD_LOG_LEVEL_DBG=y +CONFIG_NRF_CLOUD_COAP_LOG_LEVEL_DBG=y + +CONFIG_MBEDTLS_DTLS=y +CONFIG_MBEDTLS_SSL_DTLS_CONNECTION_ID=y +CONFIG_NET_SOCKETS_ENABLE_DTLS=y + +CONFIG_COAP_FOTA=n + +CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=20 + +# Disabling to prevent IPv6 error logs +CONFIG_NET_IPV6=n +#CONFIG_NET_IPV6_NBR_CACHE=y +#CONFIG_NET_IPV6_MLD=y diff --git a/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c b/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c index 0cbf591754d7..08d8f6f3e317 100644 --- a/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c +++ b/samples/cellular/nrf_cloud_multi_service/src/cloud_connection.c @@ -583,6 +583,13 @@ void cloud_connection_thread_fn(void) (void)await_provisioning_idle(K_FOREVER); } + /* TODO: Obtain time before connecting to the cloud, + * otherwise NTP query will fail. */ + if (IS_ENABLED(CONFIG_WIFI)) { + LOG_INF("Waiting to obtain date/time"); + (void)await_date_time_known(K_FOREVER); + } + /* Attempt to connect to nRF Cloud. */ if (connect_cloud()) { LOG_DBG("Awaiting disconnection from nRF Cloud");