PN532 Driver library This library provides an abstract API to drive the pn532 nfc chip, with I2C/HSU/SPI interface. Its based on the Seeed Studio's Arduino version.
PN532/snep.cpp@2:ae258f195bbd, 2020-06-26 (annotated)
- Committer:
- maximemau
- Date:
- Fri Jun 26 09:16:19 2020 +0000
- Revision:
- 2:ae258f195bbd
- Parent:
- 0:db8030e71f55
lib_pn532
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dotnfc | 0:db8030e71f55 | 1 | |
dotnfc | 0:db8030e71f55 | 2 | #include "snep.h" |
dotnfc | 0:db8030e71f55 | 3 | #include "PN532_debug.h" |
dotnfc | 0:db8030e71f55 | 4 | |
dotnfc | 0:db8030e71f55 | 5 | int8_t SNEP::write(const uint8_t *buf, uint8_t len, uint16_t timeout) |
dotnfc | 0:db8030e71f55 | 6 | { |
dotnfc | 0:db8030e71f55 | 7 | if (0 >= llcp.activate(timeout)) { |
dotnfc | 0:db8030e71f55 | 8 | DMSG("failed to activate PN532 as a target\n"); |
dotnfc | 0:db8030e71f55 | 9 | return -1; |
dotnfc | 0:db8030e71f55 | 10 | } |
dotnfc | 0:db8030e71f55 | 11 | |
dotnfc | 0:db8030e71f55 | 12 | if (0 >= llcp.connect(timeout)) { |
dotnfc | 0:db8030e71f55 | 13 | DMSG("failed to set up a connection\n"); |
dotnfc | 0:db8030e71f55 | 14 | return -2; |
dotnfc | 0:db8030e71f55 | 15 | } |
dotnfc | 0:db8030e71f55 | 16 | |
dotnfc | 0:db8030e71f55 | 17 | // response a success SNEP message |
dotnfc | 0:db8030e71f55 | 18 | headerBuf[0] = SNEP_DEFAULT_VERSION; |
dotnfc | 0:db8030e71f55 | 19 | headerBuf[1] = SNEP_REQUEST_PUT; |
dotnfc | 0:db8030e71f55 | 20 | headerBuf[2] = 0; |
dotnfc | 0:db8030e71f55 | 21 | headerBuf[3] = 0; |
dotnfc | 0:db8030e71f55 | 22 | headerBuf[4] = 0; |
dotnfc | 0:db8030e71f55 | 23 | headerBuf[5] = len; |
dotnfc | 0:db8030e71f55 | 24 | if (0 >= llcp.write(headerBuf, 6, buf, len)) { |
dotnfc | 0:db8030e71f55 | 25 | return -3; |
dotnfc | 0:db8030e71f55 | 26 | } |
dotnfc | 0:db8030e71f55 | 27 | |
dotnfc | 0:db8030e71f55 | 28 | uint8_t rbuf[16]; |
dotnfc | 0:db8030e71f55 | 29 | if (6 > llcp.read(rbuf, sizeof(rbuf))) { |
dotnfc | 0:db8030e71f55 | 30 | return -4; |
dotnfc | 0:db8030e71f55 | 31 | } |
dotnfc | 0:db8030e71f55 | 32 | |
dotnfc | 0:db8030e71f55 | 33 | // check SNEP version |
dotnfc | 0:db8030e71f55 | 34 | if (SNEP_DEFAULT_VERSION != rbuf[0]) { |
dotnfc | 0:db8030e71f55 | 35 | DMSG("The received SNEP message's major version is different, me: "); |
dotnfc | 0:db8030e71f55 | 36 | DMSG_HEX(SNEP_DEFAULT_VERSION); |
dotnfc | 0:db8030e71f55 | 37 | DMSG(", their: "); |
dotnfc | 0:db8030e71f55 | 38 | DMSG_HEX(rbuf[0]); |
dotnfc | 0:db8030e71f55 | 39 | DMSG("\n"); |
dotnfc | 0:db8030e71f55 | 40 | // To-do: send Unsupported Version response |
dotnfc | 0:db8030e71f55 | 41 | return -4; |
dotnfc | 0:db8030e71f55 | 42 | } |
dotnfc | 0:db8030e71f55 | 43 | |
dotnfc | 0:db8030e71f55 | 44 | // expect a put request |
dotnfc | 0:db8030e71f55 | 45 | if (SNEP_RESPONSE_SUCCESS != rbuf[1]) { |
dotnfc | 0:db8030e71f55 | 46 | DMSG("Expect a success response\n"); |
dotnfc | 0:db8030e71f55 | 47 | return -4; |
dotnfc | 0:db8030e71f55 | 48 | } |
dotnfc | 0:db8030e71f55 | 49 | |
dotnfc | 0:db8030e71f55 | 50 | llcp.disconnect(timeout); |
dotnfc | 0:db8030e71f55 | 51 | |
dotnfc | 0:db8030e71f55 | 52 | return 1; |
dotnfc | 0:db8030e71f55 | 53 | } |
dotnfc | 0:db8030e71f55 | 54 | |
dotnfc | 0:db8030e71f55 | 55 | int16_t SNEP::read(uint8_t *buf, uint8_t len, uint16_t timeout) |
dotnfc | 0:db8030e71f55 | 56 | { |
dotnfc | 0:db8030e71f55 | 57 | if (0 >= llcp.activate(timeout)) { |
dotnfc | 0:db8030e71f55 | 58 | DMSG("failed to activate PN532 as a target\n"); |
dotnfc | 0:db8030e71f55 | 59 | return -1; |
dotnfc | 0:db8030e71f55 | 60 | } |
dotnfc | 0:db8030e71f55 | 61 | |
dotnfc | 0:db8030e71f55 | 62 | if (0 >= llcp.waitForConnection(timeout)) { |
dotnfc | 0:db8030e71f55 | 63 | DMSG("failed to set up a connection\n"); |
dotnfc | 0:db8030e71f55 | 64 | return -2; |
dotnfc | 0:db8030e71f55 | 65 | } |
dotnfc | 0:db8030e71f55 | 66 | |
dotnfc | 0:db8030e71f55 | 67 | uint16_t status = llcp.read(buf, len); |
dotnfc | 0:db8030e71f55 | 68 | if (6 > status) { |
dotnfc | 0:db8030e71f55 | 69 | return -3; |
dotnfc | 0:db8030e71f55 | 70 | } |
dotnfc | 0:db8030e71f55 | 71 | |
dotnfc | 0:db8030e71f55 | 72 | // check SNEP version |
dotnfc | 0:db8030e71f55 | 73 | |
dotnfc | 0:db8030e71f55 | 74 | // in case of platform specific bug, shift SNEP message for 4 bytes. |
dotnfc | 0:db8030e71f55 | 75 | // tested on Nexus 5, Android 5.1 |
dotnfc | 0:db8030e71f55 | 76 | if (SNEP_DEFAULT_VERSION != buf[0] && SNEP_DEFAULT_VERSION == buf[4]) { |
dotnfc | 0:db8030e71f55 | 77 | for (uint8_t i = 0; i < len - 4; i++) { |
dotnfc | 0:db8030e71f55 | 78 | buf[i] = buf[i + 4]; |
dotnfc | 0:db8030e71f55 | 79 | } |
dotnfc | 0:db8030e71f55 | 80 | } |
dotnfc | 0:db8030e71f55 | 81 | |
dotnfc | 0:db8030e71f55 | 82 | if (SNEP_DEFAULT_VERSION != buf[0]) { |
dotnfc | 0:db8030e71f55 | 83 | DMSG("SNEP->read: The received SNEP message's major version is different, me: "); |
dotnfc | 0:db8030e71f55 | 84 | DMSG_HEX(SNEP_DEFAULT_VERSION); |
dotnfc | 0:db8030e71f55 | 85 | DMSG(", their: "); |
dotnfc | 0:db8030e71f55 | 86 | DMSG_HEX(buf[0]); |
dotnfc | 0:db8030e71f55 | 87 | DMSG("\n"); |
dotnfc | 0:db8030e71f55 | 88 | // To-do: send Unsupported Version response |
dotnfc | 0:db8030e71f55 | 89 | return -4; |
dotnfc | 0:db8030e71f55 | 90 | } |
dotnfc | 0:db8030e71f55 | 91 | |
dotnfc | 0:db8030e71f55 | 92 | // expect a put request |
dotnfc | 0:db8030e71f55 | 93 | if (SNEP_REQUEST_PUT != buf[1]) { |
dotnfc | 0:db8030e71f55 | 94 | DMSG("Expect a put request\n"); |
dotnfc | 0:db8030e71f55 | 95 | return -4; |
dotnfc | 0:db8030e71f55 | 96 | } |
dotnfc | 0:db8030e71f55 | 97 | |
dotnfc | 0:db8030e71f55 | 98 | // check message's length |
dotnfc | 0:db8030e71f55 | 99 | uint32_t length = (buf[2] << 24) + (buf[3] << 16) + (buf[4] << 8) + buf[5]; |
dotnfc | 0:db8030e71f55 | 100 | // length should not be more than 244 (header + body < 255, header = 6 + 3 + 2) |
dotnfc | 0:db8030e71f55 | 101 | if (length > (status - 6)) { |
dotnfc | 0:db8030e71f55 | 102 | DMSG("The SNEP message is too large: "); |
dotnfc | 0:db8030e71f55 | 103 | DMSG_INT(length); |
dotnfc | 0:db8030e71f55 | 104 | DMSG_INT(status - 6); |
dotnfc | 0:db8030e71f55 | 105 | DMSG("\n"); |
dotnfc | 0:db8030e71f55 | 106 | return -4; |
dotnfc | 0:db8030e71f55 | 107 | } |
dotnfc | 0:db8030e71f55 | 108 | for (uint8_t i = 0; i < length; i++) { |
dotnfc | 0:db8030e71f55 | 109 | buf[i] = buf[i + 6]; |
dotnfc | 0:db8030e71f55 | 110 | } |
dotnfc | 0:db8030e71f55 | 111 | |
dotnfc | 0:db8030e71f55 | 112 | // response a success SNEP message |
dotnfc | 0:db8030e71f55 | 113 | headerBuf[0] = SNEP_DEFAULT_VERSION; |
dotnfc | 0:db8030e71f55 | 114 | headerBuf[1] = SNEP_RESPONSE_SUCCESS; |
dotnfc | 0:db8030e71f55 | 115 | headerBuf[2] = 0; |
dotnfc | 0:db8030e71f55 | 116 | headerBuf[3] = 0; |
dotnfc | 0:db8030e71f55 | 117 | headerBuf[4] = 0; |
dotnfc | 0:db8030e71f55 | 118 | headerBuf[5] = 0; |
dotnfc | 0:db8030e71f55 | 119 | llcp.write(headerBuf, 6); |
dotnfc | 0:db8030e71f55 | 120 | |
dotnfc | 0:db8030e71f55 | 121 | return length; |
dotnfc | 0:db8030e71f55 | 122 | } |