104 lines
2.4 KiB
C
104 lines
2.4 KiB
C
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#include <nfc/nfc.h>
|
|
#include <nfc/nfc-types.h>
|
|
|
|
#include "tts.h"
|
|
#include "ldap.h"
|
|
#include "base64.h"
|
|
|
|
#define E_BAD_CARD 1
|
|
#define E_NO_SUCH_USER 2
|
|
#define E_FUCKUP 3
|
|
#define E_TIMEOUT 4
|
|
|
|
static nfc_device_t *g_NFCDevice = NULL;
|
|
|
|
const nfc_modulation_t g_NFCModulations[5] = {
|
|
{ .nmt = NMT_ISO14443A, .nbr = NBR_106 },
|
|
{ .nmt = NMT_ISO14443B, .nbr = NBR_106 },
|
|
{ .nmt = NMT_FELICA, .nbr = NBR_212 },
|
|
{ .nmt = NMT_FELICA, .nbr = NBR_424 },
|
|
{ .nmt = NMT_JEWEL, .nbr = NBR_106 },
|
|
};
|
|
const size_t g_NFCModulationCount = 5;
|
|
|
|
int nfc_read(char *UIDOut)
|
|
{
|
|
nfc_target_t Target;
|
|
bool Result = nfc_initiator_poll_target(g_NFCDevice, g_NFCModulations, g_NFCModulationCount, 20, 2, &Target);
|
|
|
|
if (Result)
|
|
{
|
|
if (Target.nm.nmt != NMT_ISO14443A)
|
|
return E_BAD_CARD;
|
|
|
|
if (Target.nti.nai.szUidLen != 4)
|
|
return E_BAD_CARD;
|
|
|
|
printf("Scanned Mifare %x.\n", *(int *)Target.nti.nai.abtUid);
|
|
|
|
char MifareID[5];
|
|
sprintf(MifareID, "%04x", *(int *)Target.nti.nai.abtUid);
|
|
|
|
char DN[128];
|
|
int DNLength = 128;
|
|
int LDAPResult = ldap_dn_by_mifare(MifareID, DN, &DNLength);
|
|
|
|
if (LDAPResult > 0)
|
|
{
|
|
if (LDAPResult == NO_SUCH_CARD)
|
|
{
|
|
tts_speak("Nieznana karta.");
|
|
return E_NO_SUCH_USER;
|
|
}
|
|
else
|
|
{
|
|
tts_speak("Nieznany błąd przy połączeniu z eldapem.");
|
|
return E_FUCKUP;
|
|
}
|
|
}
|
|
printf("This appears to be %s.\n", DN);
|
|
strncpy(UIDOut, DN, 128);
|
|
return 0;
|
|
}
|
|
return E_TIMEOUT;
|
|
}
|
|
|
|
int nfc_initialize(void)
|
|
{
|
|
nfc_device_desc_t *Devices;
|
|
|
|
size_t NumDevices;
|
|
Devices = (nfc_device_desc_t *) malloc(10 * sizeof(*Devices));
|
|
nfc_list_devices(Devices, 10, &NumDevices);
|
|
|
|
if (NumDevices == 0)
|
|
{
|
|
fprintf(stderr, "Error: No NFC device found.\n");
|
|
return 1;
|
|
}
|
|
|
|
if (NumDevices > 1)
|
|
{
|
|
fprintf(stderr, "Error: More than one NFC device found.\n");
|
|
return 1;
|
|
}
|
|
|
|
g_NFCDevice = nfc_connect(&Devices[0]);
|
|
|
|
if (g_NFCDevice == NULL)
|
|
{
|
|
fprintf(stderr, "Error: Could not connect to NFC device.\n");
|
|
return 1;
|
|
}
|
|
|
|
nfc_initiator_init(g_NFCDevice);
|
|
printf("Connected to NFC device.\n");
|
|
|
|
return 0;
|
|
}
|