summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/plcd/src/plcd_process.c
blob: 58fb7cdd2a88ebd2b2fc2faf2b0fb4cf8bffb32e (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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/* SPC300 bundle {{{
 *
 * Copyright (C) 2010 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    devkit/plcd/src/plcd_process.c
 * \brief   processing functions of PLC daemon
 * \ingroup plcd
 *
 * After starting the PLC firmware,
 * some events occur according to new status of AV / EoC stack,
 * or user actions.
 */
#include "plcd_process.h"
#include "plcd_stack.h"
#include "plcd_autoswitch.h"
#include "plcd_multicast.h"
#include "plcd_ctx.h"

#include "libmme.h"
#include "libspid.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <linux/if_ether.h>
#include <errno.h>
#include <arpa/inet.h>
#include <sys/time.h>

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

static char *
mme_drv_status_assoc_table[] =
{
    LIBSPID_HPAV_INFO_VALUE_STATUS_UNASSOCIATED,
    LIBSPID_HPAV_INFO_VALUE_STATUS_ASSOCIATED,
    LIBSPID_HPAV_INFO_VALUE_STATUS_AUTHENTICATED,
    NULL
};

static char *
mme_drv_status_cco_table[] =
{
    LIBSPID_HPAV_INFO_VALUE_CCO_STATION,
    LIBSPID_HPAV_INFO_VALUE_CCO_PROXY,
    LIBSPID_HPAV_INFO_VALUE_CCO_MAIN,
    NULL
};

static char *
mme_drv_key_sl_table[] =
{
    LIBSPID_HPAV_CONF_VALUE_SL_SC,
    LIBSPID_HPAV_CONF_VALUE_SL_HS,
    NULL
};

struct hpav_info_is_write_needed
{
    libspid_boolean_t status;
    libspid_boolean_t cco;
    libspid_boolean_t is_backup_cco;
    libspid_boolean_t is_sc;
    libspid_boolean_t is_sc_button;
};

static libspid_error_t
plcd_write_hpav_info (const libspid_hpav_info_t *new_values,
                      const struct hpav_info_is_write_needed *write,
                      unsigned int *out_count)
{
    const char *labels[LIBSPID_HPAV_INFO_ITEMS_COUNT] = {NULL};
    const char *values[LIBSPID_HPAV_INFO_ITEMS_COUNT] = {NULL};
    unsigned int count = 0;

    PLCD_ASSERT (NULL != new_values);
    PLCD_ASSERT (NULL != write);
    PLCD_ASSERT (NULL != out_count);

    if (write->status)
    {
        labels[count] = LIBSPID_HPAV_INFO_LABEL_STATUS;
        values[count] = new_values->status;
        count++;
    }
    if (write->cco)
    {
        labels[count] = LIBSPID_HPAV_INFO_LABEL_CCO;
        values[count] = new_values->cco;
        count++;
    }
    if (write->is_backup_cco)
    {
        labels[count] = LIBSPID_HPAV_INFO_LABEL_BACKUP_CCO;
        values[count] = (LIBSPID_TRUE == new_values->is_backup_cco ? \
            LIBSPID_VALUE_BOOLEAN_TRUE : LIBSPID_VALUE_BOOLEAN_FALSE);
        count++;
    }
    if (write->is_sc)
    {
        labels[count] = LIBSPID_HPAV_INFO_LABEL_SC;
        values[count] = (LIBSPID_TRUE == new_values->is_sc ? \
            LIBSPID_VALUE_BOOLEAN_TRUE : LIBSPID_VALUE_BOOLEAN_FALSE);
        count++;
    }
    if (write->is_sc_button)
    {
        labels[count] = LIBSPID_HPAV_INFO_LABEL_SC_BUTTON;
        values[count] = (LIBSPID_TRUE == new_values->is_sc_button ? \
            LIBSPID_VALUE_BOOLEAN_TRUE : LIBSPID_VALUE_BOOLEAN_FALSE);
        count++;
    }

    /* update number of items to write */
    *out_count = count;

    if (0 == count)
    {
        return LIBSPID_SUCCESS;
    }

    return libspid_config_write_items (LIBSPID_HPAV_INFO_PATH, labels, values,
                                       count);
}

int
plcd_stack_event_status (plcd_ctx_t *ctx, mme_ctx_t *indication_ctx)
{
    unsigned int len = 0;
    unsigned char rx_status = 0, rx_cco = 0, rx_is_cco_preferred = 0, \
        rx_is_backup_cco = 0, rx_is_sc = 0;
    char was_cco_str[LIBSPID_BOOLEAN_STR_MAX_LEN] = {0};
    libspid_hpav_info_t hpav_info_new_values;
    struct hpav_info_is_write_needed hpav_info_write =
    {
        .status = LIBSPID_FALSE,
        .cco = LIBSPID_FALSE,
        .is_backup_cco = LIBSPID_FALSE,
        .is_sc = LIBSPID_FALSE,
        .is_sc_button = LIBSPID_FALSE
    };
    unsigned int count = 0;
    libspid_error_t ret;
    struct itimerspec its;

    PLCD_ASSERT (NULL != ctx);
    PLCD_ASSERT (NULL != indication_ctx);
    PLCD_ASSERT ((DRV_STA_STATUS | MME_IND) == indication_ctx->mmtype);

    memcpy (&hpav_info_new_values, &ctx->hpav_info, sizeof (libspid_hpav_info_t));

    /* check if status needs to be updated in hpav.info
     * with received association status value */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_status, sizeof (rx_status),
                                 &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (MME_DRV_STATUS_ASSOC_NB > rx_status);
    if (strcmp (ctx->hpav_info.status, mme_drv_status_assoc_table[rx_status]))
    {
        /* save new status */
        strcpy (hpav_info_new_values.status, mme_drv_status_assoc_table[rx_status]);
        hpav_info_write.status = LIBSPID_TRUE;
        /* Check if we need to stop the autoswitch timer. */
        if (autoswitch_en
            && autoswitch_timer_alive
            && (rx_status == MME_DRV_STATUS_ASSOC_AUTHENTICATED
                || rx_status == MME_DRV_STATUS_ASSOC_ASSOCIATED))
        {
            its.it_value.tv_sec = 0;
            its.it_value.tv_nsec = 0;
            its.it_interval.tv_sec = 0;
            its.it_interval.tv_nsec = 0;
            timer_settime (autoswitch_timer, 0, &its, NULL);
            autoswitch_timer_alive = 0;

            /* Mark the image as working if necessary */
            plcd_autoswitch_image_update ();

        }
        /* Check if we need to rearm the autoswitch timer. */
        if (autoswitch_en
            && !autoswitch_timer_alive
            && rx_status == MME_DRV_STATUS_ASSOC_UNASSOCIATED)
        {
            its.it_value.tv_sec = PLCD_AUTOSWITCH_TIMEOUT_SEC;
            its.it_value.tv_nsec = 0;
            its.it_interval.tv_sec = 0;
            its.it_interval.tv_nsec = 0;
            timer_settime (autoswitch_timer, 0, &its, NULL);
            autoswitch_timer_alive = 1;
        }
    }

    /* check if cco needs to be updated in hpav.info
     * with received cco status value */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_cco, sizeof (rx_cco), &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (MME_DRV_STATUS_CCO_NB > rx_cco);
    if (strcmp (ctx->hpav_info.cco, mme_drv_status_cco_table[rx_cco]))
    {
        /* save new cco */
        strcpy (hpav_info_new_values.cco, mme_drv_status_cco_table[rx_cco]);
        hpav_info_write.cco = LIBSPID_TRUE;
        /* Check if we need to stop the autoswitch timer. */
        if (autoswitch_en
            && rx_cco == MME_DRV_STATUS_CCO_MAIN)
        {
            timer_delete (autoswitch_timer);
            /* We "force" autoswitch off to prevent further action on the
             * timer. */
            autoswitch_en = 0;
        }
    }

    /* check if WAS_CCO needs to be updated in hpav.conf
     * depending on received cco value */
    if ((MME_DRV_STATUS_ASSOC_AUTHENTICATED == rx_status)
        && (((MME_DRV_STATUS_CCO_MAIN == rx_cco) && !ctx->hpav_conf.was_cco)
            || ((rx_cco != MME_DRV_STATUS_CCO_MAIN) && ctx->hpav_conf.was_cco)))
    {
        strcpy (was_cco_str, MME_DRV_STATUS_CCO_MAIN == rx_cco ? \
                LIBSPID_VALUE_BOOLEAN_TRUE : LIBSPID_VALUE_BOOLEAN_FALSE);
        if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
            LIBSPID_HPAV_CONF_LABEL_WAS_CCO, was_cco_str))
        {
            syslog (LOG_WARNING, "libspid config write item failed");
            return -1;
        }
        /* save this value into PLCD context */
        ctx->hpav_conf.was_cco = LIBSPID_GET_BOOLEAN (was_cco_str);
        ctx->is_save_conf_needed = LIBSPID_TRUE;
    }

    /* check if cco preferred needs to be updated in hpav.conf
     * with received cco preferred value */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_is_cco_preferred,
                                 sizeof (rx_is_cco_preferred), &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (LIBSPID_BOOLEAN_NB > rx_is_cco_preferred);
    if (rx_is_cco_preferred != ctx->hpav_conf.is_cco_preferred)
    {
        if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
            LIBSPID_HPAV_CONF_LABEL_CCO_PREFERRED, LIBSPID_TRUE == rx_is_cco_preferred ? \
            LIBSPID_VALUE_BOOLEAN_TRUE : LIBSPID_VALUE_BOOLEAN_FALSE))
        {
            syslog (LOG_WARNING, "libspid config write item failed");
            return -1;
        }
        /* save this value into PLCD context */
        ctx->hpav_conf.is_cco_preferred = rx_is_cco_preferred;
        ctx->is_save_conf_needed = LIBSPID_TRUE;
    }

    /* check if backup cco needs to be updated in hpav.info
     * with received backup cco value */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_is_backup_cco,
                                 sizeof (rx_is_backup_cco), &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (LIBSPID_BOOLEAN_NB > rx_is_backup_cco);
    if (rx_is_backup_cco != ctx->hpav_info.is_backup_cco)
    {
        /* save is_backup_cco */
        hpav_info_new_values.is_backup_cco = rx_is_backup_cco;
        hpav_info_write.is_backup_cco = LIBSPID_TRUE;
    }

    /* check if SC has changed from "yes" to "no" in hpav.info */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_is_sc, sizeof (rx_is_sc),
                                 &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (LIBSPID_BOOLEAN_NB > rx_is_sc);
    if (rx_is_sc != ctx->hpav_info.is_sc)
    {
        /* must exit from SC state */
        /* save is_sc */
        hpav_info_new_values.is_sc = rx_is_sc;
        hpav_info_write.is_sc = LIBSPID_TRUE;

        if (!rx_is_sc)
        {
            /* must reset SC button */
            /* save is_sc_button */
            hpav_info_new_values.is_sc_button = LIBSPID_FALSE;
            hpav_info_write.is_sc_button = LIBSPID_TRUE;
        }
    }

    syslog (LOG_INFO, "stack event status: STATUS=%d CCO=%d BACKUP_CCO=%d SC=%d CCO_PREFERRED=%d WAS_CCO=%d",
            rx_status, rx_cco, rx_is_backup_cco, rx_is_sc, rx_is_cco_preferred, ctx->hpav_conf.was_cco);

    if (LIBSPID_SUCCESS != (ret = plcd_write_hpav_info (&hpav_info_new_values,
        &hpav_info_write, &count)))
    {
        syslog (LOG_ERR, "libspid config write items failed with error %d", ret);
        return -1;
    }

    if (count > 0)
    {
        ctx->is_warn_needed = LIBSPID_TRUE;
        /* save PLCD context */
        memcpy (&ctx->hpav_info, &hpav_info_new_values, sizeof (libspid_hpav_info_t));
    }

    return 0;
}

int
plcd_stack_event_hfid (plcd_ctx_t *ctx, mme_ctx_t *indication_ctx)
{
    char rx_hfid[LIBSPID_HPAV_CONF_HFID_MAX_LEN] = {0};
    unsigned int len = 0;

    PLCD_ASSERT (NULL != ctx);
    PLCD_ASSERT (NULL != indication_ctx);

    /* get the HFID value from MME */
    if (MME_SUCCESS != mme_pull (indication_ctx, rx_hfid,
                                 LIBSPID_HPAV_CONF_HFID_MAX_LEN, &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }

    /* compare to current config value */
    if (((DRV_STA_SET_U_STA_HFID | MME_IND) == indication_ctx->mmtype)
        && (memcmp (rx_hfid, ctx->hpav_conf.user_hfid,
                    LIBSPID_HPAV_CONF_HFID_MAX_LEN)))
    {
        if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
            LIBSPID_HPAV_CONF_LABEL_USER_HFID, rx_hfid))
        {
            syslog (LOG_WARNING, "libspid config write item failed");
            return -1;
        }
        /* save this value into PLCD context */
        memcpy (ctx->hpav_conf.user_hfid, rx_hfid, LIBSPID_HPAV_CONF_HFID_MAX_LEN);
        ctx->is_save_conf_needed = LIBSPID_TRUE;
    }
    else if (((DRV_STA_SET_AVLN_HFID | MME_IND) == \
              indication_ctx->mmtype)
             && (memcmp (rx_hfid, ctx->hpav_conf.avln_hfid,
                         LIBSPID_HPAV_CONF_HFID_MAX_LEN)))
    {
        if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
            LIBSPID_HPAV_CONF_LABEL_AVLN_HFID, rx_hfid))
        {
            syslog (LOG_WARNING, "libspid config write item failed");
            return -1;
        }
        /* save this value into PLCD context */
        memcpy (ctx->hpav_conf.avln_hfid, rx_hfid, LIBSPID_HPAV_CONF_HFID_MAX_LEN);
        ctx->is_save_conf_needed = LIBSPID_TRUE;
    }
    else
    {
        syslog (LOG_WARNING, "stack event hfid: unexpected mmtype (%04x)", indication_ctx->mmtype);
        return -1;
    }

    return 0;
}

int
plcd_stack_event_key (plcd_ctx_t *ctx, mme_ctx_t *indication_ctx)
{
    unsigned char rx_nmk[LIBSPID_HPAV_CONF_NMK_BIN_LEN] = {0}, rx_type = 0, \
        rx_nid[LIBSPID_HPAV_CONF_NID_BIN_LEN] = {0}, rx_sl = 0;
    char nmk_str[LIBSPID_HPAV_CONF_NMK_STR_LEN] = {0}, \
        nid_str[LIBSPID_HPAV_CONF_NID_STR_LEN] = {0}, \
        sl_str[LIBSPID_HPAV_CONF_SL_STR_MAX_LEN] = {0};
    unsigned int len = 0;

    PLCD_ASSERT (NULL != ctx);
    PLCD_ASSERT (NULL != indication_ctx);
    PLCD_ASSERT ((DRV_STA_SET_KEY | MME_IND) == indication_ctx->mmtype);

    /* get received NMK */
    if (MME_SUCCESS != mme_pull (indication_ctx, rx_nmk,
                                 LIBSPID_HPAV_CONF_NMK_BIN_LEN, &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }

    /* get received Type: NID or SL */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_type, sizeof (rx_type), &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (MME_DRV_KEY_TYPE_NB > rx_type);

    /* get received NID */
    if (MME_SUCCESS != mme_pull (indication_ctx, rx_nid,
                                 LIBSPID_HPAV_CONF_NID_BIN_LEN, &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }

    /* get received SL */
    if (MME_SUCCESS != mme_pull (indication_ctx, &rx_sl, sizeof (rx_sl), &len))
    {
        syslog (LOG_WARNING, "mme pull failed");
        return -1;
    }
    PLCD_ASSERT (MME_DRV_KEY_SL_NB > rx_sl);

    /* convert NMK, NID, SL to string */
    if ((LIBSPID_SUCCESS != libspid_binary_to_hexstring (rx_nmk,
        LIBSPID_HPAV_CONF_NMK_BIN_LEN, nmk_str))
        || (LIBSPID_SUCCESS != libspid_binary_to_hexstring (rx_nid,
        LIBSPID_HPAV_CONF_NID_BIN_LEN, nid_str)))
    {
        syslog (LOG_WARNING, "libspid binary to string failed");
        return -1;
    }
    strcpy (sl_str, mme_drv_key_sl_table[rx_sl]);

    /* update config file according to Type */
    if (memcmp (nmk_str, ctx->hpav_conf.nmk_str, LIBSPID_HPAV_CONF_NMK_STR_LEN))
    {
        /* NMK change => set it in hpav.conf */
        syslog (LOG_DEBUG, "stack event key: nmk update");
        if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
            LIBSPID_HPAV_CONF_LABEL_NMK, nmk_str))
        {
            syslog (LOG_WARNING, "libspid config write failed");
            return -1;
        }

        /* save NMK */
        memcpy (ctx->hpav_conf.nmk_str, nmk_str, LIBSPID_HPAV_CONF_NMK_STR_LEN);

        /* hpav.conf save is needed */
        ctx->is_save_conf_needed = LIBSPID_TRUE;
    }
    if (MME_DRV_KEY_TYPE_NID == rx_type)
    {
        syslog (LOG_DEBUG, "stack event key: rx type is nid");
        if (memcmp (nid_str, ctx->hpav_conf.nid_str, LIBSPID_HPAV_CONF_NID_STR_LEN))
        {
            /* NID change => set it in hpav.conf */
            syslog (LOG_DEBUG, "stack event key: nid update");
            if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
                LIBSPID_HPAV_CONF_LABEL_NID, nid_str))
            {
                syslog (LOG_WARNING, "libspid config write failed");
                return -1;
            }

            /* save NID */
            memcpy (ctx->hpav_conf.nid_str, nid_str, LIBSPID_HPAV_CONF_NID_STR_LEN);

            /* hpav.conf save is needed */
            ctx->is_save_conf_needed = LIBSPID_TRUE;
        }
    }
    else
    {
        syslog (LOG_DEBUG, "stack event key: rx type is sl");
        if (strcmp (sl_str, ctx->hpav_conf.sl_str))
        {
            /* SL change and NID reset to none => set them in hpav.conf */
            syslog (LOG_DEBUG, "stack event key: sl update and nid reset");
            if ((LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
                LIBSPID_HPAV_CONF_LABEL_SL, sl_str))
                || (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_conf_path,
                LIBSPID_HPAV_CONF_LABEL_NID, LIBSPID_VALUE_NONE)))
            {
                syslog (LOG_WARNING, "libspid config write failed");
                return -1;
            }

            /* save SL and NID */
            strcpy (ctx->hpav_conf.sl_str, sl_str);
            memcpy (ctx->hpav_conf.nid_str, nid_str, LIBSPID_HPAV_CONF_NID_STR_LEN);

            /* hpav.conf save is needed */
            ctx->is_save_conf_needed = LIBSPID_TRUE;
        }
    }

    return 0;
}

