summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcd/src/plcd_main.c
blob: d1f71fb238fbe1ee585599ba1da8459f3496ca81 (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
/* 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"

/* 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 help for plcd usage.
 *
 * \param  cmd  command
 */
static void
plcd_print_usage (const char *cmd)
{
    fprintf (stderr, "Usage : %s " \
             "[ -c --conf_file hpav_conf_file ]" \
             "[ -i --info_file hpav_info_file ]" \
             "[ -v --version show version number ]\n",
             cmd);
}

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

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

/**
 * Un-initialize PLC daemon.
 *
 * \param  ctx  plcd context
 */
static void
plcd_uninit (plcd_ctx_t *ctx)
{
    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)
{
    int c = 0, opt_index = 0, init_nb = 0, init_ret = -1;

    /* main context */
    plcd_ctx_t ctx;

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

    /* init context */
    if (0 > plcd_init (&ctx))
    {
        exit (EXIT_FAILURE);
    }

    /* show PLCD version */
    syslog (LOG_NOTICE, "PLC Daemon (%s) Running\n", VERSION);

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

    /* process options */
    while (-1 != (c = getopt_long_only (argc, argv, "c:i:v:", long_opts,
                                        &opt_index)))
    {
        switch(c)
        {
            case 'c':
                /* HPAV config file */
                ctx.hpav_conf_path = optarg;
                break;
            case 'i':
                /* HPAV info file */
                ctx.hpav_info_path = optarg;
                break;
            case 'v':
                /* version number */
                fprintf (stdout, "%s\n", VERSION);
                exit (EXIT_SUCCESS);
            default:
                plcd_print_usage (argv[0]);
                exit (EXIT_SUCCESS);
        }
    }

    /* 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);
    plcd_uninit (&ctx);
    exit (EXIT_SUCCESS);
}