From 412a8f99928beff605805807b0f07f6bf8d0a965 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Fri, 13 May 2022 12:45:53 +0200 Subject: code rename --- fw/fe310/eos/net/sock.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 fw/fe310/eos/net/sock.c (limited to 'fw/fe310/eos/net/sock.c') diff --git a/fw/fe310/eos/net/sock.c b/fw/fe310/eos/net/sock.c new file mode 100644 index 0000000..7365c97 --- /dev/null +++ b/fw/fe310/eos/net/sock.c @@ -0,0 +1,154 @@ +#include +#include +#include + +#include "eos.h" +#include "event.h" +#include "net.h" + +#include "sock.h" + +static eos_evt_handler_t evt_handler[EOS_SOCK_MAX_SOCK]; + +static void sock_handle_msg(unsigned char type, unsigned char *buffer, uint16_t len) { + unsigned char sock; + + if ((buffer == NULL) || (len < 2)) { + eos_net_bad_handler(type, buffer, len); + return; + } + + sock = buffer[1]; + if ((sock == 0) || (sock > EOS_SOCK_MAX_SOCK) || (evt_handler[sock - 1] == NULL)) { + eos_net_bad_handler(type, buffer, len); + return; + } + + switch(buffer[0]) { + case EOS_SOCK_MTYPE_PKT: + evt_handler[sock - 1](type, buffer, len); + break; + default: + eos_net_bad_handler(type, buffer, len); + break; + } +} + +void eos_sock_netinit(void) { + int i; + + for (i=0; ihost, 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; +} + +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