int
plcd_handle_user_key (plcd_ctx_t *ctx, const char *nmk_str, const char *nid_str,
                      const char *sl_str)
{
    PLCD_ASSERT (NULL != ctx);
    PLCD_ASSERT (NULL != nmk_str);
    PLCD_ASSERT (NULL != nid_str);
    PLCD_ASSERT (NULL != sl_str);

    if (0 > plcd_stack_set_key (ctx, nmk_str, nid_str, sl_str))
    {
        syslog (LOG_WARNING, "stack set key failed");
        return -1;
    }

    /* save NMK, NID, SL */
    memcpy (ctx->hpav_conf.nmk_str, nmk_str, LIBSPID_HPAV_CONF_NMK_STR_LEN);
    memcpy (ctx->hpav_conf.nid_str, nid_str, LIBSPID_HPAV_CONF_NID_STR_LEN);
    strcpy (ctx->hpav_conf.sl_str, sl_str);

    return 0;
}

int
plcd_handle_user_sc (plcd_ctx_t *ctx, const char *status,
                     libspid_boolean_t is_sc, libspid_boolean_t is_sc_button,
                     const char *sl_str)
{
    unsigned char sc = 0;

    PLCD_ASSERT (NULL != ctx);
    PLCD_ASSERT (NULL != status);
    PLCD_ASSERT (NULL != sl_str);

    /* check SC and SC button */
    if ((LIBSPID_TRUE == is_sc_button) && (LIBSPID_FALSE == is_sc))
    {
        /* check that Security Level is set to Simple Connect */
        if (!strcmp (LIBSPID_HPAV_CONF_VALUE_SL_SC, sl_str))
        {
            /* start SC process */
            /* check authentication status to know which kind of SC to send */
            if (!strcmp (status, LIBSPID_HPAV_INFO_VALUE_STATUS_UNASSOCIATED))
            {
                /* send SC_JOIN because we are unassociated */
                sc = MME_DRV_SC_JOIN;
            }
            else
            {
                /* send SC_ADD because we are associated or authenticated */
                sc = MME_DRV_SC_ADD;
            }

            if (0 > plcd_send_recv_single_value (ctx, DRV_STA_SC, &sc,
                                                 sizeof (unsigned char)))
            {
                syslog (LOG_WARNING, "send recv single value failed (errno = %s)",
                        strerror (errno));
                return -1;
            }

            if (LIBSPID_SUCCESS != libspid_config_write_item (ctx->hpav_info_path,
                LIBSPID_HPAV_INFO_LABEL_SC, LIBSPID_VALUE_BOOLEAN_TRUE))
            {
                syslog (LOG_WARNING, "libspid config write item failed");
                return -1;
            }
            /* save this value into PLCD context */
            ctx->hpav_info.is_sc = LIBSPID_TRUE;
            ctx->is_warn_needed = LIBSPID_TRUE;
            syslog (LOG_INFO, "handle user sc: sc started");
        }
        else
        {
            /* reset SC button */
            if (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 SC button value */
            is_sc_button = LIBSPID_FALSE;
            ctx->is_warn_needed = LIBSPID_TRUE;
        }
    }

    /* save status and SC button into PLCD context */
    strcpy (ctx->hpav_info.status, status);
    ctx->hpav_info.is_sc_button = is_sc_button;

    return 0;
}

void
plcd_process_signal (plcd_ctx_t *ctx)
{

    /* hpav.conf */
    char is_cco_preferred_str[LIBSPID_BOOLEAN_STR_MAX_LEN] = {0};
    char was_cco_str[LIBSPID_BOOLEAN_STR_MAX_LEN] = {0};
    char nmk_str[LIBSPID_HPAV_CONF_NMK_STR_LEN] = {0};
    char nid_str[LIBSPID_HPAV_CONF_NID_STR_LEN] = {0};
    char sl_str[LIBSPID_HPAV_CONF_SL_STR_MAX_LEN] = {0};
    char user_hfid[LIBSPID_HPAV_CONF_HFID_MAX_LEN] = {0};
    char avln_hfid[LIBSPID_HPAV_CONF_HFID_MAX_LEN] = {0};

    /* hpav.info */
    libspid_hpav_info_t hpav_info;
    memset (&hpav_info, 0, sizeof (libspid_hpav_info_t));

    PLCD_ASSERT (NULL != ctx);

    /* read 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, nmk_str,
        LIBSPID_HPAV_CONF_NMK_STR_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_NID, nid_str,
        LIBSPID_HPAV_CONF_NID_STR_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_SL, 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, user_hfid,
        LIBSPID_HPAV_CONF_HFID_MAX_LEN))
        || (LIBSPID_SUCCESS != libspid_config_read_item (ctx->hpav_conf_path,
        LIBSPID_HPAV_CONF_LABEL_AVLN_HFID, avln_hfid,
        LIBSPID_HPAV_CONF_HFID_MAX_LEN)))
    {
        syslog (LOG_WARNING, "libspid config read item failed");
        return;
    }

    /* check which information has changed in which file */

    if (LIBSPID_GET_BOOLEAN (is_cco_preferred_str) != ctx->hpav_conf.is_cco_preferred)
    {
        /* CCO_PREFERRED => send DRV_STA_SET_CCO_PREF? */
    }

    if (LIBSPID_GET_BOOLEAN (was_cco_str) != ctx->hpav_conf.was_cco)
    {
        /* WAS_CCO => send DRV_STA_SET_WAS_CCO? */
    }

    /* check NMK, NID, SL changes */
    /* check if we need to update NMK/NID/SL */
    if (memcmp (nmk_str, ctx->hpav_conf.nmk_str, LIBSPID_HPAV_CONF_NMK_STR_LEN)
        || memcmp (nid_str, ctx->hpav_conf.nid_str, LIBSPID_HPAV_CONF_NID_STR_LEN)
        || strcmp (sl_str, ctx->hpav_conf.sl_str))
    {
        if (0 > plcd_handle_user_key (ctx, nmk_str, nid_str, sl_str))
        {
            syslog (LOG_WARNING, "handle user key failed");
            return;
        }
    }

    if (memcmp (user_hfid, ctx->hpav_conf.user_hfid, LIBSPID_HPAV_CONF_HFID_MAX_LEN))
    {
        /* USER_HFID => send DRV_STA_SET_U_STA_HFID? */
    }

    if (memcmp (user_hfid, ctx->hpav_conf.user_hfid, LIBSPID_HPAV_CONF_HFID_MAX_LEN))
    {
        /* AVLN_HFID => send DRV_STA_SET_AVLN_HFID? */
    }

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

    if (strcmp (hpav_info.status, ctx->hpav_info.status)
        || strcmp (hpav_info.cco, ctx->hpav_info.cco)
        || (hpav_info.is_backup_cco != ctx->hpav_info.is_backup_cco)
        || (hpav_info.is_sc != ctx->hpav_info.is_sc))
    {
        /* raise an error: cannot happen, this information is modified only by PLC daemon */
        syslog (LOG_ERR, "hpav.info has been modified by a non-allowed application or user");
    }

    /* check if we need to start SC process */
    if (hpav_info.is_sc_button != ctx->hpav_info.is_sc_button)
    {
        /* can start “Simple Connect” procedure */
        if (0 > plcd_handle_user_sc (ctx, hpav_info.status, hpav_info.is_sc,
                                     hpav_info.is_sc_button,
                                     sl_str))
        {
            syslog (LOG_WARNING, "handle user sc failed");
            return;
        }
    }

    /* mcast.info */
    if (plcd_multicast_info_updated (ctx) == LIBSPID_TRUE)
    {
        plcd_multicast_process (ctx);
    }
}