-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
zephyr: add complete sample application
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
Showing
3 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |