summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/p1905_managerd/src/p1905_managerd.c
blob: 9c52e666ceeb31a436ba40c7f31a3eca3ba263de (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
/*
 * cleopatre/application/p1905_managerd/src/p1905_managerd.c
 *
 * (C) Copyright 2013 MSsar Semiconductor, Inc.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <signal.h>


#include "p1905_managerd.h"
#include "cmdu.h"
#include "libspid.h"

#ifdef SUPPORT_WIFI
#include "wifi_utils.h"
#endif

//#define P1905_MANAGERD_DEBUG
#ifdef P1905_MANAGERD_DEBUG
#define debug(...)          printf(__VA_ARGS__)
#define debug_syslog(...)        printf(__VA_ARGS__)
#else
#define debug(...)
#define debug_syslog(format, ...)   syslog(LOG_WARNING, format, ##__VA_ARGS__)
#endif

#ifdef CHANGE_BR_AGEING_TIME
#define BRIDGE_AGEING_TIME 150//seconds
#endif

#define P1905_TIMER_INTERVAL_SEC 5
#define TOPOLOGY_DISCOVERY_COUNT 55
#define DUMP_INFO_COUNT 70

#ifdef SUPPORT_ALME
#define ALME_WAIT_LINK_METRIC_RSP_CNT 2
#endif

unsigned char p1905_multicast_address[6]
                    = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x13 };

unsigned char nearest_bridge_group_address[6]
                    = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x0e };

static unsigned char lldpdu_timer_expired = 0;
volatile sig_atomic_t timer_expired = 0;
volatile sig_atomic_t exit_signal = 0;
volatile sig_atomic_t hpav_info_has_changed = 0;

static void p1905_timer_handler(int signum)
{
    if (signum == SIGINT || signum == SIGTERM)
        exit_signal = 1;
    else if (signum == SIGALRM)
        timer_expired = 1;
    else if (signum == SIGHUP)
        hpav_info_has_changed = 1;
}

static void p1905_signal_handler(struct p1905_managerd_ctx *ctx)
{
    static unsigned int cnt = 0;
    int is_tpg_changed = 0;
    libspid_hpav_info_t info;
    memset (&info, 0, sizeof (libspid_hpav_info_t));

    if(timer_expired)
    {
        timer_expired =0;

        is_tpg_changed = is_topology_changed(&(ctx->topology_entry.tpddb_head),\
               ctx->non_p1905_neighbor_dev);

        if(is_tpg_changed)
        {
            delete_non_p1905_neighbor_dev_info(ctx->non_p1905_neighbor_dev);
            update_non_1905_device_from_fdb(&(ctx->topology_entry.tpddb_head),
                ctx->non_p1905_neighbor_dev);

            /* if detect new non-1905.1 device join, send topology discovery
             * this mechanism will let 1905.1 device discover soon
             */
            ctx->mid ++;
            insert_cmdu_txq(p1905_multicast_address,ctx->plc0_mac_addr,\
                            e_topology_discovery,ctx->mid);
            insert_cmdu_txq(p1905_multicast_address,ctx->eth0_mac_addr,\
                            e_topology_discovery,ctx->mid);
#ifdef SUPPORT_WIFI
            insert_cmdu_txq(p1905_multicast_address, ctx->wifi0_mac_addr,\
                            e_topology_discovery, ctx->mid);
#endif

            /* use is_topology_changed() to detect any non-1905.1 neighbor
             * device joined/removed. if yes, need to send notify.
             */
            ctx->mid ++;
            insert_cmdu_txq(p1905_multicast_address,ctx->plc0_mac_addr,\
                            e_topology_notification,ctx->mid);
            insert_cmdu_txq(p1905_multicast_address,ctx->eth0_mac_addr,\
                            e_topology_notification,ctx->mid);
#ifdef SUPPORT_WIFI
            insert_cmdu_txq(p1905_multicast_address, ctx->wifi0_mac_addr,\
                            e_topology_notification, ctx->mid);
#endif
        }

        if (hpav_info_has_changed)
        {
            hpav_info_has_changed = 0;
            if (libspid_hpav_info_read_file (&info) == LIBSPID_SUCCESS)
            {
                if (info.is_sc_button)
                {
                    if(0 > p1905_push_button_process(ctx))
                    {
                        debug_syslog("p1905_push_button_process fail \n");
                        return -1;
                    }
                }
            }
        }

        /* now this function be triggerd per second, use cnt to seperate
         * different timer
         */
        cnt++;

        if((cnt % P1905_TIMER_INTERVAL_SEC) == 0 )
        {
            /*1. update LLDP database, if exceed time to live , delete*/
            update_lldp_queue_ttl(P1905_TIMER_INTERVAL_SEC);

            /*2. delete 1905.1 neighbor if not receiving new topology discovery*/
            if(delete_not_exist_p1905_neighbor_device(ctx, P1905_TIMER_INTERVAL_SEC) &&\
                (is_tpg_changed == 0))
            {
                    ctx->mid ++;
                    insert_cmdu_txq(p1905_multicast_address,ctx->plc0_mac_addr,\
                                    e_topology_notification,ctx->mid);
                    insert_cmdu_txq(p1905_multicast_address,ctx->eth0_mac_addr,\
                                    e_topology_notification,ctx->mid);
#ifdef SUPPORT_WIFI
                    insert_cmdu_txq(p1905_multicast_address, ctx->wifi0_mac_addr,\
                                    e_topology_notification, ctx->mid);
#endif
            }

            /*3. if push button is pressed, need to check any new device join*/
            if(ctx->pbc_param.is_sc)
                detect_new_device_join(ctx);

            /*4. if support AP auto config(enrollee or registrar)
             *   use this timer to do re-send mechanism
             */
#if defined(SUPPORT_AP_ENROLLE)
            if(ctx->need_ap_config && (ctx->enrolle_state == no_ap_autoconfig))
            {
                ctx->enrolle_state = wait_4_send_ap_autoconfig_search;
            }

            if(ctx->enrolle_state > wait_4_send_ap_autoconfig_search &&
                    ctx->enrolle_state < wait_4_set_config)
            {
                ctx->ap_config_timeout_cnt++;
            }
#elif defined(SUPPORT_AP_REGISTRAR)
            if(ctx->need_ap_config && is_connection_status_authenticated())
            {
                ap_auto_config_renew_start(ctx);
                ctx->need_ap_config = 0;
            }
#endif

            /*5. if support ALME link metrics query, use this timer to timeout*/
#ifdef SUPPORT_ALME
            if(ctx->alme_state == alme_wait_4_link_metrics_rsp)
            {
                ctx->alme_wait_4_link_metrics_cnt += 1;

                if(ctx->alme_wait_4_link_metrics_cnt >= ALME_WAIT_LINK_METRIC_RSP_CNT)
                {
                    ctx->alme_wait_4_link_metrics_cnt = 0;
                    ctx->alme_state = alme_none;

                    if(ctx->almefd > 0)
                    {
                        wait_4_link_metric_rsp_timeout(ctx->almefd);
                        close(ctx->almefd);
                        ctx->almefd = -1;
                    }
                    else
                    {
                        debug_syslog("error!! alme connection already closed\n");
                    }
                }
            }
#endif
        }

        if((cnt % TOPOLOGY_DISCOVERY_COUNT) == 0 )
        {
            ctx->mid ++;
            insert_cmdu_txq(p1905_multicast_address,ctx->plc0_mac_addr,\
                            e_topology_discovery,ctx->mid);
            insert_cmdu_txq(p1905_multicast_address,ctx->eth0_mac_addr,\
                            e_topology_discovery,ctx->mid);
#ifdef SUPPORT_WIFI
            insert_cmdu_txq(p1905_multicast_address, ctx->wifi0_mac_addr,\
                            e_topology_discovery, ctx->mid);
#endif
            lldpdu_timer_expired = 1;

            /*increment fragment timeout counter*/
            add_fragment_cnt();
            if(get_fragment_cnt() > 2)
                delete_fragment_queue_all();
        }

#ifdef P1905_MANAGERD_DEBUG
        if((cnt % DUMP_INFO_COUNT) == 0)
        {
            dump_topology_info(ctx);
        }
#endif

    }
}

static int p1905_database_init(struct p1905_managerd_ctx *ctx)
{
    LIST_INIT(&(ctx->topology_entry.tpddb_head));
    SLIST_INIT(&(ctx->topology_entry.tprdb_head));
    return 0;
}

static int p1905_interface_init(struct p1905_managerd_ctx *ctx)
{
#ifdef SUPPORT_WIFI
    RF_BAND rf_band;
#endif

    /*init plc0 interface information*/
    memcpy(ctx->itf[0].mac_addr,ctx->plc0_mac_addr,ETH_ALEN);
    ctx->itf[0].media_type = 0x0201;
    ctx->itf[0].vs_info_length = 7;
    ctx->itf[0].vs_info= malloc(ctx->itf[0].vs_info_length);
    memset(ctx->itf[0].vs_info,0,ctx->itf[0].vs_info_length);

    /*init eth0 interface information*/
    memcpy(ctx->itf[1].mac_addr,ctx->eth0_mac_addr,ETH_ALEN);
    ctx->itf[1].media_type = 0x0001;
    ctx->itf[1].vs_info_length = 0;
#ifdef SUPPORT_WIFI
    /*init wifi0 interface information*/
    memcpy(ctx->itf[2].mac_addr, ctx->wifi0_mac_addr, ETH_ALEN);

    if(wifi_utils_success != get_rf_band(&rf_band))
    {
        debug_syslog("interface init get wifi band fail\n");
        rf_band = rf_band_2p4G;
        //return -1;
    }
    if(rf_band == rf_band_2p4G)
        ctx->itf[2].media_type = 0x0103;//802.11n 2.4G
    else
        ctx->itf[2].media_type = 0x0104;//802.11n 5G

    ctx->itf[2].vs_info_length = 10;
    ctx->itf[2].vs_info = malloc(ctx->itf[2].vs_info_length);
    memset(ctx->itf[2].vs_info, 0, ctx->itf[2].vs_info_length);
    /*WIFI device is AP, so directly copy mac address to BSSID field*/
    memcpy(ctx->itf[2].vs_info, ctx->wifi0_mac_addr, 6);
#ifdef SUPPORT_WIFI_STATION
    *((ctx->itf[2].vs_info) + 6) = 0x40;//non-ap station
#endif

#endif

    /*init local bridge capability */
#ifdef SUPPORT_WIFI
    ctx->br_cap[0].interface_num = 3;
#else
    ctx->br_cap[0].interface_num = 2;
#endif
    ctx->br_cap[0].itf_mac_list=malloc(ETH_ALEN * ctx->br_cap[0].interface_num);
    memcpy(ctx->br_cap[0].itf_mac_list,ctx->plc0_mac_addr,ETH_ALEN);
    memcpy((ctx->br_cap[0].itf_mac_list + ETH_ALEN),ctx->eth0_mac_addr,ETH_ALEN);
#ifdef SUPPORT_WIFI
    memcpy((ctx->br_cap[0].itf_mac_list + (ETH_ALEN * 2)),ctx->wifi0_mac_addr,ETH_ALEN);
#endif

    /*init local non-p1905.1 neighbor device information*/
    memcpy(ctx->non_p1905_neighbor_dev[0].local_mac_addr,ctx->plc0_mac_addr,ETH_ALEN);
    ctx->non_p1905_neighbor_dev[0].br_port_no = get_bridge_port_no(PLC0_IFNAME);
    if(ctx->non_p1905_neighbor_dev[0].br_port_no == 0)
    {
        syslog(LOG_WARNING, "get plc0 br port no fail");
        return -1;
    }
    debug("plc0 br port no = %d\n",ctx->non_p1905_neighbor_dev[0].br_port_no);

    LIST_INIT(&(ctx->non_p1905_neighbor_dev[0].non_p1905nbr_head));

    memcpy(ctx->non_p1905_neighbor_dev[1].local_mac_addr,ctx->eth0_mac_addr,ETH_ALEN);
    ctx->non_p1905_neighbor_dev[1].br_port_no = get_bridge_port_no(ETH0_IFNAME);
    if(ctx->non_p1905_neighbor_dev[1].br_port_no == 0)
    {
        syslog(LOG_WARNING, "get eth0 br port no fail");
        return -1;
    }
    debug("eth0 br port no = %d\n",ctx->non_p1905_neighbor_dev[1].br_port_no);

    LIST_INIT(&(ctx->non_p1905_neighbor_dev[1].non_p1905nbr_head));

#ifdef SUPPORT_WIFI
    memcpy(ctx->non_p1905_neighbor_dev[2].local_mac_addr, ctx->wifi0_mac_addr, ETH_ALEN);
    ctx->non_p1905_neighbor_dev[2].br_port_no = get_bridge_port_no(WIFI0_IFNAME);
    if(ctx->non_p1905_neighbor_dev[2].br_port_no == 0)
    {
        syslog(LOG_WARNING, "get wifi0 br port no fail");
        return -1;
    }
    LIST_INIT(&(ctx->non_p1905_neighbor_dev[2].non_p1905nbr_head));
#endif

    /*init local p1905.1 neighbor device information*/
    memcpy(ctx->p1905_neighbor_dev[0].local_mac_addr,ctx->plc0_mac_addr,ETH_ALEN);
    LIST_INIT(&(ctx->p1905_neighbor_dev[0].p1905nbr_head));
    memcpy(ctx->p1905_neighbor_dev[1].local_mac_addr,ctx->eth0_mac_addr,ETH_ALEN);
    LIST_INIT(&(ctx->p1905_neighbor_dev[1].p1905nbr_head));
#ifdef SUPPORT_WIFI
    memcpy(ctx->p1905_neighbor_dev[2].local_mac_addr, ctx->wifi0_mac_addr, ETH_ALEN);
    LIST_INIT(&(ctx->p1905_neighbor_dev[2].p1905nbr_head));
#endif
    return 0;
}

