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
87 | 87 | case 0x30: // Mifare read |
88 | 88 | // block address is in pbtInput[1] |
89 | 89 | *pszOutput = 15; |
90 | strcpy((char*)pbtOutput, "You read block "); | |
90 | strcpy((char *)pbtOutput, "You read block "); | |
91 | 91 | pbtOutput[15] = pbtInput[1]; |
92 | 92 | break; |
93 | 93 | case 0x50: // HLTA (ISO14443-3) |
62 | 62 | } |
63 | 63 | |
64 | 64 | static void |
65 | print_usage(const char* progname) | |
65 | print_usage(const char *progname) | |
66 | 66 | { |
67 | 67 | printf("usage: %s [-v]\n", progname); |
68 | 68 | printf(" -v\t verbose display\n"); |
66 | 66 | |
67 | 67 | #define MAX_FRAME_LEN 264 |
68 | 68 | |
69 | int main(int argc, const char* argv[]) | |
69 | int main(int argc, const char *argv[]) | |
70 | 70 | { |
71 | 71 | nfc_device *pnd; |
72 | 72 | uint8_t abtRx[MAX_FRAME_LEN]; |
73 | 73 | uint8_t abtTx[MAX_FRAME_LEN]; |
74 | 74 | size_t szRx = sizeof(abtRx); |
75 | 75 | size_t szTx; |
76 | FILE* input = NULL; | |
76 | FILE *input = NULL; | |
77 | 77 | |
78 | 78 | if (argc >= 2) { |
79 | 79 | if ((input = fopen(argv[1], "r")) == NULL) { |
53 | 53 | void *data; |
54 | 54 | }; |
55 | 55 | |
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); | |
57 | 57 | |
58 | 58 | #ifdef __cplusplus |
59 | 59 | } |
119 | 119 | NFC_EXPORT int nfc_device_get_information_about(nfc_device *pnd, char *buf, size_t buflen); |
120 | 120 | |
121 | 121 | /* 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); | |
124 | 124 | |
125 | 125 | |
126 | 126 | /* Error codes */ |
126 | 126 | if (available_bytes_count == 0) { |
127 | 127 | return; |
128 | 128 | } |
129 | char* rx = malloc(available_bytes_count); | |
129 | char *rx = malloc(available_bytes_count); | |
130 | 130 | // There is something available, read the data |
131 | 131 | res = read(UART_DATA(sp)->fd, rx, available_bytes_count); |
132 | 132 | log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "%d bytes have eatten.", available_bytes_count); |
249 | 249 | int |
250 | 250 | uart_receive(serial_port sp, uint8_t *pbtRx, const size_t szRx, void *abort_p, int timeout) |
251 | 251 | { |
252 | int iAbortFd = abort_p ? *((int*)abort_p) : 0; | |
252 | int iAbortFd = abort_p ? *((int *)abort_p) : 0; | |
253 | 253 | int received_bytes_count = 0; |
254 | 254 | int available_bytes_count = 0; |
255 | 255 | const int expected_bytes_count = (int)szRx; |
138 | 138 | } |
139 | 139 | |
140 | 140 | 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) | |
142 | 142 | { |
143 | 143 | DWORD dwBytesToGet = (DWORD)szRx; |
144 | 144 | DWORD dwBytesReceived = 0; |
162 | 162 | |
163 | 163 | // TODO Enhance the reception method |
164 | 164 | // - 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; | |
166 | 166 | do { |
167 | 167 | log_put(LOG_CATEGORY, NFC_PRIORITY_TRACE, "ReadFile"); |
168 | 168 | res = ReadFile(((struct serial_port_windows *) sp)->hPort, pbtRx + dwTotalBytesReceived, |
193 | 193 | } |
194 | 194 | |
195 | 195 | 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) | |
197 | 197 | { |
198 | 198 | DWORD dwTxLen = 0; |
199 | 199 | |
237 | 237 | char ** |
238 | 238 | uart_list_ports(void) |
239 | 239 | { |
240 | char **availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char*)); | |
240 | char **availablePorts = malloc((1 + MAX_SERIAL_PORT_WIN) * sizeof(char *)); | |
241 | 241 | int curIndex = 0; |
242 | 242 | int i; |
243 | 243 | for (i = 1; i <= MAX_SERIAL_PORT_WIN; i++) { |
244 | 244 | if (is_port_available(i)) { |
245 | availablePorts[curIndex] = (char*)malloc(10); | |
245 | availablePorts[curIndex] = (char *)malloc(10); | |
246 | 246 | sprintf(availablePorts[curIndex], "COM%d", i); |
247 | 247 | // printf("found candidate port: %s\n", availablePorts[curIndex]); |
248 | 248 | curIndex++; |
94 | 94 | } |
95 | 95 | |
96 | 96 | 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; | |
98 | 98 | } |
99 | 99 | |
100 | 100 | // CRC handling should be enabled by default as declared in nfc_device_new |
1188 | 1188 | const int timeout) |
1189 | 1189 | { |
1190 | 1190 | 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; | |
1192 | 1192 | |
1193 | 1193 | switch (nbr) { |
1194 | 1194 | case NBR_212: |
2344 | 2344 | int |
2345 | 2345 | pn53x_InAutoPoll(struct nfc_device *pnd, |
2346 | 2346 | 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) | |
2348 | 2348 | { |
2349 | 2349 | size_t szTargetFound = 0; |
2350 | 2350 | if (CHIP_DATA(pnd)->type != PN532) { |
2848 | 2848 | { |
2849 | 2849 | switch (nmt) { |
2850 | 2850 | 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; | |
2852 | 2852 | break; |
2853 | 2853 | 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; | |
2855 | 2855 | break; |
2856 | 2856 | case NMT_ISO14443B: |
2857 | 2857 | case NMT_ISO14443BI: |
2858 | 2858 | case NMT_ISO14443B2SR: |
2859 | 2859 | case NMT_ISO14443B2CT: { |
2860 | 2860 | 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; | |
2862 | 2862 | } else { |
2863 | *supported_br = (nfc_baud_rate*)pn533_iso14443b_supported_baud_rates; | |
2863 | *supported_br = (nfc_baud_rate *)pn533_iso14443b_supported_baud_rates; | |
2864 | 2864 | } |
2865 | 2865 | } |
2866 | 2866 | break; |
2867 | 2867 | 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; | |
2869 | 2869 | break; |
2870 | 2870 | 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; | |
2872 | 2872 | break; |
2873 | 2873 | default: |
2874 | 2874 | return NFC_EINVARG; |
537 | 537 | } |
538 | 538 | iDevice = 0; |
539 | 539 | while ((acPort = acPorts[iDevice++])) { |
540 | free((void*)acPort); | |
540 | free((void *)acPort); | |
541 | 541 | } |
542 | 542 | free(acPorts); |
543 | 543 | #endif /* SERIAL_AUTOPROBE_ENABLED */ |
155 | 155 | } |
156 | 156 | iDevice = 0; |
157 | 157 | while ((acPort = acPorts[iDevice++])) { |
158 | free((void*)acPort); | |
158 | free((void *)acPort); | |
159 | 159 | } |
160 | 160 | free(acPorts); |
161 | 161 | #endif /* SERIAL_AUTOPROBE_ENABLED */ |
377 | 377 | #ifndef WIN32 |
378 | 378 | abort_p = &(DRIVER_DATA(pnd)->iAbortFds[1]); |
379 | 379 | #else |
380 | abort_p = (void*) & (DRIVER_DATA(pnd)->abort_flag); | |
380 | abort_p = (void *) & (DRIVER_DATA(pnd)->abort_flag); | |
381 | 381 | #endif |
382 | 382 | |
383 | 383 | pnd->last_error = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout); |
507 | 507 | if (0 == memcmp(abtRx, arygon_error_none, 6)) { |
508 | 508 | uint8_t *p = abtRx + 6; |
509 | 509 | unsigned int szData; |
510 | sscanf((const char*)p, "%02x%s", &szData, p); | |
510 | sscanf((const char *)p, "%02x%s", &szData, p); | |
511 | 511 | memcpy(str, p, szData); |
512 | 512 | *(str + szData) = '\0'; |
513 | 513 | } |
133 | 133 | } |
134 | 134 | iDevice = 0; |
135 | 135 | while ((acPort = acPorts[iDevice++])) { |
136 | free((void*)acPort); | |
136 | free((void *)acPort); | |
137 | 137 | } |
138 | 138 | free(acPorts); |
139 | 139 | #endif /* SERIAL_AUTOPROBE_ENABLED */ |
357 | 357 | #ifndef WIN32 |
358 | 358 | abort_p = &(DRIVER_DATA(pnd)->iAbortFds[1]); |
359 | 359 | #else |
360 | abort_p = (void*) & (DRIVER_DATA(pnd)->abort_flag); | |
360 | abort_p = (void *) & (DRIVER_DATA(pnd)->abort_flag); | |
361 | 361 | #endif |
362 | 362 | |
363 | 363 | pnd->last_error = uart_receive(DRIVER_DATA(pnd)->port, abtRxBuf, 5, abort_p, timeout); |
25 | 25 | #include "nfc-internal.h" |
26 | 26 | |
27 | 27 | 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) | |
29 | 29 | { |
30 | 30 | switch (nm.nmt) { |
31 | 31 | case NMT_ISO14443B: { |
134 | 134 | |
135 | 135 | struct nfc_driver { |
136 | 136 | 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); | |
138 | 138 | struct nfc_device *(*open)(const nfc_connstring connstring); |
139 | 139 | void (*close)(struct nfc_device *pnd); |
140 | 140 | const char *(*strerror)(const struct nfc_device *pnd); |
141 | 141 | |
142 | 142 | 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); | |
146 | 146 | 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); | |
151 | 151 | int (*initiator_target_is_present)(struct nfc_device *pnd, const nfc_target nt); |
152 | 152 | |
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); | |
158 | 158 | |
159 | 159 | int (*device_set_property_bool)(struct nfc_device *pnd, const nfc_property property, const bool bEnable); |
160 | 160 | int (*device_set_property_int)(struct nfc_device *pnd, const nfc_property property, const int value); |
200 | 200 | nfc_device *nfc_device_new(const nfc_connstring connstring); |
201 | 201 | void nfc_device_free(nfc_device *dev); |
202 | 202 | |
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); | |
206 | 206 | |
207 | 207 | #endif // __NFC_INTERNAL_H__ |
84 | 84 | size_t szRx = sizeof(abtRx); |
85 | 85 | int res = nfc_target_init(device, &nt, abtRx, sizeof(abtRx), 0); |
86 | 86 | 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; } | |
88 | 88 | |
89 | 89 | res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); |
90 | 90 | cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device))); |
91 | 91 | szRx = (size_t) res; |
92 | 92 | const uint8_t abtAttRx[] = "Hello DEP target!"; |
93 | 93 | 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; } | |
95 | 95 | |
96 | 96 | const uint8_t abtTx[] = "Hello DEP initiator!"; |
97 | 97 | res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500); |
98 | 98 | 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; } | |
100 | 100 | |
101 | 101 | return (void *) thread_res; |
102 | 102 | } |
116 | 116 | printf("=========== INITIATOR %s =========\n", nfc_device_get_name(device)); |
117 | 117 | int res = nfc_initiator_init(device); |
118 | 118 | 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; } | |
120 | 120 | |
121 | 121 | nfc_target nt; |
122 | 122 | |
129 | 129 | 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")); |
130 | 130 | cut_assert_equal_int(NDM_ACTIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode")); |
131 | 131 | 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; } | |
133 | 133 | |
134 | 134 | const uint8_t abtTx[] = "Hello DEP target!"; |
135 | 135 | uint8_t abtRx[1024]; |
139 | 139 | |
140 | 140 | const uint8_t abtAttRx[] = "Hello DEP initiator!"; |
141 | 141 | 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; } | |
143 | 143 | res = nfc_initiator_deselect_target(device); |
144 | 144 | 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; } | |
146 | 146 | |
147 | 147 | return (void *) thread_res; |
148 | 148 | } |
83 | 83 | size_t szRx = sizeof(abtRx); |
84 | 84 | int res = nfc_target_init(device, &nt, abtRx, szRx, 0); |
85 | 85 | 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; } | |
87 | 87 | |
88 | 88 | // First pass |
89 | 89 | res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); |
92 | 92 | |
93 | 93 | const uint8_t abtAttRx[] = "Hello DEP target!"; |
94 | 94 | 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; } | |
96 | 96 | |
97 | 97 | const uint8_t abtTx[] = "Hello DEP initiator!"; |
98 | 98 | res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500); |
99 | 99 | 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; } | |
101 | 101 | |
102 | 102 | // Second pass |
103 | 103 | res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); |
105 | 105 | szRx = (size_t) res; |
106 | 106 | |
107 | 107 | 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; } | |
113 | 113 | |
114 | 114 | // Third pass |
115 | 115 | res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); |
117 | 117 | szRx = (size_t) res; |
118 | 118 | |
119 | 119 | 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; } | |
125 | 125 | |
126 | 126 | // Fourth pass |
127 | 127 | res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); |
129 | 129 | szRx = (size_t) res; |
130 | 130 | |
131 | 131 | 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; } | |
137 | 137 | |
138 | 138 | return (void *) thread_res; |
139 | 139 | } |
153 | 153 | |
154 | 154 | int res = nfc_initiator_init(device); |
155 | 155 | 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; } | |
157 | 157 | |
158 | 158 | nfc_target nt; |
159 | 159 | |
166 | 166 | 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")); |
167 | 167 | cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode")); |
168 | 168 | 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; } | |
170 | 170 | |
171 | 171 | const uint8_t abtTx[] = "Hello DEP target!"; |
172 | 172 | uint8_t abtRx[1024]; |
176 | 176 | |
177 | 177 | const uint8_t abtAttRx[] = "Hello DEP initiator!"; |
178 | 178 | 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; } | |
184 | 184 | |
185 | 185 | // Passive mode / 212Kbps (second pass) |
186 | 186 | printf("=========== INITIATOR %s (Passive mode / 212Kbps) =========\n", nfc_device_get_name(device)); |
191 | 191 | 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")); |
192 | 192 | cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode")); |
193 | 193 | 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; } | |
195 | 195 | |
196 | 196 | szRx = sizeof(abtRx); |
197 | 197 | res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, &szRx, 1000); |
198 | 198 | cut_assert_operator_int(res, >= , 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device))); |
199 | 199 | |
200 | 200 | 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; } | |
206 | 206 | |
207 | 207 | // Passive mode / 212Kbps |
208 | 208 | printf("=========== INITIATOR %s (Passive mode / 212Kbps, second pass) =========\n", nfc_device_get_name(device)); |
213 | 213 | 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")); |
214 | 214 | cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode")); |
215 | 215 | 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; } | |
217 | 217 | |
218 | 218 | szRx = sizeof(abtRx); |
219 | 219 | res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, &szRx, 5000); |
220 | 220 | cut_assert_operator_int(res, >= , 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device))); |
221 | 221 | |
222 | 222 | 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; } | |
228 | 228 | |
229 | 229 | // Passive mode / 424Kbps |
230 | 230 | printf("=========== INITIATOR %s (Passive mode / 424Kbps) =========\n", nfc_device_get_name(device)); |
235 | 235 | 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")); |
236 | 236 | cut_assert_equal_int(NDM_PASSIVE, nt.nti.ndi.ndm, cut_message("Invalid target DEP mode")); |
237 | 237 | 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; } | |
239 | 239 | |
240 | 240 | szRx = sizeof(abtRx); |
241 | 241 | res = nfc_initiator_transceive_bytes(device, abtTx, sizeof(abtTx), abtRx, &szRx, 5000); |
242 | 242 | cut_assert_operator_int(res, >= , 0, cut_message("Can't transceive bytes to target: %s", nfc_strerror(device))); |
243 | 243 | |
244 | 244 | 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; } | |
250 | 250 | |
251 | 251 | return (void *) thread_res; |
252 | 252 | } |
64 | 64 | // 1) nfc_target_init should take target in idle mode |
65 | 65 | int res = nfc_target_init(device, &nt, abtRx, szRx, 500); |
66 | 66 | 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; } | |
68 | 68 | |
69 | 69 | // 2) act as target |
70 | 70 | nfc_target nt1 = { |
90 | 90 | sleep(6); |
91 | 91 | res = nfc_target_init(device, &nt1, abtRx, szRx, 0); |
92 | 92 | 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; } | |
94 | 94 | |
95 | 95 | res = nfc_target_receive_bytes(device, abtRx, sizeof(abtRx), 500); |
96 | 96 | cut_assert_operator_int(res, > , 0, cut_message("Can't receive bytes from initiator: %s", nfc_strerror(device))); |
98 | 98 | |
99 | 99 | const uint8_t abtAttRx[] = "Hello DEP target!"; |
100 | 100 | 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; } | |
102 | 102 | |
103 | 103 | const uint8_t abtTx[] = "Hello DEP initiator!"; |
104 | 104 | res = nfc_target_send_bytes(device, abtTx, sizeof(abtTx), 500); |
105 | 105 | 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; } | |
107 | 107 | |
108 | 108 | // 3) idle mode |
109 | 109 | sleep(1); |
127 | 127 | |
128 | 128 | int res = nfc_initiator_init(device); |
129 | 129 | 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; } | |
131 | 131 | |
132 | 132 | // 1) As other device should be in idle mode, nfc_initiator_poll_dep_target should return 0 |
133 | 133 | nfc_target nt; |
134 | 134 | res = nfc_initiator_poll_dep_target(device, NDM_PASSIVE, NBR_106, NULL, &nt, 1000); |
135 | 135 | 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; } | |
137 | 137 | |
138 | 138 | |
139 | 139 | // 2 As other device should be in target mode, nfc_initiator_poll_dep_target should be positive. |
148 | 148 | 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")); |
149 | 149 | cut_assert_equal_int(NDM_PASSIVE, nt1.nti.ndi.ndm, cut_message("Invalid target DEP mode")); |
150 | 150 | 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; } | |
152 | 152 | |
153 | 153 | const uint8_t abtTx[] = "Hello DEP target!"; |
154 | 154 | uint8_t abtRx[1024]; |
158 | 158 | |
159 | 159 | const uint8_t abtAttRx[] = "Hello DEP initiator!"; |
160 | 160 | 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; } | |
162 | 162 | |
163 | 163 | res = nfc_initiator_deselect_target(device); |
164 | 164 | 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; } | |
166 | 166 | |
167 | 167 | // 3) As other device should be in idle mode, nfc_initiator_poll_dep_target should return 0 |
168 | 168 | nfc_target nt2; |
169 | 169 | res = nfc_initiator_poll_dep_target(device, NDM_PASSIVE, NBR_106, NULL, &nt2, 1000); |
170 | 170 | 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; } | |
172 | 172 | |
173 | 173 | return (void *) thread_res; |
174 | 174 | } |
58 | 58 | static nfc_device *pnd; |
59 | 59 | |
60 | 60 | static void |
61 | print_usage(const char* progname) | |
61 | print_usage(const char *progname) | |
62 | 62 | { |
63 | 63 | printf("usage: %s [-v]\n", progname); |
64 | 64 | printf(" -v\t verbose display\n"); |
62 | 62 | }; |
63 | 63 | |
64 | 64 | static void |
65 | print_success_or_failure(bool bFailure, uint32_t * uiCounter) | |
65 | print_success_or_failure(bool bFailure, uint32_t *uiCounter) | |
66 | 66 | { |
67 | 67 | printf("%c", (bFailure) ? 'x' : '.'); |
68 | 68 | if (uiCounter) |
59 | 59 | static nfc_device *pnd; |
60 | 60 | |
61 | 61 | static void |
62 | print_usage(const char* progname) | |
62 | print_usage(const char *progname) | |
63 | 63 | { |
64 | 64 | printf("usage: %s [-v]\n", progname); |
65 | 65 | printf(" -v\t verbose display\n"); |
176 | 176 | print_usage(argv[0]); |
177 | 177 | exit(EXIT_FAILURE); |
178 | 178 | } |
179 | FILE* message_stream = NULL; | |
180 | FILE* ndef_stream = NULL; | |
179 | FILE *message_stream = NULL; | |
180 | FILE *ndef_stream = NULL; | |
181 | 181 | |
182 | 182 | if ((strlen(ndef_output) == 1) && (ndef_output[0] == '-')) { |
183 | 183 | message_stream = stderr; |
219 | 219 | |
220 | 220 | int error = EXIT_SUCCESS; |
221 | 221 | // 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"; | |
223 | 223 | if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReq, 5, &nt) < 0) { |
224 | 224 | nfc_perror(pnd, "nfc_initiator_select_passive_target"); |
225 | 225 | error = EXIT_FAILURE; |
230 | 230 | const uint8_t abtNfcForumSysCode[] = { 0x12, 0xfc }; |
231 | 231 | if (0 != memcmp(nt.nti.nfi.abtSysCode, abtNfcForumSysCode, 2)) { |
232 | 232 | // 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"; | |
234 | 234 | if (nfc_initiator_select_passive_target(pnd, nm, pbtSensfReqNfcForum, 5, &nt) < 0) { |
235 | 235 | nfc_perror(pnd, "nfc_initiator_select_passive_target"); |
236 | 236 | error = EXIT_FAILURE; |
67 | 67 | static bool initiator_only_mode = false; |
68 | 68 | static bool target_only_mode = false; |
69 | 69 | static int waiting_time = 0; |
70 | FILE * fd3; | |
71 | FILE * fd4; | |
70 | FILE *fd3; | |
71 | FILE *fd4; | |
72 | 72 | |
73 | 73 | static void |
74 | 74 | intr_hdlr(int sig) |
335 | 335 | // PC/SC pseudo-ATR = 3B 80 80 01 01 if there is no historical bytes |
336 | 336 | |
337 | 337 | // Creates ATS and copy max 48 bytes of Tk: |
338 | uint8_t * pbtTk; | |
338 | uint8_t *pbtTk; | |
339 | 339 | size_t szTk; |
340 | 340 | pbtTk = iso14443a_locate_historical_bytes(ntEmulatedTarget.nti.nai.abtAts, ntEmulatedTarget.nti.nai.szAtsLen, &szTk); |
341 | 341 | szTk = (szTk > 48) ? 48 : szTk; |