-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
166 additions
and
61 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
This file was deleted.
Oops, something went wrong.
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,83 @@ | ||
// | ||
// Copyright (c) 2024 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 <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_PUBLICATION == 1 | ||
|
||
#define IFACE "#iface=lo" // Not used by this platform, but should be present | ||
#define KEYEXPR "demo/example/zenoh-pico-pub" | ||
#define VALUE "[RPI] Pub from Zenoh-Pico!" | ||
|
||
void app_main(void) { | ||
z_owned_config_t config; | ||
z_config_default(&config); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, ZENOH_CONFIG_MODE); | ||
if (strcmp(ZENOH_CONFIG_CONNECT, "") != 0) { | ||
printf("Connect endpoint: %s\n", ZENOH_CONFIG_CONNECT); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT); | ||
} | ||
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) { | ||
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE); | ||
} | ||
|
||
printf("Opening %s session ...\n", ZENOH_CONFIG_MODE); | ||
z_owned_session_t s; | ||
if (z_open(&s, z_move(config), NULL) < 0) { | ||
printf("Unable to open session!\n"); | ||
return; | ||
} | ||
|
||
printf("Declaring publisher for '%s'...\n", KEYEXPR); | ||
z_owned_publisher_t pub; | ||
z_view_keyexpr_t ke; | ||
z_view_keyexpr_from_str_unchecked(&ke, KEYEXPR); | ||
if (z_declare_publisher(z_loan(s), &pub, z_loan(ke), NULL) < 0) { | ||
printf("Unable to declare publisher for key expression!\n"); | ||
return; | ||
} | ||
|
||
char *buf = (char *)pvPortMalloc(256); | ||
z_clock_t now = z_clock_now(); | ||
for (int idx = 0;; ++idx) { | ||
if (z_clock_elapsed_ms(&now) > 1000) { | ||
snprintf(buf, 256, "[%4d] %s", idx, VALUE); | ||
printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); | ||
|
||
// Create payload | ||
z_owned_bytes_t payload; | ||
z_bytes_copy_from_str(&payload, buf); | ||
|
||
z_publisher_put(z_loan(pub), z_move(payload), NULL); | ||
++idx; | ||
|
||
now = z_clock_now(); | ||
} | ||
|
||
zp_read(z_loan(s), NULL); | ||
zp_send_keep_alive(z_loan(s), NULL); | ||
zp_send_join(z_loan(s), NULL); | ||
} | ||
|
||
z_drop(z_move(pub)); | ||
|
||
z_drop(z_move(s)); | ||
} | ||
#else | ||
void app_main(void) { | ||
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_PUBLICATION but this example requires it.\n"); | ||
} | ||
#endif |
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,81 @@ | ||
// | ||
// Copyright (c) 2024 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 <zenoh-pico.h> | ||
|
||
#if Z_FEATURE_SUBSCRIPTION == 1 | ||
|
||
#define IFACE "#iface=lo" // Not used by this platform, but should be present | ||
#define KEYEXPR "demo/example/zenoh-pico-pub" | ||
#define VALUE "[RPI] Pub from Zenoh-Pico!" | ||
int msg_nb = 0; | ||
|
||
void data_handler(z_loaned_sample_t *sample, void *ctx) { | ||
(void)(ctx); | ||
z_view_string_t keystr; | ||
z_keyexpr_as_view_string(z_sample_keyexpr(sample), &keystr); | ||
z_owned_string_t value; | ||
z_bytes_to_string(z_sample_payload(sample), &value); | ||
printf(">> [Subscriber] Received ('%.*s': '%.*s')\n", (int)z_string_len(z_loan(keystr)), | ||
z_string_data(z_loan(keystr)), (int)z_string_len(z_loan(value)), z_string_data(z_loan(value))); | ||
z_drop(z_move(value)); | ||
msg_nb++; | ||
} | ||
|
||
void app_main(void) { | ||
z_owned_config_t config; | ||
z_config_default(&config); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, ZENOH_CONFIG_MODE); | ||
if (strcmp(ZENOH_CONFIG_CONNECT, "") != 0) { | ||
printf("Connect endpoint: %s\n", ZENOH_CONFIG_CONNECT); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, ZENOH_CONFIG_CONNECT); | ||
} | ||
if (strcmp(ZENOH_CONFIG_LISTEN, "") != 0) { | ||
printf("Listen endpoint: %s\n", ZENOH_CONFIG_LISTEN IFACE); | ||
zp_config_insert(z_loan_mut(config), Z_CONFIG_LISTEN_KEY, ZENOH_CONFIG_LISTEN IFACE); | ||
} | ||
|
||
printf("Opening %s session ...\n", ZENOH_CONFIG_MODE); | ||
z_owned_session_t s; | ||
if (z_open(&s, z_move(config), NULL) < 0) { | ||
printf("Unable to open session!\n"); | ||
return; | ||
} | ||
|
||
z_owned_closure_sample_t callback; | ||
z_closure(&callback, data_handler, NULL, NULL); | ||
printf("Declaring Subscriber on '%s'...\n", KEYEXPR); | ||
z_view_keyexpr_t ke; | ||
z_view_keyexpr_from_str_unchecked(&ke, KEYEXPR); | ||
z_owned_subscriber_t sub; | ||
if (z_declare_subscriber(z_loan(s), &sub, z_loan(ke), z_move(callback), NULL) < 0) { | ||
printf("Unable to declare subscriber.\n"); | ||
return; | ||
} | ||
|
||
while (true) { | ||
zp_read(z_loan(s), NULL); | ||
zp_send_keep_alive(z_loan(s), NULL); | ||
zp_send_join(z_loan(s), NULL); | ||
} | ||
|
||
z_drop(z_move(sub)); | ||
z_drop(z_move(s)); | ||
} | ||
#else | ||
void app_main(void) { | ||
printf("ERROR: Zenoh pico was compiled without Z_FEATURE_SUBSCRIPTION but this example requires it.\n"); | ||
} | ||
#endif |