summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcd/src/plcd_main.c
blob: 5dadd26448a4debc131f7cd0fb522ff67809b1c8 (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
/* SPC300 bundle {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    devkit/plcd/src/plcd_main.c
 * \brief   PLC daemon for AV / EoC stack management
 * \ingroup plcd
 *
 * This PLC daemon is responsible for the initialization of
 * the AV / EoC stack: start, parameter sending, status watching.
 * It is also responsible for any configuration change from
 * the ethernet / plc sides through MME-s.
 *
 */
#include "plcd_main.h"
#include "plcd_stack.h"
#include "plcd_autoswitch.h"
#include "plcd_ctx.h"

#include "libspid.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h> /* for getpid() */
#include <net/if.h>
#include <sys/ioctl.h>
#include <errno.h>

#include "ioctl.h"
#include "nvram_utils.h"

/* "utest_override" include must be the last include. */
#include "utest_override.h"

typedef void (*plcd_main_task_fct_t)(plcd_ctx_t *ctx);

/* Interface name */
#define PLC_IFNAME  "plc0"

static plcd_nvram_t g_nvram;

volatile sig_atomic_t is_process_signal_needed;

volatile sig_atomic_t exit_requested;

static int
nvram_init (plcd_nvram_t *nvram)
{
    int status = libspid_get_plc_address (&(nvram->plc_address), 6);
    status |= libspid_get_device_password (&(nvram->device_password), 32);
    status |= libspid_get_product_name (&(nvram->product_name), 64);
    status |= libspid_get_tonemask (&(nvram->tonemask), 192);

    return status;
}

/**
 * Print plcd version.
 *
 * \param  ctx  plc daemon context.
 */
static void
plcd_main_print_version (plcd_ctx_t *ctx)
{
    fprintf (stdout, "%s\n", VERSION);
}

/**
 * Print help for plcd usage.
 *
 * \param  ctx  plc daemon context.
 */
static void
plcd_main_print_usage (plcd_ctx_t *ctx)
{
    PLCD_ASSERT (ctx);

    fprintf (stderr, "Usage: %s [options]\n"
             "\n"
             "Options:\n"
             "  -c, --conf-file     set config file location\n"
             "  -i, --info-file     set info file location\n"
             "  -v, --version       show program version\n",
             ctx->my_name);
}

/**
 * Handle signals reception.
 *
 * \param  signal_nb  signal identifier
 */
static void
plcd_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;
    }
}

/**
 * Initialize PLC daemon.
 *
 * \param  ctx  plcd context
 * \return -1 on error, 0 otherwise
 */