static int p1905_interface_uninit(struct p1905_managerd_ctx *ctx)
{
    int i = 0;

    for(i=0;i<ITF_NUM;i++)
    {
        if(ctx->itf[i].vs_info_length > 0)
            free(ctx->itf[i].vs_info);
    }

    for(i=0;i<BRIDGE_NUM;i++)
    {
        free(ctx->br_cap[i].itf_mac_list);
    }
}

static int p1905_managerd_init(struct p1905_managerd_ctx *ctx)
{

    struct itimerval t;
    sigset_t sigmask;

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

    if(-1 == cmdu_init(ctx))
        return -1;
#ifdef SUPPORT_WIFI
    if(wifi_utils_success != wifi_utils_init(ctx->wifi0_mac_addr))
        return -1;
#endif

    if(-1 == p1905_database_init(ctx))
        return -1;

    if(-1 == p1905_interface_init(ctx))//must do this function after cmdu_init
        return -1;

    if(-1 == lldpdu_init(ctx))
        return -1;

#ifdef SUPPORT_ALME
    if(-1 == ALME_init(ctx))
        return -1;
#endif
    if(-1 == p1905_push_button_init(ctx))
        return -1;

#ifdef SUPPORT_ALME
    if(-1 == ALME_sync_init(ctx))
        return -1;
#endif

#ifdef CHANGE_BR_AGEING_TIME
        /*set bridge ageing time*/
        if(-1 == set_br_fdb_ageing_time(BRIDGE_AGEING_TIME))
            return -1;
#endif

#ifdef SUPPORT_AP_AUTO_CONFIG
    ap_autoconfig_init(ctx);
#endif
    /* Block the SIGALRM signal */
    sigemptyset (&sigmask);
    sigaddset (&sigmask, SIGALRM);

    if (sigprocmask (SIG_BLOCK, &sigmask, NULL) == -1)
    {
        syslog(LOG_WARNING, "sigprocmask (%s)", strerror(errno));
        return -1;
    }

    t.it_interval.tv_usec = 0;
    t.it_interval.tv_sec = 1;//P1905_TIMER_INTERVAL_SEC;
    t.it_value.tv_usec = 0;
    t.it_value.tv_sec = 1;//P1905_TIMER_INTERVAL_SEC;

    if( setitimer( ITIMER_REAL, &t, NULL) < 0 ){
        debug("set p1905 timer fail\n");
        return -1;
    }
    signal( SIGALRM, p1905_timer_handler);
    signal( SIGINT, p1905_timer_handler);
    signal( SIGTERM, p1905_timer_handler);

    // Register to signal SIGHUP on hpav.info modification.*/
    if (LIBSPID_SUCCESS != libspid_system_file_update_register (
            getpid(),
            LIBSPID_HPAV_INFO_PATH, p1905_timer_handler))
    {
        return -1;
    }

    return 0;
}

