summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/managerd.c
blob: ed912618b65b9853febed12a48cb0539ec66a45e (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
/*
 * cleopatre/application/managerd/src/managerd.c
 *
 * (C) Copyright 2009 SPiDCOM Technologies
     *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
     *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <assert.h>

#include "bridge.h"
#include "mme_nl.h"
#include "gpio_event.h"
#include "managerd.h"
#include "libspid.h"

/* Global variable indicating if a SIGHUP signal occurred,
 * has been caught in managerd signal handler,
 * and has now to be processed. */
volatile sig_atomic_t is_process_signal_needed;

/* Global variable indicating if a SIGTERM signal occurred, should exit. */
volatile sig_atomic_t exit_requested;

/**
 * Handle signal reception.
 *
 * \param  signal_nb  signal identifier
 */
void
managerd_signal_handler (int signal_nb)
{
    /* Check received signal. */
    if (SIGHUP == signal_nb)
    {
        is_process_signal_needed = 1;
    }
    else if (SIGTERM == signal_nb)
    {
        exit_requested = 1;
    }
}

/**
 * This function is called after reception of a SIGHUP by managerd signal handler.
 * Check which information has changed in hpav.info file,
 * and depending on this information, call the appropriate processing function.
 *
 * \param  ctx managerd context
 * \return -1 on error, 0 otherwise
 */
int
managerd_process_signal (struct managerd_ctx *ctx)
{
    /* hpav.info */
    libspid_hpav_info_t hpav_info;
    memset (&hpav_info, 0, sizeof (libspid_hpav_info_t));

    assert (NULL != ctx);

    /* read hpav.info file contents */
    if (libspid_hpav_info_read_file (&hpav_info) != LIBSPID_SUCCESS)
    {
        syslog (LOG_WARNING, "libspid config read item failed");
        return -1;
    }

    if (strcmp (hpav_info.status, ctx->hpav_info.status)
        || (hpav_info.is_sc != ctx->hpav_info.is_sc))
    {
        /* attachment */
        if (0 > led_attachment_event (ctx, hpav_info.status, hpav_info.is_sc))
        {
            syslog (LOG_WARNING, "led attachment event failed");
            return -1;
        }

        /* save values in managerd context */
        strcpy (ctx->hpav_info.status, hpav_info.status);
        ctx->hpav_info.is_sc = hpav_info.is_sc;
    }
    if (strcmp (hpav_info.cco, ctx->hpav_info.cco))
    {
        /* LED1 */
        if (0 > led1_event (ctx, hpav_info.cco))
        {
            syslog (LOG_WARNING, "led1 event failed");
            return -1;
        }

        /* save value in managerd context */
        strcpy (ctx->hpav_info.cco, hpav_info.cco);
    }
    if (hpav_info.is_backup_cco != ctx->hpav_info.is_backup_cco)
    {
        /* LED2 */
        if (0 > led2_event (ctx, hpav_info.is_backup_cco))
        {
            syslog (LOG_WARNING, "led2 event failed");
            return -1;
        }

        /* save value in managerd context */
        ctx->hpav_info.is_backup_cco = hpav_info.is_backup_cco;
    }

    return 0;
}

/**
 * Daemon core process.
 * Wait frame on BR and MME netlink, processing it before
 * sending it to BR or MME.
 * On each RX timeout, process other events.
 *
 * \param  ctx  managerd context.
 * \return  error code.
 */
static int
managerd_process (struct managerd_ctx *ctx)
{
    uint8_t *buffer;
    int result = 0;
    int len;

    //Check arguments
    assert (ctx != NULL);

    //Allocate buffer for bridge part
    buffer = (uint8_t*) malloc (MAX_PKT_LEN);
    if (NULL == buffer)
    {
        syslog (LOG_WARNING, "cannot allocate memory (%s)", strerror (errno));
        return -1;
    }

    while(!exit_requested)
    {
        fd_set readfds, exceptfds;
        struct timeval timeout;
        int result;

        //Configure timeout
        timeout.tv_sec = WAIT_TOUT;
        timeout.tv_usec = 0;
        FD_ZERO (&readfds);
        FD_ZERO (&exceptfds);
        FD_SET (ctx->sock_br, &readfds);
        FD_SET (ctx->sock_lo, &readfds);
        FD_SET (ctx->sock_mme, &readfds);
        FD_SET (ctx->gpio_fd, &exceptfds);
        if (ctx->is_rx_if_plc)
        {
            FD_SET (ctx->sock_plc, &readfds);
        }
        //Select
        result = select (ctx->gpio_fd + 1, &readfds, NULL, &exceptfds, &timeout);

        /* Select error */
        if (0 > result)
        {
            /* look for interrupt */
            if (EINTR == errno)
            {
                /* reset errno */
                errno = 0;
                /* process interrupt */
            }
            else
            {
                syslog (LOG_WARNING, "select failed (%s)", strerror (errno));
                return -1;
            }
        }
        else if (0 == result)
        {
            /* No reception coming check rfs button*/
            if (restore_factory_settings_event (ctx) < 0)
                break;
            else if ((ctx->autoconf.check_timer != NULL) && (ctx->autoconf.check_timer (&ctx->autoconf) < 0))
                break;
            else
               continue;
        }
        else if (FD_ISSET (ctx->sock_br, &readfds)
                 || FD_ISSET (ctx->sock_lo, &readfds)
                 || ((ctx->is_rx_if_plc) && FD_ISSET (ctx->sock_plc, &readfds)))
        {
            if (FD_ISSET (ctx->sock_br, &readfds))
            {
                ctx->rx_if = MANAGERD_IF_BR;
            }
            else if (FD_ISSET (ctx->sock_lo, &readfds))
            {
                ctx->rx_if = MANAGERD_IF_LO;
            }
            else if ((ctx->is_rx_if_plc) && FD_ISSET (ctx->sock_plc, &readfds))
            {
                ctx->rx_if = MANAGERD_IF_PLC;
            }
            //Receive a frame from bridge: process it
            len = bridge_receive (ctx, buffer, MAX_PKT_LEN);
            if (0 >= len)
            {
                result = -1;
                continue;
            }

            //Processing this reception
            if (TO_SEND == bridge_processing (ctx, buffer, &len, MAX_PKT_LEN))
            {
                //Send the process result to bridge interface
                len = bridge_send (ctx, buffer, len);
                if (0 > len)
                {
                    result = -1;
                    break;
                }
            }
        }
        else if FD_ISSET (ctx->sock_mme, &readfds)
        {
            //Receive a frame from MME if
            len = mme_nl_receive (ctx, buffer, MAX_PKT_LEN);
            if (0 >= len)
            {
                result = -1;
                break;
            }

            /* Processing this reception */
            if (TO_SEND == mme_nl_processing (ctx, buffer, &len, MAX_PKT_LEN))
            {
                /* Relay it to the bridge */
                len = bridge_send (ctx, buffer, len);
                if (0 > len)
                {
                    result = -1;
                    break;
                }
            }
        }
        else if (FD_ISSET (ctx->gpio_fd, &exceptfds))
        {
            /* Manage simple connect */
            if (simple_connect_event (ctx) < 0)
                break;
            else
                continue;
        }

        if (is_process_signal_needed)
        {
            is_process_signal_needed = 0;

            /* now handle all info file changes (from plcd) */
            managerd_process_signal (ctx);
        }
    }

    //Freeing buffer
    free (buffer);
    return result;
}

/**
 * Initialize managerd and prepare context.
 *
 * \param  ctx  managerd context.
 * \return  error code.
 */
static int
managerd_init (struct managerd_ctx *ctx)
{
    //Check arguments
    assert (ctx != NULL);

    //Set context
    memset (ctx, '\0', sizeof (struct managerd_ctx));

    //Initialize bridge part
    if (0 > bridge_init (ctx))
        return -1;

    //Initialize mme part
    if (0 > mme_nl_init (ctx))
    {
        bridge_uninit (ctx);
        if (!ctx->is_rx_if_plc)
            close (ctx->sock_plc);
        return -1;
    }

    //Initialize simple connect part (open "/dev/gpio")
    if (0 > simple_connect_init (ctx))
    {
        bridge_uninit (ctx);
        mme_nl_uninit (ctx);
        return -1;
    }

    /* Initialize led part (need "/dev/gpio") */
    if (0 > led_init (ctx))
    {
        bridge_uninit (ctx);
        mme_nl_uninit (ctx);
        simple_connect_uninit (ctx);
        return -1;
    }

    /* Initialize rfs part (need "/dev/gpio") */
    if (0 > restore_factory_settings_init (ctx))
    {
        bridge_uninit (ctx);
        mme_nl_uninit (ctx);
        led_uninit (ctx);
        simple_connect_uninit (ctx);
        return -1;
    }

    /* Set default values of hpav.info file into managerd context */
    strcpy (ctx->hpav_info.status, LIBSPID_HPAV_INFO_VALUE_STATUS_UNASSOCIATED);
    strcpy (ctx->hpav_info.cco, LIBSPID_HPAV_INFO_VALUE_CCO_STATION);
    ctx->hpav_info.is_backup_cco = LIBSPID_FALSE;
    ctx->hpav_info.is_sc = LIBSPID_FALSE;
    ctx->hpav_info.is_sc_button = LIBSPID_FALSE;

    /* Initialize to 1 to set LED-s status
     * according to current hpav.info contents. */
    is_process_signal_needed = 1;

    /* set jiangsu send flag and timeout  to zero */
    ctx->js_eoc_mme_get_info_recv = 0;
    ctx->js_eoc_mme_diagnostic_info_recv = 0;

    /* Register to hpav.info file update */
    if (LIBSPID_SUCCESS != libspid_system_file_update_register (getpid(),
        LIBSPID_HPAV_INFO_PATH, managerd_signal_handler))
    {
        syslog (LOG_WARNING, "libspid system file update register failed");
        bridge_uninit (ctx);
        mme_nl_uninit (ctx);
        restore_factory_settings_uninit (ctx);
        led_uninit (ctx);
        simple_connect_uninit (ctx);
        return -1;
    }

    /* Catch SIGTERM. */
    signal (SIGTERM, managerd_signal_handler);

    return 0;
}

/**
 * Uninitialize manager daemon.
 *
 * \param  ctx  managerd context.
 */
static void
managerd_uninit (struct managerd_ctx *ctx)
{
    //Check arguments
    assert (ctx != NULL);

    //Uninitialize bridge part
    bridge_uninit (ctx);

    //Uninitialize mme part
    mme_nl_uninit (ctx);

    /* Uninitialize rfs part (need "/dev/gpio") */
    restore_factory_settings_uninit (ctx);

    /* Uninitialize led part (need "/dev/gpio") */
    led_uninit (ctx);

    //Uninitialize simple connect part (close "/dev/gpio")
    simple_connect_uninit (ctx);

    /* Unregister to file update. */
    libspid_system_file_update_unregister (getpid (), LIBSPID_HPAV_INFO_PATH);
}

/**
 * Main function
 *
 * \param  argc  number of arguments.
 * \param  argv  arguments pointer.
 * \return  error code.
 */
int
main (int argc, char **argv)
{
    struct managerd_ctx ctx;
    int result;

    //Open syslog file
    openlog ("managerd", LOG_PID, LOG_DAEMON);

    //Initialize manager daemon
    if (0 != managerd_init (&ctx))
    {
        closelog ();
        return -1;
    }

    syslog (LOG_NOTICE, "Manager Daemon Running");

    // send reset info (VS_MME_RESET.IND)
    vs_mme_reset_ind ();

    //Start manager daemon core
    result = managerd_process (&ctx);
    syslog (LOG_NOTICE, "Manager Daemon exiting");

    //Uninitialize manager daemon
    managerd_uninit (&ctx);

    //Open syslog file
    closelog ();

    return result;
}