PN532 NFC library for Seeed Studio's NFC Shield

Fork of PN532 by Yihui Xiong

Committer:
yihui
Date:
Thu Nov 21 04:30:49 2013 +0000
Revision:
3:4189a10038e6
sync with https://github.com/Seeed-Studio/PN532/releases/tag/v0.9.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 3:4189a10038e6 1
yihui 3:4189a10038e6 2 #include "snep.h"
yihui 3:4189a10038e6 3 #include "PN532_debug.h"
yihui 3:4189a10038e6 4
yihui 3:4189a10038e6 5 int8_t SNEP::write(const uint8_t *buf, uint8_t len, uint16_t timeout)
yihui 3:4189a10038e6 6 {
yihui 3:4189a10038e6 7 if (0 >= llcp.activate(timeout)) {
yihui 3:4189a10038e6 8 DMSG("failed to activate PN532 as a target\n");
yihui 3:4189a10038e6 9 return -1;
yihui 3:4189a10038e6 10 }
yihui 3:4189a10038e6 11
yihui 3:4189a10038e6 12 if (0 >= llcp.connect(timeout)) {
yihui 3:4189a10038e6 13 DMSG("failed to set up a connection\n");
yihui 3:4189a10038e6 14 return -2;
yihui 3:4189a10038e6 15 }
yihui 3:4189a10038e6 16
yihui 3:4189a10038e6 17 // response a success SNEP message
yihui 3:4189a10038e6 18 headerBuf[0] = SNEP_DEFAULT_VERSION;
yihui 3:4189a10038e6 19 headerBuf[1] = SNEP_REQUEST_PUT;
yihui 3:4189a10038e6 20 headerBuf[2] = 0;
yihui 3:4189a10038e6 21 headerBuf[3] = 0;
yihui 3:4189a10038e6 22 headerBuf[4] = 0;
yihui 3:4189a10038e6 23 headerBuf[5] = len;
yihui 3:4189a10038e6 24 if (0 >= llcp.write(headerBuf, 6, buf, len)) {
yihui 3:4189a10038e6 25 return -3;
yihui 3:4189a10038e6 26 }
yihui 3:4189a10038e6 27
yihui 3:4189a10038e6 28 uint8_t rbuf[16];
yihui 3:4189a10038e6 29 if (6 > llcp.read(rbuf, sizeof(rbuf))) {
yihui 3:4189a10038e6 30 return -4;
yihui 3:4189a10038e6 31 }
yihui 3:4189a10038e6 32
yihui 3:4189a10038e6 33 // check SNEP version
yihui 3:4189a10038e6 34 if (SNEP_DEFAULT_VERSION != rbuf[0]) {
yihui 3:4189a10038e6 35 DMSG("The received SNEP message's major version is different\n");
yihui 3:4189a10038e6 36 // To-do: send Unsupported Version response
yihui 3:4189a10038e6 37 return -4;
yihui 3:4189a10038e6 38 }
yihui 3:4189a10038e6 39
yihui 3:4189a10038e6 40 // expect a put request
yihui 3:4189a10038e6 41 if (SNEP_RESPONSE_SUCCESS != rbuf[1]) {
yihui 3:4189a10038e6 42 DMSG("Expect a success response\n");
yihui 3:4189a10038e6 43 return -4;
yihui 3:4189a10038e6 44 }
yihui 3:4189a10038e6 45
yihui 3:4189a10038e6 46 llcp.disconnect(timeout);
yihui 3:4189a10038e6 47
yihui 3:4189a10038e6 48 return 1;
yihui 3:4189a10038e6 49 }
yihui 3:4189a10038e6 50
yihui 3:4189a10038e6 51 int16_t SNEP::read(uint8_t *buf, uint8_t len, uint16_t timeout)
yihui 3:4189a10038e6 52 {
yihui 3:4189a10038e6 53 if (0 >= llcp.activate(timeout)) {
yihui 3:4189a10038e6 54 DMSG("failed to activate PN532 as a target\n");
yihui 3:4189a10038e6 55 return -1;
yihui 3:4189a10038e6 56 }
yihui 3:4189a10038e6 57
yihui 3:4189a10038e6 58 if (0 >= llcp.waitForConnection(timeout)) {
yihui 3:4189a10038e6 59 DMSG("failed to set up a connection\n");
yihui 3:4189a10038e6 60 return -2;
yihui 3:4189a10038e6 61 }
yihui 3:4189a10038e6 62
yihui 3:4189a10038e6 63 uint16_t status = llcp.read(buf, len);
yihui 3:4189a10038e6 64 if (6 > status) {
yihui 3:4189a10038e6 65 return -3;
yihui 3:4189a10038e6 66 }
yihui 3:4189a10038e6 67
yihui 3:4189a10038e6 68
yihui 3:4189a10038e6 69 // check SNEP version
yihui 3:4189a10038e6 70 if (SNEP_DEFAULT_VERSION != buf[0]) {
yihui 3:4189a10038e6 71 DMSG("The received SNEP message's major version is different\n");
yihui 3:4189a10038e6 72 // To-do: send Unsupported Version response
yihui 3:4189a10038e6 73 return -4;
yihui 3:4189a10038e6 74 }
yihui 3:4189a10038e6 75
yihui 3:4189a10038e6 76 // expect a put request
yihui 3:4189a10038e6 77 if (SNEP_REQUEST_PUT != buf[1]) {
yihui 3:4189a10038e6 78 DMSG("Expect a put request\n");
yihui 3:4189a10038e6 79 return -4;
yihui 3:4189a10038e6 80 }
yihui 3:4189a10038e6 81
yihui 3:4189a10038e6 82 // check message's length
yihui 3:4189a10038e6 83 uint32_t length = (buf[2] << 24) + (buf[3] << 16) + (buf[4] << 8) + buf[5];
yihui 3:4189a10038e6 84 // length should not be more than 244 (header + body < 255, header = 6 + 3 + 2)
yihui 3:4189a10038e6 85 if (length > (status - 6)) {
yihui 3:4189a10038e6 86 DMSG("The SNEP message is too large: ");
yihui 3:4189a10038e6 87 DMSG_INT(length);
yihui 3:4189a10038e6 88 DMSG_INT(status - 6);
yihui 3:4189a10038e6 89 DMSG("\n");
yihui 3:4189a10038e6 90 return -4;
yihui 3:4189a10038e6 91 }
yihui 3:4189a10038e6 92 for (uint8_t i = 0; i < length; i++) {
yihui 3:4189a10038e6 93 buf[i] = buf[i + 6];
yihui 3:4189a10038e6 94 }
yihui 3:4189a10038e6 95
yihui 3:4189a10038e6 96 // response a success SNEP message
yihui 3:4189a10038e6 97 headerBuf[0] = SNEP_DEFAULT_VERSION;
yihui 3:4189a10038e6 98 headerBuf[1] = SNEP_RESPONSE_SUCCESS;
yihui 3:4189a10038e6 99 headerBuf[2] = 0;
yihui 3:4189a10038e6 100 headerBuf[3] = 0;
yihui 3:4189a10038e6 101 headerBuf[4] = 0;
yihui 3:4189a10038e6 102 headerBuf[5] = 0;
yihui 3:4189a10038e6 103 llcp.write(headerBuf, 6);
yihui 3:4189a10038e6 104
yihui 3:4189a10038e6 105 return length;
yihui 3:4189a10038e6 106 }