Skip to content

Commit 64cd525

Browse files
authored
Update and improve serial protocol (#823)
* Update serial protocol * Remove buffers reusage to avoid conflicts
1 parent 916ec33 commit 64cd525

File tree

29 files changed

+582
-570
lines changed

29 files changed

+582
-570
lines changed

.github/workflows/arduino_esp32.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
5252
mkdir -p $ARDUINO_BASE
5353
cd $ARDUINO_BASE
54-
platformio init -b esp32thing_plus -O "board_build.cmake_extra_args=-DZ_FEATURE_LINK_BLUETOOTH=1" -O "build_flags=-DZENOH_DEBUG=3 -DZENOH_COMPILER_GCC" -O "lib_ldf_mode=deep+"
54+
platformio init -b esp32thing_plus -O "board_build.cmake_extra_args=-DZ_FEATURE_LINK_BLUETOOTH=1 -DZ_FEATURE_LINK_SERIAL=1" -O "build_flags=-DZENOH_DEBUG=3 -DZENOH_COMPILER_GCC" -O "lib_ldf_mode=deep+"
5555
5656
cd $ARDUINO_BASE/lib
5757
ln -s $ZENOH_PICO_BASE

.github/workflows/espidf.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
5252
mkdir -p $ESPIDF_BASE
5353
cd $ESPIDF_BASE
54-
platformio init -b az-delivery-devkit-v4 --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3"
54+
platformio init -b az-delivery-devkit-v4 -O "board_build.cmake_extra_args=-DZ_FEATURE_LINK_SERIAL=1" --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3"
5555
5656
cd $ESPIDF_BASE/lib
5757
ln -s $ZENOH_PICO_BASE

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ file(GLOB_RECURSE Sources
343343
"src/session/*.c"
344344
"src/transport/*.c"
345345
"src/utils/*.c"
346-
"src/system/platform_common.c"
346+
"src/system/common/*.c"
347347
)
348348

349349
if(WITH_ZEPHYR)

examples/rpi_pico/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ void print_ip_address() {
4444

4545
void main_task(void *params) {
4646
(void)params;
47+
#ifndef NDEBUG
4748
vTaskDelay(pdMS_TO_TICKS(3000));
49+
#endif
50+
4851
#if WIFI_SUPPORT_ENABLED
4952
if (cyw43_arch_init()) {
5053
printf("Failed to initialise\n");

include/zenoh-pico/collections/arc_slice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "refcount.h"
2424
#include "slice.h"
25-
#include "zenoh-pico/system/platform_common.h"
25+
#include "zenoh-pico/system/common/platform.h"
2626

2727
#ifdef __cplusplus
2828
extern "C" {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Copyright (c) 2024 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13+
//
14+
15+
#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_SERIAL_H
16+
#define INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_SERIAL_H
17+
18+
#include <stdint.h>
19+
20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
24+
size_t _z_serial_msg_serialize(uint8_t *dest, size_t dest_len, const uint8_t *src, size_t src_len, uint8_t header,
25+
uint8_t *tmp_buf, size_t tmp_buf_len);
26+
size_t _z_serial_msg_deserialize(const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_len, uint8_t *header,
27+
uint8_t *tmp_buf, size_t tmp_buf_len);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_CODEC_SERIAL_H */
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// Copyright (c) 2022 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13+
//
14+
15+
#ifndef INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_SERIAL_H
16+
#define INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_SERIAL_H
17+
18+
#include <stdint.h>
19+
20+
#include "zenoh-pico/link/endpoint.h"
21+
#include "zenoh-pico/protocol/definitions/network.h"
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/// ZSerial Frame Format
28+
///
29+
/// Using COBS
30+
///
31+
/// +-+-+----+------------+--------+-+
32+
/// |O|H|XXXX|ZZZZ....ZZZZ|CCCCCCCC|0|
33+
/// +-+----+------------+--------+-+
34+
/// |O| |Len | Data | CRC32 |C|
35+
/// +-+-+-2--+----N-------+---4----+-+
36+
///
37+
/// Header: 1byte
38+
/// +---------------+
39+
/// |7|6|5|4|3|2|1|0|
40+
/// +---------------+
41+
/// |x|x|x|x|x|R|A|I|
42+
/// +---------------+
43+
///
44+
/// Flags:
45+
/// I - Init
46+
/// A - Ack
47+
/// R - Reset
48+
///
49+
/// Max Frame Size: 1510
50+
/// Max MTU: 1500
51+
/// Max On-the-wire length: 1516 (MFS + Overhead Byte (OHB) + Kind Byte + End of packet (EOP))
52+
53+
#define _Z_FLAG_SERIAL_INIT 0x01
54+
#define _Z_FLAG_SERIAL_ACK 0x02
55+
#define _Z_FLAG_SERIAL_RESET 0x04
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
60+
61+
#endif /* INCLUDE_ZENOH_PICO_PROTOCOL_DEFINITIONS_SERIAL_H*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright (c) 2024 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13+
//
14+
15+
#ifndef ZENOH_PICO_SYSTEM_COMMON_SERIAL_H
16+
#define ZENOH_PICO_SYSTEM_COMMON_SERIAL_H
17+
18+
#include <stdint.h>
19+
20+
#include "zenoh-pico/system/common/platform.h"
21+
#include "zenoh-pico/utils/result.h"
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
z_result_t _z_connect_serial(const _z_sys_net_socket_t sock);
28+
size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len);
29+
size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len);
30+
size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif /* ZENOH_PICO_SYSTEM_COMMON_SERIAL_H */

include/zenoh-pico/system/link/serial.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extern "C" {
2828
#if Z_FEATURE_LINK_SERIAL == 1
2929

3030
#define _Z_SERIAL_MTU_SIZE 1500
31-
#define _Z_SERIAL_MFS_SIZE _Z_SERIAL_MTU_SIZE + 2 + 4 // MTU + Serial Len + Serial CRC32
31+
#define _Z_SERIAL_MFS_SIZE _Z_SERIAL_MTU_SIZE + 1 + 2 + 4 // MTU + Header + Serial Len + Serial CRC32
3232
#define _Z_SERIAL_MAX_COBS_BUF_SIZE \
3333
1516 // Max On-the-wire length for an MFS/MTU of 1510/1500 (MFS + Overhead Byte (OHB) + End of packet (EOP))
3434

@@ -42,8 +42,8 @@ z_result_t _z_listen_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin,
4242
z_result_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t baudrate);
4343
void _z_close_serial(_z_sys_net_socket_t *sock);
4444
size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len);
45-
size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len);
46-
size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len);
45+
size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len);
46+
size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len);
4747

