Skip to content

Commit b5483d7

Browse files
authored
Refactor link capabilities (#280)
1 parent e420029 commit b5483d7

File tree

19 files changed

+329
-201
lines changed

19 files changed

+329
-201
lines changed

include/zenoh-pico/link/link.h

+34-14
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,44 @@
4343
#include "zenoh-pico/utils/result.h"
4444

4545
/**
46-
* Link capabilities values, defined as a bitmask.
46+
* Link transport capability enum.
4747
*
4848
* Enumerators:
49-
* Z_LINK_CAPABILITY_NONE: Bitmask to define that link has no capabilities.
50-
* Z_LINK_CAPABILITY_RELIABLE: Bitmask to define and check if link is reliable.
51-
* Z_LINK_CAPABILITY_STREAMED: Bitmask to define and check if link is streamed.
52-
* Z_LINK_CAPABILITY_MULTICAST: Bitmask to define and check if link is multicast.
49+
* Z_LINK_CAP_TRANSPORT_UNICAST: Link has unicast capabilities.
50+
* Z_LINK_CAP_TRANSPORT_MULTICAST: Link has multicast capabilities.
5351
*/
5452
typedef enum {
55-
Z_LINK_CAPABILITY_NONE = 0x00, // 0
56-
Z_LINK_CAPABILITY_RELIABLE = 0x01, // 1 << 0
57-
Z_LINK_CAPABILITY_STREAMED = 0x02, // 1 << 1
58-
Z_LINK_CAPABILITY_MULTICAST = 0x04 // 1 << 2
59-
} _z_link_capabilities_t;
53+
Z_LINK_CAP_TRANSPORT_UNICAST = 0,
54+
Z_LINK_CAP_TRANSPORT_MULTICAST = 1,
55+
} _z_link_cap_transport_t;
6056

61-
#define _Z_LINK_IS_RELIABLE(X) ((X & Z_LINK_CAPABILITY_RELIABLE) == Z_LINK_CAPABILITY_RELIABLE)
62-
#define _Z_LINK_IS_STREAMED(X) ((X & Z_LINK_CAPABILITY_STREAMED) == Z_LINK_CAPABILITY_STREAMED)
63-
#define _Z_LINK_IS_MULTICAST(X) ((X & Z_LINK_CAPABILITY_MULTICAST) == Z_LINK_CAPABILITY_MULTICAST)
57+
/**
58+
* Link flow capability enum.
59+
*
60+
* Enumerators:
61+
* Z_LINK_CAP_FLOW_STREAM: Link use datagrams.
62+
* Z_LINK_CAP_FLOW_DATAGRAM: Link use byte stream.
63+
*/
64+
typedef enum {
65+
Z_LINK_CAP_FLOW_DATAGRAM = 0,
66+
Z_LINK_CAP_FLOW_STREAM = 1,
67+
} _z_link_cap_flow_t;
68+
69+
/**
70+
* Link capabilities, stored as a register-like object.
71+
*
72+
* Fields:
73+
* transport: 2 bits, see _z_link_cap_transport_t enum.
74+
* flow: 1 bit, see _z_link_cap_flow_t enum.
75+
* reliable: 1 bit, 1 if the link is reliable (network definition)
76+
* reserved: 4 bits, reserved for futur use
77+
*/
78+
typedef struct _z_link_capabilities_t {
79+
uint8_t _transport : 2;
80+
uint8_t _flow : 1;
81+
uint8_t _is_reliable : 1;
82+
uint8_t _reserved : 4;
83+
} _z_link_capabilities_t;
6484

6585
struct _z_link_t; // Forward declaration to be used in _z_f_link_*
6686

@@ -104,7 +124,7 @@ typedef struct _z_link_t {
104124
_z_f_link_free _free_f;
105125

106126
uint16_t _mtu;
107-
uint8_t _capabilities;
127+
_z_link_capabilities_t _cap;
108128
} _z_link_t;
109129

110130
void _z_link_clear(_z_link_t *zl);

include/zenoh-pico/transport/common/tx.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "zenoh-pico/net/session.h"
2020
#include "zenoh-pico/transport/transport.h"
2121

22-
void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, _Bool is_streamed);
23-
void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, _Bool is_streamed);
22+
void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, uint8_t link_flow_capability);
23+
void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, uint8_t link_flow_capability);
2424
/*This function is unsafe because it operates in potentially concurrent
2525
data.*Make sure that the following mutexes are locked before calling this function : *-ztu->mutex_tx */
2626
int8_t __unsafe_z_serialize_zenoh_fragment(_z_wbuf_t *dst, _z_wbuf_t *src, z_reliability_t reliability, size_t sn);

