Skip to content

Commit

Permalink
zephyr: add complete sample application
Browse files Browse the repository at this point in the history
This sample can be built with west using:

west build -b <board> zephyr/samples/publication

features can be enabled/disable using Kconfig in prj.conf

Signed-off-by: Anas Nashif <[email protected]>
  • Loading branch information
nashif committed Apr 4, 2024
1 parent b0c4a1e commit f00fc59
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
10 changes: 10 additions & 0 deletions zephyr/samples/publication/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
18 changes: 18 additions & 0 deletions zephyr/samples/publication/prj.conf
Original file line number Diff line number Diff line change
@@ -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
91 changes: 91 additions & 0 deletions zephyr/samples/publication/src/main.c
Original file line number Diff line number Diff line change
@@ -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, <[email protected]>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <zenoh-pico.h>

#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

0 comments on commit f00fc59

Please sign in to comment.