summaryrefslogtreecommitdiff
path: root/cesar/host/station/src/station.c
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/host/station/src/station.c')
-rw-r--r--cesar/host/station/src/station.c366
1 files changed, 89 insertions, 277 deletions
diff --git a/cesar/host/station/src/station.c b/cesar/host/station/src/station.c
index 5659c31510..16bc1b67e0 100644
--- a/cesar/host/station/src/station.c
+++ b/cesar/host/station/src/station.c
@@ -5,7 +5,7 @@
* <<<Licence>>>
*
* }}} */
-
+
/**
* \file station.c
* \brief The station management functions
@@ -13,28 +13,16 @@
*
* This file provide station management functions
*
- * \todo
+ * \todo
*/
-
#include "common/std.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <errno.h>
#include "ecos/packages/hal/maximus/arch/current/include/hal_host_intr.h"
#include "host/sci/sci.h"
#include "host/station/station.h"
-#ifndef UNIT_TEST
-#include "host/sci/maximus/inc/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
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
#define STATION_BUFFER_SIZE (STATION_MAX_LOG_SIZE + 2)
@@ -45,7 +33,7 @@ static fcall_ctx_t _my_fcall;
static probe_ctx_t _my_probe;
static system_ctx_t _my_system;
static netclock_callback_t _ecos_tick_cb;
-
+
/**
* fill buffer with the in/out pipe name for sci msg communication
* \param suffix pipe name suffix; normaly "in" or "out"
@@ -70,133 +58,26 @@ 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)
{
errno = EINVAL;
return -1;
}
-
+
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;
+
+ /* use standard stdin(0), stdout(1), and stderr(2). */
+ station->fd_in = 0;
+ station->fd_out = 1;
+ station->fd_log = 2;
/* get station id */
station->id = getpid();
-
+
/* set variables */
station->log_level = STATION_LOG_WARNING;
- /* 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); // in case of an error occurs after, unlink now
-#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); // in case of an error occurs after, unlink now
- sprintf(station->pipe_out_name, "%s/%s_out_%d", STATION_PIPE_PATH, STATION_PIPE_PREFIX, station->id);
- unlink(station->pipe_out_name); // in case of an error occurs after, unlink now
-#endif
- // reset errno generated by the 'unlink() function'
- errno = 0;
-
- /* 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)
- {
- station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
- "%s: failed to create fifo '%s' (errno=%d)",
- __FUNCTION__, station->pipe_in_name, errno);
- goto failed;
- }
- if((station->pipe_in_fd = open(station->pipe_in_name, O_RDWR, S_IRWXU | S_IRWXG)) < 0)
- {
- station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
- "%s: failed to open fifo '%s' (errno=%d)",
- __FUNCTION__, station->pipe_in_name, errno);
- goto failed;
- }
-
- /* open out pipe */
- if(mknod(station->pipe_out_name, 0770 | S_IFIFO, 0) < 0)
- {
- station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
- "%s: failed to create fifo '%s' (errno=%d)",
- __FUNCTION__, station->pipe_out_name, errno);
- goto failed;
- }
- if((station->pipe_out_fd = open(station->pipe_out_name, O_RDWR, S_IRWXU | S_IRWXG)) < 0)
- {
- station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION,
- "%s: failed to open fifo '%s' (errno=%d)",
- __FUNCTION__, station->pipe_out_name, errno);
- goto failed;
- }
-
-#endif /* STATION_SOCK */
-
/* init all contexts */
station->sci = &_my_sci;
sci_init(station->sci, station);
@@ -210,88 +91,33 @@ int station_init(station_ctx_t *station)
system_init(station->system, station->sci);
station->ecos_tick_cb = &_ecos_tick_cb;
station->status = STATION_STATUS_RUNNING;
-
+
/* 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 */
-
return 0;
-
-failed:
- 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);
- unlink(station->pipe_out_name);
- unlink(station->pipe_in_name);
- station->pipe_out_fd = -1;
- station->pipe_in_fd = -1;
-#endif /* STATION_SOCK */
- station->status = STATION_STATUS_ERROR;
- return -1;
-}
+}
-/**
- * station context to clean and reset, with pipe closing
- * \param station pointer to station context to stop
- */
-void station_down(station_ctx_t *station)
+void
+station_uninit (station_ctx_t *station)
{
- if((station == NULL)
- || (station->status == STATION_STATUS_INIT))
- return;
+ DBG_ASSERT(station);
-#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);
- unlink(station->pipe_out_name);
- unlink(station->pipe_in_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;
-}
+ /* Return to first state. */
+ station->status = STATION_STATUS_INIT;
+
+ /* Unload modules. */
+ station->ecos_tick_cb = NULL;
+ /* TODO: system_uninit (station->system); */
+ station->system = NULL;
+ /* TODO: probe_uninit (station->probe); */
+ station->probe = NULL;
+ /* TODO: fcall_uninit (station->fcall); */
+ station->fcall = NULL;
+ /* TODO: netclock_uninit (station->netclock); */
+ station->netclock = NULL;
+ /* TODO: sci_uninit (station->sci); */
+ station->sci = NULL;
+}
/**
* station is into idle state; it sends a sci 'idle' message and waits for sci message reception
@@ -308,23 +134,22 @@ int station_idle(station_ctx_t *station)
fd_set read_fds;
struct timeval timeout;
int sel;
-
+
DBG_ASSERT(station);
DBG_ASSERT(station->status != STATION_STATUS_INIT);
if((station == NULL)
- || (station->status == STATION_STATUS_INIT))
- //|| !station_is_initialized(station))
+ || (station->status == STATION_STATUS_INIT))
{
errno = EINVAL;
return -1;
}
-
+
if(station->status == STATION_STATUS_RUNNING)
{
/* init msg */
if(sci_msg_init(&msg, buffer, 64) < 0)
return -1;
-
+
/* fill system header */
if (0 > sci_msg_push (&msg, sizeof (station_msg_hdr_t)))
return -1;
@@ -332,7 +157,7 @@ int station_idle(station_ctx_t *station)
msg.hdr.station->version = SYSTEM_VERSION;
msg.hdr.station->type = SYSTEM_TYPE_IDLE;
msg.hdr.station->flags = 0;
-
+
/* fill sci header and send msg */
if(sci_fill_hdr(station->sci, &msg, SCI_MSG_TYPE_SYSTEM, 0) < 0)
return -1;
@@ -341,21 +166,12 @@ int station_idle(station_ctx_t *station)
station_log(station, STATION_LOG_WARNING, STATION_LOGTYPE_STATION, "%s failed to send system idle (errno=%d)", __FUNCTION__, errno);
return -1;
}
- station->status = STATION_STATUS_IDLE;
+ station->status = STATION_STATUS_IDLE;
}
-
- /* don't wait for next message with unit tests */
-#ifdef UNIT_TEST
- return 0;
-#endif
-
- /* vait for next message with 1 second timeout */
+
+ /* wait 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 */
+ FD_SET (station->fd_in, &read_fds);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
sel = select(STATION_MAX_FD + 1, &read_fds, NULL, NULL, &timeout);
@@ -365,8 +181,8 @@ int station_idle(station_ctx_t *station)
return -1;
}
else if(sel > 0)
- {
- station->status = STATION_STATUS_RUNNING;
+ {
+ station->status = STATION_STATUS_RUNNING;
return sci_recv(station->sci);
}
else
@@ -380,13 +196,11 @@ int station_idle(station_ctx_t *station)
static void _ecos_set_itimer_cb(void *data)
{
uint32_t *isr;
-#ifndef UNIT_TEST
station_log(&my_station, STATION_LOG_DEBUG, STATION_LOGTYPE_STATION, "%s", __FUNCTION__);
-#endif /* UNIT_TEST */
- isr = (uint32_t *)data;
+ isr = (uint32_t *)data;
*isr |= (1 << CYGNUM_HAL_INTERRUPT_RTC);
return;
-}
+}
/**
* schedule an eCos tick event (every 10ms)
@@ -401,21 +215,21 @@ int station_ecos_set_itimer(station_ctx_t *station, tick_t tick)
DBG_ASSERT(station);
DBG_ASSERT(station->status != STATION_STATUS_INIT);
if((station == NULL)
- || (station->status == STATION_STATUS_INIT))
+ || (station->status == STATION_STATUS_INIT))
{
errno = EINVAL;
return -1;
}
-
- return netclock_schedule(station->netclock,
- station->ecos_tick_cb,
- NETWORK_CLOCK_TYPE_STATION,
- tick,
- _ecos_set_itimer_cb,
- (void *)&maximus_pending_isrs,
- &id);
-}
-
+
+ return netclock_schedule(station->netclock,
+ station->ecos_tick_cb,
+ NETWORK_CLOCK_TYPE_STATION,
+ tick,
+ _ecos_set_itimer_cb,
+ (void *)&maximus_pending_isrs,
+ &id);
+}
+
/**
* set the station log level
* \param station pointer to station context
@@ -430,14 +244,14 @@ int station_log_set_level(station_ctx_t *station, station_log_level_t level)
DBG_ASSERT(level > STATION_LOG_NONE);
DBG_ASSERT(level < STATION_LOG_NB);
if((station == NULL)
- || (station->status == STATION_STATUS_INIT)
- || (level <= STATION_LOG_NONE)
- || (level >= STATION_LOG_NB))
+ || (station->status == STATION_STATUS_INIT)
+ || (level <= STATION_LOG_NONE)
+ || (level >= STATION_LOG_NB))
{
errno = EINVAL;
return -1;
}
-
+
station->log_level = level;
return 0;
}
@@ -446,72 +260,70 @@ int station_log_set_level(station_ctx_t *station, station_log_level_t level)
* send message to station log system
* \param station pointer to station context
* \param level level of log message
- * \param format string structure describing the data log format
+ * \param format string structure describing the data log format
* \param ... suite of parameters to fit the format
*/
- void station_log(station_ctx_t *station,
- station_log_level_t level,
- unsigned long type,
- const char *format,
- ...)
+void station_log(station_ctx_t *station,
+ station_log_level_t level,
+ unsigned long type,
+ const char *format,
+ ...)
{
va_list va_args;
int hdr_len, ret = 0;
static char buffer[STATION_BUFFER_SIZE];
if((station == NULL)
- || (station->status == STATION_STATUS_INIT)
- || (format == NULL)
- || (station->system == NULL))
+ || (station->status == STATION_STATUS_INIT)
+ || (format == NULL)
+ || (station->system == NULL))
return;
- if ((level <= STATION_LOG_NONE)
+ if ((level <= STATION_LOG_NONE)
|| (level > station->log_level))
return;
-
- if (station->pipe_log_fd < 0)
- return;
-
+
if(type == 0)
type = STATION_LOGTYPE_ALL;
-
+
if(!(station->log_mask & type))
return;
-
+
/* build header and send it */
if(strlen(station->system->station_name) == 0)
{
sprintf(buffer, "sta(%d) %Lu: ",
- station->id,
- station->current_tick_tck
- );
+ station->id,
+ station->current_tick_tck
+ );
}
else
{
sprintf(buffer, "== %s ==(%d) %Lu: ",
- station->system->station_name,
- station->id,
- station->current_tick_tck
- );
+ station->system->station_name,
+ station->id,
+ station->current_tick_tck
+ );
}
if(level >= STATION_LOG_DEBUG)
{
strcat(buffer, "<DEBUG> ");
}
hdr_len = strlen(buffer);
- write(station->pipe_log_fd, buffer, hdr_len);
-
+ write (station->fd_log, buffer, hdr_len);
+
/* build body and send it */
buffer[STATION_BUFFER_SIZE - 1] = '\0';
va_start(va_args, format);
ret = vsnprintf (buffer, STATION_BUFFER_SIZE - 1, format, va_args);
va_end (va_args);
- write(station->pipe_log_fd, buffer, strlen(buffer));
+ write (station->fd_log, buffer, strlen(buffer));
/* send CR */
if (ret == STATION_MAX_LOG_SIZE)
- write (station->pipe_log_fd, STATION_MAX_LOG_MSG, strlen (STATION_MAX_LOG_MSG));
- write(station->pipe_log_fd, "\n", 1);
+ write (station->fd_log, STATION_MAX_LOG_MSG,
+ strlen (STATION_MAX_LOG_MSG));
+ write (station->fd_log, "\n", 1);
return;
-}
+}