// main entry point int main(int argc, const char *argv[]) { init_leds(); nfc_device *device_1; nfc_target target_1; nfc_context *context_1; nfc_device *device_2; nfc_target target_2; nfc_context *context_2; nfc_device *device_3; nfc_target target_3; nfc_context *context_3; // Initialize libnfc and set the nfc_context nfc_init(&context_1); nfc_init(&context_2); nfc_init(&context_3); if (context_1 == NULL || context_2 == NULL) || context_3 == NULL)) { printf("Unable to init libnfc.\n"); exit(EXIT_FAILURE); } device_1 = nfc_open(context_1, NULL); device_2 = nfc_open(context_1, NULL); device_3 = nfc_open(context_2, NULL); if (device_1 == NULL || device_2 == NULL || device_3 == NULL) { printf("ERROR: %s\n", "Unable to open NFC device."); exit(EXIT_FAILURE); } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } const char *deviceId = nfc_device_get_connstring(pnd); printf("NFC reader: %s opened\n", nfc_device_get_name(pnd)); printf("NFC reader connstring: %s \n", deviceId); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; // get the index of the leds to flickr int index; index = atoi(argv[4]); const char* url; url = argv[3]; char body[50]; while(1){ // always make sure leds are just on properly on init of loop // by giving an out of range index all leds will init flash_leds(12); if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) > 0) { printf("The following (NFC) ISO14443A tag was found:\n"); printf(" ATQA (SENS_RES): "); print_hex(nt.nti.nai.abtAtqa, 2); printf(" UID (NFCID%c): ", (nt.nti.nai.abtUid[0] == 0x08 ? '3' : '1')); print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf(" SAK (SEL_RES): "); print_hex(&nt.nti.nai.btSak, 1); if (nt.nti.nai.szAtsLen) { printf(" ATS (ATR): "); print_hex(nt.nti.nai.abtAts, nt.nti.nai.szAtsLen); } // convert the tag into human readable format to work with char usertagid[20]=""; size_t szPos; for(szPos=0; szPos<nt.nti.nai.szUidLen;szPos++){ sprintf(usertagid + strlen(usertagid), "%02x", nt.nti.nai.abtUid[szPos]); } // logging printf("tag found w/ id \"%s\"\n", usertagid); printf("writing to file \"%s\"", argv[2]); flash_leds(index); usleep(60*1000); flash_leds(12); usleep(60*1000); flash_leds(index); usleep(60*1000); flash_leds(12); // get time //struct timeval tv; //gettimeofday(&tv,NULL); //tv.tv_sec // seconds //tv.tv_usec // microseconds int ctime; ctime = ((unsigned)time(NULL))*1000; // convert to ms // write that tag to the file arg // TODO write all params so we can use this as a backup in case of no network FILE *file; file = fopen(argv[2],"a+"); /* apend file (add text to a file or create a file if it does not exist.*/ fprintf(file,"%d;%s\n",ctime, usertagid); /*writes*/ fclose(file); /*done!*/ CURL *curl; CURLcode res; /* get a curl handle */ curl = curl_easy_init(); if(curl) { /* First set the URL that is about to receive our POST. This URL can just as well be a https:// URL if that is what should receive the data. */ curl_easy_setopt(curl, CURLOPT_URL, url); http://stackoverflow.com/questions/11444583/command-line-curl-timeout-parameter curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 2); /* Now specify the POST data */ // prepare the body // timestamp and other fields to be added here instead of this sample data sprintf( body, "timestamp=%d&tagId=%s&%s", ctime, usertagid, argv[5]); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body); // logging posting to printf("posting to url [%s] with body [%s]", url, body); /* Perform the request, res will get the return code */ res = curl_easy_perform(curl); /* Check for errors */ if(res != CURLE_OK) fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); /* always cleanup */ curl_easy_cleanup(curl); } } // end of found target if // add some wait time here, sleep expects seconds // check usleep, should work with microseconds instead usleep(100); }// end of loop // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { LONG rv; SCARDCONTEXT hContext; SCARDHANDLE hCard; char *reader; BYTE pbSendBuffer[1 + 1 + sizeof(nfc_connstring)]; DWORD dwSendLength; BYTE pbRecvBuffer[1]; DWORD dwActiveProtocol, dwRecvLength, dwReaders; char* mszReaders = NULL; if (argc == 1 || (argc == 2 && (strncmp(argv[1], "yes", strlen("yes")) == 0))) pbSendBuffer[0] = IFDNFC_SET_ACTIVE; else if (argc == 2 && (strncmp(argv[1], "no", strlen("no")) == 0)) pbSendBuffer[0] = IFDNFC_SET_INACTIVE; else if (argc == 2 && (strncmp(argv[1], "se", strlen("se")) == 0)) pbSendBuffer[0] = IFDNFC_SET_ACTIVE_SE; else if (argc == 2 && (strncmp(argv[1], "status", strlen("status")) == 0)) pbSendBuffer[0] = IFDNFC_GET_STATUS; else { printf("Usage: %s [yes|no|status]\n", argv[0]); exit(EXIT_FAILURE); } rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext); if (rv < 0) goto pcsc_error; dwReaders = 0; // Ask how many bytes readers list take rv = SCardListReaders(hContext, NULL, NULL, &dwReaders); if (rv < 0) goto pcsc_error; // Then allocate and fill mszReaders mszReaders = malloc(dwReaders); rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders); if (rv < 0) goto pcsc_error; int l; for (reader = mszReaders; dwReaders > 0; l = strlen(reader) + 1, dwReaders -= l, reader += l) { if (strcmp(IFDNFC_READER_NAME, reader) <= 0) break; } if (dwReaders <= 0) { printf("Could not find a reader named: %s\n", IFDNFC_READER_NAME); rv = SCARD_E_NO_READERS_AVAILABLE; goto pcsc_error; } // TODO Handle multiple ifdnfc instance for multiple NFC device ? rv = SCardConnect(hContext, reader, SCARD_SHARE_DIRECT, 0, &hCard, &dwActiveProtocol); if (rv < 0) goto pcsc_error; if ((pbSendBuffer[0] == IFDNFC_SET_ACTIVE) || (pbSendBuffer[0] == IFDNFC_SET_ACTIVE_SE)) { const BYTE command = pbSendBuffer[0]; // To correctly probe NFC devices, ifdnfc must be disactivated first pbSendBuffer[0] = IFDNFC_SET_INACTIVE; dwSendLength = 1; rv = SCardControl(hCard, IFDNFC_CTRL_ACTIVE, pbSendBuffer, dwSendLength, pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength); if (rv < 0) { goto pcsc_error; } pbSendBuffer[0] = command; // Initialize libnfc nfc_context *context; nfc_init(&context); if (context == NULL) { fprintf(stderr, "Unable to init libnfc (malloc)\n"); goto error; } // Allocate nfc_connstring array nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List devices size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); int connstring_index = -1; switch (szDeviceFound) { case 0: fprintf(stderr, "Unable to activate ifdnfc: no NFC device found.\n"); nfc_exit(context); goto error; break; case 1: // Only one NFC device available, so auto-select it! connstring_index = 0; break; default: // More than one available NFC devices, propose a shell menu: printf("%d NFC devices found, please select one:\n", (int)szDeviceFound); for (size_t i = 0; i < szDeviceFound; i++) { nfc_device *pnd = nfc_open(context, connstrings[i]); if (pnd != NULL) { printf("[%d] %s\t (%s)\n", (int)i, nfc_device_get_name(pnd), nfc_device_get_connstring(pnd)); nfc_close(pnd); } else { fprintf(stderr, "nfc_open failed for %s\n", connstrings[i]); } } // libnfc isn't needed anymore nfc_exit(context); printf(">> "); // Take user's choice if (1 != scanf("%2d", &connstring_index)) { fprintf(stderr, "Value must an integer.\n"); goto error; } if ((connstring_index < 0) || (connstring_index >= (int)szDeviceFound)) { fprintf(stderr, "Invalid index selection.\n"); goto error; } break; } printf("Activating ifdnfc with \"%s\"...\n", connstrings[connstring_index]); // pbSendBuffer = { IFDNFC_SET_ACTIVE (1 byte), length (2 bytes), nfc_connstring (lenght bytes)} const uint16_t u16ConnstringLength = strlen(connstrings[connstring_index]) + 1; memcpy(pbSendBuffer + 1, &u16ConnstringLength, sizeof(u16ConnstringLength)); memcpy(pbSendBuffer + 1 + sizeof(u16ConnstringLength), connstrings[connstring_index], u16ConnstringLength); dwSendLength = 1 + sizeof(u16ConnstringLength) + u16ConnstringLength; } else { // pbSendBuffer[0] != IFDNFC_SET_ACTIVE dwSendLength = 1; } rv = SCardControl(hCard, IFDNFC_CTRL_ACTIVE, pbSendBuffer, dwSendLength, pbRecvBuffer, sizeof(pbRecvBuffer), &dwRecvLength); if (rv < 0) { goto pcsc_error; } if (dwRecvLength < 1) { rv = SCARD_F_INTERNAL_ERROR; goto pcsc_error; } switch (pbRecvBuffer[0]) { case IFDNFC_IS_ACTIVE: { uint16_t u16ConnstringLength; if (dwRecvLength < (1 + sizeof(u16ConnstringLength))) { rv = SCARD_F_INTERNAL_ERROR; goto pcsc_error; } memcpy(&u16ConnstringLength, pbRecvBuffer + 1, sizeof(u16ConnstringLength)); if ((dwRecvLength - (1 + sizeof(u16ConnstringLength))) != u16ConnstringLength) { rv = SCARD_F_INTERNAL_ERROR; goto pcsc_error; } nfc_connstring connstring; memcpy(connstring, pbRecvBuffer + 1 + sizeof(u16ConnstringLength), u16ConnstringLength); printf("%s is active using %s.\n", IFDNFC_READER_NAME, connstring); } break; case IFDNFC_IS_INACTIVE: printf("%s is inactive.\n", IFDNFC_READER_NAME); break; default: rv = SCARD_F_INTERNAL_ERROR; goto pcsc_error; } rv = SCardDisconnect(hCard, SCARD_LEAVE_CARD); if (rv < 0) goto pcsc_error; free(mszReaders); exit(EXIT_SUCCESS); pcsc_error: puts(pcsc_stringify_error(rv)); error: if (mszReaders) free(mszReaders); exit(EXIT_FAILURE); }