static int p1905_managerd_process(struct p1905_managerd_ctx *ctx)
{
    int result = 0;
    int length = 0;

    unsigned char *buffer;
    sigset_t empty_sigmask;

    buffer = (unsigned char*)malloc(MAX_PKT_LEN);
    if(NULL == buffer)
    {
        debug("allocate fail\n");
        syslog(LOG_WARNING, "cannot allocate memory (%s)", strerror(errno));
        return -1;
    }

    //Check arguments
    assert(ctx != NULL);
    sigemptyset (&empty_sigmask);

    //debug_syslog("sock_br0 %d, sock_lldp %d, sock_alme %d, gpio_fd %d\n",
    //    ctx->sock_br0, ctx->sock_lldp, ctx->sock_alme, ctx->gpio_fd);

    while(!exit_signal)
    {
        fd_set readfds, exceptfds;

        FD_ZERO(&readfds);
        /*listen to cmdu message, by ether type*/
        FD_SET(ctx->sock_br0, &readfds);
        /*listen to lldp message, by ether type*/
        FD_SET(ctx->sock_lldp, &readfds);
#ifdef SUPPORT_ALME
        /*listen to socket for ALME*/
        FD_SET(ctx->sock_alme, &readfds);
#endif
        /*do not block signal in pselect, if any signal arrive , return errno = EINTR */
        result = pselect (ctx->gpio_fd + 1, &readfds, NULL, NULL, NULL,&empty_sigmask);
        /*block signal from here*/
        if (result > 0)
        {
            if(FD_ISSET(ctx->sock_br0, &readfds))
            {
                /*receive cmdu , process here*/
                debug("receive 1905.1 cmdu\n");

                length = cmdu_bridge_receive(ctx,buffer,MAX_PKT_LEN);
                if(0 > length)
                {
                   debug_syslog("cmdu_bridge_receive fail \n");
                   return -1;
                }

                if(0 > cmdu_parse(ctx,buffer,length))
                {
                   debug_syslog("cmdu parsing fail \n");
                   return -1;
                }
            }
            else if(FD_ISSET(ctx->sock_lldp, &readfds))
            {
                /*reveive LLDPDU, process here*/
                debug("receive lldp message\n");
                length = receive_802_1_bridge_discovery_msg(ctx, buffer, MAX_PKT_LEN);
                if(0 > length)
                {
                   debug("802.1 bridge discovery receive fail \n");
                   return -1;
                }

                if(0 > parse_802_1_bridge_discovery_msg(ctx, buffer))
                {
                    debug("802.1 bridge discovery parse fail \n");
                    return -1;
                }
            }
#ifdef SUPPORT_ALME
            else if(FD_ISSET(ctx->sock_alme, &readfds))
            {
                debug("receive ALME\n");
                length = alme_receive(ctx, buffer, sizeof(ALME_MSG));
                debug("receive ALME len = %d\n",length);
                alme_process(ctx, buffer, length);
            }
#endif
        }
        else
        {
            /*by signal*/
            if(errno == EINTR)
            {
                /*pselect was interrupted and return errno == EINTR,
                  deal with the signal here*/
                p1905_signal_handler(ctx);
                errno = 0;
            }
            else
            {
                syslog (LOG_WARNING, "select failed (%s)", strerror (errno));
                return -1;
            }
        }
#ifdef SUPPORT_AP_ENROLLE
        if(ctx->enrolle_state != no_ap_autoconfig)
            ap_autoconfig_enrolle_sm(ctx);
#endif
        /*if any cmdu need to be transmited , it will be sent here*/
        if(0 > process_cmdu_txq(ctx,buffer))
        {
            debug_syslog("process cmdu txq fail \n");
            return -1;
        }

        if(lldpdu_timer_expired)
        {
            /*according to IEEE P1905.1 spec, we need to send 802.1 bridge
             *detection message after sending topology discovery message
             *so we implement this functionality here
             */
            lldpdu_timer_expired = 0;
            if(0 > send_802_1_bridge_discovery_msg(ctx,\
                   nearest_bridge_group_address, ctx->eth0_mac_addr,buffer))
            {
                debug_syslog("ETH send lldpdu  fail \n");
                return -1;
            }

            if(0 > send_802_1_bridge_discovery_msg(ctx,\
                   nearest_bridge_group_address, ctx->plc0_mac_addr,buffer))
            {
                debug_syslog("PLC send lldpdu  fail \n");
                return -1;
            }
#ifdef SUPPORT_WIFI
            if(0 > send_802_1_bridge_discovery_msg(ctx,\
                   nearest_bridge_group_address, ctx->wifi0_mac_addr, buffer))
            {
                debug_syslog("WIFI send lldpdu  fail \n");
                return -1;
            }
#endif
        }
    }

    free(buffer);
    return result;
}

static void p1905_managerd_uninit(struct p1905_managerd_ctx *ctx)
{
    assert(ctx != NULL);

    p1905_interface_uninit(ctx);
    cmdu_uninit(ctx);
    lldpdu_uninit(ctx);
    p1905_push_button_uninit(ctx);

#ifdef SUPPORT_ALME
    ALME_uninit(ctx);
    ALME_sync_uninit(ctx);
#endif
}

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

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

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

    syslog (LOG_NOTICE, "p1905_Manager Daemon Running");

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

    //Uninitialize p1905_manager daemon
     p1905_managerd_uninit(&ctx);

    //Open syslog file
    closelog();

    return result;
}