Refucktoring nfc_poll -> nfc_read.

master
q3k 2012-05-01 06:43:16 +00:00
parent 3e90efe0c5
commit 36d5feaf2f
3 changed files with 27 additions and 9 deletions

View File

@ -11,9 +11,18 @@ int main(int argc, char **argv)
tts_speak("Refryżyrator gotowy do pracy."); tts_speak("Refryżyrator gotowy do pracy.");
char UID[128];
while (1) while (1)
{ {
nfc_poll(); int NFCResult = nfc_read(UID);
if (NFCResult == 0)
{
// Do barcode stuff here
}
else if (NFCResult != NFC_TIMEOUT)
{
// Do error handling here
}
} }
} }

View File

@ -10,6 +10,10 @@
#include "ldap.h" #include "ldap.h"
#include "base64.h" #include "base64.h"
#define E_BAD_CARD 1
#define E_NO_SUCH_USER 2
#define E_FUCKUP 3
static nfc_device_t *g_NFCDevice = NULL; static nfc_device_t *g_NFCDevice = NULL;
const nfc_modulation_t g_NFCModulations[5] = { const nfc_modulation_t g_NFCModulations[5] = {
@ -21,7 +25,7 @@ const nfc_modulation_t g_NFCModulations[5] = {
}; };
const size_t g_NFCModulationCount = 5; const size_t g_NFCModulationCount = 5;
void nfc_poll(void) int nfc_read(char *UIDOut)
{ {
nfc_target_t Target; nfc_target_t Target;
bool Result = nfc_initiator_poll_target(g_NFCDevice, g_NFCModulations, g_NFCModulationCount, 20, 2, &Target); bool Result = nfc_initiator_poll_target(g_NFCDevice, g_NFCModulations, g_NFCModulationCount, 20, 2, &Target);
@ -29,10 +33,10 @@ void nfc_poll(void)
if (Result) if (Result)
{ {
if (Target.nm.nmt != NMT_ISO14443A) if (Target.nm.nmt != NMT_ISO14443A)
return; return E_BAD_CARD;
if (Target.nti.nai.szUidLen != 4) if (Target.nti.nai.szUidLen != 4)
return; return E_BAD_CARD;
printf("Scanned Mifare %x.\n", *(int *)Target.nti.nai.abtUid); printf("Scanned Mifare %x.\n", *(int *)Target.nti.nai.abtUid);
@ -48,18 +52,19 @@ void nfc_poll(void)
if (LDAPResult == NO_SUCH_CARD) if (LDAPResult == NO_SUCH_CARD)
{ {
tts_speak("Nieznana karta."); tts_speak("Nieznana karta.");
sleep(5); return E_NO_SUCH_USER;
return;
} }
else else
{ {
tts_speak("Nieznany błąd przy połączeniu z eldapem."); tts_speak("Nieznany błąd przy połączeniu z eldapem.");
sleep(10); return E_FUCKUP;
return;
} }
} }
printf("This appears to be %s.\n", DN); printf("This appears to be %s.\n", DN);
strncpy(UIDOut, 128, DN);
return 0;
} }
return 1;
} }
int nfc_initialize(void) int nfc_initialize(void)

View File

@ -1,7 +1,11 @@
#ifndef __NFC_H__ #ifndef __NFC_H__
#define __NFC_H__ #define __NFC_H__
#define NFC_BAD_CARD 1
#define NFC_NO_SUCH_USER 2
#define NFC_FUCKUP 3
int nfc_initialize(void); int nfc_initialize(void);
void nfc_poll(void); int nfc_read(char *UIDOut);
#endif #endif