Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session restart example #218

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions examples/zephyr/z_pub_session_retry.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// 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 PEER "" // If empty, it will scout
#elif CLIENT_OR_PEER == 1
#define MODE "peer"
#define PEER "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!"

int main(int argc, char **argv) {

while (true) {
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(PEER, "") != 0) {
zp_config_insert(z_loan(config), Z_CONFIG_PEER_KEY, z_string_make(PEER));
}

// 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");
bool session_is_valid = true;

// 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; session_is_valid; ++idx) {
sleep(1);
sprintf(buf, "[%4d] %s", idx, VALUE);
printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf);
int put_res = z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), NULL);
if (put_res != _Z_RES_OK) {
printf("Error publishing message (%d)\n", put_res);
session_is_valid = false;
}
if(!z_check(s)) {
printf("Zenoh session has become invalid\n");
}
}

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;
}
4 changes: 2 additions & 2 deletions include/zenoh-pico/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
*/
#ifndef Z_BATCH_SIZE_RX
#define Z_BATCH_SIZE_RX \
65535 // Warning: changing this value can break the communication
3072 // Warning: changing this value can break the communication
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not intend to merge changes made to this file. They are just to make the session opening work as my microcontroller doesn't have enough RAM for the default value.

// with zenohd in the current protocol version.
// In the future, it will be possible to negotiate such value.
// Change it at your own risk.
Expand All @@ -232,7 +232,7 @@
* Defaulf maximum batch size possible to be sent.
*/
#ifndef Z_BATCH_SIZE_TX
#define Z_BATCH_SIZE_TX 65535
#define Z_BATCH_SIZE_TX 3072
#endif

/**
Expand Down