From 265405d1ea67f3065d1298b6e2bbdb7378506d0a Mon Sep 17 00:00:00 2001 From: Jean-Roland Gosse Date: Fri, 5 Apr 2024 15:42:19 +0200 Subject: [PATCH] Fix CI failures (#394) * add support for qos settings in sample * - silence cpp compiler regading compound literals and type conversions, - add missing dots in docs * fix _z_qos_t definition and doc * fix z_qos_t definition and doc * - replace binary literals (non-core C) with hexadecimal ones - replace _z_n_qos_unmake with _z_n_qos_unmake_public in subscription.c * make _z_n_qos_unmake static inline to prevent linking errors * fix format * reduce qos size to 1 byte; use getters to extract individual qos settings * fix _z_n_qos_make to no longer use compund literals to avoid warnings from zenoh-cpp * fix z_qos_default() signature * Add Flipper platform (#351) * Add Flipper platform * Fix warnings and other platform builds * move z_n_qos_t related functions into .c (to hide designated initializers from c++) * z_sample_t docs update to include qos * move qos functions back to header and remove usage of designated initializers * format * fix: Bump sphinx doc build dep to `7.2.6` (#361) * replac zp_ prefix for platform functions that are also available in zenoh-c * fix format * replace zp_ prefix with z_ for zp_random_u64 * put back previously removed zp_ functions as deprecated * added missing new line at the end of the file * typo fix * move deprecated platform header to a separate folder * Add config endpoint in raweth locator (#364) * feat: add ethtype to reth endpoint config * feat: type renaming * fix: remove unused macro call * feat: remove static raweth config * feat: use defines for separators * feat: add mapping function * fix: add default raweth mapping * feat: add debug traces on raweth config parsing * fix: ethtype validity test * fix: strtok parsing calls * chore: clang-format * fix: include first mapping entry in lookup * fix: appease the tyran codacy * Fix bug reading from ws (#370) * Expose timeout option in z_get_options_t (#375) * Expose timeout option in z_get_options_t * Add default get timeout * Align examples and remove reading from stdin (#359) * Remove reading from stdin, align example implementations * Update examples tests * Adjust z_sub_thr output * Fix z_get single query examples * Replace pthread uses with z_mutex and z_condvar * Add multi-thread feature condition to z_get examples * Update error message for features absence * Update z_get expected output in modularity test * Update sample count for freertos single-thread examples * fix: remove unused variable warning (#379) * Changing references to zenoh:master to zenoh:main. (#381) * correct unsigned atomic in refcount.h (#382) * Fix refcount cast (#384) * Fix esp32 CI (#386) * fix: const discard warning * build: fix platformio dependency check * Serial timeout (#383) * correct unsigned atomic in refcount.h * made espidf freertos compatible with timeout on read --------- Co-authored-by: Luca Cominardi * Run clang-format (#388) * Fix misra violations (#390) * chore: run clang format * fix: misra issues * ci: Allow building zenoh from arbitrary branch in build-check workflow (#389) * ci: Allow building Zenoh from arbitrary branch in build-check workflow * Update other jobs --------- Co-authored-by: OlivierHecart * build: point ci to zenoh protocol_changes branch * fix: check declare flag at declare level * fix: missing function prototype warning * build: switch zenoh branch to interests * fix: ignore unknown final interest * fix: missing argument in tigger_local_subs * fix: add wait join step in z_pub for raweth testing * fix: raweth memory leaks * feat: add message number option to z_sub * fix: null malloc when cloning rc list * doc: remove obsolete types * fix: encoding option made fragment test fail * fix: replace all deprecated system calls * fix: improve raweth test stability --------- Co-authored-by: Denis Biryukov Co-authored-by: Alexander Co-authored-by: Michael Ilyin Co-authored-by: Mahmoud Mazouz Co-authored-by: OlivierHecart Co-authored-by: Alexander Co-authored-by: oteffahi <70609372+oteffahi@users.noreply.github.com> Co-authored-by: Geoff Martin Co-authored-by: Lieven Co-authored-by: Luca Cominardi Co-authored-by: OlivierHecart --- .github/workflows/arduino_esp32.yaml | 2 +- .github/workflows/build-check.yaml | 83 ++++- CMakeLists.txt | 1 + GNUmakefile | 2 +- README.md | 8 +- docs/api.rst | 11 - docs/requirements.txt | 8 +- examples/arduino/z_pub.ino | 3 +- examples/arduino/z_queryable.ino | 2 +- examples/arduino/z_sub.ino | 2 +- examples/espidf/z_queryable.c | 2 +- examples/espidf/z_sub.c | 2 +- examples/freertos_plus_tcp/z_get.c | 2 +- examples/freertos_plus_tcp/z_pub.c | 6 +- examples/freertos_plus_tcp/z_pub_st.c | 9 +- examples/freertos_plus_tcp/z_pull.c | 2 +- examples/freertos_plus_tcp/z_put.c | 2 +- examples/freertos_plus_tcp/z_queryable.c | 2 +- examples/freertos_plus_tcp/z_sub.c | 2 +- examples/freertos_plus_tcp/z_sub_st.c | 7 +- examples/mbed/z_get.cpp | 2 +- examples/mbed/z_pub.cpp | 2 +- examples/mbed/z_pull.cpp | 2 +- examples/mbed/z_queryable.cpp | 2 +- examples/mbed/z_sub.cpp | 2 +- examples/unix/c11/z_get.c | 46 +-- examples/unix/c11/z_ping.c | 36 +- examples/unix/c11/z_pub.c | 16 +- examples/unix/c11/z_pub_st.c | 10 +- examples/unix/c11/z_queryable.c | 9 +- examples/unix/c11/z_sub.c | 23 +- examples/unix/c11/z_sub_st.c | 5 +- examples/unix/c11/z_sub_thr.c | 25 +- examples/unix/c99/z_get.c | 46 +-- examples/unix/c99/z_ping.c | 36 +- examples/unix/c99/z_pub.c | 10 +- examples/unix/c99/z_pub_st.c | 20 +- examples/unix/c99/z_queryable.c | 9 +- examples/unix/c99/z_sub.c | 9 +- examples/unix/c99/z_sub_st.c | 18 +- examples/windows/z_get.c | 45 +-- examples/windows/z_ping.c | 36 +- examples/windows/z_pub.c | 1 + examples/windows/z_pub_st.c | 11 +- examples/windows/z_queryable.c | 8 +- examples/windows/z_sub.c | 8 +- examples/windows/z_sub_st.c | 8 +- examples/zephyr/z_queryable.c | 2 +- examples/zephyr/z_sub.c | 2 +- extra_script.py | 5 + include/zenoh-pico/api/primitives.h | 2 +- include/zenoh-pico/api/types.h | 32 +- include/zenoh-pico/collections/array.h | 6 +- include/zenoh-pico/collections/element.h | 4 +- include/zenoh-pico/collections/intmap.h | 2 +- include/zenoh-pico/collections/list.h | 1 + include/zenoh-pico/collections/refcount.h | 37 +- include/zenoh-pico/config.h | 7 + include/zenoh-pico/deprecated/platform.h | 88 +++++ include/zenoh-pico/link/config/bt.h | 16 +- include/zenoh-pico/link/config/serial.h | 2 +- include/zenoh-pico/link/config/tcp.h | 2 +- include/zenoh-pico/link/config/udp.h | 16 +- include/zenoh-pico/net/primitives.h | 2 +- include/zenoh-pico/net/session.h | 6 +- include/zenoh-pico/protocol/core.h | 8 + .../protocol/definitions/declarations.h | 2 - .../zenoh-pico/protocol/definitions/network.h | 34 +- include/zenoh-pico/protocol/keyexpr.h | 2 - include/zenoh-pico/session/session.h | 8 +- include/zenoh-pico/session/subscription.h | 16 +- include/zenoh-pico/system/link/raweth.h | 29 +- include/zenoh-pico/system/platform-common.h | 108 ++++++ include/zenoh-pico/system/platform.h | 92 +---- .../system/platform/arduino/esp32.h | 12 +- .../system/platform/arduino/opencr.h | 12 +- .../zenoh-pico/system/platform/emscripten.h | 12 +- include/zenoh-pico/system/platform/espidf.h | 31 +- include/zenoh-pico/system/platform/flipper.h | 46 +++ .../system/platform/freertos_plus_tcp.h | 12 +- include/zenoh-pico/system/platform/mbed.h | 14 +- include/zenoh-pico/system/platform/unix.h | 12 +- include/zenoh-pico/system/platform/void.h | 12 +- include/zenoh-pico/system/platform/windows.h | 12 +- include/zenoh-pico/system/platform/zephyr.h | 12 +- .../zenoh-pico/transport/multicast/lease.h | 2 +- include/zenoh-pico/transport/multicast/read.h | 2 +- include/zenoh-pico/transport/raweth/config.h | 58 ---- include/zenoh-pico/transport/raweth/read.h | 2 +- include/zenoh-pico/transport/transport.h | 18 +- include/zenoh-pico/transport/unicast/lease.h | 2 +- include/zenoh-pico/transport/unicast/read.h | 2 +- include/zenoh-pico/utils/logging.h | 8 +- src/api/api.c | 48 +-- src/collections/bytes.c | 6 +- src/collections/intmap.c | 8 +- src/collections/list.c | 20 +- src/collections/string.c | 18 +- src/collections/vec.c | 12 +- src/deprecated/platform.c | 63 ++++ src/link/endpoint.c | 12 +- src/link/link.c | 2 +- src/link/multicast/udp.c | 14 +- src/link/unicast/tcp.c | 14 +- src/link/unicast/udp.c | 14 +- src/link/unicast/ws.c | 14 +- src/net/config.c | 2 +- src/net/filtering.c | 6 +- src/net/memory.c | 8 +- src/net/primitives.c | 19 +- src/net/publish.c | 2 +- src/net/query.c | 6 +- src/net/session.c | 20 +- src/net/subscribe.c | 2 +- src/protocol/codec.c | 4 +- src/protocol/codec/declarations.c | 10 +- src/protocol/codec/ext.c | 11 +- src/protocol/codec/message.c | 4 +- src/protocol/codec/network.c | 4 + src/protocol/codec/transport.c | 2 +- src/protocol/config.c | 4 +- src/protocol/definitions/network.c | 7 +- src/protocol/iobuf.c | 18 +- src/protocol/keyexpr.c | 2 +- src/session/interest.c | 7 +- src/session/push.c | 2 +- src/session/query.c | 8 +- src/session/queryable.c | 2 +- src/session/reply.c | 5 +- src/session/resource.c | 6 +- src/session/rx.c | 5 +- src/session/scout.c | 6 +- src/session/subscription.c | 21 +- src/session/utils.c | 10 +- src/system/arduino/esp32/network.cpp | 30 +- src/system/arduino/esp32/system.c | 102 +++--- src/system/arduino/opencr/system.c | 110 +++--- src/system/emscripten/network.c | 17 +- src/system/emscripten/system.c | 82 ++--- src/system/espidf/network.c | 117 ++++--- src/system/espidf/system.c | 167 +++++---- src/system/flipper/network.c | 225 +++++++++++++ src/system/flipper/system.c | 295 ++++++++++++++++ src/system/freertos_plus_tcp/system.c | 86 ++--- src/system/mbed/system.cpp | 80 ++--- src/system/unix/link/raweth.c | 26 +- src/system/unix/network.c | 22 +- src/system/unix/system.c | 102 +++--- src/system/windows/network.c | 24 +- src/system/windows/system.c | 100 +++--- src/system/zephyr/network.c | 38 +-- src/system/zephyr/system.c | 96 +++--- src/transport/multicast/lease.c | 16 +- src/transport/multicast/read.c | 10 +- src/transport/multicast/rx.c | 12 +- src/transport/multicast/transport.c | 34 +- src/transport/multicast/tx.c | 10 +- src/transport/raweth/config.c | 47 --- src/transport/raweth/link.c | 317 +++++++++++++++++- src/transport/raweth/read.c | 10 +- src/transport/raweth/rx.c | 7 +- src/transport/raweth/tx.c | 39 +-- src/transport/transport.c | 2 +- src/transport/unicast/lease.c | 8 +- src/transport/unicast/read.c | 10 +- src/transport/unicast/rx.c | 4 +- src/transport/unicast/transport.c | 33 +- src/transport/unicast/tx.c | 10 +- tests/modularity.py | 116 +++---- tests/raweth.py | 60 ++-- tests/single_thread.py | 44 +-- tests/z_api_alignment_test.c | 4 +- tests/z_client_test.c | 70 ++-- tests/z_data_struct_test.c | 4 +- tests/z_iobuf_test.c | 24 +- tests/z_msgcodec_test.c | 28 +- tests/z_peer_multicast_test.c | 22 +- tests/z_perf_rx.c | 8 +- tests/z_perf_tx.c | 6 +- tests/z_session_test.c | 2 +- tests/z_test_fragment_rx.c | 13 +- tests/z_test_fragment_tx.c | 36 +- 182 files changed, 2905 insertions(+), 1683 deletions(-) create mode 100644 include/zenoh-pico/deprecated/platform.h create mode 100644 include/zenoh-pico/system/platform-common.h create mode 100644 include/zenoh-pico/system/platform/flipper.h delete mode 100644 include/zenoh-pico/transport/raweth/config.h create mode 100644 src/deprecated/platform.c create mode 100644 src/system/flipper/network.c create mode 100644 src/system/flipper/system.c delete mode 100644 src/transport/raweth/config.c diff --git a/.github/workflows/arduino_esp32.yaml b/.github/workflows/arduino_esp32.yaml index ce4125da5..1bc0f27e4 100644 --- a/.github/workflows/arduino_esp32.yaml +++ b/.github/workflows/arduino_esp32.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $ARDUINO_BASE cd $ARDUINO_BASE - platformio init -b esp32thing_plus --project-option="build_flags=-DZ_FEATURE_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3 -DZENOH_COMPILER_GCC" + platformio init -b esp32thing_plus -O "build_flags=-DZ_FEATURE_LINK_BLUETOOTH=1 -DZENOH_DEBUG=3 -DZENOH_COMPILER_GCC" -O "lib_ldf_mode=deep+" cd $ARDUINO_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/.github/workflows/build-check.yaml b/.github/workflows/build-check.yaml index ba90599cc..7785bd8b4 100644 --- a/.github/workflows/build-check.yaml +++ b/.github/workflows/build-check.yaml @@ -54,7 +54,25 @@ jobs: sudo apt install -y ninja-build FORCE_C99=ON CMAKE_GENERATOR=Ninja make + zenoh_build: + name: Build Zenoh from source + runs-on: ubuntu-latest + outputs: + artifact-name: ${{ steps.main.outputs.artifact-name }} + steps: + - id: main + name: Build Zenoh + uses: eclipse-zenoh/ci/build-crates-standalone@main + with: + repo: eclipse-zenoh/zenoh + branch: interests + artifact-patterns: | + ^zenohd$ + ^libzenoh_plugin_rest.so$ + ^libzenoh_plugin_storage_manager.so$ + modular_build: + needs: zenoh_build name: Modular build on ubuntu-latest runs-on: ubuntu-latest strategy: @@ -67,8 +85,19 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Run docker image - run: docker run --name zenoh_router --init --net host -d eclipse/zenoh:latest + - name: Download Zenoh artifacts + uses: actions/download-artifact@v4 + with: + name: ${{ needs.zenoh_build.outputs.artifact-name }} + + - name: Unzip Zenoh artifacts + run: unzip ${{ needs.zenoh_build.outputs.artifact-name }} -d zenoh-standalone + + - id: run-zenoh + name: Run Zenoh router + run: | + RUST_LOG=debug ./zenoh-standalone/zenohd & + echo "zenohd-pid=$!" >> $GITHUB_OUTPUT - name: Build project run: | @@ -82,11 +111,9 @@ jobs: Z_FEATURE_QUERYABLE: ${{ matrix.feature_queryable }} Z_FEATURE_QUERY: ${{ matrix.feature_query }} - - name: Stop docker image + - name: Kill Zenoh router if: always() - run: | - docker stop zenoh_router - docker rm zenoh_router + run: kill ${{ steps.run-zenoh.outputs.zenohd-pid }} raweth_build: name: Build and test raweth transport on ubuntu-latest @@ -108,14 +135,26 @@ jobs: Z_FEATURE_RAWETH_TRANSPORT: ${{ matrix.feature_reth }} st_build: + needs: zenoh_build name: Build and test in single thread on ubuntu-latest runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - - name: Run docker image - run: docker run --name zenoh_router --init --net host -d eclipse/zenoh:latest + - name: Download Zenoh artifacts + uses: actions/download-artifact@v4 + with: + name: ${{ needs.zenoh_build.outputs.artifact-name }} + + - name: Unzip Zenoh artifacts + run: unzip ${{ needs.zenoh_build.outputs.artifact-name }} -d zenoh-standalone + + - id: run-zenoh + name: Run Zenoh router + run: | + RUST_LOG=debug ./zenoh-standalone/zenohd & + echo "zenohd-pid=$!" >> $GITHUB_OUTPUT - name: Build project and run test run: | @@ -126,21 +165,31 @@ jobs: env: Z_FEATURE_MULTI_THREAD: 0 - - name: Stop docker image + - name: Kill Zenoh router if: always() - run: | - docker stop zenoh_router - docker rm zenoh_router + run: kill ${{ steps.run-zenoh.outputs.zenohd-pid }} fragment_test: + needs: zenoh_build name: Test multicast and unicast fragmentation runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - - name: Run docker image - run: docker run --name zenoh_router --init --net host -d eclipse/zenoh:latest + - name: Download Zenoh artifacts + uses: actions/download-artifact@v4 + with: + name: ${{ needs.zenoh_build.outputs.artifact-name }} + + - name: Unzip Zenoh artifacts + run: unzip ${{ needs.zenoh_build.outputs.artifact-name }} -d zenoh-standalone + + - id: run-zenoh + name: Run Zenoh router + run: | + RUST_LOG=debug ./zenoh-standalone/zenohd & + echo "zenohd-pid=$!" >> $GITHUB_OUTPUT - name: Build project and run test run: | @@ -150,8 +199,6 @@ jobs: python3 ./build/tests/fragment.py timeout-minutes: 5 - - name: Stop docker image + - name: Kill Zenoh router if: always() - run: | - docker stop zenoh_router - docker rm zenoh_router + run: kill ${{ steps.run-zenoh.outputs.zenohd-pid }} diff --git a/CMakeLists.txt b/CMakeLists.txt index 3df82dd66..c597d5ccc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -218,6 +218,7 @@ target_include_directories(${Libname} PUBLIC ${PROJECT_SOURCE_DIR}/include) file(GLOB_RECURSE Sources "src/api/*.c" "src/collections/*.c" + "src/deprecated/*.c" "src/link/*.c" "src/net/*.c" "src/protocol/*.c" diff --git a/GNUmakefile b/GNUmakefile index 3160a168b..1b34a6bc9 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -57,7 +57,7 @@ Z_FEATURE_SUBSCRIPTION?=1 Z_FEATURE_QUERY?=1 Z_FEATURE_QUERYABLE?=1 Z_FEATURE_ATTACHMENT?=1 -Z_FEATURE_INTEREST?=0 +Z_FEATURE_INTEREST?=1 Z_FEATURE_RAWETH_TRANSPORT?=0 # zenoh-pico/ directory diff --git a/README.md b/README.md index 033521869..6eb1a2ee8 100644 --- a/README.md +++ b/README.md @@ -336,15 +336,15 @@ The simplest way to run some of the example is to get a Docker image of the **ze ### 3.1. Starting the Zenoh Router Assuming you've pulled the Docker image of the **zenoh** router on a Linux host (to leverage UDP multicast scouting as explained [here](https://zenoh.io/docs/getting-started/quick-test/#run-zenoh-router-in-a-docker-container), then simply do: ```bash -$ docker run --init --net host eclipse/zenoh:master +$ docker run --init --net host eclipse/zenoh:main ``` To see the zenoh manual page, simply do: ```bash -$ docker run --init --net host eclipse/zenoh:master --help +$ docker run --init --net host eclipse/zenoh:main --help ``` -:warning: **Please notice that the `--net host` option in Docker is restricted to Linux only.** +:warning: **Please notice that the `--net host` option in Docker is restricted to Linux only.** The cause is that Docker doesn't support UDP multicast between a container and its host (see cases [moby/moby#23659](https://github.com/moby/moby/issues/23659), [moby/libnetwork#2397](https://github.com/moby/libnetwork/issues/2397) or [moby/libnetwork#552](https://github.com/moby/libnetwork/issues/552)). The only known way to make it work is to use the `--net host` option that is [only supported on Linux hosts](https://docs.docker.com/network/host/). ### 3.2. Basic Pub/Sub Example @@ -385,7 +385,7 @@ where `lo0` is the network interface you want to use for multicast communication ### 3.4. Basic Pub/Sub Example - Mixing Client and P2P communication To allow Zenoh-Pico unicast clients to talk to Zenoh-Pico multicast peers, as well as with any other Zenoh client/peer, you need to start a Zenoh Router that listens on both multicast and unicast: ```bash -$ docker run --init --net host eclipse/zenoh:master -l udp/224.0.0.123:7447#iface=lo0 -l tcp/127.0.0.1:7447 +$ docker run --init --net host eclipse/zenoh:main -l udp/224.0.0.123:7447#iface=lo0 -l tcp/127.0.0.1:7447 ``` Assuming that (1) you are running the **zenoh** router as indicated above, and (2) you are under the build directory, do: diff --git a/docs/api.rst b/docs/api.rst index 1b925de03..08aaa7a95 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -31,7 +31,6 @@ Enums .. autocenum:: constants.h::z_reply_tag_t .. autocenum:: constants.h::z_congestion_control_t .. autocenum:: constants.h::z_priority_t -.. autocenum:: constants.h::z_submode_t .. autocenum:: constants.h::z_query_target_t Data Structures @@ -45,13 +44,11 @@ Data Structures .. autoctype:: types.h::z_config_t .. autoctype:: types.h::z_session_t .. autoctype:: types.h::z_subscriber_t -.. autoctype:: types.h::z_pull_subscriber_t .. autoctype:: types.h::z_publisher_t .. autoctype:: types.h::z_queryable_t .. autoctype:: types.h::z_encoding_t .. autoctype:: types.h::z_value_t .. autoctype:: types.h::z_subscriber_options_t -.. autoctype:: types.h::z_pull_subscriber_options_t .. autoctype:: types.h::z_query_consolidation_t .. autoctype:: types.h::z_publisher_options_t .. autoctype:: types.h::z_queryable_options_t @@ -118,10 +115,6 @@ To check if ``val`` is still valid, you may use ``z_X_check(&val)`` or ``z_check A zenoh-allocated :c:type:`z_subscriber_t`. -.. c:type:: z_owned_pull_subscriber_t - - A zenoh-allocated :c:type:`z_pull_subscriber_t`. - .. c:type:: z_owned_publisher_t A zenoh-allocated :c:type:`z_publisher_t`. @@ -239,10 +232,6 @@ Primitives .. autocfunction:: primitives.h::z_subscriber_options_default .. autocfunction:: primitives.h::z_declare_subscriber .. autocfunction:: primitives.h::z_undeclare_subscriber -.. autocfunction:: primitives.h::z_pull_subscriber_options_default -.. autocfunction:: primitives.h::z_declare_pull_subscriber -.. autocfunction:: primitives.h::z_undeclare_pull_subscriber -.. autocfunction:: primitives.h::z_subscriber_pull .. autocfunction:: primitives.h::z_queryable_options_default .. autocfunction:: primitives.h::z_declare_queryable .. autocfunction:: primitives.h::z_undeclare_queryable diff --git a/docs/requirements.txt b/docs/requirements.txt index 670562c3e..a5a4b5f60 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,4 +1,4 @@ -sphinx==4.5.0 -sphinx_c_autodoc -sphinx_rtd_theme -clang==14 +sphinx==7.2.6 +sphinx_c_autodoc==1.3.0 +sphinx_rtd_theme==2.0.0 +clang==14.0 diff --git a/examples/arduino/z_pub.ino b/examples/arduino/z_pub.ino index c7e60d62e..ce0a14621 100644 --- a/examples/arduino/z_pub.ino +++ b/examples/arduino/z_pub.ino @@ -94,6 +94,7 @@ void setup() { } void loop() { + delay(1000); char buf[256]; sprintf(buf, "[%4d] %s", idx++, VALUE); Serial.print("Writing Data ('"); @@ -105,8 +106,6 @@ void loop() { if (z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), NULL) < 0) { Serial.println("Error while publishing data"); } - - delay(1000); } #else void setup() { diff --git a/examples/arduino/z_queryable.ino b/examples/arduino/z_queryable.ino index 772f60a0a..b6e3c7ed5 100644 --- a/examples/arduino/z_queryable.ino +++ b/examples/arduino/z_queryable.ino @@ -106,7 +106,7 @@ void setup() { delay(300); } -void loop() { delay(5000); } +void loop() { delay(1000); } #else void setup() { diff --git a/examples/arduino/z_sub.ino b/examples/arduino/z_sub.ino index 1a3302f55..9abc75df0 100644 --- a/examples/arduino/z_sub.ino +++ b/examples/arduino/z_sub.ino @@ -104,7 +104,7 @@ void setup() { delay(300); } -void loop() { delay(5000); } +void loop() { delay(1000); } #else void setup() { diff --git a/examples/espidf/z_queryable.c b/examples/espidf/z_queryable.c index 4448b3b2b..15b4e9afd 100644 --- a/examples/espidf/z_queryable.c +++ b/examples/espidf/z_queryable.c @@ -159,7 +159,7 @@ void app_main() { printf("Zenoh setup finished!\n"); while (1) { - sleep(5); + sleep(1); } printf("Closing Zenoh Session..."); diff --git a/examples/espidf/z_sub.c b/examples/espidf/z_sub.c index 33f01548b..467160440 100644 --- a/examples/espidf/z_sub.c +++ b/examples/espidf/z_sub.c @@ -154,7 +154,7 @@ void app_main() { printf("OK!\n"); while (1) { - sleep(5); + sleep(1); } printf("Closing Zenoh Session..."); diff --git a/examples/freertos_plus_tcp/z_get.c b/examples/freertos_plus_tcp/z_get.c index 6abd0175e..09c1d3507 100644 --- a/examples/freertos_plus_tcp/z_get.c +++ b/examples/freertos_plus_tcp/z_get.c @@ -76,7 +76,7 @@ void app_main(void) { } while (1) { - zp_sleep_s(5); + z_sleep_s(5); printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts = z_get_options_default(); if (strcmp(VALUE, "") != 0) { diff --git a/examples/freertos_plus_tcp/z_pub.c b/examples/freertos_plus_tcp/z_pub.c index c2d8ca55b..abb4594f4 100644 --- a/examples/freertos_plus_tcp/z_pub.c +++ b/examples/freertos_plus_tcp/z_pub.c @@ -48,7 +48,7 @@ void app_main(void) { static StackType_t read_task_stack[1000]; static StaticTask_t read_task_buffer; - zp_task_attr_t read_task_attr = { + z_task_attr_t read_task_attr = { .name = "ZenohReadTask", .priority = 10, .stack_depth = 1000, @@ -62,7 +62,7 @@ void app_main(void) { static StackType_t lease_task_stack[1000]; static StaticTask_t lease_task_buffer; - zp_task_attr_t lease_task_attr = { + z_task_attr_t lease_task_attr = { .name = "ZenohLeaseTask", .priority = 10, .stack_depth = 1000, @@ -89,7 +89,7 @@ void app_main(void) { char *buf = (char *)pvPortMalloc(256); for (int idx = 0; 1; ++idx) { - zp_sleep_s(1); + z_sleep_s(1); snprintf(buf, 256, "[%4d] %s", idx, VALUE); printf("Putting Data ('%s': '%s')...\n", KEYEXPR, buf); diff --git a/examples/freertos_plus_tcp/z_pub_st.c b/examples/freertos_plus_tcp/z_pub_st.c index 49b7761eb..5803030c4 100644 --- a/examples/freertos_plus_tcp/z_pub_st.c +++ b/examples/freertos_plus_tcp/z_pub_st.c @@ -30,6 +30,7 @@ #define KEYEXPR "demo/example/zenoh-pico-pub" #define VALUE "[FreeRTOS-Plus-TCP] Pub from Zenoh-Pico!" +#define N 2147483647 // max int value by default void app_main(void) { z_owned_config_t config = z_config_default(); @@ -53,15 +54,15 @@ void app_main(void) { } char *buf = (char *)pvPortMalloc(256); - zp_clock_t now = zp_clock_now(); - for (int idx = 0; 1;) { - if (zp_clock_elapsed_ms(&now) > 1000) { + z_clock_t now = z_clock_now(); + for (int idx = 0; idx < N;) { + 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); ++idx; - now = zp_clock_now(); + now = z_clock_now(); } zp_read(z_loan(s), NULL); diff --git a/examples/freertos_plus_tcp/z_pull.c b/examples/freertos_plus_tcp/z_pull.c index b4da92a31..2b043e052 100644 --- a/examples/freertos_plus_tcp/z_pull.c +++ b/examples/freertos_plus_tcp/z_pull.c @@ -69,7 +69,7 @@ void app_main(void) { // } // while (1) { - // zp_sleep_s(5); + // z_sleep_s(5); // printf("Pulling data from '%s'...\n", KEYEXPR); // z_subscriber_pull(z_loan(sub)); // } diff --git a/examples/freertos_plus_tcp/z_put.c b/examples/freertos_plus_tcp/z_put.c index 898d0e9b8..1253af540 100644 --- a/examples/freertos_plus_tcp/z_put.c +++ b/examples/freertos_plus_tcp/z_put.c @@ -68,7 +68,7 @@ void app_main(void) { } while (1) { - zp_sleep_s(1); + z_sleep_s(1); } // Clean up diff --git a/examples/freertos_plus_tcp/z_queryable.c b/examples/freertos_plus_tcp/z_queryable.c index 0d9ce87e4..6e4a36b30 100644 --- a/examples/freertos_plus_tcp/z_queryable.c +++ b/examples/freertos_plus_tcp/z_queryable.c @@ -80,7 +80,7 @@ void app_main(void) { } while (1) { - zp_sleep_s(5); + z_sleep_s(1); } z_undeclare_queryable(z_move(qable)); diff --git a/examples/freertos_plus_tcp/z_sub.c b/examples/freertos_plus_tcp/z_sub.c index 91395e59f..9331619a6 100644 --- a/examples/freertos_plus_tcp/z_sub.c +++ b/examples/freertos_plus_tcp/z_sub.c @@ -66,7 +66,7 @@ void app_main(void) { } while (1) { - zp_sleep_s(5); + z_sleep_s(1); } z_undeclare_subscriber(z_move(sub)); diff --git a/examples/freertos_plus_tcp/z_sub_st.c b/examples/freertos_plus_tcp/z_sub_st.c index 811a49877..20ff5d636 100644 --- a/examples/freertos_plus_tcp/z_sub_st.c +++ b/examples/freertos_plus_tcp/z_sub_st.c @@ -27,6 +27,9 @@ #endif #define KEYEXPR "demo/example/**" +#define N 2147483647 // max int value by default + +int msg_nb = 0; void data_handler(const z_sample_t *sample, void *ctx) { (void)(ctx); @@ -34,6 +37,7 @@ void data_handler(const z_sample_t *sample, void *ctx) { printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, sample->payload.start); z_drop(z_move(keystr)); + msg_nb++; } void app_main(void) { @@ -58,7 +62,8 @@ void app_main(void) { return; } - while (1) { + printf("Running until %d messages are received...\n", N); + while (msg_nb < N) { zp_read(z_loan(s), NULL); zp_send_keep_alive(z_loan(s), NULL); zp_send_join(z_loan(s), NULL); diff --git a/examples/mbed/z_get.cpp b/examples/mbed/z_get.cpp index 21e77c33d..905ac4e1c 100644 --- a/examples/mbed/z_get.cpp +++ b/examples/mbed/z_get.cpp @@ -72,7 +72,7 @@ int main(int argc, char **argv) { zp_start_lease_task(z_session_loan(&s), NULL); while (1) { - zp_sleep_s(5); + z_sleep_s(5); printf("Sending Query '%s'...\n", KEYEXPR); z_get_options_t opts = z_get_options_default(); if (strcmp(VALUE, "") != 0) { diff --git a/examples/mbed/z_pub.cpp b/examples/mbed/z_pub.cpp index 7ef9f1dc4..0fb939071 100644 --- a/examples/mbed/z_pub.cpp +++ b/examples/mbed/z_pub.cpp @@ -68,7 +68,7 @@ int main(int argc, char **argv) { char buf[256]; for (int idx = 0; 1; ++idx) { - zp_sleep_s(1); + 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); diff --git a/examples/mbed/z_pull.cpp b/examples/mbed/z_pull.cpp index aae56316e..24acaeaec 100644 --- a/examples/mbed/z_pull.cpp +++ b/examples/mbed/z_pull.cpp @@ -78,7 +78,7 @@ int main(int argc, char **argv) { // printf("OK!\n"); // while (1) { - // zp_sleep_s(5); + // z_sleep_s(5); // printf("Pulling data from '%s'...\n", KEYEXPR); // z_subscriber_pull(z_pull_subscriber_loan(&sub)); // } diff --git a/examples/mbed/z_queryable.cpp b/examples/mbed/z_queryable.cpp index b2f597d58..b0b9358bf 100644 --- a/examples/mbed/z_queryable.cpp +++ b/examples/mbed/z_queryable.cpp @@ -80,7 +80,7 @@ int main(int argc, char **argv) { printf("Zenoh setup finished!\n"); while (1) { - zp_sleep_s(5); + z_sleep_s(1); } printf("Closing Zenoh Session..."); diff --git a/examples/mbed/z_sub.cpp b/examples/mbed/z_sub.cpp index 62ac126d3..b60f82d44 100644 --- a/examples/mbed/z_sub.cpp +++ b/examples/mbed/z_sub.cpp @@ -75,7 +75,7 @@ int main(int argc, char **argv) { printf("OK!\n"); while (1) { - zp_sleep_s(5); + z_sleep_s(1); } printf("Closing Zenoh Session..."); diff --git a/examples/unix/c11/z_get.c b/examples/unix/c11/z_get.c index eb7543f21..f98426a15 100644 --- a/examples/unix/c11/z_get.c +++ b/examples/unix/c11/z_get.c @@ -18,10 +18,15 @@ #include #include -#if Z_FEATURE_QUERY == 1 +#if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 +static z_condvar_t cond; +static z_mutex_t mutex; + void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); + z_condvar_signal(&cond); + z_condvar_free(&cond); } void reply_handler(z_owned_reply_t *reply, void *ctx) { @@ -73,6 +78,9 @@ int main(int argc, char **argv) { } } + z_mutex_init(&mutex); + z_condvar_init(&cond); + z_owned_config_t config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (clocator != NULL) { @@ -102,27 +110,19 @@ int main(int argc, char **argv) { return -1; } - printf("Enter any key to get data or 'q' to quit...\n"); - char c = '\0'; - while (1) { - fflush(stdin); - int ret = scanf("%c", &c); - (void)ret; // Remove unused result warning - if (c == 'q') { - break; - } - - printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); - if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); - } - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { - printf("Unable to send query.\n"); - return -1; - } + z_mutex_lock(&mutex); + printf("Sending Query '%s'...\n", keyexpr); + z_get_options_t opts = z_get_options_default(); + if (value != NULL) { + opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + } + z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); + if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + printf("Unable to send query.\n"); + return -1; } + z_condvar_wait(&cond, &mutex); + z_mutex_unlock(&mutex); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan(s)); @@ -134,7 +134,9 @@ int main(int argc, char **argv) { } #else int main(void) { - printf("ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY but this example requires it.\n"); + printf( + "ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY or Z_FEATURE_MULTI_THREAD but this example requires " + "them.\n"); return -2; } #endif diff --git a/examples/unix/c11/z_ping.c b/examples/unix/c11/z_ping.c index 564cbc365..f4da4dd18 100644 --- a/examples/unix/c11/z_ping.c +++ b/examples/unix/c11/z_ping.c @@ -27,17 +27,17 @@ #define DEFAULT_PING_NB 100 #define DEFAULT_WARMUP_MS 1000 -static zp_condvar_t cond; -static zp_mutex_t mutex; +static z_condvar_t cond; +static z_mutex_t mutex; void callback(const z_sample_t* sample, void* context) { (void)sample; (void)context; - zp_condvar_signal(&cond); + z_condvar_signal(&cond); } void drop(void* context) { (void)context; - zp_condvar_free(&cond); + z_condvar_free(&cond); } struct args_t { @@ -61,8 +61,8 @@ int main(int argc, char** argv) { DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS); return 1; } - zp_mutex_init(&mutex); - zp_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); if (!z_check(session)) { @@ -92,34 +92,34 @@ int main(int argc, char** argv) { return -1; } - uint8_t* data = zp_malloc(args.size); + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = (uint8_t)(i % 10); } - zp_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); - zp_clock_t warmup_start = zp_clock_now(); + 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); - zp_condvar_wait(&cond, &mutex); - elapsed_us = zp_clock_elapsed_us(&warmup_start); + z_condvar_wait(&cond, &mutex); + elapsed_us = z_clock_elapsed_us(&warmup_start); } } - unsigned long* results = zp_malloc(sizeof(unsigned long) * args.number_of_pings); + unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { - zp_clock_t measure_start = zp_clock_now(); + z_clock_t measure_start = z_clock_now(); z_publisher_put(z_loan(pub), data, args.size, NULL); - zp_condvar_wait(&cond, &mutex); - results[i] = zp_clock_elapsed_us(&measure_start); + z_condvar_wait(&cond, &mutex); + results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2); } - zp_mutex_unlock(&mutex); - zp_free(results); - zp_free(data); + z_mutex_unlock(&mutex); + z_free(results); + z_free(data); z_drop(z_move(pub)); z_drop(z_move(sub)); diff --git a/examples/unix/c11/z_pub.c b/examples/unix/c11/z_pub.c index 064b53677..92a8b0c5b 100644 --- a/examples/unix/c11/z_pub.c +++ b/examples/unix/c11/z_pub.c @@ -30,7 +30,7 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; - int n = 10; + int n = 2147483647; // max int value by default int opt; while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) { @@ -95,15 +95,21 @@ int main(int argc, char **argv) { printf("Unable to declare publisher for key expression!\n"); return -1; } - + // Wait for joins in peer mode + if (strcmp(mode, "peer") == 0) { + printf("Waiting for joins...\n"); + sleep(3); + } + printf("Press CTRL-C to quit...\n"); + char buf[256]; for (int idx = 0; idx < n; ++idx) { sleep(1); - (void)idx; - printf("Putting Data ('%s': '%s')...\n", keyexpr, value); + sprintf(buf, "[%4d] %s", idx, value); + printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); z_publisher_put_options_t options = z_publisher_put_options_default(); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_publisher_put(z_loan(pub), (const uint8_t *)value, strlen(value), &options); + z_publisher_put(z_loan(pub), (const uint8_t *)buf, strlen(buf), &options); } 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 225272ed7..deb9e1f60 100644 --- a/examples/unix/c11/z_pub_st.c +++ b/examples/unix/c11/z_pub_st.c @@ -27,7 +27,7 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; - int n = 10; + int n = 2147483647; // max int value by default int opt; while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) { @@ -86,11 +86,13 @@ int main(int argc, char **argv) { return -1; } // Main loop + printf("Press CTRL-C to quit...\n"); + char buf[256]; for (int idx = 0; idx < n; idx++) { sleep(1); - (void)idx; - printf("Putting Data ('%s': '%s')...\n", keyexpr, value); - z_publisher_put(z_loan(pub), (const uint8_t *)value, strlen(value), NULL); + 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); zp_read(z_loan(s), NULL); zp_send_keep_alive(z_loan(s), NULL); diff --git a/examples/unix/c11/z_queryable.c b/examples/unix/c11/z_queryable.c index e9d1b95e5..47001ec1f 100644 --- a/examples/unix/c11/z_queryable.c +++ b/examples/unix/c11/z_queryable.c @@ -109,12 +109,9 @@ int main(int argc, char **argv) { return -1; } - printf("Enter 'q' to quit...\n"); - char c = '\0'; - while (c != 'q') { - fflush(stdin); - int ret = scanf("%c", &c); - (void)ret; // Remove unused result warning + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_queryable(z_move(qable)); diff --git a/examples/unix/c11/z_sub.c b/examples/unix/c11/z_sub.c index e35fc1427..185e3df63 100644 --- a/examples/unix/c11/z_sub.c +++ b/examples/unix/c11/z_sub.c @@ -22,6 +22,8 @@ #if Z_FEATURE_SUBSCRIPTION == 1 +static int msg_nb = 0; + #if Z_FEATURE_ATTACHMENT == 1 int8_t attachment_handler(z_bytes_t key, z_bytes_t value, void *ctx) { (void)ctx; @@ -42,6 +44,7 @@ void data_handler(const z_sample_t *sample, void *ctx) { } #endif z_drop(z_move(keystr)); + msg_nb++; } int main(int argc, char **argv) { @@ -49,9 +52,10 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; + int n = 0; int opt; - while ((opt = getopt(argc, argv, "k:e:m:l:")) != -1) { + while ((opt = getopt(argc, argv, "k:e:m:l:n:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -65,8 +69,11 @@ int main(int argc, char **argv) { case 'l': llocator = optarg; break; + case 'n': + n = atoi(optarg); + break; case '?': - if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'l') { + if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'l' || optopt == 'n') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -108,12 +115,12 @@ int main(int argc, char **argv) { return -1; } - printf("Enter 'q' to quit...\n"); - char c = '\0'; - while (c != 'q') { - fflush(stdin); - int ret = scanf("%c", &c); - (void)ret; // Remove unused result warning + printf("Press CTRL-C to quit...\n"); + while (1) { + if ((n != 0) && (msg_nb >= n)) { + break; + } + sleep(1); } z_undeclare_subscriber(z_move(sub)); diff --git a/examples/unix/c11/z_sub_st.c b/examples/unix/c11/z_sub_st.c index 3bc53e4f5..8e825a06e 100644 --- a/examples/unix/c11/z_sub_st.c +++ b/examples/unix/c11/z_sub_st.c @@ -37,7 +37,7 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; - int n = -1; + int n = 2147483647; // max int value by default int opt; while ((opt = getopt(argc, argv, "k:e:m:l:n:")) != -1) { @@ -93,7 +93,8 @@ int main(int argc, char **argv) { return -1; } - while (msg_nb != n) { + printf("Press CTRL-C to quit...\n"); + while (msg_nb < n) { zp_read(z_loan(s), NULL); zp_send_keep_alive(z_loan(s), NULL); zp_send_join(z_loan(s), NULL); diff --git a/examples/unix/c11/z_sub_thr.c b/examples/unix/c11/z_sub_thr.c index 39e3e894a..788b25b03 100644 --- a/examples/unix/c11/z_sub_thr.c +++ b/examples/unix/c11/z_sub_thr.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "zenoh-pico.h" @@ -24,8 +25,8 @@ typedef struct { volatile unsigned long count; volatile unsigned long finished_rounds; - zp_clock_t start; - zp_clock_t first_start; + z_clock_t start; + z_clock_t first_start; } z_stats_t; #if Z_FEATURE_SUBSCRIPTION == 1 @@ -44,23 +45,22 @@ void on_sample(const z_sample_t *sample, void *context) { stats->count++; // Start set measurement if (stats->count == 1) { - stats->start = zp_clock_now(); + stats->start = z_clock_now(); if (stats->first_start.tv_nsec == 0) { stats->first_start = stats->start; } } else if (stats->count >= PACKET_NB) { // Stop set measurement stats->finished_rounds++; - unsigned long elapsed_ms = zp_clock_elapsed_ms(&stats->start); - printf("Received %d msg in %lu ms (%.1f msg/s)\n", PACKET_NB, elapsed_ms, - (double)(PACKET_NB * 1000) / (double)elapsed_ms); + unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->start); + printf("%f msg/s\n", (double)(PACKET_NB * 1000) / (double)elapsed_ms); stats->count = 0; } } void drop_stats(void *context) { z_stats_t *stats = (z_stats_t *)context; - unsigned long elapsed_ms = zp_clock_elapsed_ms(&stats->first_start); + unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->first_start); const unsigned long sent_messages = PACKET_NB * stats->finished_rounds + stats->count; printf("Stats after unsubscribing: received %ld messages over %lu miliseconds (%.1f msg/s)\n", sent_messages, elapsed_ms, (double)(sent_messages * 1000) / (double)elapsed_ms); @@ -99,14 +99,11 @@ int main(int argc, char **argv) { exit(-1); } // Listen until stopped - printf("Start listening.\n"); - char c = 0; - while (c != 'q') { - c = (char)fgetc(stdin); + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } - // Wait for everything to settle - printf("End of test\n"); - zp_sleep_s(1); + // Clean up z_undeclare_subscriber(z_move(sub)); zp_stop_read_task(z_loan(s)); diff --git a/examples/unix/c99/z_get.c b/examples/unix/c99/z_get.c index bfb1dacc1..7bec8e497 100644 --- a/examples/unix/c99/z_get.c +++ b/examples/unix/c99/z_get.c @@ -18,10 +18,15 @@ #include #include -#if Z_FEATURE_QUERY == 1 +#if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 +z_condvar_t cond; +z_mutex_t mutex; + void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); + z_condvar_signal(&cond); + z_condvar_free(&cond); } void reply_handler(z_owned_reply_t *reply, void *ctx) { @@ -73,6 +78,9 @@ int main(int argc, char **argv) { } } + z_mutex_init(&mutex); + z_condvar_init(&cond); + z_owned_config_t config = z_config_default(); zp_config_insert(z_config_loan(&config), Z_CONFIG_MODE_KEY, z_string_make(mode)); if (clocator != NULL) { @@ -102,27 +110,19 @@ int main(int argc, char **argv) { return -1; } - printf("Enter any key to get data or 'q' to quit...\n"); - char c = '\0'; - while (1) { - fflush(stdin); - int ret = scanf("%c", &c); - (void)ret; // Clear unused result warning - if (c == 'q') { - break; - } - - printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); - if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); - } - z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); - if (z_get(z_session_loan(&s), ke, "", z_closure_reply_move(&callback), &opts) < 0) { - printf("Unable to send query.\n"); - return -1; - } + z_mutex_lock(&mutex); + printf("Sending Query '%s'...\n", keyexpr); + z_get_options_t opts = z_get_options_default(); + if (value != NULL) { + opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + } + z_owned_closure_reply_t callback = z_closure_reply(reply_handler, reply_dropper, NULL); + if (z_get(z_session_loan(&s), ke, "", z_closure_reply_move(&callback), &opts) < 0) { + printf("Unable to send query.\n"); + return -1; } + z_condvar_wait(&cond, &mutex); + z_mutex_unlock(&mutex); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_session_loan(&s)); @@ -134,7 +134,9 @@ int main(int argc, char **argv) { } #else int main(void) { - printf("ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY but this example requires it.\n"); + printf( + "ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY or Z_FEATURE_MULTI_THREAD but this example requires " + "them.\n"); return -2; } #endif diff --git a/examples/unix/c99/z_ping.c b/examples/unix/c99/z_ping.c index 3bcafece7..673943898 100644 --- a/examples/unix/c99/z_ping.c +++ b/examples/unix/c99/z_ping.c @@ -28,17 +28,17 @@ #define DEFAULT_PING_NB 100 #define DEFAULT_WARMUP_MS 1000 -static zp_condvar_t cond; -static zp_mutex_t mutex; +static z_condvar_t cond; +static z_mutex_t mutex; void callback(const z_sample_t* sample, void* context) { (void)sample; (void)context; - zp_condvar_signal(&cond); + z_condvar_signal(&cond); } void drop(void* context) { (void)context; - zp_condvar_free(&cond); + z_condvar_free(&cond); } struct args_t { @@ -62,8 +62,8 @@ int main(int argc, char** argv) { DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS); return 1; } - zp_mutex_init(&mutex); - zp_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_config_move(&config)); if (!z_session_check(&session)) { @@ -94,34 +94,34 @@ int main(int argc, char** argv) { return -1; } - uint8_t* data = zp_malloc(args.size); + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = (uint8_t)(i % 10); } - zp_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); - zp_clock_t warmup_start = zp_clock_now(); + 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); - zp_condvar_wait(&cond, &mutex); - elapsed_us = zp_clock_elapsed_us(&warmup_start); + z_condvar_wait(&cond, &mutex); + elapsed_us = z_clock_elapsed_us(&warmup_start); } } - unsigned long* results = zp_malloc(sizeof(unsigned long) * args.number_of_pings); + unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { - zp_clock_t measure_start = zp_clock_now(); + z_clock_t measure_start = z_clock_now(); z_publisher_put(z_publisher_loan(&pub), data, args.size, NULL); - zp_condvar_wait(&cond, &mutex); - results[i] = zp_clock_elapsed_us(&measure_start); + z_condvar_wait(&cond, &mutex); + results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2); } - zp_mutex_unlock(&mutex); - zp_free(results); - zp_free(data); + z_mutex_unlock(&mutex); + z_free(results); + z_free(data); z_undeclare_subscriber(z_subscriber_move(&sub)); z_undeclare_publisher(z_publisher_move(&pub)); diff --git a/examples/unix/c99/z_pub.c b/examples/unix/c99/z_pub.c index 36483fae3..a81065c9e 100644 --- a/examples/unix/c99/z_pub.c +++ b/examples/unix/c99/z_pub.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; - int n = 10; + int n = 2147483647; // max int value by default int opt; while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) { @@ -92,14 +92,16 @@ int main(int argc, char **argv) { return -1; } + printf("Press CTRL-C to quit...\n"); + char *buf = (char *)malloc(256); for (int idx = 0; idx < n; ++idx) { sleep(1); - (void)idx; - printf("Putting Data ('%s': '%s')...\n", keyexpr, value); + snprintf(buf, 256, "[%4d] %s", idx, value); + printf("Putting Data ('%s': '%s')...\n", keyexpr, buf); z_publisher_put_options_t options = z_publisher_put_options_default(); options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); - z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)value, strlen(value), &options); + z_publisher_put(z_publisher_loan(&pub), (const uint8_t *)buf, strlen(buf), &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 1bbaef106..f39b3753b 100644 --- a/examples/unix/c99/z_pub_st.c +++ b/examples/unix/c99/z_pub_st.c @@ -26,9 +26,10 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; + int n = 2147483647; // max int value by default int opt; - while ((opt = getopt(argc, argv, "k:v:e:m:l:")) != -1) { + while ((opt = getopt(argc, argv, "k:v:e:m:l:n:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -45,8 +46,12 @@ int main(int argc, char **argv) { case 'l': llocator = optarg; break; + case 'n': + n = atoi(optarg); + break; case '?': - if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm' || optopt == 'l') { + if (optopt == 'k' || optopt == 'v' || optopt == 'e' || optopt == 'm' || optopt == 'l' || + optopt == 'n') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -80,16 +85,17 @@ int main(int argc, char **argv) { return -1; } + printf("Press CTRL-C to quit...\n"); char *buf = (char *)malloc(256); - zp_clock_t now = zp_clock_now(); - for (int idx = 0; 1;) { - if (zp_clock_elapsed_ms(&now) > 1000) { + z_clock_t now = z_clock_now(); + for (int idx = 0; idx < n;) { + 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); ++idx; - now = zp_clock_now(); + now = z_clock_now(); } zp_read(z_session_loan(&s), NULL); @@ -98,9 +104,7 @@ int main(int argc, char **argv) { } z_undeclare_publisher(z_publisher_move(&pub)); - z_close(z_session_move(&s)); - free(buf); return 0; } diff --git a/examples/unix/c99/z_queryable.c b/examples/unix/c99/z_queryable.c index 8460aef16..c6ccf555c 100644 --- a/examples/unix/c99/z_queryable.c +++ b/examples/unix/c99/z_queryable.c @@ -105,12 +105,9 @@ int main(int argc, char **argv) { return -1; } - printf("Enter 'q' to quit...\n"); - char c = '\0'; - while (c != 'q') { - fflush(stdin); - int ret = scanf("%c", &c); - (void)ret; // Clear unused result warning + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_queryable(z_queryable_move(&qable)); diff --git a/examples/unix/c99/z_sub.c b/examples/unix/c99/z_sub.c index 8d25dbb74..185c742ed 100644 --- a/examples/unix/c99/z_sub.c +++ b/examples/unix/c99/z_sub.c @@ -93,12 +93,9 @@ int main(int argc, char **argv) { return -1; } - printf("Enter 'q' to quit...\n"); - char c = '\0'; - while (c != 'q') { - fflush(stdin); - int ret = scanf("%c", &c); - (void)ret; // Clear unused result warning + printf("Press CTRL-C to quit...\n"); + while (1) { + sleep(1); } z_undeclare_subscriber(z_subscriber_move(&sub)); diff --git a/examples/unix/c99/z_sub_st.c b/examples/unix/c99/z_sub_st.c index 326f5fcbc..765bf3d5a 100644 --- a/examples/unix/c99/z_sub_st.c +++ b/examples/unix/c99/z_sub_st.c @@ -20,12 +20,16 @@ #include #if Z_FEATURE_SUBSCRIPTION == 1 + +static int msg_nb = 0; + void data_handler(const z_sample_t *sample, void *arg) { (void)(arg); z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_str_loan(&keystr), (int)sample->payload.len, sample->payload.start); z_str_drop(z_str_move(&keystr)); + msg_nb++; } int main(int argc, char **argv) { @@ -33,9 +37,10 @@ int main(int argc, char **argv) { const char *mode = "client"; char *clocator = NULL; char *llocator = NULL; + int n = 2147483647; // max int value by default int opt; - while ((opt = getopt(argc, argv, "k:e:m:l:")) != -1) { + while ((opt = getopt(argc, argv, "k:e:m:l:n:")) != -1) { switch (opt) { case 'k': keyexpr = optarg; @@ -49,8 +54,11 @@ int main(int argc, char **argv) { case 'l': llocator = optarg; break; + case 'n': + n = atoi(optarg); + break; case '?': - if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'l') { + if (optopt == 'k' || optopt == 'e' || optopt == 'm' || optopt == 'l' || optopt == 'n') { fprintf(stderr, "Option -%c requires an argument.\n", optopt); } else { fprintf(stderr, "Unknown option `-%c'.\n", optopt); @@ -86,16 +94,14 @@ int main(int argc, char **argv) { return -1; } - while (1) { + printf("Press CTRL-C to quit...\n"); + while (msg_nb < n) { zp_read(z_session_loan(&s), NULL); zp_send_keep_alive(z_session_loan(&s), NULL); zp_send_join(z_session_loan(&s), NULL); } - z_undeclare_subscriber(z_subscriber_move(&sub)); - z_close(z_session_move(&s)); - return 0; } #else diff --git a/examples/windows/z_get.c b/examples/windows/z_get.c index 7843f600b..597287afd 100644 --- a/examples/windows/z_get.c +++ b/examples/windows/z_get.c @@ -17,10 +17,15 @@ #include #include -#if Z_FEATURE_QUERY == 1 +#if Z_FEATURE_QUERY == 1 && Z_FEATURE_MULTI_THREAD == 1 +z_condvar_t cond; +z_mutex_t mutex; + void reply_dropper(void *ctx) { (void)(ctx); printf(">> Received query final notification\n"); + z_condvar_signal(&cond); + z_condvar_free(&cond); } void reply_handler(z_owned_reply_t *reply, void *ctx) { @@ -42,6 +47,9 @@ int main(int argc, char **argv) { const char *locator = NULL; const char *value = NULL; + z_mutex_init(&mutex); + z_condvar_init(&cond); + z_owned_config_t config = z_config_default(); if (locator != NULL) { zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(locator)); @@ -67,26 +75,19 @@ int main(int argc, char **argv) { return -1; } - printf("Enter any key to get data or 'q' to quit...\n"); - char c = '\0'; - while (1) { - fflush(stdin); - scanf("%c", &c); - if (c == 'q') { - break; - } - - printf("Sending Query '%s'...\n", keyexpr); - z_get_options_t opts = z_get_options_default(); - if (value != NULL) { - opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); - } - z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); - if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { - printf("Unable to send query.\n"); - return -1; - } + z_mutex_lock(&mutex); + printf("Sending Query '%s'...\n", keyexpr); + z_get_options_t opts = z_get_options_default(); + if (value != NULL) { + opts.value.payload = _z_bytes_wrap((const uint8_t *)value, strlen(value)); + } + z_owned_closure_reply_t callback = z_closure(reply_handler, reply_dropper); + if (z_get(z_loan(s), ke, "", z_move(callback), &opts) < 0) { + printf("Unable to send query.\n"); + return -1; } + z_condvar_wait(&cond, &mutex); + z_mutex_unlock(&mutex); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan(s)); @@ -98,7 +99,9 @@ int main(int argc, char **argv) { } #else int main(void) { - printf("ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY but this example requires it.\n"); + printf( + "ERROR: Zenoh pico was compiled without Z_FEATURE_QUERY or Z_FEATURE_MULTI_THREAD but this example requires " + "them.\n"); return -2; } #endif diff --git a/examples/windows/z_ping.c b/examples/windows/z_ping.c index 5643832c0..262e21659 100644 --- a/examples/windows/z_ping.c +++ b/examples/windows/z_ping.c @@ -27,17 +27,17 @@ #define DEFAULT_PING_NB 100 #define DEFAULT_WARMUP_MS 1000 -static zp_condvar_t cond; -static zp_mutex_t mutex; +static z_condvar_t cond; +static z_mutex_t mutex; void callback(const z_sample_t* sample, void* context) { (void)sample; (void)context; - zp_condvar_signal(&cond); + z_condvar_signal(&cond); } void drop(void* context) { (void)context; - zp_condvar_free(&cond); + z_condvar_free(&cond); } struct args_t { @@ -61,8 +61,8 @@ int main(int argc, char** argv) { DEFAULT_PKT_SIZE, DEFAULT_PING_NB, DEFAULT_WARMUP_MS); return 1; } - zp_mutex_init(&mutex); - zp_condvar_init(&cond); + z_mutex_init(&mutex); + z_condvar_init(&cond); z_owned_config_t config = z_config_default(); z_owned_session_t session = z_open(z_move(config)); if (!z_check(session)) { @@ -91,34 +91,34 @@ int main(int argc, char** argv) { return -1; } - uint8_t* data = zp_malloc(args.size); + uint8_t* data = z_malloc(args.size); for (unsigned int i = 0; i < args.size; i++) { data[i] = i % 10; } - zp_mutex_lock(&mutex); + z_mutex_lock(&mutex); if (args.warmup_ms) { printf("Warming up for %dms...\n", args.warmup_ms); - zp_clock_t warmup_start = zp_clock_now(); + 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); - zp_condvar_wait(&cond, &mutex); - elapsed_us = zp_clock_elapsed_us(&warmup_start); + z_condvar_wait(&cond, &mutex); + elapsed_us = z_clock_elapsed_us(&warmup_start); } } - unsigned long* results = zp_malloc(sizeof(unsigned long) * args.number_of_pings); + unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (unsigned int i = 0; i < args.number_of_pings; i++) { - zp_clock_t measure_start = zp_clock_now(); + z_clock_t measure_start = z_clock_now(); z_publisher_put(z_loan(pub), data, args.size, NULL); - zp_condvar_wait(&cond, &mutex); - results[i] = zp_clock_elapsed_us(&measure_start); + z_condvar_wait(&cond, &mutex); + results[i] = z_clock_elapsed_us(&measure_start); } for (unsigned int i = 0; i < args.number_of_pings; i++) { printf("%d bytes: seq=%d rtt=%luus, lat=%luus\n", args.size, i, results[i], results[i] / 2); } - zp_mutex_unlock(&mutex); - zp_free(results); - zp_free(data); + z_mutex_unlock(&mutex); + z_free(results); + z_free(data); z_drop(z_move(pub)); z_drop(z_move(sub)); diff --git a/examples/windows/z_pub.c b/examples/windows/z_pub.c index 8c17d7f7d..ce62da93d 100644 --- a/examples/windows/z_pub.c +++ b/examples/windows/z_pub.c @@ -54,6 +54,7 @@ int main(int argc, char **argv) { return -1; } + printf("Press CTRL-C to quit...\n"); char *buf = (char *)malloc(256); for (int idx = 0; 1; ++idx) { Sleep(1); diff --git a/examples/windows/z_pub_st.c b/examples/windows/z_pub_st.c index 8adea5c53..6d6f59d87 100644 --- a/examples/windows/z_pub_st.c +++ b/examples/windows/z_pub_st.c @@ -18,6 +18,8 @@ #include #include +#define N 2147483647 // max int value by default + #if Z_FEATURE_PUBLICATION == 1 int main(int argc, char **argv) { (void)(argc); @@ -47,16 +49,17 @@ int main(int argc, char **argv) { return -1; } + printf("Press CTRL-C to quit...\n"); char *buf = (char *)malloc(256); - zp_clock_t now = zp_clock_now(); - for (int idx = 0; 1;) { - if (zp_clock_elapsed_ms(&now) > 1000) { + z_clock_t now = z_clock_now(); + for (int idx = 0; idx < N;) { + 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); ++idx; - now = zp_clock_now(); + now = z_clock_now(); } zp_read(z_loan(s), NULL); diff --git a/examples/windows/z_queryable.c b/examples/windows/z_queryable.c index 7a2d7771c..ea504d0e7 100644 --- a/examples/windows/z_queryable.c +++ b/examples/windows/z_queryable.c @@ -74,11 +74,9 @@ int main(int argc, char **argv) { return -1; } - printf("Enter 'q' to quit...\n"); - char c = '\0'; - while (c != 'q') { - fflush(stdin); - scanf("%c", &c); + printf("Press CTRL-C to quit...\n"); + while (1) { + Sleep(1); } z_undeclare_queryable(z_move(qable)); diff --git a/examples/windows/z_sub.c b/examples/windows/z_sub.c index 0ffd26568..bde475e8b 100644 --- a/examples/windows/z_sub.c +++ b/examples/windows/z_sub.c @@ -62,11 +62,9 @@ int main(int argc, char **argv) { return -1; } - printf("Enter 'q' to quit...\n"); - char c = '\0'; - while (c != 'q') { - fflush(stdin); - scanf("%c", &c); + printf("Press CTRL-C to quit...\n"); + while (1) { + Sleep(1); } z_undeclare_subscriber(z_move(sub)); diff --git a/examples/windows/z_sub_st.c b/examples/windows/z_sub_st.c index 9b41877fa..fa91d9e41 100644 --- a/examples/windows/z_sub_st.c +++ b/examples/windows/z_sub_st.c @@ -18,6 +18,9 @@ #include #include +#define N 2147483647 // max int value by default +int msg_nb = 0; + #if Z_FEATURE_SUBSCRIPTION == 1 void data_handler(const z_sample_t *sample, void *ctx) { (void)(ctx); @@ -25,6 +28,8 @@ void data_handler(const z_sample_t *sample, void *ctx) { printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_loan(keystr), (int)sample->payload.len, sample->payload.start); z_drop(z_move(keystr)); + + msg_nb++; } int main(int argc, char **argv) { @@ -55,7 +60,8 @@ int main(int argc, char **argv) { return -1; } - while (1) { + printf("Press CTRL-C to quit...\n"); + while (msg_nb < N) { zp_read(z_loan(s), NULL); zp_send_keep_alive(z_loan(s), NULL); zp_send_join(z_loan(s), NULL); diff --git a/examples/zephyr/z_queryable.c b/examples/zephyr/z_queryable.c index 044664bb2..febdaabe2 100644 --- a/examples/zephyr/z_queryable.c +++ b/examples/zephyr/z_queryable.c @@ -75,7 +75,7 @@ int main(int argc, char **argv) { printf("Zenoh setup finished!\n"); while (1) { - sleep(5); + sleep(1); } printf("Closing Zenoh Session..."); diff --git a/examples/zephyr/z_sub.c b/examples/zephyr/z_sub.c index ac307a47a..ce0a0e73d 100644 --- a/examples/zephyr/z_sub.c +++ b/examples/zephyr/z_sub.c @@ -69,7 +69,7 @@ int main(int argc, char **argv) { printf("OK!\n"); while (1) { - sleep(5); + sleep(1); } printf("Closing Zenoh Session..."); diff --git a/extra_script.py b/extra_script.py index c80f143c2..dc4332e65 100644 --- a/extra_script.py +++ b/extra_script.py @@ -28,6 +28,7 @@ "-", "-", "-", + "-", "-"] CPPDEFINES = ["ZENOH_ZEPHYR"] @@ -43,6 +44,7 @@ "-", "-", "-", + "-", "-", "-"] CPPDEFINES = ["ZENOH_ARDUINO_ESP32", "ZENOH_C_STANDARD=99"] @@ -58,6 +60,7 @@ "-", "-", "-", + "-", "-", "-"] CPPDEFINES = ["ZENOH_ARDUINO_OPENCR", "ZENOH_C_STANDARD=99", "Z_FEATURE_MULTI_THREAD=0"] @@ -71,6 +74,7 @@ "-", "-", "-", + "-", "-", "-"] CPPDEFINES = ["ZENOH_ESPIDF"] @@ -84,6 +88,7 @@ "-", "-", "-", + "-", "-", "-"] CPPDEFINES = ["ZENOH_MBED", "ZENOH_C_STANDARD=99"] diff --git a/include/zenoh-pico/api/primitives.h b/include/zenoh-pico/api/primitives.h index a72878b99..7fb5c6120 100644 --- a/include/zenoh-pico/api/primitives.h +++ b/include/zenoh-pico/api/primitives.h @@ -114,7 +114,7 @@ _Bool zp_keyexpr_was_declared(const z_keyexpr_t *keyexpr); /** * Constructs a null-terminated string departing from a :c:type:`z_keyexpr_t` for a given :c:type:`z_session_t`. - * The user is responsible of dropping the returned string using ``zp_free``. + * The user is responsible of dropping the returned string using ``z_free``. * * Parameters: * zs: A loaned instance of the the :c:type:`z_session_t` to resolve the keyexpr. diff --git a/include/zenoh-pico/api/types.h b/include/zenoh-pico/api/types.h index 6932536e4..0c53a7383 100644 --- a/include/zenoh-pico/api/types.h +++ b/include/zenoh-pico/api/types.h @@ -335,6 +335,7 @@ typedef struct { z_value_t value; z_query_consolidation_t consolidation; z_query_target_t target; + uint32_t timeout_ms; #if Z_FEATURE_ATTACHMENT == 1 // TODO:ATT z_attachment_t attachment; #endif @@ -346,7 +347,7 @@ typedef struct { */ typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 - zp_task_attr_t *task_attributes; + z_task_attr_t *task_attributes; #else uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior #endif @@ -358,7 +359,7 @@ typedef struct { */ typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 - zp_task_attr_t *task_attributes; + z_task_attr_t *task_attributes; #else uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior #endif @@ -388,6 +389,32 @@ typedef struct { uint8_t __dummy; // Just to avoid empty structures that might cause undefined behavior } zp_send_join_options_t; +/** + * QoS settings of zenoh message. + */ +typedef _z_qos_t z_qos_t; +/** + * Returns message priority. + */ +static inline z_priority_t z_qos_get_priority(z_qos_t qos) { + z_priority_t ret = _z_n_qos_get_priority(qos); + return ret == _Z_PRIORITY_CONTROL ? Z_PRIORITY_DEFAULT : ret; +} +/** + * Returns message congestion control. + */ +static inline z_congestion_control_t z_qos_get_congestion_control(z_qos_t qos) { + return _z_n_qos_get_congestion_control(qos); +} +/** + * Returns message express flag. If set to true, the message is not batched to reduce the latency. + */ +static inline _Bool z_qos_get_express(z_qos_t qos) { return _z_n_qos_get_express(qos); } +/** + * Returns default qos settings. + */ +static inline z_qos_t z_qos_default(void) { return _Z_N_QOS_DEFAULT; } + /** * Represents a data sample. * @@ -399,6 +426,7 @@ typedef struct { * z_encoding_t encoding: The encoding of the value of this data sample. * z_sample_kind_t kind: The kind of this data sample (PUT or DELETE). * z_timestamp_t timestamp: The timestamp of this data sample. + * z_qos_t qos: Quality of service settings used to deliver this sample. */ typedef _z_sample_t z_sample_t; diff --git a/include/zenoh-pico/collections/array.h b/include/zenoh-pico/collections/array.h index a54a66af3..fe2b3f306 100644 --- a/include/zenoh-pico/collections/array.h +++ b/include/zenoh-pico/collections/array.h @@ -33,7 +33,7 @@ } \ static inline name##_array_t name##_array_make(size_t capacity) { \ name##_array_t a; \ - a._val = (type *)zp_malloc(capacity * sizeof(type)); \ + a._val = (type *)z_malloc(capacity * sizeof(type)); \ if (a._val != NULL) { \ a._len = capacity; \ } else { \ @@ -58,7 +58,7 @@ for (size_t i = 0; i < a->_len; i++) { \ name##_elem_clear(&a->_val[i]); \ } \ - zp_free(a->_val); \ + z_free(a->_val); \ a->_len = 0; \ a->_val = NULL; \ } \ @@ -66,7 +66,7 @@ name##_array_t *ptr = *a; \ if (ptr != NULL) { \ name##_array_clear(ptr); \ - zp_free(ptr); \ + z_free(ptr); \ *a = NULL; \ } \ } diff --git a/include/zenoh-pico/collections/element.h b/include/zenoh-pico/collections/element.h index 5e8997954..96a62e4eb 100644 --- a/include/zenoh-pico/collections/element.h +++ b/include/zenoh-pico/collections/element.h @@ -35,13 +35,13 @@ typedef _Bool (*z_element_eq_f)(const void *left, const void *right); type *ptr = (type *)*e; \ if (ptr != NULL) { \ elem_clear_f(ptr); \ - zp_free(ptr); \ + z_free(ptr); \ *e = NULL; \ } \ } \ static inline void name##_elem_copy(void *dst, const void *src) { elem_copy_f((type *)dst, (type *)src); } \ static inline void *name##_elem_clone(const void *src) { \ - type *dst = (type *)zp_malloc(elem_size_f((type *)src)); \ + type *dst = (type *)z_malloc(elem_size_f((type *)src)); \ if (dst != NULL) { \ elem_copy_f(dst, (type *)src); \ } \ diff --git a/include/zenoh-pico/collections/intmap.h b/include/zenoh-pico/collections/intmap.h index 1d33d8afc..caa54e035 100644 --- a/include/zenoh-pico/collections/intmap.h +++ b/include/zenoh-pico/collections/intmap.h @@ -70,7 +70,7 @@ void _z_int_void_map_free(_z_int_void_map_t **map, z_element_free_f f); name##_intmap_entry_t *ptr = (name##_intmap_entry_t *)*e; \ if (ptr != NULL) { \ name##_elem_free(&ptr->_val); \ - zp_free(ptr); \ + z_free(ptr); \ *e = NULL; \ } \ } \ diff --git a/include/zenoh-pico/collections/list.h b/include/zenoh-pico/collections/list.h index 339e7c25f..210737047 100644 --- a/include/zenoh-pico/collections/list.h +++ b/include/zenoh-pico/collections/list.h @@ -42,6 +42,7 @@ void *_z_list_head(const _z_list_t *xs); _z_list_t *_z_list_tail(const _z_list_t *xs); _z_list_t *_z_list_push(_z_list_t *xs, void *x); +_z_list_t *_z_list_push_back(_z_list_t *xs, void *x); _z_list_t *_z_list_pop(_z_list_t *xs, z_element_free_f f_f, void **x); _z_list_t *_z_list_find(const _z_list_t *xs, z_element_eq_f f_f, void *e); diff --git a/include/zenoh-pico/collections/refcount.h b/include/zenoh-pico/collections/refcount.h index 183d11e46..73d5f4807 100644 --- a/include/zenoh-pico/collections/refcount.h +++ b/include/zenoh-pico/collections/refcount.h @@ -18,6 +18,8 @@ #include #include +#include "zenoh-pico/utils/result.h" + #if Z_FEATURE_MULTI_THREAD == 1 #if ZENOH_C_STANDARD != 99 @@ -43,9 +45,10 @@ // c11 atomic variant #define _ZP_RC_CNT_TYPE _z_atomic(unsigned int) -#define _ZP_RC_OP_INIT_CNT _z_atomic_store_explicit(&p.in->_cnt, 1, _z_memory_order_relaxed); -#define _ZP_RC_OP_INCR_CNT _z_atomic_fetch_add_explicit(&p->in->_cnt, 1, _z_memory_order_relaxed); -#define _ZP_RC_OP_DECR_AND_CMP _z_atomic_fetch_sub_explicit(&p->in->_cnt, 1, _z_memory_order_release) > 1 +#define _ZP_RC_OP_INIT_CNT _z_atomic_store_explicit(&p.in->_cnt, (unsigned int)1, _z_memory_order_relaxed); +#define _ZP_RC_OP_INCR_CNT _z_atomic_fetch_add_explicit(&p->in->_cnt, (unsigned int)1, _z_memory_order_relaxed); +#define _ZP_RC_OP_DECR_AND_CMP \ + _z_atomic_fetch_sub_explicit(&p->in->_cnt, (unsigned int)1, _z_memory_order_release) > (unsigned int)1 #define _ZP_RC_OP_SYNC atomic_thread_fence(_z_memory_order_acquire); #else // ZENOH_C_STANDARD == 99 @@ -53,11 +56,11 @@ // c99 gcc sync builtin variant #define _ZP_RC_CNT_TYPE unsigned int -#define _ZP_RC_OP_INIT_CNT \ - __sync_fetch_and_and(&p.in->_cnt, 0); \ - __sync_fetch_and_add(&p.in->_cnt, 1); -#define _ZP_RC_OP_INCR_CNT __sync_fetch_and_add(&p->in->_cnt, 1); -#define _ZP_RC_OP_DECR_AND_CMP __sync_fetch_and_sub(&p->in->_cnt, 1) > 1 +#define _ZP_RC_OP_INIT_CNT \ + __sync_fetch_and_and(&p.in->_cnt, (unsigned int)0); \ + __sync_fetch_and_add(&p.in->_cnt, (unsigned int)1); +#define _ZP_RC_OP_INCR_CNT __sync_fetch_and_add(&p->in->_cnt, (unsigned int)1); +#define _ZP_RC_OP_DECR_AND_CMP __sync_fetch_and_sub(&p->in->_cnt, (unsigned int)1) > (unsigned int)1 #define _ZP_RC_OP_SYNC __sync_synchronize(); #else // !ZENOH_COMPILER_GCC @@ -76,9 +79,9 @@ // Single thread variant #define _ZP_RC_CNT_TYPE unsigned int -#define _ZP_RC_OP_INIT_CNT p.in->_cnt = 1; -#define _ZP_RC_OP_INCR_CNT p->in->_cnt += 1; -#define _ZP_RC_OP_DECR_AND_CMP p->in->_cnt-- > 1 +#define _ZP_RC_OP_INIT_CNT p.in->_cnt = (unsigned int)1; +#define _ZP_RC_OP_INCR_CNT p->in->_cnt += (unsigned int)1; +#define _ZP_RC_OP_DECR_AND_CMP p->in->_cnt-- > (unsigned int)1 #define _ZP_RC_OP_SYNC #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -94,7 +97,7 @@ } name##_rc_t; \ static inline name##_rc_t name##_rc_new(void) { \ name##_rc_t p; \ - p.in = (name##_inner_rc_t *)zp_malloc(sizeof(name##_inner_rc_t)); \ + p.in = (name##_inner_rc_t *)z_malloc(sizeof(name##_inner_rc_t)); \ if (p.in != NULL) { \ memset(&p.in->val, 0, sizeof(type##_t)); \ _ZP_RC_OP_INIT_CNT \ @@ -103,7 +106,7 @@ } \ static inline name##_rc_t name##_rc_new_from_val(type##_t val) { \ name##_rc_t p; \ - p.in = (name##_inner_rc_t *)zp_malloc(sizeof(name##_inner_rc_t)); \ + p.in = (name##_inner_rc_t *)z_malloc(sizeof(name##_inner_rc_t)); \ if (p.in != NULL) { \ p.in->val = val; \ _ZP_RC_OP_INIT_CNT \ @@ -117,7 +120,7 @@ return c; \ } \ static inline name##_rc_t *name##_rc_clone_as_ptr(name##_rc_t *p) { \ - name##_rc_t *c = (name##_rc_t *)zp_malloc(sizeof(name##_rc_t)); \ + name##_rc_t *c = (name##_rc_t *)z_malloc(sizeof(name##_rc_t)); \ if (c != NULL) { \ c->in = p->in; \ _ZP_RC_OP_INCR_CNT \ @@ -140,8 +143,12 @@ } \ _ZP_RC_OP_SYNC \ type##_clear(&p->in->val); \ - zp_free(p->in); \ + z_free(p->in); \ return true; \ + } \ + static inline size_t name##_rc_size(name##_rc_t *p) { \ + _ZP_UNUSED(p); \ + return sizeof(name##_rc_t); \ } #endif /* ZENOH_PICO_COLLECTIONS_REFCOUNT_H */ diff --git a/include/zenoh-pico/config.h b/include/zenoh-pico/config.h index 76a2d301c..c61f8ebb6 100644 --- a/include/zenoh-pico/config.h +++ b/include/zenoh-pico/config.h @@ -337,4 +337,11 @@ #define ZP_ASM_NOP __asm__("nop") #endif +/** + * Default get timeout in milliseconds. + */ +#ifndef Z_GET_TIMEOUT_DEFAULT +#define Z_GET_TIMEOUT_DEFAULT 10000 +#endif + #endif /* INCLUDE_ZENOH_PICO_CONFIG_H */ diff --git a/include/zenoh-pico/deprecated/platform.h b/include/zenoh-pico/deprecated/platform.h new file mode 100644 index 000000000..5e47d65c6 --- /dev/null +++ b/include/zenoh-pico/deprecated/platform.h @@ -0,0 +1,88 @@ +// 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, + +#ifndef ZENOH_PICO_DEPRECATED_PLATFORM_H +#define ZENOH_PICO_DEPRECATED_PLATFORM_H + +#include + +#include "zenoh-pico/config.h" +#include "zenoh-pico/system/platform-common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*------------------ Random ------------------*/ +uint8_t zp_random_u8(void); +uint16_t zp_random_u16(void); +uint32_t zp_random_u32(void); +uint64_t zp_random_u64(void); +void zp_random_fill(void *buf, size_t len); + +/*------------------ Memory ------------------*/ +void *zp_malloc(size_t size); +void *zp_realloc(void *ptr, size_t size); +void zp_free(void *ptr); + +#if Z_FEATURE_MULTI_THREAD == 1 +/*------------------ Thread ------------------*/ +typedef z_task_t zp_task_t; +typedef z_task_attr_t zp_task_attr_t; + +int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg); +int8_t zp_task_join(zp_task_t *task); +void zp_task_free(zp_task_t **task); + +/*------------------ Mutex ------------------*/ +typedef z_mutex_t zp_mutex_t; +int8_t zp_mutex_init(zp_mutex_t *m); +int8_t zp_mutex_free(zp_mutex_t *m); + +int8_t zp_mutex_lock(zp_mutex_t *m); +int8_t zp_mutex_trylock(zp_mutex_t *m); +int8_t zp_mutex_unlock(zp_mutex_t *m); + +/*------------------ CondVar ------------------*/ +typedef z_condvar_t zp_condvar_t; +int8_t zp_condvar_init(zp_condvar_t *cv); +int8_t zp_condvar_free(zp_condvar_t *cv); + +int8_t zp_condvar_signal(zp_condvar_t *cv); +int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m); +#endif // Z_FEATURE_MULTI_THREAD == 1 + +/*------------------ Sleep ------------------*/ +int zp_sleep_us(size_t time); +int zp_sleep_ms(size_t time); +int zp_sleep_s(size_t time); + +/*------------------ Clock ------------------*/ +typedef z_clock_t zp_clock_t; +zp_clock_t zp_clock_now(void); +unsigned long zp_clock_elapsed_us(zp_clock_t *time); +unsigned long zp_clock_elapsed_ms(zp_clock_t *time); +unsigned long zp_clock_elapsed_s(zp_clock_t *time); + +/*------------------ Time ------------------*/ +typedef z_time_t zp_time_t; +zp_time_t zp_time_now(void); +const char *zp_time_now_as_str(char *const buf, unsigned long buflen); +unsigned long zp_time_elapsed_us(zp_time_t *time); +unsigned long zp_time_elapsed_ms(zp_time_t *time); +unsigned long zp_time_elapsed_s(zp_time_t *time); + +#ifdef __cplusplus +} +#endif + +#endif /* ZENOH_PICO_DEPRECATED_PLATFORM_H */ diff --git a/include/zenoh-pico/link/config/bt.h b/include/zenoh-pico/link/config/bt.h index 40827e76c..0a68898b0 100644 --- a/include/zenoh-pico/link/config/bt.h +++ b/include/zenoh-pico/link/config/bt.h @@ -32,14 +32,14 @@ #define BT_CONFIG_TOUT_KEY 0x03 #define BT_CONFIG_TOUT_STR "tout" -#define BT_CONFIG_MAPPING_BUILD \ - _z_str_intmapping_t args[BT_CONFIG_ARGC]; \ - args[0]._key = BT_CONFIG_MODE_KEY; \ - args[0]._str = BT_CONFIG_MODE_STR; \ - args[1]._key = BT_CONFIG_PROFILE_KEY; \ - args[1]._str = BT_CONFIG_PROFILE_STR; \ - args[2]._key = BT_CONFIG_TOUT_KEY; \ - args[2]._str = BT_CONFIG_TOUT_STR; +#define BT_CONFIG_MAPPING_BUILD \ + _z_str_intmapping_t args[BT_CONFIG_ARGC]; \ + args[0]._key = BT_CONFIG_MODE_KEY; \ + args[0]._str = (char *)BT_CONFIG_MODE_STR; \ + args[1]._key = BT_CONFIG_PROFILE_KEY; \ + args[1]._str = (char *)BT_CONFIG_PROFILE_STR; \ + args[2]._key = BT_CONFIG_TOUT_KEY; \ + args[2]._str = (char *)BT_CONFIG_TOUT_STR; size_t _z_bt_config_strlen(const _z_str_intmap_t *s); diff --git a/include/zenoh-pico/link/config/serial.h b/include/zenoh-pico/link/config/serial.h index 85270be62..7eb4408ba 100644 --- a/include/zenoh-pico/link/config/serial.h +++ b/include/zenoh-pico/link/config/serial.h @@ -45,7 +45,7 @@ #define SERIAL_CONFIG_MAPPING_BUILD \ _z_str_intmapping_t args[SERIAL_CONFIG_ARGC]; \ args[0]._key = SERIAL_CONFIG_BAUDRATE_KEY; \ - args[0]._str = SERIAL_CONFIG_BAUDRATE_STR; + args[0]._str = (char *)SERIAL_CONFIG_BAUDRATE_STR; // args[1]._key = SERIAL_CONFIG_DATABITS_KEY; // args[1]._str = SERIAL_CONFIG_DATABITS_STR; // args[2]._key = SERIAL_CONFIG_FLOWCONTROL_KEY; diff --git a/include/zenoh-pico/link/config/tcp.h b/include/zenoh-pico/link/config/tcp.h index ed57e7700..9e3e0f5a8 100644 --- a/include/zenoh-pico/link/config/tcp.h +++ b/include/zenoh-pico/link/config/tcp.h @@ -29,7 +29,7 @@ #define TCP_CONFIG_MAPPING_BUILD \ _z_str_intmapping_t args[TCP_CONFIG_ARGC]; \ args[0]._key = TCP_CONFIG_TOUT_KEY; \ - args[0]._str = TCP_CONFIG_TOUT_STR; + args[0]._str = (char *)TCP_CONFIG_TOUT_STR; size_t _z_tcp_config_strlen(const _z_str_intmap_t *s); diff --git a/include/zenoh-pico/link/config/udp.h b/include/zenoh-pico/link/config/udp.h index dd374b5b6..fb6ee2c64 100644 --- a/include/zenoh-pico/link/config/udp.h +++ b/include/zenoh-pico/link/config/udp.h @@ -30,14 +30,14 @@ #define UDP_CONFIG_JOIN_STR "join" #if Z_FEATURE_LINK_UDP_UNICAST == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1 -#define UDP_CONFIG_MAPPING_BUILD \ - _z_str_intmapping_t args[UDP_CONFIG_ARGC]; \ - args[0]._key = UDP_CONFIG_IFACE_KEY; \ - args[0]._str = UDP_CONFIG_IFACE_STR; \ - args[1]._key = UDP_CONFIG_TOUT_KEY; \ - args[1]._str = UDP_CONFIG_TOUT_STR; \ - args[2]._key = UDP_CONFIG_JOIN_KEY; \ - args[2]._str = UDP_CONFIG_JOIN_STR; +#define UDP_CONFIG_MAPPING_BUILD \ + _z_str_intmapping_t args[UDP_CONFIG_ARGC]; \ + args[0]._key = UDP_CONFIG_IFACE_KEY; \ + args[0]._str = (char *)UDP_CONFIG_IFACE_STR; \ + args[1]._key = UDP_CONFIG_TOUT_KEY; \ + args[1]._str = (char *)UDP_CONFIG_TOUT_STR; \ + args[2]._key = UDP_CONFIG_JOIN_KEY; \ + args[2]._str = (char *)UDP_CONFIG_JOIN_STR; size_t _z_udp_config_strlen(const _z_str_intmap_t *s); diff --git a/include/zenoh-pico/net/primitives.h b/include/zenoh-pico/net/primitives.h index ae8980ceb..7000044e1 100644 --- a/include/zenoh-pico/net/primitives.h +++ b/include/zenoh-pico/net/primitives.h @@ -223,7 +223,7 @@ int8_t _z_send_reply(const _z_query_t *query, const _z_keyexpr_t keyexpr, const */ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, const _z_value_t value, _z_reply_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop + void *arg_call, _z_drop_handler_t dropper, void *arg_drop, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment diff --git a/include/zenoh-pico/net/session.h b/include/zenoh-pico/net/session.h index 8de397141..d4190c9e7 100644 --- a/include/zenoh-pico/net/session.h +++ b/include/zenoh-pico/net/session.h @@ -29,7 +29,7 @@ */ typedef struct _z_session_t { #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_t _mutex_inner; + z_mutex_t _mutex_inner; #endif // Z_FEATURE_MULTI_THREAD == 1 // Zenoh-pico is considering a single transport per session. @@ -148,7 +148,7 @@ int8_t _zp_send_join(_z_session_t *z); * Returns: * ``0`` in case of success, ``-1`` in case of failure. */ -int8_t _zp_start_read_task(_z_session_t *z, zp_task_attr_t *attr); +int8_t _zp_start_read_task(_z_session_t *z, z_task_attr_t *attr); /** * Stop the read task. This may result in stopping a thread or a process depending @@ -175,7 +175,7 @@ int8_t _zp_stop_read_task(_z_session_t *z); * Returns: * ``0`` in case of success, ``-1`` in case of failure. */ -int8_t _zp_start_lease_task(_z_session_t *z, zp_task_attr_t *attr); +int8_t _zp_start_lease_task(_z_session_t *z, z_task_attr_t *attr); /** * Stop the lease task. This may result in stopping a thread or a process depending diff --git a/include/zenoh-pico/protocol/core.h b/include/zenoh-pico/protocol/core.h index 113566319..94e86215a 100644 --- a/include/zenoh-pico/protocol/core.h +++ b/include/zenoh-pico/protocol/core.h @@ -211,6 +211,13 @@ _z_keyexpr_t _z_rname(const char *rname); */ _z_keyexpr_t _z_rid_with_suffix(uint16_t rid, const char *suffix); +/** + * QoS settings of zenoh message. + */ +typedef struct { + uint8_t _val; +} _z_qos_t; + /** * A zenoh-net data sample. * @@ -227,6 +234,7 @@ typedef struct { _z_timestamp_t timestamp; _z_encoding_t encoding; z_sample_kind_t kind; + _z_qos_t qos; #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t attachment; #endif diff --git a/include/zenoh-pico/protocol/definitions/declarations.h b/include/zenoh-pico/protocol/definitions/declarations.h index b40cb3fc4..66e21add2 100644 --- a/include/zenoh-pico/protocol/definitions/declarations.h +++ b/include/zenoh-pico/protocol/definitions/declarations.h @@ -95,8 +95,6 @@ typedef struct { } _z_undecl_interest_t; _z_undecl_interest_t _z_undecl_interest_null(void); -#define _Z_FLAG_INTEREST_ID (1 << 5) - typedef struct { enum { _Z_DECL_KEXPR, diff --git a/include/zenoh-pico/protocol/definitions/network.h b/include/zenoh-pico/protocol/definitions/network.h index 94d7736c8..1a26cddbb 100644 --- a/include/zenoh-pico/protocol/definitions/network.h +++ b/include/zenoh-pico/protocol/definitions/network.h @@ -37,6 +37,12 @@ /*=============================*/ #define _Z_FLAG_N_Z 0x80 // 1 << 7 +// DECLARE message flags: +// - I: Interest If I==1 then the declare is in a response to an Interest with future==false +// - X: Reserved +// - Z: Extension If Z==1 then Zenoh extensions are present +#define _Z_FLAG_N_DECLARE_I 0x20 // 1 << 5 + // PUSH message flags: // N Named if N==1 then the key expr has name/suffix // M Mapping if M==1 then keyexpr mapping is the one declared by the sender, otherwise by the receiver @@ -58,12 +64,25 @@ #define _Z_FLAG_N_RESPONSE_N 0x20 // 1 << 5 #define _Z_FLAG_N_RESPONSE_M 0x40 // 1 << 6 -typedef struct { - uint8_t _val; -} _z_n_qos_t; - -#define _z_n_qos_make(express, nodrop, priority) \ - (_z_n_qos_t) { ._val = (((express) << 4) | ((nodrop) << 3) | (priority)) } +typedef _z_qos_t _z_n_qos_t; + +static inline _z_qos_t _z_n_qos_create(_Bool express, z_congestion_control_t congestion_control, + z_priority_t priority) { + _z_n_qos_t ret; + _Bool nodrop = congestion_control == Z_CONGESTION_CONTROL_DROP ? 0 : 1; + ret._val = (uint8_t)((express << 4) | (nodrop << 3) | priority); + return ret; +} +static inline z_priority_t _z_n_qos_get_priority(_z_n_qos_t n_qos) { + return (z_priority_t)(n_qos._val & 0x07 /* 0b111 */); +} +static inline z_congestion_control_t _z_n_qos_get_congestion_control(_z_n_qos_t n_qos) { + return (n_qos._val & 0x08 /* 0b1000 */) ? Z_CONGESTION_CONTROL_BLOCK : Z_CONGESTION_CONTROL_DROP; +} +static inline _Bool _z_n_qos_get_express(_z_n_qos_t n_qos) { return (_Bool)(n_qos._val & 0x10 /* 0b10000 */); } +#define _z_n_qos_make(express, nodrop, priority) \ + _z_n_qos_create((_Bool)express, nodrop ? Z_CONGESTION_CONTROL_BLOCK : Z_CONGESTION_CONTROL_DROP, \ + (z_priority_t)priority) #define _Z_N_QOS_DEFAULT _z_n_qos_make(0, 0, 5) // RESPONSE FINAL message flags: @@ -220,7 +239,8 @@ _Z_VEC_DEFINE(_z_network_message, _z_network_message_t) void _z_msg_fix_mapping(_z_zenoh_message_t *msg, uint16_t mapping); _z_network_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value + z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, + uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment diff --git a/include/zenoh-pico/protocol/keyexpr.h b/include/zenoh-pico/protocol/keyexpr.h index abb165170..193d3b54e 100644 --- a/include/zenoh-pico/protocol/keyexpr.h +++ b/include/zenoh-pico/protocol/keyexpr.h @@ -34,8 +34,6 @@ static inline _z_keyexpr_t _z_keyexpr_null(void) { _z_keyexpr_t keyexpr = {0, {0}, NULL}; return keyexpr; } -_z_timestamp_t _z_timestamp_duplicate(const _z_timestamp_t *tstamp); -void _z_timestamp_clear(_z_timestamp_t *tstamp); void _z_keyexpr_clear(_z_keyexpr_t *rk); void _z_keyexpr_free(_z_keyexpr_t **rk); diff --git a/include/zenoh-pico/session/session.h b/include/zenoh-pico/session/session.h index 5f2980d86..95809b8e1 100644 --- a/include/zenoh-pico/session/session.h +++ b/include/zenoh-pico/session/session.h @@ -51,7 +51,6 @@ void _z_reply_data_clear(_z_reply_data_t *rd); _Z_ELEM_DEFINE(_z_reply_data, _z_reply_data_t, _z_noop_size, _z_reply_data_clear, _z_noop_copy) _Z_LIST_DEFINE(_z_reply_data, _z_reply_data_t) -_Z_ARRAY_DEFINE(_z_reply_data, _z_reply_data_t) /** * An reply to a :c:func:`z_query`. @@ -103,7 +102,8 @@ void _z_subscription_clear(_z_subscription_t *sub); _Z_REFCOUNT_DEFINE(_z_subscription, _z_subscription) _Z_ELEM_DEFINE(_z_subscriber, _z_subscription_t, _z_noop_size, _z_subscription_clear, _z_noop_copy) -_Z_ELEM_DEFINE(_z_subscription_rc, _z_subscription_rc_t, _z_noop_size, _z_subscription_rc_drop, _z_subscription_rc_copy) +_Z_ELEM_DEFINE(_z_subscription_rc, _z_subscription_rc_t, _z_subscription_rc_size, _z_subscription_rc_drop, + _z_subscription_rc_copy) _Z_LIST_DEFINE(_z_subscription_rc, _z_subscription_rc_t) typedef struct { @@ -174,8 +174,8 @@ _Z_LIST_DEFINE(_z_pending_query, _z_pending_query_t) typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_t _mutex; - zp_condvar_t _cond_var; + z_mutex_t _mutex; + z_condvar_t _cond_var; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_reply_data_list_t *_replies; } _z_pending_query_collect_t; diff --git a/include/zenoh-pico/session/subscription.h b/include/zenoh-pico/session/subscription.h index b84091f2c..3532a0c68 100644 --- a/include/zenoh-pico/session/subscription.h +++ b/include/zenoh-pico/session/subscription.h @@ -17,21 +17,23 @@ #include "zenoh-pico/net/session.h" -#if Z_FEATURE_SUBSCRIPTION == 1 /*------------------ Subscription ------------------*/ -_z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); -_z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); - -_z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len + _z_zint_t payload_len, _z_n_qos_t qos #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t att #endif ); + +#if Z_FEATURE_SUBSCRIPTION == 1 +_z_subscription_rc_t *_z_get_subscription_by_id(_z_session_t *zn, uint8_t is_local, const _z_zint_t id); +_z_subscription_rc_list_t *_z_get_subscriptions_by_key(_z_session_t *zn, uint8_t is_local, const _z_keyexpr_t *keyexpr); + +_z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_local, _z_subscription_t *sub); int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, - const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp + const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, + const _z_n_qos_t qos #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t att diff --git a/include/zenoh-pico/system/link/raweth.h b/include/zenoh-pico/system/link/raweth.h index 2996b1931..ce956c835 100644 --- a/include/zenoh-pico/system/link/raweth.h +++ b/include/zenoh-pico/system/link/raweth.h @@ -18,6 +18,7 @@ #include #include "zenoh-pico/collections/string.h" +#include "zenoh-pico/protocol/core.h" #include "zenoh-pico/system/platform.h" #if Z_FEATURE_RAWETH_TRANSPORT == 1 @@ -31,6 +32,27 @@ // Max frame size #define _ZP_MAX_ETH_FRAME_SIZE 1514 +// Endpoing config types +typedef struct { + _z_keyexpr_t _keyexpr; + uint16_t _vlan; // vlan tag (pcp + dei + id), big endian + uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; + _Bool _has_vlan; +} _zp_raweth_mapping_entry_t; + +void _z_raweth_clear_mapping_entry(_zp_raweth_mapping_entry_t *entry); + +_Z_ELEM_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t, _z_noop_size, _z_raweth_clear_mapping_entry, + _z_noop_copy) +_Z_ARRAY_DEFINE(_zp_raweth_mapping, _zp_raweth_mapping_entry_t) + +typedef struct { + uint8_t _mac[_ZP_MAC_ADDR_LENGTH]; +} _zp_raweth_whitelist_entry_t; + +_Z_ELEM_DEFINE(_zp_raweth_whitelist, _zp_raweth_whitelist_entry_t, _z_noop_size, _z_noop_clear, _z_noop_copy) +_Z_ARRAY_DEFINE(_zp_raweth_whitelist, _zp_raweth_whitelist_entry_t) + // Ethernet header structure type typedef struct { uint8_t dmac[_ZP_MAC_ADDR_LENGTH]; // Destination mac address @@ -51,8 +73,10 @@ typedef struct { typedef struct { const char *_interface; _z_sys_net_socket_t _sock; + _zp_raweth_mapping_array_t _mapping; + _zp_raweth_whitelist_array_t _whitelist; uint16_t _vlan; - uint16_t ethtype; + uint16_t _ethtype; uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; uint8_t _smac[_ZP_MAC_ADDR_LENGTH]; _Bool _has_vlan; @@ -60,7 +84,8 @@ typedef struct { int8_t _z_open_raweth(_z_sys_net_socket_t *sock, const char *interface); size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t buff_len); -size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr); +size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr, + const _zp_raweth_whitelist_array_t *whitelist); int8_t _z_close_raweth(_z_sys_net_socket_t *sock); size_t _z_raweth_ntohs(size_t val); size_t _z_raweth_htons(size_t val); diff --git a/include/zenoh-pico/system/platform-common.h b/include/zenoh-pico/system/platform-common.h new file mode 100644 index 000000000..081a5a9c4 --- /dev/null +++ b/include/zenoh-pico/system/platform-common.h @@ -0,0 +1,108 @@ +// +// 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, +// Błażej Sowa, + +#ifndef ZENOH_PICO_SYSTEM_PLATFORM_COMMON_H +#define ZENOH_PICO_SYSTEM_PLATFORM_COMMON_H + +#include + +#include "zenoh-pico/config.h" + +#if defined(ZENOH_LINUX) || defined(ZENOH_MACOS) || defined(ZENOH_BSD) +#include "zenoh-pico/system/platform/unix.h" +#elif defined(ZENOH_WINDOWS) +#include "zenoh-pico/system/platform/windows.h" +#elif defined(ZENOH_ESPIDF) +#include "zenoh-pico/system/platform/espidf.h" +#elif defined(ZENOH_MBED) +#include "zenoh-pico/system/platform/mbed.h" +#elif defined(ZENOH_ZEPHYR) +#include "zenoh-pico/system/platform/zephyr.h" +#elif defined(ZENOH_ARDUINO_ESP32) +#include "zenoh-pico/system/platform/arduino/esp32.h" +#elif defined(ZENOH_ARDUINO_OPENCR) +#include "zenoh-pico/system/platform/arduino/opencr.h" +#elif defined(ZENOH_EMSCRIPTEN) +#include "zenoh-pico/system/platform/emscripten.h" +#elif defined(ZENOH_FLIPPER) +#include "zenoh-pico/system/platform/flipper.h" +#elif defined(ZENOH_FREERTOS_PLUS_TCP) +#include "zenoh-pico/system/platform/freertos_plus_tcp.h" +#else +#include "zenoh-pico/system/platform/void.h" +#error "Unknown platform" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/*------------------ Random ------------------*/ +uint8_t z_random_u8(void); +uint16_t z_random_u16(void); +uint32_t z_random_u32(void); +uint64_t z_random_u64(void); +void z_random_fill(void *buf, size_t len); + +/*------------------ Memory ------------------*/ +void *z_malloc(size_t size); +void *z_realloc(void *ptr, size_t size); +void z_free(void *ptr); + +#if Z_FEATURE_MULTI_THREAD == 1 +/*------------------ Thread ------------------*/ +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg); +int8_t z_task_join(z_task_t *task); +int8_t zp_task_cancel(z_task_t *task); +void z_task_free(z_task_t **task); + +/*------------------ Mutex ------------------*/ +int8_t z_mutex_init(z_mutex_t *m); +int8_t z_mutex_free(z_mutex_t *m); + +int8_t z_mutex_lock(z_mutex_t *m); +int8_t z_mutex_trylock(z_mutex_t *m); +int8_t z_mutex_unlock(z_mutex_t *m); + +/*------------------ CondVar ------------------*/ +int8_t z_condvar_init(z_condvar_t *cv); +int8_t z_condvar_free(z_condvar_t *cv); + +int8_t z_condvar_signal(z_condvar_t *cv); +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m); +#endif // Z_FEATURE_MULTI_THREAD == 1 + +/*------------------ Sleep ------------------*/ +int z_sleep_us(size_t time); +int z_sleep_ms(size_t time); +int z_sleep_s(size_t time); + +/*------------------ Clock ------------------*/ +z_clock_t z_clock_now(void); +unsigned long z_clock_elapsed_us(z_clock_t *time); +unsigned long z_clock_elapsed_ms(z_clock_t *time); +unsigned long z_clock_elapsed_s(z_clock_t *time); + +/*------------------ Time ------------------*/ +z_time_t z_time_now(void); +const char *z_time_now_as_str(char *const buf, unsigned long buflen); +unsigned long z_time_elapsed_us(z_time_t *time); +unsigned long z_time_elapsed_ms(z_time_t *time); +unsigned long z_time_elapsed_s(z_time_t *time); + +#ifdef __cplusplus +} +#endif + +#endif /* ZENOH_PICO_SYSTEM_PLATFORM_COMMON_H */ diff --git a/include/zenoh-pico/system/platform.h b/include/zenoh-pico/system/platform.h index 5852928de..4fe70198d 100644 --- a/include/zenoh-pico/system/platform.h +++ b/include/zenoh-pico/system/platform.h @@ -12,95 +12,13 @@ // ZettaScale Zenoh Team, // Błażej Sowa, -#ifndef ZENOH_PICO_SYSTEM_COMMON_H -#define ZENOH_PICO_SYSTEM_COMMON_H +#ifndef ZENOH_PICO_SYSTEM_PLATFORM_H +#define ZENOH_PICO_SYSTEM_PLATFORM_H #include #include "zenoh-pico/config.h" +#include "zenoh-pico/deprecated/platform.h" +#include "zenoh-pico/system/platform-common.h" -#if defined(ZENOH_LINUX) || defined(ZENOH_MACOS) || defined(ZENOH_BSD) -#include "zenoh-pico/system/platform/unix.h" -#elif defined(ZENOH_WINDOWS) -#include "zenoh-pico/system/platform/windows.h" -#elif defined(ZENOH_ESPIDF) -#include "zenoh-pico/system/platform/espidf.h" -#elif defined(ZENOH_MBED) -#include "zenoh-pico/system/platform/mbed.h" -#elif defined(ZENOH_ZEPHYR) -#include "zenoh-pico/system/platform/zephyr.h" -#elif defined(ZENOH_ARDUINO_ESP32) -#include "zenoh-pico/system/platform/arduino/esp32.h" -#elif defined(ZENOH_ARDUINO_OPENCR) -#include "zenoh-pico/system/platform/arduino/opencr.h" -#elif defined(ZENOH_EMSCRIPTEN) -#include "zenoh-pico/system/platform/emscripten.h" -#elif defined(ZENOH_FREERTOS_PLUS_TCP) -#include "zenoh-pico/system/platform/freertos_plus_tcp.h" -#else -#include "zenoh-pico/system/platform/void.h" -#error "Unknown platform" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -/*------------------ Random ------------------*/ -uint8_t zp_random_u8(void); -uint16_t zp_random_u16(void); -uint32_t zp_random_u32(void); -uint64_t zp_random_u64(void); -void zp_random_fill(void *buf, size_t len); - -/*------------------ Memory ------------------*/ -void *zp_malloc(size_t size); -void *zp_realloc(void *ptr, size_t size); -void zp_free(void *ptr); - -#if Z_FEATURE_MULTI_THREAD == 1 -/*------------------ Thread ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg); -int8_t zp_task_join(zp_task_t *task); -int8_t zp_task_cancel(zp_task_t *task); -void zp_task_free(zp_task_t **task); - -/*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m); -int8_t zp_mutex_free(zp_mutex_t *m); - -int8_t zp_mutex_lock(zp_mutex_t *m); -int8_t zp_mutex_trylock(zp_mutex_t *m); -int8_t zp_mutex_unlock(zp_mutex_t *m); - -/*------------------ CondVar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv); -int8_t zp_condvar_free(zp_condvar_t *cv); - -int8_t zp_condvar_signal(zp_condvar_t *cv); -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m); -#endif // Z_FEATURE_MULTI_THREAD == 1 - -/*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time); -int zp_sleep_ms(size_t time); -int zp_sleep_s(size_t time); - -/*------------------ Clock ------------------*/ -zp_clock_t zp_clock_now(void); -unsigned long zp_clock_elapsed_us(zp_clock_t *time); -unsigned long zp_clock_elapsed_ms(zp_clock_t *time); -unsigned long zp_clock_elapsed_s(zp_clock_t *time); - -/*------------------ Time ------------------*/ -zp_time_t zp_time_now(void); -const char *zp_time_now_as_str(char *const buf, unsigned long buflen); -unsigned long zp_time_elapsed_us(zp_time_t *time); -unsigned long zp_time_elapsed_ms(zp_time_t *time); -unsigned long zp_time_elapsed_s(zp_time_t *time); - -#ifdef __cplusplus -} -#endif - -#endif /* ZENOH_PICO_SYSTEM_COMMON_H */ +#endif /* ZENOH_PICO_SYSTEM_PLATFORM_H */ diff --git a/include/zenoh-pico/system/platform/arduino/esp32.h b/include/zenoh-pico/system/platform/arduino/esp32.h index c26c99a88..abdc37f67 100644 --- a/include/zenoh-pico/system/platform/arduino/esp32.h +++ b/include/zenoh-pico/system/platform/arduino/esp32.h @@ -24,14 +24,14 @@ #endif // Z_FEATURE_MULTI_THREAD == 1 #if Z_FEATURE_MULTI_THREAD == 1 -typedef TaskHandle_t zp_task_t; -typedef void *zp_task_attr_t; // Not used in ESP32 -typedef pthread_mutex_t zp_mutex_t; -typedef pthread_cond_t zp_condvar_t; +typedef TaskHandle_t z_task_t; +typedef void *z_task_attr_t; // Not used in ESP32 +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef struct timespec zp_clock_t; -typedef struct timeval zp_time_t; +typedef struct timespec z_clock_t; +typedef struct timeval z_time_t; typedef struct BluetoothSerial BluetoothSerial; // Forward declaration to be used in _z_sys_net_socket_t typedef struct HardwareSerial HardwareSerial; // Forward declaration to be used in _z_sys_net_socket_t diff --git a/include/zenoh-pico/system/platform/arduino/opencr.h b/include/zenoh-pico/system/platform/arduino/opencr.h index ebc054164..af16ce60b 100644 --- a/include/zenoh-pico/system/platform/arduino/opencr.h +++ b/include/zenoh-pico/system/platform/arduino/opencr.h @@ -21,14 +21,14 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef void *zp_task_t; -typedef void *zp_task_attr_t; -typedef void *zp_mutex_t; -typedef void *zp_condvar_t; +typedef void *z_task_t; +typedef void *z_task_attr_t; +typedef void *z_mutex_t; +typedef void *z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef struct timespec zp_clock_t; -typedef struct timeval zp_time_t; +typedef struct timespec z_clock_t; +typedef struct timeval z_time_t; typedef struct IPAddress IPAddress; // Forward declaration to be used in __z_net_iptcp_addr_t typedef struct WiFiClient WiFiClient; // Forward declaration to be used in _z_sys_net_socket_t diff --git a/include/zenoh-pico/system/platform/emscripten.h b/include/zenoh-pico/system/platform/emscripten.h index e74216bf5..b89921197 100644 --- a/include/zenoh-pico/system/platform/emscripten.h +++ b/include/zenoh-pico/system/platform/emscripten.h @@ -22,14 +22,14 @@ #if Z_FEATURE_MULTI_THREAD == 1 #include -typedef pthread_t zp_task_t; -typedef pthread_attr_t zp_task_attr_t; -typedef pthread_mutex_t zp_mutex_t; -typedef pthread_cond_t zp_condvar_t; +typedef pthread_t z_task_t; +typedef pthread_attr_t z_task_attr_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef double zp_clock_t; -typedef double zp_time_t; +typedef double z_clock_t; +typedef double z_time_t; typedef struct { union { diff --git a/include/zenoh-pico/system/platform/espidf.h b/include/zenoh-pico/system/platform/espidf.h index 6210a9abc..b7353b561 100644 --- a/include/zenoh-pico/system/platform/espidf.h +++ b/include/zenoh-pico/system/platform/espidf.h @@ -17,6 +17,7 @@ #include #include +#include #include #include "zenoh-pico/config.h" @@ -24,14 +25,26 @@ #if Z_FEATURE_MULTI_THREAD == 1 #include -typedef TaskHandle_t zp_task_t; -typedef void *zp_task_attr_t; // Not used in ESP32 -typedef pthread_mutex_t zp_mutex_t; -typedef pthread_cond_t zp_condvar_t; +typedef struct { + const char *name; + UBaseType_t priority; + size_t stack_depth; +#if (configSUPPORT_STATIC_ALLOCATION == 1) + _Bool static_allocation; + StackType_t *stack_buffer; + StaticTask_t *task_buffer; +#endif /* SUPPORT_STATIC_ALLOCATION */ +} z_task_attr_t; +typedef struct { + TaskHandle_t handle; + EventGroupHandle_t join_event; +} z_task_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef struct timespec zp_clock_t; -typedef struct timeval zp_time_t; +typedef struct timespec z_clock_t; +typedef struct timeval z_time_t; typedef struct { union { @@ -39,7 +52,11 @@ typedef struct { int _fd; #endif #if Z_FEATURE_LINK_SERIAL == 1 - uart_port_t _serial; + struct { + uart_port_t _serial; + uint8_t *before_cobs; + uint8_t *after_cobs; + }; #endif }; } _z_sys_net_socket_t; diff --git a/include/zenoh-pico/system/platform/flipper.h b/include/zenoh-pico/system/platform/flipper.h new file mode 100644 index 000000000..124714afe --- /dev/null +++ b/include/zenoh-pico/system/platform/flipper.h @@ -0,0 +1,46 @@ +// +// 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, +// + +#ifndef ZENOH_PICO_SYSTEM_FLIPPER_TYPES_H +#define ZENOH_PICO_SYSTEM_FLIPPER_TYPES_H + +#include +#include +#include + +#include "zenoh-pico/config.h" + +#define FLIPPER_DEFAULT_THREAD_STACK_SIZE 2048 +#define FLIPPER_SERIAL_STREAM_BUFFER_SIZE 512 +#define FLIPPER_SERIAL_STREAM_TRIGGERED_LEVEL 10 +#define FLIPPER_SERIAL_TIMEOUT_MS 200 + +#if Z_FEATURE_MULTI_THREAD == 1 +typedef FuriThread* z_task_t; +typedef uint32_t z_task_attr_t; +typedef FuriMutex* z_mutex_t; +typedef void* z_condvar_t; +#endif // Z_FEATURE_MULTI_THREAD == 1 + +typedef struct timespec z_clock_t; +typedef struct timeval z_time_t; + +typedef struct { +#if Z_FEATURE_LINK_SERIAL == 1 + FuriStreamBuffer* _rx_stream; + FuriHalSerialHandle* _serial; +#endif +} _z_sys_net_socket_t; + +#endif /* ZENOH_PICO_SYSTEM_FLIPPER_TYPES_H */ diff --git a/include/zenoh-pico/system/platform/freertos_plus_tcp.h b/include/zenoh-pico/system/platform/freertos_plus_tcp.h index 8af5d8041..f85269dab 100644 --- a/include/zenoh-pico/system/platform/freertos_plus_tcp.h +++ b/include/zenoh-pico/system/platform/freertos_plus_tcp.h @@ -28,19 +28,19 @@ typedef struct { StackType_t *stack_buffer; StaticTask_t *task_buffer; #endif /* SUPPORT_STATIC_ALLOCATION */ -} zp_task_attr_t; +} z_task_attr_t; typedef struct { TaskHandle_t handle; EventGroupHandle_t join_event; -} zp_task_t; +} z_task_t; -typedef SemaphoreHandle_t zp_mutex_t; -typedef void *zp_condvar_t; +typedef SemaphoreHandle_t z_mutex_t; +typedef void *z_condvar_t; #endif // Z_MULTI_THREAD == 1 -typedef TickType_t zp_clock_t; -typedef TickType_t zp_time_t; +typedef TickType_t z_clock_t; +typedef TickType_t z_time_t; typedef struct { union { diff --git a/include/zenoh-pico/system/platform/mbed.h b/include/zenoh-pico/system/platform/mbed.h index 922e95fee..170b230f3 100644 --- a/include/zenoh-pico/system/platform/mbed.h +++ b/include/zenoh-pico/system/platform/mbed.h @@ -23,14 +23,14 @@ typedef int _z_socket_t; #if Z_FEATURE_MULTI_THREAD == 1 -typedef void *zp_task_t; // Workaround as MBED is a C++ library -typedef void *zp_task_attr_t; // Workaround as MBED is a C++ library -typedef void *zp_mutex_t; // Workaround as MBED is a C++ library -typedef void *zp_condvar_t; // Workaround as MBED is a C++ library -#endif // Z_FEATURE_MULTI_THREAD == 1 +typedef void *z_task_t; // Workaround as MBED is a C++ library +typedef void *z_task_attr_t; // Workaround as MBED is a C++ library +typedef void *z_mutex_t; // Workaround as MBED is a C++ library +typedef void *z_condvar_t; // Workaround as MBED is a C++ library +#endif // Z_FEATURE_MULTI_THREAD == 1 -typedef void *zp_clock_t; // Not defined -typedef struct timeval zp_time_t; +typedef void *z_clock_t; // Not defined +typedef struct timeval z_time_t; typedef struct BufferedSerial BufferedSerial; // Forward declaration to be used in _z_sys_net_socket_t typedef struct UDPSocket UDPSocket; // Forward declaration to be used in _z_sys_net_socket_t diff --git a/include/zenoh-pico/system/platform/unix.h b/include/zenoh-pico/system/platform/unix.h index d3344a73f..8fd10421e 100644 --- a/include/zenoh-pico/system/platform/unix.h +++ b/include/zenoh-pico/system/platform/unix.h @@ -25,14 +25,14 @@ #if Z_FEATURE_MULTI_THREAD == 1 #include -typedef pthread_t zp_task_t; -typedef pthread_attr_t zp_task_attr_t; -typedef pthread_mutex_t zp_mutex_t; -typedef pthread_cond_t zp_condvar_t; +typedef pthread_t z_task_t; +typedef pthread_attr_t z_task_attr_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef struct timespec zp_clock_t; -typedef struct timeval zp_time_t; +typedef struct timespec z_clock_t; +typedef struct timeval z_time_t; typedef struct { union { diff --git a/include/zenoh-pico/system/platform/void.h b/include/zenoh-pico/system/platform/void.h index b124bbc8f..b6fc1dd99 100644 --- a/include/zenoh-pico/system/platform/void.h +++ b/include/zenoh-pico/system/platform/void.h @@ -18,13 +18,13 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef void *zp_task_t; -typedef void *zp_task_attr_t; -typedef void *zp_mutex_t; -typedef void *zp_condvar_t; +typedef void *z_task_t; +typedef void *z_task_attr_t; +typedef void *z_mutex_t; +typedef void *z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef void *zp_clock_t; -typedef void *zp_time_t; +typedef void *z_clock_t; +typedef void *z_time_t; #endif /* ZENOH_PICO_SYSTEM_VOID_H */ diff --git a/include/zenoh-pico/system/platform/windows.h b/include/zenoh-pico/system/platform/windows.h index 44e3b5a25..d003be987 100644 --- a/include/zenoh-pico/system/platform/windows.h +++ b/include/zenoh-pico/system/platform/windows.h @@ -21,14 +21,14 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef HANDLE *zp_task_t; -typedef void *zp_task_attr_t; // Not used in Windows -typedef SRWLOCK zp_mutex_t; -typedef CONDITION_VARIABLE zp_condvar_t; +typedef HANDLE *z_task_t; +typedef void *z_task_attr_t; // Not used in Windows +typedef SRWLOCK z_mutex_t; +typedef CONDITION_VARIABLE z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef LARGE_INTEGER zp_clock_t; -typedef struct timeb zp_time_t; +typedef LARGE_INTEGER z_clock_t; +typedef struct timeb z_time_t; typedef struct { union { diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index 830f7f2f4..da7b41997 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -31,14 +31,14 @@ #include "zenoh-pico/config.h" #if Z_FEATURE_MULTI_THREAD == 1 -typedef pthread_t zp_task_t; -typedef pthread_attr_t zp_task_attr_t; -typedef pthread_mutex_t zp_mutex_t; -typedef pthread_cond_t zp_condvar_t; +typedef pthread_t z_task_t; +typedef pthread_attr_t z_task_attr_t; +typedef pthread_mutex_t z_mutex_t; +typedef pthread_cond_t z_condvar_t; #endif // Z_FEATURE_MULTI_THREAD == 1 -typedef struct timespec zp_clock_t; -typedef struct timeval zp_time_t; +typedef struct timespec z_clock_t; +typedef struct timeval z_time_t; typedef struct { union { diff --git a/include/zenoh-pico/transport/multicast/lease.h b/include/zenoh-pico/transport/multicast/lease.h index 7c5ba8296..628eaa088 100644 --- a/include/zenoh-pico/transport/multicast/lease.h +++ b/include/zenoh-pico/transport/multicast/lease.h @@ -23,7 +23,7 @@ int8_t _zp_multicast_stop_lease_task(_z_transport_multicast_t *ztm); void *_zp_multicast_lease_task(void *ztm_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) -int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, zp_task_attr_t *attr, zp_task_t *task); +int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, z_task_attr_t *attr, z_task_t *task); #else int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && (Z_FEATURE_MULTICAST_TRANSPORT == 1 || Z_FEATURE_RAWETH_TRANSPORT == 1) */ diff --git a/include/zenoh-pico/transport/multicast/read.h b/include/zenoh-pico/transport/multicast/read.h index f321a74d6..67b046397 100644 --- a/include/zenoh-pico/transport/multicast/read.h +++ b/include/zenoh-pico/transport/multicast/read.h @@ -22,7 +22,7 @@ int8_t _zp_multicast_stop_read_task(_z_transport_t *zt); void *_zp_multicast_read_task(void *ztm_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_MULTICAST_TRANSPORT == 1 -int8_t _zp_multicast_start_read_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task); +int8_t _zp_multicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); #else int8_t _zp_multicast_start_read_task(_z_transport_t *zt, void *attr, void *task); #endif /* #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_MULTICAST_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/transport/raweth/config.h b/include/zenoh-pico/transport/raweth/config.h deleted file mode 100644 index c8d644747..000000000 --- a/include/zenoh-pico/transport/raweth/config.h +++ /dev/null @@ -1,58 +0,0 @@ -// -// 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, -// - -#ifndef ZENOH_PICO_RAWETH_CONFIG_H -#define ZENOH_PICO_RAWETH_CONFIG_H - -#include - -#include "zenoh-pico/protocol/core.h" -#include "zenoh-pico/system/link/raweth.h" -#include "zenoh-pico/transport/transport.h" -#include "zenoh-pico/utils/result.h" - -#if Z_FEATURE_RAWETH_TRANSPORT == 1 - -typedef struct { - _z_keyexpr_t _keyexpr; - uint16_t _vlan; // vlan tag (pcp + dei + id), big endian - uint8_t _dmac[_ZP_MAC_ADDR_LENGTH]; - _Bool _has_vlan; -} _zp_raweth_cfg_entry; - -typedef struct { - uint8_t _mac[_ZP_MAC_ADDR_LENGTH]; -} _zp_raweth_cfg_whitelist_val; - -// Ethertype to use in frame -extern const uint16_t _ZP_RAWETH_CFG_ETHTYPE; - -// Interface to use -extern const char *_ZP_RAWETH_CFG_INTERFACE; - -// Source mac address -extern const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH]; - -// Main config array -extern const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[]; - -// Mac address rx whitelist array -extern const _zp_raweth_cfg_whitelist_val _ZP_RAWETH_CFG_WHITELIST[]; - -// Array size -extern const size_t _ZP_RAWETH_CFG_SIZE; -extern const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE; - -#endif // Z_FEATURE_RAWETH_TRANSPORT == 1 -#endif // ZENOH_PICO_RAWETH_CONFIG_H diff --git a/include/zenoh-pico/transport/raweth/read.h b/include/zenoh-pico/transport/raweth/read.h index bc8325465..3df0d2979 100644 --- a/include/zenoh-pico/transport/raweth/read.h +++ b/include/zenoh-pico/transport/raweth/read.h @@ -22,7 +22,7 @@ int8_t _zp_raweth_stop_read_task(_z_transport_t *zt); void *_zp_raweth_read_task(void *ztm_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_RAWETH_TRANSPORT == 1 -int8_t _zp_raweth_start_read_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task); +int8_t _zp_raweth_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); #else int8_t _zp_raweth_start_read_task(_z_transport_t *zt, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_RAWETH_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/transport/transport.h b/include/zenoh-pico/transport/transport.h index 313f77268..76921bd9c 100644 --- a/include/zenoh-pico/transport/transport.h +++ b/include/zenoh-pico/transport/transport.h @@ -69,8 +69,8 @@ typedef struct { #if Z_FEATURE_MULTI_THREAD == 1 // TX and RX mutexes - zp_mutex_t _mutex_rx; - zp_mutex_t _mutex_tx; + z_mutex_t _mutex_rx; + z_mutex_t _mutex_tx; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_link_t _link; @@ -96,8 +96,8 @@ typedef struct { volatile _z_zint_t _lease; #if Z_FEATURE_MULTI_THREAD == 1 - zp_task_t *_read_task; - zp_task_t *_lease_task; + z_task_t *_read_task; + z_task_t *_lease_task; volatile _Bool _read_task_running; volatile _Bool _lease_task_running; #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -112,11 +112,11 @@ typedef struct _z_transport_multicast_t { #if Z_FEATURE_MULTI_THREAD == 1 // TX and RX mutexes - zp_mutex_t _mutex_rx; - zp_mutex_t _mutex_tx; + z_mutex_t _mutex_rx; + z_mutex_t _mutex_tx; // Peer list mutex - zp_mutex_t _mutex_peer; + z_mutex_t _mutex_peer; #endif // Z_FEATURE_MULTI_THREAD == 1 _z_link_t _link; @@ -138,8 +138,8 @@ typedef struct _z_transport_multicast_t { _zp_f_send_tmsg _send_f; #if Z_FEATURE_MULTI_THREAD == 1 - zp_task_t *_read_task; - zp_task_t *_lease_task; + z_task_t *_read_task; + z_task_t *_lease_task; volatile _Bool _read_task_running; volatile _Bool _lease_task_running; #endif // Z_FEATURE_MULTI_THREAD == 1 diff --git a/include/zenoh-pico/transport/unicast/lease.h b/include/zenoh-pico/transport/unicast/lease.h index 6cdfa195d..8ccad0e7b 100644 --- a/include/zenoh-pico/transport/unicast/lease.h +++ b/include/zenoh-pico/transport/unicast/lease.h @@ -22,7 +22,7 @@ int8_t _zp_unicast_stop_lease_task(_z_transport_t *zt); void *_zp_unicast_lease_task(void *ztu_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 -int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task); +int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); #else int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/transport/unicast/read.h b/include/zenoh-pico/transport/unicast/read.h index c54069427..c3c17bc05 100644 --- a/include/zenoh-pico/transport/unicast/read.h +++ b/include/zenoh-pico/transport/unicast/read.h @@ -22,7 +22,7 @@ int8_t _zp_unicast_stop_read_task(_z_transport_t *zt); void *_zp_unicast_read_task(void *ztu_arg); // The argument is void* to avoid incompatible pointer types in tasks #if Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 -int8_t _zp_unicast_start_read_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task); +int8_t _zp_unicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task); #else int8_t _zp_unicast_start_read_task(_z_transport_t *zt, void *attr, void *task); #endif /* Z_FEATURE_MULTI_THREAD == 1 && Z_FEATURE_UNICAST_TRANSPORT == 1 */ diff --git a/include/zenoh-pico/utils/logging.h b/include/zenoh-pico/utils/logging.h index 5b8f73136..f6e3304ef 100644 --- a/include/zenoh-pico/utils/logging.h +++ b/include/zenoh-pico/utils/logging.h @@ -25,7 +25,7 @@ // Timestamp function static inline void __z_print_timestamp(void) { char ret[64]; - printf("[%s ", zp_time_now_as_str(ret, sizeof(ret))); + printf("[%s ", z_time_now_as_str(ret, sizeof(ret))); } // Logging macros @@ -47,7 +47,7 @@ static inline void __z_print_timestamp(void) { if (ZENOH_DEBUG >= _Z_LOG_LVL_DEBUG) { \ _Z_LOG_PREFIX(DEBUG); \ printf(__VA_ARGS__); \ - printf("\n"); \ + printf("\r\n"); \ } \ } while (false) @@ -56,7 +56,7 @@ static inline void __z_print_timestamp(void) { if (ZENOH_DEBUG >= _Z_LOG_LVL_INFO) { \ _Z_LOG_PREFIX(INFO); \ printf(__VA_ARGS__); \ - printf("\n"); \ + printf("\r\n"); \ } \ } while (false) @@ -65,7 +65,7 @@ static inline void __z_print_timestamp(void) { if (ZENOH_DEBUG >= _Z_LOG_LVL_ERROR) { \ _Z_LOG_PREFIX(ERROR); \ printf(__VA_ARGS__); \ - printf("\n"); \ + printf("\r\n"); \ } \ } while (false) #endif // ZENOH_DEBUG == 0 && !defined(Z_BUILD_DEBUG) diff --git a/src/api/api.c b/src/api/api.c index 1369c82b1..e91f03937 100644 --- a/src/api/api.c +++ b/src/api/api.c @@ -60,7 +60,7 @@ z_owned_str_t z_keyexpr_to_string(z_keyexpr_t keyexpr) { if (keyexpr._id == Z_RESOURCE_ID_NONE) { size_t ke_len = _z_str_size(keyexpr._suffix); - ret._value = (char *)zp_malloc(ke_len); + ret._value = (char *)z_malloc(ke_len); if (ret._value != NULL) { _z_str_n_copy(ret._value, keyexpr._suffix, ke_len); } @@ -349,7 +349,7 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) ownedtype *z_##name##_move(ownedtype *val) { return val; } \ ownedtype z_##name##_clone(ownedtype *val) { \ ownedtype ret; \ - ret._value = (type *)zp_malloc(sizeof(type)); \ + ret._value = (type *)z_malloc(sizeof(type)); \ if (ret._value != NULL) { \ f_copy(ret._value, val->_value); \ } \ @@ -370,7 +370,7 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) #define OWNED_FUNCTIONS_PTR_CLONE(type, ownedtype, name, f_copy) \ ownedtype z_##name##_clone(ownedtype *val) { \ ownedtype ret; \ - ret._value = (_##type *)zp_malloc(sizeof(_##type)); \ + ret._value = (_##type *)z_malloc(sizeof(_##type)); \ if (ret._value != NULL) { \ f_copy(ret._value, val->_value); \ } \ @@ -392,7 +392,7 @@ void z_closure_zid_call(const z_owned_closure_zid_t *closure, const z_id_t *id) ownedtype z_##name##_clone(ownedtype *val) { \ ownedtype ret; \ size_t size = _z_str_size(val->_value); \ - ret._value = (_##type)zp_malloc(size); \ + ret._value = (_##type)z_malloc(size); \ if (ret._value != NULL) { \ f_copy(ret._value, val->_value, size); \ } \ @@ -496,26 +496,26 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb // TODO[API-NET]: When API and NET are a single layer, there is no wrap the user callback and args // to enclose the z_reply_t into a z_owned_reply_t. __z_hello_handler_wrapper_t *wrapped_ctx = - (__z_hello_handler_wrapper_t *)zp_malloc(sizeof(__z_hello_handler_wrapper_t)); + (__z_hello_handler_wrapper_t *)z_malloc(sizeof(__z_hello_handler_wrapper_t)); if (wrapped_ctx != NULL) { wrapped_ctx->user_call = callback->call; wrapped_ctx->ctx = ctx; char *opt_as_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { - opt_as_str = Z_CONFIG_SCOUTING_WHAT_DEFAULT; + opt_as_str = (char *)Z_CONFIG_SCOUTING_WHAT_DEFAULT; } z_what_t what = strtol(opt_as_str, NULL, 10); opt_as_str = _z_config_get(config->_value, Z_CONFIG_MULTICAST_LOCATOR_KEY); if (opt_as_str == NULL) { - opt_as_str = Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; + opt_as_str = (char *)Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; } char *mcast_locator = opt_as_str; opt_as_str = _z_config_get(config->_value, Z_CONFIG_SCOUTING_TIMEOUT_KEY); if (opt_as_str == NULL) { - opt_as_str = Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; + opt_as_str = (char *)Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; } uint32_t timeout = strtoul(opt_as_str, NULL, 10); @@ -527,7 +527,7 @@ int8_t z_scout(z_owned_scouting_config_t *config, z_owned_closure_hello_t *callb _z_scout(what, zid, mcast_locator, timeout, __z_hello_handler, wrapped_ctx, callback->drop, ctx); - zp_free(wrapped_ctx); + z_free(wrapped_ctx); z_scouting_config_drop(config); } else { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -649,9 +649,10 @@ int8_t z_put(z_session_t zs, z_keyexpr_t keyexpr, const uint8_t *payload, z_zint ); // Trigger local subscriptions - _z_trigger_local_subscriptions(&zs._val.in->val, keyexpr, payload, payload_len + _z_trigger_local_subscriptions(&zs._val.in->val, keyexpr, payload, payload_len, + _z_n_qos_make(0, opt.congestion_control == Z_CONGESTION_CONTROL_BLOCK, opt.priority) #if Z_FEATURE_ATTACHMENT == 1 - , + , opt.attachment #endif ); @@ -764,7 +765,7 @@ int8_t z_publisher_put(const z_publisher_t pub, const uint8_t *payload, size_t l ); } // Trigger local subscriptions - _z_trigger_local_subscriptions(&pub._val->_zn.in->val, pub._val->_key, payload, len + _z_trigger_local_subscriptions(&pub._val->_zn.in->val, pub._val->_key, payload, len, _Z_N_QOS_DEFAULT #if Z_FEATURE_ATTACHMENT == 1 , opt.attachment @@ -785,7 +786,7 @@ int8_t z_publisher_delete(const z_publisher_t pub, const z_publisher_delete_opti } z_owned_keyexpr_t z_publisher_keyexpr(z_publisher_t publisher) { - z_owned_keyexpr_t ret = {._value = zp_malloc(sizeof(_z_keyexpr_t))}; + z_owned_keyexpr_t ret = {._value = z_malloc(sizeof(_z_keyexpr_t))}; if (ret._value != NULL && publisher._val != NULL) { *ret._value = _z_keyexpr_duplicate(publisher._val->_key); } @@ -803,6 +804,7 @@ z_get_options_t z_get_options_default(void) { #if Z_FEATURE_ATTACHMENT == 1 // TODO:ATT.attachment = z_attachment_null() #endif + .timeout_ms = Z_GET_TIMEOUT_DEFAULT }; } @@ -844,14 +846,14 @@ int8_t z_get(z_session_t zs, z_keyexpr_t keyexpr, const char *parameters, z_owne // TODO[API-NET]: When API and NET are a single layer, there is no wrap the user callback and args // to enclose the z_reply_t into a z_owned_reply_t. __z_reply_handler_wrapper_t *wrapped_ctx = - (__z_reply_handler_wrapper_t *)zp_malloc(sizeof(__z_reply_handler_wrapper_t)); + (__z_reply_handler_wrapper_t *)z_malloc(sizeof(__z_reply_handler_wrapper_t)); if (wrapped_ctx != NULL) { wrapped_ctx->user_call = callback->call; wrapped_ctx->ctx = ctx; } ret = _z_query(&zs._val.in->val, keyexpr, parameters, opt.target, opt.consolidation.mode, opt.value, - __z_reply_handler, wrapped_ctx, callback->drop, ctx + __z_reply_handler, wrapped_ctx, callback->drop, ctx, opt.timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() @@ -942,7 +944,7 @@ int8_t z_query_reply(const z_query_t *query, const z_keyexpr_t keyexpr, const ui z_owned_keyexpr_t z_keyexpr_new(const char *name) { z_owned_keyexpr_t key; - key._value = name ? (z_keyexpr_t *)zp_malloc(sizeof(z_keyexpr_t)) : NULL; + key._value = name ? (z_keyexpr_t *)z_malloc(sizeof(z_keyexpr_t)) : NULL; if (key._value != NULL) { *key._value = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, name); } @@ -953,7 +955,7 @@ z_owned_keyexpr_t z_keyexpr_new(const char *name) { z_owned_keyexpr_t z_declare_keyexpr(z_session_t zs, z_keyexpr_t keyexpr) { z_owned_keyexpr_t key; - key._value = (z_keyexpr_t *)zp_malloc(sizeof(z_keyexpr_t)); + key._value = (z_keyexpr_t *)z_malloc(sizeof(z_keyexpr_t)); if (key._value != NULL) { uint16_t id = _z_declare_resource(&zs._val.in->val, keyexpr); *key._value = _z_rid_with_suffix(id, NULL); @@ -998,7 +1000,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z if (wild != NULL && wild != keyexpr._suffix) { wild -= 1; size_t len = wild - keyexpr._suffix; - suffix = zp_malloc(len + 1); + suffix = z_malloc(len + 1); if (suffix != NULL) { memcpy(suffix, keyexpr._suffix, len); suffix[len] = 0; @@ -1021,7 +1023,7 @@ z_owned_subscriber_t z_declare_subscriber(z_session_t zs, z_keyexpr_t keyexpr, z } _z_subscriber_t *sub = _z_declare_subscriber(&zs._val, key, subinfo, callback->call, callback->drop, ctx); if (suffix != NULL) { - zp_free(suffix); + z_free(suffix); } return (z_owned_subscriber_t){._value = sub}; @@ -1045,7 +1047,7 @@ z_owned_keyexpr_t z_subscriber_keyexpr(z_subscriber_t sub) { _z_subscription_rc_t *head = _z_subscription_rc_list_head(tail); if (head->in->val._id == lookup) { _z_keyexpr_t key = _z_keyexpr_duplicate(head->in->val._key); - ret = (z_owned_keyexpr_t){._value = zp_malloc(sizeof(_z_keyexpr_t))}; + ret = (z_owned_keyexpr_t){._value = z_malloc(sizeof(_z_keyexpr_t))}; if (ret._value != NULL) { *ret._value = key; } else { @@ -1211,7 +1213,7 @@ void z_bytes_map_insert_by_alias(const z_owned_bytes_map_t *this_, z_bytes_t key _z_bytes_clear(&head->value); head->value = _z_bytes_wrap(value.start, value.len); } else { - struct _z_bytes_pair_t *insert = zp_malloc(sizeof(struct _z_bytes_pair_t)); + struct _z_bytes_pair_t *insert = z_malloc(sizeof(struct _z_bytes_pair_t)); memset(insert, 0, sizeof(struct _z_bytes_pair_t)); insert->key = _z_bytes_wrap(key.start, key.len); insert->value = _z_bytes_wrap(value.start, value.len); @@ -1235,7 +1237,7 @@ void z_bytes_map_insert_by_copy(const z_owned_bytes_map_t *this_, z_bytes_t key, _z_bytes_copy(&head->key, &key); } } else { - struct _z_bytes_pair_t *insert = zp_malloc(sizeof(struct _z_bytes_pair_t)); + struct _z_bytes_pair_t *insert = z_malloc(sizeof(struct _z_bytes_pair_t)); memset(insert, 0, sizeof(struct _z_bytes_pair_t)); _z_bytes_copy(&insert->key, &key); _z_bytes_copy(&insert->value, &value); @@ -1258,4 +1260,4 @@ z_owned_bytes_map_t z_bytes_map_new(void) { return (z_owned_bytes_map_t){._inner z_owned_bytes_map_t z_bytes_map_null(void) { return (z_owned_bytes_map_t){._inner = NULL}; } z_bytes_t z_bytes_from_str(const char *str) { return z_bytes_wrap((const uint8_t *)str, strlen(str)); } z_bytes_t z_bytes_null(void) { return (z_bytes_t){.len = 0, ._is_alloc = false, .start = NULL}; } -#endif \ No newline at end of file +#endif diff --git a/src/collections/bytes.c b/src/collections/bytes.c index 1fa4f71d0..40e67bd36 100644 --- a/src/collections/bytes.c +++ b/src/collections/bytes.c @@ -26,7 +26,7 @@ _z_bytes_t _z_bytes_empty(void) { return (_z_bytes_t){.start = NULL, .len = 0, . int8_t _z_bytes_init(_z_bytes_t *bs, size_t capacity) { int8_t ret = _Z_RES_OK; - bs->start = (uint8_t *)zp_malloc(capacity); + bs->start = (uint8_t *)z_malloc(capacity); if (bs->start != NULL) { bs->len = capacity; bs->_is_alloc = true; @@ -61,7 +61,7 @@ void _z_bytes_reset(_z_bytes_t *bs) { void _z_bytes_clear(_z_bytes_t *bs) { if ((bs->_is_alloc == true) && (bs->start != NULL)) { - zp_free((uint8_t *)bs->start); + z_free((uint8_t *)bs->start); } _z_bytes_reset(bs); } @@ -72,7 +72,7 @@ void _z_bytes_free(_z_bytes_t **bs) { if (ptr != NULL) { _z_bytes_clear(ptr); - zp_free(ptr); + z_free(ptr); *bs = NULL; } } diff --git a/src/collections/intmap.c b/src/collections/intmap.c index 203052b9e..5abeab4f7 100644 --- a/src/collections/intmap.c +++ b/src/collections/intmap.c @@ -66,7 +66,7 @@ void *_z_int_void_map_insert(_z_int_void_map_t *map, size_t k, void *v, z_elemen if (map->_vals == NULL) { // Lazily allocate and initialize to NULL all the pointers size_t len = map->_capacity * sizeof(_z_list_t *); - map->_vals = (_z_list_t **)zp_malloc(len); + map->_vals = (_z_list_t **)z_malloc(len); if (map->_vals != NULL) { (void)memset(map->_vals, 0, len); } @@ -77,7 +77,7 @@ void *_z_int_void_map_insert(_z_int_void_map_t *map, size_t k, void *v, z_elemen _z_int_void_map_remove(map, k, f_f); // Insert the element - _z_int_void_map_entry_t *entry = (_z_int_void_map_entry_t *)zp_malloc(sizeof(_z_int_void_map_entry_t)); + _z_int_void_map_entry_t *entry = (_z_int_void_map_entry_t *)z_malloc(sizeof(_z_int_void_map_entry_t)); if (entry != NULL) { entry->_key = k; entry->_val = v; @@ -116,7 +116,7 @@ void _z_int_void_map_clear(_z_int_void_map_t *map, z_element_free_f f_f) { _z_list_free(&map->_vals[idx], f_f); } - zp_free(map->_vals); + z_free(map->_vals); map->_vals = NULL; } } @@ -126,7 +126,7 @@ void _z_int_void_map_free(_z_int_void_map_t **map, z_element_free_f f) { if (ptr != NULL) { _z_int_void_map_clear(ptr, f); - zp_free(ptr); + z_free(ptr); *map = NULL; } } diff --git a/src/collections/list.c b/src/collections/list.c index b47340d8b..36dbadd51 100644 --- a/src/collections/list.c +++ b/src/collections/list.c @@ -18,7 +18,7 @@ /*-------- Inner single-linked list --------*/ _z_list_t *_z_list_of(void *x) { - _z_list_t *xs = (_z_list_t *)zp_malloc(sizeof(_z_list_t)); + _z_list_t *xs = (_z_list_t *)z_malloc(sizeof(_z_list_t)); if (xs != NULL) { xs->_val = x; xs->_tail = NULL; @@ -32,6 +32,20 @@ _z_list_t *_z_list_push(_z_list_t *xs, void *x) { return lst; } +_z_list_t *_z_list_push_back(_z_list_t *xs, void *x) { + _z_list_t *l = (_z_list_t *)xs; + while (l != NULL && l->_tail != NULL) { + l = l->_tail; + } + if (l == NULL) { + l = _z_list_of(x); + return l; + } else { + l->_tail = _z_list_of(x); + return xs; + } +} + void *_z_list_head(const _z_list_t *xs) { return xs->_val; } _z_list_t *_z_list_tail(const _z_list_t *xs) { return xs->_tail; } @@ -57,7 +71,7 @@ _z_list_t *_z_list_pop(_z_list_t *xs, z_element_free_f f_f, void **x) { } else { f_f(&head->_val); } - zp_free(head); + z_free(head); return l; } @@ -99,7 +113,7 @@ _z_list_t *_z_list_drop_filter(_z_list_t *xs, z_element_free_f f_f, z_element_eq } f_f(&this_->_val); - zp_free(this_); + z_free(this_); break; } else { previous = current; diff --git a/src/collections/string.c b/src/collections/string.c index 98d7dda3d..51a19c38b 100644 --- a/src/collections/string.c +++ b/src/collections/string.c @@ -53,7 +53,7 @@ void _z_string_reset(_z_string_t *str) { } void _z_string_clear(_z_string_t *str) { - zp_free(str->val); + z_free(str->val); _z_string_reset(str); } @@ -62,7 +62,7 @@ void _z_string_free(_z_string_t **str) { if (ptr != NULL) { _z_string_clear(ptr); - zp_free(ptr); + z_free(ptr); *str = NULL; } } @@ -70,7 +70,7 @@ void _z_string_free(_z_string_t **str) { _z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { _z_string_t s; size_t len = bs->len * (size_t)2; - char *s_val = (char *)zp_malloc((len + (size_t)1) * sizeof(char)); + char *s_val = (char *)z_malloc((len + (size_t)1) * sizeof(char)); if (s_val != NULL) { const char c[] = "0123456789ABCDEF"; @@ -92,7 +92,7 @@ _z_string_t _z_string_from_bytes(const _z_bytes_t *bs) { /*-------- str --------*/ size_t _z_str_size(const char *src) { return strlen(src) + (size_t)1; } -void _z_str_clear(char *src) { zp_free(src); } +void _z_str_clear(char *src) { z_free(src); } void _z_str_free(char **src) { char *ptr = *src; @@ -116,7 +116,7 @@ void _z_str_n_copy(char *dst, const char *src, size_t size) { char *_z_str_clone(const char *src) { size_t len = _z_str_size(src); - char *dst = (char *)zp_malloc(len); + char *dst = (char *)z_malloc(len); if (dst != NULL) { _z_str_n_copy(dst, src, len); } @@ -129,7 +129,7 @@ _Bool _z_str_eq(const char *left, const char *right) { return strcmp(left, right /*-------- str_array --------*/ void _z_str_array_init(_z_str_array_t *sa, size_t len) { char **val = (char **)&sa->val; - *val = (char *)zp_malloc(len * sizeof(char *)); + *val = (char *)z_malloc(len * sizeof(char *)); if (*val != NULL) { sa->len = len; } @@ -151,9 +151,9 @@ _Bool _z_str_array_is_empty(const _z_str_array_t *sa) { return sa->len == 0; } void _z_str_array_clear(_z_str_array_t *sa) { for (size_t i = 0; i < sa->len; i++) { - zp_free(sa->val[i]); + z_free(sa->val[i]); } - zp_free(sa->val); + z_free(sa->val); } void _z_str_array_free(_z_str_array_t **sa) { @@ -161,7 +161,7 @@ void _z_str_array_free(_z_str_array_t **sa) { if (ptr != NULL) { _z_str_array_clear(ptr); - zp_free(ptr); + z_free(ptr); *sa = NULL; } } diff --git a/src/collections/vec.c b/src/collections/vec.c index fabd490fb..20afff6ca 100644 --- a/src/collections/vec.c +++ b/src/collections/vec.c @@ -22,7 +22,7 @@ _z_vec_t _z_vec_make(size_t capacity) { _z_vec_t v = {._capacity = capacity, ._len = 0, ._val = NULL}; if (capacity != 0) { - v._val = (void **)zp_malloc(sizeof(void *) * capacity); + v._val = (void **)z_malloc(sizeof(void *) * capacity); } if (v._val != NULL) { v._capacity = capacity; @@ -33,7 +33,7 @@ _z_vec_t _z_vec_make(size_t capacity) { void _z_vec_copy(_z_vec_t *dst, const _z_vec_t *src, z_element_clone_f d_f) { dst->_capacity = src->_capacity; dst->_len = src->_len; - dst->_val = (void **)zp_malloc(sizeof(void *) * src->_capacity); + dst->_val = (void **)z_malloc(sizeof(void *) * src->_capacity); if (dst->_val != NULL) { for (size_t i = 0; i < src->_len; i++) { _z_vec_append(dst, d_f(src->_val[i])); @@ -54,7 +54,7 @@ void _z_vec_clear(_z_vec_t *v, z_element_free_f free_f) { free_f(&v->_val[i]); } - zp_free(v->_val); + z_free(v->_val); v->_val = NULL; v->_capacity = 0; @@ -67,7 +67,7 @@ void _z_vec_free(_z_vec_t **v, z_element_free_f free_f) { if (ptr != NULL) { _z_vec_clear(ptr, free_f); - zp_free(ptr); + z_free(ptr); *v = NULL; } } @@ -80,12 +80,12 @@ void _z_vec_append(_z_vec_t *v, void *e) { if (v->_len == v->_capacity) { // Allocate a new vector size_t _capacity = (v->_capacity << 1) | 0x01; - void **_val = (void **)zp_malloc(_capacity * sizeof(void *)); + void **_val = (void **)z_malloc(_capacity * sizeof(void *)); if (_val != NULL) { (void)memcpy(_val, v->_val, v->_capacity * sizeof(void *)); // Free the old vector - zp_free(v->_val); + z_free(v->_val); // Update the current vector v->_val = _val; diff --git a/src/deprecated/platform.c b/src/deprecated/platform.c new file mode 100644 index 000000000..4081d5b1e --- /dev/null +++ b/src/deprecated/platform.c @@ -0,0 +1,63 @@ +// +// 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 "zenoh-pico/deprecated/platform.h" + +#include "zenoh-pico/system/platform-common.h" + +uint8_t zp_random_u8(void) { return z_random_u8(); }; +uint16_t zp_random_u16(void) { return z_random_u16(); }; +uint32_t zp_random_u32(void) { return z_random_u32(); }; +uint64_t zp_random_u64(void) { return z_random_u64(); }; +void zp_random_fill(void *buf, size_t len) { return z_random_fill(buf, len); }; + +void *zp_malloc(size_t size) { return z_malloc(size); }; +void *zp_realloc(void *ptr, size_t size) { return z_realloc(ptr, size); }; +void zp_free(void *ptr) { return z_free(ptr); }; + +#if Z_FEATURE_MULTI_THREAD == 1 + +int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { + return z_task_init(task, attr, fun, arg); +}; +int8_t zp_task_join(zp_task_t *task) { return z_task_join(task); }; +void zp_task_free(zp_task_t **task) { return z_task_free(task); }; + +int8_t zp_mutex_init(zp_mutex_t *m) { return z_mutex_init(m); }; +int8_t zp_mutex_free(zp_mutex_t *m) { return z_mutex_free(m); }; +int8_t zp_mutex_lock(zp_mutex_t *m) { return z_mutex_lock(m); }; +int8_t zp_mutex_trylock(zp_mutex_t *m) { return z_mutex_trylock(m); }; +int8_t zp_mutex_unlock(zp_mutex_t *m) { return z_mutex_unlock(m); }; + +int8_t zp_condvar_init(zp_condvar_t *cv) { return z_condvar_init(cv); }; +int8_t zp_condvar_free(zp_condvar_t *cv) { return z_condvar_free(cv); }; + +int8_t zp_condvar_signal(zp_condvar_t *cv) { return z_condvar_signal(cv); }; +int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return z_condvar_wait(cv, m); }; +#endif // Z_FEATURE_MULTI_THREAD == 1 + +int zp_sleep_us(size_t time) { return z_sleep_us(time); }; +int zp_sleep_ms(size_t time) { return z_sleep_ms(time); }; +int zp_sleep_s(size_t time) { return z_sleep_s(time); }; + +zp_clock_t zp_clock_now(void) { return z_clock_now(); }; +unsigned long zp_clock_elapsed_us(zp_clock_t *time) { return z_clock_elapsed_us(time); }; +unsigned long zp_clock_elapsed_ms(zp_clock_t *time) { return z_clock_elapsed_ms(time); }; +unsigned long zp_clock_elapsed_s(zp_clock_t *time) { return z_clock_elapsed_s(time); }; + +zp_time_t zp_time_now(void) { return z_time_now(); }; +const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { return z_time_now_as_str(buf, buflen); }; +unsigned long zp_time_elapsed_us(zp_time_t *time) { return z_time_elapsed_us(time); }; +unsigned long zp_time_elapsed_ms(zp_time_t *time) { return z_time_elapsed_ms(time); }; +unsigned long zp_time_elapsed_s(zp_time_t *time) { return z_time_elapsed_s(time); }; diff --git a/src/link/endpoint.c b/src/link/endpoint.c index dc44a0b30..035e646a6 100644 --- a/src/link/endpoint.c +++ b/src/link/endpoint.c @@ -56,7 +56,7 @@ void _z_locator_free(_z_locator_t **lc) { if (ptr != NULL) { _z_locator_clear(ptr); - zp_free(ptr); + z_free(ptr); *lc = NULL; } } @@ -91,7 +91,7 @@ char *_z_locator_protocol_from_str(const char *str) { const char *p_end = strchr(p_start, LOCATOR_PROTOCOL_SEPARATOR); if ((p_end != NULL) && (p_start != p_end)) { size_t p_len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(p_len); + ret = (char *)z_malloc(p_len); if (ret != NULL) { _z_str_n_copy(ret, p_start, p_len); } @@ -118,7 +118,7 @@ char *_z_locator_address_from_str(const char *str) { if (p_start != p_end) { size_t a_len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(a_len); + ret = (char *)z_malloc(a_len); if (ret != NULL) { _z_str_n_copy(ret, p_start, a_len); } @@ -256,7 +256,7 @@ void __z_locator_onto_str(char *dst, size_t dst_len, const _z_locator_t *loc) { */ char *_z_locator_to_str(const _z_locator_t *l) { size_t len = _z_locator_strlen(l) + (size_t)1; - char *dst = (char *)zp_malloc(len); + char *dst = (char *)z_malloc(len); if (dst != NULL) { __z_locator_onto_str(dst, len, l); } @@ -281,7 +281,7 @@ void _z_endpoint_free(_z_endpoint_t **ep) { _z_locator_clear(&ptr->_locator); _z_str_intmap_clear(&ptr->_config); - zp_free(ptr); + z_free(ptr); *ep = NULL; } } @@ -430,7 +430,7 @@ char *_z_endpoint_to_str(const _z_endpoint_t *endpoint) { } // Reconstruct the endpoint as a string - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { ret[0] = '\0'; len = len - (size_t)1; diff --git a/src/link/link.c b/src/link/link.c index 674da4bf1..91d484594 100644 --- a/src/link/link.c +++ b/src/link/link.c @@ -130,7 +130,7 @@ void _z_link_free(_z_link_t **l) { if (ptr != NULL) { _z_link_clear(ptr); - zp_free(ptr); + z_free(ptr); *l = NULL; } } diff --git a/src/link/multicast/udp.c b/src/link/multicast/udp.c index 4961948c2..69d359ba1 100644 --- a/src/link/multicast/udp.c +++ b/src/link/multicast/udp.c @@ -35,7 +35,7 @@ char *__z_parse_port_segment_udp_multicast(const char *address) { const char *p_end = &address[strlen(address)]; size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -55,7 +55,7 @@ char *__z_parse_address_segment_udp_multicast(const char *address) { p_start = _z_cptr_char_offset(p_start, 1); p_end = _z_cptr_char_offset(p_end, -1); size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -63,7 +63,7 @@ char *__z_parse_address_segment_udp_multicast(const char *address) { // IPv4 else { size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -84,7 +84,7 @@ int8_t _z_endpoint_udp_multicast_valid(_z_endpoint_t *endpoint) { if (s_address == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - zp_free(s_address); + z_free(s_address); } } @@ -97,7 +97,7 @@ int8_t _z_endpoint_udp_multicast_valid(_z_endpoint_t *endpoint) { if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - zp_free(s_port); + z_free(s_port); } } @@ -183,8 +183,8 @@ int8_t _z_new_link_udp_multicast(_z_link_t *zl, _z_endpoint_t endpoint) { char *s_port = __z_parse_port_segment_udp_multicast(endpoint._locator._address); ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_address, s_port); memset(&zl->_socket._udp._lep, 0, sizeof(zl->_socket._udp._lep)); - zp_free(s_address); - zp_free(s_port); + z_free(s_address); + z_free(s_port); zl->_open_f = _z_f_link_open_udp_multicast; zl->_listen_f = _z_f_link_listen_udp_multicast; diff --git a/src/link/unicast/tcp.c b/src/link/unicast/tcp.c index dfec4dd6a..a6cf3de7a 100644 --- a/src/link/unicast/tcp.c +++ b/src/link/unicast/tcp.c @@ -35,7 +35,7 @@ char *__z_parse_port_segment_tcp(char *address) { const char *p_end = &address[strlen(address)]; size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -55,7 +55,7 @@ char *__z_parse_address_segment_tcp(char *address) { p_start = _z_cptr_char_offset(p_start, 1); p_end = _z_cptr_char_offset(p_end, -1); size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -63,7 +63,7 @@ char *__z_parse_address_segment_tcp(char *address) { // IPv4 else { size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -84,7 +84,7 @@ int8_t _z_endpoint_tcp_valid(_z_endpoint_t *endpoint) { if (s_address == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - zp_free(s_address); + z_free(s_address); } } @@ -97,7 +97,7 @@ int8_t _z_endpoint_tcp_valid(_z_endpoint_t *endpoint) { if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - zp_free(s_port); + z_free(s_port); } } @@ -166,8 +166,8 @@ int8_t _z_new_link_tcp(_z_link_t *zl, _z_endpoint_t *endpoint) { char *s_address = __z_parse_address_segment_tcp(endpoint->_locator._address); char *s_port = __z_parse_port_segment_tcp(endpoint->_locator._address); ret = _z_create_endpoint_tcp(&zl->_socket._tcp._rep, s_address, s_port); - zp_free(s_address); - zp_free(s_port); + z_free(s_address); + z_free(s_port); zl->_open_f = _z_f_link_open_tcp; zl->_listen_f = _z_f_link_listen_tcp; diff --git a/src/link/unicast/udp.c b/src/link/unicast/udp.c index c10c4c0f3..cd93cdff7 100644 --- a/src/link/unicast/udp.c +++ b/src/link/unicast/udp.c @@ -35,7 +35,7 @@ char *__z_parse_port_segment_udp_unicast(char *address) { const char *p_end = &address[strlen(address)]; size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -55,7 +55,7 @@ char *__z_parse_address_segment_udp_unicast(char *address) { p_start = _z_cptr_char_offset(p_start, 1); p_end = _z_cptr_char_offset(p_end, -1); size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -63,7 +63,7 @@ char *__z_parse_address_segment_udp_unicast(char *address) { // IPv4 else { size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -84,7 +84,7 @@ int8_t _z_endpoint_udp_unicast_valid(_z_endpoint_t *endpoint) { if (s_address == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - zp_free(s_address); + z_free(s_address); } } @@ -97,7 +97,7 @@ int8_t _z_endpoint_udp_unicast_valid(_z_endpoint_t *endpoint) { if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - zp_free(s_port); + z_free(s_port); } } @@ -172,8 +172,8 @@ int8_t _z_new_link_udp_unicast(_z_link_t *zl, _z_endpoint_t endpoint) { char *s_address = __z_parse_address_segment_udp_unicast(endpoint._locator._address); char *s_port = __z_parse_port_segment_udp_unicast(endpoint._locator._address); ret = _z_create_endpoint_udp(&zl->_socket._udp._rep, s_address, s_port); - zp_free(s_address); - zp_free(s_port); + z_free(s_address); + z_free(s_port); zl->_open_f = _z_f_link_open_udp_unicast; zl->_listen_f = _z_f_link_listen_udp_unicast; diff --git a/src/link/unicast/ws.c b/src/link/unicast/ws.c index f67d1aeda..f59fde872 100644 --- a/src/link/unicast/ws.c +++ b/src/link/unicast/ws.c @@ -36,7 +36,7 @@ char *__z_parse_port_segment_ws(char *address) { const char *p_end = &address[strlen(address)]; size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -56,7 +56,7 @@ char *__z_parse_address_segment_ws(char *address) { p_start = _z_cptr_char_offset(p_start, 1); p_end = _z_cptr_char_offset(p_end, -1); size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -64,7 +64,7 @@ char *__z_parse_address_segment_ws(char *address) { // IPv4 else { size_t len = _z_ptr_char_diff(p_end, p_start) + (size_t)1; - ret = (char *)zp_malloc(len); + ret = (char *)z_malloc(len); if (ret != NULL) { _z_str_n_copy(ret, p_start, len); } @@ -85,7 +85,7 @@ int8_t _z_endpoint_ws_valid(_z_endpoint_t *endpoint) { if (s_addr == NULL) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } else { - zp_free(s_addr); + z_free(s_addr); } } @@ -98,7 +98,7 @@ int8_t _z_endpoint_ws_valid(_z_endpoint_t *endpoint) { if ((port < (uint32_t)1) || (port > (uint32_t)65355)) { // Port numbers should range from 1 to 65355 ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } - zp_free(s_port); + z_free(s_port); } } @@ -167,8 +167,8 @@ int8_t _z_new_link_ws(_z_link_t *zl, _z_endpoint_t *endpoint) { char *s_addr = __z_parse_address_segment_ws(endpoint->_locator._address); char *s_port = __z_parse_port_segment_ws(endpoint->_locator._address); ret = _z_create_endpoint_ws(&zl->_socket._ws._rep, s_addr, s_port); - zp_free(s_addr); - zp_free(s_port); + z_free(s_addr); + z_free(s_port); zl->_open_f = _z_f_link_open_ws; zl->_listen_f = _z_f_link_listen_ws; diff --git a/src/net/config.c b/src/net/config.c index 58bd252c3..7b0457836 100644 --- a/src/net/config.c +++ b/src/net/config.c @@ -18,7 +18,7 @@ #include "zenoh-pico/net/config.h" _z_config_t *_z_config_empty(void) { - _z_config_t *config = (_z_config_t *)zp_malloc(sizeof(_z_config_t)); + _z_config_t *config = (_z_config_t *)z_malloc(sizeof(_z_config_t)); if (config != NULL) { _z_config_init(config); } diff --git a/src/net/filtering.c b/src/net/filtering.c index 9d4cb4d83..bf26bbdf4 100644 --- a/src/net/filtering.c +++ b/src/net/filtering.c @@ -75,7 +75,7 @@ static void _z_write_filter_callback(const _z_interest_msg_t *msg, void *arg) { int8_t _z_write_filter_create(_z_publisher_t *pub) { uint8_t flags = _Z_INTEREST_FLAG_KEYEXPRS | _Z_INTEREST_FLAG_SUBSCRIBERS | _Z_INTEREST_FLAG_RESTRICTED | _Z_INTEREST_FLAG_CURRENT | _Z_INTEREST_FLAG_FUTURE | _Z_INTEREST_FLAG_AGGREGATE; - _z_writer_filter_ctx_t *ctx = (_z_writer_filter_ctx_t *)zp_malloc(sizeof(_z_writer_filter_ctx_t)); + _z_writer_filter_ctx_t *ctx = (_z_writer_filter_ctx_t *)z_malloc(sizeof(_z_writer_filter_ctx_t)); if (ctx == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -87,7 +87,7 @@ int8_t _z_write_filter_create(_z_publisher_t *pub) { pub->_filter._interest_id = _z_declare_interest(&pub->_zn.in->val, _z_keyexpr_alias(pub->_key), _z_write_filter_callback, flags, (void *)ctx); if (pub->_filter._interest_id == 0) { - zp_free(ctx); + z_free(ctx); return _Z_ERR_GENERIC; } return _Z_RES_OK; @@ -95,7 +95,7 @@ int8_t _z_write_filter_create(_z_publisher_t *pub) { int8_t _z_write_filter_destroy(const _z_publisher_t *pub) { _Z_RETURN_IF_ERR(_z_undeclare_interest(&pub->_zn.in->val, pub->_filter._interest_id)); - zp_free(pub->_filter.ctx); + z_free(pub->_filter.ctx); return _Z_RES_OK; } diff --git a/src/net/memory.c b/src/net/memory.c index b8cd9f55c..33f7582ec 100644 --- a/src/net/memory.c +++ b/src/net/memory.c @@ -44,7 +44,7 @@ void _z_sample_free(_z_sample_t **sample) { if (ptr != NULL) { _z_sample_clear(ptr); - zp_free(ptr); + z_free(ptr); *sample = NULL; } } @@ -61,7 +61,7 @@ void _z_hello_free(_z_hello_t **hello) { if (ptr != NULL) { _z_hello_clear(ptr); - zp_free(ptr); + z_free(ptr); *hello = NULL; } } @@ -77,7 +77,7 @@ void _z_reply_data_free(_z_reply_data_t **reply_data) { if (ptr != NULL) { _z_reply_data_clear(ptr); - zp_free(ptr); + z_free(ptr); *reply_data = NULL; } } @@ -93,7 +93,7 @@ void _z_value_free(_z_value_t **value) { if (ptr != NULL) { _z_value_clear(ptr); - zp_free(ptr); + z_free(ptr); *value = NULL; } } diff --git a/src/net/primitives.c b/src/net/primitives.c index 7d459350a..4fa322980 100644 --- a/src/net/primitives.c +++ b/src/net/primitives.c @@ -103,7 +103,7 @@ int8_t _z_undeclare_resource(_z_session_t *zn, uint16_t rid) { _z_publisher_t *_z_declare_publisher(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, z_congestion_control_t congestion_control, z_priority_t priority) { // Allocate publisher - _z_publisher_t *ret = (_z_publisher_t *)zp_malloc(sizeof(_z_publisher_t)); + _z_publisher_t *ret = (_z_publisher_t *)z_malloc(sizeof(_z_publisher_t)); if (ret == NULL) { return NULL; } @@ -200,7 +200,7 @@ _z_subscriber_t *_z_declare_subscriber(_z_session_rc_t *zn, _z_keyexpr_t keyexpr s._arg = arg; // Allocate subscriber - _z_subscriber_t *ret = (_z_subscriber_t *)zp_malloc(sizeof(_z_subscriber_t)); + _z_subscriber_t *ret = (_z_subscriber_t *)z_malloc(sizeof(_z_subscriber_t)); if (ret == NULL) { _z_subscription_clear(&s); return NULL; @@ -269,7 +269,7 @@ _z_queryable_t *_z_declare_queryable(_z_session_rc_t *zn, _z_keyexpr_t keyexpr, q._arg = arg; // Allocate queryable - _z_queryable_t *ret = (_z_queryable_t *)zp_malloc(sizeof(_z_queryable_t)); + _z_queryable_t *ret = (_z_queryable_t *)z_malloc(sizeof(_z_queryable_t)); if (ret == NULL) { _z_session_queryable_clear(&q); return NULL; @@ -381,7 +381,7 @@ int8_t _z_send_reply(const _z_query_t *query, _z_keyexpr_t keyexpr, const _z_val /*------------------ Query ------------------*/ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, const z_query_target_t target, const z_consolidation_mode_t consolidation, _z_value_t value, _z_reply_handler_t callback, - void *arg_call, _z_drop_handler_t dropper, void *arg_drop + void *arg_call, _z_drop_handler_t dropper, void *arg_drop, uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment @@ -390,7 +390,7 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, int8_t ret = _Z_RES_OK; // Create the pending query object - _z_pending_query_t *pq = (_z_pending_query_t *)zp_malloc(sizeof(_z_pending_query_t)); + _z_pending_query_t *pq = (_z_pending_query_t *)z_malloc(sizeof(_z_pending_query_t)); if (pq != NULL) { pq->_id = _z_get_query_id(zn); pq->_key = _z_get_expanded_key_from_key(zn, &keyexpr); @@ -407,12 +407,13 @@ int8_t _z_query(_z_session_t *zn, _z_keyexpr_t keyexpr, const char *parameters, ret = _z_register_pending_query(zn, pq); // Add the pending query to the current session if (ret == _Z_RES_OK) { _z_bytes_t params = _z_bytes_wrap((uint8_t *)pq->_parameters, strlen(pq->_parameters)); - _z_zenoh_message_t z_msg = _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value + _z_zenoh_message_t z_msg = + _z_msg_make_query(&keyexpr, ¶ms, pq->_id, pq->_consolidation, &value, timeout_ms #if Z_FEATURE_ATTACHMENT == 1 - , - attachment + , + attachment #endif - ); + ); if (_z_send_n_msg(zn, &z_msg, Z_RELIABILITY_RELIABLE, Z_CONGESTION_CONTROL_BLOCK) != _Z_RES_OK) { _z_unregister_pending_query(zn, pq); diff --git a/src/net/publish.c b/src/net/publish.c index a17dbc97a..7b2d1bffc 100644 --- a/src/net/publish.c +++ b/src/net/publish.c @@ -25,7 +25,7 @@ void _z_publisher_free(_z_publisher_t **pub) { if (ptr != NULL) { _z_publisher_clear(ptr); - zp_free(ptr); + z_free(ptr); *pub = NULL; } } diff --git a/src/net/query.c b/src/net/query.c index 41b290f52..74b1ad88c 100644 --- a/src/net/query.c +++ b/src/net/query.c @@ -24,7 +24,7 @@ void _z_query_clear(_z_query_t *q) { } // Clean up memory _z_msg_clear(&z_msg); - zp_free(q->_parameters); + z_free(q->_parameters); _z_keyexpr_clear(&q->_key); _z_value_clear(&q->_value); // Ideally free session rc if you have one @@ -36,7 +36,7 @@ _z_query_t _z_query_create(const _z_value_t *value, const _z_keyexpr_t *key, con _z_query_t q; q._request_id = request_id; q._zn = zn; // Ideally would have been an rc - q._parameters = (char *)zp_malloc(parameters->len + 1); + q._parameters = (char *)z_malloc(parameters->len + 1); memcpy(q._parameters, parameters->start, parameters->len); q._parameters[parameters->len] = 0; q._anyke = (strstr(q._parameters, Z_SELECTOR_QUERY_MATCH) == NULL) ? false : true; @@ -56,7 +56,7 @@ void _z_queryable_free(_z_queryable_t **qbl) { if (ptr != NULL) { _z_queryable_clear(ptr); - zp_free(ptr); + z_free(ptr); *qbl = NULL; } } diff --git a/src/net/session.c b/src/net/session.c index 4c386e3a3..fd3d5997d 100644 --- a/src/net/session.c +++ b/src/net/session.c @@ -71,19 +71,19 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { if (connect == NULL && listen == NULL) { // Scout if peer is not configured opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_WHAT_KEY); if (opt_as_str == NULL) { - opt_as_str = Z_CONFIG_SCOUTING_WHAT_DEFAULT; + opt_as_str = (char *)Z_CONFIG_SCOUTING_WHAT_DEFAULT; } z_what_t what = strtol(opt_as_str, NULL, 10); opt_as_str = _z_config_get(config, Z_CONFIG_MULTICAST_LOCATOR_KEY); if (opt_as_str == NULL) { - opt_as_str = Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; + opt_as_str = (char *)Z_CONFIG_MULTICAST_LOCATOR_DEFAULT; } char *mcast_locator = opt_as_str; opt_as_str = _z_config_get(config, Z_CONFIG_SCOUTING_TIMEOUT_KEY); if (opt_as_str == NULL) { - opt_as_str = Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; + opt_as_str = (char *)Z_CONFIG_SCOUTING_TIMEOUT_DEFAULT; } uint32_t timeout = strtoul(opt_as_str, NULL, 10); @@ -150,7 +150,7 @@ int8_t _z_open(_z_session_t *zn, _z_config_t *config) { void _z_close(_z_session_t *zn) { _z_session_close(zn, _Z_CLOSE_GENERIC); } _z_config_t *_z_info(const _z_session_t *zn) { - _z_config_t *ps = (_z_config_t *)zp_malloc(sizeof(_z_config_t)); + _z_config_t *ps = (_z_config_t *)z_malloc(sizeof(_z_config_t)); if (ps != NULL) { _z_config_init(ps); _z_bytes_t local_zid = _z_bytes_wrap(zn->_local_zid.id, _z_id_len(zn->_local_zid)); @@ -179,10 +179,10 @@ int8_t _zp_send_keep_alive(_z_session_t *zn) { return _z_send_keep_alive(&zn->_t int8_t _zp_send_join(_z_session_t *zn) { return _z_send_join(&zn->_tp); } #if Z_FEATURE_MULTI_THREAD == 1 -int8_t _zp_start_read_task(_z_session_t *zn, zp_task_attr_t *attr) { +int8_t _zp_start_read_task(_z_session_t *zn, z_task_attr_t *attr) { int8_t ret = _Z_RES_OK; // Allocate task - zp_task_t *task = (zp_task_t *)zp_malloc(sizeof(zp_task_t)); + z_task_t *task = (z_task_t *)z_malloc(sizeof(z_task_t)); if (task == NULL) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -203,15 +203,15 @@ int8_t _zp_start_read_task(_z_session_t *zn, zp_task_attr_t *attr) { } // Free task if operation failed if (ret != _Z_RES_OK) { - zp_free(task); + z_free(task); } return ret; } -int8_t _zp_start_lease_task(_z_session_t *zn, zp_task_attr_t *attr) { +int8_t _zp_start_lease_task(_z_session_t *zn, z_task_attr_t *attr) { int8_t ret = _Z_RES_OK; // Allocate task - zp_task_t *task = (zp_task_t *)zp_malloc(sizeof(zp_task_t)); + z_task_t *task = (z_task_t *)z_malloc(sizeof(z_task_t)); if (task == NULL) { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -232,7 +232,7 @@ int8_t _zp_start_lease_task(_z_session_t *zn, zp_task_attr_t *attr) { } // Free task if operation failed if (ret != _Z_RES_OK) { - zp_free(task); + z_free(task); } return ret; } diff --git a/src/net/subscribe.c b/src/net/subscribe.c index 23c7faddb..e076ca218 100644 --- a/src/net/subscribe.c +++ b/src/net/subscribe.c @@ -31,7 +31,7 @@ void _z_subscriber_free(_z_subscriber_t **sub) { if (ptr != NULL) { _z_subscriber_clear(ptr); - zp_free(ptr); + z_free(ptr); *sub = NULL; } } diff --git a/src/protocol/codec.c b/src/protocol/codec.c index 8a371ef52..04f4d3a20 100644 --- a/src/protocol/codec.c +++ b/src/protocol/codec.c @@ -277,8 +277,8 @@ int8_t _z_str_decode(char **str, _z_zbuf_t *zbf) { _z_zint_t len = 0; ret |= _z_zsize_decode(&len, zbf); if (ret == _Z_RES_OK) { - if (_z_zbuf_len(zbf) >= len) { // Check if we have enough bytes to read - char *tmp = (char *)zp_malloc(len + (size_t)1); // Allocate space for the string terminator + if (_z_zbuf_len(zbf) >= len) { // Check if we have enough bytes to read + char *tmp = (char *)z_malloc(len + (size_t)1); // Allocate space for the string terminator if (tmp != NULL) { tmp[len] = '\0'; _z_zbuf_read_bytes(zbf, (uint8_t *)tmp, 0, len); diff --git a/src/protocol/codec/declarations.c b/src/protocol/codec/declarations.c index bb0806a6a..aa9426dce 100644 --- a/src/protocol/codec/declarations.c +++ b/src/protocol/codec/declarations.c @@ -250,7 +250,7 @@ int8_t _z_undecl_decode_extensions(_z_msg_ext_t *extension, void *ctx) { _Z_RETURN_IF_ERR(_z_zint16_decode(&ke->_id, zbf)); if (_Z_HAS_FLAG(header, 1)) { size_t len = _z_zbuf_len(zbf); - ke->_suffix = zp_malloc(len + 1); + ke->_suffix = z_malloc(len + 1); if (!ke->_suffix) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -287,7 +287,7 @@ int8_t _z_decl_commons_decode(_z_zbuf_t *zbf, uint8_t header, _Bool *has_extensi if (_z_zbuf_len(zbf) < len) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } - ke->_suffix = zp_malloc(len + 1); + ke->_suffix = z_malloc(len + 1); if (ke->_suffix == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -387,7 +387,7 @@ int8_t _z_decl_interest_decode(_z_decl_interest_t *decl, _z_zbuf_t *zbf, uint8_t if (_z_zbuf_len(zbf) < len) { return _Z_ERR_MESSAGE_DESERIALIZATION_FAILED; } - decl->_keyexpr._suffix = zp_malloc(len + 1); + decl->_keyexpr._suffix = z_malloc(len + 1); if (decl->_keyexpr._suffix == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -429,10 +429,6 @@ int8_t _z_undecl_interest_decode(_z_undecl_interest_t *decl, _z_zbuf_t *zbf, uin int8_t _z_declaration_decode(_z_declaration_t *decl, _z_zbuf_t *zbf) { uint8_t header; _Z_RETURN_IF_ERR(_z_uint8_decode(&header, zbf)); - if (_Z_HAS_FLAG(header, _Z_FLAG_INTEREST_ID)) { - return _Z_ERR_MESSAGE_FLAG_UNEXPECTED; - } - int8_t ret; switch (_Z_MID(header)) { case _Z_DECL_KEXPR_MID: { diff --git a/src/protocol/codec/ext.c b/src/protocol/codec/ext.c index bc7c18c5d..6ca7ecaee 100644 --- a/src/protocol/codec/ext.c +++ b/src/protocol/codec/ext.c @@ -154,7 +154,7 @@ int8_t _z_msg_ext_vec_encode(_z_wbuf_t *wbf, const _z_msg_ext_vec_t *extensions) return ret; } int8_t _z_msg_ext_vec_push_callback(_z_msg_ext_t *extension, _z_msg_ext_vec_t *extensions) { - _z_msg_ext_t *ext = (_z_msg_ext_t *)zp_malloc(sizeof(_z_msg_ext_t)); + _z_msg_ext_t *ext = (_z_msg_ext_t *)z_malloc(sizeof(_z_msg_ext_t)); *ext = *extension; *extension = _z_msg_ext_make_unit(0); _z_msg_ext_vec_append(extensions, extension); @@ -167,8 +167,8 @@ int8_t _z_msg_ext_vec_decode(_z_msg_ext_vec_t *extensions, _z_zbuf_t *zbf) { } int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { - uint8_t ext_id = _Z_EXT_ID(extension->_header); #if (ZENOH_DEBUG >= 1) + uint8_t ext_id = _Z_EXT_ID(extension->_header); switch (_Z_EXT_ENC(extension->_header)) { case _Z_MSG_EXT_ENC_UNIT: { _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), UNIT", ext_id, trace_id); @@ -181,13 +181,13 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { } case _Z_MSG_EXT_ENC_ZBUF: { _z_bytes_t buf = extension->_body._zbuf._val; - char *hex = zp_malloc(buf.len * 2 + 1); + char *hex = z_malloc(buf.len * 2 + 1); for (size_t i = 0; i < buf.len; ++i) { snprintf(hex + 2 * i, 3, "%02x", buf.start[i]); } _Z_ERROR("Unknown mandatory extension found (extension_id: %02x, trace_id: %02x), ZBUF(%.*s)", ext_id, trace_id, (int)buf.len * 2, hex); - zp_free(hex); + z_free(hex); break; } default: { @@ -195,6 +195,9 @@ int8_t _z_msg_ext_unknown_error(_z_msg_ext_t *extension, uint8_t trace_id) { trace_id); } } +#else + _ZP_UNUSED(extension); + _ZP_UNUSED(trace_id); #endif return _Z_ERR_MESSAGE_EXTENSION_MANDATORY_AND_UNKNOWN; } diff --git a/src/protocol/codec/message.c b/src/protocol/codec/message.c index 7dd176d87..62859e729 100644 --- a/src/protocol/codec/message.c +++ b/src/protocol/codec/message.c @@ -150,7 +150,7 @@ int8_t _z_locators_encode(_z_wbuf_t *wbf, const _z_locator_array_t *la) { for (size_t i = 0; i < la->_len; i++) { char *s = _z_locator_to_str(&la->_val[i]); _Z_RETURN_IF_ERR(_z_str_encode(wbf, s)) - zp_free(s); + z_free(s); } return ret; @@ -172,7 +172,7 @@ int8_t _z_locators_decode_na(_z_locator_array_t *a_loc, _z_zbuf_t *zbf) { if (ret == _Z_RES_OK) { _z_locator_init(&a_loc->_val[i]); ret |= _z_locator_from_str(&a_loc->_val[i], str); - zp_free(str); + z_free(str); } else { a_loc->_len = i; } diff --git a/src/protocol/codec/network.c b/src/protocol/codec/network.c index 5074b8a2e..5d88d9143 100644 --- a/src/protocol/codec/network.c +++ b/src/protocol/codec/network.c @@ -436,6 +436,10 @@ int8_t _z_declare_decode(_z_n_msg_declare_t *decl, _z_zbuf_t *zbf, uint8_t heade if (_Z_HAS_FLAG(header, _Z_FLAG_N_Z)) { _Z_RETURN_IF_ERR(_z_msg_ext_decode_iter(zbf, _z_declare_decode_extensions, decl)) } + // FIXME: For now, zenoh pico should not receive this (answer from interests with current=1, future=0) + if (_Z_HAS_FLAG(header, _Z_FLAG_N_DECLARE_I)) { + return _Z_ERR_MESSAGE_FLAG_UNEXPECTED; + } return _z_declaration_decode(&decl->_decl, zbf); } diff --git a/src/protocol/codec/transport.c b/src/protocol/codec/transport.c index 730a5ab83..87eba5ae5 100644 --- a/src/protocol/codec/transport.c +++ b/src/protocol/codec/transport.c @@ -350,7 +350,7 @@ int8_t _z_frame_decode(_z_t_msg_frame_t *msg, _z_zbuf_t *zbf, uint8_t header) { while (_z_zbuf_len(zbf) > 0) { // Mark the reading position of the iobfer size_t r_pos = _z_zbuf_get_rpos(zbf); - _z_network_message_t *nm = (_z_network_message_t *)zp_malloc(sizeof(_z_network_message_t)); + _z_network_message_t *nm = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); memset(nm, 0, sizeof(_z_network_message_t)); ret |= _z_network_message_decode(nm, zbf); if (ret == _Z_RES_OK) { diff --git a/src/protocol/config.c b/src/protocol/config.c index ffee65656..3c70a27e6 100644 --- a/src/protocol/config.c +++ b/src/protocol/config.c @@ -81,7 +81,7 @@ int8_t _z_str_intmap_from_strn(_z_str_intmap_t *strint, const char *s, uint8_t a } size_t p_value_len = _z_ptr_char_diff(p_value_end, p_value_start) + (size_t)1; - char *p_value = (char *)zp_malloc(p_value_len); + char *p_value = (char *)z_malloc(p_value_len); if (p_value != NULL) { _z_str_n_copy(p_value, p_value_start, p_value_len); @@ -155,7 +155,7 @@ char *_z_str_intmap_to_str(const _z_str_intmap_t *s, uint8_t argc, _z_str_intmap // Calculate the string length to allocate size_t len = _z_str_intmap_strlen(s, argc, argv) + (size_t)1; // Build the string - char *dst = (char *)zp_malloc(len); + char *dst = (char *)z_malloc(len); if (dst != NULL) { _z_str_intmap_onto_str(dst, len, s, argc, argv); } diff --git a/src/protocol/definitions/network.c b/src/protocol/definitions/network.c index 9836113cb..b4202486b 100644 --- a/src/protocol/definitions/network.c +++ b/src/protocol/definitions/network.c @@ -126,13 +126,14 @@ void _z_n_msg_free(_z_network_message_t **msg) { if (ptr != NULL) { _z_n_msg_clear(ptr); - zp_free(ptr); + z_free(ptr); *msg = NULL; } } _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes_t) parameters, _z_zint_t qid, - z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value + z_consolidation_mode_t consolidation, _Z_MOVE(_z_value_t) value, + uint32_t timeout_ms #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t attachment @@ -156,7 +157,7 @@ _z_zenoh_message_t _z_msg_make_query(_Z_MOVE(_z_keyexpr_t) key, _Z_MOVE(_z_bytes ._ext_budget = 0, ._ext_qos = _Z_N_QOS_DEFAULT, ._ext_target = Z_QUERY_TARGET_BEST_MATCHING, - ._ext_timeout_ms = 0, + ._ext_timeout_ms = timeout_ms, ._ext_timestamp = _z_timestamp_null(), }, }; diff --git a/src/protocol/iobuf.c b/src/protocol/iobuf.c index 67802e9fe..c5700886f 100644 --- a/src/protocol/iobuf.c +++ b/src/protocol/iobuf.c @@ -40,7 +40,7 @@ void __z_iosli_init(_z_iosli_t *ios, size_t capacity) { ios->_w_pos = 0; ios->_capacity = capacity; ios->_is_alloc = true; - ios->_buf = (uint8_t *)zp_malloc(capacity); + ios->_buf = (uint8_t *)z_malloc(capacity); if (ios->_buf == NULL) { ios->_capacity = 0; ios->_is_alloc = false; @@ -54,7 +54,7 @@ _z_iosli_t _z_iosli_make(size_t capacity) { } _z_iosli_t *_z_iosli_new(size_t capacity) { - _z_iosli_t *pios = (_z_iosli_t *)zp_malloc(sizeof(_z_iosli_t)); + _z_iosli_t *pios = (_z_iosli_t *)z_malloc(sizeof(_z_iosli_t)); if (pios != NULL) { __z_iosli_init(pios, capacity); } @@ -119,7 +119,7 @@ size_t _z_iosli_size(const _z_iosli_t *ios) { void _z_iosli_clear(_z_iosli_t *ios) { if ((ios->_is_alloc == true) && (ios->_buf != NULL)) { - zp_free(ios->_buf); + z_free(ios->_buf); ios->_buf = NULL; } } @@ -130,7 +130,7 @@ void _z_iosli_free(_z_iosli_t **ios) { if (ptr != NULL) { _z_iosli_clear(ptr); - zp_free(ptr); + z_free(ptr); *ios = NULL; } } @@ -141,7 +141,7 @@ void _z_iosli_copy(_z_iosli_t *dst, const _z_iosli_t *src) { dst->_capacity = src->_capacity; dst->_is_alloc = src->_is_alloc; if (dst->_is_alloc == true) { - dst->_buf = (uint8_t *)zp_malloc(src->_capacity); + dst->_buf = (uint8_t *)z_malloc(src->_capacity); if (dst->_buf != NULL) { (void)memcpy(dst->_buf, src->_buf, src->_capacity); } @@ -151,7 +151,7 @@ void _z_iosli_copy(_z_iosli_t *dst, const _z_iosli_t *src) { } _z_iosli_t *_z_iosli_clone(const _z_iosli_t *src) { - _z_iosli_t *dst = (_z_iosli_t *)zp_malloc(_z_iosli_size(src)); + _z_iosli_t *dst = (_z_iosli_t *)z_malloc(_z_iosli_size(src)); if (dst != NULL) { _z_iosli_copy(dst, src); } @@ -233,7 +233,7 @@ void _z_zbuf_free(_z_zbuf_t **zbf) { if (ptr != NULL) { _z_iosli_clear(&ptr->_ios); - zp_free(ptr); + z_free(ptr); *zbf = NULL; } } @@ -245,7 +245,7 @@ void _z_wbuf_add_iosli(_z_wbuf_t *wbf, _z_iosli_t *ios) { } _z_iosli_t *__z_wbuf_new_iosli(size_t capacity) { - _z_iosli_t *ios = (_z_iosli_t *)zp_malloc(sizeof(_z_iosli_t)); + _z_iosli_t *ios = (_z_iosli_t *)z_malloc(sizeof(_z_iosli_t)); if (ios != NULL) { __z_iosli_init(ios, capacity); } @@ -559,7 +559,7 @@ void _z_wbuf_free(_z_wbuf_t **wbf) { if (ptr != NULL) { _z_wbuf_clear(ptr); - zp_free(ptr); + z_free(ptr); *wbf = NULL; } } diff --git a/src/protocol/keyexpr.c b/src/protocol/keyexpr.c index 4f1b10a60..49fefcbf7 100644 --- a/src/protocol/keyexpr.c +++ b/src/protocol/keyexpr.c @@ -69,7 +69,7 @@ void _z_keyexpr_free(_z_keyexpr_t **rk) { if (ptr != NULL) { _z_keyexpr_clear(ptr); - zp_free(ptr); + z_free(ptr); *rk = NULL; } } diff --git a/src/session/interest.c b/src/session/interest.c index 499a917d6..eb8bc5be0 100644 --- a/src/session/interest.c +++ b/src/session/interest.c @@ -195,7 +195,7 @@ _z_session_interest_rc_t *_z_register_interest(_z_session_t *zn, _z_session_inte _z_session_interest_rc_t *ret = NULL; _zp_session_lock_mutex(zn); - ret = (_z_session_interest_rc_t *)zp_malloc(sizeof(_z_session_interest_rc_t)); + ret = (_z_session_interest_rc_t *)z_malloc(sizeof(_z_session_interest_rc_t)); if (ret != NULL) { *ret = _z_session_interest_rc_new_from_val(*intr); zn->_local_interests = _z_session_interest_rc_list_push(zn->_local_interests, ret); @@ -206,7 +206,7 @@ _z_session_interest_rc_t *_z_register_interest(_z_session_t *zn, _z_session_inte static int8_t _unsafe_z_register_declare(_z_session_t *zn, const _z_keyexpr_t *key, uint32_t id, uint8_t type) { _z_declare_data_t *decl = NULL; - decl = (_z_declare_data_t *)zp_malloc(sizeof(_z_declare_data_t)); + decl = (_z_declare_data_t *)z_malloc(sizeof(_z_declare_data_t)); if (decl == NULL) { return _Z_ERR_SYSTEM_OUT_OF_MEMORY; } @@ -361,6 +361,9 @@ int8_t _z_interest_process_final_interest(_z_session_t *zn, uint32_t id) { _zp_session_lock_mutex(zn); _z_session_interest_rc_t *intr = __unsafe_z_get_interest_by_id(zn, id); _zp_session_unlock_mutex(zn); + if (intr == NULL) { + return _Z_RES_OK; + } // Trigger callback if (intr->in->val._callback != NULL) { intr->in->val._callback(&msg, intr->in->val._arg); diff --git a/src/session/push.c b/src/session/push.c index 55187bae7..0fa85cb4c 100644 --- a/src/session/push.c +++ b/src/session/push.c @@ -32,7 +32,7 @@ int8_t _z_trigger_push(_z_session_t *zn, _z_n_msg_push_t *push) { #if Z_FEATURE_ATTACHMENT == 1 z_attachment_t att = _z_encoded_as_attachment(&push->_body._body._put._attachment); #endif - ret = _z_trigger_subscriptions(zn, push->_key, payload, encoding, kind, push->_timestamp + ret = _z_trigger_subscriptions(zn, push->_key, payload, encoding, kind, push->_timestamp, push->_qos #if Z_FEATURE_ATTACHMENT == 1 , att diff --git a/src/session/query.c b/src/session/query.c index a3080761d..8e596e6c3 100644 --- a/src/session/query.c +++ b/src/session/query.c @@ -25,7 +25,7 @@ #if Z_FEATURE_QUERY == 1 _z_reply_t *_z_reply_alloc_and_move(_z_reply_t *_reply) { - _z_reply_t *reply = (_z_reply_t *)zp_malloc(sizeof(_z_reply_t)); + _z_reply_t *reply = (_z_reply_t *)z_malloc(sizeof(_z_reply_t)); if (reply != NULL) { *reply = *_reply; (void)memset(_reply, 0, sizeof(_z_reply_t)); @@ -41,7 +41,7 @@ void _z_reply_free(_z_reply_t **reply) { if (*reply != NULL) { _z_reply_clear(ptr); - zp_free(ptr); + z_free(ptr); *reply = NULL; } } @@ -63,7 +63,7 @@ void _z_pending_query_clear(_z_pending_query_t *pen_qry) { pen_qry->_dropper(pen_qry->_drop_arg); } - zp_free(pen_qry->_call_arg); + z_free(pen_qry->_call_arg); _z_keyexpr_clear(&pen_qry->_key); _z_str_clear(pen_qry->_parameters); @@ -186,7 +186,7 @@ int8_t _z_trigger_query_reply_partial(_z_session_t *zn, const _z_zint_t id, cons if (drop == false) { // Cache most recent reply - pen_rep = (_z_pending_reply_t *)zp_malloc(sizeof(_z_pending_reply_t)); + pen_rep = (_z_pending_reply_t *)z_malloc(sizeof(_z_pending_reply_t)); if (pen_rep != NULL) { if (pen_qry->_consolidation == Z_CONSOLIDATION_MODE_MONOTONIC) { // No need to store the whole reply in the monotonic mode. diff --git a/src/session/queryable.c b/src/session/queryable.c index d85533466..fc587cccf 100644 --- a/src/session/queryable.c +++ b/src/session/queryable.c @@ -121,7 +121,7 @@ _z_session_queryable_rc_t *_z_register_session_queryable(_z_session_t *zn, _z_se _zp_session_lock_mutex(zn); - ret = (_z_session_queryable_rc_t *)zp_malloc(sizeof(_z_session_queryable_rc_t)); + ret = (_z_session_queryable_rc_t *)z_malloc(sizeof(_z_session_queryable_rc_t)); if (ret != NULL) { *ret = _z_session_queryable_rc_new_from_val(*q); zn->_local_queryable = _z_session_queryable_rc_list_push(zn->_local_queryable, ret); diff --git a/src/session/reply.c b/src/session/reply.c index 7441b5e7f..dc57530e3 100644 --- a/src/session/reply.c +++ b/src/session/reply.c @@ -37,11 +37,12 @@ int8_t _z_trigger_reply_partial(_z_session_t *zn, _z_zint_t id, _z_keyexpr_t key int8_t _z_trigger_reply_final(_z_session_t *zn, _z_n_msg_response_final_t *final) { int8_t ret = _Z_RES_OK; +#if Z_FEATURE_QUERY == 1 // TODO check id to know where to dispatch _z_zint_t id = final->_request_id; - -#if Z_FEATURE_QUERY == 1 _z_trigger_query_reply_final(zn, id); +#else + _ZP_UNUSED(final); #endif return ret; } diff --git a/src/session/resource.c b/src/session/resource.c index 477ff2789..0c2dc44ce 100644 --- a/src/session/resource.c +++ b/src/session/resource.c @@ -42,7 +42,7 @@ void _z_resource_free(_z_resource_t **res) { if (ptr != NULL) { _z_resource_clear(ptr); - zp_free(ptr); + z_free(ptr); *res = NULL; } } @@ -126,7 +126,7 @@ _z_keyexpr_t __z_get_expanded_key_from_key(_z_resource_list_t *xs, const _z_keye if (len != (size_t)0) { char *rname = NULL; - rname = (char *)zp_malloc(len); + rname = (char *)z_malloc(len); if (rname != NULL) { rname[0] = '\0'; // NULL terminator must be set (required to strcat) len = len - (size_t)1; @@ -231,7 +231,7 @@ int16_t _z_register_resource(_z_session_t *zn, _z_keyexpr_t key, uint16_t id, ui } ret = key._id; if ((key._suffix != NULL)) { - _z_resource_t *res = zp_malloc(sizeof(_z_resource_t)); + _z_resource_t *res = z_malloc(sizeof(_z_resource_t)); if (res == NULL) { ret = Z_RESOURCE_ID_NONE; } else { diff --git a/src/session/rx.c b/src/session/rx.c index 4e0a5ba16..629437e44 100644 --- a/src/session/rx.c +++ b/src/session/rx.c @@ -107,7 +107,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint z_attachment_t att = _z_encoded_as_attachment(&put._attachment); #endif ret = _z_trigger_subscriptions(zn, req._key, put._payload, put._encoding, Z_SAMPLE_KIND_PUT, - put._commons._timestamp + put._commons._timestamp, req._ext_qos #if Z_FEATURE_ATTACHMENT == 1 , att @@ -123,7 +123,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint _z_msg_del_t del = req._body._del; #if Z_FEATURE_SUBSCRIPTION == 1 ret = _z_trigger_subscriptions(zn, req._key, _z_bytes_empty(), z_encoding_default(), - Z_SAMPLE_KIND_DELETE, del._commons._timestamp + Z_SAMPLE_KIND_DELETE, del._commons._timestamp, req._ext_qos #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_null() @@ -149,6 +149,7 @@ int8_t _z_handle_network_message(_z_session_t *zn, _z_zenoh_message_t *msg, uint // @TODO: expose zenoh errors to the user _z_msg_err_t error = response._body._err; _z_bytes_t payload = error._payload; + _ZP_UNUSED(payload); // Unused when logs are deactivated _Z_ERROR("Received Err for query %zu: message=%.*s", response._request_id, (int)payload.len, payload.start); } break; diff --git a/src/session/scout.c b/src/session/scout.c index 0099552d4..b3a8dffb8 100644 --- a/src/session/scout.c +++ b/src/session/scout.c @@ -55,8 +55,8 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig // The receiving buffer _z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE); - zp_clock_t start = zp_clock_now(); - while (zp_clock_elapsed_ms(&start) < period) { + z_clock_t start = z_clock_now(); + while (z_clock_elapsed_ms(&start) < period) { // Eventually read hello messages _z_zbuf_reset(&zbf); @@ -76,7 +76,7 @@ _z_hello_list_t *__z_scout_loop(const _z_wbuf_t *wbf, const char *locator, unsig switch (_Z_MID(s_msg._header)) { case _Z_MID_HELLO: { _Z_INFO("Received _Z_HELLO message"); - _z_hello_t *hello = (_z_hello_t *)zp_malloc(sizeof(_z_hello_t)); + _z_hello_t *hello = (_z_hello_t *)z_malloc(sizeof(_z_hello_t)); if (hello != NULL) { hello->version = s_msg._body._hello._version; hello->whatami = s_msg._body._hello._whatami; diff --git a/src/session/subscription.c b/src/session/subscription.c index e3a43c2fd..27a258804 100644 --- a/src/session/subscription.c +++ b/src/session/subscription.c @@ -19,6 +19,7 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/protocol/core.h" +#include "zenoh-pico/protocol/definitions/network.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/session/resource.h" #include "zenoh-pico/session/session.h" @@ -120,7 +121,7 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca _zp_session_lock_mutex(zn); - ret = (_z_subscription_rc_t *)zp_malloc(sizeof(_z_subscription_rc_t)); + ret = (_z_subscription_rc_t *)z_malloc(sizeof(_z_subscription_rc_t)); if (ret != NULL) { *ret = _z_subscription_rc_new_from_val(*s); if (is_local == _Z_RESOURCE_IS_LOCAL) { @@ -136,7 +137,7 @@ _z_subscription_rc_t *_z_register_subscription(_z_session_t *zn, uint8_t is_loca } void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len + _z_zint_t payload_len, _z_n_qos_t qos #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t att @@ -144,9 +145,9 @@ void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr ) { _z_encoding_t encoding = {.prefix = Z_ENCODING_PREFIX_DEFAULT, .suffix = _z_bytes_wrap(NULL, 0)}; int8_t ret = _z_trigger_subscriptions(zn, keyexpr, _z_bytes_wrap(payload, payload_len), encoding, Z_SAMPLE_KIND_PUT, - _z_timestamp_null() + _z_timestamp_null(), qos #if Z_FEATURE_ATTACHMENT == 1 - , + , att #endif ); @@ -154,7 +155,8 @@ void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr } int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const _z_bytes_t payload, - const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp + const _z_encoding_t encoding, const _z_zint_t kind, const _z_timestamp_t timestamp, + const _z_n_qos_t qos #if Z_FEATURE_ATTACHMENT == 1 , z_attachment_t att @@ -179,6 +181,7 @@ int8_t _z_trigger_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, co s.encoding = encoding; s.kind = kind; s.timestamp = timestamp; + s.qos = qos; #if Z_FEATURE_ATTACHMENT == 1 s.attachment = att; #endif @@ -225,11 +228,17 @@ void _z_flush_subscriptions(_z_session_t *zn) { #else // Z_FEATURE_SUBSCRIPTION == 0 void _z_trigger_local_subscriptions(_z_session_t *zn, const _z_keyexpr_t keyexpr, const uint8_t *payload, - _z_zint_t payload_len) { + _z_zint_t payload_len, _z_n_qos_t qos +#if Z_FEATURE_ATTACHMENT == 1 + , + z_attachment_t att +#endif +) { _ZP_UNUSED(zn); _ZP_UNUSED(keyexpr); _ZP_UNUSED(payload); _ZP_UNUSED(payload_len); + _ZP_UNUSED(qos); } #endif // Z_FEATURE_SUBSCRIPTION == 1 diff --git a/src/session/utils.c b/src/session/utils.c index eac976016..8600b5211 100644 --- a/src/session/utils.c +++ b/src/session/utils.c @@ -46,7 +46,7 @@ _Bool _z_timestamp_check(const _z_timestamp_t *stamp) { int8_t _z_session_generate_zid(_z_id_t *bs, uint8_t size) { int8_t ret = _Z_RES_OK; - zp_random_fill((uint8_t *)bs->id, size); + z_random_fill((uint8_t *)bs->id, size); return ret; } @@ -74,7 +74,7 @@ int8_t _z_session_init(_z_session_t *zn, _z_id_t *zid) { #endif #if Z_FEATURE_MULTI_THREAD == 1 - ret = zp_mutex_init(&zn->_mutex_inner); + ret = z_mutex_init(&zn->_mutex_inner); if (ret != _Z_RES_OK) { _z_transport_clear(&zn->_tp); return ret; @@ -119,7 +119,7 @@ void _z_session_clear(_z_session_t *zn) { _z_flush_interest(zn); #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_free(&zn->_mutex_inner); + z_mutex_free(&zn->_mutex_inner); #endif // Z_FEATURE_MULTI_THREAD == 1 } @@ -134,8 +134,8 @@ int8_t _z_session_close(_z_session_t *zn, uint8_t reason) { } #if Z_FEATURE_MULTI_THREAD == 1 -void _zp_session_lock_mutex(_z_session_t *zn) { (void)zp_mutex_lock(&zn->_mutex_inner); } -void _zp_session_unlock_mutex(_z_session_t *zn) { (void)zp_mutex_unlock(&zn->_mutex_inner); } +void _zp_session_lock_mutex(_z_session_t *zn) { (void)z_mutex_lock(&zn->_mutex_inner); } +void _zp_session_unlock_mutex(_z_session_t *zn) { (void)z_mutex_unlock(&zn->_mutex_inner); } #else void _zp_session_lock_mutex(_z_session_t *zn) { _ZP_UNUSED(zn); } void _zp_session_unlock_mutex(_z_session_t *zn) { _ZP_UNUSED(zn); } diff --git a/src/system/arduino/esp32/network.cpp b/src/system/arduino/esp32/network.cpp index a865aa5cc..ba4e65206 100644 --- a/src/system/arduino/esp32/network.cpp +++ b/src/system/arduino/esp32/network.cpp @@ -171,7 +171,7 @@ int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_ sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -246,7 +246,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin struct sockaddr *lsockaddr = NULL; unsigned int addrlen = 0; if (rep._iptcp->ai_family == AF_INET) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); @@ -259,7 +259,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin ret = _Z_ERR_GENERIC; } } else if (rep._iptcp->ai_family == AF_INET6) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in6)); addrlen = sizeof(struct sockaddr_in6); @@ -279,7 +279,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -312,7 +312,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // Create laddr endpoint if (ret == _Z_RES_OK) { - struct addrinfo *laddr = (struct addrinfo *)zp_malloc(sizeof(struct addrinfo)); + struct addrinfo *laddr = (struct addrinfo *)z_malloc(sizeof(struct addrinfo)); if (laddr != NULL) { laddr->ai_flags = 0; laddr->ai_family = rep._iptcp->ai_family; @@ -336,7 +336,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } if (ret != _Z_RES_OK) { - zp_free(lsockaddr); + z_free(lsockaddr); } } else { ret = _Z_ERR_GENERIC; @@ -353,7 +353,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo struct sockaddr *lsockaddr = NULL; unsigned int addrlen = 0; if (rep._iptcp->ai_family == AF_INET) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); @@ -366,7 +366,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } } else if (rep._iptcp->ai_family == AF_INET6) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in6)); addrlen = sizeof(struct sockaddr_in6); @@ -387,7 +387,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -438,7 +438,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } - zp_free(lsockaddr); + z_free(lsockaddr); } else { ret = _Z_ERR_GENERIC; } @@ -759,7 +759,7 @@ size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { while (sock._serial->available() < 1) { - zp_sleep_ms(1); // FIXME: Yield by sleeping. + z_sleep_ms(1); // FIXME: Yield by sleeping. } before_cobs[i] = sock._serial->read(); rb = rb + (size_t)1; @@ -824,7 +824,7 @@ size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) { int8_t ret = _Z_RES_OK; - uint8_t *before_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); size_t i = 0; for (i = 0; i < sizeof(uint16_t); ++i) { before_cobs[i] = (len >> (i * (size_t)8)) & (size_t)0XFF; @@ -839,7 +839,7 @@ size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t i = i + (size_t)1; } - uint8_t *after_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs); after_cobs[twb] = 0x00; // Manually add the COBS delimiter ssize_t wb = sock._serial->write(after_cobs, twb + (ssize_t)1); @@ -847,8 +847,8 @@ size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t ret = _Z_ERR_GENERIC; } - zp_free(before_cobs); - zp_free(after_cobs); + z_free(before_cobs); + z_free(after_cobs); size_t sb = len; if (ret != _Z_RES_OK) { diff --git a/src/system/arduino/esp32/system.c b/src/system/arduino/esp32/system.c index 6fc6f9b68..6c82757a5 100644 --- a/src/system/arduino/esp32/system.c +++ b/src/system/arduino/esp32/system.c @@ -20,29 +20,29 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return zp_random_u32(); } +uint8_t z_random_u8(void) { return z_random_u32(); } -uint16_t zp_random_u16(void) { return zp_random_u32(); } +uint16_t z_random_u16(void) { return z_random_u32(); } -uint32_t zp_random_u32(void) { return esp_random(); } +uint32_t z_random_u32(void) { return esp_random(); } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; - ret |= zp_random_u32(); + ret |= z_random_u32(); ret = ret << 32; - ret |= zp_random_u32(); + ret |= z_random_u32(); return ret; } -void zp_random_fill(void *buf, size_t len) { esp_fill_random(buf, len); } +void z_random_fill(void *buf, size_t len) { esp_fill_random(buf, len); } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return heap_caps_malloc(size, MALLOC_CAP_8BIT); } +void *z_malloc(size_t size) { return heap_caps_malloc(size, MALLOC_CAP_8BIT); } -void *zp_realloc(void *ptr, size_t size) { return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT); } +void *z_realloc(void *ptr, size_t size) { return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT); } -void zp_free(void *ptr) { heap_caps_free(ptr); } +void z_free(void *ptr) { heap_caps_free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 // This wrapper is only used for ESP32. @@ -56,14 +56,14 @@ typedef struct { void z_task_wrapper(z_task_arg *targ) { targ->_fun(targ->_arg); vTaskDelete(NULL); - zp_free(targ); + z_free(targ); } /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { int ret = 0; - z_task_arg *z_arg = (z_task_arg *)zp_malloc(sizeof(z_task_arg)); + z_task_arg *z_arg = (z_task_arg *)z_malloc(sizeof(z_task_arg)); if (z_arg != NULL) { z_arg->_fun = fun; z_arg->_arg = arg; @@ -77,86 +77,86 @@ int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), return ret; } -int8_t zp_task_join(zp_task_t *task) { +int8_t z_task_join(z_task_t *task) { // Note: task/thread join not supported on FreeRTOS API, so we force its deletion instead. return zp_task_cancel(task); } -int8_t zp_task_cancel(zp_task_t *task) { +int8_t zp_task_cancel(z_task_t *task) { vTaskDelete(*task); return 0; } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { return pthread_mutex_init(m, NULL); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, NULL); } -int8_t zp_mutex_free(zp_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t zp_mutex_lock(zp_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return pthread_cond_init(cv, NULL); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } -int8_t zp_condvar_free(zp_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep(time); } -int zp_sleep_ms(size_t time) { - zp_time_t start = zp_time_now(); +int z_sleep_ms(size_t time) { + z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. // This may compound, so this approach may make sleeps longer than expected. // This extra check tries to minimize the amount of extra time it might sleep. - while (zp_time_elapsed_ms(&start) < time) { - zp_sleep_us(1000); + while (z_time_elapsed_ms(&start) < time) { + z_sleep_us(1000); } return 0; } -int zp_sleep_s(size_t time) { return sleep(time); } +int z_sleep_s(size_t time) { return sleep(time); } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { - zp_clock_t now; +z_clock_t z_clock_now(void) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); return now; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); return elapsed; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); return elapsed; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = now.tv_sec - instant->tv_sec; @@ -164,38 +164,38 @@ unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; gettimeofday(&now, NULL); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.tv_sec); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/system/arduino/opencr/system.c b/src/system/arduino/opencr/system.c index 93612bf2e..ed017d200 100644 --- a/src/system/arduino/opencr/system.c +++ b/src/system/arduino/opencr/system.c @@ -20,40 +20,40 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return random(0xFF); } +uint8_t z_random_u8(void) { return random(0xFF); } -uint16_t zp_random_u16(void) { return random(0xFFFF); } +uint16_t z_random_u16(void) { return random(0xFFFF); } -uint32_t zp_random_u32(void) { return random(0xFFFFFFFF); } +uint32_t z_random_u32(void) { return random(0xFFFFFFFF); } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; - ret |= zp_random_u32(); + ret |= z_random_u32(); ret = ret << 32; - ret |= zp_random_u32(); + ret |= z_random_u32(); return ret; } -void zp_random_fill(void *buf, size_t len) { +void z_random_fill(void *buf, size_t len) { for (size_t i = 0; i < len; i++) { - ((uint8_t *)buf)[i] = zp_random_u8(); + ((uint8_t *)buf)[i] = z_random_u8(); } } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { +void *z_malloc(size_t size) { // return pvPortMalloc(size); // FIXME: Further investigation is required to understand // why pvPortMalloc or pvPortMallocAligned are failing return malloc(size); } -void *zp_realloc(void *ptr, size_t size) { +void *z_realloc(void *ptr, size_t size) { // Not implemented by the platform return NULL; } -void zp_free(void *ptr) { +void z_free(void *ptr) { // vPortFree(ptr); // FIXME: Further investigation is required to understand // why vPortFree or vPortFreeAligned are failing return free(ptr); @@ -63,133 +63,133 @@ void zp_free(void *ptr) { #error "Multi-threading not supported yet on OpenCR port. Disable it by defining Z_FEATURE_MULTI_THREAD=0" /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { return -1; } +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return -1; } -int8_t zp_task_join(zp_task_t *task) { return -1; } +int8_t z_task_join(z_task_t *task) { return -1; } -int8_t zp_task_cancel(zp_task_t *task) { return -1; } +int8_t zp_task_cancel(z_task_t *task) { return -1; } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { return -1; } +int8_t z_mutex_init(z_mutex_t *m) { return -1; } -int8_t zp_mutex_free(zp_mutex_t *m) { return -1; } +int8_t z_mutex_free(z_mutex_t *m) { return -1; } -int8_t zp_mutex_lock(zp_mutex_t *m) { return -1; } +int8_t z_mutex_lock(z_mutex_t *m) { return -1; } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return -1; } +int8_t z_mutex_trylock(z_mutex_t *m) { return -1; } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return -1; } +int8_t z_mutex_unlock(z_mutex_t *m) { return -1; } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return -1; } +int8_t z_condvar_init(z_condvar_t *cv) { return -1; } -int8_t zp_condvar_free(zp_condvar_t *cv) { return -1; } +int8_t z_condvar_free(z_condvar_t *cv) { return -1; } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return -1; } +int8_t z_condvar_signal(z_condvar_t *cv) { return -1; } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return -1; } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return -1; } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { +int z_sleep_us(size_t time) { delay_us(time); return 0; } -int zp_sleep_ms(size_t time) { +int z_sleep_ms(size_t time) { delay_ms(time); return 0; } -int zp_sleep_s(size_t time) { - zp_time_t start = zp_time_now(); +int z_sleep_s(size_t time) { + z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. // This may compound, so this approach may make sleeps longer than expected. // This extra check tries to minimize the amount of extra time it might sleep. - while (zp_time_elapsed_s(&start) < time) { - zp_sleep_ms(1000); + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); } return 0; } /*------------------ Instant ------------------*/ -void __zp_clock_gettime(zp_clock_t *ts) { +void __z_clock_gettime(z_clock_t *ts) { uint64_t m = millis(); ts->tv_sec = m / (uint64_t)1000000; ts->tv_nsec = (m % (uint64_t)1000000) * (uint64_t)1000; } -zp_clock_t zp_clock_now(void) { - zp_clock_t now; - __zp_clock_gettime(&now); +z_clock_t z_clock_now(void) { + z_clock_t now; + __z_clock_gettime(&now); return now; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { - zp_clock_t now; - __zp_clock_gettime(&now); +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; + __z_clock_gettime(&now); unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); return elapsed; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { - zp_clock_t now; - __zp_clock_gettime(&now); +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now; + __z_clock_gettime(&now); unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); return elapsed; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { - zp_clock_t now; - __zp_clock_gettime(&now); +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_clock_t now; + __z_clock_gettime(&now); unsigned long elapsed = now.tv_sec - instant->tv_sec; return elapsed; } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; gettimeofday(&now, NULL); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.tv_sec); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/system/emscripten/network.c b/src/system/emscripten/network.c index 262217495..4db1dadb3 100644 --- a/src/system/emscripten/network.c +++ b/src/system/emscripten/network.c @@ -83,7 +83,7 @@ int8_t _z_open_ws(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, ui // WARNING: workaround as connect returns before the websocket is // actually open. - zp_sleep_ms(100); + z_sleep_ms(100); if (ret != _Z_RES_OK) { close(sock->_ws._fd); @@ -111,14 +111,13 @@ void _z_close_ws(_z_sys_net_socket_t *sock) { close(sock->_ws._fd); } size_t _z_read_ws(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { // WARNING: workaroud implementing here the timeout ssize_t rb = 0; - zp_time_t start = zp_time_now(); - while ((zp_time_elapsed_ms(&start) < sock._ws._tout) && (rb <= 0)) { - zp_sleep_ms(WS_LINK_SLEEP); // WARNING: workaround need to give the hand to the emscripten threads + z_time_t start = z_time_now(); + while ((z_time_elapsed_ms(&start) < sock._ws._tout) && (rb <= 0)) { + z_sleep_ms(WS_LINK_SLEEP); // WARNING: workaround need to give the hand to the emscripten threads rb = recv(sock._ws._fd, ptr, len, 0); } - // WARNING: workaround as the recv returns -1 not only in case of errors if (rb < 0) { - rb = 0; + rb = SIZE_MAX; } return rb; } @@ -144,9 +143,9 @@ size_t _z_read_exact_ws(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len size_t _z_send_ws(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) { // WARNING: workaroud implementing here the timeout ssize_t sb = 0; - zp_time_t start = zp_time_now(); - while ((zp_time_elapsed_ms(&start) < sock._ws._tout) && (sb <= 0)) { - zp_sleep_ms(WS_LINK_SLEEP); // WARNING: workaround need to give the hand to the emscripten threads + z_time_t start = z_time_now(); + while ((z_time_elapsed_ms(&start) < sock._ws._tout) && (sb <= 0)) { + z_sleep_ms(WS_LINK_SLEEP); // WARNING: workaround need to give the hand to the emscripten threads sb = send(sock._ws._fd, ptr, len, 0); } // WARNING: workaround as the recv returns -1 not only in case of errors diff --git a/src/system/emscripten/system.c b/src/system/emscripten/system.c index dc2396575..121724269 100644 --- a/src/system/emscripten/system.c +++ b/src/system/emscripten/system.c @@ -21,112 +21,112 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return (emscripten_random() * 255.0); } +uint8_t z_random_u8(void) { return (emscripten_random() * 255.0); } -uint16_t zp_random_u16(void) { return (emscripten_random() * 65535.0); } +uint16_t z_random_u16(void) { return (emscripten_random() * 65535.0); } -uint32_t zp_random_u32(void) { return (emscripten_random() * 4294967295.0); } +uint32_t z_random_u32(void) { return (emscripten_random() * 4294967295.0); } -uint64_t zp_random_u64(void) { return (emscripten_random() * 18446744073709551615.0); } +uint64_t z_random_u64(void) { return (emscripten_random() * 18446744073709551615.0); } -void zp_random_fill(void *buf, size_t len) { +void z_random_fill(void *buf, size_t len) { for (size_t i = 0; i < len; i++) { - *((uint8_t *)buf) = zp_random_u8(); + *((uint8_t *)buf) = z_random_u8(); } } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return malloc(size); } +void *z_malloc(size_t size) { return malloc(size); } -void *zp_realloc(void *ptr, size_t size) { return realloc(ptr, size); } +void *z_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -void zp_free(void *ptr) { free(ptr); } +void z_free(void *ptr) { free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, pthread_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t z_task_init(z_task_t *task, pthread_attr_t *attr, void *(*fun)(void *), void *arg) { return pthread_create(task, attr, fun, arg); } -int8_t zp_task_join(zp_task_t *task) { return pthread_join(*task, NULL); } +int8_t z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } -int8_t zp_task_cancel(zp_task_t *task) { return pthread_cancel(*task); } +int8_t zp_task_cancel(z_task_t *task) { return pthread_cancel(*task); } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } -int8_t zp_mutex_free(zp_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t zp_mutex_lock(zp_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } -int8_t zp_condvar_free(zp_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { +int z_sleep_us(size_t time) { emscripten_sleep((time / 1000) + (time % 1000 == 0 ? 0 : 1)); return 0; } -int zp_sleep_ms(size_t time) { +int z_sleep_ms(size_t time) { emscripten_sleep(time); return 0; } -int zp_sleep_s(size_t time) { - zp_time_t start = zp_time_now(); +int z_sleep_s(size_t time) { + z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. // This may compound, so this approach may make sleeps longer than expected. // This extra check tries to minimize the amount of extra time it might sleep. - while (zp_time_elapsed_s(&start) < time) { - zp_sleep_ms(1000); + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); } return 0; } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { return zp_time_now(); } +z_clock_t z_clock_now(void) { return z_time_now(); } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { return zp_clock_elapsed_ms(instant) * 1000; } +unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms(instant) * 1000; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { return zp_time_elapsed_ms(instant); } +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { return z_time_elapsed_ms(instant); } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { return zp_time_elapsed_ms(instant) * 1000; } +unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_time_elapsed_ms(instant) * 1000; } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { return emscripten_get_now(); } +z_time_t z_time_now(void) { return emscripten_get_now(); } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - snprintf(buf, buflen, "%f", zp_time_now()); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + snprintf(buf, buflen, "%f", z_time_now()); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { return zp_time_elapsed_ms(time) * 1000; } +unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now = emscripten_get_now(); +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now = emscripten_get_now(); unsigned long elapsed = now - *time; return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { return zp_time_elapsed_ms(time) * 1000; } +unsigned long z_time_elapsed_s(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } diff --git a/src/system/espidf/network.c b/src/system/espidf/network.c index f11d60658..5ae14373f 100644 --- a/src/system/espidf/network.c +++ b/src/system/espidf/network.c @@ -167,7 +167,7 @@ int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_ sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -242,7 +242,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin struct sockaddr *lsockaddr = NULL; socklen_t addrlen = 0; if (rep._iptcp->ai_family == AF_INET) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); @@ -255,7 +255,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin ret = _Z_ERR_GENERIC; } } else if (rep._iptcp->ai_family == AF_INET6) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in6)); addrlen = sizeof(struct sockaddr_in6); @@ -275,7 +275,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -308,7 +308,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // Create laddr endpoint if (ret == _Z_RES_OK) { - struct addrinfo *laddr = (struct addrinfo *)zp_malloc(sizeof(struct addrinfo)); + struct addrinfo *laddr = (struct addrinfo *)z_malloc(sizeof(struct addrinfo)); if (laddr != NULL) { laddr->ai_flags = 0; laddr->ai_family = rep._iptcp->ai_family; @@ -332,7 +332,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } if (ret != _Z_RES_OK) { - zp_free(lsockaddr); + z_free(lsockaddr); } } else { ret = _Z_ERR_GENERIC; @@ -349,7 +349,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo struct sockaddr *lsockaddr = NULL; unsigned int addrlen = 0; if (rep._iptcp->ai_family == AF_INET) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); @@ -362,7 +362,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } } else if (rep._iptcp->ai_family == AF_INET6) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in6)); addrlen = sizeof(struct sockaddr_in6); @@ -383,7 +383,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -434,7 +434,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } - zp_free(lsockaddr); + z_free(lsockaddr); } else { ret = _Z_ERR_GENERIC; } @@ -593,7 +593,9 @@ int8_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t ba const int uart_buffer_size = (1024 * 2); QueueHandle_t uart_queue; uart_driver_install(sock->_serial, uart_buffer_size, 0, 100, &uart_queue, 0); - + uart_flush_input(sock->_serial); + sock->after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); + sock->before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); return ret; } @@ -622,65 +624,74 @@ int8_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t return ret; } -void _z_close_serial(_z_sys_net_socket_t *sock) { uart_driver_delete(sock->_serial); } +void _z_close_serial(_z_sys_net_socket_t *sock) { + uart_wait_tx_done(sock->_serial, 1000); + uart_flush(sock->_serial); + uart_driver_delete(sock->_serial); + z_free(sock->after_cobs); + z_free(sock->before_cobs); +} size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { int8_t ret = _Z_RES_OK; - uint8_t *before_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; - for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { - size_t len = 0; - do { - uart_get_buffered_data_len(sock._serial, &len); - if (len < 1) { - zp_sleep_ms(10); // FIXME: Yield by sleeping. - } else { - break; + while (rb < _Z_SERIAL_MAX_COBS_BUF_SIZE) { + int r = uart_read_bytes(sock._serial, &sock.before_cobs[rb], 1, 1000); + if (r == 0) { + _Z_DEBUG("Timeout reading from serial"); + if (rb == 0) { + ret = _Z_ERR_GENERIC; } - } while (1); - uart_read_bytes(sock._serial, &before_cobs[i], 1, 100); - rb = rb + (size_t)1; - if (before_cobs[i] == (uint8_t)0x00) { break; + } else if (r == 1) { + rb = rb + (size_t)1; + if (sock.before_cobs[rb - 1] == (uint8_t)0x00) { + break; + } + } else { + _Z_ERROR("Error reading from serial"); + ret = _Z_ERR_GENERIC; } } - - uint8_t *after_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MFS_SIZE); - size_t trb = _z_cobs_decode(before_cobs, rb, after_cobs); - - size_t i = 0; uint16_t payload_len = 0; - for (i = 0; i < sizeof(payload_len); i++) { - payload_len |= (after_cobs[i] << ((uint8_t)i * (uint8_t)8)); - } - - if (trb == (size_t)(payload_len + (uint16_t)6)) { - (void)memcpy(ptr, &after_cobs[i], payload_len); - i = i + (size_t)payload_len; - uint32_t crc = 0; - for (uint8_t j = 0; j < sizeof(crc); j++) { - crc |= (uint32_t)(after_cobs[i] << (j * (uint8_t)8)); - i = i + (size_t)1; + if (ret == _Z_RES_OK) { + _Z_DEBUG("Read %u bytes from serial", rb); + size_t trb = _z_cobs_decode(sock.before_cobs, rb, sock.after_cobs); + _Z_DEBUG("Decoded %u bytes from serial", trb); + size_t i = 0; + for (i = 0; i < sizeof(payload_len); i++) { + payload_len |= (sock.after_cobs[i] << ((uint8_t)i * (uint8_t)8)); } + _Z_DEBUG("payload_len = %u <= %X %X", payload_len, sock.after_cobs[1], sock.after_cobs[0]); + + if (trb == (size_t)(payload_len + (uint16_t)6)) { + (void)memcpy(ptr, &sock.after_cobs[i], payload_len); + i = i + (size_t)payload_len; - uint32_t c_crc = _z_crc32(ptr, payload_len); - if (c_crc != crc) { + uint32_t crc = 0; + for (uint8_t j = 0; j < sizeof(crc); j++) { + crc |= (uint32_t)(sock.after_cobs[i] << (j * (uint8_t)8)); + i = i + (size_t)1; + } + + uint32_t c_crc = _z_crc32(ptr, payload_len); + if (c_crc != crc) { + _Z_ERROR("CRC mismatch: %d != %d ", c_crc, crc); + ret = _Z_ERR_GENERIC; + } + } else { + _Z_ERROR("length mismatch => %d <> %d ", trb, payload_len + (uint16_t)6); ret = _Z_ERR_GENERIC; } - } else { - ret = _Z_ERR_GENERIC; } - zp_free(before_cobs); - zp_free(after_cobs); - rb = payload_len; if (ret != _Z_RES_OK) { rb = SIZE_MAX; } - + _Z_DEBUG("return _z_read_serial() = %d ", rb); return rb; } @@ -702,7 +713,7 @@ size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) { int8_t ret = _Z_RES_OK; - uint8_t *before_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); size_t i = 0; for (i = 0; i < sizeof(uint16_t); ++i) { before_cobs[i] = (len >> (i * (size_t)8)) & (size_t)0XFF; @@ -717,7 +728,7 @@ size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t i = i + (size_t)1; } - uint8_t *after_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs); after_cobs[twb] = 0x00; // Manually add the COBS delimiter ssize_t wb = uart_write_bytes(sock._serial, after_cobs, twb + (ssize_t)1); @@ -725,8 +736,8 @@ size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t ret = _Z_ERR_GENERIC; } - zp_free(before_cobs); - zp_free(after_cobs); + z_free(before_cobs); + z_free(after_cobs); size_t sb = len; if (ret != _Z_RES_OK) { diff --git a/src/system/espidf/system.c b/src/system/espidf/system.c index 12633ad2b..e6d3d018f 100644 --- a/src/system/espidf/system.c +++ b/src/system/espidf/system.c @@ -21,143 +21,174 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return zp_random_u32(); } +uint8_t z_random_u8(void) { return z_random_u32(); } -uint16_t zp_random_u16(void) { return zp_random_u32(); } +uint16_t z_random_u16(void) { return z_random_u32(); } -uint32_t zp_random_u32(void) { return esp_random(); } +uint32_t z_random_u32(void) { return esp_random(); } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; - ret |= zp_random_u32(); + ret |= z_random_u32(); ret = ret << 32; - ret |= zp_random_u32(); + ret |= z_random_u32(); return ret; } -void zp_random_fill(void *buf, size_t len) { esp_fill_random(buf, len); } +void z_random_fill(void *buf, size_t len) { esp_fill_random(buf, len); } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return heap_caps_malloc(size, MALLOC_CAP_8BIT); } +void *z_malloc(size_t size) { return heap_caps_malloc(size, MALLOC_CAP_8BIT); } -void *zp_realloc(void *ptr, size_t size) { return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT); } +void *z_realloc(void *ptr, size_t size) { return heap_caps_realloc(ptr, size, MALLOC_CAP_8BIT); } -void zp_free(void *ptr) { heap_caps_free(ptr); } +void z_free(void *ptr) { heap_caps_free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 // This wrapper is only used for ESP32. // In FreeRTOS, tasks created using xTaskCreate must end with vTaskDelete. // A task function should __not__ simply return. typedef struct { - void *(*_fun)(void *); - void *_arg; + void *(*fun)(void *); + void *arg; + EventGroupHandle_t join_event; } z_task_arg; -void z_task_wrapper(z_task_arg *targ) { - targ->_fun(targ->_arg); +static void z_task_wrapper(void *arg) { + z_task_arg *targ = (z_task_arg *)arg; + targ->fun(targ->arg); + xEventGroupSetBits(targ->join_event, 1); vTaskDelete(NULL); - zp_free(targ); } -/*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { - int ret = 0; +static z_task_attr_t z_default_task_attr = { + .name = "", + .priority = configMAX_PRIORITIES / 2, + .stack_depth = 5120, +#if (configSUPPORT_STATIC_ALLOCATION == 1) + .static_allocation = false, + .stack_buffer = NULL, + .task_buffer = NULL, +#endif /* SUPPORT_STATIC_ALLOCATION */ +}; + +/*------------------ Thread ------------------*/ +int8_t z_task_init(z_task_t *task, z_task_attr_t *arg_attr, void *(*fun)(void *), void *arg) { + z_task_attr_t *attr = arg_attr; + z_task_arg *z_arg = (z_task_arg *)z_malloc(sizeof(z_task_arg)); + if (z_arg == NULL) { + return -1; + } + + z_arg->fun = fun; + z_arg->arg = arg; + task->join_event = xEventGroupCreate(); + z_arg->join_event = task->join_event; + + if (attr == NULL) { + attr = &z_default_task_attr; + } - z_task_arg *z_arg = (z_task_arg *)zp_malloc(sizeof(z_task_arg)); - if (z_arg != NULL) { - z_arg->_fun = fun; - z_arg->_arg = arg; - if (xTaskCreate((void *)z_task_wrapper, "", 5120, z_arg, configMAX_PRIORITIES / 2, task) != pdPASS) { - ret = -1; +#if (configSUPPORT_STATIC_ALLOCATION == 1) + if (attr->static_allocation) { + task->handle = xTaskCreateStatic(z_task_wrapper, attr->name, attr->stack_depth, z_arg, attr->priority, + attr->stack_buffer, attr->task_buffer); + if (task->handle == NULL) { + return -1; } } else { - ret = -1; +#endif /* SUPPORT_STATIC_ALLOCATION */ + if (xTaskCreate(z_task_wrapper, attr->name, attr->stack_depth, z_arg, attr->priority, &task->handle) != + pdPASS) { + return -1; + } +#if (configSUPPORT_STATIC_ALLOCATION == 1) } +#endif /* SUPPORT_STATIC_ALLOCATION */ - return ret; + return 0; } -int8_t zp_task_join(zp_task_t *task) { - // Note: task/thread join not supported on FreeRTOS API, so we force its deletion instead. - return zp_task_cancel(task); +int8_t z_task_join(z_task_t *task) { + xEventGroupWaitBits(task->join_event, 1, pdFALSE, pdFALSE, portMAX_DELAY); + return 0; } -int8_t zp_task_cancel(zp_task_t *task) { - vTaskDelete(*task); +int8_t z_task_cancel(z_task_t *task) { + vTaskDelete(task->handle); return 0; } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); - *task = NULL; +void z_task_free(z_task_t **task) { + z_free((*task)->join_event); + z_free(*task); } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { return pthread_mutex_init(m, NULL); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, NULL); } -int8_t zp_mutex_free(zp_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t zp_mutex_lock(zp_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return pthread_cond_init(cv, NULL); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, NULL); } -int8_t zp_condvar_free(zp_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep(time); } -int zp_sleep_ms(size_t time) { - zp_time_t start = zp_time_now(); +int z_sleep_ms(size_t time) { + z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. // This may compound, so this approach may make sleeps longer than expected. // This extra check tries to minimize the amount of extra time it might sleep. - while (zp_time_elapsed_ms(&start) < time) { - zp_sleep_us(1000); + while (z_time_elapsed_ms(&start) < time) { + vTaskDelay(1 / portTICK_PERIOD_MS); } return 0; } -int zp_sleep_s(size_t time) { return sleep(time); } +int z_sleep_s(size_t time) { return sleep(time); } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { - zp_clock_t now; +z_clock_t z_clock_now(void) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); return now; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); return elapsed; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); return elapsed; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = now.tv_sec - instant->tv_sec; @@ -165,38 +196,38 @@ unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; gettimeofday(&now, NULL); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.tv_sec); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/system/flipper/network.c b/src/system/flipper/network.c new file mode 100644 index 000000000..7067ae27f --- /dev/null +++ b/src/system/flipper/network.c @@ -0,0 +1,225 @@ +// +// 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 "zenoh-pico/config.h" +#include "zenoh-pico/system/link/serial.h" +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/logging.h" + +#if Z_FEATURE_LINK_TCP == 1 +#error "TCP is not supported on Flipper port of Zenoh-Pico" +#endif + +#if Z_FEATURE_LINK_UDP_UNICAST == 1 || Z_FEATURE_LINK_UDP_MULTICAST == 1 +#error "UDP is not supported on Flipper port of Zenoh-Pico" +#endif + +#if Z_FEATURE_LINK_SERIAL == 1 + +/*------------------ Serial sockets ------------------*/ + +static void _z_serial_received_byte_callback(FuriHalSerialHandle* handle, FuriHalSerialRxEvent event, void* context) { + if (!context) { + return; + } + FuriStreamBuffer* buf = context; + + if (event == FuriHalSerialRxEventData) { + uint8_t data = furi_hal_serial_async_rx(handle); + furi_stream_buffer_send(buf, (void*)&data, 1, FLIPPER_SERIAL_STREAM_TRIGGERED_LEVEL); + } +} + +int8_t _z_open_serial_from_pins(_z_sys_net_socket_t* sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) { + return _Z_ERR_GENERIC; +} + +int8_t _z_open_serial_from_dev(_z_sys_net_socket_t* sock, char* dev, uint32_t baudrate) { + if (furi_hal_serial_control_is_busy(FuriHalSerialIdUsart)) { + _Z_ERROR("Serial port is busy"); + return _Z_ERR_TRANSPORT_OPEN_FAILED; + } + + FuriHalSerialId sid; + if (!strcmp(dev, "usart")) { + sid = FuriHalSerialIdUsart; + } else if (!strcmp(dev, "lpuart")) { + sid = FuriHalSerialIdLpuart; + } else { + _Z_ERROR("Unknown serial port device: %s", dev); + return _Z_ERR_TRANSPORT_OPEN_FAILED; + } + + sock->_serial = furi_hal_serial_control_acquire(sid); + if (!sock->_serial) { + _Z_ERROR("Serial port control acquire failed"); + return _Z_ERR_TRANSPORT_OPEN_FAILED; + }; + furi_hal_serial_init(sock->_serial, baudrate); + + sock->_rx_stream = + furi_stream_buffer_alloc(FLIPPER_SERIAL_STREAM_BUFFER_SIZE, FLIPPER_SERIAL_STREAM_TRIGGERED_LEVEL); + if (!sock->_rx_stream) { + _Z_ERROR("Serial stream buffer allocation failed"); + return _Z_ERR_TRANSPORT_NO_SPACE; + }; + furi_hal_serial_async_rx_start(sock->_serial, _z_serial_received_byte_callback, sock->_rx_stream, false); + + _Z_DEBUG("Serial port opened: %s (%li)", dev, baudrate); + return _Z_RES_OK; +} + +int8_t _z_listen_serial_from_pins(_z_sys_net_socket_t* sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) { + (void)(sock); + (void)(txpin); + (void)(rxpin); + (void)(baudrate); + + return _Z_ERR_GENERIC; +} + +int8_t _z_listen_serial_from_dev(_z_sys_net_socket_t* sock, char* dev, uint32_t baudrate) { + (void)(sock); + (void)(dev); + (void)(baudrate); + + // @TODO: To be implemented + + return _Z_ERR_GENERIC; +} + +void _z_close_serial(_z_sys_net_socket_t* sock) { + if (sock->_serial) { + furi_hal_serial_async_rx_stop(sock->_serial); + furi_hal_serial_deinit(sock->_serial); + furi_hal_serial_control_release(sock->_serial); + + // Wait until the serial read timeout ends + z_sleep_ms(FLIPPER_SERIAL_TIMEOUT_MS * 2); + + furi_stream_buffer_free(sock->_rx_stream); + + sock->_serial = 0; + sock->_rx_stream = 0; + } + _Z_DEBUG("Serial port closed"); +} + +size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t* ptr, size_t len) { + int8_t ret = _Z_RES_OK; + + uint8_t* before_cobs = (uint8_t*)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t rb = 0; + for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { + size_t len = 0; + len = furi_stream_buffer_receive(sock._rx_stream, &before_cobs[i], 1, FLIPPER_SERIAL_TIMEOUT_MS); + if (!len) { + z_free(before_cobs); + return SIZE_MAX; + } + rb += 1; + + if (before_cobs[i] == (uint8_t)0x00) { + break; + } + } + + uint8_t* after_cobs = (uint8_t*)z_malloc(_Z_SERIAL_MFS_SIZE); + size_t trb = _z_cobs_decode(before_cobs, rb, after_cobs); + + size_t i = 0; + uint16_t payload_len = 0; + for (i = 0; i < sizeof(payload_len); i++) { + payload_len |= (after_cobs[i] << ((uint8_t)i * (uint8_t)8)); + } + + if (trb == (size_t)(payload_len + (uint16_t)6)) { + (void)memcpy(ptr, &after_cobs[i], payload_len); + i += (size_t)payload_len; + + uint32_t crc = 0; + for (uint8_t j = 0; j < sizeof(crc); j++) { + crc |= (uint32_t)(after_cobs[i] << (j * (uint8_t)8)); + i += 1; + } + + uint32_t c_crc = _z_crc32(ptr, payload_len); + if (c_crc != crc) { + ret = _Z_ERR_GENERIC; + } + } else { + ret = _Z_ERR_GENERIC; + } + + z_free(before_cobs); + z_free(after_cobs); + + rb = payload_len; + if (ret != _Z_RES_OK) { + rb = SIZE_MAX; + } + + return rb; +} + +size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t* ptr, size_t len) { + size_t n = len; + + do { + size_t rb = _z_read_serial(sock, ptr, n); + if (rb == SIZE_MAX) { + return rb; + } + n -= rb; + ptr += len - n; + } while (n > 0); + + return len; +} + +size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t* ptr, size_t len) { + int8_t ret = _Z_RES_OK; + + uint8_t* before_cobs = (uint8_t*)z_malloc(_Z_SERIAL_MFS_SIZE); + size_t i = 0; + for (i = 0; i < sizeof(uint16_t); ++i) { + before_cobs[i] = (len >> (i * (size_t)8)) & (size_t)0XFF; + } + + (void)memcpy(&before_cobs[i], ptr, len); + i += len; + + uint32_t crc = _z_crc32(ptr, len); + for (uint8_t j = 0; j < sizeof(crc); j++) { + before_cobs[i] = (crc >> (j * (uint8_t)8)) & (uint32_t)0XFF; + i++; + } + + uint8_t* after_cobs = (uint8_t*)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs); + after_cobs[twb] = 0x00; // Manually add the COBS delimiter + + furi_hal_serial_tx(sock._serial, after_cobs, twb + (ssize_t)1); + furi_hal_serial_tx_wait_complete(sock._serial); + + z_free(before_cobs); + z_free(after_cobs); + + return len; +} +#endif + +#if Z_FEATURE_RAWETH_TRANSPORT == 1 +#error "Raw ethernet transport not supported on Flipper port of Zenoh-Pico" +#endif diff --git a/src/system/flipper/system.c b/src/system/flipper/system.c new file mode 100644 index 000000000..e723f6aa5 --- /dev/null +++ b/src/system/flipper/system.c @@ -0,0 +1,295 @@ +// +// 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 +#include + +#include "zenoh-pico/config.h" +#include "zenoh-pico/system/platform.h" +#include "zenoh-pico/utils/result.h" + +/*------------------ Random ------------------*/ +uint8_t z_random_u8(void) { return random(); } + +uint16_t z_random_u16(void) { return random(); } + +uint32_t z_random_u32(void) { return random(); } + +uint64_t z_random_u64(void) { + uint64_t ret = 0; + ret |= z_random_u32(); + ret = ret << 32; + ret |= z_random_u32(); + + return ret; +} + +void z_random_fill(void* buf, size_t len) { + for (size_t i = 0; i < len; i++) { + ((uint8_t*)buf)[i] = z_random_u8(); + } +} + +/*------------------ Memory ------------------*/ +void* z_malloc(size_t size) { + if (!size) { + return NULL; + } + return malloc(size); +} + +void* z_realloc(void* ptr, size_t size) { + if (!size) { + free(ptr); + return NULL; + } + return realloc(ptr, size); +} + +void z_free(void* ptr) { return free(ptr); } + +/*------------------ Task ------------------*/ + +int8_t z_task_init(z_task_t* task, z_task_attr_t* attr, void* (*fun)(void*), void* arg) { + if (task == NULL) { + return -1; + } + + uint32_t stack_size = FLIPPER_DEFAULT_THREAD_STACK_SIZE; + if (attr) { + stack_size = *attr; + } + + *task = furi_thread_alloc_ex(NULL, stack_size, (FuriThreadCallback)fun, arg); + if (*task == NULL) { + return -1; + } + furi_thread_start(*task); + return _Z_RES_OK; +} + +int8_t z_task_join(z_task_t* task) { + if (task == NULL) { + return -1; + } + return furi_thread_join(*task); +} + +int8_t zp_task_cancel(z_task_t* task) { return -1; } + +void z_task_free(z_task_t** task) { + if (task == NULL || *task == NULL) { + return; + } + furi_thread_free(**task); + *task = NULL; +} + +/*------------------ Mutex ------------------*/ +int8_t z_mutex_init(z_mutex_t* m) { + if (m == NULL) { + return -1; + } + *m = furi_mutex_alloc(FuriMutexTypeRecursive); + return (*m != 0) ? _Z_RES_OK : _Z_ERR_SYSTEM_TASK_FAILED; +} + +int8_t z_mutex_free(z_mutex_t* m) { + if (m == NULL) { + return -1; + } + if (*m == 0) { + return 0; + } + furi_mutex_free(*m); + *m = NULL; + return 0; +} + +int8_t z_mutex_lock(z_mutex_t* m) { + if (m == NULL) { + return -1; + } + if (*m == 0) { + return 0; + } + return furi_mutex_acquire(*m, FuriWaitForever); +} + +int8_t z_mutex_trylock(z_mutex_t* m) { return -1; } + +int8_t z_mutex_unlock(z_mutex_t* m) { + if (m == NULL) { + return -1; + } + if (*m == 0) { + return 0; + } + return furi_mutex_release(*m); +} + +/*------------------ Condvar ------------------*/ +int8_t z_condvar_init(z_condvar_t* cv) { return -1; } + +int8_t z_condvar_free(z_condvar_t* cv) { return -1; } + +int8_t z_condvar_signal(z_condvar_t* cv) { return -1; } + +int8_t z_condvar_wait(z_condvar_t* cv, z_mutex_t* m) { return -1; } + +/*------------------ Sleep ------------------*/ +int z_sleep_us(size_t time) { + furi_delay_us(time); + return 0; +} + +int z_sleep_ms(size_t time) { + furi_delay_ms(time); + return 0; +} + +int z_sleep_s(size_t time) { + z_time_t start = z_time_now(); + + // Most sleep APIs promise to sleep at least whatever you asked them to. + // This may compound, so this approach may make sleeps longer than expected. + // This extra check tries to minimize the amount of extra time it might sleep. + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); + } + + return 0; +} + +/*------------------ Instant ------------------*/ +void __z_clock_gettime(z_clock_t* ts) { + uint64_t m = millis(); + ts->tv_sec = m / (uint64_t)1000000; + ts->tv_nsec = (m % (uint64_t)1000000) * (uint64_t)1000; +} + +z_clock_t z_clock_now(void) { + z_clock_t now; + __z_clock_gettime(&now); + return now; +} + +unsigned long z_clock_elapsed_us(z_clock_t* instant) { + z_clock_t now; + __z_clock_gettime(&now); + + unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); + return elapsed; +} + +unsigned long z_clock_elapsed_ms(z_clock_t* instant) { + z_clock_t now; + __z_clock_gettime(&now); + + unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); + return elapsed; +} + +unsigned long z_clock_elapsed_s(z_clock_t* instant) { + z_clock_t now; + __z_clock_gettime(&now); + + unsigned long elapsed = now.tv_sec - instant->tv_sec; + return elapsed; +} + +/*------------------ Time ------------------*/ +z_time_t z_time_now(void) { + z_time_t now; + gettimeofday(&now, NULL); + return now; +} + +const char* z_time_now_as_str(char* const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); + struct tm ts; + ts = *localtime(&tv.tv_sec); + strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); + return buf; +} + +unsigned long z_time_elapsed_us(z_time_t* time) { + z_time_t now; + gettimeofday(&now, NULL); + + unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); + return elapsed; +} + +unsigned long z_time_elapsed_ms(z_time_t* time) { + z_time_t now; + gettimeofday(&now, NULL); + + unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); + return elapsed; +} + +unsigned long z_time_elapsed_s(z_time_t* time) { + z_time_t now; + gettimeofday(&now, NULL); + + unsigned long elapsed = now.tv_sec - time->tv_sec; + return elapsed; +} + +char* strncat(char* dest, const char* src, size_t dest_size) { + size_t dest_len = strlen(dest); + size_t i; + + for (i = 0; i < dest_size && src[i] != '\0'; i++) { + dest[dest_len + i] = src[i]; + } + dest[dest_len + i] = '\0'; + + return dest; +} + +char* strpbrk(const char* str, const char* charset) { + while (*str != '\0') { + const char* c = charset; + while (*c != '\0') { + if (*str == *c) { + return (char*)str; + } + c++; + } + str++; + } + return NULL; +} + +size_t strftime(char* s, size_t max, const char* format, const struct tm* tm) { + // not supported + s[0] = 0; + return 0; +} + +int gettimeofday(struct timeval* __restrict __p, void* __restrict __tz) { + // not supported + __p->tv_sec = 0; + __p->tv_usec = 0; + return 0; +} + +struct tm* localtime(const time_t* timep) { + // not supported + static struct tm t; + return &t; +} diff --git a/src/system/freertos_plus_tcp/system.c b/src/system/freertos_plus_tcp/system.c index a2d17992a..025ccc44a 100644 --- a/src/system/freertos_plus_tcp/system.c +++ b/src/system/freertos_plus_tcp/system.c @@ -23,39 +23,39 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return zp_random_u32(); } +uint8_t z_random_u8(void) { return z_random_u32(); } -uint16_t zp_random_u16(void) { return zp_random_u32(); } +uint16_t z_random_u16(void) { return z_random_u32(); } -uint32_t zp_random_u32(void) { +uint32_t z_random_u32(void) { uint32_t ret = 0; xApplicationGetRandomNumber(&ret); return ret; } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; - ret |= zp_random_u32(); + ret |= z_random_u32(); ret = ret << 32; - ret |= zp_random_u32(); + ret |= z_random_u32(); return ret; } -void zp_random_fill(void *buf, size_t len) { +void z_random_fill(void *buf, size_t len) { for (size_t i = 0; i < len; i++) { - *((uint8_t *)buf) = zp_random_u8(); + *((uint8_t *)buf) = z_random_u8(); } } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return pvPortMalloc(size); } +void *z_malloc(size_t size) { return pvPortMalloc(size); } -void *zp_realloc(void *ptr, size_t size) { +void *z_realloc(void *ptr, size_t size) { // realloc not implemented in FreeRTOS return NULL; } -void zp_free(void *ptr) { vPortFree(ptr); } +void z_free(void *ptr) { vPortFree(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 // In FreeRTOS, tasks created using xTaskCreate must end with vTaskDelete. @@ -73,7 +73,7 @@ static void z_task_wrapper(void *arg) { vTaskDelete(NULL); } -static zp_task_attr_t z_default_task_attr = { +static z_task_attr_t z_default_task_attr = { .name = "", .priority = configMAX_PRIORITIES / 2, .stack_depth = 5120, @@ -85,8 +85,8 @@ static zp_task_attr_t z_default_task_attr = { }; /*------------------ Thread ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { - z_task_arg *z_arg = (z_task_arg *)zp_malloc(sizeof(z_task_arg)); +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { + z_task_arg *z_arg = (z_task_arg *)z_malloc(sizeof(z_task_arg)); if (z_arg == NULL) { return -1; } @@ -119,86 +119,86 @@ int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), return 0; } -int8_t zp_task_join(zp_task_t *task) { +int8_t z_task_join(z_task_t *task) { xEventGroupWaitBits(task->join_event, 1, pdFALSE, pdFALSE, portMAX_DELAY); return 0; } -int8_t zp_task_cancel(zp_task_t *task) { +int8_t zp_task_cancel(z_task_t *task) { vTaskDelete(task->handle); return 0; } -void zp_task_free(zp_task_t **task) { - zp_free((*task)->join_event); - zp_free(*task); +void z_task_free(z_task_t **task) { + z_free((*task)->join_event); + z_free(*task); } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { +int8_t z_mutex_init(z_mutex_t *m) { *m = xSemaphoreCreateRecursiveMutex(); return *m == NULL ? -1 : 0; } -int8_t zp_mutex_free(zp_mutex_t *m) { - zp_free(*m); +int8_t z_mutex_free(z_mutex_t *m) { + z_free(*m); return 0; } -int8_t zp_mutex_lock(zp_mutex_t *m) { return xSemaphoreTakeRecursive(*m, portMAX_DELAY) == pdTRUE ? 0 : -1; } +int8_t z_mutex_lock(z_mutex_t *m) { return xSemaphoreTakeRecursive(*m, portMAX_DELAY) == pdTRUE ? 0 : -1; } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return xSemaphoreTakeRecursive(*m, 0) == pdTRUE ? 0 : -1; } +int8_t z_mutex_trylock(z_mutex_t *m) { return xSemaphoreTakeRecursive(*m, 0) == pdTRUE ? 0 : -1; } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return xSemaphoreGiveRecursive(*m) == pdTRUE ? 0 : -1; } +int8_t z_mutex_unlock(z_mutex_t *m) { return xSemaphoreGiveRecursive(*m) == pdTRUE ? 0 : -1; } /*------------------ CondVar ------------------*/ // Condition variables not supported in FreeRTOS -int8_t zp_condvar_init(zp_condvar_t *cv) { return -1; } -int8_t zp_condvar_free(zp_condvar_t *cv) { return -1; } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return -1; } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return -1; } +int8_t z_condvar_init(z_condvar_t *cv) { return -1; } +int8_t z_condvar_free(z_condvar_t *cv) { return -1; } +int8_t z_condvar_signal(z_condvar_t *cv) { return -1; } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return -1; } #endif // Z_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { +int z_sleep_us(size_t time) { vTaskDelay(pdMS_TO_TICKS(time / 1000)); return 0; } -int zp_sleep_ms(size_t time) { +int z_sleep_ms(size_t time) { vTaskDelay(pdMS_TO_TICKS(time)); return 0; } -int zp_sleep_s(size_t time) { +int z_sleep_s(size_t time) { vTaskDelay(pdMS_TO_TICKS(time * 1000)); return 0; } /*------------------ Clock ------------------*/ -zp_clock_t zp_clock_now(void) { return zp_time_now(); } +z_clock_t z_clock_now(void) { return z_time_now(); } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { return zp_clock_elapsed_ms(instant) * 1000; } +unsigned long z_clock_elapsed_us(z_clock_t *instant) { return z_clock_elapsed_ms(instant) * 1000; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { return zp_time_elapsed_ms(instant); } +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { return z_time_elapsed_ms(instant); } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { return zp_clock_elapsed_ms(instant) / 1000; } +unsigned long z_clock_elapsed_s(z_clock_t *instant) { return z_clock_elapsed_ms(instant) / 1000; } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { return xTaskGetTickCount(); } +z_time_t z_time_now(void) { return xTaskGetTickCount(); } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - snprintf(buf, buflen, "%u", zp_time_now()); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + snprintf(buf, buflen, "%u", z_time_now()); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { return zp_time_elapsed_ms(time) * 1000; } +unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now = zp_time_now(); +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now = z_time_now(); unsigned long elapsed = (now - *time) * portTICK_PERIOD_MS; return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { return zp_time_elapsed_ms(time) / 1000; } +unsigned long z_time_elapsed_s(z_time_t *time) { return z_time_elapsed_ms(time) / 1000; } diff --git a/src/system/mbed/system.cpp b/src/system/mbed/system.cpp index b8205a9fb..7b2ffc65f 100644 --- a/src/system/mbed/system.cpp +++ b/src/system/mbed/system.cpp @@ -22,86 +22,86 @@ extern "C" { #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return randLIB_get_8bit(); } +uint8_t z_random_u8(void) { return randLIB_get_8bit(); } -uint16_t zp_random_u16(void) { return randLIB_get_16bit(); } +uint16_t z_random_u16(void) { return randLIB_get_16bit(); } -uint32_t zp_random_u32(void) { return randLIB_get_32bit(); } +uint32_t z_random_u32(void) { return randLIB_get_32bit(); } -uint64_t zp_random_u64(void) { return randLIB_get_64bit(); } +uint64_t z_random_u64(void) { return randLIB_get_64bit(); } -void zp_random_fill(void *buf, size_t len) { randLIB_get_n_bytes_random(buf, len); } +void z_random_fill(void *buf, size_t len) { randLIB_get_n_bytes_random(buf, len); } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return malloc(size); } +void *z_malloc(size_t size) { return malloc(size); } -void *zp_realloc(void *ptr, size_t size) { return realloc(ptr, size); } +void *z_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -void zp_free(void *ptr) { free(ptr); } +void z_free(void *ptr) { free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { *task = new Thread(); mbed::Callback c = mbed::Callback(fun, arg); return ((Thread *)*task)->start(c); } -int8_t zp_task_join(zp_task_t *task) { +int8_t z_task_join(z_task_t *task) { int res = ((Thread *)*task)->join(); delete ((Thread *)*task); return res; } -int8_t zp_task_cancel(zp_task_t *task) { +int8_t zp_task_cancel(z_task_t *task) { int res = ((Thread *)*task)->terminate(); delete ((Thread *)*task); return res; } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { +int8_t z_mutex_init(z_mutex_t *m) { *m = new Mutex(); return 0; } -int8_t zp_mutex_free(zp_mutex_t *m) { +int8_t z_mutex_free(z_mutex_t *m) { delete ((Mutex *)*m); return 0; } -int8_t zp_mutex_lock(zp_mutex_t *m) { +int8_t z_mutex_lock(z_mutex_t *m) { ((Mutex *)*m)->lock(); return 0; } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return (((Mutex *)*m)->trylock() == true) ? 0 : -1; } +int8_t z_mutex_trylock(z_mutex_t *m) { return (((Mutex *)*m)->trylock() == true) ? 0 : -1; } -int8_t zp_mutex_unlock(zp_mutex_t *m) { +int8_t z_mutex_unlock(z_mutex_t *m) { ((Mutex *)*m)->unlock(); return 0; } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return 0; } +int8_t z_condvar_init(z_condvar_t *cv) { return 0; } -int8_t zp_condvar_free(zp_condvar_t *cv) { +int8_t z_condvar_free(z_condvar_t *cv) { delete ((ConditionVariable *)*cv); return 0; } -int8_t zp_condvar_signal(zp_condvar_t *cv) { +int8_t z_condvar_signal(z_condvar_t *cv) { ((ConditionVariable *)*cv)->notify_all(); return 0; } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { *cv = new ConditionVariable(*((Mutex *)*m)); ((ConditionVariable *)*cv)->wait(); return 0; @@ -109,75 +109,75 @@ int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { +int z_sleep_us(size_t time) { ThisThread::sleep_for(chrono::milliseconds(((time / 1000) + (time % 1000 == 0 ? 0 : 1)))); return 0; } -int zp_sleep_ms(size_t time) { +int z_sleep_ms(size_t time) { ThisThread::sleep_for(chrono::milliseconds(time)); return 0; } -int zp_sleep_s(size_t time) { +int z_sleep_s(size_t time) { ThisThread::sleep_for(chrono::seconds(time)); return 0; } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { +z_clock_t z_clock_now(void) { // Not supported by default return NULL; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { +unsigned long z_clock_elapsed_us(z_clock_t *instant) { // Not supported by default return -1; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { // Not supported by default return -1; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { +unsigned long z_clock_elapsed_s(z_clock_t *instant) { // Not supported by default return -1; } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; gettimeofday(&now, NULL); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.tv_sec); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/system/unix/link/raweth.c b/src/system/unix/link/raweth.c index 78a8aee92..10e35c565 100644 --- a/src/system/unix/link/raweth.c +++ b/src/system/unix/link/raweth.c @@ -31,8 +31,9 @@ #include "zenoh-pico/collections/string.h" #include "zenoh-pico/config.h" +#include "zenoh-pico/protocol/keyexpr.h" +#include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/system/platform/unix.h" -#include "zenoh-pico/transport/raweth/config.h" #include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/pointers.h" @@ -43,6 +44,8 @@ #else #include +void _z_raweth_clear_mapping_entry(_zp_raweth_mapping_entry_t *entry) { _z_keyexpr_clear(&entry->_keyexpr); } + int8_t _z_open_raweth(_z_sys_net_socket_t *sock, const char *interface) { int8_t ret = _Z_RES_OK; // Open a raw network socket in promiscuous mode @@ -89,19 +92,24 @@ size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t return (size_t)wb; } -size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr) { +size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr, + const _zp_raweth_whitelist_array_t *whitelist) { // Read from socket ssize_t bytesRead = recvfrom(sock->_fd, buff, buff_len, 0, NULL, NULL); if ((bytesRead < 0) || (bytesRead < sizeof(_zp_eth_header_t))) { return SIZE_MAX; } - // Address filtering - _zp_eth_header_t *header = (_zp_eth_header_t *)buff; - _Bool is_valid = false; - for (size_t i = 0; i < _ZP_RAWETH_CFG_WHITELIST_SIZE; i++) { - if (memcmp(&header->smac, _ZP_RAWETH_CFG_WHITELIST[i]._mac, _ZP_MAC_ADDR_LENGTH) == 0) { // Test byte ordering - is_valid = true; - break; + _Bool is_valid = true; + // Address filtering (only if there is a whitelist) + if (_zp_raweth_whitelist_array_len(whitelist) > 0) { + is_valid = false; + const _zp_eth_header_t *header = (_zp_eth_header_t *)buff; + for (size_t i = 0; i < _zp_raweth_whitelist_array_len(whitelist); i++) { + const _zp_raweth_whitelist_entry_t *entry = _zp_raweth_whitelist_array_get(whitelist, i); + if (memcmp(&header->smac, entry->_mac, _ZP_MAC_ADDR_LENGTH) == 0) { + is_valid = true; + break; + } } } // Ignore packet from unknown sources diff --git a/src/system/unix/network.c b/src/system/unix/network.c index 5f0cc4b96..d5ec40393 100644 --- a/src/system/unix/network.c +++ b/src/system/unix/network.c @@ -59,7 +59,7 @@ int8_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, u sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -186,7 +186,7 @@ int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_ sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -264,14 +264,14 @@ unsigned int __get_ip_from_iface(const char *iface, int sa_family, struct sockad if (_z_str_eq(tmp->ifa_name, iface) == true) { if (tmp->ifa_addr->sa_family == sa_family) { if (tmp->ifa_addr->sa_family == AF_INET) { - *lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + *lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(*lsockaddr, 0, sizeof(struct sockaddr_in)); (void)memcpy(*lsockaddr, tmp->ifa_addr, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); } } else if (tmp->ifa_addr->sa_family == AF_INET6) { - *lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + *lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(*lsockaddr, 0, sizeof(struct sockaddr_in6)); (void)memcpy(*lsockaddr, tmp->ifa_addr, sizeof(struct sockaddr_in6)); @@ -300,7 +300,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -336,7 +336,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // Create lep endpoint if (ret == _Z_RES_OK) { - struct addrinfo *laddr = (struct addrinfo *)zp_malloc(sizeof(struct addrinfo)); + struct addrinfo *laddr = (struct addrinfo *)z_malloc(sizeof(struct addrinfo)); if (laddr != NULL) { laddr->ai_flags = 0; laddr->ai_family = rep._iptcp->ai_family; @@ -360,7 +360,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } if (ret != _Z_RES_OK) { - zp_free(lsockaddr); + z_free(lsockaddr); } } else { ret = _Z_ERR_GENERIC; @@ -378,7 +378,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -462,7 +462,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo } } } - zp_free(joins); + z_free(joins); } if (ret != _Z_RES_OK) { @@ -472,7 +472,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } - zp_free(lsockaddr); + z_free(lsockaddr); } else { ret = _Z_ERR_GENERIC; } @@ -500,7 +500,7 @@ void _z_close_udp_multicast(_z_sys_net_socket_t *sockrecv, _z_sys_net_socket_t * // Required to be compliant with MISRA 15.7 rule } #if defined(ZENOH_LINUX) - zp_free(lep._iptcp->ai_addr); + z_free(lep._iptcp->ai_addr); #else _ZP_UNUSED(lep); #endif diff --git a/src/system/unix/system.c b/src/system/unix/system.c index 4389a5190..799b60519 100644 --- a/src/system/unix/system.c +++ b/src/system/unix/system.c @@ -28,33 +28,33 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { +uint8_t z_random_u8(void) { uint8_t ret = 0; #if defined(ZENOH_LINUX) while (getrandom(&ret, sizeof(uint8_t), 0) <= 0) { ZP_ASM_NOP; } #elif defined(ZENOH_MACOS) || defined(ZENOH_BSD) - ret = zp_random_u32(); + ret = z_random_u32(); #endif return ret; } -uint16_t zp_random_u16(void) { +uint16_t z_random_u16(void) { uint16_t ret = 0; #if defined(ZENOH_LINUX) while (getrandom(&ret, sizeof(uint16_t), 0) <= 0) { ZP_ASM_NOP; } #elif defined(ZENOH_MACOS) || defined(ZENOH_BSD) - ret = zp_random_u32(); + ret = z_random_u32(); #endif return ret; } -uint32_t zp_random_u32(void) { +uint32_t z_random_u32(void) { uint32_t ret = 0; #if defined(ZENOH_LINUX) while (getrandom(&ret, sizeof(uint32_t), 0) <= 0) { @@ -67,22 +67,22 @@ uint32_t zp_random_u32(void) { return ret; } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; #if defined(ZENOH_LINUX) while (getrandom(&ret, sizeof(uint64_t), 0) <= 0) { ZP_ASM_NOP; } #elif defined(ZENOH_MACOS) || defined(ZENOH_BSD) - ret |= zp_random_u32(); + ret |= z_random_u32(); ret = ret << 32; - ret |= zp_random_u32(); + ret |= z_random_u32(); #endif return ret; } -void zp_random_fill(void *buf, size_t len) { +void z_random_fill(void *buf, size_t len) { #if defined(ZENOH_LINUX) while (getrandom(buf, len, 0) <= 0) { ZP_ASM_NOP; @@ -93,92 +93,92 @@ void zp_random_fill(void *buf, size_t len) { } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return malloc(size); } +void *z_malloc(size_t size) { return malloc(size); } -void *zp_realloc(void *ptr, size_t size) { return realloc(ptr, size); } +void *z_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -void zp_free(void *ptr) { free(ptr); } +void z_free(void *ptr) { free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { return pthread_create(task, attr, fun, arg); } -int8_t zp_task_join(zp_task_t *task) { return pthread_join(*task, NULL); } +int8_t z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } -int8_t zp_task_cancel(zp_task_t *task) { return pthread_cancel(*task); } +int8_t zp_task_cancel(z_task_t *task) { return pthread_cancel(*task); } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } -int8_t zp_mutex_free(zp_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t zp_mutex_lock(zp_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } -int8_t zp_condvar_free(zp_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { return usleep(time); } +int z_sleep_us(size_t time) { return usleep(time); } -int zp_sleep_ms(size_t time) { - zp_time_t start = zp_time_now(); +int z_sleep_ms(size_t time) { + z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. // This may compound, so this approach may make sleeps longer than expected. // This extra check tries to minimize the amount of extra time it might sleep. - while (zp_time_elapsed_ms(&start) < time) { - zp_sleep_us(1000); + while (z_time_elapsed_ms(&start) < time) { + z_sleep_us(1000); } return 0; } -int zp_sleep_s(size_t time) { return sleep(time); } +int z_sleep_s(size_t time) { return sleep(time); } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { - zp_clock_t now; +z_clock_t z_clock_now(void) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); return now; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); return elapsed; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); return elapsed; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = now.tv_sec - instant->tv_sec; @@ -186,38 +186,38 @@ unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; gettimeofday(&now, NULL); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.tv_sec); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/system/windows/network.c b/src/system/windows/network.c index 0d10466d6..833e43a97 100644 --- a/src/system/windows/network.c +++ b/src/system/windows/network.c @@ -64,7 +64,7 @@ int8_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, u sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); if (sock->_sock._fd != INVALID_SOCKET) { - zp_time_t tv; + z_time_t tv; tv.time = tout / (uint32_t)1000; tv.millitm = tout % (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -200,7 +200,7 @@ int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_ sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); if (sock->_sock._fd != INVALID_SOCKET) { - zp_time_t tv; + z_time_t tv; tv.time = tout / (uint32_t)1000; tv.millitm = tout % (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_sock._fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -277,20 +277,20 @@ unsigned int __get_ip_from_iface(const char *iface, int sa_family, SOCKADDR **ls unsigned int addrlen = 0U; unsigned long outBufLen = 15000; - IP_ADAPTER_ADDRESSES *l_ifaddr = (IP_ADAPTER_ADDRESSES *)zp_malloc(outBufLen); + IP_ADAPTER_ADDRESSES *l_ifaddr = (IP_ADAPTER_ADDRESSES *)z_malloc(outBufLen); if (l_ifaddr != NULL) { if (GetAdaptersAddresses(sa_family, 0, NULL, l_ifaddr, &outBufLen) != ERROR_BUFFER_OVERFLOW) { for (IP_ADAPTER_ADDRESSES *tmp = l_ifaddr; tmp != NULL; tmp = tmp->Next) { if (_z_str_eq(tmp->AdapterName, iface) == true) { if (sa_family == AF_INET) { - *lsockaddr = (SOCKADDR *)zp_malloc(sizeof(SOCKADDR_IN)); + *lsockaddr = (SOCKADDR *)z_malloc(sizeof(SOCKADDR_IN)); if (lsockaddr != NULL) { (void)memset(*lsockaddr, 0, sizeof(SOCKADDR_IN)); (void)memcpy(*lsockaddr, tmp->FirstUnicastAddress->Address.lpSockaddr, sizeof(SOCKADDR_IN)); addrlen = sizeof(SOCKADDR_IN); } } else if (sa_family == AF_INET6) { - *lsockaddr = (SOCKADDR *)zp_malloc(sizeof(SOCKADDR_IN6)); + *lsockaddr = (SOCKADDR *)z_malloc(sizeof(SOCKADDR_IN6)); if (lsockaddr != NULL) { (void)memset(*lsockaddr, 0, sizeof(SOCKADDR_IN6)); (void)memcpy(*lsockaddr, tmp->FirstUnicastAddress->Address.lpSockaddr, @@ -306,7 +306,7 @@ unsigned int __get_ip_from_iface(const char *iface, int sa_family, SOCKADDR **ls } } - zp_free(l_ifaddr); + z_free(l_ifaddr); l_ifaddr = NULL; } @@ -326,7 +326,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin if (addrlen != 0U) { sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); if (sock->_sock._fd != INVALID_SOCKET) { - zp_time_t tv; + z_time_t tv; tv.time = tout / (uint32_t)1000; tv.millitm = tout % (uint32_t)1000; if ((ret == _Z_RES_OK) && @@ -374,7 +374,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // Create lep endpoint if (ret == _Z_RES_OK) { - ADDRINFOA *laddr = (ADDRINFOA *)zp_malloc(sizeof(ADDRINFOA)); + ADDRINFOA *laddr = (ADDRINFOA *)z_malloc(sizeof(ADDRINFOA)); if (laddr != NULL) { laddr->ai_flags = 0; laddr->ai_family = rep._ep._iptcp->ai_family; @@ -391,7 +391,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // https://lists.debian.org/debian-glibc/2016/03/msg00241.html // To avoid a fix to break zenoh-pico, we are let it leak for the moment. // #if defined(ZENOH_LINUX) - // zp_free(lsockaddr); + // z_free(lsockaddr); // #endif } else { ret = _Z_ERR_GENERIC; @@ -407,7 +407,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } if (ret != _Z_RES_OK) { - zp_free(lsockaddr); + z_free(lsockaddr); } } else { ret = _Z_ERR_GENERIC; @@ -430,7 +430,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo if (addrlen != 0U) { sock->_sock._fd = socket(rep._ep._iptcp->ai_family, rep._ep._iptcp->ai_socktype, rep._ep._iptcp->ai_protocol); if (sock->_sock._fd != INVALID_SOCKET) { - zp_time_t tv; + z_time_t tv; tv.time = tout / (uint32_t)1000; tv.millitm = tout % (uint32_t)1000; if ((ret == _Z_RES_OK) && @@ -497,7 +497,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } - zp_free(lsockaddr); + z_free(lsockaddr); } else { ret = _Z_ERR_GENERIC; } diff --git a/src/system/windows/system.c b/src/system/windows/system.c index 217b815ec..3997df9ab 100644 --- a/src/system/windows/system.c +++ b/src/system/windows/system.c @@ -23,44 +23,44 @@ #include "zenoh-pico/utils/result.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { +uint8_t z_random_u8(void) { uint8_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } -uint16_t zp_random_u16(void) { +uint16_t z_random_u16(void) { uint16_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } -uint32_t zp_random_u32(void) { +uint32_t z_random_u32(void) { uint32_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } -void zp_random_fill(void *buf, size_t len) { RtlGenRandom(buf, (unsigned long)len); } +void z_random_fill(void *buf, size_t len) { RtlGenRandom(buf, (unsigned long)len); } /*------------------ Memory ------------------*/ // #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) // #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) -void *zp_malloc(size_t size) { return malloc(size); } +void *z_malloc(size_t size) { return malloc(size); } -void *zp_realloc(void *ptr, size_t size) { return realloc(ptr, size); } +void *z_realloc(void *ptr, size_t size) { return realloc(ptr, size); } -void zp_free(void *ptr) { free(ptr); } +void z_free(void *ptr) { free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { (void)(attr); int8_t ret = _Z_RES_OK; *task = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fun, arg, 0, NULL); @@ -70,45 +70,45 @@ int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), return ret; } -int8_t zp_task_join(zp_task_t *task) { +int8_t z_task_join(z_task_t *task) { int8_t ret = _Z_RES_OK; WaitForSingleObject(*task, INFINITE); return ret; } -int8_t zp_task_cancel(zp_task_t *task) { +int8_t zp_task_cancel(z_task_t *task) { int8_t ret = _Z_RES_OK; TerminateThread(*task, 0); return ret; } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; CloseHandle(*ptr); - zp_free(ptr); + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { +int8_t z_mutex_init(z_mutex_t *m) { int8_t ret = _Z_RES_OK; InitializeSRWLock(m); return ret; } -int8_t zp_mutex_free(zp_mutex_t *m) { +int8_t z_mutex_free(z_mutex_t *m) { (void)(m); int8_t ret = _Z_RES_OK; return ret; } -int8_t zp_mutex_lock(zp_mutex_t *m) { +int8_t z_mutex_lock(z_mutex_t *m) { int8_t ret = _Z_RES_OK; AcquireSRWLockExclusive(m); return ret; } -int8_t zp_mutex_trylock(zp_mutex_t *m) { +int8_t z_mutex_trylock(z_mutex_t *m) { int8_t ret = _Z_RES_OK; if (TryAcquireSRWLockExclusive(m) == 0) { ret = _Z_ERR_GENERIC; @@ -116,32 +116,32 @@ int8_t zp_mutex_trylock(zp_mutex_t *m) { return ret; } -int8_t zp_mutex_unlock(zp_mutex_t *m) { +int8_t z_mutex_unlock(z_mutex_t *m) { int8_t ret = _Z_RES_OK; ReleaseSRWLockExclusive(m); return ret; } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { +int8_t z_condvar_init(z_condvar_t *cv) { int8_t ret = _Z_RES_OK; InitializeConditionVariable(cv); return ret; } -int8_t zp_condvar_free(zp_condvar_t *cv) { +int8_t z_condvar_free(z_condvar_t *cv) { (void)(cv); int8_t ret = _Z_RES_OK; return ret; } -int8_t zp_condvar_signal(zp_condvar_t *cv) { +int8_t z_condvar_signal(z_condvar_t *cv) { int8_t ret = _Z_RES_OK; WakeConditionVariable(cv); return ret; } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { int8_t ret = _Z_RES_OK; SleepConditionVariableSRW(cv, m, INFINITE, 0); return ret; @@ -149,9 +149,9 @@ int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { return zp_sleep_ms((time / 1000) + (time % 1000 == 0 ? 0 : 1)); } +int z_sleep_us(size_t time) { return z_sleep_ms((time / 1000) + (time % 1000 == 0 ? 0 : 1)); } -int zp_sleep_ms(size_t time) { +int z_sleep_ms(size_t time) { // Guarantees that size_t is split into DWORD segments for Sleep uint8_t ratio = sizeof(size_t) / sizeof(DWORD); DWORD ratio_time = (DWORD)((time / ratio) + (time % ratio == 0 ? 0 : 1)); @@ -161,28 +161,28 @@ int zp_sleep_ms(size_t time) { return 0; } -int zp_sleep_s(size_t time) { - zp_time_t start = zp_time_now(); +int z_sleep_s(size_t time) { + z_time_t start = z_time_now(); // Most sleep APIs promise to sleep at least whatever you asked them to. // This may compound, so this approach may make sleeps longer than expected. // This extra check tries to minimize the amount of extra time it might sleep. - while (zp_time_elapsed_s(&start) < time) { - zp_sleep_ms(1000); + while (z_time_elapsed_s(&start) < time) { + z_sleep_ms(1000); } return 0; } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { - zp_clock_t now; +z_clock_t z_clock_now(void) { + z_clock_t now; QueryPerformanceCounter(&now); return now; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; LARGE_INTEGER frequency; QueryPerformanceCounter(&now); QueryPerformanceFrequency(&frequency); // ticks per second @@ -196,8 +196,8 @@ unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { return (unsigned long)elapsed; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now; LARGE_INTEGER frequency; QueryPerformanceCounter(&now); QueryPerformanceFrequency(&frequency); // ticks per second @@ -211,8 +211,8 @@ unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { return (unsigned long)elapsed; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_clock_t now; LARGE_INTEGER frequency; QueryPerformanceCounter(&now); QueryPerformanceFrequency(&frequency); // ticks per second @@ -226,32 +226,32 @@ unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; ftime(&now); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.time); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { return zp_time_elapsed_ms(time) * 1000; } +unsigned long z_time_elapsed_us(z_time_t *time) { return z_time_elapsed_ms(time) * 1000; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; ftime(&now); unsigned long elapsed = ((unsigned long)(now.time - time->time) * 1000) + (now.millitm - time->millitm); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; ftime(&now); unsigned long elapsed = (unsigned long)(now.time - time->time); diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index ae95b1e72..00d3ec1ce 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -60,7 +60,7 @@ int8_t _z_open_tcp(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_t rep, u sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -175,7 +175,7 @@ int8_t _z_open_udp_unicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoint_ sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -251,7 +251,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin struct sockaddr *lsockaddr = NULL; unsigned int addrlen = 0; if (rep._iptcp->ai_family == AF_INET) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); @@ -264,7 +264,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin ret = _Z_ERR_GENERIC; } } else if (rep._iptcp->ai_family == AF_INET6) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in6)); addrlen = sizeof(struct sockaddr_in6); @@ -284,7 +284,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin if (addrlen != 0U) { sock->_fd = socket(rep._iptcp->ai_family, rep._iptcp->ai_socktype, rep._iptcp->ai_protocol); if (sock->_fd != -1) { - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -303,7 +303,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin // Create lep endpoint if (ret == _Z_RES_OK) { - struct addrinfo *laddr = (struct addrinfo *)zp_malloc(sizeof(struct addrinfo)); + struct addrinfo *laddr = (struct addrinfo *)z_malloc(sizeof(struct addrinfo)); if (laddr != NULL) { laddr->ai_flags = 0; laddr->ai_family = rep._iptcp->ai_family; @@ -327,7 +327,7 @@ int8_t _z_open_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpoin } if (ret != _Z_RES_OK) { - zp_free(lsockaddr); + z_free(lsockaddr); } } else { ret = _Z_ERR_GENERIC; @@ -344,7 +344,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo struct sockaddr *lsockaddr = NULL; unsigned int addrlen = 0; if (rep._iptcp->ai_family == AF_INET) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in)); addrlen = sizeof(struct sockaddr_in); @@ -357,7 +357,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } } else if (rep._iptcp->ai_family == AF_INET6) { - lsockaddr = (struct sockaddr *)zp_malloc(sizeof(struct sockaddr_in6)); + lsockaddr = (struct sockaddr *)z_malloc(sizeof(struct sockaddr_in6)); if (lsockaddr != NULL) { (void)memset(lsockaddr, 0, sizeof(struct sockaddr_in6)); addrlen = sizeof(struct sockaddr_in6); @@ -382,7 +382,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo ret = _Z_ERR_GENERIC; } - zp_time_t tv; + z_time_t tv; tv.tv_sec = tout / (uint32_t)1000; tv.tv_usec = (tout % (uint32_t)1000) * (uint32_t)1000; if ((ret == _Z_RES_OK) && (setsockopt(sock->_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv)) < 0)) { @@ -431,7 +431,7 @@ int8_t _z_listen_udp_multicast(_z_sys_net_socket_t *sock, const _z_sys_net_endpo } } - zp_free(lsockaddr); + z_free(lsockaddr); } else { ret = _Z_ERR_GENERIC; } @@ -619,7 +619,7 @@ void _z_close_serial(_z_sys_net_socket_t *sock) {} size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { int8_t ret = _Z_RES_OK; - uint8_t *before_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { int res = -1; @@ -633,7 +633,7 @@ size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) } } - uint8_t *after_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); size_t trb = _z_cobs_decode(before_cobs, rb, after_cobs); size_t i = 0; @@ -660,8 +660,8 @@ size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) ret = _Z_ERR_GENERIC; } - zp_free(before_cobs); - zp_free(after_cobs); + z_free(before_cobs); + z_free(after_cobs); rb = payload_len; if (ret != _Z_RES_OK) { @@ -692,7 +692,7 @@ size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) { int8_t ret = _Z_RES_OK; - uint8_t *before_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); size_t i = 0; for (; i < sizeof(uint16_t); ++i) { before_cobs[i] = (len >> (i * (size_t)8)) & (size_t)0XFF; @@ -707,15 +707,15 @@ size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t i = i + (size_t)1; } - uint8_t *after_cobs = (uint8_t *)zp_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs); after_cobs[twb] = 0x00; // Manually add the COBS delimiter for (ssize_t i = 0; i < (twb + (ssize_t)1); i++) { uart_poll_out(sock._serial, after_cobs[i]); } - zp_free(before_cobs); - zp_free(after_cobs); + z_free(before_cobs); + z_free(after_cobs); size_t sb = len; if (ret != _Z_RES_OK) { diff --git a/src/system/zephyr/system.c b/src/system/zephyr/system.c index 3d1b0b9d5..ec1dccb6e 100644 --- a/src/system/zephyr/system.c +++ b/src/system/zephyr/system.c @@ -28,32 +28,32 @@ #include "zenoh-pico/system/platform.h" /*------------------ Random ------------------*/ -uint8_t zp_random_u8(void) { return zp_random_u32(); } +uint8_t z_random_u8(void) { return z_random_u32(); } -uint16_t zp_random_u16(void) { return zp_random_u32(); } +uint16_t z_random_u16(void) { return z_random_u32(); } -uint32_t zp_random_u32(void) { return sys_rand32_get(); } +uint32_t z_random_u32(void) { return sys_rand32_get(); } -uint64_t zp_random_u64(void) { +uint64_t z_random_u64(void) { uint64_t ret = 0; - ret |= zp_random_u32(); + ret |= z_random_u32(); ret = ret << 32; - ret |= zp_random_u32(); + ret |= z_random_u32(); return ret; } -void zp_random_fill(void *buf, size_t len) { sys_rand_get(buf, len); } +void z_random_fill(void *buf, size_t len) { sys_rand_get(buf, len); } /*------------------ Memory ------------------*/ -void *zp_malloc(size_t size) { return k_malloc(size); } +void *z_malloc(size_t size) { return k_malloc(size); } -void *zp_realloc(void *ptr, size_t size) { +void *z_realloc(void *ptr, size_t size) { // k_realloc not implemented in Zephyr return NULL; } -void zp_free(void *ptr) { k_free(ptr); } +void z_free(void *ptr) { k_free(ptr); } #if Z_FEATURE_MULTI_THREAD == 1 @@ -71,9 +71,9 @@ K_THREAD_STACK_ARRAY_DEFINE(thread_stack_area, Z_THREADS_NUM, Z_PTHREAD_STACK_SI static int thread_index = 0; /*------------------ Task ------------------*/ -int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), void *arg) { - zp_task_attr_t *lattr = NULL; - zp_task_attr_t tmp; +int8_t z_task_init(z_task_t *task, z_task_attr_t *attr, void *(*fun)(void *), void *arg) { + z_task_attr_t *lattr = NULL; + z_task_attr_t tmp; if (attr == NULL) { (void)pthread_attr_init(&tmp); (void)pthread_attr_setstack(&tmp, &thread_stack_area[thread_index++], Z_PTHREAD_STACK_SIZE_DEFAULT); @@ -83,39 +83,39 @@ int8_t zp_task_init(zp_task_t *task, zp_task_attr_t *attr, void *(*fun)(void *), return pthread_create(task, lattr, fun, arg); } -int8_t zp_task_join(zp_task_t *task) { return pthread_join(*task, NULL); } +int8_t z_task_join(z_task_t *task) { return pthread_join(*task, NULL); } -int8_t zp_task_cancel(zp_task_t *task) { return pthread_cancel(*task); } +int8_t zp_task_cancel(z_task_t *task) { return pthread_cancel(*task); } -void zp_task_free(zp_task_t **task) { - zp_task_t *ptr = *task; - zp_free(ptr); +void z_task_free(z_task_t **task) { + z_task_t *ptr = *task; + z_free(ptr); *task = NULL; } /*------------------ Mutex ------------------*/ -int8_t zp_mutex_init(zp_mutex_t *m) { return pthread_mutex_init(m, 0); } +int8_t z_mutex_init(z_mutex_t *m) { return pthread_mutex_init(m, 0); } -int8_t zp_mutex_free(zp_mutex_t *m) { return pthread_mutex_destroy(m); } +int8_t z_mutex_free(z_mutex_t *m) { return pthread_mutex_destroy(m); } -int8_t zp_mutex_lock(zp_mutex_t *m) { return pthread_mutex_lock(m); } +int8_t z_mutex_lock(z_mutex_t *m) { return pthread_mutex_lock(m); } -int8_t zp_mutex_trylock(zp_mutex_t *m) { return pthread_mutex_trylock(m); } +int8_t z_mutex_trylock(z_mutex_t *m) { return pthread_mutex_trylock(m); } -int8_t zp_mutex_unlock(zp_mutex_t *m) { return pthread_mutex_unlock(m); } +int8_t z_mutex_unlock(z_mutex_t *m) { return pthread_mutex_unlock(m); } /*------------------ Condvar ------------------*/ -int8_t zp_condvar_init(zp_condvar_t *cv) { return pthread_cond_init(cv, 0); } +int8_t z_condvar_init(z_condvar_t *cv) { return pthread_cond_init(cv, 0); } -int8_t zp_condvar_free(zp_condvar_t *cv) { return pthread_cond_destroy(cv); } +int8_t z_condvar_free(z_condvar_t *cv) { return pthread_cond_destroy(cv); } -int8_t zp_condvar_signal(zp_condvar_t *cv) { return pthread_cond_signal(cv); } +int8_t z_condvar_signal(z_condvar_t *cv) { return pthread_cond_signal(cv); } -int8_t zp_condvar_wait(zp_condvar_t *cv, zp_mutex_t *m) { return pthread_cond_wait(cv, m); } +int8_t z_condvar_wait(z_condvar_t *cv, z_mutex_t *m) { return pthread_cond_wait(cv, m); } #endif // Z_FEATURE_MULTI_THREAD == 1 /*------------------ Sleep ------------------*/ -int zp_sleep_us(size_t time) { +int z_sleep_us(size_t time) { int32_t rem = time; while (rem > 0) { rem = k_usleep(rem); // This function is unlikely to work as expected without kernel tuning. @@ -129,7 +129,7 @@ int zp_sleep_us(size_t time) { return 0; } -int zp_sleep_ms(size_t time) { +int z_sleep_ms(size_t time) { int32_t rem = time; while (rem > 0) { rem = k_msleep(rem); @@ -138,7 +138,7 @@ int zp_sleep_ms(size_t time) { return 0; } -int zp_sleep_s(size_t time) { +int z_sleep_s(size_t time) { int32_t rem = time; while (rem > 0) { rem = k_sleep(K_SECONDS(rem)); @@ -148,30 +148,30 @@ int zp_sleep_s(size_t time) { } /*------------------ Instant ------------------*/ -zp_clock_t zp_clock_now(void) { - zp_clock_t now; +z_clock_t z_clock_now(void) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); return now; } -unsigned long zp_clock_elapsed_us(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_us(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000); return elapsed; } -unsigned long zp_clock_elapsed_ms(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_ms(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = (1000 * (now.tv_sec - instant->tv_sec) + (now.tv_nsec - instant->tv_nsec) / 1000000); return elapsed; } -unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { - zp_clock_t now; +unsigned long z_clock_elapsed_s(z_clock_t *instant) { + z_clock_t now; clock_gettime(CLOCK_MONOTONIC, &now); unsigned long elapsed = now.tv_sec - instant->tv_sec; @@ -179,38 +179,38 @@ unsigned long zp_clock_elapsed_s(zp_clock_t *instant) { } /*------------------ Time ------------------*/ -zp_time_t zp_time_now(void) { - zp_time_t now; +z_time_t z_time_now(void) { + z_time_t now; gettimeofday(&now, NULL); return now; } -const char *zp_time_now_as_str(char *const buf, unsigned long buflen) { - zp_time_t tv = zp_time_now(); +const char *z_time_now_as_str(char *const buf, unsigned long buflen) { + z_time_t tv = z_time_now(); struct tm ts; ts = *localtime(&tv.tv_sec); strftime(buf, buflen, "%Y-%m-%dT%H:%M:%SZ", &ts); return buf; } -unsigned long zp_time_elapsed_us(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_us(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec)); return elapsed; } -unsigned long zp_time_elapsed_ms(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_ms(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = (1000 * (now.tv_sec - time->tv_sec) + (now.tv_usec - time->tv_usec) / 1000); return elapsed; } -unsigned long zp_time_elapsed_s(zp_time_t *time) { - zp_time_t now; +unsigned long z_time_elapsed_s(z_time_t *time) { + z_time_t now; gettimeofday(&now, NULL); unsigned long elapsed = now.tv_sec - time->tv_sec; diff --git a/src/transport/multicast/lease.c b/src/transport/multicast/lease.c index 5c7ef21d6..f728535f6 100644 --- a/src/transport/multicast/lease.c +++ b/src/transport/multicast/lease.c @@ -99,7 +99,7 @@ void *_zp_multicast_lease_task(void *ztm_arg) { _z_transport_peer_entry_list_t *it = NULL; while (ztm->_lease_task_running == true) { - zp_mutex_lock(&ztm->_mutex_peer); + z_mutex_lock(&ztm->_mutex_peer); if (next_lease <= 0) { it = ztm->_peers; @@ -158,13 +158,13 @@ void *_zp_multicast_lease_task(void *ztm_arg) { } } - zp_mutex_unlock(&ztm->_mutex_peer); + z_mutex_unlock(&ztm->_mutex_peer); // The keep alive and lease intervals are expressed in milliseconds - zp_sleep_ms(interval); + z_sleep_ms(interval); // Decrement all intervals - zp_mutex_lock(&ztm->_mutex_peer); + z_mutex_lock(&ztm->_mutex_peer); it = ztm->_peers; while (it != NULL) { @@ -176,16 +176,16 @@ void *_zp_multicast_lease_task(void *ztm_arg) { next_keep_alive = next_keep_alive - interval; next_join = next_join - interval; - zp_mutex_unlock(&ztm->_mutex_peer); + z_mutex_unlock(&ztm->_mutex_peer); } return 0; } -int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, zp_task_attr_t *attr, zp_task_t *task) { +int8_t _zp_multicast_start_lease_task(_z_transport_multicast_t *ztm, z_task_attr_t *attr, z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(zp_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); // Init task - if (zp_task_init(task, attr, _zp_multicast_lease_task, ztm) != _Z_RES_OK) { + if (z_task_init(task, attr, _zp_multicast_lease_task, ztm) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/multicast/read.c b/src/transport/multicast/read.c index 830ae11c1..9e293f990 100644 --- a/src/transport/multicast/read.c +++ b/src/transport/multicast/read.c @@ -51,7 +51,7 @@ void *_zp_multicast_read_task(void *ztm_arg) { _z_transport_multicast_t *ztm = (_z_transport_multicast_t *)ztm_arg; // Acquire and keep the lock - zp_mutex_lock(&ztm->_mutex_rx); + z_mutex_lock(&ztm->_mutex_rx); // Prepare the buffer _z_zbuf_reset(&ztm->_zbuf); @@ -124,15 +124,15 @@ void *_zp_multicast_read_task(void *ztm_arg) { // Move the read position of the read buffer _z_zbuf_set_rpos(&ztm->_zbuf, _z_zbuf_get_rpos(&ztm->_zbuf) + to_read); } - zp_mutex_unlock(&ztm->_mutex_rx); + z_mutex_unlock(&ztm->_mutex_rx); return NULL; } -int8_t _zp_multicast_start_read_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task) { +int8_t _zp_multicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(zp_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); // Init task - if (zp_task_init(task, attr, _zp_multicast_read_task, &zt->_transport._multicast) != _Z_RES_OK) { + if (z_task_init(task, attr, _zp_multicast_read_task, &zt->_transport._multicast) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/multicast/rx.c b/src/transport/multicast/rx.c index 9fb789045..048a6563f 100644 --- a/src/transport/multicast/rx.c +++ b/src/transport/multicast/rx.c @@ -35,7 +35,7 @@ static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_trans #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - zp_mutex_lock(&ztm->_mutex_rx); + z_mutex_lock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 size_t to_read = 0; @@ -82,7 +82,7 @@ static int8_t _z_multicast_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_trans } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_rx); + z_mutex_unlock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; @@ -125,7 +125,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t int8_t ret = _Z_RES_OK; #if Z_FEATURE_MULTI_THREAD == 1 // Acquire and keep the lock - zp_mutex_lock(&ztm->_mutex_peer); + z_mutex_lock(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 // Mark the session that we have received data from this peer @@ -257,7 +257,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t if (entry == NULL) // New peer { - entry = (_z_transport_peer_entry_t *)zp_malloc(sizeof(_z_transport_peer_entry_t)); + entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); if (entry != NULL) { entry->_sn_res = _z_sn_max(t_msg->_body._join._seq_num_res); @@ -296,7 +296,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t ztm->_peers = _z_transport_peer_entry_list_insert(ztm->_peers, entry); } else { - zp_free(entry); + z_free(entry); } } else { ret = _Z_ERR_SYSTEM_OUT_OF_MEMORY; @@ -341,7 +341,7 @@ int8_t _z_multicast_handle_transport_message(_z_transport_multicast_t *ztm, _z_t } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_peer); + z_mutex_unlock(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; diff --git a/src/transport/multicast/transport.c b/src/transport/multicast/transport.c index f7c07a61a..6f6057fd8 100644 --- a/src/transport/multicast/transport.c +++ b/src/transport/multicast/transport.c @@ -52,17 +52,17 @@ int8_t _z_multicast_transport_create(_z_transport_t *zt, _z_link_t *zl, } #if Z_FEATURE_MULTI_THREAD == 1 // Initialize the mutexes - ret = zp_mutex_init(&ztm->_mutex_tx); + ret = z_mutex_init(&ztm->_mutex_tx); if (ret == _Z_RES_OK) { - ret = zp_mutex_init(&ztm->_mutex_rx); + ret = z_mutex_init(&ztm->_mutex_rx); if (ret == _Z_RES_OK) { - ret = zp_mutex_init(&ztm->_mutex_peer); + ret = z_mutex_init(&ztm->_mutex_peer); if (ret != _Z_RES_OK) { - zp_mutex_free(&ztm->_mutex_tx); - zp_mutex_free(&ztm->_mutex_rx); + z_mutex_free(&ztm->_mutex_tx); + z_mutex_free(&ztm->_mutex_rx); } } else { - zp_mutex_free(&ztm->_mutex_tx); + z_mutex_free(&ztm->_mutex_tx); } } #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -79,9 +79,9 @@ int8_t _z_multicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _Z_ERROR("Not enough memory to allocate transport tx rx buffers!"); #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_free(&ztm->_mutex_tx); - zp_mutex_free(&ztm->_mutex_rx); - zp_mutex_free(&ztm->_mutex_peer); + z_mutex_free(&ztm->_mutex_tx); + z_mutex_free(&ztm->_mutex_rx); + z_mutex_free(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_wbuf_clear(&ztm->_wbuf); @@ -124,7 +124,7 @@ int8_t _z_multicast_open_peer(_z_transport_multicast_establish_param_t *param, c int8_t ret = _Z_RES_OK; _z_zint_t initial_sn_tx = 0; - zp_random_fill(&initial_sn_tx, sizeof(initial_sn_tx)); + z_random_fill(&initial_sn_tx, sizeof(initial_sn_tx)); initial_sn_tx = initial_sn_tx & !_z_sn_modulo_mask(Z_SN_RESOLUTION); _z_conduit_sn_list_t next_sn; @@ -184,17 +184,17 @@ void _z_multicast_transport_clear(_z_transport_t *zt) { #if Z_FEATURE_MULTI_THREAD == 1 // Clean up tasks if (ztm->_read_task != NULL) { - zp_task_join(ztm->_read_task); - zp_task_free(&ztm->_read_task); + z_task_join(ztm->_read_task); + z_task_free(&ztm->_read_task); } if (ztm->_lease_task != NULL) { - zp_task_join(ztm->_lease_task); - zp_task_free(&ztm->_lease_task); + z_task_join(ztm->_lease_task); + z_task_free(&ztm->_lease_task); } // Clean up the mutexes - zp_mutex_free(&ztm->_mutex_tx); - zp_mutex_free(&ztm->_mutex_rx); - zp_mutex_free(&ztm->_mutex_peer); + z_mutex_free(&ztm->_mutex_tx); + z_mutex_free(&ztm->_mutex_rx); + z_mutex_free(&ztm->_mutex_peer); #endif // Z_FEATURE_MULTI_THREAD == 1 // Clean up the buffers diff --git a/src/transport/multicast/tx.c b/src/transport/multicast/tx.c index 18aefa3e4..5de35fe4f 100644 --- a/src/transport/multicast/tx.c +++ b/src/transport/multicast/tx.c @@ -46,7 +46,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - zp_mutex_lock(&ztm->_mutex_tx); + z_mutex_lock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Prepare the buffer eventually reserving space for the message length @@ -65,7 +65,7 @@ int8_t _z_multicast_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_tx); + z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; @@ -82,11 +82,11 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m _Bool drop = false; if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_lock(&ztm->_mutex_tx); + z_mutex_lock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } else { #if Z_FEATURE_MULTI_THREAD == 1 - int8_t locked = zp_mutex_trylock(&ztm->_mutex_tx); + int8_t locked = z_mutex_trylock(&ztm->_mutex_tx); if (locked != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control"); // We failed to acquire the lock, drop the message @@ -153,7 +153,7 @@ int8_t _z_multicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_m } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_tx); + z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } diff --git a/src/transport/raweth/config.c b/src/transport/raweth/config.c deleted file mode 100644 index 08e4684e7..000000000 --- a/src/transport/raweth/config.c +++ /dev/null @@ -1,47 +0,0 @@ -// -// 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, -// - -#include "zenoh-pico/transport/raweth/config.h" - -#if Z_FEATURE_RAWETH_TRANSPORT == 1 - -// Should be generated (big endian format) -const uint16_t _ZP_RAWETH_CFG_ETHTYPE = 0x72e0; - -// Should be generated -const char *_ZP_RAWETH_CFG_INTERFACE = "lo"; - -// Should be generated -const uint8_t _ZP_RAWETH_CFG_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}; - -// Should be generated -const _zp_raweth_cfg_entry _ZP_RAWETH_CFG_ARRAY[] = { - {{0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}, // Default mac addr - {{0, {0}, "some/key/expr"}, 0x8c00, {0x00, 0x11, 0x22, 0x33, 0x44, 0x55}, true}, // entry1 - {{0, {0}, "demo/example/zenoh-pico-pub"}, 0xab00, {0x41, 0x55, 0xa8, 0x00, 0x9d, 0xc0}, true}, // entry2 - {{0, {0}, "another/keyexpr"}, 0x4300, {0x01, 0x23, 0x45, 0x67, 0x89, 0xab}, true}, // entry3 -}; - -// Should be generated -const _zp_raweth_cfg_whitelist_val _ZP_RAWETH_CFG_WHITELIST[] = { - {{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}}, - {{0x00, 0x11, 0x22, 0x33, 0x44, 0x55}}, - {{0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}}, -}; - -// Don't modify -const size_t _ZP_RAWETH_CFG_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_ARRAY); -const size_t _ZP_RAWETH_CFG_WHITELIST_SIZE = _ZP_ARRAY_SIZE(_ZP_RAWETH_CFG_WHITELIST); - -#endif // Z_FEATURE_RAWETH_TRANSPORT == 1 diff --git a/src/transport/raweth/link.c b/src/transport/raweth/link.c index 8eb42abc9..a6430bc60 100644 --- a/src/transport/raweth/link.c +++ b/src/transport/raweth/link.c @@ -20,22 +20,69 @@ #include "zenoh-pico/config.h" #include "zenoh-pico/link/config/raweth.h" #include "zenoh-pico/link/manager.h" +#include "zenoh-pico/protocol/codec/core.h" #include "zenoh-pico/system/link/raweth.h" #include "zenoh-pico/system/platform.h" -#include "zenoh-pico/transport/raweth/config.h" +#include "zenoh-pico/utils/logging.h" #include "zenoh-pico/utils/pointers.h" #if Z_FEATURE_RAWETH_TRANSPORT == 1 -#define RAWETH_CONFIG_ARGC 1 +#define RAWETH_CFG_TUPLE_SEPARATOR '#' +#define RAWETH_CFG_LIST_SEPARATOR "," + +#define RAWETH_CONFIG_ARGC 4 #define RAWETH_CONFIG_IFACE_KEY 0x01 #define RAWETH_CONFIG_IFACE_STR "iface" +#define RAWETH_CONFIG_ETHTYPE_KEY 0x02 +#define RAWETH_CONFIG_ETHTYPE_STR "ethtype" + +#define RAWETH_CONFIG_MAPPING_KEY 0x03 +#define RAWETH_CONFIG_MAPPING_STR "mapping" + +#define RAWETH_CONFIG_WHITELIST_KEY 0x04 +#define RAWETH_CONFIG_WHITELIST_STR "whitelist" + #define RAWETH_CONFIG_MAPPING_BUILD \ _z_str_intmapping_t args[RAWETH_CONFIG_ARGC]; \ args[0]._key = RAWETH_CONFIG_IFACE_KEY; \ - args[0]._str = RAWETH_CONFIG_IFACE_STR; + args[0]._str = RAWETH_CONFIG_IFACE_STR; \ + args[1]._key = RAWETH_CONFIG_ETHTYPE_KEY; \ + args[1]._str = RAWETH_CONFIG_ETHTYPE_STR; \ + args[2]._key = RAWETH_CONFIG_MAPPING_KEY; \ + args[2]._str = RAWETH_CONFIG_MAPPING_STR; \ + args[3]._key = RAWETH_CONFIG_WHITELIST_KEY; \ + args[3]._str = RAWETH_CONFIG_WHITELIST_STR; + +const uint16_t _ZP_RAWETH_DEFAULT_ETHTYPE = 0x72e0; +const char *_ZP_RAWETH_DEFAULT_INTERFACE = "lo"; +const uint8_t _ZP_RAWETH_DEFAULT_SMAC[_ZP_MAC_ADDR_LENGTH] = {0x30, 0x03, 0xc8, 0x37, 0x25, 0xa1}; +const _zp_raweth_mapping_entry_t _ZP_RAWETH_DEFAULT_MAPPING = { + {0, {0}, ""}, 0x0000, {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}, false}; + +static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config); +static const char *_z_get_iface_raweth(_z_str_intmap_t *config); +static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config); +static long _z_get_ethtype_raweth(_z_str_intmap_t *config); +static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config); +static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_array_t *array, size_t size); +static size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config); +static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitelist_array_t *array, size_t size); +static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *storage); +static _Bool _z_valid_mapping_entry(char *entry); +static _Bool _z_valid_address_raweth(const char *address); +static uint8_t *_z_parse_address_raweth(const char *address); +static int8_t _z_f_link_open_raweth(_z_link_t *self); +static int8_t _z_f_link_listen_raweth(_z_link_t *self); +static void _z_f_link_close_raweth(_z_link_t *self); +static void _z_f_link_free_raweth(_z_link_t *self); +static size_t _z_f_link_write_raweth(const _z_link_t *self, const uint8_t *ptr, size_t len); +static size_t _z_f_link_write_all_raweth(const _z_link_t *self, const uint8_t *ptr, size_t len); +static size_t _z_f_link_read_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); +static size_t _z_f_link_read_exact_raweth(const _z_link_t *self, uint8_t *ptr, size_t len, _z_bytes_t *addr); +static uint16_t _z_get_link_mtu_raweth(void); static _Bool _z_valid_iface_raweth(_z_str_intmap_t *config) { const char *iface = _z_str_intmap_get(config, RAWETH_CONFIG_IFACE_KEY); @@ -46,6 +93,220 @@ static const char *_z_get_iface_raweth(_z_str_intmap_t *config) { return _z_str_intmap_get(config, RAWETH_CONFIG_IFACE_KEY); } +static _Bool _z_valid_ethtype_raweth(_z_str_intmap_t *config) { + const char *s_ethtype = _z_str_intmap_get(config, RAWETH_CONFIG_ETHTYPE_KEY); + if (s_ethtype == NULL) { + return false; + } + long ethtype = strtol(s_ethtype, NULL, 16); + return (_z_raweth_htons(ethtype) > 0x600); // Ethtype must be at least 0x600 in network order +} + +static long _z_get_ethtype_raweth(_z_str_intmap_t *config) { + const char *s_ethtype = _z_str_intmap_get(config, RAWETH_CONFIG_ETHTYPE_KEY); + return strtol(s_ethtype, NULL, 16); +} + +static size_t _z_valid_mapping_raweth(_z_str_intmap_t *config) { + // Retrieve list + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_MAPPING_KEY); + if (cfg_str == NULL) { + return 0; + } + char *s_mapping = z_malloc(strlen(cfg_str)); + if (s_mapping == NULL) { + return 0; + } + size_t size = 0; + strcpy(s_mapping, cfg_str); + // Parse list + const char *delim = RAWETH_CFG_LIST_SEPARATOR; + char *entry = strtok(s_mapping, delim); + while (entry != NULL) { + // Check entry + if (!_z_valid_mapping_entry(entry)) { + z_free(s_mapping); + return 0; + } + size++; + entry = strtok(NULL, delim); + } + // Clean up + z_free(s_mapping); + return size; +} + +static int8_t _z_get_mapping_raweth(_z_str_intmap_t *config, _zp_raweth_mapping_array_t *array, size_t size) { + // Retrieve data + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_MAPPING_KEY); + if (cfg_str == NULL) { + return _Z_ERR_GENERIC; + } + // Copy data + char *s_mapping = z_malloc(strlen(cfg_str)); + if (s_mapping == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + strcpy(s_mapping, cfg_str); + // Allocate array + *array = _zp_raweth_mapping_array_make(size); + if (_zp_raweth_mapping_array_len(array) == 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + size_t idx = 0; + // Parse list + const char *delim = RAWETH_CFG_LIST_SEPARATOR; + char *entry = strtok(s_mapping, delim); + while ((entry != NULL) && (idx < _zp_raweth_mapping_array_len(array))) { + // Copy data into array + _Z_CLEAN_RETURN_IF_ERR(_z_get_mapping_entry(entry, _zp_raweth_mapping_array_get(array, idx)), + z_free(s_mapping)); + // Next iteration + idx++; + entry = strtok(NULL, delim); + } + // Clean up + z_free(s_mapping); + return _Z_RES_OK; +} + +static const size_t _z_valid_whitelist_raweth(_z_str_intmap_t *config) { + // Retrieve data + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_WHITELIST_KEY); + if (cfg_str == NULL) { + return 0; + } + // Copy data + char *s_whitelist = z_malloc(strlen(cfg_str)); + if (s_whitelist == NULL) { + return 0; + } + strcpy(s_whitelist, cfg_str); + // Parse list + size_t size = 0; + const char *delim = RAWETH_CFG_LIST_SEPARATOR; + char *entry = strtok(s_whitelist, delim); + while (entry != NULL) { + // Check entry + if (!_z_valid_address_raweth(entry)) { + z_free(s_whitelist); + return 0; + } + size++; + entry = strtok(NULL, delim); + } + // Parse last entry + + // Clean up + z_free(s_whitelist); + return size; +} + +static int8_t _z_get_whitelist_raweth(_z_str_intmap_t *config, _zp_raweth_whitelist_array_t *array, size_t size) { + // Retrieve data + const char *cfg_str = _z_str_intmap_get(config, RAWETH_CONFIG_WHITELIST_KEY); + if (cfg_str == NULL) { + return _Z_ERR_GENERIC; + } + // Copy data + char *s_whitelist = z_malloc(strlen(cfg_str)); + if (s_whitelist == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + strcpy(s_whitelist, cfg_str); + // Allocate array + *array = _zp_raweth_whitelist_array_make(size); + if (_zp_raweth_whitelist_array_len(array) == 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + size_t idx = 0; + // Parse list + const char *delim = RAWETH_CFG_LIST_SEPARATOR; + char *entry = strtok(s_whitelist, delim); + while ((entry != NULL) && (idx < _zp_raweth_whitelist_array_len(array))) { + // Convert address from string to int array + uint8_t *addr = _z_parse_address_raweth(entry); + if (addr == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + // Copy address to entry + _zp_raweth_whitelist_entry_t *elem = _zp_raweth_whitelist_array_get(array, idx); + memcpy(elem->_mac, addr, _ZP_MAC_ADDR_LENGTH); + z_free(addr); + // Next iteration + idx++; + entry = strtok(NULL, delim); + } + // Clean up + z_free(s_whitelist); + return _Z_RES_OK; +} + +static int8_t _z_get_mapping_entry(char *entry, _zp_raweth_mapping_entry_t *storage) { + size_t len = strlen(entry); + const char *entry_end = &entry[len - (size_t)1]; + + // Get first tuple member (keyexpr) + char *p_start = &entry[0]; + char *p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + size_t ke_len = (uintptr_t)p_end - (uintptr_t)p_start; + char *ke_suffix = (char *)z_malloc(ke_len); + if (ke_suffix == NULL) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + memcpy(ke_suffix, p_start, ke_len); + storage->_keyexpr = _z_rid_with_suffix(Z_RESOURCE_ID_NONE, ke_suffix); + + // Check second entry (address) + p_start = p_end; + p_start++; + p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + *p_end = '\0'; + uint8_t *addr = _z_parse_address_raweth(p_start); + memcpy(storage->_dmac, addr, _ZP_MAC_ADDR_LENGTH); + z_free(addr); + *p_end = RAWETH_CFG_TUPLE_SEPARATOR; + + // Check optional third entry (vlan id) + p_start = p_end; + p_start++; + if (p_start >= entry_end) { // No entry + storage->_has_vlan = false; + } else { + storage->_has_vlan = true; + storage->_vlan = (uint16_t)strtol(p_start, NULL, 16); + } + return _Z_RES_OK; +} +static _Bool _z_valid_mapping_entry(char *entry) { + size_t len = strlen(entry); + const char *entry_end = &entry[len - (size_t)1]; + + // Check first tuple member (keyexpr) + char *p_start = &entry[0]; + char *p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + if (p_end == NULL) { + return false; + } + // Check second entry (address) + p_start = p_end; + p_start++; + if (p_start > entry_end) { + return false; + } + p_end = strchr(p_start, RAWETH_CFG_TUPLE_SEPARATOR); + if (p_end == NULL) { + return false; + } + *p_end = '\0'; + if (!_z_valid_address_raweth(p_start)) { + *p_end = RAWETH_CFG_TUPLE_SEPARATOR; + return false; + } + *p_end = RAWETH_CFG_TUPLE_SEPARATOR; + return true; +} + static _Bool _z_valid_address_raweth(const char *address) { // Check if the string has the correct length size_t len = strlen(address); @@ -73,7 +334,7 @@ static _Bool _z_valid_address_raweth(const char *address) { static uint8_t *_z_parse_address_raweth(const char *address) { size_t len = strlen(address); // Allocate data - uint8_t *ret = (uint8_t *)zp_malloc(_ZP_MAC_ADDR_LENGTH); + uint8_t *ret = (uint8_t *)z_malloc(_ZP_MAC_ADDR_LENGTH); if (ret == NULL) { return ret; } @@ -89,19 +350,51 @@ static uint8_t *_z_parse_address_raweth(const char *address) { } static int8_t _z_f_link_open_raweth(_z_link_t *self) { + // Init arrays + self->_socket._raweth._mapping = _zp_raweth_mapping_array_empty(); + self->_socket._raweth._whitelist = _zp_raweth_whitelist_array_empty(); // Init socket smac if (_z_valid_address_raweth(self->_endpoint._locator._address)) { uint8_t *addr = _z_parse_address_raweth(self->_endpoint._locator._address); memcpy(&self->_socket._raweth._smac, addr, _ZP_MAC_ADDR_LENGTH); - zp_free(addr); + z_free(addr); } else { - memcpy(&self->_socket._raweth._smac, _ZP_RAWETH_CFG_SMAC, _ZP_MAC_ADDR_LENGTH); + _Z_DEBUG("Invalid locator source mac addr, using default value."); + memcpy(&self->_socket._raweth._smac, _ZP_RAWETH_DEFAULT_SMAC, _ZP_MAC_ADDR_LENGTH); } // Init socket interface if (_z_valid_iface_raweth(&self->_endpoint._config)) { self->_socket._raweth._interface = _z_get_iface_raweth(&self->_endpoint._config); } else { - self->_socket._raweth._interface = _ZP_RAWETH_CFG_INTERFACE; + _Z_DEBUG("Invalid locator interface, using default value %s", _ZP_RAWETH_DEFAULT_INTERFACE); + self->_socket._raweth._interface = _ZP_RAWETH_DEFAULT_INTERFACE; + } + // Init socket ethtype + if (_z_valid_ethtype_raweth(&self->_endpoint._config)) { + self->_socket._raweth._ethtype = (uint16_t)_z_get_ethtype_raweth(&self->_endpoint._config); + } else { + _Z_DEBUG("Invalid locator ethtype, using default value 0x%04x", _ZP_RAWETH_DEFAULT_ETHTYPE); + self->_socket._raweth._ethtype = _ZP_RAWETH_DEFAULT_ETHTYPE; + } + // Init socket mapping + size_t size = _z_valid_mapping_raweth(&self->_endpoint._config); + if (size != (size_t)0) { + _Z_RETURN_IF_ERR(_z_get_mapping_raweth(&self->_endpoint._config, &self->_socket._raweth._mapping, size)); + } else { + _Z_DEBUG("Invalid locator mapping, using default value."); + self->_socket._raweth._mapping = _zp_raweth_mapping_array_make(1); + if (_zp_raweth_mapping_array_len(&self->_socket._raweth._mapping) == 0) { + return _Z_ERR_SYSTEM_OUT_OF_MEMORY; + } + _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&self->_socket._raweth._mapping, 0); + *entry = _ZP_RAWETH_DEFAULT_MAPPING; + } + // Init socket whitelist + size = _z_valid_whitelist_raweth(&self->_endpoint._config); + if (size != (size_t)0) { + _Z_RETURN_IF_ERR(_z_get_whitelist_raweth(&self->_endpoint._config, &self->_socket._raweth._whitelist, size)); + } else { + _Z_DEBUG("Invalid locator whitelist, filtering deactivated."); } // Open raweth link return _z_open_raweth(&self->_socket._raweth._sock, self->_socket._raweth._interface); @@ -109,7 +402,15 @@ static int8_t _z_f_link_open_raweth(_z_link_t *self) { static int8_t _z_f_link_listen_raweth(_z_link_t *self) { return _z_f_link_open_raweth(self); } -static void _z_f_link_close_raweth(_z_link_t *self) { _z_close_raweth(&self->_socket._raweth._sock); } +static void _z_f_link_close_raweth(_z_link_t *self) { + // Close connection + _z_close_raweth(&self->_socket._raweth._sock); + // Clear config + _zp_raweth_mapping_array_clear(&self->_socket._raweth._mapping); + if (_zp_raweth_whitelist_array_len(&self->_socket._raweth._whitelist) != 0) { + _zp_raweth_whitelist_array_clear(&self->_socket._raweth._whitelist); + } +} static void _z_f_link_free_raweth(_z_link_t *self) { _ZP_UNUSED(self); } diff --git a/src/transport/raweth/read.c b/src/transport/raweth/read.c index 431af3c4f..227f8cc55 100644 --- a/src/transport/raweth/read.c +++ b/src/transport/raweth/read.c @@ -36,6 +36,7 @@ int8_t _zp_raweth_read(_z_transport_multicast_t *ztm) { ret = _z_multicast_handle_transport_message(ztm, &t_msg, &addr); _z_t_msg_clear(&t_msg); } + _z_bytes_clear(&addr); return ret; } #else @@ -63,18 +64,21 @@ void *_zp_raweth_read_task(void *ztm_arg) { break; case _Z_ERR_TRANSPORT_RX_FAILED: // Drop message + _z_bytes_clear(&addr); continue; break; default: // Drop message & stop task _Z_ERROR("Connection closed due to malformed message"); ztm->_read_task_running = false; + _z_bytes_clear(&addr); continue; break; } // Process message if (_z_multicast_handle_transport_message(ztm, &t_msg, &addr) != _Z_RES_OK) { ztm->_read_task_running = false; + _z_bytes_clear(&addr); continue; } _z_t_msg_clear(&t_msg); @@ -83,11 +87,11 @@ void *_zp_raweth_read_task(void *ztm_arg) { return NULL; } -int8_t _zp_raweth_start_read_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task) { +int8_t _zp_raweth_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(zp_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); // Init task - if (zp_task_init(task, attr, _zp_raweth_read_task, &zt->_transport._raweth) != _Z_RES_OK) { + if (z_task_init(task, attr, _zp_raweth_read_task, &zt->_transport._raweth) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/raweth/rx.c b/src/transport/raweth/rx.c index 162c66c28..6732532ab 100644 --- a/src/transport/raweth/rx.c +++ b/src/transport/raweth/rx.c @@ -29,7 +29,8 @@ static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_bytes_t *addr) { uint8_t *buff = _z_zbuf_get_wptr(zbf); - size_t rb = _z_receive_raweth(&link->_socket._raweth._sock, buff, _z_zbuf_space_left(zbf), addr); + size_t rb = _z_receive_raweth(&link->_socket._raweth._sock, buff, _z_zbuf_space_left(zbf), addr, + &link->_socket._raweth._whitelist); // Check validity if ((rb == SIZE_MAX) || (rb < sizeof(_zp_eth_header_t))) { return SIZE_MAX; @@ -78,7 +79,7 @@ int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_messa #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - zp_mutex_lock(&ztm->_mutex_rx); + z_mutex_lock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Prepare the buffer @@ -106,7 +107,7 @@ int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_messa } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_rx); + z_mutex_unlock(&ztm->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; diff --git a/src/transport/raweth/tx.c b/src/transport/raweth/tx.c index ac360cc9e..a3cf15a8d 100644 --- a/src/transport/raweth/tx.c +++ b/src/transport/raweth/tx.c @@ -24,7 +24,6 @@ #include "zenoh-pico/protocol/iobuf.h" #include "zenoh-pico/protocol/keyexpr.h" #include "zenoh-pico/system/link/raweth.h" -#include "zenoh-pico/transport/raweth/config.h" #include "zenoh-pico/transport/transport.h" #include "zenoh-pico/transport/utils.h" #include "zenoh-pico/utils/logging.h" @@ -32,16 +31,16 @@ #if Z_FEATURE_RAWETH_TRANSPORT == 1 #if Z_FEATURE_MULTI_THREAD == 1 -static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { zp_mutex_unlock(&ztm->_mutex_tx); } +static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { z_mutex_unlock(&ztm->_mutex_tx); } #else static void _zp_raweth_unlock_tx_mutex(_z_transport_multicast_t *ztm) { _ZP_UNUSED(ztm); } #endif static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { - for (int i = 1; i < _ZP_RAWETH_CFG_SIZE; i++) { + for (int i = 0; i < _zp_raweth_mapping_array_len(&sock->_mapping); i++) { // Find matching keyexpr - if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, _ZP_RAWETH_CFG_ARRAY[i]._keyexpr._suffix) != - _Z_RES_OK) { + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, i); + if (zp_keyexpr_intersect_null_terminated(keyexpr->_suffix, entry->_keyexpr._suffix) != _Z_RES_OK) { continue; } return i; @@ -52,14 +51,15 @@ static int _zp_raweth_find_map_entry(const _z_keyexpr_t *keyexpr, _z_raweth_sock static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socket_t *sock) { int8_t ret = _Z_RES_OK; - if (_ZP_RAWETH_CFG_SIZE < 1) { + if (_zp_raweth_mapping_array_len(&sock->_mapping) < 1) { return _Z_ERR_GENERIC; } if (keyexpr == NULL) { // Store default value into socket - memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_ARRAY[0]._dmac, _ZP_MAC_ADDR_LENGTH); - uint16_t vlan = _ZP_RAWETH_CFG_ARRAY[0]._vlan; - sock->_has_vlan = _ZP_RAWETH_CFG_ARRAY[0]._has_vlan; + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, 0); + memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); + uint16_t vlan = entry->_vlan; + sock->_has_vlan = entry->_has_vlan; if (sock->_has_vlan) { memcpy(&sock->_vlan, &vlan, sizeof(vlan)); } @@ -72,9 +72,10 @@ static int8_t _zp_raweth_set_socket(const _z_keyexpr_t *keyexpr, _z_raweth_socke _Z_DEBUG("Key '%s' wasn't found in config mapping, sending to default address", keyexpr->_suffix); } // Store data into socket - memcpy(&sock->_dmac, &_ZP_RAWETH_CFG_ARRAY[idx]._dmac, _ZP_MAC_ADDR_LENGTH); - uint16_t vlan = _ZP_RAWETH_CFG_ARRAY[idx]._vlan; - sock->_has_vlan = _ZP_RAWETH_CFG_ARRAY[idx]._has_vlan; + const _zp_raweth_mapping_entry_t *entry = _zp_raweth_mapping_array_get(&sock->_mapping, idx); + memcpy(&sock->_dmac, &entry->_dmac, _ZP_MAC_ADDR_LENGTH); + uint16_t vlan = entry->_vlan; + sock->_has_vlan = entry->_has_vlan; if (sock->_has_vlan) { memcpy(&sock->_vlan, &vlan, sizeof(vlan)); } @@ -128,7 +129,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH); header.vlan_type = _ZP_ETH_TYPE_VLAN; header.tag = resocket->_vlan; - header.ethtype = _ZP_RAWETH_CFG_ETHTYPE; + header.ethtype = resocket->_ethtype; header.data_length = _z_raweth_htons(wpos - sizeof(header)); // Write header _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (uint8_t *)&header, 0, sizeof(header))); @@ -137,7 +138,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_link_t *zl, _z_wbuf_t *wbf) { // Set header memcpy(&header.dmac, &resocket->_dmac, _ZP_MAC_ADDR_LENGTH); memcpy(&header.smac, &resocket->_smac, _ZP_MAC_ADDR_LENGTH); - header.ethtype = _ZP_RAWETH_CFG_ETHTYPE; + header.ethtype = resocket->_ethtype; header.data_length = _z_raweth_htons(wpos - sizeof(header)); // Write header _Z_RETURN_IF_ERR(_z_wbuf_write_bytes(wbf, (uint8_t *)&header, 0, sizeof(header))); @@ -195,7 +196,7 @@ int8_t _z_raweth_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport_me _Z_DEBUG(">> send session message"); #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_lock(&ztm->_mutex_tx); + z_mutex_lock(&ztm->_mutex_tx); #endif // Reset wbuf _z_wbuf_reset(&ztm->_wbuf); @@ -213,7 +214,7 @@ int8_t _z_raweth_send_t_msg(_z_transport_multicast_t *ztm, const _z_transport_me ztm->_transmitted = true; #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_tx); + z_mutex_unlock(&ztm->_mutex_tx); #endif return ret; @@ -228,9 +229,9 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, // Acquire the lock and drop the message if needed #if Z_FEATURE_MULTI_THREAD == 1 if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { - zp_mutex_lock(&ztm->_mutex_tx); + z_mutex_lock(&ztm->_mutex_tx); } else { - if (zp_mutex_trylock(&ztm->_mutex_tx) != (int8_t)0) { + if (z_mutex_trylock(&ztm->_mutex_tx) != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control"); // We failed to acquire the lock, drop the message return ret; @@ -313,7 +314,7 @@ int8_t _z_raweth_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg, #endif } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztm->_mutex_tx); + z_mutex_unlock(&ztm->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; } diff --git a/src/transport/transport.c b/src/transport/transport.c index 3ced940ca..a53b7e364 100644 --- a/src/transport/transport.c +++ b/src/transport/transport.c @@ -73,7 +73,7 @@ void _z_transport_free(_z_transport_t **zt) { } // Clear and free transport _z_transport_clear(ptr); - zp_free(ptr); + z_free(ptr); *zt = NULL; } diff --git a/src/transport/unicast/lease.c b/src/transport/unicast/lease.c index a8d929f63..03d0ffd1b 100644 --- a/src/transport/unicast/lease.c +++ b/src/transport/unicast/lease.c @@ -87,7 +87,7 @@ void *_zp_unicast_lease_task(void *ztu_arg) { } // The keep alive and lease intervals are expressed in milliseconds - zp_sleep_ms(interval); + z_sleep_ms(interval); next_lease = next_lease - interval; next_keep_alive = next_keep_alive - interval; @@ -95,11 +95,11 @@ void *_zp_unicast_lease_task(void *ztu_arg) { return 0; } -int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task) { +int8_t _zp_unicast_start_lease_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(zp_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); // Init task - if (zp_task_init(task, attr, _zp_unicast_lease_task, &zt->_transport._unicast) != _Z_RES_OK) { + if (z_task_init(task, attr, _zp_unicast_lease_task, &zt->_transport._unicast) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/unicast/read.c b/src/transport/unicast/read.c index 3b02ff597..80e35180c 100644 --- a/src/transport/unicast/read.c +++ b/src/transport/unicast/read.c @@ -49,7 +49,7 @@ void *_zp_unicast_read_task(void *ztu_arg) { _z_transport_unicast_t *ztu = (_z_transport_unicast_t *)ztu_arg; // Acquire and keep the lock - zp_mutex_lock(&ztu->_mutex_rx); + z_mutex_lock(&ztu->_mutex_rx); // Prepare the buffer _z_zbuf_reset(&ztu->_zbuf); @@ -117,15 +117,15 @@ void *_zp_unicast_read_task(void *ztu_arg) { // Move the read position of the read buffer _z_zbuf_set_rpos(&ztu->_zbuf, _z_zbuf_get_rpos(&ztu->_zbuf) + to_read); } - zp_mutex_unlock(&ztu->_mutex_rx); + z_mutex_unlock(&ztu->_mutex_rx); return NULL; } -int8_t _zp_unicast_start_read_task(_z_transport_t *zt, zp_task_attr_t *attr, zp_task_t *task) { +int8_t _zp_unicast_start_read_task(_z_transport_t *zt, z_task_attr_t *attr, z_task_t *task) { // Init memory - (void)memset(task, 0, sizeof(zp_task_t)); + (void)memset(task, 0, sizeof(z_task_t)); // Init task - if (zp_task_init(task, attr, _zp_unicast_read_task, &zt->_transport._unicast) != _Z_RES_OK) { + if (z_task_init(task, attr, _zp_unicast_read_task, &zt->_transport._unicast) != _Z_RES_OK) { return _Z_ERR_SYSTEM_TASK_FAILED; } // Attach task diff --git a/src/transport/unicast/rx.c b/src/transport/unicast/rx.c index 3ed08a0a6..6278ca735 100644 --- a/src/transport/unicast/rx.c +++ b/src/transport/unicast/rx.c @@ -32,7 +32,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag int8_t ret = _Z_RES_OK; #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - zp_mutex_lock(&ztu->_mutex_rx); + z_mutex_lock(&ztu->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 size_t to_read = 0; @@ -85,7 +85,7 @@ int8_t _z_unicast_recv_t_msg_na(_z_transport_unicast_t *ztu, _z_transport_messag } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztu->_mutex_rx); + z_mutex_unlock(&ztu->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; diff --git a/src/transport/unicast/transport.c b/src/transport/unicast/transport.c index b919e3339..11e7467e6 100644 --- a/src/transport/unicast/transport.c +++ b/src/transport/unicast/transport.c @@ -38,11 +38,11 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo #if Z_FEATURE_MULTI_THREAD == 1 // Initialize the mutexes - ret = zp_mutex_init(&zt->_transport._unicast._mutex_tx); + ret = z_mutex_init(&zt->_transport._unicast._mutex_tx); if (ret == _Z_RES_OK) { - ret = zp_mutex_init(&zt->_transport._unicast._mutex_rx); + ret = z_mutex_init(&zt->_transport._unicast._mutex_rx); if (ret != _Z_RES_OK) { - zp_mutex_free(&zt->_transport._unicast._mutex_tx); + z_mutex_free(&zt->_transport._unicast._mutex_tx); } } #endif // Z_FEATURE_MULTI_THREAD == 1 @@ -86,8 +86,8 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo _Z_ERROR("Not enough memory to allocate transport tx rx buffers!"); #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_free(&zt->_transport._unicast._mutex_tx); - zp_mutex_free(&zt->_transport._unicast._mutex_rx); + z_mutex_free(&zt->_transport._unicast._mutex_tx); + z_mutex_free(&zt->_transport._unicast._mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_wbuf_clear(&zt->_transport._unicast._wbuf); @@ -115,14 +115,17 @@ int8_t _z_unicast_transport_create(_z_transport_t *zt, _z_link_t *zl, _z_transpo _z_wbuf_clear(&zt->_transport._unicast._dbuf_best_effort); #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_free(&zt->_transport._unicast._mutex_tx); - zp_mutex_free(&zt->_transport._unicast._mutex_rx); + z_mutex_free(&zt->_transport._unicast._mutex_tx); + z_mutex_free(&zt->_transport._unicast._mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 _z_wbuf_clear(&zt->_transport._unicast._wbuf); _z_zbuf_clear(&zt->_transport._unicast._zbuf); } -#endif +#else + _ZP_UNUSED(dbuf_size); + _ZP_UNUSED(expandable); +#endif // Z_FEATURE_FRAGMENTATION == 1 } if (ret == _Z_RES_OK) { @@ -212,7 +215,7 @@ int8_t _z_unicast_open_client(_z_transport_unicast_establish_param_t *param, con param->_req_id_res = 0x08 << param->_req_id_res; // The initial SN at TX side - zp_random_fill(¶m->_initial_sn_tx, sizeof(param->_initial_sn_tx)); + z_random_fill(¶m->_initial_sn_tx, sizeof(param->_initial_sn_tx)); param->_initial_sn_tx = param->_initial_sn_tx & !_z_sn_modulo_mask(param->_seq_num_res); // Initialize the Local and Remote Peer IDs @@ -287,17 +290,17 @@ void _z_unicast_transport_clear(_z_transport_t *zt) { #if Z_FEATURE_MULTI_THREAD == 1 // Clean up tasks if (ztu->_read_task != NULL) { - zp_task_join(ztu->_read_task); - zp_task_free(&ztu->_read_task); + z_task_join(ztu->_read_task); + z_task_free(&ztu->_read_task); } if (ztu->_lease_task != NULL) { - zp_task_join(ztu->_lease_task); - zp_task_free(&ztu->_lease_task); + z_task_join(ztu->_lease_task); + z_task_free(&ztu->_lease_task); } // Clean up the mutexes - zp_mutex_free(&ztu->_mutex_tx); - zp_mutex_free(&ztu->_mutex_rx); + z_mutex_free(&ztu->_mutex_tx); + z_mutex_free(&ztu->_mutex_rx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Clean up the buffers diff --git a/src/transport/unicast/tx.c b/src/transport/unicast/tx.c index b89b7c0e9..3eb220e56 100644 --- a/src/transport/unicast/tx.c +++ b/src/transport/unicast/tx.c @@ -49,7 +49,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes #if Z_FEATURE_MULTI_THREAD == 1 // Acquire the lock - zp_mutex_lock(&ztu->_mutex_tx); + z_mutex_lock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 // Prepare the buffer eventually reserving space for the message length @@ -68,7 +68,7 @@ int8_t _z_unicast_send_t_msg(_z_transport_unicast_t *ztu, const _z_transport_mes } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztu->_mutex_tx); + z_mutex_unlock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 return ret; @@ -85,11 +85,11 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg _Bool drop = false; if (cong_ctrl == Z_CONGESTION_CONTROL_BLOCK) { #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_lock(&ztu->_mutex_tx); + z_mutex_lock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } else { #if Z_FEATURE_MULTI_THREAD == 1 - int8_t locked = zp_mutex_trylock(&ztu->_mutex_tx); + int8_t locked = z_mutex_trylock(&ztu->_mutex_tx); if (locked != (int8_t)0) { _Z_INFO("Dropping zenoh message because of congestion control"); // We failed to acquire the lock, drop the message @@ -163,7 +163,7 @@ int8_t _z_unicast_send_n_msg(_z_session_t *zn, const _z_network_message_t *n_msg } #if Z_FEATURE_MULTI_THREAD == 1 - zp_mutex_unlock(&ztu->_mutex_tx); + z_mutex_unlock(&ztu->_mutex_tx); #endif // Z_FEATURE_MULTI_THREAD == 1 } diff --git a/tests/modularity.py b/tests/modularity.py index 662236a1d..000b6b90a 100644 --- a/tests/modularity.py +++ b/tests/modularity.py @@ -1,4 +1,6 @@ import argparse +import os +from signal import SIGINT import subprocess import sys import time @@ -15,16 +17,17 @@ def pub_and_sub(args): z_pub_expected_status = 0 z_pub_expected_output = '''Opening session... Declaring publisher for 'demo/example/zenoh-pico-pub'... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')...''' +Press CTRL-C to quit... +Putting Data ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')...''' else : z_pub_expected_status = 254 z_pub_expected_output = ("ERROR: Zenoh pico was compiled without " @@ -32,25 +35,25 @@ def pub_and_sub(args): # Expected z_sub output & status if args.sub == 1: - z_sub_expected_status = 0 + z_sub_expected_status = -2 if args.pub == 1: z_sub_expected_output = '''Opening session... Declaring Subscriber on 'demo/example/**'... -Enter 'q' to quit... ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!')''' +Press CTRL-C to quit... +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')''' else: z_sub_expected_output = '''Opening session... Declaring Subscriber on 'demo/example/**'... -Enter 'q' to quit...''' +Press CTRL-C to quit...''' else : z_sub_expected_status = 254 z_sub_expected_output = ("ERROR: Zenoh pico was compiled without " @@ -58,19 +61,21 @@ def pub_and_sub(args): print("Start subscriber") # Start z_sub in the background - z_sub_command = f"./{DIR_EXAMPLES}/z_sub" + z_sub_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_sub" z_sub_process = subprocess.Popen(z_sub_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True) + stderr=subprocess.PIPE, + start_new_session=True, + text=True) # Introduce a delay to ensure z_sub starts time.sleep(2) print("Start publisher") # Start z_pub - z_pub_command = f"./{DIR_EXAMPLES}/z_pub" + z_pub_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_pub -n 10" z_pub_process = subprocess.Popen(z_pub_command, shell=True, stdin=subprocess.PIPE, @@ -83,9 +88,9 @@ def pub_and_sub(args): print("Stop subscriber") if z_sub_process.poll() is None: - # Send "q" command to z_sub to stop it - z_sub_process.stdin.write("q\n") - z_sub_process.stdin.flush() + # send SIGINT to group + z_sub_process_gid = os.getpgid(z_sub_process.pid) + os.killpg(z_sub_process_gid, SIGINT) # Wait for z_sub to finish z_sub_process.wait() @@ -139,44 +144,30 @@ def query_and_queryable(args): z_query_expected_status = 0 if args.queryable == 1: z_query_expected_output = '''Opening session... -Enter any key to pull data or 'q' to quit... -Sending Query 'demo/example/**'... ->> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!') ->> Received query final notification -Sending Query 'demo/example/**'... ->> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!') ->> Received query final notification Sending Query 'demo/example/**'... >> Received ('demo/example/zenoh-pico-queryable': 'Queryable from Pico!') >> Received query final notification''' else: z_query_expected_output = '''Opening session... -Enter any key to pull data or 'q' to quit... -Sending Query 'demo/example/**'... ->> Received query final notification -Sending Query 'demo/example/**'... ->> Received query final notification Sending Query 'demo/example/**'... >> Received query final notification''' else : z_query_expected_status = 254 z_query_expected_output = ("ERROR: Zenoh pico was compiled without " - "Z_FEATURE_QUERY but this example requires it.") + "Z_FEATURE_QUERY or Z_FEATURE_MULTI_THREAD but this example requires them.") # Expected z_queryable output & status if args.queryable == 1: - z_queryable_expected_status = 0 + z_queryable_expected_status = -2 if args.query == 1: z_queryable_expected_output = '''Opening session... Creating Queryable on 'demo/example/zenoh-pico-queryable'... -Enter 'q' to quit... - >> [Queryable handler] Received Query 'demo/example/**?' - >> [Queryable handler] Received Query 'demo/example/**?' +Press CTRL-C to quit... >> [Queryable handler] Received Query 'demo/example/**?''' else: z_queryable_expected_output = '''Opening session... Creating Queryable on 'demo/example/zenoh-pico-queryable'... -Enter 'q' to quit...''' +Press CTRL-C to quit...''' else : z_queryable_expected_status = 254 z_queryable_expected_output = ("ERROR: Zenoh pico was compiled without " @@ -184,12 +175,13 @@ def query_and_queryable(args): print("Start queryable") # Start z_queryable in the background - z_queryable_command = f"./{DIR_EXAMPLES}/z_queryable" + z_queryable_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_queryable" z_queryable_process = subprocess.Popen(z_queryable_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + start_new_session=True, text=True) # Introduce a delay to ensure z_queryable starts @@ -197,7 +189,7 @@ def query_and_queryable(args): print("Start query") # Start z_query - z_query_command = f"./{DIR_EXAMPLES}/z_get" + z_query_command = f"stdbuf -oL -eL ./{DIR_EXAMPLES}/z_get" z_query_process = subprocess.Popen(z_query_command, shell=True, stdin=subprocess.PIPE, @@ -205,34 +197,14 @@ def query_and_queryable(args): stderr=subprocess.PIPE, text=True) - # Introduce a delay to ensure z_query starts - time.sleep(2) - - print("Send requests") - if z_query_process.poll() is None: - z_query_process.stdin.write("\n") - z_query_process.stdin.flush() - time.sleep(1) - z_query_process.stdin.write("\n") - z_query_process.stdin.flush() - time.sleep(1) - z_query_process.stdin.write("\n") - z_query_process.stdin.flush() - time.sleep(1) - - print("Stop query") - if z_query_process.poll() is None: - z_query_process.stdin.write("q\n") - z_query_process.stdin.flush() - # Wait for z_query to finish z_query_process.wait() print("Stop queryable") if z_queryable_process.poll() is None: - # Send "q" command to z_sub to stop it - z_queryable_process.stdin.write("q\n") - z_queryable_process.stdin.flush() + # send SIGINT to group + z_quaryable_process_gid = os.getpgid(z_queryable_process.pid) + os.killpg(z_quaryable_process_gid, SIGINT) # Wait for z_queryable to finish z_queryable_process.wait() diff --git a/tests/raweth.py b/tests/raweth.py index 8f14e3b78..41c6fcc4a 100644 --- a/tests/raweth.py +++ b/tests/raweth.py @@ -1,4 +1,6 @@ import argparse +import os +from signal import SIGINT import subprocess import sys import time @@ -15,16 +17,18 @@ def pub_and_sub(args): z_pub_expected_status = 0 z_pub_expected_output = '''Opening session... Declaring publisher for 'demo/example/zenoh-pico-pub'... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')...''' +Waiting for joins... +Press CTRL-C to quit... +Putting Data ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')...''' else : z_pub_expected_status = 255 z_pub_expected_output = '''Opening session... @@ -35,17 +39,17 @@ def pub_and_sub(args): z_sub_expected_status = 0 z_sub_expected_output = '''Opening session... Declaring Subscriber on 'demo/example/**'... -Enter 'q' to quit... ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!')''' +Press CTRL-C to quit... +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')''' else : z_sub_expected_status = 255 z_sub_expected_output = '''Opening session... @@ -53,19 +57,20 @@ def pub_and_sub(args): print("Start subscriber") # Start z_sub in the background - z_sub_command = f"sudo ./{DIR_EXAMPLES}/z_sub -m \"peer\" -l \"reth/0\"s" + z_sub_command = f"sudo stdbuf -oL -eL ./{DIR_EXAMPLES}/z_sub -n 10 -m \"peer\" -l \"reth/0\"s" z_sub_process = subprocess.Popen(z_sub_command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, text=True) + stderr=subprocess.PIPE, + text=True) # Introduce a delay to ensure z_sub starts time.sleep(2) print("Start publisher") # Start z_pub - z_pub_command = f"sudo ./{DIR_EXAMPLES}/z_pub -m \"peer\" -l \"reth/0\"s" + z_pub_command = f"sudo stdbuf -oL -eL ./{DIR_EXAMPLES}/z_pub -n 10 -m \"peer\" -l \"reth/0\"s" z_pub_process = subprocess.Popen(z_pub_command, shell=True, stdin=subprocess.PIPE, @@ -77,10 +82,11 @@ def pub_and_sub(args): z_pub_process.wait() print("Stop subscriber") + time.sleep(2) if z_sub_process.poll() is None: - # Send "q" command to z_sub to stop it - z_sub_process.stdin.write("q\n") - z_sub_process.stdin.flush() + # send SIGINT to group + z_sub_process_gid = os.getpgid(z_sub_process.pid) + os.killpg(z_sub_process_gid, SIGINT) # Wait for z_sub to finish z_sub_process.wait() diff --git a/tests/single_thread.py b/tests/single_thread.py index cffd7b82b..00a4955b8 100644 --- a/tests/single_thread.py +++ b/tests/single_thread.py @@ -14,31 +14,33 @@ def pub_and_sub(): z_pub_expected_status = 0 z_pub_expected_output = '''Opening session... Declaring publisher for 'demo/example/zenoh-pico-pub'... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')... -Putting Data ('demo/example/zenoh-pico-pub': 'Pub from Pico!')...''' +Press CTRL-C to quit... +Putting Data ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!')... +Putting Data ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')...''' # Expected z_sub output z_sub_expected_status = 0 z_sub_expected_output = '''Opening session... Declaring Subscriber on 'demo/example/**'... ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!') ->> [Subscriber] Received ('demo/example/zenoh-pico-pub': 'Pub from Pico!')''' +Press CTRL-C to quit... +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 0] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 1] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 2] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 3] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 4] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 5] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 6] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 7] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 8] Pub from Pico!') +>> [Subscriber] Received ('demo/example/zenoh-pico-pub': '[ 9] Pub from Pico!')''' print("Start subscriber") # Start z_sub in the background @@ -55,7 +57,7 @@ def pub_and_sub(): print("Start publisher") # Start z_pub - z_pub_command = f"./{DIR_EXAMPLES}/z_pub_st" + z_pub_command = f"./{DIR_EXAMPLES}/z_pub_st -n 10" z_pub_process = subprocess.Popen(z_pub_command, shell=True, stdin=subprocess.PIPE, diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 84a3a53df..ac208fa68 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -215,9 +215,9 @@ int main(int argc, char **argv) { assert(hellos >= 1); uint32_t _scouting_timeout = strtoul(SCOUTING_TIMEOUT, NULL, 10); - zp_sleep_ms(_scouting_timeout); + z_sleep_ms(_scouting_timeout); printf("Ok\n"); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); z_owned_session_t s1 = z_open(z_move(_ret_config)); assert(z_check(s1)); diff --git a/tests/z_client_test.c b/tests/z_client_test.c index bbb73bd30..023798062 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -130,7 +130,7 @@ int main(int argc, char **argv) { zp_start_read_task(z_loan(s1), NULL); zp_start_lease_task(z_loan(s1), NULL); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(argv[1])); @@ -145,7 +145,7 @@ int main(int argc, char **argv) { zp_start_read_task(z_loan(s2), NULL); zp_start_lease_task(z_loan(s2), NULL); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Declare resources on both sessions char *s1_res = (char *)malloc(64); @@ -156,7 +156,7 @@ int main(int argc, char **argv) { rids1[i] = expr; } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); @@ -165,12 +165,12 @@ int main(int argc, char **argv) { rids2[i] = expr; } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Declare subscribers and queryabales on second session for (unsigned int i = 0; i < SET; i++) { z_owned_closure_sample_t callback = z_closure(data_handler, NULL, &idx[i]); - z_owned_subscriber_t *sub = (z_owned_subscriber_t *)zp_malloc(sizeof(z_owned_subscriber_t)); + z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_loan(rids2[i]), &callback, NULL); assert(z_check(*sub)); printf("Declared subscription on session 2: %ju %u %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_entity_id, @@ -178,12 +178,12 @@ int main(int argc, char **argv) { subs2 = _z_list_push(subs2, sub); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); for (unsigned int i = 0; i < SET; i++) { snprintf(s1_res, 64, "%s%u", uri, i); z_owned_closure_query_t callback = z_closure(query_handler, NULL, &idx[i]); - z_owned_queryable_t *qle = (z_owned_queryable_t *)zp_malloc(sizeof(z_owned_queryable_t)); + z_owned_queryable_t *qle = (z_owned_queryable_t *)z_malloc(sizeof(z_owned_queryable_t)); *qle = z_declare_queryable(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*qle)); printf("Declared queryable on session 2: %ju %zu %s\n", (uintmax_t)qle->_value->_entity_id, (z_zint_t)0, @@ -191,21 +191,21 @@ int main(int argc, char **argv) { qles2 = _z_list_push(qles2, qle); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Declare publisher on first session for (unsigned int i = 0; i < SET; i++) { - z_owned_publisher_t *pub = (z_owned_publisher_t *)zp_malloc(sizeof(z_owned_publisher_t)); + z_owned_publisher_t *pub = (z_owned_publisher_t *)z_malloc(sizeof(z_owned_publisher_t)); *pub = z_declare_publisher(z_loan(s1), z_loan(rids1[i]), NULL); if (!z_check(*pub)) printf("Declared publisher on session 1: %zu\n", z_loan(*pub)._val->_id); pubs1 = _z_list_push(pubs1, pub); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Write data from first session size_t len = MSG_LEN; - uint8_t *payload = (uint8_t *)zp_malloc(len); + uint8_t *payload = (uint8_t *)z_malloc(len); memset(payload, 1, MSG_LEN); total = MSG * SET; @@ -220,12 +220,12 @@ int main(int argc, char **argv) { } // Wait to receive all the data - zp_clock_t now = zp_clock_now(); + z_clock_t now = z_clock_now(); unsigned int expected = is_reliable ? total : 1; while (datas < expected) { - assert(zp_clock_elapsed_s(&now) < TIMEOUT); + assert(z_clock_elapsed_s(&now) < TIMEOUT); printf("Waiting for datas... %u/%u\n", datas, expected); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); } if (is_reliable == true) assert(datas == expected); @@ -233,13 +233,13 @@ int main(int argc, char **argv) { assert(datas >= expected); datas = 0; - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Write fragment data from first session if (is_reliable) { - zp_free((uint8_t *)payload); + z_free((uint8_t *)payload); len = FRAGMENT_MSG_LEN; - payload = (uint8_t *)zp_malloc(len); + payload = (uint8_t *)z_malloc(len); memset(payload, 1, FRAGMENT_MSG_LEN); total = FRAGMENT_MSG_NB * SET; @@ -253,17 +253,17 @@ int main(int argc, char **argv) { } } // Wait to receive all the data - now = zp_clock_now(); + now = z_clock_now(); while (datas < total) { - assert(zp_clock_elapsed_s(&now) < TIMEOUT); + assert(z_clock_elapsed_s(&now) < TIMEOUT); printf("Waiting for fragment datas... %u/%u\n", datas, total); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); } if (is_reliable == true) { assert(datas == total); } datas = 0; - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); } // Query data from first session @@ -278,12 +278,12 @@ int main(int argc, char **argv) { } // Wait to receive all the expected queries - now = zp_clock_now(); + now = z_clock_now(); expected = is_reliable ? total : 1; while (queries < expected) { - assert(zp_clock_elapsed_s(&now) < TIMEOUT); + assert(z_clock_elapsed_s(&now) < TIMEOUT); printf("Waiting for queries... %u/%u\n", queries, expected); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); } if (is_reliable == true) assert(queries == expected); @@ -292,11 +292,11 @@ int main(int argc, char **argv) { queries = 0; // Wait to receive all the expected replies - now = zp_clock_now(); + now = z_clock_now(); while (replies < expected) { - assert(zp_clock_elapsed_s(&now) < TIMEOUT); + assert(z_clock_elapsed_s(&now) < TIMEOUT); printf("Waiting for replies... %u/%u\n", replies, expected); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); } if (is_reliable == true) assert(replies == expected); @@ -304,7 +304,7 @@ int main(int argc, char **argv) { assert(replies >= expected); replies = 0; - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Undeclare publishers on first session while (pubs1) { @@ -314,7 +314,7 @@ int main(int argc, char **argv) { pubs1 = _z_list_pop(pubs1, _z_noop_elem_free, NULL); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Undeclare subscribers and queryables on second session while (subs2) { @@ -324,7 +324,7 @@ int main(int argc, char **argv) { subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); while (qles2) { z_owned_queryable_t *qle = _z_list_head(qles2); @@ -333,7 +333,7 @@ int main(int argc, char **argv) { qles2 = _z_list_pop(qles2, _z_noop_elem_free, NULL); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Undeclare resources on both sessions for (unsigned int i = 0; i < SET; i++) { @@ -341,14 +341,14 @@ int main(int argc, char **argv) { z_undeclare_keyexpr(z_loan(s1), z_move(rids1[i])); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); for (unsigned int i = 0; i < SET; i++) { printf("Undeclared resource on session 2: %u\n", z_loan(rids2[i])._id); z_undeclare_keyexpr(z_loan(s2), z_move(rids2[i])); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Stop both sessions printf("Stopping threads on session 1\n"); @@ -363,12 +363,12 @@ int main(int argc, char **argv) { printf("Closing session 1\n"); z_close(z_move(s1)); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); printf("Closing session 2\n"); z_close(z_move(s2)); - zp_free((uint8_t *)payload); + z_free((uint8_t *)payload); payload = NULL; free(s1_res); diff --git a/tests/z_data_struct_test.c b/tests/z_data_struct_test.c index b877d08ae..461ee92ca 100644 --- a/tests/z_data_struct_test.c +++ b/tests/z_data_struct_test.c @@ -27,7 +27,7 @@ void entry_list_test(void) { _z_transport_peer_entry_list_t *root = _z_transport_peer_entry_list_new(); for (int i = 0; i < 10; i++) { - _z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)zp_malloc(sizeof(_z_transport_peer_entry_t)); + _z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); root = _z_transport_peer_entry_list_insert(root, entry); } _z_transport_peer_entry_list_t *list = root; @@ -37,7 +37,7 @@ void entry_list_test(void) { _z_transport_peer_entry_list_head(root)->_peer_id = _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE - 1; for (int i = 0; i < 11; i++) { - _z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)zp_malloc(sizeof(_z_transport_peer_entry_t)); + _z_transport_peer_entry_t *entry = (_z_transport_peer_entry_t *)z_malloc(sizeof(_z_transport_peer_entry_t)); root = _z_transport_peer_entry_list_insert(root, entry); } assert(_z_transport_peer_entry_list_head(root)->_peer_id == _Z_KEYEXPR_MAPPING_UNKNOWN_REMOTE - 1); diff --git a/tests/z_iobuf_test.c b/tests/z_iobuf_test.c index 88fe777b7..3a851efc0 100644 --- a/tests/z_iobuf_test.c +++ b/tests/z_iobuf_test.c @@ -46,13 +46,13 @@ void print_iosli(_z_iosli_t *ios) { /*=============================*/ /* Generating functions */ /*=============================*/ -int gen_bool(void) { return zp_random_u8() % 2; } +int gen_bool(void) { return z_random_u8() % 2; } -uint8_t gen_uint8(void) { return zp_random_u8() % 255; } +uint8_t gen_uint8(void) { return z_random_u8() % 255; } size_t gen_size_t(void) { size_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } @@ -154,7 +154,7 @@ void iosli_writable_readable(void) { assert(_z_iosli_readable(cios) == 0); printf(" - IOSli bytes\n"); - uint8_t *payload = (uint8_t *)zp_malloc(len); + uint8_t *payload = (uint8_t *)z_malloc(len); memset((uint8_t *)payload, 1, len); for (size_t i = 0; i < len; i++) { @@ -172,7 +172,7 @@ void iosli_writable_readable(void) { assert(readable == i + 1); } - uint8_t *buffer = (uint8_t *)zp_malloc(len); + uint8_t *buffer = (uint8_t *)z_malloc(len); memset((uint8_t *)buffer, 1, len); _z_iosli_write_bytes(&ios, payload, 0, len); @@ -203,12 +203,12 @@ void iosli_writable_readable(void) { assert(_z_iosli_readable(&wios) == 0); _z_iosli_clear(pios); - zp_free(pios); + z_free(pios); _z_iosli_clear(cios); - zp_free(cios); + z_free(cios); - zp_free(buffer); - zp_free(payload); + z_free(buffer); + z_free(payload); } void zbuf_writable_readable(void) { @@ -382,7 +382,7 @@ void wbuf_write_zbuf_read_bytes(void) { print_wbuf_overview(&wbf); printf(" Writing %zu bytes\n", len); - uint8_t *buf01 = (uint8_t *)zp_malloc(len); + uint8_t *buf01 = (uint8_t *)z_malloc(len); for (size_t i = 0; i < len; i++) { buf01[i] = (uint8_t)i % 255; } @@ -402,7 +402,7 @@ void wbuf_write_zbuf_read_bytes(void) { } printf(" ]\n"); - zp_free(buf01); + z_free(buf01); _z_zbuf_clear(&zbf); _z_wbuf_clear(&wbf); } @@ -442,7 +442,7 @@ void wbuf_put_zbuf_get(void) { void wbuf_reusable_write_zbuf_read(void) { _z_wbuf_t wbf = gen_wbuf(128); for (int i = 1; i <= 10; i++) { - size_t len = zp_random_u8() % 128; + size_t len = z_random_u8() % 128; printf("\n>>> WBuf => Write and Read round %d\n", i); print_wbuf_overview(&wbf); printf(" Writing %zu bytes\n", len); diff --git a/tests/z_msgcodec_test.c b/tests/z_msgcodec_test.c index 23e1e5f8d..4105c42dc 100644 --- a/tests/z_msgcodec_test.c +++ b/tests/z_msgcodec_test.c @@ -133,27 +133,27 @@ void print_scouting_message_type(uint8_t header) { /*=============================*/ /* Generating functions */ /*=============================*/ -_Bool gen_bool(void) { return zp_random_u8() % 2; } +_Bool gen_bool(void) { return z_random_u8() % 2; } -uint8_t gen_uint8(void) { return zp_random_u8(); } +uint8_t gen_uint8(void) { return z_random_u8(); } -uint16_t gen_uint16(void) { return zp_random_u16(); } +uint16_t gen_uint16(void) { return z_random_u16(); } uint64_t gen_uint64(void) { uint64_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } unsigned int gen_uint(void) { unsigned int ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } _z_zint_t gen_zint(void) { _z_zint_t ret = 0; - zp_random_fill(&ret, sizeof(ret)); + z_random_fill(&ret, sizeof(ret)); return ret; } @@ -172,8 +172,8 @@ _z_bytes_t gen_payload(size_t len) { _z_bytes_t pld; pld._is_alloc = true; pld.len = len; - pld.start = (uint8_t *)zp_malloc(len); - zp_random_fill((uint8_t *)pld.start, pld.len); + pld.start = (uint8_t *)z_malloc(len); + z_random_fill((uint8_t *)pld.start, pld.len); return pld; } @@ -185,7 +185,7 @@ _z_bytes_t gen_bytes(size_t len) { arr.start = NULL; if (len == 0) return arr; - arr.start = (uint8_t *)zp_malloc(sizeof(uint8_t) * len); + arr.start = (uint8_t *)z_malloc(sizeof(uint8_t) * len); for (_z_zint_t i = 0; i < len; i++) { ((uint8_t *)arr.start)[i] = gen_uint8() & 0x7f; // 0b01111111 } @@ -208,9 +208,9 @@ _z_id_t gen_zid(void) { char *gen_str(size_t size) { char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - char *str = (char *)zp_malloc(size + 1); + char *str = (char *)z_malloc(size + 1); for (_z_zint_t i = 0; i < size; i++) { - uint32_t key = zp_random_u32() % (sizeof(charset) - 1); + uint32_t key = z_random_u32() % (sizeof(charset) - 1); str[i] = charset[key]; } str[size] = '\0'; @@ -328,8 +328,8 @@ void assert_eq_locator_array(const _z_locator_array_t *left, const _z_locator_ar printf("%s:%s", ls, rs); if (i < left->_len - 1) printf(" "); - zp_free(ls); - zp_free(rs); + z_free(ls); + z_free(rs); assert(_z_locator_eq(l, r) == true); } @@ -1574,7 +1574,7 @@ void assert_eq_net_msg(const _z_network_message_t *left, const _z_network_messag _z_network_message_vec_t gen_net_msgs(size_t n) { _z_network_message_vec_t ret = _z_network_message_vec_make(n); for (size_t i = 0; i < n; i++) { - _z_network_message_t *msg = (_z_network_message_t *)zp_malloc(sizeof(_z_network_message_t)); + _z_network_message_t *msg = (_z_network_message_t *)z_malloc(sizeof(_z_network_message_t)); memset(msg, 0, sizeof(_z_network_message_t)); *msg = gen_net_msg(); _z_network_message_vec_append(&ret, msg); diff --git a/tests/z_peer_multicast_test.c b/tests/z_peer_multicast_test.c index f0448ad41..cd8b28f05 100644 --- a/tests/z_peer_multicast_test.c +++ b/tests/z_peer_multicast_test.c @@ -83,7 +83,7 @@ int main(int argc, char **argv) { zp_start_read_task(z_loan(s1), NULL); zp_start_lease_task(z_loan(s1), NULL); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); config = z_config_default(); zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make("peer")); @@ -100,7 +100,7 @@ int main(int argc, char **argv) { zp_start_read_task(z_loan(s2), NULL); zp_start_lease_task(z_loan(s2), NULL); - zp_sleep_s(SLEEP * 5); + z_sleep_s(SLEEP * 5); // Declare subscribers on second session char *s1_res = (char *)malloc(64); @@ -108,7 +108,7 @@ int main(int argc, char **argv) { memset(s1_res, 0, 64); snprintf(s1_res, 64, "%s%u", uri, i); z_owned_closure_sample_t callback = z_closure(data_handler, NULL, &idx[i]); - z_owned_subscriber_t *sub = (z_owned_subscriber_t *)zp_malloc(sizeof(z_owned_subscriber_t)); + z_owned_subscriber_t *sub = (z_owned_subscriber_t *)z_malloc(sizeof(z_owned_subscriber_t)); *sub = z_declare_subscriber(z_loan(s2), z_keyexpr(s1_res), &callback, NULL); assert(z_check(*sub)); printf("Declared subscription on session 2: %ju %zu %s\n", (uintmax_t)z_subscriber_loan(sub)._val->_entity_id, @@ -118,7 +118,7 @@ int main(int argc, char **argv) { // Write data from first session size_t len = MSG_LEN; - uint8_t *payload = (uint8_t *)zp_malloc(len); + uint8_t *payload = (uint8_t *)z_malloc(len); memset(payload, 1, MSG_LEN); total = MSG * SET; @@ -134,13 +134,13 @@ int main(int argc, char **argv) { } // Wait to receive all the data - zp_time_t now = zp_time_now(); + z_time_t now = z_time_now(); unsigned int expected = is_reliable ? total : 1; while (datas < expected) { - assert(zp_time_elapsed_s(&now) < TIMEOUT); + assert(z_time_elapsed_s(&now) < TIMEOUT); (void)(now); printf("Waiting for datas... %u/%u\n", datas, expected); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); } if (is_reliable == true) assert(datas == expected); @@ -148,7 +148,7 @@ int main(int argc, char **argv) { assert(datas >= expected); datas = 0; - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Undeclare subscribers and queryables on second session while (subs2) { @@ -158,7 +158,7 @@ int main(int argc, char **argv) { subs2 = _z_list_pop(subs2, _z_noop_elem_free, NULL); } - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); // Stop both sessions printf("Stopping threads on session 1\n"); @@ -173,12 +173,12 @@ int main(int argc, char **argv) { printf("Closing session 1\n"); z_close(z_move(s1)); - zp_sleep_s(SLEEP); + z_sleep_s(SLEEP); printf("Closing session 2\n"); z_close(z_move(s2)); - zp_free((uint8_t *)payload); + z_free((uint8_t *)payload); payload = NULL; free(s1_res); diff --git a/tests/z_perf_rx.c b/tests/z_perf_rx.c index 2eadf731c..58d5959c1 100644 --- a/tests/z_perf_rx.c +++ b/tests/z_perf_rx.c @@ -21,7 +21,7 @@ typedef struct { volatile unsigned long count; unsigned long curr_len; - zp_clock_t start; + z_clock_t start; } z_stats_t; static z_stats_t test_stats; @@ -34,7 +34,7 @@ void z_stats_stop(z_stats_t *stats) { return; } // Print values - unsigned long elapsed_ms = zp_clock_elapsed_ms(&stats->start); + unsigned long elapsed_ms = z_clock_elapsed_ms(&stats->start); printf("End test for pkt len: %lu, msg nb: %lu, time ms: %lu\n", stats->curr_len, stats->count, elapsed_ms); stats->count = 0; } @@ -53,7 +53,7 @@ void on_sample(const z_sample_t *sample, void *context) { } // Start new measurement printf("Starting test for pkt len: %lu\n", stats->curr_len); - stats->start = zp_clock_now(); + stats->start = z_clock_now(); } stats->count++; } @@ -109,7 +109,7 @@ int main(int argc, char **argv) { } // Wait for everything to settle printf("End of test\n"); - zp_sleep_s(1); + z_sleep_s(1); // Clean up z_undeclare_subscriber(z_move(sub)); zp_stop_read_task(z_loan(s)); diff --git a/tests/z_perf_tx.c b/tests/z_perf_tx.c index 168294d28..1852ecf37 100644 --- a/tests/z_perf_tx.c +++ b/tests/z_perf_tx.c @@ -23,11 +23,11 @@ #if Z_FEATURE_PUBLICATION == 1 int send_packets(unsigned long pkt_len, z_owned_publisher_t *pub, uint8_t *value) { - zp_clock_t test_start = zp_clock_now(); + z_clock_t test_start = z_clock_now(); unsigned long elapsed_us = 0; while (elapsed_us < TEST_DURATION_US) { z_publisher_put(z_loan(*pub), (const uint8_t *)value, pkt_len, NULL); - elapsed_us = zp_clock_elapsed_us(&test_start); + elapsed_us = z_clock_elapsed_us(&test_start); } return 0; } @@ -83,7 +83,7 @@ int main(int argc, char **argv) { // Wait for joins if (strcmp(mode, "peer") == 0) { printf("Waiting for JOIN messages\n"); - zp_sleep_s(3); + z_sleep_s(3); } // Send packets for (size_t i = 0; i < ARRAY_SIZE(len_array); i++) { diff --git a/tests/z_session_test.c b/tests/z_session_test.c index 33b1977bc..40058e102 100644 --- a/tests/z_session_test.c +++ b/tests/z_session_test.c @@ -40,7 +40,7 @@ int main(void) { } // Commented out wait for 1 second. Stopping should work without it. - // zp_sleep_ms(1000); + // z_sleep_ms(1000); // Stop read and lease tasks for zenoh-pico zp_stop_read_task(z_loan(s)); diff --git a/tests/z_test_fragment_rx.c b/tests/z_test_fragment_rx.c index 133497694..ae562a518 100644 --- a/tests/z_test_fragment_rx.c +++ b/tests/z_test_fragment_rx.c @@ -35,21 +35,30 @@ void data_handler(const z_sample_t *sample, void *ctx) { int main(int argc, char **argv) { const char *keyexpr = "test/zenoh-pico-fragment"; - const char *mode = "client"; + const char *mode = NULL; char *llocator = NULL; + char *clocator = NULL; (void)argv; // Check if peer mode if (argc > 1) { mode = "peer"; llocator = "udp/224.0.0.224:7447#iface=lo"; + } else { + mode = "client"; + clocator = "tcp/127.0.0.1:7447"; } // Set config z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (mode != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + } if (llocator != NULL) { zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } + if (clocator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + } // Open session z_owned_session_t s = z_open(z_move(config)); if (!z_check(s)) { diff --git a/tests/z_test_fragment_tx.c b/tests/z_test_fragment_tx.c index b002a34d8..f964797f9 100644 --- a/tests/z_test_fragment_tx.c +++ b/tests/z_test_fragment_tx.c @@ -15,22 +15,20 @@ #include #include #include -#include +#include + +#include "zenoh-pico.h" #if Z_FEATURE_PUBLICATION == 1 int main(int argc, char **argv) { const char *keyexpr = "test/zenoh-pico-fragment"; - const char *mode = "client"; + const char *mode = NULL; char *llocator = NULL; + char *clocator = NULL; uint8_t *value = NULL; size_t size = 10000; (void)argv; - // Check if peer mode - if (argc > 1) { - mode = "peer"; - llocator = "udp/224.0.0.224:7447#iface=lo"; - } // Init value value = malloc(size); if (value == NULL) { @@ -39,12 +37,25 @@ int main(int argc, char **argv) { for (size_t i = 0; i < size; i++) { value[i] = (uint8_t)i; } + // Check if peer or client mode + if (argc > 1) { + mode = "peer"; + llocator = "udp/224.0.0.224:7447#iface=lo"; + } else { + mode = "client"; + clocator = "tcp/127.0.0.1:7447"; + } // Set config z_owned_config_t config = z_config_default(); - zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + if (mode != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_MODE_KEY, z_string_make(mode)); + } if (llocator != NULL) { zp_config_insert(z_loan(config), Z_CONFIG_LISTEN_KEY, z_string_make(llocator)); } + if (clocator != NULL) { + zp_config_insert(z_loan(config), Z_CONFIG_CONNECT_KEY, z_string_make(clocator)); + } // Open session z_owned_session_t s = z_open(z_move(config)); if (!z_check(s)) { @@ -57,14 +68,17 @@ int main(int argc, char **argv) { z_close(z_session_move(&s)); return -1; } + // Wait for joins + if (strcmp(mode, "peer") == 0) { + z_sleep_s(3); + } // Put data - z_put_options_t options = z_put_options_default(); - options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL); for (int i = 0; i < 5; i++) { printf("[tx]: Sending packet on %s, len: %d\n", keyexpr, (int)size); - if (z_put(z_loan(s), z_keyexpr(keyexpr), (const uint8_t *)value, size, &options) < 0) { + if (z_put(z_loan(s), z_keyexpr(keyexpr), (const uint8_t *)value, size, NULL) < 0) { printf("Oh no! Put has failed...\n"); } + z_sleep_s(1); } // Clean up zp_stop_read_task(z_loan(s));