summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/gpio_event.c
blob: 6798e2f6d0ef9e78bf1efae233f0b76bafa0196e (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
/*
 * cleopatre/application/managerd/src/gpio_event.c
 *
 * (C) Copyright 2010 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 <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <syslog.h>
#include <errno.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>

#include "nvram.h"
#include "../../linux/gpio.h"

#include "managerd.h"
#include "gpio_event.h"

/* Global variable for "/dev/gpio" file descriptor
 * needed during SIGALRM signal handling */
int gpio_fd = -1;

/**
 * Simple Connect detection event.
 *
 * \param  ctx  managerd context
 * \return  error code
 */
int
simple_connect_event (struct managerd_ctx *ctx)
{
    if (!ctx->is_sc_available)
        return 0;

    /* Write SC button GPIO status */
    libspid_config_write_item (LIBSPID_HPAV_INFO_PATH,
                               LIBSPID_HPAV_INFO_LABEL_SC_BUTTON,
                               "yes");

    /* plc daemon needs to be informed */
    if (LIBSPID_SUCCESS != libspid_system_file_update_warn (getpid(),
        LIBSPID_HPAV_INFO_PATH))
    {
        syslog (LOG_WARNING, "libspid system file update warn failed");
        return -1;
    }

    return 0;
}

/**
 * Open and set GPIO direction.
 *
 * \param  ctx  managerd context
 * \return  error code
 */
int
simple_connect_init (struct managerd_ctx *ctx)
{
    /* Check arguments */
    assert (ctx != NULL);

    /* Set GPIO info for SC button */
    ctx->sc_gpio.gpiodir.num = SC_BUT_GPIO_NUM;
    ctx->sc_gpio.gpiodir.dir = SPC300_GPIO_DIRECTION_INPUT;

    /* Set GPIO info for SC button interruption */
    ctx->sc_it.gpioit.enable = 1;
    ctx->sc_it.gpioit.mask = (1 << SC_BUT_GPIO_NUM);

    struct gpio_debounce debounce;
    debounce.num = SC_BUT_GPIO_NUM;
    debounce.val = SC_BUT_DEBOUNCE;

    /* Open GPIO device */
    if (0 > (ctx->gpio_fd = open (GPIO_DEVICE_NAME, O_RDWR)))
    {
        syslog (LOG_WARNING, "cannot open gpio device (%s)", strerror (errno));
        return -1;
    }

    /* Set gpio direction for Simple Connect */
    if (0 > ioctl (ctx->gpio_fd, GPIOIOC_SETDIRECTION,
                   (unsigned long *) &ctx->sc_gpio))
    {
        syslog (LOG_WARNING, "cannot call ioctl for SC gpio (%s)", strerror (errno));
    }
    else
    {
        ctx->is_sc_available = 1;
    }
    if (0 > ioctl (ctx->gpio_fd, GPIOIOC_SETDEBOUNCE,
                   (unsigned long *) &debounce))
    {
        syslog (LOG_WARNING, "cannot call ioctl for SC gpio (%s)", strerror (errno));
    }

    /* Set GPIO interrupt for SC button */
    if (0 > ioctl (ctx->gpio_fd, GPIOIOC_SETIT,
                   (unsigned long *) &ctx->sc_it))
    {
        syslog (LOG_WARNING, "cannot call ioctl for SC interruption (%s)", strerror (errno));
    }

    return 0;
}

unsigned int
simple_connect_button_status_get (struct managerd_ctx *ctx)
{
    if (0 > ioctl (ctx->gpio_fd, GPIOIOC_GETVALUE,
                   (unsigned long *) &ctx->sc_gpio))
    {
        syslog (LOG_WARNING, "cannot call ioctl for SC gpio (%s)",
                strerror (errno));
        return -1;
    }
    else
    {
        return ctx->sc_gpio.gpioval.val;
    }
}

/**
 * Close gpio device.
 *
 * \param  ctx  managerd context
 */
void
simple_connect_uninit (struct managerd_ctx *ctx)
{
    /* Check arguments */
    assert (ctx != NULL);

    /* Reet GPIO info for SC button interruption */
    ctx->sc_it.gpioit.enable = 0;
    ctx->sc_it.gpioit.mask = 0;

    /* Reset GPIO interrupt for SC button */
    if (0 > ioctl (ctx->gpio_fd, GPIOIOC_SETIT,
                   (unsigned long *) &ctx->sc_it))
    {
        syslog (LOG_WARNING, "cannot call ioctl for SC interruption (%s)", strerror (errno));
    }

    /* Close GPIO device */
    close (ctx->gpio_fd);
}

/**
 * Handle SIGALRM reception.
 *
 * \param  signal_nb  signal identifier
 */
void
led_signal_handler (int signal_nb)
{
    /* check that we received a SIGALRM signal (other signals must be ignored) */
    if (SIGALRM == signal_nb)
    {
        led_flashing ();
    }
}

/**
 * When timer expires, this function is called to invert current value of LED,
 * in order to make it blink.
 */
void
led_flashing (void)
{
    union gpio_info led_gpio;

    /* Set GPIO number */
    led_gpio.gpioval.num = LED_ATTACHMENT_GPIO_NUM;

    /* Get GPIO value */
    if (0 > (ioctl (gpio_fd, GPIOIOC_GETVALUE, (unsigned long *) &led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl GETVALUE for LED GPIO (%s)",
                strerror (errno));
    }

    /* Invert GPIO value */
    if (led_gpio.gpioval.val)
        led_gpio.gpioval.val = 0;
    else
        led_gpio.gpioval.val = 1;

    /* Set GPIO value */
    if (0 > (ioctl (gpio_fd, GPIOIOC_SETVALUE, (unsigned long *) &led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED GPIO (%s)",
                strerror (errno));
    }
}

/**
 * LED attachment event.
 *
 * \param  ctx  managerd context
 * \return  error code
 */
int
led_attachment_event (struct managerd_ctx *ctx, const char *status,
                      libspid_boolean_t is_sc)
{
    struct itimerval itv;
    /* Set default timer interval */
    itv.it_interval.tv_sec = 0;
    itv.it_interval.tv_usec = 0;
    /* Set default current value */
    itv.it_value.tv_sec = 0;
    itv.it_value.tv_usec = 0;

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

    /* Set GPIO number */
    ctx->led_gpio.gpioval.num = LED_ATTACHMENT_GPIO_NUM;

    if ((LIBSPID_TRUE == ctx->hpav_info.is_sc) && (LIBSPID_FALSE == is_sc)
        && strcmp (LIBSPID_HPAV_INFO_VALUE_STATUS_AUTHENTICATED, status))
    {
        /* SC has failed: modem is not authenticated after a SC procedure
         * Fast flashing of the LED: one flash every 100ms
         * Start timer */
        itv.it_interval.tv_usec = LED_FAST_TIMER_US;
        itv.it_value.tv_usec = LED_FAST_TIMER_US;
        if (0 > setitimer (ITIMER_REAL, &itv, &itv))
        {
            syslog (LOG_WARNING, "setitimer failed (%s)", strerror (errno));
        }
    }
    else if (((LIBSPID_FALSE == is_sc)
        && !strcmp(LIBSPID_HPAV_INFO_VALUE_STATUS_ASSOCIATED, status))
        || (LIBSPID_TRUE == is_sc))
    {
        /* Associated (or associating): standard attachment procedure,
         * or SC is on-going
         * Low flashing of the LED: one flash every 500ms
         * Start timer */
        itv.it_interval.tv_usec = LED_LOW_TIMER_US;
        itv.it_value.tv_usec = LED_LOW_TIMER_US;
        if (0 > setitimer (ITIMER_REAL, &itv, &itv))
        {
            syslog (LOG_WARNING, "setitimer failed (%s)", strerror (errno));
        }
    }
    else if (!strcmp (LIBSPID_HPAV_INFO_VALUE_STATUS_AUTHENTICATED, status))
    {
        /* Authenticated: modem is attached to an AVLN
         * Stop timer and set GPIO value */
        if (0 > setitimer (ITIMER_REAL, &itv, &itv))
        {
            syslog (LOG_WARNING, "setitimer failed (%s)", strerror (errno));
        }
        ctx->led_gpio.gpioval.val = 1;
        if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                 (unsigned long *) &ctx->led_gpio)))
        {
            syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED GPIO (%s)",
                    strerror (errno));
        }
    }
    else if ((LIBSPID_FALSE == is_sc)
        && !strcmp (LIBSPID_HPAV_INFO_VALUE_STATUS_UNASSOCIATED, status))
    {
        /* Unassociated: modem is on,
         * but standard attachment procedure has not started (or has failed)
         * Stop timer and reset GPIO value */
        if (0 > setitimer (ITIMER_REAL, &itv, &itv))
        {
            syslog (LOG_WARNING, "setitimer failed (%s)", strerror (errno));
        }
        ctx->led_gpio.gpioval.val = 0;
        if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                 (unsigned long *) &ctx->led_gpio)))
        {
            syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED GPIO (%s)",
                    strerror (errno));
        }
    }
    else
    {
        syslog (LOG_WARNING, "led_attachment_event: unknown state");
    }

    return 0;
}

