summaryrefslogtreecommitdiffstats
path: root/pingid.c
blob: 0dc182a0d522200008befd82cdb7d97b1894c75a (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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <linux/input.h>

#ifndef GPIO
#define GPIO 4
#endif

#define STRINGIFY(a) #a
#define STRINGIFY_HARDER(a) STRINGIFY(a)
#define GPIO_STR STRINGIFY_HARDER(GPIO)


char *names[KEY_MAX + 1] =
  { "", "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "",
  "\t", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "", "",
    "a", "s", "d", "f", "g",
  "h", "j", "k", "l", ";", "'", "`", "", "\\", "z", "x", "c", "v", "b", "n",
    "m", ",", ".",
  "/"
};

static char *
strcasestr (const char *haystack, const char *needle)
{
  size_t len = strlen (needle);

  for (; *haystack; haystack++)
    {

      if (!strncasecmp (haystack, needle, len))
        return (char *) haystack;

    }

  return NULL;

}

static int
squirt (const char *fn, const char *content)
{
  size_t len = strlen (content);
  int fd = open (fn, O_WRONLY);
  int ret = -1;

  if (fd < 0)
    return ret;

  if (write (fd, content, len) == len)
    ret = 0;

  close (fd);
  return ret;
}

int
open_ykhid (void)
{
#define  MAX_DEV	64
  char name[128];
  unsigned i;
  int fd;

  for (i = 0; i < MAX_DEV; ++i)
    {
      sprintf (name, "/dev/input/event%d", i);
      fd = open (name, O_RDWR);
      if (fd < 0)
        continue;

      ioctl (fd, EVIOCGNAME (sizeof (name)), name);
      name[sizeof (name) - 1] = 0;

      if (strcasestr (name, "yubikey"))
        return fd;

      close (fd);
    }

  return -1;
}



static int
get_string_with_timeout (int fd, struct timeval *tv, char *buf,
                         size_t buf_len)
{
  fd_set rfds;
  struct input_event ev;

  if (buf_len)
    buf_len--;


  FD_ZERO (&rfds);
  for (;;)
    {
      FD_SET (fd, &rfds);

      if (select (fd + 1, &rfds, NULL, NULL, tv) < 1)
        break;

      if (read (fd, &ev, sizeof (struct input_event)) !=
          sizeof (struct input_event))
        continue;

      if (ev.type != EV_KEY)
        continue;

      if (buf_len && ev.code == KEY_ENTER && !ev.value)
        return 0;

      if (!ev.value)
        continue;

// printf("code %d (%s)\n",ev.code,names[ev.code]);

      if (buf_len && names[ev.code][0])
        {
          *(buf++) = names[ev.code][0];
          *buf = 0;
          buf_len--;
        }
    }


  return -1;
}

int
main (int argc, char *argv[])
{
  int fd = open_ykhid ();
  struct timeval tv;
  char ret[128];


  squirt ("/sys/class/gpio/export", GPIO_STR);
  squirt ("/sys/class/gpio/gpio" GPIO_STR "/direction", "out");
  squirt ("/sys/class/gpio/gpio" GPIO_STR "/value", "0");


  tv.tv_sec = 0;
  tv.tv_usec = 100;
  get_string_with_timeout (fd, &tv, NULL, 0);

  squirt ("/sys/class/gpio/gpio" GPIO_STR "/value", "1");


  tv.tv_sec = 2;
  tv.tv_usec = 0;

  get_string_with_timeout (fd, &tv, ret, sizeof (ret));

  squirt ("/sys/class/gpio/gpio" GPIO_STR "/value", "0");

  printf ("Access-Control-Allow-Origin: *\n");
  printf ("Content-Type: text/plain\n\n%s\n", ret);

  return 0;
}