Codebase list libfreefare / 5683179
astyle --formatted --mode=c --indent=spaces=2 --indent-switches --indent-preprocessor --keep-one-line-blocks --max-instatement-indent=60 --brackets=linux --pad-oper --unpad-paren --pad-header --align-pointer=name Philippe Teuwen 11 years ago
21 changed file(s) with 116 addition(s) and 116 deletion(s). Raw diff Collapse all Expand all
8787 case 0x30: // Mifare read
8888 // block address is in pbtInput[1]
8989 *pszOutput = 15;
90 strcpy((char*)pbtOutput, "You read block ");
90 strcpy((char *)pbtOutput, "You read block ");
9191 pbtOutput[15] = pbtInput[1];
9292 break;
9393 case 0x50: // HLTA (ISO14443-3)
6262 }
6363
6464 static void
65 print_usage(const char* progname)
65 print_usage(const char *progname)
6666 {
6767 printf("usage: %s [-v]\n", progname);
6868 printf(" -v\t verbose display\n");
6666
6767 #define MAX_FRAME_LEN 264
6868
69 int main(int argc, const char* argv[])
69 int main(int argc, const char *argv[])
7070 {
7171 nfc_device *pnd;
7272 uint8_t abtRx[MAX_FRAME_LEN];
7373 uint8_t abtTx[MAX_FRAME_LEN];
7474 size_t szRx = sizeof(abtRx);
7575 size_t szTx;
76 FILE* input = NULL;
76 FILE *input = NULL;
7777
7878 if (argc >= 2) {
7979 if ((input = fopen(argv[1], "r")) == NULL) {
5353 void *data;
5454 };
5555
56 NFC_EXPORT int nfc_emulate_target(nfc_device* pnd, struct nfc_emulator *emulator);
56 NFC_EXPORT int nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator);
5757
5858 #ifdef __cplusplus
5959 }
119119 NFC_EXPORT int nfc_device_get_information_about(nfc_device *pnd, char *buf, size_t buflen);
120120
121121 /* String converter functions */
122 NFC_EXPORT const char * str_nfc_modulation_type(const nfc_modulation_type nmt);
123 NFC_EXPORT const char * str_nfc_baud_rate(const nfc_baud_rate nbr);
122 NFC_EXPORT const char *str_nfc_modulation_type(const nfc_modulation_type nmt);
123 NFC_EXPORT const char *str_nfc_baud_rate(const nfc_baud_rate nbr);
124124
125125
126126 /* Error codes */
126126 if (available_bytes_count == 0) {
127127 return;
128128 }
129 char* rx = malloc(available_bytes_count);
129 char *rx = malloc(available_bytes_count);
130130 // There is something available, read the data
131131 res = read(UART_DATA(sp)->fd, rx, available_bytes_count);
132132 log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count);
249249 int
250250 uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout)
251251 {
252 int iAbortFd = abort_p ? *((int*)abort_p) : 0;
252 int iAbortFd = abort_p ? *((int *)abort_p) : 0;
253253 int received_bytes_count = 0;
254254 int available_bytes_count = 0;
255255 const int expected_bytes_count = (int)szRx;
138138 }
139139
140140 int
141 uart_receive(serial_port sp, uint8_t * pbtRx, const size_t szRx, void * abort_p, int timeout)
141 uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout)
142142 {
143143 DWORD dwBytesToGet = (DWORD)szRx;
144144 DWORD dwBytesReceived = 0;
162162
163163 // TODO Enhance the reception method
164164 // - According to MSDN, it could be better to implement nfc_abort_command() mecanism using Cancello()
165 volatile bool * abort_flag_p = (volatile bool *)abort_p;
165 volatile bool *abort_flag_p = (volatile bool *)abort_p;
166166 do {
167167 log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "ReadFile");
168168 res = ReadFile(((struct serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived,
193193 }
194194
195195 int
196 uart_send(serial_port sp, const uint8_t * pbtTx, const size_t szTx, int timeout)
196 uart_send(serial_port sp, const uint8_t *pbtTx, const size_t szTx, int timeout)
197197 {
198198 DWORD dwTxLen = 0;
199199
237237 char **
238238 uart_list_ports(void)
239239 {
240 char **availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char*));
240 char **availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char *));
241241 int curIndex = 0;
242242 int i;
243243 for (i = 1; i <= MAX_SERIAL_PORT_WIN; i++) {
244244 if (is_port_available(i)) {
245 availablePorts[curIndex] = (char*)malloc(10);
245 availablePorts[curIndex] = (char *)malloc(10);
246246 sprintf(availablePorts[curIndex], "COM%d", i);
247247 // printf("found candidate port: %s\n", availablePorts[curIndex]);
248248 curIndex++;
9494 }
9595
9696 if (!CHIP_DATA(pnd)->supported_modulation_as_target) {
97 CHIP_DATA(pnd)->supported_modulation_as_target = (nfc_modulation_type*) pn53x_supported_modulation_as_target;
97 CHIP_DATA(pnd)->supported_modulation_as_target = (nfc_modulation_type *) pn53x_supported_modulation_as_target;
9898 }
9999
100100 // CRC handling should be enabled by default as declared in nfc_device_new
11881188 const int timeout)
11891189 {
11901190 const uint8_t abtPassiveInitiatorData[] = { 0x00, 0xff, 0xff, 0x00, 0x0f }; // Only for 212/424 kpbs: First 4 bytes shall be set like this according to NFCIP-1, last byte is TSN (Time Slot Number)
1191 const uint8_t * pbtPassiveInitiatorData = NULL;
1191 const uint8_t *pbtPassiveInitiatorData = NULL;
11921192
11931193 switch (nbr) {
11941194 case NBR_212:
23442344 int
23452345 pn53x_InAutoPoll(struct nfc_device *pnd,
23462346 const pn53x_target_type *ppttTargetTypes, const size_t szTargetTypes,
2347 const uint8_t btPollNr, const uint8_t btPeriod, nfc_target * pntTargets, const int timeout)
2347 const uint8_t btPollNr, const uint8_t btPeriod, nfc_target *pntTargets, const int timeout)
23482348 {
23492349 size_t szTargetFound = 0;
23502350 if (CHIP_DATA(pnd)->type != PN532) {
28482848 {
28492849 switch (nmt) {
28502850 case NMT_FELICA:
2851 *supported_br = (nfc_baud_rate*)pn53x_felica_supported_baud_rates;
2851 *supported_br = (nfc_baud_rate *)pn53x_felica_supported_baud_rates;
28522852 break;
28532853 case NMT_ISO14443A:
2854 *supported_br = (nfc_baud_rate*)pn53x_iso14443a_supported_baud_rates;
2854 *supported_br = (nfc_baud_rate *)pn53x_iso14443a_supported_baud_rates;
28552855 break;
28562856 case NMT_ISO14443B:
28572857 case NMT_ISO14443BI:
28582858 case NMT_ISO14443B2SR:
28592859 case NMT_ISO14443B2CT: {
28602860 if ((CHIP_DATA(pnd)->type != PN533)) {
2861 *supported_br = (nfc_baud_rate*)pn532_iso14443b_supported_baud_rates;
2861 *supported_br = (nfc_baud_rate *)pn532_iso14443b_supported_baud_rates;
28622862 } else {
2863 *supported_br = (nfc_baud_rate*)pn533_iso14443b_supported_baud_rates;
2863 *supported_br = (nfc_baud_rate *)pn533_iso14443b_supported_baud_rates;
28642864 }
28652865 }
28662866 break;
28672867 case NMT_JEWEL:
2868 *supported_br = (nfc_baud_rate*)pn53x_jewel_supported_baud_rates;
2868 *supported_br = (nfc_baud_rate *)pn53x_jewel_supported_baud_rates;
28692869 break;
28702870 case NMT_DEP:
2871 *supported_br = (nfc_baud_rate*)pn53x_dep_supported_baud_rates;
2871 *supported_br = (nfc_baud_rate *)pn53x_dep_supported_baud_rates;
28722872 break;
28732873 default:
28742874 return NFC_EINVARG;
537537 }
538538 iDevice = 0;
539539 while ((acPort = acPorts[iDevice++])) {
540 free((void*)acPort);
540 free((void *)acPort);
541541 }
542542 free(acPorts);
543543 #endif /* SERIAL_AUTOPROBE_ENABLED */
155155 }
156156 iDevice = 0;
157157 while ((acPort = acPorts[iDevice++])) {
158 free((void*)acPort);
158 free((void *)acPort);
159159 }
160160 free(acPorts);
161161 #endif /* SERIAL_AUTOPROBE_ENABLED */
377377 #ifndef WIN32
378378 abort_p = &(DRIVER_DATA(pnd)->iAbortFds[1]);
379379 #else
380 abort_p = (void*) & (DRIVER_DATA(pnd)->abort_flag);
380 abort_p = (void *) & (DRIVER_DATA(pnd)->abort_flag);
381381 #endif
382382
383383 pnd->last_error = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout);
507507 if (0 == memcmp(abtRx, arygon_error_none, 6)) {
508508 uint8_t *p = abtRx + 6;
509509 unsigned int szData;
510 sscanf((const char*)p, "%02x%s", &szData, p);
510 sscanf((const char *)p, "%02x%s", &szData, p);
511511 memcpy(str, p, szData);
512512 *(str + szData) = '\0';
513513 }
133133 }
134134 iDevice = 0;
135135 while ((acPort = acPorts[iDevice++])) {
136 free((void*)acPort);
136 free((void *)acPort);
137137 }
138138 free(acPorts);
139139 #endif /* SERIAL_AUTOPROBE_ENABLED */
357357 #ifndef WIN32
358358 abort_p = &(DRIVER_DATA(pnd)->iAbortFds[1]);
359359 #else
360 abort_p = (void*) & (DRIVER_DATA(pnd)->abort_flag);
360 abort_p = (void *) & (DRIVER_DATA(pnd)->abort_flag);
361361 #endif
362362
363363 pnd->last_error = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout);
2525 #include "nfc-internal.h"
2626
2727 void
28 prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t * pszInitiatorData)
28 prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData)
2929 {
3030 switch (nm.nmt) {
3131 case NMT_ISO14443B: {
134134
135135 struct nfc_driver {
136136 const char *name;
137 bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t * pszDeviceFound);
137 bool (*probe)(nfc_connstring connstrings[], size_t connstrings_len, size_t *pszDeviceFound);
138138 struct nfc_device *(*open)(const nfc_connstring connstring);
139139 void (*close)(struct nfc_device *pnd);
140140 const char *(*strerror)(const struct nfc_device *pnd);
141141
142142 int (*initiator_init)(struct nfc_device *pnd);
143 int (*initiator_select_passive_target)(struct nfc_device *pnd, const nfc_modulation nm, const uint8_t * pbtInitData, const size_t szInitData, nfc_target * pnt);
144 int (*initiator_poll_target)(struct nfc_device *pnd, const nfc_modulation * pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target * pnt);
145 int (*initiator_select_dep_target)(struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info * pndiInitiator, nfc_target * pnt, const int timeout);
143 int (*initiator_select_passive_target)(struct nfc_device *pnd, const nfc_modulation nm, const uint8_t *pbtInitData, const size_t szInitData, nfc_target *pnt);
144 int (*initiator_poll_target)(struct nfc_device *pnd, const nfc_modulation *pnmModulations, const size_t szModulations, const uint8_t uiPollNr, const uint8_t btPeriod, nfc_target *pnt);
145 int (*initiator_select_dep_target)(struct nfc_device *pnd, const nfc_dep_mode ndm, const nfc_baud_rate nbr, const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout);
146146 int (*initiator_deselect_target)(struct nfc_device *pnd);
147 int (*initiator_transceive_bytes)(struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, const size_t szRx, int timeout);
148 int (*initiator_transceive_bits)(struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, uint8_t * pbtRxPar);
149 int (*initiator_transceive_bytes_timed)(struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, uint8_t * pbtRx, uint32_t * cycles);
150 int (*initiator_transceive_bits_timed)(struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar, uint8_t * pbtRx, uint8_t * pbtRxPar, uint32_t * cycles);
147 int (*initiator_transceive_bytes)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, const size_t szRx, int timeout);
148 int (*initiator_transceive_bits)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar);
149 int (*initiator_transceive_bytes_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, uint32_t *cycles);
150 int (*initiator_transceive_bits_timed)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar, uint8_t *pbtRx, uint8_t *pbtRxPar, uint32_t *cycles);
151151 int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target nt);
152152
153 int (*target_init)(struct nfc_device *pnd, nfc_target * pnt, uint8_t * pbtRx, const size_t szRx, int timeout);
154 int (*target_send_bytes)(struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTx, int timeout);
155 int (*target_receive_bytes)(struct nfc_device *pnd, uint8_t * pbtRx, const size_t szRxLen, int timeout);
156 int (*target_send_bits)(struct nfc_device *pnd, const uint8_t * pbtTx, const size_t szTxBits, const uint8_t * pbtTxPar);
157 int (*target_receive_bits)(struct nfc_device *pnd, uint8_t * pbtRx, const size_t szRxLen, uint8_t * pbtRxPar);
153 int (*target_init)(struct nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout);
154 int (*target_send_bytes)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout);
155 int (*target_receive_bytes)(struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, int timeout);
156 int (*target_send_bits)(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar);
157 int (*target_receive_bits)(struct nfc_device *pnd, uint8_t *pbtRx, const size_t szRxLen, uint8_t *pbtRxPar);
158158
159159 int (*device_set_property_bool)(struct nfc_device *pnd, const nfc_property property, const bool bEnable);
160160 int (*device_set_property_int)(struct nfc_device *pnd, const nfc_property property, const int value);
200200 nfc_device *nfc_device_new(const nfc_connstring connstring);
201201 void nfc_device_free(nfc_device *dev);
202202
203 void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t * pbtCascadedUID, size_t * pszCascadedUID);
204
205 void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t * pszInitiatorData);
203 void iso14443_cascade_uid(const uint8_t abtUID[], const size_t szUID, uint8_t *pbtCascadedUID, size_t *pszCascadedUID);
204
205 void prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, size_t *pszInitiatorData);
206206
207207 #endif // __NFC_INTERNAL_H__
8484 size_t szRx = sizeof(abtRx);
8585 int res = nfc_target_init(device, &nt, abtRx, sizeof(abtRx), 0);
8686 cut_assert_operator_int(res, > , 0, cut_message("Can't initialize NFC device as target: %s", nfc_strerror(device)));
87 if (res < 0) { thread_res = -1; return (void*) thread_res; }
87 if (res < 0) { thread_res = -1; return (void *) thread_res; }
8888
8989 res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
9090 cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device)));
9191 szRx = (size_t) res;
9292 const uint8_t abtAttRx[] = "Hello DEP target!";
9393 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
94 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
94 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
9595
9696 const uint8_t abtTx[] = "Hello DEP initiator!";
9797 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
9898 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
99 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
99 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
100100
101101 return (void *) thread_res;
102102 }
116116 printf("=========== INITIATOR %s =========\n", nfc_device_get_name(device));
117117 int res = nfc_initiator_init(device);
118118 cut_assert_equal_int(0, res, cut_message("Can't initialize NFC device as initiator: %s", nfc_strerror(device)));
119 if (res < 0) { thread_res = -1; return (void*) thread_res; }
119 if (res < 0) { thread_res = -1; return (void *) thread_res; }
120120
121121 nfc_target nt;
122122
129129 cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
130130 cut_assert_equal_int(NDM_ACTIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
131131 cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
132 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
132 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
133133
134134 const uint8_t abtTx[] = "Hello DEP target!";
135135 uint8_t abtRx[1024];
139139
140140 const uint8_t abtAttRx[] = "Hello DEP initiator!";
141141 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
142 if (res < 0) { thread_res = -1; return (void*) thread_res; }
142 if (res < 0) { thread_res = -1; return (void *) thread_res; }
143143 res = nfc_initiator_deselect_target(device);
144144 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
145 if (res < 0) { thread_res = -1; return (void*) thread_res; }
145 if (res < 0) { thread_res = -1; return (void *) thread_res; }
146146
147147 return (void *) thread_res;
148148 }
8383 size_t szRx = sizeof(abtRx);
8484 int res = nfc_target_init(device, &nt, abtRx, szRx, 0);
8585 cut_assert_operator_int(res, > , 0, cut_message("Can't initialize NFC device as target: %s", nfc_strerror(device)));
86 if (res < 0) { thread_res = -1; return (void*) thread_res; }
86 if (res < 0) { thread_res = -1; return (void *) thread_res; }
8787
8888 // First pass
8989 res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
9292
9393 const uint8_t abtAttRx[] = "Hello DEP target!";
9494 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
95 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
95 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
9696
9797 const uint8_t abtTx[] = "Hello DEP initiator!";
9898 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
9999 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
100 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
100 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
101101
102102 // Second pass
103103 res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
105105 szRx = (size_t) res;
106106
107107 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
108 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
109
110 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
111 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
112 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
108 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
109
110 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
111 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
112 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
113113
114114 // Third pass
115115 res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
117117 szRx = (size_t) res;
118118
119119 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
120 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
121
122 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
123 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
124 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
120 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
121
122 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
123 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
124 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
125125
126126 // Fourth pass
127127 res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
129129 szRx = (size_t) res;
130130
131131 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
132 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
133
134 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
135 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
136 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
132 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
133
134 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
135 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
136 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
137137
138138 return (void *) thread_res;
139139 }
153153
154154 int res = nfc_initiator_init(device);
155155 cut_assert_equal_int(0, res, cut_message("Can't initialize NFC device as initiator: %s", nfc_strerror(device)));
156 if (res < 0) { thread_res = -1; return (void*) thread_res; }
156 if (res < 0) { thread_res = -1; return (void *) thread_res; }
157157
158158 nfc_target nt;
159159
166166 cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
167167 cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
168168 cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
169 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
169 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
170170
171171 const uint8_t abtTx[] = "Hello DEP target!";
172172 uint8_t abtRx[1024];
176176
177177 const uint8_t abtAttRx[] = "Hello DEP initiator!";
178178 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
179 if (res < 0) { thread_res = -1; return (void*) thread_res; }
180
181 res = nfc_initiator_deselect_target(device);
182 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
183 if (res < 0) { thread_res = -1; return (void*) thread_res; }
179 if (res < 0) { thread_res = -1; return (void *) thread_res; }
180
181 res = nfc_initiator_deselect_target(device);
182 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
183 if (res < 0) { thread_res = -1; return (void *) thread_res; }
184184
185185 // Passive mode / 212Kbps (second pass)
186186 printf("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name(device));
191191 cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
192192 cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
193193 cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
194 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
194 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
195195
196196 szRx = sizeof(abtRx);
197197 res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, &szRx, 1000);
198198 cut_assert_operator_int(res, >= , 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
199199
200200 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
201 if (res < 0) { thread_res = -1; return (void*) thread_res; }
202
203 res = nfc_initiator_deselect_target(device);
204 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
205 if (res < 0) { thread_res = -1; return (void*) thread_res; }
201 if (res < 0) { thread_res = -1; return (void *) thread_res; }
202
203 res = nfc_initiator_deselect_target(device);
204 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
205 if (res < 0) { thread_res = -1; return (void *) thread_res; }
206206
207207 // Passive mode / 212Kbps
208208 printf("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name(device));
213213 cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
214214 cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
215215 cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
216 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
216 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
217217
218218 szRx = sizeof(abtRx);
219219 res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, &szRx, 5000);
220220 cut_assert_operator_int(res, >= , 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
221221
222222 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
223 if (res < 0) { thread_res = -1; return (void*) thread_res; }
224
225 res = nfc_initiator_deselect_target(device);
226 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
227 if (res < 0) { thread_res = -1; return (void*) thread_res; }
223 if (res < 0) { thread_res = -1; return (void *) thread_res; }
224
225 res = nfc_initiator_deselect_target(device);
226 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
227 if (res < 0) { thread_res = -1; return (void *) thread_res; }
228228
229229 // Passive mode / 424Kbps
230230 printf("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name(device));
235235 cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
236236 cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
237237 cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt.nti.ndi.abtGB, nt.nti.ndi.szGB, cut_message("Invalid target general bytes"));
238 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
238 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
239239
240240 szRx = sizeof(abtRx);
241241 res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, &szRx, 5000);
242242 cut_assert_operator_int(res, >= , 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device)));
243243
244244 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
245 if (res < 0) { thread_res = -1; return (void*) thread_res; }
246
247 res = nfc_initiator_deselect_target(device);
248 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
249 if (res < 0) { thread_res = -1; return (void*) thread_res; }
245 if (res < 0) { thread_res = -1; return (void *) thread_res; }
246
247 res = nfc_initiator_deselect_target(device);
248 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
249 if (res < 0) { thread_res = -1; return (void *) thread_res; }
250250
251251 return (void *) thread_res;
252252 }
6464 // 1) nfc_target_init should take target in idle mode
6565 int res = nfc_target_init(device, &nt, abtRx, szRx, 500);
6666 cut_assert_operator_int(res, >= , 0, cut_message("Can't initialize NFC device as target: %s", nfc_strerror(device)));
67 if (res < 0) { thread_res = -1; return (void*) thread_res; }
67 if (res < 0) { thread_res = -1; return (void *) thread_res; }
6868
6969 // 2) act as target
7070 nfc_target nt1 = {
9090 sleep(6);
9191 res = nfc_target_init(device, &nt1, abtRx, szRx, 0);
9292 cut_assert_operator_int(res, > , 0, cut_message("Can't initialize NFC device as target: %s", nfc_strerror(device)));
93 if (res < 0) { thread_res = -1; return (void*) thread_res; }
93 if (res < 0) { thread_res = -1; return (void *) thread_res; }
9494
9595 res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500);
9696 cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device)));
9898
9999 const uint8_t abtAttRx[] = "Hello DEP target!";
100100 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
101 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
101 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
102102
103103 const uint8_t abtTx[] = "Hello DEP initiator!";
104104 res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500);
105105 cut_assert_operator_int(res, > , 0, cut_message("Can't send bytes to initiator: %s", nfc_strerror(device)));
106 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
106 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
107107
108108 // 3) idle mode
109109 sleep(1);
127127
128128 int res = nfc_initiator_init(device);
129129 cut_assert_equal_int(0, res, cut_message("Can't initialize NFC device as initiator: %s", nfc_strerror(device)));
130 if (res < 0) { thread_res = -1; return (void*) thread_res; }
130 if (res < 0) { thread_res = -1; return (void *) thread_res; }
131131
132132 // 1) As other device should be in idle mode, nfc_initiator_poll_dep_target should return 0
133133 nfc_target nt;
134134 res = nfc_initiator_poll_dep_target(device, NDM_PASSIVE, NBR_106, NULL, &nt, 1000);
135135 cut_assert_equal_int(0, res, cut_message("Problem with nfc_idle"));
136 if (res != 0) { thread_res = -1; return (void*) thread_res; }
136 if (res != 0) { thread_res = -1; return (void *) thread_res; }
137137
138138
139139 // 2 As other device should be in target mode, nfc_initiator_poll_dep_target should be positive.
148148 cut_assert_equal_memory("\x11\x22\x33\x44\x55\x66\x77\x88\x99\xAA", 10, nt1.nti.ndi.abtNFCID3, 10, cut_message("Invalid target NFCID3"));
149149 cut_assert_equal_int(NDM_PASSIVE, nt1.nti.ndi.ndm, cut_message("Invalid target DEP mode"));
150150 cut_assert_equal_memory("\x12\x34\x56\x78", 4, nt1.nti.ndi.abtGB, nt1.nti.ndi.szGB, cut_message("Invalid target general bytes"));
151 if (res <= 0) { thread_res = -1; return (void*) thread_res; }
151 if (res <= 0) { thread_res = -1; return (void *) thread_res; }
152152
153153 const uint8_t abtTx[] = "Hello DEP target!";
154154 uint8_t abtRx[1024];
158158
159159 const uint8_t abtAttRx[] = "Hello DEP initiator!";
160160 cut_assert_equal_memory(abtAttRx, sizeof(abtAttRx), abtRx, szRx, cut_message("Invalid received data"));
161 if (res < 0) { thread_res = -1; return (void*) thread_res; }
161 if (res < 0) { thread_res = -1; return (void *) thread_res; }
162162
163163 res = nfc_initiator_deselect_target(device);
164164 cut_assert_operator_int(res, >= , 0, cut_message("Can't deselect target: %s", nfc_strerror(device)));
165 if (res < 0) { thread_res = -1; return (void*) thread_res; }
165 if (res < 0) { thread_res = -1; return (void *) thread_res; }
166166
167167 // 3) As other device should be in idle mode, nfc_initiator_poll_dep_target should return 0
168168 nfc_target nt2;
169169 res = nfc_initiator_poll_dep_target(device, NDM_PASSIVE, NBR_106, NULL, &nt2, 1000);
170170 cut_assert_equal_int(0, res, cut_message("Problem with nfc_idle"));
171 if (res != 0) { thread_res = -1; return (void*) thread_res; }
171 if (res != 0) { thread_res = -1; return (void *) thread_res; }
172172
173173 return (void *) thread_res;
174174 }
5858 static nfc_device *pnd;
5959
6060 static void
61 print_usage(const char* progname)
61 print_usage(const char *progname)
6262 {
6363 printf("usage: %s [-v]\n", progname);
6464 printf(" -v\t verbose display\n");
6262 };
6363
6464 static void
65 print_success_or_failure(bool bFailure, uint32_t * uiCounter)
65 print_success_or_failure(bool bFailure, uint32_t *uiCounter)
6666 {
6767 printf("%c", (bFailure) ? 'x' : '.');
6868 if (uiCounter)
5959 static nfc_device *pnd;
6060
6161 static void
62 print_usage(const char* progname)
62 print_usage(const char *progname)
6363 {
6464 printf("usage: %s [-v]\n", progname);
6565 printf(" -v\t verbose display\n");
176176 print_usage(argv[0]);
177177 exit(EXIT_FAILURE);
178178 }
179 FILE* message_stream = NULL;
180 FILE* ndef_stream = NULL;
179 FILE *message_stream = NULL;
180 FILE *ndef_stream = NULL;
181181
182182 if ((strlen(ndef_output) == 1) && (ndef_output[0] == '-')) {
183183 message_stream = stderr;
219219
220220 int error = EXIT_SUCCESS;
221221 // Polling payload (SENSF_REQ) must be present (see NFC Digital Protol)
222 const uint8_t *pbtSensfReq = (uint8_t*)"\x00\xff\xff\x01\x00";
222 const uint8_t *pbtSensfReq = (uint8_t *)"\x00\xff\xff\x01\x00";
223223 if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) < 0) {
224224 nfc_perror(pnd, "nfc_initiator_select_passive_target");
225225 error = EXIT_FAILURE;
230230 const uint8_t abtNfcForumSysCode[] = { 0x12, 0xfc };
231231 if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) {
232232 // Retry with special polling
233 const uint8_t *pbtSensfReqNfcForum = (uint8_t*)"\x00\x12\xfc\x01\x00";
233 const uint8_t *pbtSensfReqNfcForum = (uint8_t *)"\x00\x12\xfc\x01\x00";
234234 if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) < 0) {
235235 nfc_perror(pnd, "nfc_initiator_select_passive_target");
236236 error = EXIT_FAILURE;
6767 static bool initiator_only_mode = false;
6868 static bool target_only_mode = false;
6969 static int waiting_time = 0;
70 FILE * fd3;
71 FILE * fd4;
70 FILE *fd3;
71 FILE *fd4;
7272
7373 static void
7474 intr_hdlr(int sig)
335335 // PC/SC pseudo-ATR = 3B 80 80 01 01 if there is no historical bytes
336336
337337 // Creates ATS and copy max 48 bytes of Tk:
338 uint8_t * pbtTk;
338 uint8_t *pbtTk;
339339 size_t szTk;
340340 pbtTk = iso14443a_locate_historical_bytes(ntEmulatedTarget.nti.nai.abtAts, ntEmulatedTarget.nti.nai.szAtsLen, &szTk);
341341 szTk = (szTk > 48) ? 48 : szTk;