src/link/link.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,17 @@ size_t _z_link_recv_exact_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, size_t len
148148

149149
int8_t _z_link_send_wbuf(const _z_link_t *link, const _z_wbuf_t *wbf) {
150150
int8_t ret = _Z_RES_OK;
151-
_Bool link_is_streamed = _Z_LINK_IS_STREAMED(link->_capabilities);
151+
_Bool link_is_streamed = false;
152+
153+
switch (link->_cap._flow) {
154+
case Z_LINK_CAP_FLOW_STREAM:
155+
link_is_streamed = true;
156+
break;
157+
case Z_LINK_CAP_FLOW_DATAGRAM:
158+
default:
159+
link_is_streamed = false;
160+
break;
161+
}
152162
for (size_t i = 0; (i < _z_wbuf_len_iosli(wbf)) && (ret == _Z_RES_OK); i++) {
153163
_z_bytes_t bs = _z_iosli_to_bytes(_z_wbuf_get_iosli(wbf, i));
154164
size_t n = bs.len;

src/link/multicast/bt.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ uint16_t _z_get_link_mtu_bt(void) { return SPP_MAXIMUM_PAYLOAD; }
115115
int8_t _z_new_link_bt(_z_link_t *zl, _z_endpoint_t endpoint) {
116116
int8_t ret = _Z_RES_OK;
117117

118-
zl->_capabilities = Z_LINK_CAPABILITY_STREAMED | Z_LINK_CAPABILITY_MULTICAST;
118+
zl->_cap._transport = Z_LINK_CAP_TRANSPORT_MULTICAST;
119+
zl->_cap._flow = Z_LINK_CAP_FLOW_STREAM;
120+
zl->_cap._is_reliable = false;
121+
119122
zl->_mtu = _z_get_link_mtu_bt();
120123

121124
zl->_endpoint = endpoint;

src/link/multicast/udp.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ uint16_t _z_get_link_mtu_udp_multicast(void) {
171171
int8_t _z_new_link_udp_multicast(_z_link_t *zl, _z_endpoint_t endpoint) {
172172
int8_t ret = _Z_RES_OK;
173173

174-
zl->_capabilities = Z_LINK_CAPABILITY_MULTICAST;
174+
zl->_cap._transport = Z_LINK_CAP_TRANSPORT_MULTICAST;
175+
zl->_cap._flow = Z_LINK_CAP_FLOW_DATAGRAM;
176+
zl->_cap._is_reliable = false;
177+
175178
zl->_mtu = _z_get_link_mtu_udp_multicast();
176179

177180
zl->_endpoint = endpoint;

src/link/unicast/serial.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ uint16_t _z_get_link_mtu_serial(void) { return _Z_SERIAL_MTU_SIZE; }
116116
int8_t _z_new_link_serial(_z_link_t *zl, _z_endpoint_t endpoint) {
117117
int8_t ret = _Z_RES_OK;
118118

119-
zl->_capabilities = Z_LINK_CAPABILITY_NONE;
119+
zl->_cap._transport = Z_LINK_CAP_TRANSPORT_UNICAST;
120+
zl->_cap._flow = Z_LINK_CAP_FLOW_DATAGRAM;
121+
zl->_cap._is_reliable = false;
122+
120123
zl->_mtu = _z_get_link_mtu_serial();
121124

122125
zl->_endpoint = endpoint;

src/link/unicast/tcp.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ uint16_t _z_get_link_mtu_tcp(void) {
156156
int8_t _z_new_link_tcp(_z_link_t *zl, _z_endpoint_t *endpoint) {
157157
int8_t ret = _Z_RES_OK;
158158

159-
zl->_capabilities = Z_LINK_CAPABILITY_RELIABLE | Z_LINK_CAPABILITY_STREAMED;
159+
zl->_cap._transport = Z_LINK_CAP_TRANSPORT_UNICAST;
160+
zl->_cap._flow = Z_LINK_CAP_FLOW_STREAM;
161+
zl->_cap._is_reliable = true;
162+
160163
zl->_mtu = _z_get_link_mtu_tcp();
161164

162165
zl->_endpoint = *endpoint;

src/link/unicast/udp.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ uint16_t _z_get_link_mtu_udp_unicast(void) {
162162
int8_t _z_new_link_udp_unicast(_z_link_t *zl, _z_endpoint_t endpoint) {
163163
int8_t ret = _Z_RES_OK;
164164

165-
zl->_capabilities = Z_LINK_CAPABILITY_NONE;
165+
zl->_cap._transport = Z_LINK_CAP_TRANSPORT_UNICAST;
166+
zl->_cap._flow = Z_LINK_CAP_FLOW_DATAGRAM;
167+
zl->_cap._is_reliable = false;
168+
166169
zl->_mtu = _z_get_link_mtu_udp_unicast();
167170

168171
zl->_endpoint = endpoint;

src/link/unicast/ws.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ uint16_t _z_get_link_mtu_ws(void) {
157157
int8_t _z_new_link_ws(_z_link_t *zl, _z_endpoint_t *endpoint) {
158158
int8_t ret = _Z_RES_OK;
159159

160-
zl->_capabilities = Z_LINK_CAPABILITY_RELIABLE;
160+
zl->_cap._transport = Z_LINK_CAP_TRANSPORT_UNICAST;
161+
zl->_cap._flow = Z_LINK_CAP_FLOW_DATAGRAM;
162+
zl->_cap._is_reliable = true;
163+
161164
zl->_mtu = _z_get_link_mtu_ws();
162165

163166
zl->_endpoint = *endpoint;

src/transport/common/rx.c

+26-21
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,37 @@ int8_t _z_link_recv_t_msg(_z_transport_message_t *t_msg, const _z_link_t *zl) {
2828
_z_zbuf_t zbf = _z_zbuf_make(Z_BATCH_UNICAST_SIZE);
2929
_z_zbuf_reset(&zbf);
3030

31-
if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) {
32-
// Read the message length
33-
if (_z_link_recv_exact_zbuf(zl, &zbf, _Z_MSG_LEN_ENC_SIZE, NULL) == _Z_MSG_LEN_ENC_SIZE) {
34-
size_t len = 0;
35-
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
36-
len |= (size_t)(_z_zbuf_read(&zbf) << (i * (uint8_t)8));
37-
}
31+
switch (zl->_cap._flow) {
32+
case Z_LINK_CAP_FLOW_STREAM:
33+
// Read the message length
34+
if (_z_link_recv_exact_zbuf(zl, &zbf, _Z_MSG_LEN_ENC_SIZE, NULL) == _Z_MSG_LEN_ENC_SIZE) {
35+
size_t len = 0;
36+
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
37+
len |= (size_t)(_z_zbuf_read(&zbf) << (i * (uint8_t)8));
38+
}
3839

39-
size_t writable = _z_zbuf_capacity(&zbf) - _z_zbuf_len(&zbf);
40-
if (writable >= len) {
41-
// Read enough bytes to decode the message
42-
if (_z_link_recv_exact_zbuf(zl, &zbf, len, NULL) != len) {
43-
ret = _Z_ERR_TRANSPORT_RX_FAILED;
40+
size_t writable = _z_zbuf_capacity(&zbf) - _z_zbuf_len(&zbf);
41+
if (writable >= len) {
42+
// Read enough bytes to decode the message
43+
if (_z_link_recv_exact_zbuf(zl, &zbf, len, NULL) != len) {
44+
ret = _Z_ERR_TRANSPORT_RX_FAILED;
45+
}
46+
} else {
47+
ret = _Z_ERR_TRANSPORT_NO_SPACE;
4448
}
4549
} else {
46-
ret = _Z_ERR_TRANSPORT_NO_SPACE;
50+
ret = _Z_ERR_TRANSPORT_RX_FAILED;
4751
}
48-
} else {
49-
ret = _Z_ERR_TRANSPORT_RX_FAILED;
50-
}
51-
} else {
52-
if (_z_link_recv_zbuf(zl, &zbf, NULL) == SIZE_MAX) {
53-
ret = _Z_ERR_TRANSPORT_RX_FAILED;
54-
}
52+
break;
53+
case Z_LINK_CAP_FLOW_DATAGRAM:
54+
if (_z_link_recv_zbuf(zl, &zbf, NULL) == SIZE_MAX) {
55+
ret = _Z_ERR_TRANSPORT_RX_FAILED;
56+
}
57+
break;
58+
default:
59+
ret = _Z_ERR_GENERIC;
60+
break;
5561
}
56-
5762
if (ret == _Z_RES_OK) {
5863
_z_transport_message_t l_t_msg;
5964
ret = _z_transport_message_decode(&l_t_msg, &zbf);

src/transport/common/tx.c

+52-23
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@
2727
* Make sure that the following mutexes are locked before calling this function:
2828
* - ztu->mutex_tx
2929
*/
30-
void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, _Bool is_streamed) {
30+
void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, uint8_t link_flow_capability) {
3131
_z_wbuf_reset(buf);
3232

33-
if (is_streamed == true) {
34-
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
35-
_z_wbuf_put(buf, 0, i);
36-
}
37-
_z_wbuf_set_wpos(buf, _Z_MSG_LEN_ENC_SIZE);
33+
switch (link_flow_capability) {
34+
// Stream capable links
35+
case Z_LINK_CAP_FLOW_STREAM:
36+
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
37+
_z_wbuf_put(buf, 0, i);
38+
}
39+
_z_wbuf_set_wpos(buf, _Z_MSG_LEN_ENC_SIZE);
40+
break;
41+
// Datagram capable links
42+
case Z_LINK_CAP_FLOW_DATAGRAM:
43+
default:
44+
break;
3845
}
3946
}
4047

@@ -43,12 +50,20 @@ void __unsafe_z_prepare_wbuf(_z_wbuf_t *buf, _Bool is_streamed) {
4350
* Make sure that the following mutexes are locked before calling this function:
4451
* - ztu->mutex_tx
4552
*/
46-
void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, _Bool is_streamed) {
47-
if (is_streamed == true) {
48-
size_t len = _z_wbuf_len(buf) - _Z_MSG_LEN_ENC_SIZE;
49-
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
50-
_z_wbuf_put(buf, (uint8_t)((len >> (uint8_t)8 * i) & (uint8_t)0xFF), i);
53+
void __unsafe_z_finalize_wbuf(_z_wbuf_t *buf, uint8_t link_flow_capability) {
54+
switch (link_flow_capability) {
55+
// Stream capable links
56+
case Z_LINK_CAP_FLOW_STREAM: {
57+
size_t len = _z_wbuf_len(buf) - _Z_MSG_LEN_ENC_SIZE;
58+
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
59+
_z_wbuf_put(buf, (uint8_t)((len >> (uint8_t)8 * i) & (uint8_t)0xFF), i);
60+
}
61+
break;
5162
}
63+
// Datagram capable links
64+
case Z_LINK_CAP_FLOW_DATAGRAM:
65+
default:
66+
break;
5267
}
5368
}
5469

@@ -74,24 +89,38 @@ int8_t _z_link_send_t_msg(const _z_link_t *zl, const _z_transport_message_t *t_m
7489
// Create and prepare the buffer to serialize the message on
7590
uint16_t mtu = (zl->_mtu < Z_BATCH_UNICAST_SIZE) ? zl->_mtu : Z_BATCH_UNICAST_SIZE;
7691
_z_wbuf_t wbf = _z_wbuf_make(mtu, false);
77-
if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) {
78-
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
79-
_z_wbuf_put(&wbf, 0, i);
80-
}
81-
_z_wbuf_set_wpos(&wbf, _Z_MSG_LEN_ENC_SIZE);
82-
}
8392

93+
switch (zl->_cap._flow) {
94+
case Z_LINK_CAP_FLOW_STREAM:
95+
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
96+
_z_wbuf_put(&wbf, 0, i);
97+
}
98+
_z_wbuf_set_wpos(&wbf, _Z_MSG_LEN_ENC_SIZE);
99+
break;
100+
case Z_LINK_CAP_FLOW_DATAGRAM:
101+
break;
102+
default:
103+
ret = _Z_ERR_GENERIC;
104+
break;
105+
}
84106
// Encode the session message
85107
ret = _z_transport_message_encode(&wbf, t_msg);
86108
if (ret == _Z_RES_OK) {
87-
// Write the message length in the reserved space if needed
88-
if (_Z_LINK_IS_STREAMED(zl->_capabilities) == true) {
89-
size_t len = _z_wbuf_len(&wbf) - _Z_MSG_LEN_ENC_SIZE;
90-
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
91-
_z_wbuf_put(&wbf, (uint8_t)((len >> (uint8_t)8 * i) & (uint8_t)0xFF), i);
109+
switch (zl->_cap._flow) {
110+
case Z_LINK_CAP_FLOW_STREAM: {
111+
// Write the message length in the reserved space if needed
112+
size_t len = _z_wbuf_len(&wbf) - _Z_MSG_LEN_ENC_SIZE;
113+
for (uint8_t i = 0; i < _Z_MSG_LEN_ENC_SIZE; i++) {
114+
_z_wbuf_put(&wbf, (uint8_t)((len >> (uint8_t)8 * i) & (uint8_t)0xFF), i);
115+
}
116+
break;
92117
}
118+
case Z_LINK_CAP_FLOW_DATAGRAM:
119+
break;
120+
default:
121+
ret = _Z_ERR_GENERIC;
122+
break;
93123
}
94-
95124
// Send the wbuf on the socket
96125
ret = _z_link_send_wbuf(zl, &wbf);
97126
}

0 commit comments

Comments
 (0)