static int
plcd_init (plcd_ctx_t *ctx)
{
    char is_cco_preferred_str[LIBSPID_BOOLEAN_STR_MAX_LEN] = {0};
    char was_cco_str[LIBSPID_BOOLEAN_STR_MAX_LEN] = {0};
    struct plcdrv_setpid user_data;
    struct ifreq ifr;
    pid_t pid = getpid ();

    PLCD_ASSERT (NULL != ctx);

    is_process_signal_needed = 0;
    memset (&user_data, 0, sizeof (struct plcdrv_setpid));
    memset (&ifr, 0, sizeof (struct ifreq));

    /* create an empty hpav.info file for writing default values */
    if ((LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_info_path,
        LIBSPID_HPAV_INFO_LABEL_STATUS, LIBSPID_HPAV_INFO_VALUE_STATUS_UNASSOCIATED))
        || (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_info_path,
        LIBSPID_HPAV_INFO_LABEL_CCO, LIBSPID_HPAV_INFO_VALUE_CCO_STATION))
        || (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_info_path,
        LIBSPID_HPAV_INFO_LABEL_BACKUP_CCO, LIBSPID_VALUE_BOOLEAN_FALSE))
        || (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_info_path,
        LIBSPID_HPAV_INFO_LABEL_SC, LIBSPID_VALUE_BOOLEAN_FALSE))
        || (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_info_path,
        LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, LIBSPID_VALUE_BOOLEAN_FALSE)))
    {
        syslog (LOG_WARNING, "libspid config write item failed");
        return -1;
    }

    /* save these values into PLCD 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;

    /* open netlink socket */
    if (0 > (ctx->plc_sock = socket (PF_NETLINK, SOCK_RAW, NETLINK_PLC_DRV)))
    {
        syslog (LOG_WARNING, "socket open failed");
        return -1;
    }
    memset (&ctx->plcd_addr, 0, sizeof (ctx->plcd_addr));
    ctx->plcd_addr.nl_family = AF_NETLINK;
    ctx->plcd_addr.nl_pid = pid; /* self pid */
    ctx->plcd_addr.nl_groups = 0; /* not in mcast group */
    if (0 > bind (ctx->plc_sock, (struct sockaddr *) &ctx->plcd_addr,
                  sizeof (ctx->plcd_addr)))
    {
        syslog (LOG_WARNING, "socket bind failed");
        return -1;
    }

    /* Set plcd pid for reception on drv netlink */
    user_data.nl = NETLINK_PLC_DRV;
    user_data.pid = pid;
    ifr.ifr_data = (void *) &user_data;
    strncpy (ifr.ifr_name, (char*) PLC_IFNAME, IFNAMSIZ);
    if (0 > (ioctl (ctx->plc_sock, PLCDRV_IOCTL_SETPID, &ifr)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETPID (%s)", strerror (errno));
    }

    ctx->nvram = &g_nvram;
    if (nvram_init(ctx->nvram))
    {
        syslog (LOG_WARNING, "libspid system get nvram failed");
        return -1;
    }

    /* read and save hpav.conf file contents */
    if ((LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED, is_cco_preferred_str,
        LIBSPID_BOOLEAN_STR_MAX_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_WAS_CCO, was_cco_str, LIBSPID_BOOLEAN_STR_MAX_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_NMK, ctx->hpav_conf.nmk_str,
        LIBSPID_HPAV_CONF_NMK_STR_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_NID, ctx->hpav_conf.nid_str,
        LIBSPID_HPAV_CONF_NID_STR_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_SL, ctx->hpav_conf.sl_str,
        LIBSPID_HPAV_CONF_SL_STR_MAX_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_USER_HFID, ctx->hpav_conf.user_hfid,
        LIBSPID_HPAV_CONF_HFID_MAX_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_AVLN_HFID, ctx->hpav_conf.avln_hfid,
        LIBSPID_HPAV_CONF_HFID_MAX_LEN)))
    {
        syslog (LOG_WARNING, "libspid config read item failed");
        return -1;
    }
    ctx->hpav_conf.is_cco_preferred = LIBSPID_GET_BOOLEAN (is_cco_preferred_str);
    ctx->hpav_conf.was_cco = LIBSPID_GET_BOOLEAN (was_cco_str);

    return 0;
}

