summaryrefslogtreecommitdiff
path: root/host/src/station.c
blob: 9b38f40488fa9793776fd94fb8569aef17d52ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Cesar project {{{
 *
 * Copyright (C) 2007 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
 
/**
 * \file    station.c
 * \brief   The station management functions
 * \ingroup host
 *
 * This file provide station management functions
 *
 * \todo   
 */

#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "ecos/packages/hal/maximus/arch/current/include/hal_host_intr.h"
#include "host/inc/sci.h"
#include "host/inc/station.h"
#ifndef UNIT_TEST
#include "host/inc/syscall.h"
#endif

/* awfull, but needed */
static sci_ctx_t _my_sci;
static netclock_ctx_t _my_netclock;
static fcall_ctx_t _my_fcall;
static probe_ctx_t _my_probe;
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"
 * \param buffer string buffer to fill in
 * \param max_size max size of buffer to be filled
 * \return the length of pipe name, -1 if failed with errno
 * - EINVAL if suffix or buffer are NULL
 * - ENOSPC if buffer is too small for pipe name
 */
//static int _get_pipe_name(char *suffix, unsigned short id, char *buffer, int max_size)
//{
//    /* code here */
//    return 0;
//}

/**
 * station context initialization, with pipe opening
 * \param station pointer to station context to initialize
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if station is NULL
 * - all errno generated by open() call
 */
int station_init(station_ctx_t *station)
{
    if(station == NULL)
    {
        errno = EINVAL;
        return -1;
    }
    
    memset(station, '\0', sizeof(station_ctx_t));
    station->pipe_in_fd = -1;
    station->pipe_out_fd = -1;
    station->pipe_log_fd = -1;

    /* 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);
    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);

    /* 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;
        
    /* open in pipe */
    if(mknod(station->pipe_in_name, 0770 | S_IFIFO, 0) < 0)
    {
        station_log(station, STATION_LOG_WARNING, 
            "%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 | O_NONBLOCK, S_IRWXU | S_IRWXG)) < 0)
    {
        station_log(station, STATION_LOG_WARNING, 
            "%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, 
            "%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 | O_NONBLOCK, S_IRWXU | S_IRWXG)) < 0)
    {
        station_log(station, STATION_LOG_WARNING, 
            "%s: failed to open fifo '%s' (errno=%d)", 
            __FUNCTION__, station->pipe_out_name, errno);        
        goto failed;
    }
    
    /* init all contexts */
    station->sci = &_my_sci;
    sci_init(station->sci, station);
    station->netclock = &_my_netclock;
    netclock_init(station->netclock, station->sci);
    station->fcall = &_my_fcall;
    fcall_init(station->fcall, station->sci);
    station->probe = &_my_probe;
    //probe_init(station->probe, station);
    station->ecos_tick_cb = &_ecos_tick_cb;
    station->status = STATION_STATUS_RUNNING;
    
#ifndef UNIT_TEST
    /* first call for tick */
    station_ecos_set_itimer(station, TICK_HZ / 100);
#endif /* UNIT_TEST */
    
    return 0;
    
failed:
    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;
    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)
{
    if((station == NULL)
        || (station->status != STATION_STATUS_INIT))
        return;
        
    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;
    return;
}    

/**
 * station is into idle state; it sends a sci 'idle' message and waits for sci message reception
 * \param station the current station context
 * \return 0 when message has been received and process<br>
 * -1 if an error happened with errno=<br>
 * <ul><li>EINVAL: station is invalid<br>
 * <li>all error codes from recv() call</ul>
 */
int station_idle(station_ctx_t *station)
{
    unsigned char buffer[64];
    sci_msg_t msg;
    fd_set read_fds;
    struct timeval timeout;
    int sel;
    
    if((station == NULL)
        || (station->status == STATION_STATUS_INIT))
        //|| !station_is_initialized(station))
    {
        errno = EINVAL;
        return -1;
    }
    
    if(station->status == STATION_STATUS_RUNNING)
    {
        /* init msg */
        sci_msg_init(&msg, buffer, 64);
    
        /* fill system header */
        sci_msg_push(&msg, sizeof(station_msg_hdr_t));
        msg.hdr.station = (station_msg_hdr_t *)msg.data_begin;
        msg.hdr.station->version = SYSTEM_VERSION;
        msg.hdr.station->type = SYSTEM_TYPE_IDLE;
        msg.hdr.station->flags = 0;
    
        /* fill sci header and send msg */
        sci_fill_hdr(station->sci, &msg, SCI_MSG_TYPE_SYSTEM, 0);
        if(sci_send(station->sci, &msg) < 0)
        {
            station_log(station, STATION_LOG_WARNING, "%s failed to send system idle (errno=%d)", __FUNCTION__, errno);
            return -1;
        }
        station->status = STATION_STATUS_IDLE; 
    }
    
    /* don't wait for next message with unit tests */
    if(station->is_unit_test)
        return 0;
    
    /* vait for next message with 1 second timeout */
    FD_ZERO(&read_fds);
    FD_SET(station->pipe_in_fd, &read_fds);
    timeout.tv_sec = 1;
    timeout.tv_usec = 0;
    sel = select(station->pipe_in_fd + 1, &read_fds, NULL, NULL, &timeout);
    if(sel < 0)
    {
        station_log(station, STATION_LOG_WARNING, "%s select failed (errno=%d)", __FUNCTION__, errno);
        return -1;
    }
    else if(sel > 0)
    {    
        station->status = STATION_STATUS_RUNNING;        
        return sci_recv(station->sci);
    }
    else
        /* timeout */
        return 0;
}

/**
 * callback function to set the eCos timer interrupt register
 */
static void _ecos_set_itimer_cb(void *data)
{
    uint32_t *isr;
    isr = (uint32_t *)data; 
    *isr |= CYGNUM_HAL_INTERRUPT_RTC;
    return;
} 

/**
 * schedule an eCos tick event (every 10ms)
 * \param station pointer to station context
 * \param tick tick for event scheduling
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if station id NULL or if wanted tick is to old
 */
int station_ecos_set_itimer(station_ctx_t *station, tick_t tick)
{
    if((station == NULL)
        || (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);
} 
 
/**
 * set the station log level
 * \param station pointer to station context
 * \param level new log level
 * \return 0 if ok, -1 if failed with errno=
 * - EINVAL if station is NULL or level is out of range
 */
int station_log_set_level(station_ctx_t *station, station_log_level_t level)
{
    if((station == NULL)
        || (station->status == STATION_STATUS_INIT)
        || (level <= STATION_LOG_NONE)
        || (level >= STATION_LOG_NB))
    {
        errno = EINVAL;
        return -1;
    }
    
    station->log_level = level;
    return 0;
}

/**
 * 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 ... suite of parameters to fit the format
 */
 void station_log(station_ctx_t *station, 
    station_log_level_t level, 
    const char *format,
    ...)
{
    va_list va_args;
    int hdr_len;
    static char buffer[STATION_MAX_LOG_SIZE];

    if((station == NULL)
        || (station->status == STATION_STATUS_INIT)
        || (format == NULL))
        return;

    if ((level <= STATION_LOG_NONE) 
        || (level > station->log_level))
        return;
        
    if (station->pipe_log_fd < 0)
        return;
    
    /* build header and send it */
    sprintf(buffer, "sta(%d) %Lu: %s",
        station->id,
        station->current_tick_tck,
        (level >= STATION_LOG_DEBUG ? "DEBUG ": "")
    );
    hdr_len = strlen(buffer);
    write(station->pipe_log_fd, buffer, hdr_len);
    
    /* build body and send it */
    buffer[STATION_MAX_LOG_SIZE - 1] = '\0';
    va_start(va_args, format);
    vsnprintf(buffer, STATION_MAX_LOG_SIZE - 1, format, va_args);
    va_end (va_args);
    write(station->pipe_log_fd, buffer, strlen(buffer));

    /* send CR */
    write(station->pipe_log_fd, "\n", 1);

    return;
}