Skip to content

Commit 7add668

Browse files
committed
Rework arduino/esp32 serail (not tested)
1 parent addd19e commit 7add668

File tree

2 files changed

+38
-108
lines changed

2 files changed

+38
-108
lines changed

include/zenoh-pico/system/platform/arduino/esp32.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ typedef struct {
4747
BluetoothSerial *_bts; // As pointer to cross the boundary between C and C++
4848
#endif
4949
#if Z_FEATURE_LINK_SERIAL == 1
50-
HardwareSerial *_serial; // As pointer to cross the boundary between C and C++
50+
struct {
51+
HardwareSerial *_serial; // As pointer to cross the boundary between C and C++
52+
uint8_t *tmp_buf;
53+
uint8_t *raw_buf;
54+
};
5155
#endif
5256
};
5357
} _z_sys_net_socket_t;

src/system/arduino/esp32/network.cpp

+33-107
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ extern "C" {
2424
#include "zenoh-pico/collections/slice.h"
2525
#include "zenoh-pico/collections/string.h"
2626
#include "zenoh-pico/config.h"
27+
#include "zenoh-pico/protocol/codec/serial.h"
28+
#include "zenoh-pico/system/common/serial.h"
2729
#include "zenoh-pico/system/link/bt.h"
2830
#include "zenoh-pico/system/link/serial.h"
2931
#include "zenoh-pico/system/platform.h"
@@ -687,8 +689,6 @@ z_result_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, u
687689
}
688690

689691
z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t baudrate) {
690-
z_result_t ret = _Z_RES_OK;
691-
692692
uint8_t uart = 255;
693693
uint32_t rxpin = 0;
694694
uint32_t txpin = 0;
@@ -705,28 +705,26 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_
705705
rxpin = 16;
706706
txpin = 17;
707707
} else {
708-
ret = _Z_ERR_GENERIC;
708+
return _Z_ERR_GENERIC;
709709
}
710710

711-
if (ret == _Z_RES_OK) {
712-
// WARNING: Glitch on the very 1st byte is common when the serial input line is allowed to float.
713-
// To minimize this issue the RxPin is set to INPUT_PULLUP, and set the TxPin is driven to HIGH. This
714-
// will keep the pins high and upon initialization of the serial port using serial.begin() the line
715-
// does not float (drops to low).
716-
pinMode(rxpin, INPUT_PULLUP);
717-
pinMode(txpin, OUTPUT);
718-
digitalWrite(txpin, HIGH);
719-
720-
sock->_serial = new HardwareSerial(uart);
721-
if (sock->_serial != NULL) {
722-
sock->_serial->begin(baudrate);
723-
sock->_serial->flush();
724-
} else {
725-
ret = _Z_ERR_GENERIC;
726-
}
711+
// WARNING: Glitch on the very 1st byte is common when the serial input line is allowed to float.
712+
// To minimize this issue the RxPin is set to INPUT_PULLUP, and set the TxPin is driven to HIGH. This
713+
// will keep the pins high and upon initialization of the serial port using serial.begin() the line
714+
// does not float (drops to low).
715+
pinMode(rxpin, INPUT_PULLUP);
716+
pinMode(txpin, OUTPUT);
717+
digitalWrite(txpin, HIGH);
718+
719+
sock->_serial = new HardwareSerial(uart);
720+
if (sock->_serial != NULL) {
721+
sock->_serial->begin(baudrate);
722+
sock->_serial->flush();
723+
} else {
724+
return _Z_ERR_GENERIC;
727725
}
728726

729-
return ret;
727+
return _z_connect_serial(*sock);
730728
}
731729

