mirror of
https://github.com/Sucareto/ESP32-CardReader.git
synced 2024-11-27 17:00:57 +01:00
复制库,删除未使用函数:
https://github.com/elechouse/PN532 https://github.com/spicetools/spicetools
This commit is contained in:
parent
abaf6d5fe3
commit
7a279737d3
@ -15,8 +15,7 @@
|
||||
|
||||
#define HAL(func) (_interface->func)
|
||||
|
||||
PN532::PN532(PN532Interface &interface)
|
||||
{
|
||||
PN532::PN532(PN532Interface &interface) {
|
||||
_interface = &interface;
|
||||
}
|
||||
|
||||
@ -25,10 +24,11 @@ PN532::PN532(PN532Interface &interface)
|
||||
@brief Setups the HW
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void PN532::begin()
|
||||
{
|
||||
HAL(begin)();
|
||||
HAL(wakeup)();
|
||||
void PN532::begin() {
|
||||
HAL(begin)
|
||||
();
|
||||
HAL(wakeup)
|
||||
();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
@ -38,8 +38,7 @@ void PN532::begin()
|
||||
@returns The chip's firmware version and ID
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint32_t PN532::getFirmwareVersion(void)
|
||||
{
|
||||
uint32_t PN532::getFirmwareVersion(void) {
|
||||
uint32_t response;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION;
|
||||
@ -75,8 +74,7 @@ uint32_t PN532::getFirmwareVersion(void)
|
||||
@returns The register value.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint32_t PN532::readRegister(uint16_t reg)
|
||||
{
|
||||
uint32_t PN532::readRegister(uint16_t reg) {
|
||||
uint32_t response;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_READREGISTER;
|
||||
@ -108,8 +106,7 @@ uint32_t PN532::readRegister(uint16_t reg)
|
||||
@returns 0 for failure, 1 for success.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint32_t PN532::writeRegister(uint16_t reg, uint8_t val)
|
||||
{
|
||||
uint32_t PN532::writeRegister(uint16_t reg, uint8_t val) {
|
||||
uint32_t response;
|
||||
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_WRITEREGISTER;
|
||||
@ -136,8 +133,7 @@ uint32_t PN532::writeRegister(uint16_t reg, uint8_t val)
|
||||
@brief Configures the SAM (Secure Access Module)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::SAMConfig(void)
|
||||
{
|
||||
bool PN532::SAMConfig(void) {
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION;
|
||||
pn532_packetbuffer[1] = 0x01; // normal mode;
|
||||
pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second
|
||||
@ -159,8 +155,7 @@ bool PN532::SAMConfig(void)
|
||||
@returns 1 if everything executed properly, 0 for an error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::setPassiveActivationRetries(uint8_t maxRetries)
|
||||
{
|
||||
bool PN532::setPassiveActivationRetries(uint8_t maxRetries) {
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION;
|
||||
pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries)
|
||||
pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF)
|
||||
@ -190,8 +185,7 @@ bool PN532::setPassiveActivationRetries(uint8_t maxRetries)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
bool PN532::setRFField(uint8_t autoRFCA, uint8_t rFOnOff)
|
||||
{
|
||||
bool PN532::setRFField(uint8_t autoRFCA, uint8_t rFOnOff) {
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION;
|
||||
pn532_packetbuffer[1] = 1;
|
||||
pn532_packetbuffer[2] = 0x00 | autoRFCA | rFOnOff;
|
||||
@ -218,8 +212,7 @@ bool PN532::setRFField(uint8_t autoRFCA, uint8_t rFOnOff)
|
||||
@returns 1 if everything executed properly, 0 for an error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout)
|
||||
{
|
||||
bool PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout) {
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
|
||||
pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later)
|
||||
pn532_packetbuffer[2] = cardbaudrate;
|
||||
@ -272,8 +265,7 @@ bool PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uid
|
||||
in the sector (block 0 relative to the current sector)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::mifareclassic_IsFirstBlock (uint32_t uiBlock)
|
||||
{
|
||||
bool PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) {
|
||||
// Test if we are in the small or big sectors
|
||||
if (uiBlock < 128)
|
||||
return ((uiBlock) % 4 == 0);
|
||||
@ -286,8 +278,7 @@ bool PN532::mifareclassic_IsFirstBlock (uint32_t uiBlock)
|
||||
Indicates whether the specified block number is the sector trailer
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::mifareclassic_IsTrailerBlock (uint32_t uiBlock)
|
||||
{
|
||||
bool PN532::mifareclassic_IsTrailerBlock(uint32_t uiBlock) {
|
||||
// Test if we are in the small or big sectors
|
||||
if (uiBlock < 128)
|
||||
return ((uiBlock + 1) % 4 == 0);
|
||||
@ -314,8 +305,7 @@ bool PN532::mifareclassic_IsTrailerBlock (uint32_t uiBlock)
|
||||
@returns 1 if everything executed properly, 0 for an error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t PN532::mifareclassic_AuthenticateBlock (uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData)
|
||||
{
|
||||
uint8_t PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData) {
|
||||
uint8_t i;
|
||||
|
||||
// Hang on to the key and uid data
|
||||
@ -337,7 +327,8 @@ uint8_t PN532::mifareclassic_AuthenticateBlock (uint8_t *uid, uint8_t uidLen, ui
|
||||
return 0;
|
||||
|
||||
// Read the response packet
|
||||
HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
|
||||
HAL(readResponse)
|
||||
(pn532_packetbuffer, sizeof(pn532_packetbuffer));
|
||||
|
||||
// Check if the response is valid and we are authenticated???
|
||||
// for an auth success it should be bytes 5-7: 0xD5 0x41 0x00
|
||||
@ -362,8 +353,7 @@ uint8_t PN532::mifareclassic_AuthenticateBlock (uint8_t *uid, uint8_t uidLen, ui
|
||||
@returns 1 if everything executed properly, 0 for an error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t PN532::mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t *data)
|
||||
{
|
||||
uint8_t PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data) {
|
||||
|
||||
/* Prepare the command */
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
||||
@ -377,7 +367,8 @@ uint8_t PN532::mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t *data)
|
||||
}
|
||||
|
||||
/* Read the response packet */
|
||||
HAL(readResponse)(pn532_packetbuffer, sizeof(pn532_packetbuffer));
|
||||
HAL(readResponse)
|
||||
(pn532_packetbuffer, sizeof(pn532_packetbuffer));
|
||||
|
||||
/* If byte 8 isn't 0x00 we probably have an error */
|
||||
if (pn532_packetbuffer[0] != 0x00) {
|
||||
@ -403,8 +394,7 @@ uint8_t PN532::mifareclassic_ReadDataBlock (uint8_t blockNumber, uint8_t *data)
|
||||
@returns 1 if everything executed properly, 0 for an error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
uint8_t PN532::mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t *data)
|
||||
{
|
||||
uint8_t PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data) {
|
||||
/* Prepare the first command */
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE;
|
||||
pn532_packetbuffer[1] = 1; /* Card number */
|
||||
@ -431,8 +421,7 @@ uint8_t PN532::mifareclassic_WriteDataBlock (uint8_t blockNumber, uint8_t *data)
|
||||
@param responseLength Pointer to the response data length
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength)
|
||||
{
|
||||
bool PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength) {
|
||||
uint8_t i;
|
||||
|
||||
pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
|
||||
@ -472,8 +461,7 @@ bool PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response,
|
||||
peer acting as card/responder.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
bool PN532::inListPassiveTarget()
|
||||
{
|
||||
bool PN532::inListPassiveTarget() {
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
|
||||
pn532_packetbuffer[1] = 1;
|
||||
pn532_packetbuffer[2] = 0;
|
||||
@ -516,8 +504,7 @@ int8_t PN532::tgInitAsTarget(const uint8_t* command, const uint8_t len, const ui
|
||||
/**
|
||||
* Peer to Peer
|
||||
*/
|
||||
int8_t PN532::tgInitAsTarget(uint16_t timeout)
|
||||
{
|
||||
int8_t PN532::tgInitAsTarget(uint16_t timeout) {
|
||||
const uint8_t command[] = {
|
||||
PN532_COMMAND_TGINITASTARGET,
|
||||
0,
|
||||
@ -536,8 +523,7 @@ int8_t PN532::tgInitAsTarget(uint16_t timeout)
|
||||
return tgInitAsTarget(command, sizeof(command), timeout);
|
||||
}
|
||||
|
||||
int16_t PN532::tgGetData(uint8_t *buf, uint8_t len)
|
||||
{
|
||||
int16_t PN532::tgGetData(uint8_t *buf, uint8_t len) {
|
||||
buf[0] = PN532_COMMAND_TGGETDATA;
|
||||
|
||||
if (HAL(writeCommand)(buf, 1)) {
|
||||
@ -563,8 +549,7 @@ int16_t PN532::tgGetData(uint8_t *buf, uint8_t len)
|
||||
return length;
|
||||
}
|
||||
|
||||
bool PN532::tgSetData(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
|
||||
{
|
||||
bool PN532::tgSetData(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen) {
|
||||
if (hlen > (sizeof(pn532_packetbuffer) - 1)) {
|
||||
if ((body != 0) || (header == pn532_packetbuffer)) {
|
||||
return false;
|
||||
@ -629,8 +614,7 @@ int16_t PN532::inRelease(const uint8_t relevantTarget){
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_Polling(uint16_t systemCode, uint8_t requestCode, uint8_t * idm, uint8_t * pmm, uint16_t *systemCodeResponse, uint16_t timeout)
|
||||
{
|
||||
int8_t PN532::felica_Polling(uint16_t systemCode, uint8_t requestCode, uint8_t *idm, uint8_t *pmm, uint16_t *systemCodeResponse, uint16_t timeout) {
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET;
|
||||
pn532_packetbuffer[1] = 1;
|
||||
pn532_packetbuffer[2] = 1;
|
||||
@ -691,8 +675,7 @@ int8_t PN532::felica_Polling(uint16_t systemCode, uint8_t requestCode, uint8_t *
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_SendCommand (const uint8_t *command, uint8_t commandlength, uint8_t *response, uint8_t *responseLength)
|
||||
{
|
||||
int8_t PN532::felica_SendCommand(const uint8_t *command, uint8_t commandlength, uint8_t *response, uint8_t *responseLength) {
|
||||
if (commandlength > 0xFE) {
|
||||
return -1;
|
||||
}
|
||||
@ -739,8 +722,7 @@ int8_t PN532::felica_SendCommand (const uint8_t *command, uint8_t commandlength,
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_RequestService(uint8_t numNode, uint16_t *nodeCodeList, uint16_t *keyVersions)
|
||||
{
|
||||
int8_t PN532::felica_RequestService(uint8_t numNode, uint16_t *nodeCodeList, uint16_t *keyVersions) {
|
||||
if (numNode > FELICA_REQ_SERVICE_MAX_NODE_NUM) {
|
||||
return -1;
|
||||
}
|
||||
@ -786,8 +768,7 @@ int8_t PN532::felica_RequestService(uint8_t numNode, uint16_t *nodeCodeList, uin
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_RequestResponse(uint8_t * mode)
|
||||
{
|
||||
int8_t PN532::felica_RequestResponse(uint8_t *mode) {
|
||||
uint8_t cmd[9];
|
||||
cmd[0] = FELICA_CMD_REQUEST_RESPONSE;
|
||||
memcpy(&cmd[1], _felicaIDm, 8);
|
||||
@ -820,8 +801,7 @@ int8_t PN532::felica_RequestResponse(uint8_t * mode)
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_ReadWithoutEncryption (uint8_t numService, const uint16_t *serviceCodeList, uint8_t numBlock, const uint16_t *blockList, uint8_t blockData[][16])
|
||||
{
|
||||
int8_t PN532::felica_ReadWithoutEncryption(uint8_t numService, const uint16_t *serviceCodeList, uint8_t numBlock, const uint16_t *blockList, uint8_t blockData[][16]) {
|
||||
if (numService > FELICA_READ_MAX_SERVICE_NUM) {
|
||||
return -1;
|
||||
}
|
||||
@ -887,8 +867,7 @@ int8_t PN532::felica_ReadWithoutEncryption (uint8_t numService, const uint16_t *
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_WriteWithoutEncryption (uint8_t numService, const uint16_t *serviceCodeList, uint8_t numBlock, const uint16_t *blockList, uint8_t blockData[][16])
|
||||
{
|
||||
int8_t PN532::felica_WriteWithoutEncryption(uint8_t numService, const uint16_t *serviceCodeList, uint8_t numBlock, const uint16_t *blockList, uint8_t blockData[][16]) {
|
||||
if (numService > FELICA_WRITE_MAX_SERVICE_NUM) {
|
||||
return -1;
|
||||
}
|
||||
@ -948,8 +927,7 @@ int8_t PN532::felica_WriteWithoutEncryption (uint8_t numService, const uint16_t
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_RequestSystemCode(uint8_t * numSystemCode, uint16_t *systemCodeList)
|
||||
{
|
||||
int8_t PN532::felica_RequestSystemCode(uint8_t *numSystemCode, uint16_t *systemCodeList) {
|
||||
uint8_t cmd[9];
|
||||
cmd[0] = FELICA_CMD_REQUEST_SYSTEM_CODE;
|
||||
memcpy(&cmd[1], _felicaIDm, 8);
|
||||
@ -982,8 +960,7 @@ int8_t PN532::felica_RequestSystemCode(uint8_t * numSystemCode, uint16_t *system
|
||||
< 0: error
|
||||
*/
|
||||
/**************************************************************************/
|
||||
int8_t PN532::felica_Release()
|
||||
{
|
||||
int8_t PN532::felica_Release() {
|
||||
// InRelease
|
||||
pn532_packetbuffer[0] = PN532_COMMAND_INRELEASE;
|
||||
pn532_packetbuffer[1] = 0x00; // All target
|
||||
|
@ -81,8 +81,7 @@
|
||||
#define FELICA_WRITE_MAX_BLOCK_NUM 10 // for typical FeliCa card
|
||||
#define FELICA_REQ_SERVICE_MAX_NODE_NUM 32
|
||||
|
||||
class PN532
|
||||
{
|
||||
class PN532 {
|
||||
public:
|
||||
PN532(PN532Interface &interface);
|
||||
|
||||
|
@ -20,12 +20,12 @@
|
||||
#define PN532_INVALID_FRAME (-3)
|
||||
#define PN532_NO_SPACE (-4)
|
||||
|
||||
#define REVERSE_BITS_ORDER(b) b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; \
|
||||
#define REVERSE_BITS_ORDER(b) \
|
||||
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; \
|
||||
b = (b & 0xCC) >> 2 | (b & 0x33) << 2; \
|
||||
b = (b & 0xAA) >> 1 | (b & 0x55) << 1
|
||||
|
||||
class PN532Interface
|
||||
{
|
||||
class PN532Interface {
|
||||
public:
|
||||
virtual void begin() = 0;
|
||||
virtual void wakeup() = 0;
|
||||
@ -53,4 +53,3 @@ public:
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -9,15 +9,13 @@
|
||||
#define DATA_WRITE 1
|
||||
#define DATA_READ 3
|
||||
|
||||
PN532_SPI::PN532_SPI(SPIClass &spi, uint8_t ss)
|
||||
{
|
||||
PN532_SPI::PN532_SPI(SPIClass &spi, uint8_t ss) {
|
||||
command = 0;
|
||||
_spi = &spi;
|
||||
_ss = ss;
|
||||
}
|
||||
|
||||
void PN532_SPI::begin()
|
||||
{
|
||||
void PN532_SPI::begin() {
|
||||
pinMode(_ss, OUTPUT);
|
||||
|
||||
_spi->begin();
|
||||
@ -29,11 +27,9 @@ void PN532_SPI::begin()
|
||||
/** DUE spi library does not support SPI_CLOCK_DIV8 macro */
|
||||
_spi->setClockDivider(42); // set clock 2MHz(max: 5MHz)
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void PN532_SPI::wakeup()
|
||||
{
|
||||
void PN532_SPI::wakeup() {
|
||||
digitalWrite(_ss, LOW);
|
||||
delay(2);
|
||||
digitalWrite(_ss, HIGH);
|
||||
@ -41,8 +37,7 @@ void PN532_SPI::wakeup()
|
||||
|
||||
|
||||
|
||||
int8_t PN532_SPI::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
|
||||
{
|
||||
int8_t PN532_SPI::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen) {
|
||||
command = header[0];
|
||||
writeFrame(header, hlen, body, blen);
|
||||
|
||||
@ -60,8 +55,7 @@ int8_t PN532_SPI::writeCommand(const uint8_t *header, uint8_t hlen, const uint8_
|
||||
return 0;
|
||||
}
|
||||
|
||||
int16_t PN532_SPI::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout)
|
||||
{
|
||||
int16_t PN532_SPI::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout) {
|
||||
uint16_t time = 0;
|
||||
while (!isReady()) {
|
||||
delay(1);
|
||||
@ -114,7 +108,6 @@ int16_t PN532_SPI::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout)
|
||||
for (uint8_t i = 0; i < length; i++) {
|
||||
buf[i] = read();
|
||||
sum += buf[i];
|
||||
|
||||
}
|
||||
|
||||
uint8_t checksum = read();
|
||||
@ -132,8 +125,7 @@ int16_t PN532_SPI::readResponse(uint8_t buf[], uint8_t len, uint16_t timeout)
|
||||
return result;
|
||||
}
|
||||
|
||||
bool PN532_SPI::isReady()
|
||||
{
|
||||
bool PN532_SPI::isReady() {
|
||||
digitalWrite(_ss, LOW);
|
||||
|
||||
write(STATUS_READ);
|
||||
@ -142,8 +134,7 @@ bool PN532_SPI::isReady()
|
||||
return status;
|
||||
}
|
||||
|
||||
void PN532_SPI::writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen)
|
||||
{
|
||||
void PN532_SPI::writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *body, uint8_t blen) {
|
||||
digitalWrite(_ss, LOW);
|
||||
delay(2); // wake up PN532
|
||||
|
||||
@ -163,12 +154,10 @@ void PN532_SPI::writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *b
|
||||
for (uint8_t i = 0; i < hlen; i++) {
|
||||
write(header[i]);
|
||||
sum += header[i];
|
||||
|
||||
}
|
||||
for (uint8_t i = 0; i < blen; i++) {
|
||||
write(body[i]);
|
||||
sum += body[i];
|
||||
|
||||
}
|
||||
|
||||
uint8_t checksum = ~sum + 1; // checksum of TFI + DATA
|
||||
@ -176,11 +165,9 @@ void PN532_SPI::writeFrame(const uint8_t *header, uint8_t hlen, const uint8_t *b
|
||||
write(PN532_POSTAMBLE);
|
||||
|
||||
digitalWrite(_ss, HIGH);
|
||||
|
||||
}
|
||||
|
||||
int8_t PN532_SPI::readAckFrame()
|
||||
{
|
||||
int8_t PN532_SPI::readAckFrame() {
|
||||
const uint8_t PN532_ACK[] = { 0, 0, 0xFF, 0, 0xFF, 0 };
|
||||
|
||||
uint8_t ackBuf[sizeof(PN532_ACK)];
|
||||
|
76
src/connection.h
Normal file
76
src/connection.h
Normal file
@ -0,0 +1,76 @@
|
||||
/*
|
||||
库代码来自:https://github.com/spicetools/spicetools/tree/master/api/resources/arduino
|
||||
删除了未使用的函数。
|
||||
*/
|
||||
#ifndef SPICEAPI_CONNECTION_H
|
||||
#define SPICEAPI_CONNECTION_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef SPICEAPI_INTERFACE
|
||||
#define SPICEAPI_INTERFACE Serial
|
||||
#endif
|
||||
|
||||
namespace spiceapi {
|
||||
|
||||
class Connection {
|
||||
private:
|
||||
uint8_t* receive_buffer;
|
||||
size_t receive_buffer_size;
|
||||
|
||||
public:
|
||||
Connection(size_t receive_buffer_size);
|
||||
void reset();
|
||||
const char* request(char* json);
|
||||
};
|
||||
}
|
||||
|
||||
spiceapi::Connection::Connection(size_t receive_buffer_size) {
|
||||
this->receive_buffer = new uint8_t[receive_buffer_size];
|
||||
this->receive_buffer_size = receive_buffer_size;
|
||||
this->reset();
|
||||
}
|
||||
|
||||
void spiceapi::Connection::reset() {
|
||||
|
||||
// drop all input
|
||||
while (SPICEAPI_INTERFACE.available()) {
|
||||
SPICEAPI_INTERFACE.read();
|
||||
}
|
||||
}
|
||||
|
||||
const char* spiceapi::Connection::request(char* json_data) {
|
||||
auto json_len = strlen(json_data) + 1;
|
||||
|
||||
// send
|
||||
auto send_result = SPICEAPI_INTERFACE.write((const char*)json_data, (int)json_len);
|
||||
SPICEAPI_INTERFACE.flush();
|
||||
if (send_result < (int)json_len) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// receive
|
||||
size_t receive_data_len = 0;
|
||||
while (SPICEAPI_INTERFACE) {
|
||||
|
||||
// read single byte
|
||||
auto b = SPICEAPI_INTERFACE.read();
|
||||
if (b < 0) continue;
|
||||
receive_buffer[receive_data_len++] = b;
|
||||
|
||||
// check for buffer overflow
|
||||
if (receive_data_len >= receive_buffer_size) {
|
||||
this->reset();
|
||||
return "";
|
||||
}
|
||||
|
||||
// check for message end
|
||||
if (receive_buffer[receive_data_len - 1] == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
// return resulting json
|
||||
return (const char*)&receive_buffer[0];
|
||||
}
|
||||
|
||||
#endif //SPICEAPI_CONNECTION_H
|
140
src/wrappers.h
Normal file
140
src/wrappers.h
Normal file
@ -0,0 +1,140 @@
|
||||
/*
|
||||
库代码来自:https://github.com/spicetools/spicetools/tree/master/api/resources/arduino
|
||||
删除了未使用的函数。
|
||||
*/
|
||||
#ifndef SPICEAPI_WRAPPERS_H
|
||||
#define SPICEAPI_WRAPPERS_H
|
||||
|
||||
#define ARDUINOJSON_USE_LONG_LONG 1
|
||||
#include "ArduinoJson.h"
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "connection.h"
|
||||
|
||||
// default buffer sizes
|
||||
#ifndef SPICEAPI_WRAPPER_BUFFER_SIZE
|
||||
#define SPICEAPI_WRAPPER_BUFFER_SIZE 256
|
||||
#endif
|
||||
#ifndef SPICEAPI_WRAPPER_BUFFER_SIZE_STR
|
||||
#define SPICEAPI_WRAPPER_BUFFER_SIZE_STR 256
|
||||
#endif
|
||||
|
||||
namespace spiceapi {
|
||||
|
||||
/*
|
||||
* Structs
|
||||
*/
|
||||
|
||||
struct InfoAvs {
|
||||
String model, dest, spec, rev, ext;
|
||||
};
|
||||
|
||||
// static storage
|
||||
char JSON_BUFFER_STR[SPICEAPI_WRAPPER_BUFFER_SIZE_STR];
|
||||
|
||||
/*
|
||||
* Helpers
|
||||
*/
|
||||
|
||||
uint64_t msg_gen_id() {
|
||||
static uint64_t id_global = 0;
|
||||
return ++id_global;
|
||||
}
|
||||
|
||||
char *doc2str(DynamicJsonDocument *doc) {
|
||||
char *buf = JSON_BUFFER_STR;
|
||||
serializeJson(*doc, buf, SPICEAPI_WRAPPER_BUFFER_SIZE_STR);
|
||||
return buf;
|
||||
}
|
||||
|
||||
DynamicJsonDocument *request_gen(const char *module, const char *function) {
|
||||
|
||||
// create document
|
||||
auto doc = new DynamicJsonDocument(SPICEAPI_WRAPPER_BUFFER_SIZE);
|
||||
|
||||
// add attributes
|
||||
(*doc)["id"] = msg_gen_id();
|
||||
(*doc)["module"] = module;
|
||||
(*doc)["function"] = function;
|
||||
|
||||
// add params
|
||||
(*doc).createNestedArray("params");
|
||||
|
||||
// return document
|
||||
return doc;
|
||||
}
|
||||
|
||||
DynamicJsonDocument *response_get(Connection &con, const char *json) {
|
||||
|
||||
// parse document
|
||||
DynamicJsonDocument *doc = new DynamicJsonDocument(SPICEAPI_WRAPPER_BUFFER_SIZE);
|
||||
auto err = deserializeJson(*doc, (char *)json);
|
||||
|
||||
// check for parse error
|
||||
if (err) {
|
||||
delete doc;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// check id
|
||||
if (!(*doc)["id"].is<int64_t>()) {
|
||||
delete doc;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// check errors
|
||||
auto errors = (*doc)["errors"];
|
||||
if (!errors.is<JsonArray>()) {
|
||||
delete doc;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// check error count
|
||||
if (errors.as<JsonArray>().size() > 0) {
|
||||
delete doc;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// check data
|
||||
if (!(*doc)["data"].is<JsonArray>()) {
|
||||
delete doc;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// return document
|
||||
return doc;
|
||||
}
|
||||
|
||||
bool card_insert(Connection &con, size_t index, const char *card_id) {
|
||||
auto req = request_gen("card", "insert");
|
||||
auto params = (*req)["params"].as<JsonArray>();
|
||||
params.add(index);
|
||||
params.add(card_id);
|
||||
auto req_str = doc2str(req);
|
||||
delete req;
|
||||
auto res = response_get(con, con.request(req_str));
|
||||
if (!res)
|
||||
return false;
|
||||
delete res;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool info_avs(Connection &con, InfoAvs &info) {
|
||||
auto req = request_gen("info", "avs");
|
||||
auto req_str = doc2str(req);
|
||||
delete req;
|
||||
auto res = response_get(con, con.request(req_str));
|
||||
if (!res)
|
||||
return false;
|
||||
auto data = (*res)["data"][0];
|
||||
info.model = (const char *)data["model"];
|
||||
info.dest = (const char *)data["dest"];
|
||||
info.spec = (const char *)data["spec"];
|
||||
info.rev = (const char *)data["rev"];
|
||||
info.ext = (const char *)data["ext"];
|
||||
delete res;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //SPICEAPI_WRAPPERS_H
|
Loading…
Reference in New Issue
Block a user