LPSKYETEK_TAG DuplicateTagImpl(LPSKYETEK_TAG tag) { LPSKYETEK_TAG t = NULL; TCHAR *str = NULL; unsigned int ix = 0; if( tag == NULL ) return NULL; t = (LPSKYETEK_TAG)malloc(sizeof(SKYETEK_TAG)); if( t == NULL ) return NULL; memset(t,0,sizeof(SKYETEK_TAG)); t->type = tag->type; t->afi = tag->afi; t->rf = tag->rf; t->session = tag->session; InitializeTagType(t); if( tag->id != NULL ) { t->id = SkyeTek_AllocateID(tag->id->length); if( t->id == NULL ) { free(t); return NULL; } for(ix = 0; ix < tag->id->length; ix++) t->id->id[ix] = tag->id->id[ix]; } str = SkyeTek_GetStringFromID(t->id); if( str != NULL ) { _tcscpy(t->friendly, str); SkyeTek_FreeString(str); } return t; }
/* NOTE: We can add factories for each tag type * later if there is tag-specific data that * needs to be stored. For now, it's overkill. */ SKYETEK_STATUS CreateTagImpl(SKYETEK_TAGTYPE type, LPSKYETEK_ID lpId, LPSKYETEK_TAG* tag) { LPSKYETEK_TAG t; unsigned int ix; TCHAR *str = NULL; if( tag == NULL ) return SKYETEK_INVALID_PARAMETER; t = *tag = (LPSKYETEK_TAG)malloc(sizeof(SKYETEK_TAG)); if( t == NULL ) return SKYETEK_OUT_OF_MEMORY; memset(t,0,sizeof(SKYETEK_TAG)); t->type = type; InitializeTagType(t); if( lpId != NULL && lpId->id != NULL && lpId->length > 0 && lpId->length < SKYETEK_MAX_TAG_LENGTH ) { t->id = SkyeTek_AllocateID(lpId->length); if( t->id == NULL ) { free(t); return SKYETEK_OUT_OF_MEMORY; } for(ix = 0; ix < lpId->length; ix++) t->id->id[ix] = lpId->id[ix]; } str = SkyeTek_GetStringFromID(t->id); if( str != NULL ) { _tcscpy(t->friendly, str); SkyeTek_FreeString(str); } else { _tcscpy(t->friendly, _T("unknown")); } return SKYETEK_SUCCESS; }
int GetReaderVersion( LPSKYETEK_DEVICE lpDevice ) { LPDEVICEIMPL lpDI = NULL; LPSKYETEK_DATA lpData = NULL; TCHAR *msg = NULL; int bytes = 0, len = 0; if( lpDevice == NULL || lpDevice->internal == NULL ) return 0; lpDI = (LPDEVICEIMPL)lpDevice->internal; lpData = SkyeTek_AllocateData(64); lpData->data[0] = 0x02; lpData->data[1] = 0x00; lpData->data[2] = 0x01; bytes = lpDI->Write(lpDevice,lpData->data,3,500); if( bytes != 3 ) { SkyeTek_FreeData(lpData); return 0; } lpDI->Flush(lpDevice); SkyeTek_Debug(_T("Sent: 020001\r\n")); SKYETEK_Sleep(100); bytes = lpDI->Read(lpDevice,lpData->data,3,500); lpData->size = bytes; msg = SkyeTek_GetStringFromData(lpData); if( msg != NULL ) { SkyeTek_Debug(_T("Read: %s\r\n"), msg); SkyeTek_FreeString(msg); msg = NULL; } // not a reader if( bytes == 0 ) { SkyeTek_FreeData(lpData); return 0; } // bootloader mode if( bytes == 2 && lpData->data[1] == 0xFF ) { SkyeTek_FreeData(lpData); return -1; } else if( bytes == 3 && lpData->data[0] == 0x00 && lpData->data[1] == 0x03 ) { SkyeTek_FreeData(lpData); return -1; } // something else? if( bytes != 3 ) { SkyeTek_FreeData(lpData); return 0; } /* Version 2 */ if( lpData->data[0] == 0x02 && lpData->data[1] == 0x01 && lpData->data[2] == 0x82 ) { SkyeTek_FreeData(lpData); return 2; } if( lpData->data[0] == 0x02 && lpData->data[1] == 0x01 && lpData->data[2] == 0x81 ) { SkyeTek_FreeData(lpData); return 2; } if( lpData->data[0] == 0x02 && lpData->data[1] == 0x04 && lpData->data[2] == 0x88 ) { bytes = lpDI->Read(lpDevice,lpData->data+3,3,500); lpData->size = 3 + bytes; SkyeTek_FreeData(lpData); return 2; } if( lpData->data[0] == 0x02 && lpData->data[1] == 0x03 && lpData->data[2] == 0x88 ) { bytes = lpDI->Read(lpDevice,lpData->data+3,2,500); lpData->size = 3 + bytes; SkyeTek_FreeData(lpData); return 2; } /* Sanity check */ if( lpData->data[0] != 0x02 && lpData->data[1] != 0x00 ) { SkyeTek_FreeData(lpData); return 0; } /* Get length */ len = lpData->data[2]; if( len > 60 || len == 0 ) { /* Garbage */ SkyeTek_FreeData(lpData); return 0; } /* Assume Version 3 and read remaining bytes */ bytes = lpDI->Read(lpDevice,lpData->data+3,len,500); lpData->size = 3+ bytes; msg = SkyeTek_GetStringFromData(lpData); if( msg != NULL ) { SkyeTek_Debug(_T("Read: %s\r\n"), msg); SkyeTek_FreeString(msg); msg = NULL; } if( bytes != len ) { SkyeTek_FreeData(lpData); return 0; } /* Version 3 */ if( lpData->data[3] == 0x80 || lpData->data[3] == 0x90 ) { SkyeTek_FreeData(lpData); return 3; } SkyeTek_FreeData(lpData); return 0; }
LPSKYETEK_READER GetReader( LPSKYETEK_DEVICE lpDevice, LPPROTOCOLIMPL lpPI, unsigned int ver ) { LPSKYETEK_READER lpReader = NULL; SKYETEK_READER tmpReader; SKYETEK_STATUS status; SKYETEK_ADDRESS addr; LPSKYETEK_DATA lpData = NULL; TCHAR *str = NULL; int i = 0; unsigned int ix = 0; if( lpDevice == NULL ) return NULL; /* Fill temp reader for system calls */ if( ver == 2 ) { tmpReader.id = SkyeTek_AllocateID(1); if( tmpReader.id != NULL ) tmpReader.id->id[0] = 0xFF; } else { tmpReader.id = SkyeTek_AllocateID(4); if( tmpReader.id != NULL ) for( i = 0; i < 4; i++ ) tmpReader.id->id[i] = 0xFF; } tmpReader.sendRID = 1; tmpReader.lpDevice = lpDevice; tmpReader.internal = &SkyetekReaderImpl; status = STR_GetSystemAddrForParm(SYS_FIRMWARE,&addr,ver); if( status != SKYETEK_SUCCESS ) goto failure; status = lpPI->GetSystemParameter(&tmpReader, &addr, &lpData,100); if( status != SKYETEK_SUCCESS || lpData == NULL ) goto failure; lpReader = (LPSKYETEK_READER)malloc(sizeof(SKYETEK_READER)); if( lpReader == NULL ) { SkyeTek_FreeData(lpData); goto failure; } memset(lpReader, 0, sizeof(SKYETEK_READER)); str = SkyeTek_GetStringFromData(lpData); if( str != NULL ) { _tcscpy(lpReader->firmware,str); SkyeTek_FreeString(str); } if( ver == 2 && lpData != NULL && lpData->data != NULL && lpData->size > 0 ) { switch(lpData->data[0]) { case 0x00: case 0x01: case 0x50: case 0x60: case 0xA0: case 0xC0: case 0xD0: _tcscpy(lpReader->model, _T("M1")); break; case 0xE0: _tcscpy(lpReader->model, _T("M8")); break; case 0x20: _tcscpy(lpReader->model, _T("M2")); break; default: _tcscpy(lpReader->model, _T("??")); } } SkyeTek_FreeData(lpData); if( ver == 3 ) { SKYETEK_Sleep(100); status = STR_GetSystemAddrForParm(SYS_PRODUCT,&addr,ver); if( status != SKYETEK_SUCCESS ) goto failure; status = lpPI->GetSystemParameter(&tmpReader, &addr, &lpData,100); if( status == SKYETEK_SUCCESS && lpData->data != NULL && lpData->size >= 2 ) { switch(lpData->data[1]) { case 0x02: _tcscpy(lpReader->model, _T("M2")); break; case 0x04: _tcscpy(lpReader->model, _T("M4")); break; case 0x2A: _tcscpy(lpReader->model, _T("M2A")); break; case 0x07: _tcscpy(lpReader->model, _T("M7")); break; case 0x09: _tcscpy(lpReader->model, _T("M9")); break; case 0x0A: _tcscpy(lpReader->model, _T("M10")); break; default: _tcscpy(lpReader->model, _T("??")); break; } SkyeTek_FreeData(lpData); } else _tcscpy(lpReader->model, _T("??")); } SKYETEK_Sleep(100); status = STR_GetSystemAddrForParm(SYS_READER_NAME,&addr,ver); if( status != SKYETEK_SUCCESS ) goto failure; status = lpPI->GetSystemParameter(&tmpReader, &addr, &lpData,100); if( status != SKYETEK_SUCCESS ) { free(lpReader); goto failure; } if( lpData != NULL && lpData->size > 0 && lpData->data != NULL ) { /* make sure we have a null pointer */ i = 0; for( ix = 0; ix < lpData->size; ix++ ) { if( lpData->data[ix] == '\0' ) { i = 1; break; } } if( i == 1 ) { //Reader name is stored in standard ascii for(ix = 0; ix < lpData->size; ix++) lpReader->readerName[ix] = (TCHAR)lpData->data[ix]; } } SkyeTek_FreeData(lpData); SKYETEK_Sleep(100); status = STR_GetSystemAddrForParm(SYS_SERIALNUMBER,&addr,ver); if( status != SKYETEK_SUCCESS ) goto failure; status = lpPI->GetSystemParameter(&tmpReader, &addr, &lpData,100); if( status != SKYETEK_SUCCESS ) { free(lpReader); goto failure; } str = SkyeTek_GetStringFromData(lpData); if( str != NULL ) { _tcscpy(lpReader->serialNumber, str); SkyeTek_FreeString(str); } SkyeTek_FreeData(lpData); SKYETEK_Sleep(100); status = STR_GetSystemAddrForParm(SYS_RID,&addr,ver); if( status != SKYETEK_SUCCESS ) goto failure; status = lpPI->GetSystemParameter(&tmpReader, &addr, &lpData,100); if( status != SKYETEK_SUCCESS ) { free(lpReader); goto failure; } lpReader->id = (LPSKYETEK_ID)lpData; lpReader->internal = &SkyetekReaderImpl; lpReader->lpDevice = lpDevice; lpReader->lpProtocol = (LPSKYETEK_PROTOCOL)malloc(sizeof(SKYETEK_PROTOCOL)); if( lpReader->lpProtocol == NULL ) { free(lpReader); goto failure; } lpReader->lpProtocol->version = ver; lpReader->lpProtocol->internal = lpPI; _tcscpy(lpReader->manufacturer, _T("SkyeTek")); str = SkyeTek_GetStringFromID(lpReader->id); if( str == NULL ) { free(lpReader); goto failure; } _tcscpy(lpReader->rid,str); /* TODO: See if Reader Name can be used */ _stprintf(lpReader->friendly, _T("%s-%s-%s"), lpReader->manufacturer, lpReader->model, str); SkyeTek_FreeString(str); return lpReader; failure: SkyeTek_FreeID(tmpReader.id); return NULL; }
int _tmain(int argc, TCHAR* argv[]) { LPSKYETEK_DEVICE *devices = NULL; LPSKYETEK_READER *readers = NULL; LPSKYETEK_DEVICE lpDevice = NULL; LPSKYETEK_READER lpReader = NULL; LPSKYETEK_DATA lpData = NULL; LPSKYETEK_STRING lpStr = NULL; SKYETEK_STATUS st; TCHAR addr[256]; unsigned int numDevices = 0; unsigned int numReaders = 0; unsigned int i = 0; double f = 0.0; // set debugger -- uncomment this to see debug output // SkyeTek_SetDebugger(debug); // if you already know your USB address... // USB1: \\?\hid#vid_afef&pid_0f01#6&119ec940&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030} // NOTE: you need to escape the slash '\' character with a slash memset(addr,0,256*sizeof(TCHAR)); _tcscpy(addr,_T("\\\\?\\hid#vid_afef&pid_0f01#6&119ec940&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}")); /* -------------- USE THIS TO GET YOUR USB ADDRESS --------------- // discover devices _tprintf(_T("Discovering devices...\n")); numDevices = SkyeTek_DiscoverDevices(&devices); if( numDevices == 0 ) { _tprintf(_T("error: could not discover any devices\n")); goto failure; } _tprintf(_T("Discovered %d devices\n"), numDevices); for( i = 0; i < numDevices; i++ ) _tprintf(_T("Device %d: %s: %s\n"), i, devices[i]->friendly, devices[i]->address); // discover readers _tprintf(_T("Discovering readers...\n")); numReaders = SkyeTek_DiscoverReaders(devices, numDevices, &readers); if( numReaders == 0 ) { _tprintf(_T("error: could not discover any readers\n")); goto failure; } // find USB reader for( i = 0; i < numReaders; i++ ) { if( _tcscmp(readers[i]->lpDevice->type,SKYETEK_USB_DEVICE_TYPE) == 0 ) { _tprintf(_T("Found USB reader: %s\n"), readers[i]->friendly); _tcscpy(addr,readers[i]->lpDevice->address); break; } } // find any? if( _tcslen(addr) == 0 ) { _tprintf(_T("error: could not discover any readers\n")); goto failure; } // clean up readers and devices SkyeTek_FreeReaders(readers,numReaders); SkyeTek_FreeDevices(devices,numDevices); numReaders = 0; numDevices = 0; ------------- END -------------------------*/ // create new USB device st = SkyeTek_CreateDevice(addr, &lpDevice); if( st != SKYETEK_SUCCESS ) { _tprintf(_T("error: could not create USB port device: %s: %s\n"), addr, SkyeTek_GetStatusMessage(st)); goto failure; } // open device st = SkyeTek_OpenDevice(lpDevice); if( st != SKYETEK_SUCCESS ) { _tprintf(_T("error: could not open USB port: %s: %s\n"), addr, SkyeTek_GetStatusMessage(st)); goto failure; } _tprintf(_T("connected to %s\n"), addr); // create reader st = SkyeTek_CreateReader(lpDevice, &lpReader); if( st != SKYETEK_SUCCESS ) { _tprintf(_T("error: could not find reader on USB port: %s: %s\n"), addr, SkyeTek_GetStatusMessage(st)); goto failure; } _tprintf(_T("created USB reader: %s\n"), lpReader->friendly); // get system parameter st = SkyeTek_GetSystemParameter(lpReader,SYS_FIRMWARE,&lpData); if( st != SKYETEK_SUCCESS ) { _tprintf(_T("error: could not get SYS_FIRMWARE: %s\n"), SkyeTek_GetStatusMessage(st)); goto failure; } // check value if( lpData == NULL || lpData->size == 0 ) { _tprintf(_T("error: SYS_FIRMWARE is NULL or empty\n")); goto failure; } // print frequency value lpStr = SkyeTek_GetStringFromData(lpData); _tprintf(_T("current SYS_FIRMWARE is: 0x%s\n"), lpStr); SkyeTek_FreeString(lpStr); SkyeTek_FreeData(lpData); SkyeTek_FreeReader(lpReader); // do nothing if NULL SkyeTek_FreeDevice(lpDevice); // do nothing if NULL _tprintf(_T("done\n")); return 1; failure: if( numReaders > 0 ) SkyeTek_FreeReaders(readers,numReaders); if( numDevices > 0 ) SkyeTek_FreeDevices(devices,numDevices); SkyeTek_FreeReader(lpReader); // do nothing if NULL SkyeTek_FreeDevice(lpDevice); // do nothing if NULL SkyeTek_FreeData(lpData); // do nothing if NULL return 0; }
// chngintf [numTimes] int _tmain(int argc, TCHAR* argv[]) { LPSKYETEK_READER lpReader = NULL; LPSKYETEK_DEVICE *devices = NULL; LPSKYETEK_READER *readers = NULL; LPSKYETEK_TAG *lpTags = NULL; LPSKYETEK_DATA lpData = NULL; LPSKYETEK_DATA lpDataOrig = NULL; SKYETEK_ADDRESS addr; SKYETEK_STATUS st; TCHAR *str = NULL; unsigned short count; unsigned int numDevices; unsigned int numReaders; unsigned int i = 0; double f = 0.0; int numTimes = 10; int failures = 0; int index = 0; unsigned char tto; unsigned char ctl; // Initialize debugging fp = _tfopen(_T("debug.txt"),_T("w")); if( fp == NULL ) { _tprintf(_T("ERROR: could not open debug.txt output file\n")); return 0; } output(_T("SkyeTek API Change EM Config Example\n")); SkyeTek_SetDebugger(debug); // Get command line arguments if( argc >= 2 ) { numTimes = _ttoi(argv[1]); } // Discover reader output(_T("Discovering reader...\n")); numDevices = SkyeTek_DiscoverDevices(&devices); if( numDevices == 0 ) { output(_T("*** ERROR: No devices found.\n")); fclose(fp); return 0; } output(_T("Discovered %d devices\n"), numDevices); numReaders = SkyeTek_DiscoverReaders(devices, numDevices, &readers); if( numReaders == 0 ) { SkyeTek_FreeDevices(devices,numDevices); output(_T("*** ERROR: No readers found.\n")); fclose(fp); return 0; } lpReader = NULL; for( int i = 0; i < (int)numReaders; i++ ) { output(_T("Found reader: %s [%s]\n"), readers[i]->friendly, readers[i]->lpDevice->address); output(_T("Firmware: %s\n"), readers[i]->firmware); if( _tcscmp(readers[0]->model,_T("M9")) == 0 ) { lpReader = readers[i]; break; } } if( lpReader == NULL ) { output(_T("*** ERROR: No M9 found; this test is only for M9 readers.\n")); SkyeTek_FreeReaders(readers, numReaders); SkyeTek_FreeDevices(devices, numDevices); fclose(fp); return 0; } // Increase the timeout output(_T("Setting additional timeout: 5 seconds\n")); SkyeTek_SetAdditionalTimeout(lpReader->lpDevice,5000); // Set retry count lpData = SkyeTek_AllocateData(1); lpData->data[0] = 20; st = SkyeTek_SetSystemParameter(lpReader,SYS_COMMAND_RETRY,lpData); SkyeTek_FreeData(lpData); lpData = NULL; if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: failed to set M9 retries to 20: %s\n"), STPV3_LookupResponse(st)); SkyeTek_FreeReaders(readers, numReaders); SkyeTek_FreeDevices(devices, numDevices); fclose(fp); return 0; } output(_T("Set M9 retries to 20\n"), STPV3_LookupResponse(st)); // Discover tags lpTags = NULL; count = 0; st = SkyeTek_GetTags(lpReader,EM4444,&lpTags,&count); if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: SkyeTek_GetTags failed to find an EM4444 tag: %s\n"), readers[0]->friendly); SkyeTek_FreeReaders(readers, numReaders); SkyeTek_FreeDevices(devices, numDevices); fclose(fp); return 0; } if( count == 0 ) { output(_T("*** ERROR: Could not find any EM4444 tags in the field\n")); SkyeTek_FreeReaders(readers, numReaders); SkyeTek_FreeDevices(devices, numDevices); fclose(fp); return 0; } output(_T("Tag ID: %s\n"), lpTags[0]->friendly); output(_T("Tag Type: %s\n"), SkyeTek_GetTagTypeNameFromType(lpTags[0]->type)); // Loop and read and write config for( int i = 0; i < numTimes; i++ ) { // Loop output(_T("Loop %d...\n"),i); // Read configuration output(_T("Reading tag current configuration\n")); addr.start = 0x000F; addr.blocks = 1; st = SkyeTek_ReadTagData(lpReader,lpTags[0],&addr,0,0,&lpDataOrig); if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: Failed to read tag configuration: %s\n"),STPV3_LookupResponse(st)); failures++; continue; } str = SkyeTek_GetStringFromData(lpDataOrig); output(_T("Current system page: %s\n"),str); SkyeTek_FreeString(str); lpData = NULL; // Adjust index index++; if( index >= 8 ) index = 0; addr.start = 7; addr.blocks = 1; lpData = SkyeTek_AllocateData(1); lpData->data[0] = ctlVals[index]; //lpDataOrig->data[0] &= 0xC3; //lpData->data[0] |= lpDataOrig->data[0]; ctl = lpData->data[0]; st = SkyeTek_WriteTagConfig(lpReader,lpTags[0],&addr,lpData); if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: Could not set control bits to 0x%02X: %s\n"), ctl, STPV3_LookupResponse(st)); SkyeTek_FreeData(lpData); lpData = NULL; SkyeTek_FreeData(lpDataOrig); lpDataOrig = NULL; failures++; continue; } // Write configuration addr.start = 6; addr.blocks = 1; lpData->data[0] = ttoVals[index]; tto = lpData->data[0]; st = SkyeTek_WriteTagConfig(lpReader,lpTags[0],&addr,lpData); if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: Could not set TTO to 0x%02X: %s\n"), tto, STPV3_LookupResponse(st)); SkyeTek_FreeData(lpData); lpData = NULL; SkyeTek_FreeData(lpDataOrig); lpDataOrig = NULL; failures++; continue; } SkyeTek_FreeData(lpData); lpData = NULL; SkyeTek_FreeData(lpDataOrig); lpDataOrig = NULL; // Read the configuration addr.start = 6; addr.blocks = 1; st = SkyeTek_ReadTagConfig(lpReader,lpTags[0],&addr,&lpData); if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: Could not read TTO configuration: %s\n"),STPV3_LookupResponse(st)); failures++; continue; } if( lpData->data[0] != tto ) { output(_T("*** ERROR: TTO does not match what was written: read: 0x%02X != written: 0x%02X\n"), lpData->data[0], tto); SkyeTek_FreeData(lpData); lpData = NULL; failures++; continue; } output(_T("TTO matches what was written: read: 0x%02X == written: 0x%02X\n"), lpData->data[0], tto); SkyeTek_FreeData(lpData); lpData = NULL; addr.start = 7; addr.blocks = 1; st = SkyeTek_ReadTagConfig(lpReader,lpTags[0],&addr,&lpData); if( st != SKYETEK_SUCCESS ) { output(_T("*** ERROR: Could not read control bits configuration: %s\n"),STPV3_LookupResponse(st)); failures++; continue; } lpData->data[0] &= 0x3C; ctl &= 0x3C; if( lpData->data[0] != ctl ) { output(_T("*** ERROR: Control bits do not match what was written: read: 0x%02X != written: 0x%02X\n"), lpData->data[0], ctl); SkyeTek_FreeData(lpData); lpData = NULL; failures++; continue; } output(_T("Control bits matches what was written: read: 0x%02X == written: 0x%02X\n"), lpData->data[0], ctl); SkyeTek_FreeData(lpData); lpData = NULL; } // end loop // Report result if( numTimes > 0 ) { double percent = 0.0; percent = 100*((double)(numTimes-failures))/((double)numTimes); output(_T("Failed %d times out of %d attempts\n"), failures, numTimes); output(_T("RESULTS: Loop success percentage: %.01f %%\n"), percent); } SkyeTek_FreeTags(lpReader,lpTags,count); SkyeTek_FreeReaders(readers, numReaders); SkyeTek_FreeDevices(devices, numDevices); output(_T("Done.\n")); fclose(fp); return 1; }