/**
 * LED1 event.
 *
 * \param  ctx  managerd context
  * \param  cco  current CCo value from hpav.info
 * \return  error code
 */
int
led1_event (struct managerd_ctx *ctx, const char *cco)
{
    /* Check arguments */
    assert (NULL != ctx);
    assert (NULL != cco);

#if defined (CONFIG_EXTRA_LEDS)
    /* Set GPIO number */
    ctx->led_gpio.gpioval.num = CONFIG_LED1_GPIO_NUM;

    if (!strcmp (LIBSPID_HPAV_INFO_VALUE_CCO_STATION, cco)
        || !strcmp (LIBSPID_HPAV_INFO_VALUE_CCO_PROXY, cco))
    {
        /* Simple station or proxy CCo
         * Reset GPIO value */
        ctx->led_gpio.gpioval.val = 0;
        if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                 (unsigned long *) &ctx->led_gpio)))
        {
            syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED1 GPIO (%s)",
                    strerror (errno));
        }
    }
    else if (!strcmp (LIBSPID_HPAV_INFO_VALUE_CCO_MAIN, cco))
    {
        /* Main CCo: modem took control of the AVLN
         * Set GPIO value */
        ctx->led_gpio.gpioval.val = 1;
        if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                 (unsigned long *) &ctx->led_gpio)))
        {
            syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED1 GPIO (%s)",
                    strerror (errno));
        }
    }
    else
    {
        syslog (LOG_WARNING, "led1_event: unknown state");
    }
