Skip to content

Commit

Permalink
Add single thread examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc committed Dec 3, 2024
1 parent f9f595d commit 5cd11db
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 61 deletions.
4 changes: 2 additions & 2 deletions examples/rpi_pico_w/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
59 changes: 0 additions & 59 deletions examples/rpi_pico_w/app_main.c

This file was deleted.

83 changes: 83 additions & 0 deletions examples/rpi_pico_w/z_pub_st.c
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
81 changes: 81 additions & 0 deletions examples/rpi_pico_w/z_sub_st.c
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

0 comments on commit 5cd11db

Please sign in to comment.