4848
#endif
4949

include/zenoh-pico/system/platform.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
#include <stdint.h>
1919

2020
#include "zenoh-pico/config.h"
21-
#include "zenoh-pico/system/platform_common.h"
21+
#include "zenoh-pico/system/common/platform.h"
2222

2323
#endif /* ZENOH_PICO_SYSTEM_PLATFORM_H */

include/zenoh-pico/system/platform/espidf.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,7 @@ typedef struct {
5656
int _fd;
5757
#endif
5858
#if Z_FEATURE_LINK_SERIAL == 1
59-
struct {
60-
uart_port_t _serial;
61-
uint8_t *before_cobs;
62-
uint8_t *after_cobs;
63-
};
59+
uart_port_t _serial;
6460
#endif
6561
};
6662
} _z_sys_net_socket_t;

include/zenoh-pico/utils/logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <stdio.h>
1919

20-
#include "zenoh-pico/system/platform_common.h"
20+
#include "zenoh-pico/system/common/platform.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {

src/api/api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
#include "zenoh-pico/session/resource.h"
3838
#include "zenoh-pico/session/subscription.h"
3939
#include "zenoh-pico/session/utils.h"
40+
#include "zenoh-pico/system/common/platform.h"
4041
#include "zenoh-pico/system/platform.h"
41-
#include "zenoh-pico/system/platform_common.h"
4242
#include "zenoh-pico/transport/common/tx.h"
4343
#include "zenoh-pico/transport/multicast.h"
4444
#include "zenoh-pico/transport/unicast.h"

src/link/unicast/serial.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "zenoh-pico/config.h"
2222
#include "zenoh-pico/link/manager.h"
23+
#include "zenoh-pico/system/common/serial.h"
2324
#include "zenoh-pico/system/link/serial.h"
2425
#include "zenoh-pico/utils/pointers.h"
2526

