summaryrefslogtreecommitdiff
path: root/host
diff options
context:
space:
mode:
Diffstat (limited to 'host')
-rw-r--r--host/Module2
-rw-r--r--host/config.h2
-rw-r--r--host/socket.h67
-rw-r--r--host/src/sci.c33
-rw-r--r--host/src/socketcalls.c237
-rw-r--r--host/src/station.c125
-rw-r--r--host/station.h17
-rw-r--r--host/syscall.h16
-rw-r--r--host/test/src/test_fcall.c17
-rw-r--r--host/test/src/test_netclock.c30
-rw-r--r--host/test/src/test_sci.c8
-rw-r--r--host/test/src/test_station.c42
12 files changed, 563 insertions, 33 deletions
diff --git a/host/Module b/host/Module
index ad42989004..158e32e697 100644
--- a/host/Module
+++ b/host/Module
@@ -1 +1 @@
-SOURCES := fcall.c fcall_param.c probe.c netclock.c sci.c sci_msg.c station.c # phy_hal.c
+SOURCES := fcall.c fcall_param.c probe.c netclock.c sci.c sci_msg.c station.c socketcalls.c # phy_hal.c
diff --git a/host/config.h b/host/config.h
index 4915bbd1df..7051d97de8 100644
--- a/host/config.h
+++ b/host/config.h
@@ -19,6 +19,6 @@
* \todo
*/
-//#define STATION_SOCK /** enable socket communication */
+#undef STATION_SOCK /** enable socket communication */
#endif /* CONFIG_H_ */
diff --git a/host/socket.h b/host/socket.h
new file mode 100644
index 0000000000..d73a0d5afc
--- /dev/null
+++ b/host/socket.h
@@ -0,0 +1,67 @@
+#ifndef SOCKET_H_
+#define SOCKET_H_
+
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+
+ /**
+ * \file socket.h
+ * \brief The socket adaptation layer for host.
+ * \ingroup host
+ *
+ * This file implements the socket layer for host
+ *
+ * \todo
+ */
+
+#include <sys/types.h>
+
+#define HOST_PF_UNIX 1
+#define HOST_AF_UNIX HOST_PF_UNIX
+
+#define HOST_SOCK_STREAM 1
+#define HOST_SOL_SOCKET 1
+#define HOST_SO_SNDBUF 7
+#define HOST_SO_RCVBUF 8
+
+struct host_sockaddr
+{
+ unsigned short int sa_family;
+ char sa_data[14];
+};
+
+#define HOST_UNIX_PATH_MAX 108
+
+struct host_sockaddr_un
+{
+ unsigned short int sun_family; /* AF_UNIX */
+ char sun_path[HOST_UNIX_PATH_MAX]; /* pathname */
+};
+
+extern int host_socket (int __domain, int __type, int __protocol);
+extern int host_socketpair (int __domain, int __type, int __protocol, int __fds[2]);
+extern int host_bind (int __fd, const struct host_sockaddr *__addr, unsigned int __len);
+extern int host_getsockname (int __fd, const struct host_sockaddr *__addr, unsigned int *__len);
+extern int host_connect (int __fd, const struct host_sockaddr *__addr, unsigned int __len);
+extern int host_getpeername (int __fd, const struct host_sockaddr *__addr, unsigned int *__len);
+extern ssize_t host_send (int __fd, const void *__buf, size_t __n, int __flags);
+extern ssize_t host_recv (int __fd, void *__buf, size_t __n, int __flags);
+extern ssize_t host_sendto (int __fd, const void *__buf, size_t __n, int __flags,
+ const struct host_sockaddr *addr, unsigned int __addr_len);
+extern ssize_t host_recvfrom (int __fd, void *__buf, size_t __n, int __flags,
+ const struct host_sockaddr *__addr, unsigned int *__addr_len);
+//extern ssize_t host_sendmsg (int __fd, const struct msghdr *__message, int __flags);
+//extern ssize_t host_recvmsg (int __fd, struct msghdr *__message, int __flags);
+extern int host_getsockopt (int __fd, int __level, int __optname, void *__optval, unsigned int *__optlen);
+extern int host_setsockopt (int __fd, int __level, int __optname, const void *__optval,
+ unsigned int __optlen);
+extern int host_listen (int __fd, int __n);
+extern int host_accept (int __fd, const struct host_sockaddr *__addr, unsigned int *__addr_len);
+extern int host_shutdown (int __fd, int __how);
+
+#endif /*SOCKET_H_*/
diff --git a/host/src/sci.c b/host/src/sci.c
index e1edd848c3..344c07bc7f 100644
--- a/host/src/sci.c
+++ b/host/src/sci.c
@@ -167,7 +167,6 @@ int sci_fill_hdr(
int sci_send(sci_ctx_t *sci, sci_msg_t *msg)
{
int len, total_length;
- struct timespec req, rem;
DBG_ASSERT(sci);
DBG_ASSERT(msg);
@@ -181,7 +180,16 @@ int sci_send(sci_ctx_t *sci, sci_msg_t *msg)
total_length = 0;
while(total_length < msg->length)
{
- len = write(sci->station->pipe_out_fd, msg->data_begin + total_length, msg->length);
+#ifdef STATION_SOCK
+ len = write(sci->station->sock_fd, msg->data_begin + total_length, msg->length);
+ //station_log(sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_SCI, "%s: write %d byte", __FUNCTION__, ntohs(msg->length));
+ if(len < 0)
+ {
+ station_log(sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_SCI, "%s: write %d failed (errno=%d)", __FUNCTION__, ntohs(msg->sci_hdr->msg_id), errno);
+ return -1;
+ }
+#else /* STATION_SOCK */
+ len = write(sci->station->pipe_out_fd, msg->data_begin + total_length, msg->length);
if(len < 0)
{
if(errno != EAGAIN)
@@ -192,11 +200,9 @@ int sci_send(sci_ctx_t *sci, sci_msg_t *msg)
else
{
len = 0;
- req.tv_sec = 0;
- req.tv_nsec = 5000000;
- //nanosleep(&req, &rem);
}
}
+#endif /* STATION_SOCK */
total_length += len;
}
@@ -229,7 +235,12 @@ int sci_recv(sci_ctx_t *sci)
sci_msg_init(&msg, sci_buffer, SCI_MSG_MAX_SIZE);
/* at first, read msg header */
+#ifdef STATION_SOCK
+ len = read(sci->station->sock_fd, &hdr, sizeof(sci_msg_hdr_t));
+ //station_log(sci->station, STATION_LOG_DEBUG, STATION_LOGTYPE_SCI, "%s: read %d bytes", __FUNCTION__, len);
+#else /* STATION_SOCK */
len = read(sci->station->pipe_in_fd, &hdr, sizeof(sci_msg_hdr_t));
+#endif /* STATION_SOCK */
if(len < 0)
{
station_log(sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_SCI, "%s: read msg_hdr failed (errno=%d)", __FUNCTION__, errno);
@@ -279,7 +290,11 @@ int sci_recv(sci_ctx_t *sci)
/* read the remaining part of message */
sci_msg_push(&msg, ntohs(hdr.length));
+#ifdef STATION_SOCK
+ len = read(sci->station->sock_fd, msg.data_begin, msg.length);
+#else /* STATION_SOCK */
len = read(sci->station->pipe_in_fd, msg.data_begin, msg.length);
+#endif /* STATION_SOCK */
if(len < 0)
{
station_log(sci->station, STATION_LOG_WARNING, STATION_LOGTYPE_SCI, "%s: read msg_body failed (errno=%d)", __FUNCTION__, errno);
@@ -309,11 +324,19 @@ int sci_recv(sci_ctx_t *sci)
}
fd_flush:
+#ifdef STATION_SOCK
+ read(sci->station->sock_fd, sci_buffer, sizeof(sci_buffer));
+#else /* STATION_SOCK */
read(sci->station->pipe_in_fd, sci_buffer, sizeof(sci_buffer));
+#endif /* STATIOn_SOCK */
return -1;
read_end:
+#ifdef STATION_SOCK
+ read(sci->station->sock_fd, sci_buffer, hdr.length);
+#else /* STATION_SOCK */
read(sci->station->pipe_in_fd, sci_buffer, hdr.length);
+#endif /* STATION_SOCK */
return -1;
}
diff --git a/host/src/socketcalls.c b/host/src/socketcalls.c
new file mode 100644
index 0000000000..dce3f163b4
--- /dev/null
+++ b/host/src/socketcalls.c
@@ -0,0 +1,237 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+
+/**
+ * \file socket.c
+ * \brief The socket layer for ecos synthetic
+ * \ingroup host
+ *
+ * This file provide the socket functions for eCos synthetic
+ *
+ * \todo
+ */
+
+#include "common/std.h"
+#ifndef UNIT_TEST
+#include "host/syscall.h"
+#endif
+#include "host/socket.h"
+#include <errno.h>
+
+#ifdef UNIT_TEST
+int socketcall(int call, unsigned long *args)
+{
+ return 0;
+}
+#else /* UNIT_TEST */
+extern int socketcall(int call, unsigned long *args);
+#endif /* UNIT_TEST */
+
+/* Various socketcall numbers */
+#define SYS_SOCKET 1
+#define SYS_BIND 2
+#define SYS_CONNECT 3
+#define SYS_LISTEN 4
+#define SYS_ACCEPT 5
+#define SYS_GETSOCKNAME 6
+#define SYS_GETPEERNAME 7
+#define SYS_SOCKETPAIR 8
+#define SYS_SEND 9
+#define SYS_RECV 10
+#define SYS_SENDTO 11
+#define SYS_RECVFROM 12
+#define SYS_SHUTDOWN 13
+#define SYS_SETSOCKOPT 14
+#define SYS_GETSOCKOPT 15
+#define SYS_SENDMSG 16
+#define SYS_RECVMSG 17
+
+int host_accept(int s, const struct host_sockaddr *addr, unsigned int * addrlen)
+{
+ unsigned long args[3];
+
+ args[0] = s;
+ args[1] = (unsigned long) addr;
+ args[2] = (unsigned long) addrlen;
+ return socketcall(SYS_ACCEPT, args);
+}
+
+int host_bind(int sockfd, const struct host_sockaddr *myaddr, unsigned int addrlen)
+{
+ unsigned long args[3];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) myaddr;
+ args[2] = addrlen;
+ return socketcall(SYS_BIND, args);
+}
+
+int host_connect(int sockfd, const struct host_sockaddr *saddr, unsigned int addrlen)
+{
+ unsigned long args[3];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) saddr;
+ args[2] = addrlen;
+ return socketcall(SYS_CONNECT, args);
+}
+
+int host_getpeername(int sockfd, const struct host_sockaddr *addr, unsigned int * paddrlen)
+{
+ unsigned long args[3];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) addr;
+ args[2] = (unsigned long) paddrlen;
+ return socketcall(SYS_GETPEERNAME, args);
+}
+
+int host_getsockname(int sockfd, const struct host_sockaddr *addr, unsigned int * paddrlen)
+{
+ unsigned long args[3];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) addr;
+ args[2] = (unsigned long) paddrlen;
+ return socketcall(SYS_GETSOCKNAME, args);
+}
+
+int host_getsockopt(int fd, int level, int optname, void * optval,
+ unsigned int * optlen)
+{
+ unsigned long args[5];
+
+ args[0] = fd;
+ args[1] = level;
+ args[2] = optname;
+ args[3] = (unsigned long) optval;
+ args[4] = (unsigned long) optlen;
+ return (socketcall(SYS_GETSOCKOPT, args));
+}
+
+int host_listen(int sockfd, int backlog)
+{
+ unsigned long args[2];
+
+ args[0] = sockfd;
+ args[1] = backlog;
+ return socketcall(SYS_LISTEN, args);
+}
+
+ssize_t host_recv(int sockfd, void * buffer, size_t len, int flags)
+{
+ unsigned long args[4];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) buffer;
+ args[2] = len;
+ args[3] = flags;
+ return (socketcall(SYS_RECV, args));
+}
+
+ssize_t host_recvfrom(int sockfd, void * buffer, size_t len, int flags,
+ const struct host_sockaddr *to, unsigned int * tolen)
+{
+ unsigned long args[6];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) buffer;
+ args[2] = len;
+ args[3] = flags;
+ args[4] = (unsigned long) to;
+ args[5] = (unsigned long) tolen;
+ return (socketcall(SYS_RECVFROM, args));
+}
+/*ssize_t host_recvmsg(int sockfd, struct msghdr *msg, int flags)
+{
+ unsigned long args[3];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) msg;
+ args[2] = flags;
+ return (socketcall(SYS_RECVMSG, args));
+}*/
+
+ssize_t host_send(int sockfd, const void *buffer, size_t len, int flags)
+{
+ unsigned long args[4];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) buffer;
+ args[2] = len;
+ args[3] = flags;
+ return (socketcall(SYS_SEND, args));
+}
+
+/*ssize_t host_sendmsg(int sockfd, const struct msghdr *msg, int flags)
+{
+ unsigned long args[3];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) msg;
+ args[2] = flags;
+ return (socketcall(SYS_SENDMSG, args));
+}*/
+
+ssize_t host_sendto(int sockfd, const void *buffer, size_t len, int flags,
+ const struct host_sockaddr *to, unsigned int tolen)
+{
+ unsigned long args[6];
+
+ args[0] = sockfd;
+ args[1] = (unsigned long) buffer;
+ args[2] = len;
+ args[3] = flags;
+ args[4] = (unsigned long) to;
+ args[5] = tolen;
+ return (socketcall(SYS_SENDTO, args));
+}
+
+int host_setsockopt(int fd, int level, int optname, const void *optval,
+ unsigned int optlen)
+{
+ unsigned long args[5];
+
+ args[0] = fd;
+ args[1] = level;
+ args[2] = optname;
+ args[3] = (unsigned long) optval;
+ args[4] = optlen;
+ return (socketcall(SYS_SETSOCKOPT, args));
+}
+
+int host_shutdown(int sockfd, int how)
+{
+ unsigned long args[2];
+
+ args[0] = sockfd;
+ args[1] = how;
+ return (socketcall(SYS_SHUTDOWN, args));
+}
+
+int host_socket(int family, int type, int protocol)
+{
+ unsigned long args[3];
+
+ args[0] = family;
+ args[1] = type;
+ args[2] = (unsigned long) protocol;
+ return socketcall(SYS_SOCKET, args);
+}
+
+int host_socketpair(int family, int type, int protocol, int sockvec[2])
+{
+ unsigned long args[4];
+
+ args[0] = family;
+ args[1] = type;
+ args[2] = protocol;
+ args[3] = (unsigned long) sockvec;
+ return socketcall(SYS_SOCKETPAIR, args);
+}
+
diff --git a/host/src/station.c b/host/src/station.c
index 30501aa61b..55fe3522a3 100644
--- a/host/src/station.c
+++ b/host/src/station.c
@@ -26,11 +26,14 @@
#include "host/sci.h"
#include "host/station.h"
#ifndef UNIT_TEST
+#include "host/socket.h"
#include "host/syscall.h"
#else
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
+#include <sys/socket.h>
+#include <sys/un.h>
#endif
/* awfull, but needed */
@@ -64,6 +67,10 @@ static netclock_callback_t _ecos_tick_cb;
*/
int station_init(station_ctx_t *station)
{
+#ifdef STATION_SOCK
+ int sock_server_fd;
+#endif /* STATION_SOCK */
+
DBG_ASSERT(station);
if(station == NULL)
{
@@ -72,8 +79,14 @@ int station_init(station_ctx_t *station)
}
memset(station, '\0', sizeof(station_ctx_t));
+#ifdef STATION_SOCK
+ station->sock_fd = -1;
+ station->sock_pair_fd = -1;
+ sock_server_fd = -1;
+#else /* STATION_SOCK */
station->pipe_in_fd = -1;
station->pipe_out_fd = -1;
+#endif
station->pipe_log_fd = -1;
/* get station id */
@@ -85,17 +98,66 @@ int station_init(station_ctx_t *station)
/* build pipe names */
sprintf(station->pipe_log_name, "%s/%s_log_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id);
unlink(station->pipe_log_name);
+#ifndef STATION_SOCK
sprintf(station->pipe_in_name, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id);
unlink(station->pipe_in_name);
sprintf(station->pipe_out_name, "%s/%s_out_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id);
unlink(station->pipe_out_name);
-
+#endif
+
/* open log */
if(mknod(station->pipe_log_name, 0770 | S_IFIFO, 0) < 0)
goto failed;
if((station->pipe_log_fd = open(station->pipe_log_name, O_RDWR | O_NONBLOCK, S_IRWXU | S_IRWXG)) < 0)
goto failed;
+#ifdef STATION_SOCK
+ /* build sock name */
+ sprintf(station->sock_name, "%s/%s_sock_%d", STATION_SOCK_PATH, STATION_SOCK_PREFIX, station->id);
+ unlink(station->sock_name);
+
+ /* create socket */
+#ifdef UNIT_TEST
+ int sock_fd[2];
+ socketpair(PF_UNIX, SOCK_STREAM, 0, sock_fd);
+ station->sock_fd = sock_fd[0];
+ station->sock_pair_fd = sock_fd[1];
+#else /* UNIT_TEST */
+ if((sock_server_fd = socket (PF_UNIX, SOCK_STREAM, 0)) < 0)
+ {
+ station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
+ "%s: failed to create socket (errno=%d)",
+ __FUNCTION__, errno);
+ goto failed;
+ }
+
+ /* extend send buffer size */
+ {
+ int bufsize = STATION_MAX_SOCK_BUFFER_SIZE;
+ if(setsockopt (sock_server_fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize)) < 0)
+ {
+ station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
+ "%s: failed to set buffer size to %d bytes (errno=%d)",
+ __FUNCTION__, bufsize, errno);
+ goto failed;
+ }
+ }
+
+ /* bind the socket */
+ {
+ struct sockaddr_un sockaddr;
+ sockaddr.sun_family = AF_UNIX;
+ strcpy (sockaddr.sun_path, station->sock_name);
+ if((bind (sock_server_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) < 0)
+ {
+ station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
+ "%s: bind to '%s' failed (errno=%d)",
+ __FUNCTION__, station->sock_name, errno);
+ goto failed;
+ }
+ }
+#endif /* UNIT_TEST */
+#else /* STATION_SOCK */
/* open in pipe */
if(mknod(station->pipe_in_name, 0770 | S_IFIFO, 0) < 0)
{
@@ -127,6 +189,8 @@ int station_init(station_ctx_t *station)
__FUNCTION__, station->pipe_out_name, errno);
goto failed;
}
+
+#endif /* STATION_SOCK */
/* init all contexts */
station->sci = &_my_sci;
@@ -143,6 +207,19 @@ int station_init(station_ctx_t *station)
/* init the random generator */
srand(station->id);
+#if (defined STATION_SOCK) && !defined(UNIT_TEST)
+ /* listen and accept connection */
+ listen(sock_server_fd, 1);
+ if((station->sock_fd = accept(sock_server_fd, NULL, NULL)) < 0)
+ {
+ station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
+ "%s: accept failed (errno=%d)",
+ __FUNCTION__, errno);
+ goto failed;
+ }
+ close(sock_server_fd);
+#endif /* STATION_SOCK && !UNIT_TEST */
+
#ifndef UNIT_TEST
/* first call for tick */
station_ecos_set_itimer(station, TICK_HZ / 100);
@@ -151,18 +228,30 @@ int station_init(station_ctx_t *station)
return 0;
failed:
- if(station->pipe_out_fd >= 0)
+ if(station->pipe_log_fd >= 0)
+ close(station->pipe_log_fd);
+ unlink(station->pipe_log_name);
+ station->pipe_log_fd = -1;
+#ifdef STATION_SOCK
+ if(station->sock_fd >= 0)
+ close(station->sock_fd);
+ if(station->sock_pair_fd >= 0)
+ close(station->sock_pair_fd);
+ if(sock_server_fd >= 0)
+ close(sock_server_fd);
+ unlink(station->sock_name);
+ station->sock_fd = -1;
+ station->sock_pair_fd = -1;
+#else /* STATION_SOCK */
+ if(station->pipe_out_fd >= 0)
close(station->pipe_out_fd);
if(station->pipe_in_fd >= 0)
close(station->pipe_in_fd);
- if(station->pipe_log_fd >= 0)
- close(station->pipe_log_fd);
unlink(station->pipe_out_name);
unlink(station->pipe_in_name);
- unlink(station->pipe_log_name);
station->pipe_out_fd = -1;
station->pipe_in_fd = -1;
- station->pipe_log_fd = -1;
+#endif /* STATION_SOCK */
station->status = STATION_STATUS_ERROR;
return -1;
}
@@ -176,18 +265,28 @@ void station_down(station_ctx_t *station)
if((station == NULL)
|| (station->status == STATION_STATUS_INIT))
return;
-
+
+#ifdef STATION_SOCK
+ if(station->sock_fd >= 0)
+ close(station->sock_fd);
+ if(station->sock_pair_fd >= 0)
+ close(station->sock_pair_fd);
+ unlink(station->sock_name);
+ station->sock_fd = -1;
+ station->sock_pair_fd = -1;
+#else /* STATION_SOCK */
if(station->pipe_out_fd >= 0)
close(station->pipe_out_fd);
if(station->pipe_in_fd >= 0)
close(station->pipe_in_fd);
- if(station->pipe_log_fd >= 0)
- close(station->pipe_log_fd);
unlink(station->pipe_out_name);
unlink(station->pipe_in_name);
- unlink(station->pipe_log_name);
station->pipe_out_fd = -1;
station->pipe_in_fd = -1;
+#endif /* STATION_SOCK */
+ if(station->pipe_log_fd >= 0)
+ close(station->pipe_log_fd);
+ unlink(station->pipe_log_name);
station->pipe_log_fd = -1;
return;
}
@@ -247,10 +346,14 @@ int station_idle(station_ctx_t *station)
/* vait for next message with 1 second timeout */
FD_ZERO(&read_fds);
+#ifdef STATION_SOCK
+ FD_SET(station->sock_fd, &read_fds);
+#else /* STATION_SOCK */
FD_SET(station->pipe_in_fd, &read_fds);
+#endif /* STATION_SOCK */
timeout.tv_sec = 1;
timeout.tv_usec = 0;
- sel = select(station->pipe_in_fd + 1, &read_fds, NULL, NULL, &timeout);
+ sel = select(STATION_MAX_FD + 1, &read_fds, NULL, NULL, &timeout);
if(sel < 0)
{
station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, "%s select failed (errno=%d)", __FUNCTION__, errno);
diff --git a/host/station.h b/host/station.h
index b7199fa75e..3564545cf8 100644
--- a/host/station.h
+++ b/host/station.h
@@ -21,6 +21,7 @@
#include <sys/types.h>
#include "maximus/common/types/system_types.h"
+#include "host/config.h"
#include "host/fwd.h"
#include "host/sci.h"
#include "host/netclock.h"
@@ -28,7 +29,11 @@
#define STATION_PIPE_PATH "/tmp"
#define STATION_PIPE_PREFIX "station"
+#define STATION_SOCK_PATH STATION_PIPE_PATH
+#define STATION_SOCK_PREFIX STATION_PIPE_PREFIX
#define STATION_MAX_LOG_SIZE 4096
+#define STATION_MAX_SOCK_BUFFER_SIZE (256*1024)
+#define STATION_MAX_FD 16
#define TICK_PERIOD_NS 40 /** period of a tick in nanosecond */
#define TICK_HZ 25000000 /** frequence of tick (tick number per second) */
@@ -82,16 +87,22 @@ struct station_ctx
struct fcall_ctx *fcall;
struct probe_ctx *probe;
tick_t current_tick_tck; /** current global clock tick got from sci msg */
+#ifdef STATION_SOCK
+ int sock_fd; /** file descriptor for messaging input/output */
+ int sock_pair_fd; /** file descriptor for socket pair, used by unit test */
+ char sock_name[MAXPATHLEN]; /** filename of messaging input/output */
+#else /* STATION_SOCK */
int pipe_in_fd; /** file descriptor for messaging input pipe */
char pipe_in_name[MAXPATHLEN]; /** filename of messaging input file */
int pipe_out_fd; /** file descriptor for messaging output pipe */
char pipe_out_name[MAXPATHLEN]; /** filename of messaging output pipe */
+#endif /* STATION_SOCK */
int pipe_log_fd; /** output pipe file desc to send debug data */
char pipe_log_name[MAXPATHLEN]; /** output pipe filename to send debug data */
station_log_level_t log_level;
unsigned long log_mask;
struct netclock_callback *ecos_tick_cb;
- int is_unit_test;
+ //int is_unit_test;
};
//BEGIN_DECLS
@@ -146,7 +157,11 @@ int station_ecos_set_itimer(station_ctx_t *station, tick_t tick);
static inline int station_is_initialized(station_ctx_t *station)
{
+#ifdef STATION_SOCK
+ return (station->sock_fd >= 0);
+#else /* STATION_SOCK */
return ((station->pipe_in_fd >= 0) && (station->pipe_out_fd >= 0));
+#endif /* STATION_SOCK */
}
/**
diff --git a/host/syscall.h b/host/syscall.h
index f413cb9f89..a212e66cda 100644
--- a/host/syscall.h
+++ b/host/syscall.h
@@ -75,6 +75,17 @@
#define FD_CLR CYG_HAL_SYS_FD_CLR
/** functions redefinition */
+/*#define system_adapt(function) { \
+ int result; \
+ if((result = function(##args)) < 0) \
+ errno = -result; \
+ result = -1; \
+ } \
+ return result; \
+}
+
+#define open(args...) system_adapt(cyg_hal_sys_open)*/
+
static inline int open(const char *pathname, int flags, mode_t mode)
{
int result;
@@ -152,7 +163,8 @@ static inline int unlink(const char *pathname)
return result;
}
-static inline pid_t getpid(void)
+#define getpid cyg_hal_sys_getpid
+/*static inline pid_t getpid(void)
{
int result;
if((result = cyg_hal_sys_getpid()) < 0)
@@ -161,7 +173,7 @@ static inline pid_t getpid(void)
result = -1;
}
return result;
-}
+}*/
/** socket redefinition */
#define AF_UNIX HOST_AF_UNIX
diff --git a/host/test/src/test_fcall.c b/host/test/src/test_fcall.c
index cfd7f5c241..c3e21b861a 100644
--- a/host/test/src/test_fcall.c
+++ b/host/test/src/test_fcall.c
@@ -325,9 +325,12 @@ void fcall_return_test_case(test_t t)
);
} test_end;
-
+#ifdef STATION_SOCK
+ fd_in = station.sock_pair_fd;
+#else /* STATION_SOCK */
fd_in = open(station.pipe_out_name, O_RDONLY);
-
+#endif /* STATION_SOCK */
+
test_begin(t, "fcall_return")
{
test_fail_unless(
@@ -374,7 +377,9 @@ void fcall_return_test_case(test_t t)
} test_end;
station_down(&station);
+#ifndef STATION_SOCK
close(fd_in);
+#endif
}
int _test_recv_result;
@@ -447,8 +452,12 @@ void fcall_recv_test_case(test_t t)
);
} test_end;
+#ifdef STATION_SOCK
+ fd_in = station.sock_pair_fd;
+#else /* STATION_SOCK */
fd_in = open(station.pipe_out_name, O_RDONLY);
-
+#endif /* STATION_SOCK */
+
test_begin(t, "check recv")
{
sci_msg_init(&msg, buffer, 256);
@@ -539,7 +548,9 @@ void fcall_recv_test_case(test_t t)
} test_end;
station_down(&station);
+#ifndef STATION_SOCK
close(fd_in);
+#endif /* STATION_SOCK */
return;
}
diff --git a/host/test/src/test_netclock.c b/host/test/src/test_netclock.c
index 2a4cec85e5..df23add6ea 100644
--- a/host/test/src/test_netclock.c
+++ b/host/test/src/test_netclock.c
@@ -254,7 +254,11 @@ void netclock_schedule_test_case(test_t t)
);
} test_end
+#ifdef STATION_SOCK
+ fd_in = station.sock_pair_fd;
+#else /* STATION_SOCK */
fd_in = open(station.pipe_out_name, O_RDONLY);
+#endif /* STATION_SOCK */
test_begin(t, "recv netclock msg")
{
@@ -321,6 +325,9 @@ void netclock_schedule_test_case(test_t t)
// } test_end;
station_down(&station);
+#ifndef STATION_SOCK
+ close(fd_in);
+#endif /* STATION_SOCK */
return;
}
@@ -370,7 +377,11 @@ void netclock_unschedule_test_case(test_t t)
&& (netclock.callback_nb == 1)
);
+#ifdef STATION_SOCK
+ fd_in = station.sock_pair_fd;
+#else /* STATION_SOCK */
fd_in = open(station.pipe_out_name, O_RDONLY);
+#endif /* STATION_SOCK */
test_fail_if((len = read(fd_in, buffer, sizeof(sci_msg_hdr_t))) != sizeof(sci_msg_hdr_t));
hdr = (sci_msg_hdr_t *)buffer;
@@ -396,9 +407,11 @@ void netclock_unschedule_test_case(test_t t)
);
} test_end;
- close(fd_in);
station_down(&station);
-
+#ifndef STATION_SOCK
+ close(fd_in);
+#endif /* STATION_SOCK */
+
return;
}
@@ -423,7 +436,7 @@ void netclock_recv_test_case(test_t t)
netclock_id_t id;
unsigned char buffer[256];
char data_buffer[32];
- int fd_in, fd_out;
+ int fd_in;
test_case_begin(t, "recv");
@@ -448,8 +461,12 @@ void netclock_recv_test_case(test_t t)
);
} test_end;
- fd_in = open(station.pipe_out_name, O_RDONLY);
- fd_out = open(station.pipe_in_name, O_WRONLY);
+#ifdef STATION_SOCK
+ fd_in = station.sock_pair_fd;
+#else /* STATION_SOCK */
+ fd_in = open(station.pipe_out_name, O_RDONLY);
+#endif /* STATION_SOCK */
+
memset(data_buffer, '\0', 32);
test_begin(t, "recv")
@@ -473,8 +490,9 @@ void netclock_recv_test_case(test_t t)
);
} test_end;
+#ifndef STATION_SOCK
close(fd_in);
- close(fd_out);
+#endif /* STATION_SOCK */
station_down(&station);
return;
diff --git a/host/test/src/test_sci.c b/host/test/src/test_sci.c
index c116856e3a..81b724d30d 100644
--- a/host/test/src/test_sci.c
+++ b/host/test/src/test_sci.c
@@ -266,7 +266,11 @@ void sci_send_test_case(test_t t)
test_begin(t, "check send")
{
+#ifdef STATION_SOCK
+ fd_out = station.sock_pair_fd;
+#else
fd_out = open(station.pipe_out_name, O_RDONLY);
+#endif
sci_msg_push(&msg, strlen(TEST_DATA_STR));
memcpy(msg.data_begin, TEST_DATA_STR, msg.length);
sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_FUNCTION_CALL, 0);
@@ -334,7 +338,11 @@ void sci_recv_test_case(test_t t)
msg.hdr.station = (station_msg_hdr_t *)msg.data_begin;
msg.hdr.station->type = SYSTEM_TYPE_IDLE;
sci_fill_hdr(&sci, &msg, SCI_MSG_TYPE_SYSTEM, 0);
+#ifdef STATION_SOCK
+ fd_in = station.sock_pair_fd;
+#else
fd_in = open(station.pipe_in_name, O_WRONLY);
+#endif
test_begin(t, "bad magic")
{
((char *)&msg.sci_hdr->magic_id)[2] = magic_id[2] + 1;
diff --git a/host/test/src/test_station.c b/host/test/src/test_station.c
index 4c09fa7dca..f3f2295a78 100644
--- a/host/test/src/test_station.c
+++ b/host/test/src/test_station.c
@@ -66,7 +66,9 @@ void station_init_test_case(test_t t)
{
station_ctx_t station;
char buffer[256];
+#ifndef STATION_SOCK
struct stat my_stat;
+#endif
test_case_begin(t, "init");
station_init(&station);
@@ -75,8 +77,13 @@ void station_init_test_case(test_t t)
{
test_fail_unless(
(station.id == getpid())
+#ifdef STATION_SOCK
+ && (station.sock_fd >= 0)
+ && (station.sock_pair_fd >= 0)
+#else
&& (station.pipe_in_fd >= 0)
&& (station.pipe_out_fd >= 0)
+#endif
&& (station.pipe_log_fd >= 0)
&& (station.sci != NULL)
&& (station.log_level == STATION_LOG_WARNING)
@@ -90,14 +97,20 @@ void station_init_test_case(test_t t)
test_begin(t, "check pipe names")
{
/* check pipe names */
- sprintf(buffer, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid());
+#ifdef STATION_SOCK
+ sprintf(buffer, "%s/%s_sock_%d", STATION_SOCK_PATH, STATION_SOCK_PREFIX, getpid());
+ test_fail_if(strcmp(buffer, station.sock_name));
+#else
+ sprintf(buffer, "%s/%s_in_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid());
test_fail_if(strcmp(buffer, station.pipe_in_name));
sprintf(buffer, "%s/%s_out_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid());
test_fail_if(strcmp(buffer, station.pipe_out_name));
+#endif
/*sprintf(buffer, "%s/%s_debug_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, getpid());
test_fail_if(strcmp(buffer, station->debug_out_name));*/
} test_end;
+#ifndef STATION_SOCK
test_begin(t, "check if pipes are created and open")
{
test_fail_unless(
@@ -110,7 +123,8 @@ void station_init_test_case(test_t t)
&& (fstat(station.pipe_out_fd, &my_stat) >= 0)
&& S_ISFIFO(my_stat.st_mode));
} test_end;
-
+#endif
+
station_down(&station);
}
@@ -118,7 +132,9 @@ void station_down_test_case(test_t t)
{
station_ctx_t station;
int fd_in, fd_out;
+#ifndef STATION_SOCK
char name_in[256], name_out[256];
+#endif
struct stat my_stat;
test_case_begin(t, "down");
@@ -130,12 +146,25 @@ void station_down_test_case(test_t t)
} test_end;
station_init(&station);
+#ifdef STATION_SOCK
+ fd_in = station.sock_fd;
+ fd_out = station.sock_pair_fd;
+#else
fd_in = station.pipe_in_fd;
fd_out = station.pipe_out_fd;
strcpy(name_in, station.pipe_in_name);
strcpy(name_out, station.pipe_out_name);
+#endif
station_down(&station);
-
+
+#ifdef STATION_SOCK
+ test_begin(t, "socket is closed")
+ {
+ test_fail_unless(
+ (fstat(fd_in, &my_stat) < 0)
+ && (fstat(fd_out, &my_stat) < 0));
+ } test_end;
+#else
test_begin(t, "pipes are closed")
{
test_fail_unless(
@@ -149,6 +178,7 @@ void station_down_test_case(test_t t)
(stat(name_in, &my_stat) < 0)
&& (stat(name_out, &my_stat) < 0));
} test_end;
+#endif
}
void station_idle_test_case(test_t t)
@@ -173,9 +203,15 @@ void station_idle_test_case(test_t t)
test_begin(t, "recv idle msg")
{
+#ifdef STATION_SOCK
+ test_fail_unless(
+ ((fd_in = station.sock_pair_fd) >= 0)
+ );
+#else
test_fail_if(
((fd_in = open(station.pipe_out_name, O_RDONLY)) < 0)
);
+#endif
test_fail_unless(
(station_idle(&station) >= 0)
&& ((len = read(fd_in, buffer, sizeof(sci_msg_hdr_t))) == sizeof(sci_msg_hdr_t))