|
3 | 3 | NfcComparatorReaderWorker* nfc_comparator_reader_worker_alloc() {
|
4 | 4 | NfcComparatorReaderWorker* nfc_comparator_reader_worker =
|
5 | 5 | malloc(sizeof(NfcComparatorReaderWorker));
|
6 |
| - furi_assert(nfc_comparator_reader_worker); |
7 | 6 | nfc_comparator_reader_worker->nfc = nfc_alloc();
|
8 |
| - nfc_comparator_reader_worker->nfc_device = nfc_device_alloc(); |
9 | 7 | nfc_comparator_reader_worker->nfc_scanner =
|
10 | 8 | nfc_scanner_alloc(nfc_comparator_reader_worker->nfc);
|
| 9 | + nfc_comparator_reader_worker->thread = furi_thread_alloc_ex( |
| 10 | + "NfcComparatorReaderWorker", |
| 11 | + 4096, |
| 12 | + nfc_comparator_reader_worker_task, |
| 13 | + nfc_comparator_reader_worker); |
| 14 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; |
11 | 15 | return nfc_comparator_reader_worker;
|
12 | 16 | }
|
13 | 17 |
|
14 |
| -void nfc_comparator_reader_worker_free(NfcComparatorReaderWorker* nfc_comparator_reader_worker) { |
| 18 | +void nfc_comparator_reader_worker_free(void* context) { |
| 19 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
15 | 20 | furi_assert(nfc_comparator_reader_worker);
|
16 | 21 | nfc_free(nfc_comparator_reader_worker->nfc);
|
17 |
| - nfc_device_free(nfc_comparator_reader_worker->nfc_device); |
18 | 22 | nfc_scanner_free(nfc_comparator_reader_worker->nfc_scanner);
|
| 23 | + furi_thread_free(nfc_comparator_reader_worker->thread); |
19 | 24 | free(nfc_comparator_reader_worker);
|
20 | 25 | }
|
| 26 | + |
| 27 | +void nfc_comparator_reader_worker_scanner_callback(NfcScannerEvent event, void* context) { |
| 28 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 29 | + furi_assert(nfc_comparator_reader_worker); |
| 30 | + switch(event.type) { |
| 31 | + case NfcScannerEventTypeDetected: |
| 32 | + nfc_comparator_reader_worker->protocol = event.data.protocols; |
| 33 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Polling; |
| 34 | + break; |
| 35 | + default: |
| 36 | + break; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +NfcCommand nfc_comparator_reader_worker_poller_callback(NfcGenericEvent event, void* context) { |
| 41 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 42 | + furi_assert(nfc_comparator_reader_worker); |
| 43 | + UNUSED(event); |
| 44 | + NfcCommand command = NfcCommandStop; |
| 45 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Comparing; |
| 46 | + return command; |
| 47 | +} |
| 48 | + |
| 49 | +int32_t nfc_comparator_reader_worker_task(void* context) { |
| 50 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 51 | + nfc_comparator_reader_worker->nfc_scanner = |
| 52 | + nfc_scanner_alloc(nfc_comparator_reader_worker->nfc); |
| 53 | + |
| 54 | + nfc_scanner_start( |
| 55 | + nfc_comparator_reader_worker->nfc_scanner, |
| 56 | + nfc_comparator_reader_worker_scanner_callback, |
| 57 | + nfc_comparator_reader_worker); |
| 58 | + |
| 59 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning; |
| 60 | + while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Scanning) { |
| 61 | + furi_delay_ms(100); |
| 62 | + } |
| 63 | + nfc_scanner_stop(nfc_comparator_reader_worker->nfc_scanner); |
| 64 | + |
| 65 | + NfcPoller* nfc_poller = nfc_poller_alloc( |
| 66 | + nfc_comparator_reader_worker->nfc, nfc_comparator_reader_worker->protocol[0]); |
| 67 | + |
| 68 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Polling; |
| 69 | + nfc_poller_start( |
| 70 | + nfc_poller, nfc_comparator_reader_worker_poller_callback, nfc_comparator_reader_worker); |
| 71 | + |
| 72 | + while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Polling) { |
| 73 | + furi_delay_ms(100); |
| 74 | + } |
| 75 | + |
| 76 | + nfc_poller_stop(nfc_poller); |
| 77 | + |
| 78 | + NfcDevice* data = nfc_device_alloc(); |
| 79 | + nfc_device_set_data( |
| 80 | + data, |
| 81 | + nfc_comparator_reader_worker->protocol[0], |
| 82 | + (NfcDeviceData*)nfc_poller_get_data(nfc_poller)); |
| 83 | + |
| 84 | + nfc_comparator_reader_worker->compare_checks.protocol = |
| 85 | + nfc_device_get_protocol_name(nfc_device_get_protocol(data)) == |
| 86 | + nfc_device_get_protocol_name( |
| 87 | + nfc_device_get_protocol(nfc_comparator_reader_worker->loaded_nfc_card)) ? |
| 88 | + true : |
| 89 | + false; |
| 90 | + |
| 91 | + nfc_poller_free(nfc_poller); |
| 92 | + |
| 93 | + size_t poller_uid_len = 0; |
| 94 | + const uint8_t* poller_uid = nfc_device_get_uid(data, &poller_uid_len); |
| 95 | + FuriString* poller_uid_str = furi_string_alloc(); |
| 96 | + for(size_t i = 0; i < poller_uid_len; i++) { |
| 97 | + furi_string_utf8_push(poller_uid_str, poller_uid[i]); |
| 98 | + } |
| 99 | + |
| 100 | + nfc_device_free(data); |
| 101 | + |
| 102 | + size_t loaded_uid_len = 0; |
| 103 | + const uint8_t* loaded_uid = |
| 104 | + nfc_device_get_uid(nfc_comparator_reader_worker->loaded_nfc_card, &loaded_uid_len); |
| 105 | + FuriString* loaded_uid_str = furi_string_alloc(); |
| 106 | + for(size_t i = 0; i < loaded_uid_len; i++) { |
| 107 | + furi_string_utf8_push(loaded_uid_str, loaded_uid[i]); |
| 108 | + } |
| 109 | + |
| 110 | + nfc_comparator_reader_worker->compare_checks.uid = |
| 111 | + furi_string_cmpi(loaded_uid_str, poller_uid_str) == 0 ? true : false; |
| 112 | + |
| 113 | + furi_string_free(poller_uid_str); |
| 114 | + furi_string_free(loaded_uid_str); |
| 115 | + |
| 116 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; |
| 117 | + |
| 118 | + return 0; |
| 119 | +} |
| 120 | + |
| 121 | +void nfc_comparator_reader_worker_set_compare_nfc_device(void* context, NfcDevice* nfc_device) { |
| 122 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 123 | + furi_assert(nfc_comparator_reader_worker); |
| 124 | + nfc_comparator_reader_worker->loaded_nfc_card = nfc_device; |
| 125 | +} |
| 126 | + |
| 127 | +bool nfc_comparator_reader_worker_is_running(void* context) { |
| 128 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 129 | + return nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped; |
| 130 | +} |
| 131 | + |
| 132 | +NfcComparatorReaderWorkerState nfc_comparator_reader_worker_get_state(void* context) { |
| 133 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 134 | + furi_assert(nfc_comparator_reader_worker); |
| 135 | + return nfc_comparator_reader_worker->state; |
| 136 | +} |
| 137 | + |
| 138 | +void nfc_comparator_reader_worker_stop(void* context) { |
| 139 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 140 | + furi_assert(nfc_comparator_reader_worker); |
| 141 | + if(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) { |
| 142 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped; |
| 143 | + } |
| 144 | + furi_thread_join(nfc_comparator_reader_worker->thread); |
| 145 | +} |
| 146 | + |
| 147 | +void nfc_comparator_reader_worker_start(void* context) { |
| 148 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 149 | + furi_assert(nfc_comparator_reader_worker); |
| 150 | + if(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Stopped) { |
| 151 | + nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning; |
| 152 | + furi_thread_start(nfc_comparator_reader_worker->thread); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +NfcComparatorReaderWorkerCompareChecks |
| 157 | + nfc_comparator_reader_worker_get_compare_checks(void* context) { |
| 158 | + NfcComparatorReaderWorker* nfc_comparator_reader_worker = context; |
| 159 | + furi_assert(nfc_comparator_reader_worker); |
| 160 | + return nfc_comparator_reader_worker->compare_checks; |
| 161 | +} |
0 commit comments