static void
plcd_daemon_main (plcd_ctx_t *ctx)
{
    PLCD_ASSERT (ctx);

    int init_nb, init_ret = -1;

    if (0 <= plcd_init (ctx))
    {
        /* Show PLCD version. */
        syslog (LOG_NOTICE, "PLC Daemon (%s) Running\n",
                VERSION);

        /* initialize the AV / EoC stack
         * and loop (PLCD_INIT_RETRIES maximum) until successful init */
        for (init_nb = 0; init_nb < PLCD_INIT_RETRIES; init_nb++)
        {
            if (0 == (init_ret = plcd_stack_init (ctx)))
            {
                break;
            }
        }
        if (0 != init_ret)
        {
            syslog (LOG_WARNING, "stack init failed\n");
            exit (EXIT_FAILURE);
        }

        pid_t pid = getpid ();

        /* register to hpav.conf and hpav.info files update */
        if ((LIBSPID_SUCCESS != libspid_system_file_update_register (
                    pid, LIBSPID_HPAV_CONF_PATH, plcd_signal_handler))
            || (LIBSPID_SUCCESS != libspid_system_file_update_register (
                    pid, LIBSPID_HPAV_INFO_PATH, plcd_signal_handler)))
        {
            exit (EXIT_FAILURE);
        }

        /* register to mcast.info file update */
        if (libspid_system_file_update_register (
                pid, LIBSPID_MULTICAST_INFO_PATH, plcd_signal_handler)
            != LIBSPID_SUCCESS)
        {
            exit (EXIT_FAILURE);
        }

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

        /* Initialize autoswitch timer. */
        plcd_autoswitch_timer_init ();

        /* now get all events from plcdrv (i.e. DRV MME-s) */
        while (!exit_requested)
        {
            if (0 > plcd_stack_event_dispatch (ctx))
            {
                exit (EXIT_FAILURE);
            }
        }

        libspid_system_file_update_unregister (pid, LIBSPID_HPAV_CONF_PATH);
        libspid_system_file_update_unregister (pid, LIBSPID_HPAV_INFO_PATH);
        libspid_system_file_update_unregister (pid, LIBSPID_MULTICAST_INFO_PATH);
    }
}

/**
 * Initialize default variables in context.
 *
 * \param  ctx  plcd context
 */
static void
plcd_main_init (plcd_ctx_t *ctx, const char *name)
{
    PLCD_ASSERT (ctx);
    PLCD_ASSERT (name);

    memset (ctx, 0, sizeof (plcd_ctx_t));

    ctx->hpav_info_path = LIBSPID_HPAV_INFO_PATH;
    ctx->hpav_conf_path = LIBSPID_HPAV_CONF_PATH;
    ctx->phy_conf_path = LIBSPID_PHY_CONF_PATH;
    ctx->internal_conf_path = LIBSPID_INTERNAL_CONF_PATH;
    ctx->my_name = name;
}

/**
 * Un-initialize PLC daemon.
 *
 * \param  ctx  plcd context
 */
static void
plcd_main_uninit (plcd_ctx_t *ctx)
{
    PLCD_ASSERT (ctx);

    if (0 < ctx->plc_sock)
        close (ctx->plc_sock);
}

/**
 * Program entry.
 *
 * \param  argc  number of program arguments
 * \param  argv  table of (argc) program arguments
 * \return  EXIT_FAILURE on error, EXIT_SUCCESS otherwise
 */
int
main (int argc, char **argv)
{
    /* Main context. */
    static plcd_ctx_t plcd_ctx;

    /* Open log process. */
    openlog (argv[0], 0, LOG_DAEMON);

    plcd_main_init (&plcd_ctx, argv[0]);

    /* Input program arguments */
    static struct option long_opts[] = {
        {"conf_file", required_argument, NULL, 'c'},
        {"info_file", required_argument, NULL, 'i'},
        {"version", no_argument, NULL, 'v'},
        {NULL, 0, NULL, 0}
    };

    /* Parse options, and override default configuration.*/
    int opt, opt_index = 0;
    plcd_main_task_fct_t plcd_main_task_fct = &plcd_daemon_main;
    while (-1 != (opt = getopt_long (
                argc, argv, "c:i:v",
                long_opts, &opt_index)))
    {
        switch (opt)
        {
        case 'c':
            /* HPAV config file. */
            plcd_ctx.hpav_conf_path = optarg;
            break;

        case 'i':
            /* HPAV info file. */
            plcd_ctx.hpav_info_path = optarg;
            break;

        case 'v':
            plcd_main_task_fct = &plcd_main_print_version;
            break;

        default:
            plcd_main_task_fct = &plcd_main_print_usage;
            break;
        }

        if (plcd_main_task_fct != &plcd_daemon_main)
            break;
    }

    if (plcd_main_task_fct)
        plcd_main_task_fct (&plcd_ctx);
    else
        syslog (LOG_WARNING, "Undefined main task to do");

    plcd_main_uninit (&plcd_ctx);
    closelog ();
    return EXIT_SUCCESS;
}