Refucktoring NFC.

master
q3k 2012-04-28 21:58:12 +00:00
parent eefd452638
commit 057ce4985d
5 changed files with 112 additions and 94 deletions

View File

@ -1,6 +1,6 @@
project(hf-terminal)
add_executable(hf-terminal main.c tts.c base64.c ldap.c)
add_executable(hf-terminal main.c tts.c base64.c ldap.c nfc.c)
set(CMAKE_C_FLAGS "-std=c99")
target_link_libraries(hf-terminal nfc m curl)
target_link_libraries(hf-terminal nfc m)

View File

@ -1,101 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <nfc/nfc.h>
#include <nfc/nfc-types.h>
#include "nfc.h"
#include "tts.h"
#include "ldap.h"
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;
void poll_nfc(void)
{
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;
if (Target.nti.nai.szUidLen != 4)
return;
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 == 1)
{
tts_speak("Nieznana karta.");
sleep(5);
return;
}
else
{
tts_speak("Nieznany błąd przy połączeniu z eldapem.");
sleep(10);
return;
}
}
printf("This appears to be %s.\n", DN);
}
}
int main(int argc, char **argv)
{
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");
nfc_initialize();
tts_speak("Refryżyrator gotowy do pracy.");
while (1)
{
poll_nfc();
nfc_poll();
}
return 0;
}

97
terminal/nfc.c Normal file
View File

@ -0,0 +1,97 @@
#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"
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;
void nfc_poll(void)
{
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;
if (Target.nti.nai.szUidLen != 4)
return;
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 == 1)
{
tts_speak("Nieznana karta.");
sleep(5);
return;
}
else
{
tts_speak("Nieznany błąd przy połączeniu z eldapem.");
sleep(10);
return;
}
}
printf("This appears to be %s.\n", DN);
}
}
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;
}

7
terminal/nfc.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef __NFC_H__
#define __NFC_H__
int nfc_initialize(void);
void nfc_poll(void);
#endif

View File

@ -21,14 +21,11 @@ void tts_speak(char *Text)
char *URL = (char *)malloc(strlen(B64Output) + strlen(g_IvonaURL));
sprintf(URL, g_IvonaURL, B64Output);
printf("URL: %s\n", URL);
if (!fork())
{
printf("Forking...\n");
char *CommandLine = malloc(strlen(URL) + 100);
sprintf(CommandLine, "curl -L -A Mozilla \"%s\" | mpg123 -2 -q -", URL);
printf("Cline: %s\n", CommandLine);
sprintf(CommandLine, "curl -L -s -A Mozilla \"%s\" | mpg123 -2 -q -", URL);
char *Arguments[4];
Arguments[0] = "bash";
Arguments[1] = "-c";