@@ -24,6 +24,8 @@ extern "C" {
24
24
#include " zenoh-pico/collections/slice.h"
25
25
#include " zenoh-pico/collections/string.h"
26
26
#include " zenoh-pico/config.h"
27
+ #include " zenoh-pico/protocol/codec/serial.h"
28
+ #include " zenoh-pico/system/common/serial.h"
27
29
#include " zenoh-pico/system/link/bt.h"
28
30
#include " zenoh-pico/system/link/serial.h"
29
31
#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
687
689
}
688
690
689
691
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
-
692
692
uint8_t uart = 255 ;
693
693
uint32_t rxpin = 0 ;
694
694
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_
705
705
rxpin = 16 ;
706
706
txpin = 17 ;
707
707
} else {
708
- ret = _Z_ERR_GENERIC;
708
+ return _Z_ERR_GENERIC;
709
709
}
710
710
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;
727
725
}
728
726
729
- return ret ;
727
+ return _z_connect_serial (*sock) ;
730
728
}
731
729
732
730
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
757
755
void _z_close_serial (_z_sys_net_socket_t *sock) {
758
756
sock->_serial ->end ();
759
757
delete sock->_serial ;
758
+ z_free (sock->tmp_buf );
759
+ z_free (sock->raw_buf );
760
760
}
761
761
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) {
766
763
size_t rb = 0 ;
767
764
for (size_t i = 0 ; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) {
768
765
while (sock._serial ->available () < 1 ) {
769
766
z_sleep_ms (1 ); // FIXME: Yield by sleeping.
770
767
}
771
- before_cobs [i] = sock._serial ->read ();
768
+ sock. raw_buf [i] = sock._serial ->read ();
772
769
rb = rb + (size_t )1 ;
773
- if (before_cobs [i] == (uint8_t )0x00 ) {
770
+ if (sock. raw_buf [i] == (uint8_t )0x00 ) {
774
771
break ;
775
772
}
776
773
}
777
774
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);
814
776
}
815
777
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);
827
781
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;
847
784
}
848
785
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;
855
789
}
856
790
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;
866
792
}
867
793
#endif
868
794
0 commit comments