diff --git a/examples/rpi_pico_w/CMakeLists.txt b/examples/rpi_pico_w/CMakeLists.txt index 2bcb0240e..6c85e66b4 100644 --- a/examples/rpi_pico_w/CMakeLists.txt +++ b/examples/rpi_pico_w/CMakeLists.txt @@ -98,10 +98,10 @@ endfunction() add_example(z_get) add_example(z_pub) -#add_example(z_pub_st) +add_example(z_pub_st) add_example(z_pull) add_example(z_put) add_example(z_queryable) add_example(z_scout) add_example(z_sub) -#add_example(z_sub_st) +add_example(z_sub_st) diff --git a/examples/rpi_pico_w/app_main.c b/examples/rpi_pico_w/app_main.c deleted file mode 100644 index 2942db5db..000000000 --- a/examples/rpi_pico_w/app_main.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include - -#include "zenoh-pico/utils/result.h" - -int app_main() { - z_owned_config_t config; - z_config_default(&config); - zp_config_insert(z_loan_mut(config), Z_CONFIG_MODE_KEY, "client"); - // zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, "tcp/192.168.0.108:7447"); - zp_config_insert(z_loan_mut(config), Z_CONFIG_CONNECT_KEY, "tcp/192.168.21.28:7447"); - - printf("Opening session...\n"); - z_owned_session_t s; - z_result_t ret = z_open(&s, z_move(config), NULL); - if (ret < 0) { - printf("Unable to open session: %i!\n", ret); - return -1; - } - - // Start read and lease tasks for zenoh-pico - if (zp_start_read_task(z_loan_mut(s), NULL) < 0 || zp_start_lease_task(z_loan_mut(s), NULL) < 0) { - printf("Unable to start read and lease tasks\n"); - z_drop(z_move(s)); - return -1; - } - - const char* keyexpr = "demo/example/zenoh-pico-pub"; - const char* value = "[RPI] Publication from Zenoh-Pico!"; - - // Declare publisher - printf("Declaring publisher for '%s'...\n", keyexpr); - z_owned_publisher_t pub; - z_view_keyexpr_t ke; - z_view_keyexpr_from_str(&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 -1; - } - - // Publish data - char buf[256]; - for (int idx = 0;; ++idx) { - z_sleep_s(1); - sprintf(buf, "[%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); - } - // Clean up - z_drop(z_move(pub)); - z_drop(z_move(s)); - - return 0; -} diff --git a/examples/rpi_pico_w/z_pub_st.c b/examples/rpi_pico_w/z_pub_st.c new file mode 100644 index 000000000..963918330 --- /dev/null +++ b/examples/rpi_pico_w/z_pub_st.c @@ -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, +// + +#include +#include + +#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 diff --git a/examples/rpi_pico_w/z_sub_st.c b/examples/rpi_pico_w/z_sub_st.c new file mode 100644 index 000000000..2acfd608a --- /dev/null +++ b/examples/rpi_pico_w/z_sub_st.c @@ -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, +// + +#include +#include + +#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