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.

Dependents:   PN532_ReadUid Nfctest2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers snep.cpp Source File

snep.cpp

00001 
00002 #include "snep.h"
00003 #include "PN532_debug.h"
00004 
00005 int8_t SNEP::write(const uint8_t *buf, uint8_t len, uint16_t timeout)
00006 {
00007     if (0 >= llcp.activate(timeout)) {
00008         DMSG("failed to activate PN532 as a target\n");
00009         return -1;
00010     }
00011 
00012     if (0 >= llcp.connect(timeout)) {
00013         DMSG("failed to set up a connection\n");
00014         return -2;
00015     }
00016 
00017     // response a success SNEP message
00018     headerBuf[0] = SNEP_DEFAULT_VERSION;
00019     headerBuf[1] = SNEP_REQUEST_PUT;
00020     headerBuf[2] = 0;
00021     headerBuf[3] = 0;
00022     headerBuf[4] = 0;
00023     headerBuf[5] = len;
00024     if (0 >= llcp.write(headerBuf, 6, buf, len)) {
00025         return -3;
00026     }
00027 
00028     uint8_t rbuf[16];
00029     if (6 > llcp.read(rbuf, sizeof(rbuf))) {
00030         return -4;
00031     }
00032 
00033     // check SNEP version
00034     if (SNEP_DEFAULT_VERSION != rbuf[0]) {
00035         DMSG("The received SNEP message's major version is different, me: ");
00036         DMSG_HEX(SNEP_DEFAULT_VERSION);
00037         DMSG(", their: ");
00038         DMSG_HEX(rbuf[0]);
00039         DMSG("\n");
00040         // To-do: send Unsupported Version response
00041         return -4;
00042     }
00043 
00044     // expect a put request
00045     if (SNEP_RESPONSE_SUCCESS != rbuf[1]) {
00046         DMSG("Expect a success response\n");
00047         return -4;
00048     }
00049 
00050     llcp.disconnect(timeout);
00051 
00052     return 1;
00053 }
00054 
00055 int16_t SNEP::read(uint8_t *buf, uint8_t len, uint16_t timeout)
00056 {
00057     if (0 >= llcp.activate(timeout)) {
00058         DMSG("failed to activate PN532 as a target\n");
00059         return -1;
00060     }
00061 
00062     if (0 >= llcp.waitForConnection(timeout)) {
00063         DMSG("failed to set up a connection\n");
00064         return -2;
00065     }
00066 
00067     uint16_t status = llcp.read(buf, len);
00068     if (6 > status) {
00069         return -3;
00070     }
00071 
00072     // check SNEP version
00073     
00074     // in case of platform specific bug, shift SNEP message for 4 bytes.
00075     // tested on Nexus 5, Android 5.1
00076     if (SNEP_DEFAULT_VERSION != buf[0] && SNEP_DEFAULT_VERSION == buf[4]) {
00077         for (uint8_t i = 0; i < len - 4; i++) {
00078             buf[i] = buf[i + 4];
00079         }
00080     }
00081     
00082     if (SNEP_DEFAULT_VERSION != buf[0]) {
00083         DMSG("SNEP->read: The received SNEP message's major version is different, me: ");
00084         DMSG_HEX(SNEP_DEFAULT_VERSION);
00085         DMSG(", their: ");
00086         DMSG_HEX(buf[0]);
00087         DMSG("\n");
00088         // To-do: send Unsupported Version response
00089         return -4;
00090     }
00091 
00092     // expect a put request
00093     if (SNEP_REQUEST_PUT != buf[1]) {
00094         DMSG("Expect a put request\n");
00095         return -4;
00096     }
00097 
00098     // check message's length
00099     uint32_t length = (buf[2] << 24) + (buf[3] << 16) + (buf[4] << 8) + buf[5];
00100     // length should not be more than 244 (header + body < 255, header = 6 + 3 + 2)
00101     if (length > (status - 6)) {
00102         DMSG("The SNEP message is too large: "); 
00103         DMSG_INT(length);
00104         DMSG_INT(status - 6);
00105         DMSG("\n");
00106         return -4;
00107     }
00108     for (uint8_t i = 0; i < length; i++) {
00109         buf[i] = buf[i + 6];
00110     }
00111 
00112     // response a success SNEP message
00113     headerBuf[0] = SNEP_DEFAULT_VERSION;
00114     headerBuf[1] = SNEP_RESPONSE_SUCCESS;
00115     headerBuf[2] = 0;
00116     headerBuf[3] = 0;
00117     headerBuf[4] = 0;
00118     headerBuf[5] = 0;
00119     llcp.write(headerBuf, 6);
00120 
00121     return length;
00122 }