summaryrefslogtreecommitdiff
path: root/cleopatre/application/libspid/src/hardware.c
blob: 80061995f66e7c121ba21a640985f95d989cf0e3 (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
/* SPC300 bundle {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    application/libspid/src/hardware.c
 * \brief   hardware-oriented functions
 * \ingroup libspid
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include "libspid.h"
#include <syslog.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include "../../linux/gpio.h"

/**
 * Returns current internal temperature of the master (in Celsius degrees).
 *
 * \param temp_c pointer to store current master temperature
 *               in Celsius degrees
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameter
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t
libspid_hardware_get_temperature (int *temp_c)
{
    /* TEMP: return constant value */
    *temp_c = 0;

    return LIBSPID_SUCCESS;
}

/**
 * Returns tamper status of the master:
 * LIBSPID_TAMPER_STATUS_COMPROMISED if box has been opened,
 * LIBSPID_TAMPER_STATUS_INTACT if box has not been opened.
 *
 * \param tamper_state pointer to store tamper status of the master
 * \return error type (LIBSPID_SUCCESS if success)
 * \return LIBSPID_ERROR_PARAM: bad input parameter
 * \return LIBSPID_ERROR_SYSTEM: system error, see errno
 */

libspid_error_t
libspid_hardware_get_tamper (libspid_eoc_tamper_status_t *tamper_status)
{
    /* Check arguments */
    if (tamper_status == NULL)
        return LIBSPID_ERROR_PARAM;

    *tamper_status = LIBSPID_TAMPER_STATUS_INTACT;

#if defined (CONFIG_TAMPER_INDICATOR)
    int gpio_fd;
    union gpio_info tamper_gpio;

    tamper_gpio.gpiodir.num = CONFIG_TAMPER_INDICATOR_GPIO_NUM;
    tamper_gpio.gpiodir.dir = SPC300_GPIO_DIRECTION_INPUT;

     /* Open GPIO device */
    gpio_fd = open (LIBSPID_GPIO_DEVICE_NAME, O_RDWR);
    if (0 > gpio_fd)
    {
        syslog (LOG_WARNING, "cannot open gpio device");
        return LIBSPID_ERROR_SYSTEM;
    }

    /* Set gpio direction */
    if(0 > ioctl (gpio_fd, GPIOIOC_SETDIRECTION,
                   (unsigned long *) &tamper_gpio))
    {
        syslog (LOG_WARNING, "cannot call ioctl for tamper gpio ");
        close (gpio_fd);
        return LIBSPID_ERROR_SYSTEM;
    }

    /* Read TAMPER GPIO */
    if (0 > ioctl (gpio_fd, GPIOIOC_GETVALUE, (unsigned long*) &tamper_gpio))
    {
        syslog (LOG_WARNING, "cannot call ioctl for TAMPER gpio");
        close (gpio_fd);
        return LIBSPID_ERROR_SYSTEM;
    }

    if (tamper_gpio.gpioval.val == 0)
        *tamper_status = LIBSPID_TAMPER_STATUS_INTACT;
    else if (tamper_gpio.gpioval.val == 1)
        *tamper_status = LIBSPID_TAMPER_STATUS_COMPROMISED;

    /* Close GPIO device */
    close (gpio_fd);
#endif

    return LIBSPID_SUCCESS;
}