From f00fc599ffdee06fbce9775866cc8b7ea198aa45 Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Thu, 4 Apr 2024 10:16:05 -0400 Subject: [PATCH] zephyr: add complete sample application This sample can be built with west using: west build -b zephyr/samples/publication features can be enabled/disable using Kconfig in prj.conf Signed-off-by: Anas Nashif --- zephyr/samples/publication/CMakeLists.txt | 10 +++ zephyr/samples/publication/prj.conf | 18 +++++ zephyr/samples/publication/src/main.c | 91 +++++++++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100644 zephyr/samples/publication/CMakeLists.txt create mode 100644 zephyr/samples/publication/prj.conf create mode 100644 zephyr/samples/publication/src/main.c diff --git a/zephyr/samples/publication/CMakeLists.txt b/zephyr/samples/publication/CMakeLists.txt new file mode 100644 index 000000000..cec477f87 --- /dev/null +++ b/zephyr/samples/publication/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.13.1) + +add_definitions(-DSHIELD=link_board_eth) +set(DTC_OVERLAY_FILE $ENV{ZEPHYR_BASE}/boards/shields/link_board_eth/link_board_eth.overlay) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(zenoh-pico-zephyr) + +FILE(GLOB app_sources src/*.c*) +target_sources(app PRIVATE ${app_sources}) diff --git a/zephyr/samples/publication/prj.conf b/zephyr/samples/publication/prj.conf new file mode 100644 index 000000000..e2c55a69f --- /dev/null +++ b/zephyr/samples/publication/prj.conf @@ -0,0 +1,18 @@ +# Kernel options +CONFIG_MAIN_STACK_SIZE=5120 +CONFIG_HEAP_MEM_POOL_SIZE=150000 +CONFIG_ENTROPY_GENERATOR=y +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_INIT_STACKS=y + +CONFIG_ZENOH_PICO=y +CONFIG_ZENOH_PICO_LINK_SERIAL=y +CONFIG_ZENOH_PICO_PUBLICATION=y + +CONFIG_POSIX_API=y + +# Network driver config +CONFIG_TEST_RANDOM_GENERATOR=y + +CONFIG_NEWLIB_LIBC=y +CONFIG_NEWLIB_LIBC_NANO=n diff --git a/zephyr/samples/publication/src/main.c b/zephyr/samples/publication/src/main.c new file mode 100644 index 000000000..1f47be9a0 --- /dev/null +++ b/zephyr/samples/publication/src/main.c @@ -0,0 +1,91 @@ +// +// Copyright (c) 2022 ZettaScale Technology +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License 2.0 which is available at +// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// +// Contributors: +// ZettaScale Zenoh Team, + +#include +#include +#include +#include +#include + +#define CLIENT_OR_PEER 0 // 0: Client mode; 1: Peer mode +#if CLIENT_OR_PEER == 0 +#define MODE "client" +#define CONNECT "" // If empty, it will scout +#elif CLIENT_OR_PEER == 1 +#define MODE "peer" +#define CONNECT "udp/224.0.0.225:7447#iface=en0" +#else +#error "Unknown Zenoh operation mode. Check CLIENT_OR_PEER value." +#endif + +#define KEYEXPR "demo/example/zenoh-pico-pub" +#define VALUE "[STSTM32]{nucleo-F767ZI} Pub from Zenoh-Pico!" + +#if Z_FEATURE_PUBLICATION == 1 +int main(int argc, char **argv) { + sleep(5); + + // Initialize Zenoh Session and other parameters + z_owned_config_t config = z_config_default(); + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(MODE)); + if (strcmp(CONNECT, "") != 0) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(CONNECT)); + } + + // Open Zenoh session + printf("Opening Zenoh Session..."); + z_owned_session_t s = z_open(z_move(config)); + if (!z_check(s)) { + printf("Unable to open session!\n"); + exit(-1); + } + printf("OK\n"); + + // Start the receive and the session lease loop for zenoh-pico + zp_start_read_task(z_loan(s), NULL); + zp_start_lease_task(z_loan(s), NULL); + + printf("Declaring publisher for '%s'...", KEYEXPR); + z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr(KEYEXPR), NULL); + if (!z_check(pub)) { + printf("Unable to declare publisher for key expression!\n"); + exit(-1); + } + printf("OK\n"); + + char buf[256]; + for (int idx = 0; 1; ++idx) { + sleep(1); + sprintf(buf, "[%4d] %s", idx, VALUE); + printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); + z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL); + } + + printf("Closing Zenoh Session..."); + z_undeclare_publisher(z_move(pub)); + + // Stop the receive and the session lease loop for zenoh-pico + zp_stop_read_task(z_loan(s)); + zp_stop_lease_task(z_loan(s)); + + z_close(z_move(s)); + printf("OK!\n"); + + return 0; +} +#else +int main(void) { + printf("ERROR: Zenoh pico was compiled without Z_FEATURE_PUBLICATION but this example requires it.\n"); + return -2; +} +#endif