Blame view

RIOT/drivers/pn532/pn532.c 17.6 KB
a752c7ab   elopes   add first test an...
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
  /*
   * Copyright (C) 2016 TriaGnoSys GmbH
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup  drivers_pn532
   *
   * @{
   * @file
   * @brief   PN532 driver
   *
   * @author  Víctor Ariño <victor.arino@triagnosys.com>
   * @}
   */
  
  #include <stdio.h>
  #include <string.h>
  
  #include "assert.h"
  #include "xtimer.h"
  #include "mutex.h"
  #include "pn532.h"
  #include "periph/gpio.h"
  #include "periph/i2c.h"
  #include "periph/spi.h"
  
  #define ENABLE_DEBUG    (0)
  #include "debug.h"
  
  #define PN532_I2C_ADDRESS           (0x24)
  
  /* Commands */
  #define CMD_FIRMWARE_VERSION        (0x02)
  #define CMD_READ_REG                (0x06)
  #define CMD_WRITE_REG               (0x08)
  #define CMD_READ_GPIO               (0x0c)
  #define CMD_WRITE_GPIO              (0x0E)
  #define CMD_SAM_CONFIG              (0x14)
  #define CMD_RF_CONFIG               (0x32)
  #define CMD_DATA_EXCHANGE           (0x40)
  #define CMD_DESELECT                (0x44)
  #define CMD_LIST_PASSIVE            (0x4a)
  #define CMD_RELEASE                 (0x52)
  
  /* Mifare specific commands */
  #define MIFARE_CMD_READ             (0x30)
  #define MIFARE_CMD_WRITE            (0xA0)
  
  /* RF register settings */
  #define RF_CONFIG_MAX_RETRIES       (0x05)
  
  /* Buffer operations */
  #define BUFF_CMD_START              (6)
  #define BUFF_DATA_START             (BUFF_CMD_START + 1)
  #define RAPDU_DATA_BEGIN            (1)
  #define RAPDU_MAX_DATA_LEN          (PN532_BUFFER_LEN - BUFF_DATA_START - 5)
  #define CAPDU_MAX_DATA_LEN          (PN532_BUFFER_LEN - BUFF_DATA_START - 1)
  
  /* Constants and magic numbers */
  #define MIFARE_CLASSIC_BLOCK_SIZE   (16)
  #define RESET_TOGGLE_SLEEP          (400000)
  #define RESET_BACKOFF               (10000)
  #define HOST_TO_PN532               (0xD4)
  #define PN532_TO_HOST               (0xD5)
  #define SPI_DATA_WRITE              (0x80)
  #define SPI_STATUS_READING          (0x40)
  #define SPI_DATA_READ               (0xC0)
  #define SPI_WRITE_DELAY_US          (2000)
  
  /* SPI bus parameters */
  #define SPI_MODE                    (SPI_MODE_0)
  #define SPI_CLK                     (SPI_CLK_1MHZ)
  
  /* Length for passive listings */
  #define LIST_PASSIVE_LEN_14443(num)   (num * 20)
  
  #if ENABLE_DEBUG
  #define PRINTBUFF printbuff
  static void printbuff(char *buff, unsigned len)
  {
      while (len) {
          len--;
          printf("%02x ", *buff++);
      }
      printf("\n");
  }
  #else
  #define PRINTBUFF(...)
  #endif
  
  static void _nfc_event(void *dev)
  {
      mutex_unlock(&((pn532_t *)dev)->trap);
  }
  
  void pn532_reset(const pn532_t *dev)
  {
      assert(dev != NULL);
  
      DEBUG("pn532: reset\n");
      gpio_clear(dev->conf->reset);
      xtimer_usleep(RESET_TOGGLE_SLEEP);
      gpio_set(dev->conf->reset);
      xtimer_usleep(RESET_BACKOFF);
  }
  
  int pn532_init(pn532_t *dev, const pn532_params_t *params, pn532_mode_t mode)
  {
      assert(dev != NULL);
  
      dev->conf = params;
  
      gpio_init_int(dev->conf->irq, GPIO_IN_PU, GPIO_FALLING,
                    _nfc_event, (void *)dev);
  
      gpio_init(dev->conf->reset, GPIO_OUT);
      gpio_set(dev->conf->reset);
      dev->mode = mode;
      if (mode == PN532_I2C) {
  #ifdef PN532_SUPPORT_I2C
          if (i2c_init_master(dev->conf->i2c, I2C_SPEED_FAST) != 0) {
              DEBUG("pn532: initialization of I2C bus failed\n");
              return -1;
          }
  #endif
      }
      else {
  #ifdef PN532_SUPPORT_SPI
          /* we handle the CS line manually... */
          gpio_init(dev->conf->nss, GPIO_OUT);
          gpio_set(dev->conf->nss);
  #endif
      }
  
      pn532_reset(dev);
  
      mutex_init(&dev->trap);
      mutex_lock(&dev->trap);
  
      return 0;
  }
  
  static unsigned char chksum(char *b, unsigned len)
  {
      unsigned char c = 0x00;
  
      while (len--) {
          c -= *b++;
      }
      return c;
  }
  
  #ifdef PN532_SUPPORT_SPI
  static void reverse(char *buff, unsigned len)
  {
      while (len--) {
          buff[len] = (buff[len] & 0xF0) >> 4 | (buff[len] & 0x0F) << 4;
          buff[len] = (buff[len] & 0xCC) >> 2 | (buff[len] & 0x33) << 2;
          buff[len] = (buff[len] & 0xAA) >> 1 | (buff[len] & 0x55) << 1;
      }
  }
  #endif
  
  static int _write(const pn532_t *dev, char *buff, unsigned len)
  {
      int ret = -1;
  
      (void)buff;
      (void)len;
  
      if (dev->mode == PN532_I2C) {
  #ifdef PN532_SUPPORT_I2C
          i2c_acquire(dev->conf->i2c);
          ret = i2c_write_bytes(dev->conf->i2c, PN532_I2C_ADDRESS, buff, len);
          i2c_release(dev->conf->i2c);
  #endif
      }
      else {
  #ifdef PN532_SUPPORT_SPI
          spi_acquire(dev->conf->spi, SPI_CS_UNDEF, SPI_MODE, SPI_CLK);
          gpio_clear(dev->conf->nss);
          xtimer_usleep(SPI_WRITE_DELAY_US);
          reverse(buff, len);
          spi_transfer_byte(dev->conf->spi, SPI_CS_UNDEF, true, SPI_DATA_WRITE);
          spi_transfer_bytes(dev->conf->spi, SPI_CS_UNDEF, true, buff, NULL, len);
          gpio_set(dev->conf->nss);
          spi_release(dev->conf->spi);
          ret = (int)len;
  #endif
      }
      DEBUG("pn532: -> ");
      PRINTBUFF(buff, len);
      return ret;
  }
  
  static int _read(const pn532_t *dev, char *buff, unsigned len)
  {
      int ret = -1;
  
      (void)buff;
      (void)len;
  
      if (dev->mode == PN532_I2C) {
  #ifdef PN532_SUPPORT_I2C
          i2c_acquire(dev->conf->i2c);
          /* len+1 for RDY after read is accepted */
          ret = i2c_read_bytes(dev->conf->i2c, PN532_I2C_ADDRESS, buff, len + 1);
          i2c_release(dev->conf->i2c);
  #endif
      }
      else {
  #ifdef PN532_SUPPORT_SPI
          spi_acquire(dev->conf->spi, SPI_CS_UNDEF, SPI_MODE, SPI_CLK);
          gpio_clear(dev->conf->nss);
          spi_transfer_byte(dev->conf->spi, SPI_CS_UNDEF, true, SPI_DATA_READ);
          spi_transfer_bytes(dev->conf->spi, SPI_CS_UNDEF, true, NULL, &buff[1], len);
          gpio_set(dev->conf->nss);
          spi_release(dev->conf->spi);
  
          buff[0] = 0x80;
          reverse(buff, len);
          ret = (int)len + 1;
  #endif
      }
      DEBUG("pn532: <- ");
      PRINTBUFF(buff, len);
      return ret;
  }
  
  static int send_cmd(const pn532_t *dev, char *buff, unsigned len)
  {
      unsigned pos, checksum;
  
      buff[0] = 0x00;
      buff[1] = 0x00;
      buff[2] = 0xFF;
  
      len += 1;
      if (len < 0xff) {
          buff[3] = (char)len;
          buff[4] = 0x00 - buff[3];
          pos = 5;
  
      }
      else {
          buff[3] = 0xff;
          buff[4] = 0xff;
          buff[5] = (len >> 8) & 0xff;
          buff[6] = (len) & 0xff;
          buff[7] = 0x00 - buff[5] - buff[6];
          pos = 8;
      }
  
      buff[pos] = HOST_TO_PN532;
      checksum = chksum(&buff[pos], len);
  
      len += pos;
      buff[len++] = checksum;
      buff[len++] = 0x00;
  
      return _write(dev, buff, len);
  }
  
  static void wait_ready(pn532_t *dev)
  {
      mutex_lock(&dev->trap);
  }
  
  /* Returns >0 payload len (or <0 received len but not as expected) */
  static int read_command(const pn532_t *dev, char *buff, unsigned len, int expected_cmd)
  {
      int r;
      unsigned j, fi, lp, lc;
  
      /* apply framing overhead */
      len += 8;
      if (len >= 0xff) {
          len += 3;
      }
  
      r = _read(dev, buff, len);
  
      /* Validate frame structure and CRCs
       *
       * Note that all offsets are shifted by one since the first byte is always
       * 0x01. */
      if ((r < len) || (buff[1] != 0x00) || (buff[2] != 0x00) || (buff[3] != 0xFF)) {
          return -r;
      }
  
      if (buff[4] == 0xff && buff[5] == 0xff) {
          /* extended frame */
          lp = buff[6] << 8 | buff[7];
          lc = (buff[6] + buff[7] + buff[8]) & 0xff;
          fi = 9;
  
      }
      else {
          /* normal frame */
          lp = buff[4];
          lc = (buff[4] + buff[5]) & 0xff;
          fi = 6;
      }
  
      if (lc != 0 || lp >= 265 || buff[fi] != PN532_TO_HOST) {
          return -r;
      }
  
      if (lp == 0) {
          return -r;
      }
  
      if (chksum(&buff[fi], lp) != buff[fi + lp]) {
          return -r;
      }
  
      if (buff[fi + 1] != expected_cmd) {
          return -r;
      }
  
      /* Move the meaningful data to the beginning of the buffer */
      /* start copying after command byte */
      for (j = 0, fi += 2, lp -= 2; j < lp; fi++, j++) {
          buff[j] = buff[fi];
      }
  
      DEBUG("pn532: in cmd ");
      PRINTBUFF(buff, lp);
  
      return lp;
  }
  
  /* Returns 0 if OK, <0 otherwise */
  static int send_check_ack(pn532_t *dev, char *buff, unsigned len)
  {
      if (send_cmd(dev, buff, len) > 0) {
          static char ack[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
  
          wait_ready(dev);
          if (_read(dev, buff, sizeof(ack)) != sizeof(ack) + 1) {
              return -2;
          }
  
          if (memcmp(&buff[1], ack, sizeof(ack)) != 0) {
              return -3;
          }
  
          wait_ready(dev);
          return 0;
      }
  
      return -1;
  }
  
  /* sendl: send length, recvl: receive payload length */
  static int send_rcv(pn532_t *dev, char *buff, unsigned sendl, unsigned recvl)
  {
      assert(dev != NULL);
  
      int expected_cmd = buff[BUFF_CMD_START] + 1;
  
      if (send_check_ack(dev, buff, sendl + 1)) {
          return 0;
      }
  
      recvl += 1; /* cmd response */
      return read_command(dev, buff, recvl, expected_cmd);
  }
  
  int pn532_fw_version(pn532_t *dev, uint32_t *fw_ver)
  {
      unsigned ret = -1;
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START] = CMD_FIRMWARE_VERSION;
  
      if (send_rcv(dev, buff, 0, 4) == 4) {
          *fw_ver =  ((uint32_t)buff[0] << 24);   /* ic version */
          *fw_ver += ((uint32_t)buff[1] << 16);   /* fw ver */
          *fw_ver += ((uint32_t)buff[2] << 8);    /* fw rev */
          *fw_ver += (buff[3]);                   /* feature support */
          ret = 0;
      }
  
      return ret;
  }
  
  int pn532_read_reg(pn532_t *dev, char *out, unsigned addr)
  {
      int ret = -1;
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START     ] = CMD_READ_REG;
      buff[BUFF_DATA_START    ] = (addr >> 8) & 0xff;
      buff[BUFF_DATA_START + 1] = addr & 0xff;
  
      if (send_rcv(dev, buff, 2, 1) == 1) {
          *out = buff[0];
          ret = 0;
      }
  
      return ret;
  }
  
  int pn532_write_reg(pn532_t *dev, unsigned addr, char val)
  {
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START     ] = CMD_WRITE_REG;
      buff[BUFF_DATA_START    ] = (addr >> 8) & 0xff;
      buff[BUFF_DATA_START + 1] = addr & 0xff;
      buff[BUFF_DATA_START + 2] = val;
  
      return send_rcv(dev, buff, 3, 0);
  }
  
  static int _rf_configure(pn532_t *dev, char *buff, unsigned cfg_item, char *config,
                           unsigned cfg_len)
  {
      buff[BUFF_CMD_START ] = CMD_RF_CONFIG;
      buff[BUFF_DATA_START] = cfg_item;
      for (int i = 1; i <= cfg_len; i++) {
          buff[BUFF_DATA_START + i] = *config++;
      }
  
      return send_rcv(dev, buff, cfg_len + 1, 0);
  }
  
  static int _set_act_retries(pn532_t *dev, char *buff, unsigned max_retries)
  {
      char rtrcfg[] = { 0xff, 0x01, max_retries & 0xff };
  
      return _rf_configure(dev, buff, RF_CONFIG_MAX_RETRIES, rtrcfg, sizeof(rtrcfg));
  }
  
  int pn532_sam_configuration(pn532_t *dev, pn532_sam_conf_mode_t mode, unsigned timeout)
  {
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START     ] = CMD_SAM_CONFIG;
      buff[BUFF_DATA_START    ] = (char)mode;
      buff[BUFF_DATA_START + 1] = (char)(timeout / 50);
      buff[BUFF_DATA_START + 2] = 0x01;
  
      return send_rcv(dev, buff, 3, 0);
  }
  
  static int _list_passive_targets(pn532_t *dev, char *buff, pn532_target_t target,
                                   unsigned max, unsigned recvl)
  {
      buff[BUFF_CMD_START] = CMD_LIST_PASSIVE;
      buff[BUFF_DATA_START] = (char) max;
      buff[BUFF_DATA_START + 1] = (char)target;
  
      /* requested len depends on expected target num and type */
      return send_rcv(dev, buff, 2, recvl);
  }
  
  int pn532_get_passive_iso14443a(pn532_t *dev, nfc_iso14443a_t *out,
                                  unsigned max_retries)
  {
      int ret = -1;
      char buff[PN532_BUFFER_LEN];
  
      if (_set_act_retries(dev, buff, max_retries) == 0) {
          ret = _list_passive_targets(dev, buff, PN532_BR_106_ISO_14443_A, 1,
                                      LIST_PASSIVE_LEN_14443(1));
      }
  
      if (ret > 0 && buff[0] > 0) {
          out->target = buff[1];
          out->sns_res = (buff[2] << 8) | buff[3];
          out->sel_res = buff[4];
          out->id_len  = buff[5];
          out->type = ISO14443A_UNKNOWN;
  
          for (int i = 0; i < out->id_len; i++) {
              out->id[i] = buff[6 + i];
          }
  
          /* try to find out the type */
          if (out->id_len == 4) {
              out->type = ISO14443A_MIFARE;
          }
          else if (out->id_len == 7) {
              /* In the case of type 4, the first byte of RATS is the length
               * of RATS including the length itself (6+7) */
              if (buff[13] == ret - 13) {
                  out->type = ISO14443A_TYPE4;
              }
          }
          ret = 0;
      }
      else {
          ret = -1;
      }
  
      return ret;
  }
  
  void pn532_deselect_passive(pn532_t *dev, unsigned target_id)
  {
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START ] = CMD_DESELECT;
      buff[BUFF_DATA_START] = target_id;
  
      send_rcv(dev, buff, 1, 1);
  }
  
  void pn532_release_passive(pn532_t *dev, unsigned target_id)
  {
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START ] = CMD_RELEASE;
      buff[BUFF_DATA_START] = target_id;
  
      send_rcv(dev, buff, 1, 1);
  }
  
  int pn532_mifareclassic_authenticate(pn532_t *dev, nfc_iso14443a_t *card,
                                       pn532_mifare_key_t keyid, char *key, unsigned block)
  {
      int ret = -1;
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START     ] = CMD_DATA_EXCHANGE;
      buff[BUFF_DATA_START    ] = card->target;
      buff[BUFF_DATA_START + 1] = keyid;
      buff[BUFF_DATA_START + 2] = block; /* current block */
  
      /*
       * The card ID directly follows the key in the buffer
       * The key consists of 6 bytes and starts at offset 3
       */
      for (int i = 0; i < 6; i++) {
          buff[BUFF_DATA_START + 3 + i] = key[i];
      }
  
      for (int i = 0; i < card->id_len; i++) {
          buff[BUFF_DATA_START + 9 + i] = card->id[i];
      }
  
      ret = send_rcv(dev, buff, 9 + card->id_len, 1);
      if (ret == 1) {
          ret = buff[0];
          card->auth = 1;
      }
  
      return ret;
  }
  
  int pn532_mifareclassic_write(pn532_t *dev, char *idata, nfc_iso14443a_t *card,
                                unsigned block)
  {
      int ret = -1;
      char buff[PN532_BUFFER_LEN];
  
      if (card->auth) {
  
          buff[BUFF_CMD_START     ] = CMD_DATA_EXCHANGE;
          buff[BUFF_DATA_START    ] = card->target;
          buff[BUFF_DATA_START + 1] = MIFARE_CMD_WRITE;
          buff[BUFF_DATA_START + 2] = block; /* current block */
          memcpy(&buff[BUFF_DATA_START + 3], idata, MIFARE_CLASSIC_BLOCK_SIZE);
  
          if (send_rcv(dev, buff, 19, 1) == 1) {
              ret = buff[0];
          }
  
      }
      return ret;
  }
  
  static int pn532_mifare_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card,
                               unsigned block, unsigned len)
  {
      int ret = -1;
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START     ] = CMD_DATA_EXCHANGE;
      buff[BUFF_DATA_START    ] = card->target;
      buff[BUFF_DATA_START + 1] = MIFARE_CMD_READ;
      buff[BUFF_DATA_START + 2] = block; /* current block */
  
      if (send_rcv(dev, buff, 3, len + 1) == len + 1) {
          memcpy(odata, &buff[1], len);
          ret = 0;
      }
  
      return ret;
  }
  
  int pn532_mifareclassic_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card,
                               unsigned block)
  {
      if (card->auth) {
          return pn532_mifare_read(dev, odata, card, block, MIFARE_CLASSIC_BLOCK_SIZE);
      }
      else {
          return -1;
      }
  }
  
  int pn532_mifareulight_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card,
                              unsigned page)
  {
      return pn532_mifare_read(dev, odata, card, page, 32);
  }
  
  static int send_rcv_apdu(pn532_t *dev, char *buff, unsigned slen, unsigned rlen)
  {
      int ret;
  
      rlen += 3;
      if (rlen >= RAPDU_MAX_DATA_LEN) {
          return -1;
  
      }
      else if (slen >= CAPDU_MAX_DATA_LEN) {
          return -1;
      }
  
      ret = send_rcv(dev, buff, slen, rlen);
      if (ret == rlen && buff[0] == 0x00) {
          ret = (buff[rlen - 2] << 8) | buff[rlen - 1];
          if (ret == 0x9000) {
              ret = 0;
          }
      }
  
      return ret;
  }
  
  int pn532_iso14443a_4_activate(pn532_t *dev, nfc_iso14443a_t *card)
  {
      int ret;
      char buff[PN532_BUFFER_LEN];
  
      /* select app ndef tag */
      buff[BUFF_CMD_START      ] = CMD_DATA_EXCHANGE;
      buff[BUFF_DATA_START     ] = card->target;
      buff[BUFF_DATA_START +  1] = 0x00;
      buff[BUFF_DATA_START +  2] = 0xa4;
      buff[BUFF_DATA_START +  3] = 0x04;
      buff[BUFF_DATA_START +  4] = 0x00;
      buff[BUFF_DATA_START +  5] = 0x07;
      buff[BUFF_DATA_START +  6] = 0xD2;
      buff[BUFF_DATA_START +  7] = 0x76;
      buff[BUFF_DATA_START +  8] = 0x00;
      buff[BUFF_DATA_START +  9] = 0x00;
      buff[BUFF_DATA_START + 10] = 0x85;
      buff[BUFF_DATA_START + 11] = 0x01;
      buff[BUFF_DATA_START + 12] = 0x01;
      buff[BUFF_DATA_START + 13] = 0x00;
  
      DEBUG("pn532: select app\n");
      ret = send_rcv_apdu(dev, buff, 14, 0);
  
      /* select ndef file */
      buff[BUFF_CMD_START     ] = CMD_DATA_EXCHANGE;
      buff[BUFF_DATA_START    ] = card->target;
      buff[BUFF_DATA_START + 1] = 0x00;
      buff[BUFF_DATA_START + 2] = 0xa4;
      buff[BUFF_DATA_START + 3] = 0x00;
      buff[BUFF_DATA_START + 4] = 0x0c;
      buff[BUFF_DATA_START + 5] = 0x02;
      buff[BUFF_DATA_START + 6] = 0x00;
      buff[BUFF_DATA_START + 7] = 0x01;
  
      if (ret == 0) {
          DEBUG("pn532: select file\n");
          ret = send_rcv_apdu(dev, buff, 8, 0);
      }
  
      return ret;
  }
  
  int pn532_iso14443a_4_read(pn532_t *dev, char *odata, nfc_iso14443a_t *card,
                             unsigned offset, char len)
  {
      int ret;
      char buff[PN532_BUFFER_LEN];
  
      buff[BUFF_CMD_START     ] = CMD_DATA_EXCHANGE;
      buff[BUFF_DATA_START    ] = card->target;
      buff[BUFF_DATA_START + 1] = 0x00;
      buff[BUFF_DATA_START + 2] = 0xb0;
      buff[BUFF_DATA_START + 3] = (offset >> 8) & 0xff;
      buff[BUFF_DATA_START + 4] = offset & 0xff;
      buff[BUFF_DATA_START + 5] = len;
  
      ret = send_rcv_apdu(dev, buff, 6, len);
      if (ret == 0) {
          memcpy(odata, &buff[RAPDU_DATA_BEGIN], len);
      }
  
      return ret;
  }