Skip to content

Commit

Permalink
Merge branch 'eclipse-zenoh:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaz98 authored Jan 26, 2023
2 parents 820a625 + 8c3bc79 commit 4681f16
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 13 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ It is fully compatible with its main [Rust Zenoh implementation](https://github.

Currently, zenoh-pico provides support for the following (RT)OSs and protocols:

| **(RT)OS** | **Transport Layer** | **Network Layer** | **Data Link Layer** |
|:---------------:|:--------------------------------:|:-------------------:|:------------------------------------------:|
| **Unix** | UDP (unicast and multicast), TCP | IPv4, IPv6, 6LoWPAN | WiFi, Ethernet, Thread |
| **Zephyr** | UDP (unicast and multicast), TCP | IPv4, IPv6, 6LoWPAN | WiFi, Ethernet, Thread, Serial |
| **Arduino** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Bluetooth (Serial profile) |
| **ESP-IDF** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Serial |
| **MbedOS** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Serial |
| **OpenCR** | UDP (unicast and multicast), TCP | IPv4 | WiFi |
| **Emscripten** | Websocket | IPv4, IPv6 | WiFi, Ethernet |
| **(RT)OS** | **Transport Layer** | **Network Layer** | **Data Link Layer** |
|:---------------:|:--------------------------------:|:-------------------:|:--------------------------------------------------:|
| **Unix** | UDP (unicast and multicast), TCP | IPv4, IPv6, 6LoWPAN | WiFi, Ethernet, Thread |
| **Zephyr** | UDP (unicast and multicast), TCP | IPv4, IPv6, 6LoWPAN | WiFi, Ethernet, Thread, Serial |
| **Arduino** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Bluetooth (Serial profile), Serial |
| **ESP-IDF** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Serial |
| **MbedOS** | UDP (unicast and multicast), TCP | IPv4, IPv6 | WiFi, Ethernet, Serial |
| **OpenCR** | UDP (unicast and multicast), TCP | IPv4 | WiFi |
| **Emscripten** | Websocket | IPv4, IPv6 | WiFi, Ethernet |

Check the website [zenoh.io](http://zenoh.io) and the [roadmap](https://github.com/eclipse-zenoh/roadmap) for more detailed information.

Expand Down
6 changes: 4 additions & 2 deletions include/zenoh-pico/system/platform/arduino/esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ typedef struct timespec z_clock_t;
typedef struct timeval z_time_t;

typedef struct BluetoothSerial BluetoothSerial; // Forward declaration to be used in _z_sys_net_socket_t
typedef struct HardwareSerial HardwareSerial; // Forward declaration to be used in _z_sys_net_socket_t

typedef struct {
union {
Expand All @@ -43,7 +44,9 @@ typedef struct {
#if Z_LINK_BLUETOOTH == 1
BluetoothSerial *_bts; // As pointer to cross the boundary between C and C++
#endif
_Bool _err;
#if Z_LINK_SERIAL == 1
HardwareSerial *_serial; // As pointer to cross the boundary between C and C++
#endif
};
} _z_sys_net_socket_t;

Expand All @@ -52,7 +55,6 @@ typedef struct {
#if Z_LINK_TCP == 1 || Z_LINK_UDP_MULTICAST == 1 || Z_LINK_UDP_UNICAST == 1
struct addrinfo *_iptcp;
#endif
_Bool _err;
};
} _z_sys_net_endpoint_t;

Expand Down
221 changes: 219 additions & 2 deletions src/system/arduino/esp32/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//

#include <BluetoothSerial.h>
#include <HardwareSerial.h>

extern "C" {
#include <netdb.h>
Expand All @@ -24,7 +25,10 @@ extern "C" {
#include "zenoh-pico/collections/string.h"
#include "zenoh-pico/config.h"
#include "zenoh-pico/system/link/bt.h"
#include "zenoh-pico/system/link/serial.h"
#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/utils/checksum.h"
#include "zenoh-pico/utils/encoding.h"
#include "zenoh-pico/utils/logging.h"
#include "zenoh-pico/utils/pointers.h"

Expand Down Expand Up @@ -637,7 +641,220 @@ size_t _z_send_bt(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len
#endif

#if Z_LINK_SERIAL == 1
#error "Serial not supported yet on Arduino port of Zenoh-Pico"
/*------------------ Serial sockets ------------------*/
int8_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) {
int8_t ret = _Z_RES_OK;

uint8_t uart = 255;
if (rxpin == 3 && txpin == 1) {
uart = 0;
} else if (rxpin == 9 && txpin == 10) {
uart = 1;
} else if (rxpin == 16 && txpin == 17) {
uart = 2;
} else {
ret = _Z_ERR_GENERIC;
}

if (ret == _Z_RES_OK) {
// WARNING: Glitch on the very 1st byte is common when the serial input line is allowed to float.
// To minimize this issue the RxPin is set to INPUT_PULLUP, and set the TxPin is driven to HIGH. This
// will keep the pins high and upon initialization of the serial port using serial.begin() the line
// does not float (drops to low).
pinMode(rxpin, INPUT_PULLUP);
pinMode(txpin, OUTPUT);
digitalWrite(txpin, HIGH);

sock->_serial = new HardwareSerial(uart);
if (sock->_serial != NULL) {
sock->_serial->begin(baudrate);
sock->_serial->flush();
} else {
ret = _Z_ERR_GENERIC;
}
}

return ret;
}

int8_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t baudrate) {
int8_t ret = _Z_RES_OK;

uint8_t uart = 255;
uint32_t rxpin = 0;
uint32_t txpin = 0;
if (strcmp(dev, "UART_0") == 0) {
uart = 0;
rxpin = 3;
txpin = 1;
} else if (strcmp(dev, "UART_1") == 0) {
uart = 1;
rxpin = 9;
txpin = 10;
} else if (strcmp(dev, "UART_2") == 0) {
uart = 2;
rxpin = 16;
txpin = 17;
} else {
ret = _Z_ERR_GENERIC;
}

if (ret == _Z_RES_OK) {
// WARNING: Glitch on the very 1st byte is common when the serial input line is allowed to float.
// To minimize this issue the RxPin is set to INPUT_PULLUP, and set the TxPin is driven to HIGH. This
// will keep the pins high and upon initialization of the serial port using serial.begin() the line
// does not float (drops to low).
pinMode(rxpin, INPUT_PULLUP);
pinMode(txpin, OUTPUT);
digitalWrite(txpin, HIGH);

sock->_serial = new HardwareSerial(uart);
if (sock->_serial != NULL) {
sock->_serial->begin(baudrate);
sock->_serial->flush();
} else {
ret = _Z_ERR_GENERIC;
}
}

return ret;
}

int8_t _z_listen_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, uint32_t rxpin, uint32_t baudrate) {
int8_t ret = _Z_RES_OK;
(void)(sock);
(void)(txpin);
(void)(rxpin);
(void)(baudrate);

// @TODO: To be implemented
ret = _Z_ERR_GENERIC;

return ret;
}

int8_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_t baudrate) {
int8_t ret = _Z_RES_OK;
(void)(sock);
(void)(dev);
(void)(baudrate);

// @TODO: To be implemented
ret = _Z_ERR_GENERIC;

return ret;
}

void _z_close_serial(_z_sys_net_socket_t *sock) {
sock->_serial->end();
delete sock->_serial;
}

size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) {
int8_t ret = _Z_RES_OK;

uint8_t *before_cobs = new uint8_t[_Z_SERIAL_MAX_COBS_BUF_SIZE]();
size_t rb = 0;
for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) {
while (sock._serial->available() < 1) {
z_sleep_ms(1); // FIXME: Yield by sleeping.
}
before_cobs[i] = sock._serial->read();
rb = rb + (size_t)1;
if (before_cobs[i] == (uint8_t)0x00) {
break;
}
}

