Skip to content

Commit

Permalink
feat: add rx function
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Nov 23, 2023
1 parent 289a0ee commit 820eb2c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
5 changes: 3 additions & 2 deletions include/zenoh-pico/system/link/raweth.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,16 @@ typedef struct {
typedef struct {
void *_config; // Pointer to config data
_z_sys_net_socket_t _sock;
uint16_t _vlan_val;
uint16_t _vlan;
uint8_t _dmac[_ZP_MAC_ADDR_LENGTH];
uint8_t _smac[_ZP_MAC_ADDR_LENGTH];
_Bool has_vlan;
_Bool _has_vlan;
} _z_raweth_socket_t;

int8_t _z_get_smac_raweth(_z_raweth_socket_t *resock);
int8_t _z_open_raweth(_z_sys_net_socket_t *sock);
size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t buff_len);
size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr);

#endif

Expand Down
17 changes: 16 additions & 1 deletion src/system/unix/link/raweth.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// ZettaScale Zenoh Team, <[email protected]>
//

#include "zenoh-pico/system/link/raweth.h"

#include <arpa/inet.h>
#include <errno.h>
#include <ifaddrs.h>
Expand All @@ -31,7 +33,6 @@
#include "zenoh-pico/collections/string.h"
#include "zenoh-pico/config.h"
#include "zenoh-pico/system/platform/unix.h"
#include "zenoh-pico/system/link/raweth.h"
#include "zenoh-pico/utils/logging.h"
#include "zenoh-pico/utils/pointers.h"

Expand Down Expand Up @@ -105,4 +106,18 @@ size_t _z_send_raweth(const _z_sys_net_socket_t *sock, const void *buff, size_t
return (size_t)wb;
}

size_t _z_receive_raweth(const _z_sys_net_socket_t *sock, void *buff, size_t buff_len, _z_bytes_t *addr) {
size_t bytesRead = recvfrom(sock->_fd, buff, buff_len, 0, NULL, NULL);
if (bytesRead < 0) {
return SIZE_MAX;
}
// Soft Filtering ?
// Copy sender mac if needed
if ((addr != NULL) && (bytesRead > 2 * ETH_ALEN)) {
*addr = _z_bytes_make(sizeof(ETH_ALEN));
(void)memcpy((uint8_t *)addr->start, &(buff + ETH_ALEN), sizeof(ETH_ALEN));
}
return bytesRead;
}

#endif
11 changes: 9 additions & 2 deletions src/transport/raweth/rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@

#if Z_FEATURE_RAWETH_TRANSPORT == 1

static size_t _z_raweth_link_recv_zbuf(const _z_link_t *link, _z_zbuf_t *zbf, _z_bytes_t *addr) {
size_t rb = _z_receive_raweth(link->_socket._raweth._sock._fd, _z_zbuf_get_wptr(zbf), _z_zbuf_space_left(zbf), addr);
if (rb != SIZE_MAX) {
_z_zbuf_set_wpos(zbf, _z_zbuf_get_wpos(zbf) + rb);
}
return rb;
}

_z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l, _z_bytes_t *remote_addr) {
_z_transport_peer_entry_t *ret = NULL;

Expand All @@ -41,7 +49,6 @@ _z_transport_peer_entry_t *_z_find_peer_entry(_z_transport_peer_entry_list_t *l,
ret = val;
}
}

return ret;
}

Expand All @@ -60,7 +67,7 @@ int8_t _z_raweth_recv_t_msg_na(_z_transport_multicast_t *ztm, _z_transport_messa
switch (ztm->_link._cap._flow) {
case Z_LINK_CAP_FLOW_STREAM:
if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) {
_z_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr);
_z_raweth_link_recv_zbuf(&ztm->_link, &ztm->_zbuf, addr);
if (_z_zbuf_len(&ztm->_zbuf) < _Z_MSG_LEN_ENC_SIZE) {
_z_zbuf_compact(&ztm->_zbuf);
ret = _Z_ERR_TRANSPORT_NOT_ENOUGH_BYTES;
Expand Down
4 changes: 2 additions & 2 deletions src/transport/raweth/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static _z_zint_t __unsafe_z_raweth_get_sn(_z_transport_multicast_t *ztm, z_relia
*/
static void __unsafe_z_raweth_prepare_header(_z_transport_multicast_t *ztm) {
// Reserve space for ethernet header
if (&ztm->_link._socket._raweth.has_vlan) {
if (&ztm->_link._socket._raweth._has_vlan) {
_z_wbuf_set_wpos(&ztm->_wbuf, sizeof(_zp_eth_vlan_header_t));
} else {
_z_wbuf_set_wpos(&ztm->_wbuf, sizeof(_zp_eth_header_t));
Expand All @@ -70,7 +70,7 @@ static int8_t __unsafe_z_raweth_write_header(_z_transport_multicast_t *ztm) {

// FIXME config function call to set the correct dmac & vlan value
// Write eth header in buffer
if (resocket->has_vlan) {
if (resocket->_has_vlan) {
_zp_eth_vlan_header_t header;
memset(&header, 0, sizeof(header));
memcpy(&header.dmac, &resocket->_dmac, _ZP_MAC_ADDR_LENGTH);
Expand Down

0 comments on commit 820eb2c

Please sign in to comment.