/* rfid-mod
 * simplistic rfid recog program heavily based on Phidgets Inc's RFID-simple 
 * version: 0.1b
 * author: Lepht Anonym  */
#include 
#include 
/* attachHandler
 * displays status to the terminal */
int attachHandler(CPhidgetHandle reader, void *userPointer)
{
 int serial;
 const char *name;
 CPhidget_getDeviceName (reader, &name);
 CPhidget_getSerialNumber(reader, &serial);
 printf("%s %10d attached.\n", name, serial);
 return 0;
}
/* detachHandler
 * displays status to the terminal */
int detachHandler(CPhidgetHandle reader, void *userPointer)
{
 int serial;
 const char *name;
 CPhidget_getDeviceName (reader, &name);
 CPhidget_getSerialNumber(reader, &serial);
 printf("%s %10d detached.\n", name, serial);
 return 0;
}
/* errorHandler
 * prints errors to the terminal */
int errorHandler(CPhidgetHandle reader, void *userPointer, int errorCode, const char *unknown)
{
 printf("error handled: %i - %s\n", errorCode, unknown);
 return 0;
}
/* tagHandler
 * turns on the LED port and prints the ID of the tag
 * later, it will store the read tag in a local var so it can be verified as either matching L's or not
 * pls note this is not a secure thing to do as the tag value could be strings'd out of the program (should hash'n'md5 instead)  */
int tagHandler(CPhidgetRFIDHandle reader, void *userPointer, unsigned char *tag)
{
 int i;
 CPhidgetRFID_setLEDOn(reader, 1);
 printf("got tag: ");
 for(i = 0 ; i < 5 ; i++) printf("%02x ", (*(tag+i))&0xff);
 printf("\n");
 return 0;
}
/* tagLostHandler
 * turns off the LED port and prints the ID of the lost tag to the terminal */
int tagLostHandler(CPhidgetRFIDHandle reader, void *userPointer, unsigned char *tag)
{
 int i;
 int var[5];
 CPhidgetRFID_setLEDOn(reader, 0);
 printf("tag lost: ", tag);
 for(i = 0 ; i < 5 ; i++) printf("%02x ", (*(tag+i))&0xff);
 // store that same data as a variable:
 for(i = 0; i < 5; i++) { var[i] = (*(tag+i)&0xff); }
 // TODO check that the separate parts of the var array match L's implant
 printf("\n");
 return 0;
}
/* displayProperties 
 * displays the data about a physical reader to the terminal */
void displayProperties(CPhidgetRFIDHandle reader)
{
 // declare variables for the data and a buffer to store device type in:
 int serial, version, outputs, antennaStatus;
 const char* type;
 // get data from the device:
 CPhidget_getDeviceType((CPhidgetHandle)reader, &type);
 CPhidget_getSerialNumber((CPhidgetHandle)reader, &serial);
 CPhidget_getDeviceVersion((CPhidgetHandle)reader, &version);
 CPhidgetRFID_getNumOutputs(reader, &outputs);
 CPhidgetRFID_getAntennaOn(reader, &antennaStatus);
 // print to the terminal:
 printf("%s\n", type);
 printf("serial number: %10d\nversion: %8d\n", serial, version);
 printf("number of digital outputs: %d\n\n", outputs);
 printf("antenna status: %d\n", antennaStatus);
}
/* main
 * administrates */
int main(int argc, char* argv[])
{
 const char *error;
 int result;
 // declare and create a handle for the reader:
 CPhidgetRFIDHandle reader = 0;
 CPhidgetRFID_create(&reader);
 // set handlers for device and tag events:
 CPhidget_set_OnAttach_Handler((CPhidgetHandle)reader, attachHandler, NULL);
 CPhidget_set_OnDetach_Handler((CPhidgetHandle)reader, detachHandler, NULL);
 CPhidget_set_OnError_Handler((CPhidgetHandle)reader, errorHandler, NULL);
 CPhidgetRFID_set_OnTag_Handler(reader, tagHandler, NULL);
 CPhidgetRFID_set_OnTagLost_Handler(reader, tagLostHandler, NULL);
 // open device:
 CPhidget_open((CPhidgetHandle)reader, -1);
 // wait for the physical RFID reader device to be attached:
 printf("polling for physical device presence...");
 if((result = CPhidget_waitForAttachment((CPhidgetHandle)reader, 10000)))
 {
  CPhidget_getErrorDescription(result, &error);
  printf("device error: %s\n", error);
  return 1;
 }
 // activate the antenna and display properties to the terminal:
 CPhidgetRFID_setAntennaOn(reader, 1);
 displayProperties(reader);
 // keep the program open until the user is done:
 printf("now scanning for tags.\nhit any key and enter to end session.\n");
 getchar();
 // close the device and free the memory back up:
 printf("closing...\n");
 CPhidget_close((CPhidgetHandle)reader);
 CPhidget_delete((CPhidgetHandle)reader);
 return 0;
}