#else
    syslog (LOG_DEBUG, "led1_event: no led1");
#endif

    return 0;
}

/**
 * LED2 event.
 *
 * \param  ctx  managerd context
  * \param  is_backup_cco  current backup CCo value from hpav.info
 * \return  error code
 */
int
led2_event (struct managerd_ctx *ctx, libspid_boolean_t is_backup_cco)
{
    /* Check arguments */
    assert (NULL != ctx);

#if defined (CONFIG_EXTRA_LEDS)
    /* Set GPIO number */
    ctx->led_gpio.gpioval.num = CONFIG_LED2_GPIO_NUM;

    if (LIBSPID_FALSE == is_backup_cco)
    {
        /* Not a backup CCo
         * Reset GPIO value */
        ctx->led_gpio.gpioval.val = 0;
        if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                 (unsigned long *) &ctx->led_gpio)))
        {
            syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED2 GPIO (%s)",
                    strerror (errno));
        }
    }
    else if (LIBSPID_TRUE == is_backup_cco)
    {
        /* Backup CCo
         * Set GPIO value */
        ctx->led_gpio.gpioval.val = 1;
        if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                 (unsigned long *) &ctx->led_gpio)))
        {
            syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED2 GPIO (%s)",
                    strerror (errno));
        }
    }
    else
    {
        syslog (LOG_WARNING, "led2_event: unknown state");
    }
#else
    syslog (LOG_DEBUG, "led2_event: no led2");
#endif

    return 0;
}

/**
 * Set GPIO direction.
 *
 * \param  ctx  managerd context
 * \return  error code
 */
int
led_init (struct managerd_ctx *ctx)
{
    /* Check arguments */
    assert (NULL != ctx);
    assert (0 <= ctx->gpio_fd);

    /* Set GPIO direction */
    ctx->led_gpio.gpiodir.num = LED_ATTACHMENT_GPIO_NUM;
    ctx->led_gpio.gpiodir.dir = SPC300_GPIO_DIRECTION_OUTPUT;
    ctx->led_gpio.gpiodir.val = 0;
    if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETDIRECTION,
                    (unsigned long *) &ctx->led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETDIRECTION for LED GPIO (%s)",
                strerror (errno));
    }

#if defined (CONFIG_EXTRA_LEDS)
    /* Set GPIO direction of LED1&2 */
    ctx->led_gpio.gpiodir.num = CONFIG_LED1_GPIO_NUM;
    if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETDIRECTION,
                    (unsigned long *) &ctx->led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETDIRECTION for LED1 GPIO (%s)",
                strerror (errno));
    }
    ctx->led_gpio.gpiodir.num = CONFIG_LED2_GPIO_NUM;
    if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETDIRECTION,
                    (unsigned long *) &ctx->led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETDIRECTION for LED2 GPIO (%s)",
                strerror (errno));
    }
#endif

    /* Register the SIGALRM signal handler for LED flashing */
    if (SIG_ERR == signal (SIGALRM, led_signal_handler))
    {
        syslog (LOG_WARNING, "cannot register led signal handler");
        return -1;
    }

    /* Set global variable */
    gpio_fd = ctx->gpio_fd;

    return 0;
}

/**
 * Reset GPIO value.
 *
 * \param  ctx  managerd context
 */
void
led_uninit (struct managerd_ctx *ctx)
{
    /* Check argument */
    assert (ctx != NULL);

    /* Reset GPIO value */
    ctx->led_gpio.gpioval.num = LED_ATTACHMENT_GPIO_NUM;
    ctx->led_gpio.gpioval.val = 0;
    if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                    (unsigned long *) &ctx->led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED GPIO (%s)",
                strerror (errno));
    }

#if defined (CONFIG_EXTRA_LEDS)
    /* Reset GPIO value of LED1&2 */
    ctx->led_gpio.gpioval.num = CONFIG_LED1_GPIO_NUM;
    if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                    (unsigned long *) &ctx->led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED1 GPIO (%s)",
                strerror (errno));
    }
    ctx->led_gpio.gpioval.num = CONFIG_LED2_GPIO_NUM;
    if (0 > (ioctl (ctx->gpio_fd, GPIOIOC_SETVALUE,
                    (unsigned long *) &ctx->led_gpio)))
    {
        syslog (LOG_WARNING, "cannot call ioctl SETVALUE for LED2 GPIO (%s)",
                strerror (errno));
    }
#endif
}