From c6962c5700f99441538dafa346626bb7e6d12488 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Wed, 30 Mar 2022 13:22:19 +0200 Subject: sock api fixed; net reply messages fixed --- fw/fe310/eos/sock.c | 101 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 25 deletions(-) (limited to 'fw/fe310/eos/sock.c') diff --git a/fw/fe310/eos/sock.c b/fw/fe310/eos/sock.c index ef043d1..7365c97 100644 --- a/fw/fe310/eos/sock.c +++ b/fw/fe310/eos/sock.c @@ -52,52 +52,103 @@ eos_evt_handler_t eos_sock_get_handler(unsigned char sock) { return NULL; } -int eos_sock_open_udp(eos_evt_handler_t handler) { - unsigned char type = EOS_SOCK_MTYPE_OPEN_DGRAM; - unsigned char *buffer = eos_net_alloc(); - uint16_t buf_size; +int eos_sock_open_udp(eos_evt_handler_t handler, unsigned char *buffer) { + unsigned char type; + uint16_t len; + int do_release; int rv, sock; - buffer[0] = type; - rv = eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 1, 0); + do_release = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + do_release = 1; + } + + type = EOS_NET_MTYPE_SOCK; + len = 1; + buffer[0] = EOS_SOCK_MTYPE_OPEN_DGRAM; + + rv = eos_net_xchg(&type, buffer, &len); if (rv) return rv; - eos_evtq_wait(EOS_NET_MTYPE_SOCK, &type, 1, &buffer, &buf_size); - if (buf_size < 2) { - eos_net_free(buffer, 0); - return EOS_ERR_NET; - } + if (type != EOS_NET_MTYPE_SOCK) return EOS_ERR_NET; + if (len < 2) return EOS_ERR_SIZE; sock = buffer[1]; - eos_net_free(buffer, 1); - if (sock == 0) return EOS_ERR_NET; + if (do_release) { + eos_net_free(buffer, 1); + } eos_sock_set_handler(sock, handler); + return sock; } -void eos_sock_close(unsigned char sock) { - unsigned char *buffer = eos_net_alloc(); +void eos_sock_close(unsigned char sock, unsigned char *buffer) { + int async; + + async = 0; + if (buffer == NULL) { + buffer = eos_net_alloc(); + async = 1; + } buffer[0] = EOS_SOCK_MTYPE_CLOSE; buffer[1] = sock; - eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, 1); + _eos_net_send(EOS_NET_MTYPE_SOCK, buffer, 2, async, 1); eos_sock_set_handler(sock, NULL); } -int eos_sock_sendto(unsigned char sock, unsigned char *buffer, uint16_t size, unsigned char more, EOSNetAddr *addr) { - unsigned char type = EOS_NET_MTYPE_SOCK; - +static int sock_send(unsigned char sock, unsigned char *msg, uint16_t msg_len, EOSNetAddr *addr, unsigned char *buffer) { buffer[0] = EOS_SOCK_MTYPE_PKT; buffer[1] = sock; - memcpy(buffer+2, addr->host, sizeof(addr->host)); - memcpy(buffer+2+sizeof(addr->host), &addr->port, sizeof(addr->port)); - return eos_net_send(type, buffer, size, more); + buffer += 2; + memcpy(buffer, addr->host, sizeof(addr->host)); + buffer += sizeof(addr->host); + buffer[0] = addr->port >> 8; + buffer[1] = addr->port; + buffer += sizeof(addr->port); + if (msg) { + if (msg_len + EOS_SOCK_SIZE_UDP_HDR > EOS_NET_MTU) return EOS_ERR_SIZE; + memcpy(buffer, msg, msg_len); + } + + return EOS_OK; } -void eos_sock_getfrom(unsigned char *buffer, EOSNetAddr *addr) { - memcpy(addr->host, buffer+2, sizeof(addr->host)); - memcpy(&addr->port, buffer+2+sizeof(addr->host), sizeof(addr->port)); +int eos_sock_sendto(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer) { + int rv; + + rv = sock_send(sock, msg, msg_len, addr, buffer); + if (rv) return rv; + + return eos_net_send(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR); +} + +int eos_sock_sendto_async(unsigned char sock, unsigned char *msg, size_t msg_len, EOSNetAddr *addr, unsigned char *buffer, unsigned char more) { + int rv; + + rv = sock_send(sock, msg, msg_len, addr, buffer); + if (rv) return rv; + + return eos_net_send_async(EOS_NET_MTYPE_SOCK, buffer, msg_len + EOS_SOCK_SIZE_UDP_HDR, more); } +int eos_sock_recvfrom(unsigned char *buffer, uint16_t len, unsigned char *msg, size_t msg_size, EOSNetAddr *addr) { + if (len < EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE; + + if (buffer[0] != EOS_SOCK_MTYPE_PKT) return EOS_ERR_NET; + buffer += 2; + memcpy(addr->host, buffer, sizeof(addr->host)); + buffer += sizeof(addr->host); + addr->port = (uint16_t)buffer[0] << 8; + addr->port |= (uint16_t)buffer[1]; + buffer += sizeof(addr->port); + if (msg) { + if (msg_size < len - EOS_SOCK_SIZE_UDP_HDR) return EOS_ERR_SIZE; + memcpy(msg, buffer, len - EOS_SOCK_SIZE_UDP_HDR); + } + + return EOS_OK; +} -- cgit v1.2.3