summaryrefslogtreecommitdiff
path: root/src/qr-show.c
blob: 728b9ca6c4f76feff78dde55490c35a8288bb4cd (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
/*
 This file is part of TALER
 Copyright (C) 2019 GNUnet e.V.

 TALER 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 3, or (at your option) any later version.

 TALER 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
 TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
* @file qr-show.c
* @brief shows a QR code on the display for a defined amount of time
* @author Boss Marco
* @author Christian Grothoff
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#if HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_NETINET_IP_H
#include <netinet/ip.h>         /* superset of previous */
#endif
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <termios.h>
#include <nfc/nfc.h>
#include <microhttpd.h>
#include <gnunet/gnunet_util_lib.h>
#include <qrencode.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
/* for adafruit pitft display */
#include <linux/fb.h>

/**
 * Disable i18n support.
 */
#define _(s) (s)

/**
 * Handle for the Framebuffer device
 */
struct Display
{
  /**
   * File descriptor for the screen
   */
  int devicefd;

  /**
   * File descriptor to set backlight information
   */
  int backlightfd;

  /**
   * The display memory to set the pixel information
   */
  uint16_t *memory;

  /**
   * Original screen information
   */
  struct fb_var_screeninfo orig_vinfo;

  /**
   * Variable screen information (color depth ...)
   */
  struct fb_var_screeninfo var_info;

  /**
   * Fixed screen informtaion
   */
  struct fb_fix_screeninfo fix_info;
};


/**
 * Next program to launch.
 */
static char *const *arg_next;

static struct GNUNET_TIME_Relative delay;

/**
 * Refenence to the delay task
 */
static struct GNUNET_SCHEDULER_Task *delay_task;

/**
 * Name of the framebuffer device (i.e. /dev/fb1).
 */
static char *framebuffer_device_filename;

/**
 * Name of the backlight file of @e framebuffer_device_filename (i.e. /sys/class/backlight/soc:backlight/brightness).
 */
static char *framebuffer_backlight_filename;

/**
 * Global option '-i' to invert backlight on/off values
 */
static int backlight_invert;

/**
 * Standard backlight on value
 */
static char backlight_on = '1';

/**
 * Standard backlight off value
 */
static char backlight_off = '0';

/**
 * Handle for the framebuffer device
 */
static struct Display qrDisplay;


/**
 * @brief Create the QR code to pay and display it on screen
 *
 * @param uri what text to show in the QR code
 */
static void
show_qrcode (const char *uri)
{
  QRinput *qri;
  QRcode *qrc;
  unsigned int size;
  size_t xOff;
  size_t yOff;

  if (0 > qrDisplay.devicefd)
    return; /* no display, no dice */
  qri = QRinput_new2 (0, QR_ECLEVEL_L);
  if (NULL == qri)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                         "QRinput_new2");
    return;
  }
  /* first try encoding as uppercase-only alpha-numerical
     QR code (much smaller encoding); if that fails, also
     try using binary encoding (in case nick contains
     special characters). */
  if ( (0 !=
        QRinput_append (qri,
                        QR_MODE_AN,
                        strlen (uri),
                        (unsigned char *) uri)) &&
       (0 !=
        QRinput_append (qri,
                        QR_MODE_8,
                        strlen (uri),
                        (unsigned char *) uri)))
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                         "QRinput_append");
    return;
  }
  qrc = QRcode_encodeInput (qri);
  if (NULL == qrc)
  {
    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                         "QRcode_encodeInput");
    QRinput_free (qri);
    return;
  }

  /* +8 for 4-pixels border */
  size = GNUNET_MIN (qrDisplay.var_info.xres,
                     qrDisplay.var_info.yres);
  unsigned int nwidth = qrc->width + 8; /* 4 pixel border */
  xOff = 4 * size / nwidth;
  yOff = 4 * size / nwidth;
  if (qrDisplay.var_info.xres < qrDisplay.var_info.yres)
    yOff += (qrDisplay.var_info.yres - qrDisplay.var_info.xres) / 2;
  else
    xOff += (qrDisplay.var_info.xres - qrDisplay.var_info.yres) / 2;
  for (unsigned int x = 0; x < qrDisplay.var_info.xres - 2 * xOff; x++)
    for (unsigned int y = 0; y < qrDisplay.var_info.yres - 2 * yOff; y++)
    {
      unsigned int xoff = x * nwidth / size;
      unsigned int yoff = y * nwidth / size;
      unsigned int off = xoff + yoff * qrc->width;
      if ( (xoff >= (unsigned) qrc->width) ||
           (yoff >= (unsigned) qrc->width) )
        continue;
      qrDisplay.memory[(y + yOff) * qrDisplay.var_info.xres + (x + xOff)] =
        (0 == (qrc->data[off] & 1)) ? 0xFFFF : 0x0000;
    }

  QRcode_free (qrc);
  QRinput_free (qri);

  if (0 < qrDisplay.backlightfd)
    (void) ! write (qrDisplay.backlightfd,
                    &backlight_on,
                    1);
}


static void
stop_task (void *cls)
{
  (void) cls;
  delay_task = NULL;
  GNUNET_SCHEDULER_shutdown ();
}


