diff --git a/examples/arduino/z_pub.ino b/examples/arduino/z_pub.ino index df03b0bcf..c487c976b 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -99,13 +99,18 @@ void loop() { delay(1000); char buf[256]; sprintf(buf, "[%4d] %s", idx++, VALUE); + Serial.print("Writing Data ('"); Serial.print(KEYEXPR); Serial.print("': '"); Serial.print(buf); Serial.println("')"); - if (z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL) < 0) { + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + if (z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL) < 0) { Serial.println("Error while publishing data"); } } diff --git a/examples/espidf/z_pub.c b/examples/espidf/z_pub.c index 53cb2be99..5d1407239 100644 --- a/examples/espidf/z_pub.c +++ b/examples/espidf/z_pub.c @@ -154,7 +154,12 @@ void app_main() { 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); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); } printf("Closing Zenoh Session..."); diff --git a/examples/freertos_plus_tcp/z_pub.c b/examples/freertos_plus_tcp/z_pub.c index f19ddc23e..e86ce07bd 100644 --- a/examples/freertos_plus_tcp/z_pub.c +++ b/examples/freertos_plus_tcp/z_pub.c @@ -97,9 +97,13 @@ void app_main(void) { snprintf(buf, 256, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + z_publisher_put_options_t options; z_publisher_put_options_default(&options); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean-up diff --git a/examples/freertos_plus_tcp/z_pub_st.c b/examples/freertos_plus_tcp/z_pub_st.c index bfdd22692..6239171b7 100644 --- a/examples/freertos_plus_tcp/z_pub_st.c +++ b/examples/freertos_plus_tcp/z_pub_st.c @@ -62,7 +62,12 @@ void app_main(void) { if (z_clock_elapsed_ms(&now) > 1000) { snprintf(buf, 256, "[%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); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); ++idx; now = z_clock_now(); diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index 092ed503b..192b441bf 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -66,7 +66,12 @@ void app_main(void) { printf("Putting Data ('%s': '%s')...\n", KEYEXPR, VALUE); z_put_options_t options; z_put_options_default(&options); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)VALUE, strlen(VALUE), &options) < 0) { + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, VALUE); + + if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index 08bfd624b..c709e584a 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -74,7 +74,12 @@ int main(int argc, char **argv) { z_sleep_s(1); sprintf(buf, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); } printf("Closing Zenoh Session..."); diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index fa7744d64..f12e3b44f 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -106,7 +106,11 @@ int main(int argc, char** argv) { z_clock_t warmup_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < args.warmup_ms * 1000) { - z_publisher_put(z_loan(pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); elapsed_us = z_clock_elapsed_us(&warmup_start); } @@ -114,7 +118,11 @@ int main(int argc, char** argv) { unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { z_clock_t measure_start = z_clock_now(); - z_publisher_put(z_loan(pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); results[i] = z_clock_elapsed_us(&measure_start); } diff --git a/examples/unix/c11/z_pong.c b/examples/unix/c11/z_pong.c index c7e56e5d0..838da05d6 100644 --- a/examples/unix/c11/z_pong.c +++ b/examples/unix/c11/z_pong.c @@ -20,7 +20,9 @@ void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL); + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, z_slice_data(z_loan(value)), z_slice_len(z_loan(value))); + z_publisher_put(pub, z_move(payload), NULL); z_drop(z_move(value)); } void drop(void* context) { diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 576edf915..f65f7a172 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -112,6 +112,10 @@ int main(int argc, char **argv) { sprintf(buf, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, "utf8"); @@ -119,7 +123,7 @@ int main(int argc, char **argv) { z_publisher_put_options_default(&options); options.encoding = z_move(encoding); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean up z_undeclare_publisher(z_move(pub)); diff --git a/examples/unix/c11/z_pub_attachment.c b/examples/unix/c11/z_pub_attachment.c index 5db64ca4e..2376ffd7f 100644 --- a/examples/unix/c11/z_pub_attachment.c +++ b/examples/unix/c11/z_pub_attachment.c @@ -151,13 +151,19 @@ int main(int argc, char **argv) { z_owned_bytes_t attachment; // Allocate buffer - char buf[256]; char buf_ind[16]; // Publish data printf("Press CTRL-C to quit...\n"); + char buf[256]; for (int idx = 0; idx < n; ++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_serialize_from_string(&payload, buf); // Add attachment value sprintf(buf_ind, "%d", idx); @@ -166,9 +172,7 @@ int main(int argc, char **argv) { zp_bytes_serialize_from_iter(&attachment, create_attachment_iter, (void *)&ctx, kv_pairs_size(&ctx)); options.attachment = z_move(attachment); - 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), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean up z_undeclare_publisher(z_move(pub)); diff --git a/examples/unix/c11/z_pub_st.c b/examples/unix/c11/z_pub_st.c index 51037f4a4..0c6fbe697 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -95,7 +95,12 @@ int main(int argc, char **argv) { 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); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); zp_read(z_loan(s), NULL); zp_send_keep_alive(z_loan(s), NULL); diff --git a/examples/unix/c11/z_pub_thr.c b/examples/unix/c11/z_pub_thr.c index 228e70f01..a31911ee7 100644 --- a/examples/unix/c11/z_pub_thr.c +++ b/examples/unix/c11/z_pub_thr.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { } char *keyexpr = "test/thr"; size_t len = (size_t)atoi(argv[1]); - uint8_t *value = (uint8_t *)malloc(len); + uint8_t *value = (uint8_t *)z_malloc(len); memset(value, 1, len); // Set config @@ -61,14 +61,18 @@ int main(int argc, char **argv) { // Send packets while (1) { - z_publisher_put(z_loan(pub), (const uint8_t *)value, len, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, (char *)value); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); } // Clean up z_undeclare_publisher(z_move(pub)); zp_stop_read_task(z_loan_mut(s)); zp_stop_lease_task(z_loan_mut(s)); z_close(z_move(s)); - free(value); + z_free(value); exit(0); } #else diff --git a/examples/unix/c11/z_put.c b/examples/unix/c11/z_put.c index de8b39176..90efbb848 100644 --- a/examples/unix/c11/z_put.c +++ b/examples/unix/c11/z_put.c @@ -97,6 +97,10 @@ int main(int argc, char **argv) { return -1; } + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -105,7 +109,7 @@ int main(int argc, char **argv) { z_put_options_t options; z_put_options_default(&options); options.encoding = z_move(encoding); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } // Clean up diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 17fe7833c..d74a5b988 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -109,7 +109,11 @@ int main(int argc, char** argv) { z_clock_t warmup_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < args.warmup_ms * 1000) { - z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); z_condvar_wait(&cond, &mutex); elapsed_us = z_clock_elapsed_us(&warmup_start); } @@ -117,7 +121,12 @@ int main(int argc, char** argv) { unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { z_clock_t measure_start = z_clock_now(); - z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); z_condvar_wait(&cond, &mutex); results[i] = z_clock_elapsed_us(&measure_start); } diff --git a/examples/unix/c99/z_pong.c b/examples/unix/c99/z_pong.c index f5060147f..7ec6528f9 100644 --- a/examples/unix/c99/z_pong.c +++ b/examples/unix/c99/z_pong.c @@ -21,7 +21,12 @@ void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_publisher_loan((z_owned_publisher_t*)context); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - z_publisher_put(pub, z_slice_data(z_slice_loan(&value)), z_slice_len(z_slice_loan(&value)), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, z_slice_data(z_slice_loan(&value)), z_slice_len(z_slice_loan(&value))); + + z_publisher_put(pub, z_bytes_move(&payload), NULL); z_slice_drop(z_slice_move(&value)); } void drop(void* context) { diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 91e5799c2..5395865c3 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -102,6 +102,10 @@ int main(int argc, char **argv) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -109,7 +113,7 @@ int main(int argc, char **argv) { z_publisher_put_options_default(&options); options.encoding = z_encoding_move(&encoding); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), &options); } // Clean up z_undeclare_publisher(z_publisher_move(&pub)); diff --git a/examples/unix/c99/z_pub_st.c b/examples/unix/c99/z_pub_st.c index d5d8220d8..22fc17e79 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -95,7 +95,12 @@ int main(int argc, char **argv) { if (z_clock_elapsed_ms(&now) > 1000) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_publisher_loan(&pub), z_bytes_move(&payload), NULL); ++idx; now = z_clock_now(); diff --git a/examples/unix/c99/z_put.c b/examples/unix/c99/z_put.c index 701432fa6..dff0186e1 100644 --- a/examples/unix/c99/z_put.c +++ b/examples/unix/c99/z_put.c @@ -93,6 +93,10 @@ int main(int argc, char **argv) { return -1; } + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -101,7 +105,7 @@ int main(int argc, char **argv) { z_put_options_t options; z_put_options_default(&options); options.encoding = z_encoding_move(&encoding); - if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_session_loan(&s), z_keyexpr_loan(&ke), z_bytes_move(&payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 8dfd075ca..e45f7cdc6 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -105,7 +105,11 @@ int main(int argc, char** argv) { z_clock_t warmup_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < args.warmup_ms * 1000) { - z_publisher_put(z_loan(pub), data, args.size, NULL); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); elapsed_us = z_clock_elapsed_us(&warmup_start); } @@ -113,7 +117,12 @@ int main(int argc, char** argv) { unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { z_clock_t measure_start = z_clock_now(); - z_publisher_put(z_loan(pub), data, args.size, NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, data, args.size); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); z_condvar_wait(&cond, &mutex); results[i] = z_clock_elapsed_us(&measure_start); } diff --git a/examples/windows/z_pong.c b/examples/windows/z_pong.c index c7e56e5d0..dd4c23858 100644 --- a/examples/windows/z_pong.c +++ b/examples/windows/z_pong.c @@ -20,7 +20,12 @@ void callback(const z_loaned_sample_t* sample, void* context) { const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context); z_owned_slice_t value; z_bytes_deserialize_into_slice(z_sample_payload(sample), &value); - z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_slice(&payload, z_slice_data(z_loan(value)), z_slice_len(z_loan(value))); + + z_publisher_put(pub, z_move(payload), NULL); z_drop(z_move(value)); } void drop(void* context) { diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 231edeb66..13e748a22 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -64,6 +64,10 @@ int main(int argc, char **argv) { snprintf(buf, 256, "[%4d] %s", idx, value); printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -71,7 +75,7 @@ int main(int argc, char **argv) { z_publisher_put_options_default(&options); options.encoding = z_move(encoding); - z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); + z_publisher_put(z_loan(pub), z_move(payload), &options); } // Clean-up diff --git a/examples/windows/z_pub_st.c b/examples/windows/z_pub_st.c index 959fb2b6f..75c19e6bd 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -59,7 +59,12 @@ int main(int argc, char **argv) { if (z_clock_elapsed_ms(&now) > 1000) { snprintf(buf, 256, "[%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); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); ++idx; now = z_clock_now(); diff --git a/examples/windows/z_put.c b/examples/windows/z_put.c index e3c9b5982..4b3a76dda 100644 --- a/examples/windows/z_put.c +++ b/examples/windows/z_put.c @@ -59,6 +59,11 @@ int main(int argc, char **argv) { z_close(z_move(s)); return -1; } + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, value); + // Create encoding z_owned_encoding_t encoding; zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL); @@ -67,7 +72,7 @@ int main(int argc, char **argv) { z_put_options_t options; z_put_options_default(&options); options.encoding = z_move(encoding); - if (z_put(z_loan(s), z_loan(ke), (const uint8_t *)value, strlen(value), &options) < 0) { + if (z_put(z_loan(s), z_loan(ke), z_move(payload), &options) < 0) { printf("Oh no! Put has failed...\n"); } diff --git a/examples/zephyr/z_pub.c b/examples/zephyr/z_pub.c index 209f91dc5..441342e7a 100644 --- a/examples/zephyr/z_pub.c +++ b/examples/zephyr/z_pub.c @@ -71,7 +71,12 @@ int main(int argc, char **argv) { 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); + + // Create payload + z_owned_bytes_t payload; + z_bytes_serialize_from_string(&payload, buf); + + z_publisher_put(z_loan(pub), z_move(payload), NULL); } printf("Closing Zenoh Session...");