uint8_t *after_cobs = new uint8_t[_Z_SERIAL_MFS_SIZE]();
size_t trb = _z_cobs_decode(before_cobs, rb, after_cobs);

size_t i = 0;
uint16_t payload_len = 0;
for (i = 0; i < sizeof(payload_len); i++) {
payload_len |= (after_cobs[i] << ((uint8_t)i * (uint8_t)8));
}

if (trb == (size_t)(payload_len + (uint16_t)6)) {
(void)memcpy(ptr, &after_cobs[i], payload_len);
i = i + (size_t)payload_len;

uint32_t crc = 0;
for (uint8_t j = 0; j < sizeof(crc); j++) {
crc |= (uint32_t)(after_cobs[i] << (j * (uint8_t)8));
i = i + (size_t)1;
}

uint32_t c_crc = _z_crc32(ptr, payload_len);
if (c_crc != crc) {
ret = _Z_ERR_GENERIC;
}
} else {
ret = _Z_ERR_GENERIC;
}

delete before_cobs;
delete after_cobs;

rb = payload_len;
if (ret != _Z_RES_OK) {
rb = SIZE_MAX;
}

return rb;
}

size_t _z_read_exact_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) {
size_t n = len;
size_t rb = 0;

do {
rb = _z_read_serial(sock, ptr, n);
if (rb == SIZE_MAX) return rb;

n -= rb;
ptr = ptr + (len - n);
} while (n > 0);

return len;
}

size_t _z_send_serial(const _z_sys_net_socket_t sock, const uint8_t *ptr, size_t len) {
int8_t ret = _Z_RES_OK;

uint8_t *before_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE);
size_t i = 0;
for (i = 0; i < sizeof(uint16_t); ++i) {
before_cobs[i] = (len >> (i * (size_t)8)) & (size_t)0XFF;
}

(void)memcpy(&before_cobs[i], ptr, len);
i = i + len;

uint32_t crc = _z_crc32(ptr, len);
for (uint8_t j = 0; j < sizeof(crc); j++) {
before_cobs[i] = (crc >> (j * (uint8_t)8)) & (uint32_t)0XFF;
i = i + (size_t)1;
}

uint8_t *after_cobs = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE);
ssize_t twb = _z_cobs_encode(before_cobs, i, after_cobs);
after_cobs[twb] = 0x00; // Manually add the COBS delimiter
ssize_t wb = sock._serial->write(after_cobs, twb + (ssize_t)1);
if (wb != (twb + (ssize_t)1)) {
ret = _Z_ERR_GENERIC;
}

z_free(before_cobs);
z_free(after_cobs);

size_t sb = len;
if (ret != _Z_RES_OK) {
sb = SIZE_MAX;
}

return sb;
}
#endif

} // extern "C"
} // extern "C"

0 comments on commit 4681f16

Please sign in to comment.