static void
shutdown_task (void *cls)
{
  (void) cls;
  if (NULL != delay_task)
  {
    GNUNET_SCHEDULER_cancel (delay_task);
    delay_task = NULL;
  }
  if (NULL != qrDisplay.memory)
    memset (qrDisplay.memory,
            0xFF,
            qrDisplay.var_info.xres * qrDisplay.var_info.yres
            * sizeof (uint16_t));
  if (0 < qrDisplay.backlightfd)
    (void) ! write (qrDisplay.backlightfd,
                    &backlight_off,
                    1);
  if (NULL != qrDisplay.memory)
  {
    /* free the display data  */
    munmap (qrDisplay.memory,
            qrDisplay.fix_info.smem_len);
    qrDisplay.memory = NULL;
    /* reset original state */
    if (0 > ioctl (qrDisplay.devicefd,
                   FBIOPUT_VSCREENINFO,
                   &qrDisplay.orig_vinfo))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  "failed to reset originial display state\n");
    }
    /* close device */
    close (qrDisplay.devicefd);
    qrDisplay.devicefd = -1;
    if (0 < qrDisplay.backlightfd)
      close (qrDisplay.backlightfd);
    qrDisplay.backlightfd = -1;
  }
}


/**
 * @brief Start the application
 *
 * @param cls closure
 * @param args arguments left
 * @param cfgfile config file name
 * @param cfg handle for the configuation file
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  (void) cls;
  (void) cfgfile;

  if (NULL == args[0])
    return;
  arg_next = args + 1;
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (cfg,
                                               "taler-mdb",
                                               "FRAMEBUFFER_DEVICE",
                                               &framebuffer_device_filename))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "taler-mdb",
                               "FRAMEBUFFER_DEVICE");
    framebuffer_device_filename = GNUNET_strdup ("/dev/fb1");
  }
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_filename (cfg,
                                               "taler-mdb",
                                               "FRAMEBUFFER_BACKLIGHT",
                                               &framebuffer_backlight_filename))
  {
    GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
                               "taler-mdb",
                               "FRAMEBUFFER_BACKLIGHT");
    framebuffer_backlight_filename = GNUNET_strdup (
      "/sys/class/backlight/soc:backlight/brightness");
  }
  /* open the framebuffer device */
  qrDisplay.devicefd = open (framebuffer_device_filename,
                             O_RDWR);
  if (0 < qrDisplay.devicefd)
  {
    /* read information about the screen */
    ioctl (qrDisplay.devicefd,
           FBIOGET_VSCREENINFO,
           &qrDisplay.var_info);

    /* store current screeninfo for reset */
    qrDisplay.orig_vinfo = qrDisplay.var_info;

    if (16 != qrDisplay.var_info.bits_per_pixel)
    {
      /* Change variable info to 16 bit per pixel */
      qrDisplay.var_info.bits_per_pixel = 16;
      if (0 > ioctl (qrDisplay.devicefd,
                     FBIOPUT_VSCREENINFO,
                     &qrDisplay.var_info))
      {
        GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                             "ioctl(FBIOPUT_VSCREENINFO)");
        return;
      }
    }

    /* Get fixed screen information */
    if (0 > ioctl (qrDisplay.devicefd,
                   FBIOGET_FSCREENINFO,
                   &qrDisplay.fix_info))
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                           "ioctl(FBIOGET_FSCREENINFO)");
      return;
    }

    /* get pointer onto frame buffer */
    qrDisplay.memory =  mmap (NULL,
                              qrDisplay.fix_info.smem_len,
                              PROT_READ | PROT_WRITE, MAP_SHARED,
                              qrDisplay.devicefd,
                              0);

    /* open backlight file to turn display backlight on and off */
    if (0 > qrDisplay.devicefd)
    {
      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
                           "mmap");
      return;
    }

    memset (qrDisplay.memory,
            0xFF,
            qrDisplay.var_info.xres * qrDisplay.var_info.yres
            * sizeof (uint16_t));

    qrDisplay.backlightfd = open (
      framebuffer_backlight_filename, O_WRONLY);
    if (0 > qrDisplay.backlightfd)
    {
      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                                "open",
                                framebuffer_backlight_filename);
    }
    else
    {
      if (backlight_invert)
      {
        backlight_on = '0';
        backlight_off = '1';
      }
      (void) ! write (qrDisplay.backlightfd,
                      &backlight_off,
                      1);
    }
  }
  else
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                              "open",
                              framebuffer_device_filename);
  }
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
                                 NULL);
  show_qrcode (args[0]);
  delay_task = GNUNET_SCHEDULER_add_delayed (delay,
                                             &stop_task,
                                             NULL);
}


int
main (int argc,
      char*const*argv)
{
  int ret;
  /* the available command line options */
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_option_relative_time ('d',
                                        "delay",
                                        "DELAY",
                                        "how long should we display the text before continuing",
                                        &delay),
    GNUNET_GETOPT_option_flag ('i',
                               "backlight-invert",
                               "invert the backlight on/off values (standard on = 1)",
                               &backlight_invert),
    GNUNET_GETOPT_OPTION_END
  };

  ret = GNUNET_PROGRAM_run (argc,
                            argv,
                            "qr-show",
                            "This is an application to show a QR code for a defined period of time before starting another program.\n",
                            options,
                            &run,
                            NULL);
  if (GNUNET_OK != ret)
    return 1;
  if ( (NULL == arg_next) ||
       (NULL == arg_next[0]) )
    return 0;
  execvp (arg_next[0],
          arg_next);
  GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
                            "execvp",
                            arg_next[0]);
  return 1;
}