summaryrefslogtreecommitdiff
path: root/cleopatre/application/managerd/src/simple_connect.c
blob: bcbec17ffcaa3660aac8d9f5aa3287fdeb384e47 (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
/*
 * cleopatre/application/managerd/src/simple_connect.c
 *
 * (C) Copyright 2009 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 "../../linux/gpio.h"

#include "libspid.h"
#include "simple_connect.h"

/** Previous sc button gpio value */
static int previous_sc_button_value = SC_BUTTON_OFF;

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

    //Read SC button GPIO
    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;
    }

    //if SC button stay on between to events, SC button is really on
    if(previous_sc_button_value != ctx->sc_gpio.gpioval.val)
    {
        if(SC_BUTTON_ON == ctx->sc_gpio.gpioval.val)
        {
            libspid_config_write_item(LIBSPID_HPAV_INFO_PATH, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "yes");
        }
        else
        {
            libspid_config_write_item(LIBSPID_HPAV_INFO_PATH, LIBSPID_HPAV_INFO_LABEL_SC_BUTTON, "no");
        }

        //Store GPIO value for next passage
        previous_sc_button_value = ctx->sc_gpio.gpioval.val;
    }

    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 = 0; //Input

    //Set GPIO info for SC LED
    ctx->led_gpio.gpiodir.num = SC_LED_GPIO_NUM;
    ctx->led_gpio.gpiodir.dir = 1; //Output
    ctx->led_gpio.gpiodir.val = 0;

    //Open GPIO device
    if(0 > (ctx->gpio_fd=open("/dev/gpio",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;
    }

    //Set gpio direction for LED
    if(0 > (ioctl(ctx->gpio_fd, GPIOIOC_SETDIRECTION, (unsigned long*)&ctx->led_gpio)))
    {
        syslog(LOG_WARNING, "cannot call ioctl for LED gpio (%s)", strerror(errno));
    }

    return 0;
}

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

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