src/protocol/codec/serial.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// Copyright (c) 2024 ZettaScale Technology
3+
//
4+
// This program and the accompanying materials are made available under the
5+
// terms of the Eclipse Public License 2.0 which is available at
6+
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7+
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8+
//
9+
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10+
//
11+
// Contributors:
12+
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13+
//
14+
15+
#include <inttypes.h>
16+
#include <stdint.h>
17+
#include <string.h>
18+
19+
#include "zenoh-pico/utils/checksum.h"
20+
#include "zenoh-pico/utils/encoding.h"
21+
#include "zenoh-pico/utils/logging.h"
22+
#include "zenoh-pico/utils/pointers.h"
23+
24+
#define KIND_FIELD_LEN 1u
25+
#define LEN_FIELD_LEN 2u
26+
#define CRC32_LEN 4u
27+
28+
size_t _z_serial_msg_serialize(uint8_t *dest, size_t dest_len, const uint8_t *src, size_t src_len, uint8_t header,
29+
uint8_t *tmp_buf, size_t tmp_buf_len) {
30+
size_t expected_size = src_len + KIND_FIELD_LEN + LEN_FIELD_LEN + CRC32_LEN;
31+
if (tmp_buf_len < expected_size) {
32+
_Z_DEBUG("tmp buffer too small: %zu < %zu", tmp_buf_len, expected_size);
33+
return SIZE_MAX;
34+
}
35+
36+
uint32_t crc32 = _z_crc32(src, src_len);
37+
uint8_t crc_bytes[CRC32_LEN] = {(uint8_t)(crc32 & 0xFF), (uint8_t)((crc32 >> 8) & 0xFF),
38+
(uint8_t)((crc32 >> 16) & 0xFF), (uint8_t)((crc32 >> 24) & 0xFF)};
39+
40+
uint16_t wire_size = (uint16_t)src_len;
41+
uint8_t size_bytes[LEN_FIELD_LEN] = {(uint8_t)(wire_size & 0xFF), (uint8_t)((wire_size >> 8) & 0xFF)};
42+
43+
uint8_t *tmp_buf_ptr = tmp_buf;
44+
45+
tmp_buf_ptr[0] = header;
46+
tmp_buf_ptr += sizeof(header);
47+
48+
memcpy(tmp_buf_ptr, size_bytes, sizeof(size_bytes));
49+
tmp_buf_ptr += sizeof(size_bytes);
50+
51+
memcpy(tmp_buf_ptr, src, src_len);
52+
tmp_buf_ptr += src_len;
53+
54+
memcpy(tmp_buf_ptr, crc_bytes, sizeof(crc_bytes));
55+
tmp_buf_ptr += sizeof(crc_bytes);
56+
57+
size_t total_len = _z_ptr_u8_diff(tmp_buf_ptr, tmp_buf);
58+
59+
size_t ret = _z_cobs_encode(tmp_buf, total_len, dest);
60+
if (ret + 1 > dest_len) {
61+
_Z_DEBUG("destination buffer too small");
62+
return SIZE_MAX;
63+
}
64+
65+
dest[ret] = 0x00;
66+
67+
return ret + 1u;
68+
}
69+
70+
size_t _z_serial_msg_deserialize(const uint8_t *src, size_t src_len, uint8_t *dst, size_t dst_len, uint8_t *header,
71+
uint8_t *tmp_buf, size_t tmp_buf_len) {
72+
if (tmp_buf_len < src_len) {
73+
_Z_DEBUG("tmp_buf too small");
74+
return SIZE_MAX;
75+
}
76+
77+
size_t decoded_size = _z_cobs_decode(src, src_len, tmp_buf);
78+
79+
if (decoded_size < KIND_FIELD_LEN + LEN_FIELD_LEN + CRC32_LEN) {
80+
_Z_DEBUG("decoded frame too small");
81+
return SIZE_MAX;
82+
}
83+
84+
uint8_t *tmp_buf_ptr = tmp_buf;
85+
86+
*header = tmp_buf_ptr[0];
87+
tmp_buf_ptr += sizeof(uint8_t);
88+
89+
uint16_t wire_size = tmp_buf_ptr[0] | (tmp_buf_ptr[1] << 8);
90+
tmp_buf_ptr += sizeof(uint16_t);
91+
92+
size_t expected_size = wire_size + KIND_FIELD_LEN + LEN_FIELD_LEN + CRC32_LEN;
93+
if (expected_size != decoded_size) {
94+
_Z_DEBUG("wire size mismatch: %zu != %zu", expected_size, decoded_size);
95+
return SIZE_MAX;
96+
}
97+
98+
if (dst_len < wire_size) {
99+
_Z_DEBUG("destination buffer too small: %zu < %u", dst_len, wire_size);
100+
return SIZE_MAX;
101+
}
102+
103+
if (wire_size != 0) {
104+
memcpy(dst, tmp_buf_ptr, wire_size);
105+
tmp_buf_ptr += wire_size;
106+
}
107+
108+
uint32_t received_crc = tmp_buf_ptr[0] | (tmp_buf_ptr[1] << 8) | (tmp_buf_ptr[2] << 16) | (tmp_buf_ptr[3] << 24);
109+
110+
uint32_t computed_crc = _z_crc32(dst, wire_size);
111+
if (received_crc != computed_crc) {
112+
_Z_DEBUG("CRC mismatch. Received: 0x%08" PRIu32 ", Computed: 0x%08" PRIu32, received_crc, computed_crc);
113+
return SIZE_MAX;
114+
}
115+
116+
return wire_size;
117+
}

0 commit comments

Comments
 (0)