diff --git a/include/zenoh-pico/api/macros.h b/include/zenoh-pico/api/macros.h index dbda6ce6e..4dedda8dc 100644 --- a/include/zenoh-pico/api/macros.h +++ b/include/zenoh-pico/api/macros.h @@ -305,7 +305,8 @@ z_owned_reply_t : z_reply_clone, \ z_owned_hello_t : z_hello_clone, \ z_owned_string_t : z_string_clone, \ - z_owned_string_array_t : z_string_array_clone \ + z_owned_string_array_t : z_string_array_clone, \ + z_owned_config_t : z_config_clone \ )(&x, y) /** diff --git a/src/link/multicast/bt.c b/src/link/multicast/bt.c index 64c66409f..8f6285eef 100644 --- a/src/link/multicast/bt.c +++ b/src/link/multicast/bt.c @@ -28,12 +28,13 @@ int8_t _z_endpoint_bt_valid(_z_endpoint_t *ep) { int8_t ret = _Z_RES_OK; - if (_z_str_eq(ep->_locator._protocol, BT_SCHEMA) != true) { + _z_string_t bt_str = _z_string_alias_str(BT_SCHEMA); + if (!_z_string_equals(&ep->_locator._protocol, &bt_str)) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } if (ret == _Z_RES_OK) { - if (strlen(ep->_locator._address) == (size_t)0) { + if (_z_string_len(&ep->_locator._address) == (size_t)0) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } } @@ -41,6 +42,15 @@ int8_t _z_endpoint_bt_valid(_z_endpoint_t *ep) { return ret; } +static char *__z_convert_address_bt(_z_string_t *address) { + char *ret = NULL; + ret = (char *)z_malloc(_z_string_len(address) + 1); + if (ret != NULL) { + _z_str_n_copy(ret, _z_string_data(address), _z_string_len(address) + 1); + } + return ret; +} + int8_t _z_f_link_open_bt(_z_link_t *self) { int8_t ret = _Z_RES_OK; @@ -54,8 +64,8 @@ int8_t _z_f_link_open_bt(_z_link_t *self) { tout = (uint32_t)strtoul(tout_as_str, NULL, 10); } - self->_socket._bt._gname = self->_endpoint._locator._address; - ret = _z_open_bt(&self->_socket._bt._sock, self->_endpoint._locator._address, mode, profile, tout); + self->_socket._bt._gname = __z_convert_address_bt(&self->_endpoint._locator._address); + ret = _z_open_bt(&self->_socket._bt._sock, self->_socket._bt._gname, mode, profile, tout); return ret; } @@ -73,8 +83,8 @@ int8_t _z_f_link_listen_bt(_z_link_t *self) { tout = (uint32_t)strtoul(tout_as_str, NULL, 10); } - self->_socket._bt._gname = self->_endpoint._locator._address; - ret = _z_listen_bt(&self->_socket._bt._sock, self->_endpoint._locator._address, mode, profile, tout); + self->_socket._bt._gname = __z_convert_address_bt(&self->_endpoint._locator._address); + ret = _z_listen_bt(&self->_socket._bt._sock, self->_socket._bt._gname, mode, profile, tout); return ret; } diff --git a/src/link/unicast/serial.c b/src/link/unicast/serial.c index b8abc8aeb..9f8f1d741 100644 --- a/src/link/unicast/serial.c +++ b/src/link/unicast/serial.c @@ -30,24 +30,36 @@ int8_t _z_endpoint_serial_valid(_z_endpoint_t *endpoint) { int8_t ret = _Z_RES_OK; - if (_z_str_eq(endpoint->_locator._protocol, SERIAL_SCHEMA) != true) { + _z_string_t ser_str = _z_string_alias_str(SERIAL_SCHEMA); + if (!_z_string_equals(&endpoint->_locator._protocol, &ser_str)) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } if (ret == _Z_RES_OK) { - char *p_dot = strchr(endpoint->_locator._address, '.'); + size_t addr_len = _z_string_len(&endpoint->_locator._address); + const char *p_start = _z_string_data(&endpoint->_locator._address); + const char *p_dot = (char *)memchr(p_start, (int)'.', addr_len); if (p_dot != NULL) { - if ((endpoint->_locator._address == p_dot) || - (strlen(p_dot) == (size_t)1)) { // If dot is the first or last character + size_t dot_loc = _z_ptr_char_diff(p_dot, p_start); + // Check if dot is first or last character + if ((dot_loc == 0) || (dot_loc == addr_len)) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } } else { - if (strlen(endpoint->_locator._address) == (size_t)0) { + if (_z_string_len(&endpoint->_locator._address) == (size_t)0) { ret = _Z_ERR_CONFIG_LOCATOR_INVALID; } } } + return ret; +} +static char *__z_convert_address_serial(_z_string_t *address) { + char *ret = NULL; + ret = (char *)z_malloc(_z_string_len(address) + 1); + if (ret != NULL) { + _z_str_n_copy(ret, _z_string_data(address), _z_string_len(address) + 1); + } return ret; } @@ -56,15 +68,16 @@ int8_t _z_f_link_open_serial(_z_link_t *self) { const char *baudrate_str = _z_str_intmap_get(&self->_endpoint._config, SERIAL_CONFIG_BAUDRATE_KEY); uint32_t baudrate = (uint32_t)strtoul(baudrate_str, NULL, 10); + char *address = __z_convert_address_serial(&self->_endpoint._locator._address); + char *p_dot = strchr(address, '.'); - char *p_dot = strchr(self->_endpoint._locator._address, '.'); if (p_dot != NULL) { - uint32_t txpin = (uint32_t)strtoul(self->_endpoint._locator._address, &p_dot, 10); + uint32_t txpin = (uint32_t)strtoul(address, &p_dot, 10); p_dot = _z_ptr_char_offset(p_dot, 1); uint32_t rxpin = (uint32_t)strtoul(p_dot, NULL, 10); ret = _z_open_serial_from_pins(&self->_socket._serial._sock, txpin, rxpin, baudrate); } else { - ret = _z_open_serial_from_dev(&self->_socket._serial._sock, self->_endpoint._locator._address, baudrate); + ret = _z_open_serial_from_dev(&self->_socket._serial._sock, address, baudrate); } return ret; @@ -75,15 +88,16 @@ int8_t _z_f_link_listen_serial(_z_link_t *self) { const char *baudrate_str = _z_str_intmap_get(&self->_endpoint._config, SERIAL_CONFIG_BAUDRATE_KEY); uint32_t baudrate = (uint32_t)strtoul(baudrate_str, NULL, 10); + char *address = __z_convert_address_serial(&self->_endpoint._locator._address); + char *p_dot = strchr(address, '.'); - char *p_dot = strchr(self->_endpoint._locator._address, '.'); if (p_dot != NULL) { - uint32_t txpin = (uint32_t)strtoul(self->_endpoint._locator._address, &p_dot, 10); + uint32_t txpin = (uint32_t)strtoul(address, &p_dot, 10); p_dot = _z_ptr_char_offset(p_dot, 1); uint32_t rxpin = (uint32_t)strtoul(p_dot, NULL, 10); ret = _z_listen_serial_from_pins(&self->_socket._serial._sock, txpin, rxpin, baudrate); } else { - ret = _z_listen_serial_from_dev(&self->_socket._serial._sock, self->_endpoint._locator._address, baudrate); + ret = _z_listen_serial_from_dev(&self->_socket._serial._sock, address, baudrate); } return ret; diff --git a/tests/z_client_test.c b/tests/z_client_test.c index 8bdc10d16..9713163cd 100644 --- a/tests/z_client_test.c +++ b/tests/z_client_test.c @@ -53,9 +53,10 @@ void query_handler(const z_loaned_query_t *query, void *arg) { snprintf(res, 64, "%s%u", uri, *(unsigned int *)arg); printf(">> Received query: %s\t(%u/%u)\n", res, queries, total); - z_view_string_t k_str; + z_view_string_t k_str, res_str; z_keyexpr_as_view_string(z_query_keyexpr(query), &k_str); - assert(_z_str_eq(z_string_data(z_loan(k_str)), res) == true); + z_view_string_from_str(&res_str, res); + assert(_z_string_equals(z_loan(k_str), z_loan(res_str))); z_view_string_t pred; z_query_parameters(query, &pred); @@ -78,13 +79,14 @@ void reply_handler(const z_loaned_reply_t *reply, void *arg) { const z_loaned_sample_t *sample = z_reply_ok(reply); printf(">> Received reply data: %s\t(%u/%u)\n", res, replies, total); - z_view_string_t k_str; + z_view_string_t k_str, res_str; z_keyexpr_as_view_string(z_sample_keyexpr(sample), &k_str); z_owned_string_t value; z_bytes_deserialize_into_string(z_sample_payload(sample), &value); assert(z_string_len(z_loan(value)) == strlen(res)); assert(strncmp(res, z_string_data(z_loan(value)), strlen(res)) == 0); - assert(_z_str_eq(z_string_data(z_loan(k_str)), res) == true); + z_view_string_from_str(&res_str, res); + assert(_z_string_equals(z_loan(k_str), z_loan(res_str))); replies++; z_drop(z_move(value));