summaryrefslogtreecommitdiff
path: root/code/ecp/fe310
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2019-10-19 19:37:03 +0200
committerUros Majstorovic <majstor@majstor.org>2019-10-19 19:37:03 +0200
commit84735c23109b77e0691da208d31c374aa943b257 (patch)
tree2ff1430be30807c326c3b8274b0d0c0db7e802a2 /code/ecp/fe310
parent3a5f92b720afcb4adac61174dbefefd420aa5319 (diff)
fixed transport interface; risc-v transport updated to support eos sock interface
Diffstat (limited to 'code/ecp/fe310')
-rw-r--r--code/ecp/fe310/Makefile4
-rw-r--r--code/ecp/fe310/time.c22
-rw-r--r--code/ecp/fe310/transport.c81
-rw-r--r--code/ecp/fe310/transport.h9
4 files changed, 83 insertions, 33 deletions
diff --git a/code/ecp/fe310/Makefile b/code/ecp/fe310/Makefile
index 1ccf322..66663f0 100644
--- a/code/ecp/fe310/Makefile
+++ b/code/ecp/fe310/Makefile
@@ -1,5 +1,5 @@
include ../Makefile.platform
-CFLAGS=$(CFLAGS_PL) $(PIC) -I.. -I../../fe310
+CFLAGS=$(CFLAGS_PL) $(PIC) -I..
obj_tr = transport.o
obj_tm = time.o
@@ -9,7 +9,7 @@ obj_tm = time.o
$(CC) $(CFLAGS) -c $<
all: libecptr.a libecptm.a
-
+
libecptr.a: $(obj_tr)
$(AR) rcs libecptr.a $(obj_tr)
diff --git a/code/ecp/fe310/time.c b/code/ecp/fe310/time.c
index 455c762..1346fea 100644
--- a/code/ecp/fe310/time.c
+++ b/code/ecp/fe310/time.c
@@ -1,21 +1,39 @@
#include <core.h>
#include <tr.h>
+#include <eos/eos.h>
+#include <eos/event.h>
#include <eos/timer.h>
#include "encoding.h"
#include "platform.h"
+extern ECPSocket *_ecp_tr_sock;
+
+static void timer_handler(unsigned char type) {
+ ecp_cts_t next = ecp_timer_exe(_ecp_tr_sock);
+ if (next) {
+ volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
+ uint64_t tick = *mtime + next * (uint64_t)RTC_FREQ / 1000;
+ eos_timer_set(tick, EOS_TIMER_ETYPE_ECP, 0);
+ }
+}
+
+int ecp_tm_init(ECPContext *ctx) {
+ eos_timer_set_handler(EOS_TIMER_ETYPE_ECP, timer_handler, EOS_EVT_FLAG_NET_BUF_ACQ);
+ return ECP_OK;
+}
+
ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) {
volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
-
+
uint64_t now_ms = *mtime * 1000 / RTC_FREQ;
return now_ms + msec;
}
void ecp_tm_sleep_ms(ecp_cts_t msec) {
volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
-
+
uint64_t now_ms = *mtime * 1000 / RTC_FREQ;
while (*mtime * 1000 / RTC_FREQ < now_ms + msec);
}
diff --git a/code/ecp/fe310/transport.c b/code/ecp/fe310/transport.c
index 61d3355..3493966 100644
--- a/code/ecp/fe310/transport.c
+++ b/code/ecp/fe310/transport.c
@@ -1,3 +1,5 @@
+#include <stdlib.h>
+
#include <core.h>
#include <tr.h>
@@ -6,6 +8,42 @@
static unsigned char _flags = 0;
+ECPSocket *_ecp_tr_sock = NULL;
+
+static void packet_handler(unsigned char *buffer, uint16_t len) {
+ ECPNetAddr addr;
+
+ ECP2Buffer bufs;
+ ECPBuffer packet;
+ ECPBuffer payload;
+ unsigned char pld_buf[ECP_MAX_PLD];
+
+ bufs.packet = &packet;
+ bufs.payload = &payload;
+
+ packet.buffer = buffer+EOS_SOCK_SIZE_UDP_HDR;
+ packet.size = ECP_MAX_PKT;
+ payload.buffer = pld_buf;
+ payload.size = ECP_MAX_PLD;
+
+ eos_sock_getfrom(buffer, &addr);
+ ssize_t rv = ecp_pkt_handle(_ecp_tr_sock, &addr, NULL, &bufs, len-EOS_SOCK_SIZE_UDP_HDR);
+#ifdef ECP_DEBUG
+ if (rv < 0) {
+ char b[16];
+ puts("ERR:");
+ puts(itoa(rv, b, 10));
+ puts("\n");
+ }
+#endif
+ if (bufs.packet->buffer) eos_net_free(buffer, 0);
+ eos_net_release();
+}
+
+int ecp_tr_init(ECPContext *ctx) {
+ return ECP_OK;
+}
+
int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) {
if (addr1->port != addr2->port) return 0;
if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0;
@@ -16,50 +54,49 @@ int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) {
return ECP_ERR;
}
-int ecp_tr_open(int *sock, void *addr_s) {
- *sock = 0;
+int ecp_tr_open(ECPSocket *sock, void *addr_s) {
+ sock->sock = eos_sock_open_udp();
+ if (sock->sock < 0) {
+ sock->sock = 0;
+ return ECP_ERR_SEND;
+ }
+ eos_sock_set_handler(sock->sock, packet_handler, 0);
+ _ecp_tr_sock = sock;
return ECP_OK;
}
-void ecp_tr_close(int *sock) {
+void ecp_tr_close(ECPSocket *sock) {
+ eos_sock_close(sock->sock);
+ _ecp_tr_sock = NULL;
}
-ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
+ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
unsigned char *buf = NULL;
- size_t addr_len = sizeof(addr->host) + sizeof(addr->port);
- uint16_t buf_size = msg_size + addr_len;
- unsigned char type = EOS_NET_MTYPE_SOCK;
int rv;
flags |= _flags;
- if (flags & ECP_SEND_FLAG_MORE) {
- type |= EOS_NET_MTYPE_FLAG_ONEW;
- }
- if (flags & ECP_SEND_FLAG_REPLY) {
- if (packet && packet->buffer) {
- buf = packet->buffer-addr_len;
+ if (packet && packet->buffer) {
+ if (flags & ECP_SEND_FLAG_REPLY) {
+ buf = packet->buffer-EOS_SOCK_SIZE_UDP_HDR;
packet->buffer = NULL;
+ } else {
+ buf = eos_net_alloc();
+ memcpy(buf+EOS_SOCK_SIZE_UDP_HDR, packet->buffer, msg_size);
}
- } else {
- buf = eos_net_alloc();
- memcpy(buf+addr_len, packet->buffer, msg_size);
}
if (buf == NULL) return ECP_ERR;
- memcpy(buf, addr->host, sizeof(addr->host));
- memcpy(buf+sizeof(addr->host), &addr->port, sizeof(addr->port));
- rv = eos_net_send(type, buf, buf_size);
+ rv = eos_sock_sendto(sock->sock, buf, msg_size+EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE, addr);
if (rv) return ECP_ERR_SEND;
return msg_size;
}
-ssize_t ecp_tr_recv(int *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
+ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
return ECP_ERR;
}
void ecp_tr_buf_free(ECP2Buffer *b, unsigned char flags) {
- size_t addr_len = ECP_IPv4_ADDR_SIZE + sizeof(uint16_t);
if (b && b->packet && b->packet->buffer) {
- eos_net_free(b->packet->buffer-addr_len, flags & ECP_SEND_FLAG_MORE);
+ eos_net_free(b->packet->buffer-EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE);
b->packet->buffer = NULL;
}
}
diff --git a/code/ecp/fe310/transport.h b/code/ecp/fe310/transport.h
index c35e13e..0fcf43b 100644
--- a/code/ecp/fe310/transport.h
+++ b/code/ecp/fe310/transport.h
@@ -1,10 +1,5 @@
-#include <stdint.h>
-
-#define ECP_IPv4_ADDR_SIZE 4
+#include <eos/sock.h>
+typedef EOSNetAddr ECPNetAddr;
typedef int ECPNetSock;
-typedef struct ECPNetAddr {
- unsigned char host[ECP_IPv4_ADDR_SIZE];
- uint16_t port;
-} ECPNetAddr;