diff --git a/firmware/README.md b/firmware/README.md index 160f215..82ad0c3 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -1,7 +1,7 @@ ####固件开发环境 * Arduino 1.6 * github.com/Wiznet/WIZ_Ethernet_Library -* github.com/Seeed-Studio/PN532 +* github.com/593141477/PN532 Library安装方法(Arduino对库的支持实在太hack了): diff --git a/firmware/core/core.ino b/firmware/core/core.ino index 7421a20..ba408f7 100644 --- a/firmware/core/core.ino +++ b/firmware/core/core.ino @@ -9,7 +9,7 @@ const int led = 13; //LED pin config byte mac[] = {0xDE, 0xAD, 0x00, 0x09, 0x00, 0x09}; -#define SERVER_HOST "192.168.1.103" +#define SERVER_HOST "192.168.1.30" #define SERVER_PORT 39999 #define MAX_CONNECT_RETRIES 5 #define MAX_PACKET_SIZE 16 @@ -54,12 +54,14 @@ // Open serial communications and wait for port to open: Serial.begin(9600); // this check is only needed on the Leonardo: +#if 1 while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } +#endif Serial.println("Card9 controller started"); - rfid.Init(); + rfid.Init(); // start the Ethernet connection: Serial.println("Trying to get an IP address using DHCP"); @@ -100,6 +102,14 @@ } break; case PacketTypeResponse: + switch (p->payload.response.responseType) { + case ResponsePositive: + door.Open(); + break; + case ResponseNegative: + case ResponseNatural: + break; + } break; } } @@ -114,6 +124,18 @@ client.write((byte*)&p, sizeof(struct packet)); } +void sendAuthPacket(uint8_t uidLen, uint8_t* uid) +{ + struct packet *p = (packet*)new char[sizeof(packet) + uidLen]; + p->characteristicAndVersion = PacketIdentifier; + p->type = PacketTypeRequest; + p->payloadSize = sizeof(requestPayload) + uidLen; + p->payload.request.requestType = RequestAuth; + memcpy(p->payload.request.param, uid, uidLen); + client.write((byte*)p, sizeof(struct packet) + uidLen); + delete p; +} + void receivePacket(byte charRecved) { static uint8_t packetBuffer[MAX_PACKET_SIZE]; @@ -167,8 +189,11 @@ sendEventPacket(CardDidScan); if(rfid.SkeletonKey()){ door.Open(); + }else{ + uint8_t uidLen; + uint8_t* uid = rfid.GetUid(uidLen); + sendAuthPacket(uidLen, uid); } - delay(500); rfid.Next(); } diff --git a/firmware/core/rfid.h b/firmware/core/rfid.h index 2a442a7..93886d3 100644 --- a/firmware/core/rfid.h +++ b/firmware/core/rfid.h @@ -6,18 +6,21 @@ class RFID { + enum{Card_None, Card_14443A, Card_14443B}; static uint8_t skeletonKey[8]; static uint8_t skeletonKeyLength; uint8_t uid[8]; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) + uint8_t card; + bool found; unsigned long previousPollMillis; public: - RFID():uidLength(0),previousPollMillis(0){} + RFID():uidLength(0),previousPollMillis(0),card(Card_None),found(false){} void Init(); void Poll(); bool Found() { - return uidLength != 0; + return found; } uint8_t* GetUid(uint8_t &len) { @@ -27,7 +30,7 @@ bool SkeletonKey(); void Next() { - uidLength = 0; + found = false; } }; diff --git a/firmware/core/rfid.ino b/firmware/core/rfid.ino index 8d609a0..91768b3 100644 --- a/firmware/core/rfid.ino +++ b/firmware/core/rfid.ino @@ -1,11 +1,11 @@ #include "rfid.h" #include -#include +#include #include "PN532.h" -static PN532_I2C pn532_i2c(Wire); -static PN532 pn532(pn532_i2c); +static PN532_HSU pn532_hsu(Serial1); +static PN532 pn532(pn532_hsu); uint8_t RFID::skeletonKey[8] = {0x64,0x1C,0x6D,0xDB}; uint8_t RFID::skeletonKeyLength = 4; @@ -15,7 +15,7 @@ { boolean success; - if(uidLength > 0){ //A card had been found previously + if(Found()){ //A card had been found previously return; } @@ -26,10 +26,22 @@ return; } + if(card == Card_14443B){ + if(pn532.stuCardIsPresent()){ //Card doesn't leave yet + return; + }else{ + pn532.resetConfigFor14443B(); + card = Card_None; + } + }else if(card == Card_14443A){ + pn532.inRelease(); + card = Card_None; + } + // Wait for an ISO14443A type cards (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) - success = pn532.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 10); + success = pn532.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 200); if (success) { Serial.println("Found a card!"); @@ -40,20 +52,45 @@ Serial.print(" 0x");Serial.print(uid[i], HEX); } Serial.println(""); - // Wait 1 second before continuing - // delay(1000); + card = Card_14443A; + found = true; } else { // PN532 probably timed out waiting for a card - Serial.println("Timed out waiting for a card"); - uidLength = 0; + //Serial.println("Timed out waiting for a card"); + } + + static uint8_t AFI[] = {0}; + success = pn532.inListPassiveTarget(PN532_106KBPS_ISO14443B, sizeof(AFI) , AFI, 200); + if (success) { + uint8_t cardId[3]; + uint8_t expire[3]; + char studentId[11]; + + pn532.inRelease(); + success = pn532.readTsighuaStuCard(cardId, expire, studentId); + if(success){ + Serial.println("Found student card!"); + Serial.print("Student Number: "); Serial.println(studentId); + for(int i=0; i<5; i++){ + uid[i] = studentId[i+5]; + } + for(int i=0; i<3; i++){ + uid[i+5] = cardId[i]; + } + uidLength = 8; + card = Card_14443B; + found = true; + } + }else{ + } } bool RFID::SkeletonKey() { - if(uidLength > 0){ + if(card == Card_14443A){ if(uidLength != skeletonKeyLength) return false; for(int i=0; i