summaryrefslogtreecommitdiff
path: root/cesar/host/src/station.c
blob: 4b2d684c63f430e12e1f7adbe1a07d21422312c3 (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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/* 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 "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.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 */
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 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"
 * \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)
{
#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;

    /* 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 | O_NONBLOCK, 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 | O_NONBLOCK, 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);
    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->fcall);
    station->system = &_my_system;
    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)
{
    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);
    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;
}    

/**
 * 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;
    
    DBG_ASSERT(station);
    DBG_ASSERT(station->status != STATION_STATUS_INIT);
    if((station == NULL)
        || (station->status == STATION_STATUS_INIT))
        //|| !station_is_initialized(station))
    {
        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(sci_msg_push(&msg, sizeof(station_msg_hdr_t)) < (int)sizeof(station_msg_hdr_t))
            return -1;
        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 */
        if(sci_fill_hdr(station->sci, &msg, SCI_MSG_TYPE_SYSTEM, 0) < 0)
            return -1;
        if(sci_send(station->sci, &msg) < 0)
        {
            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; 
    }
    
    /* don't wait for next message with unit tests */
#ifdef UNIT_TEST
    return 0;
#endif
    
    /* 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_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);
        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;
#ifndef UNIT_TEST
    station_log(&my_station, STATION_LOG_DEBUG, STATION_LOGTYPE_STATION, "%s", __FUNCTION__);
#endif /* UNIT_TEST */ 
    isr = (uint32_t *)data; 
    *isr |= (1 << 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)
{
    netclock_id_t id;
    DBG_ASSERT(station);
    DBG_ASSERT(station->status != STATION_STATUS_INIT);
    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,
        &id);
} 
 
/**
 * 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)
{
    DBG_ASSERT(station);
    DBG_ASSERT(station->status != STATION_STATUS_INIT);
    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))
    {
        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,
    unsigned long type, 
    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)
        || (station->system == NULL))
        return;

    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
        );
    }
    else
    {
        sprintf(buffer, "== %s ==(%d) %Lu: ",
            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);
    
    /* 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;
}