732730
z_result_t _z_listen_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) {
@@ -757,112 +755,40 @@ z_result_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint3
757755
void _z_close_serial(_z_sys_net_socket_t *sock) {
758756
sock->_serial->end();
759757
delete sock->_serial;
758+
z_free(sock->tmp_buf);
759+
z_free(sock->raw_buf);
760760
}
761761

762-
size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) {
763-
z_result_t ret = _Z_RES_OK;
764-
765-
uint8_t *before_cobs = new uint8_t[_Z_SERIAL_MAX_COBS_BUF_SIZE]();
762+
size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len) {
766763
size_t rb = 0;
767764
for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) {
768765
while (sock._serial->available() < 1) {
769766
z_sleep_ms(1); // FIXME: Yield by sleeping.
770767
}
771-
before_cobs[i] = sock._serial->read();
768+
sock.raw_buf[i] = sock._serial->read();
772769
rb = rb + (size_t)1;
773-
if (before_cobs[i] == (uint8_t)0x00) {
770+
if (sock.raw_buf[i] == (uint8_t)0x00) {
774771
break;
775772
}
776773
}
777774

778-
uint8_t *after_cobs = new uint8_t[_Z_SERIAL_MFS_SIZE]();
779-
size_t trb = _z_cobs_decode(before_cobs, rb, after_cobs);
780-
781-
size_t i = 0;
782-
uint16_t payload_len = 0;
783-
for (i = 0; i < sizeof(payload_len); i++) {
784-
payload_len |= (after_cobs[i] << ((uint8_t)i * (uint8_t)8));
785-
}
786-
787-
if (trb == (size_t)(payload_len + (uint16_t)6)) {
788-
(void)memcpy(ptr, &after_cobs[i], payload_len);
789-
i = i + (size_t)payload_len;
790-
791-
uint32_t crc = 0;
792-
for (uint8_t j = 0; j < sizeof(crc); j++) {
793-
crc |= (uint32_t)(after_cobs[i] << (j * (uint8_t)8));
794-
i = i + (size_t)1;
795-
}
796-
797-
uint32_t c_crc = _z_crc32(ptr, payload_len);
798-
if (c_crc != crc) {
799-
ret = _Z_ERR_GENERIC;
800-
}
801-
} else {
802-
ret = _Z_ERR_GENERIC;
803-
}
804-
805-
delete before_cobs;
806-
delete after_cobs;
807-
808-
rb = payload_len;
809-
if (ret != _Z_RES_OK) {
810-
rb = SIZE_MAX;
811-
}
812-
813-
return rb;
775+
return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE);
814776
}
815777

816-
size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) {
817-
size_t n = len;
818-
size_t rb = 0;
819-
820-
do {
821-
rb = _z_read_serial(sock, ptr, n);
822-
if (rb == SIZE_MAX) return rb;
823-
824-
n -= rb;
825-
ptr = ptr + (len - n);
826-
} while (n > 0);
778+
size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) {
779+
size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf,
780+
_Z_SERIAL_MFS_SIZE);
827781

828-
return len;
829-
}
830-
831-
size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) {
832-
z_result_t ret = _Z_RES_OK;
833-
834-
uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE);
835-
size_t i = 0;
836-
for (i = 0; i < sizeof(uint16_t); ++i) {
837-
before_cobs[i] = (len >> (i * (size_t)8)) & (size_t)0XFF;
838-
}
839-
840-
(void)memcpy(&before_cobs[i], ptr, len);
841-
i = i + len;
842-
843-
uint32_t crc = _z_crc32(ptr, len);
844-
for (uint8_t j = 0; j < sizeof(crc); j++) {
845-
before_cobs[i] = (crc >> (j * (uint8_t)8)) & (uint32_t)0XFF;
846-
i = i + (size_t)1;
782+
if (ret == SIZE_MAX) {
783+
return ret;
847784
}
848785

849-
uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);
850-
ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs);
851-
after_cobs[twb] = 0x00; // Manually add the COBS delimiter
852-
ssize_t wb = sock._serial->write(after_cobs, twb + (ssize_t)1);
853-
if (wb != (twb + (ssize_t)1)) {
854-
ret = _Z_ERR_GENERIC;
786+
ssize_t wb = sock._serial->write(sock.raw_buf, ret);
787+
if (wb != (ssize_t)ret) {
788+
ret = SIZE_MAX;
855789
}
856790

857-
z_free(before_cobs);
858-
z_free(after_cobs);
859-
860-
size_t sb = len;
861-
if (ret != _Z_RES_OK) {
862-
sb = SIZE_MAX;
863-
}
864-
865-
return sb;
791+
return len;
866792
}
867793
#endif
868794

0 commit comments

Comments
 (0)