make_style as that is best practise

This commit is contained in:
Niel Nielsen
2026-05-11 12:36:51 +02:00
parent 0038fdd959
commit cef8b42c26
46 changed files with 720 additions and 644 deletions
File diff suppressed because it is too large Load Diff
+23 -23
View File
@@ -62,7 +62,7 @@ static bool m_is_a_btn_release = false;
static bool m_system_off_processing = false;
// NFC field generator state
volatile bool m_is_field_on = false;
volatile bool m_is_field_on = false;
// cpu reset reason
static uint32_t m_reset_source;
@@ -156,27 +156,27 @@ static void gpio_te_init(void) {
static void field_generator_rainbow_loop(void) {
static uint8_t color_index = 0;
static uint32_t last_update = 0;
if (!m_is_field_on) return;
uint32_t now = app_timer_cnt_get();
if (app_timer_cnt_diff_compute(now, last_update) < APP_TIMER_TICKS(100)) {
return;
}
last_update = now;
// Rainbow colors
const uint8_t colors[] = {RGB_RED, RGB_YELLOW, RGB_GREEN, RGB_CYAN, RGB_BLUE, RGB_MAGENTA};
set_slot_light_color(colors[color_index]);
uint32_t *led_pins = hw_get_led_array();
// Light up all LEDs with current color
for (int i = 0; i < RGB_LIST_NUM; i++) {
nrf_gpio_pin_set(led_pins[i]);
}
color_index = (color_index + 1) % 6;
}
#endif
@@ -204,9 +204,9 @@ static void timer_button_event_handle(void *arg) {
NRF_LOG_INFO("BUTTON press during shutdown");
return;
}
nrf_drv_gpiote_pin_t pin = *(nrf_drv_gpiote_pin_t *)arg;
// Check here if the current GPIO is at the pressed level
if (nrf_gpio_pin_read(pin) == 1) {
if (pin == BUTTON_1) {
@@ -673,11 +673,11 @@ static void btn_fn_copy_lf(uint8_t slot, tag_specific_type_t type) {
size = LF_HIDPROX_TAG_ID_SIZE;
data = id_buffer;
break;
case TAG_TYPE_IOPROX:
status = scan_ioprox(id_buffer, 0);
size = LF_IOPROX_TAG_ID_SIZE;
data = id_buffer;
break;
case TAG_TYPE_IOPROX:
status = scan_ioprox(id_buffer, 0);
size = LF_IOPROX_TAG_ID_SIZE;
data = id_buffer;
break;
case TAG_TYPE_EM410X:
case TAG_TYPE_EM410X_ELECTRA: {
status = scan_em410x(id_buffer);
@@ -854,16 +854,16 @@ static void run_button_function_by_settings(settings_button_function_t sbf) {
nrf_gpio_pin_set(READER_POWER); // reader power enable
nrf_gpio_cfg_output(HF_ANT_SEL);
nrf_gpio_pin_clear(HF_ANT_SEL); // hf ant switch to reader mode
pcd_14a_reader_init();
bsp_delay_ms(10);
}
pcd_14a_reader_reset();
pcd_14a_reader_antenna_on();
m_is_field_on = true;
NRF_LOG_INFO("NFC field ON");
// Set initial rainbow state
set_slot_light_color(RGB_RED);
uint32_t *led_pins = hw_get_led_array();
@@ -879,7 +879,7 @@ static void run_button_function_by_settings(settings_button_function_t sbf) {
pcd_14a_reader_antenna_off();
m_is_field_on = false;
NRF_LOG_INFO("NFC field OFF");
// If we're not in reader mode, clean up the hardware
device_mode_t current_mode = get_device_mode();
if (current_mode != DEVICE_MODE_READER) {
@@ -887,7 +887,7 @@ static void run_button_function_by_settings(settings_button_function_t sbf) {
nrf_gpio_pin_clear(READER_POWER); // reader power disable
nrf_gpio_pin_set(HF_ANT_SEL); // hf ant switch back to tag mode
}
// Restore normal LED
light_up_by_slot();
@@ -1035,17 +1035,17 @@ int main(void) {
lesc_event_process();
// Button event process
button_press_process();
#if defined(PROJECT_CHAMELEON_ULTRA)
// Field generator rainbow animation
field_generator_rainbow_loop();
#endif
// Led blink at usb status (only if field generator is off)
if (!m_is_field_on) {
blink_usb_led_status();
}
// Data pack process
data_frame_process();
// Log print process
+5 -5
View File
@@ -736,11 +736,11 @@ static void battery_level_meas_timeout_handler(void *p_context) {
// if battery service is notification enable, we can send msg to device.
err_code = ble_bas_battery_level_update(&m_bas, percentage_batt_lvl, BLE_CONN_HANDLE_ALL);
if ((err_code != NRF_SUCCESS) &&
(err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_BUSY) &&
(err_code != NRF_ERROR_FORBIDDEN) &&
(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)) {
(err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_BUSY) &&
(err_code != NRF_ERROR_FORBIDDEN) &&
(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)) {
APP_ERROR_HANDLER(err_code);
}
+1 -1
View File
@@ -34,4 +34,4 @@ const uint8_t byte_mirror[256] = {
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};
};
@@ -611,11 +611,11 @@ static inline void nrf_nfct_reset(void) {
nrf_nfct_int_enable(int_enabled);
// Disable interrupts associated with data exchange.
nrf_nfct_int_disable(NRF_NFCT_INT_RXFRAMESTART_MASK |
NRF_NFCT_INT_RXFRAMEEND_MASK |
NRF_NFCT_INT_RXERROR_MASK |
NRF_NFCT_INT_TXFRAMESTART_MASK |
NRF_NFCT_INT_TXFRAMEEND_MASK);
nrf_nfct_int_disable(NRF_NFCT_INT_RXFRAMESTART_MASK |
NRF_NFCT_INT_RXFRAMEEND_MASK |
NRF_NFCT_INT_RXERROR_MASK |
NRF_NFCT_INT_TXFRAMESTART_MASK |
NRF_NFCT_INT_TXFRAMEEND_MASK);
}
static inline void nfc_fdt_reset(void) {
@@ -668,7 +668,7 @@ void nfc_tag_14a_event_callback(nrfx_nfct_evt_t const *p_event) {
if (reset_if_field_lost) {
// Fix a bug where certain special conditions prevent triggering TX start events and actually transmit incorrect data to the card reader.
// After more more more testing, I found that simply going into sleep mode and restarting can restore work.
// After more more more testing, I found that simply going into sleep mode and restarting can restore work.
// Therefore, I suspect that there may be some issues with the NFC peripheral that require a reset to resolve.
nrf_nfct_reset();
}
@@ -681,12 +681,12 @@ void nfc_tag_14a_event_callback(nrfx_nfct_evt_t const *p_event) {
if (m_tx_sniff_cb != NULL) {
uint32_t amt = NRF_NFCT->TXD.AMOUNT;
uint16_t tx_bytes = (amt >> NFCT_TXD_AMOUNT_TXDATABYTES_Pos)
& (NFCT_TXD_AMOUNT_TXDATABYTES_Msk >> NFCT_TXD_AMOUNT_TXDATABYTES_Pos);
& (NFCT_TXD_AMOUNT_TXDATABYTES_Msk >> NFCT_TXD_AMOUNT_TXDATABYTES_Pos);
uint16_t tx_bits_rem = (amt >> NFCT_TXD_AMOUNT_TXDATABITS_Pos)
& (NFCT_TXD_AMOUNT_TXDATABITS_Msk >> NFCT_TXD_AMOUNT_TXDATABITS_Pos);
& (NFCT_TXD_AMOUNT_TXDATABITS_Msk >> NFCT_TXD_AMOUNT_TXDATABITS_Pos);
uint16_t tx_bits = (tx_bits_rem > 0)
? ((tx_bytes - 1) * 8 + tx_bits_rem)
: (tx_bytes * 8);
? ((tx_bytes - 1) * 8 + tx_bits_rem)
: (tx_bytes * 8);
if (tx_bits > 0 && tx_bytes <= MAX_NFC_TX_BUFFER_SIZE) {
m_tx_sniff_cb(m_nfc_tx_buffer, tx_bits);
}
@@ -94,7 +94,7 @@ static uint8_t m_large_resp_count = 0;
/* ------------------------------------------------------------------ */
void nfc_tag_14a_4_add_static_response(const uint8_t *cmd, uint8_t cmd_len,
const uint8_t *resp, uint16_t resp_len) {
const uint8_t *resp, uint16_t resp_len) {
if (cmd_len > NFC_14A_4_MAX_STATIC_CMD_LEN) cmd_len = NFC_14A_4_MAX_STATIC_CMD_LEN;
if (resp_len > NFC_14A_4_MAX_STATIC_RESP_LEN) {
@@ -132,7 +132,7 @@ void nfc_tag_14a_4_clear_static_responses(void) {
}
static bool find_static_response(const uint8_t *apdu, uint16_t apdu_len,
uint8_t **resp_out, uint16_t *resp_len_out) {
uint8_t **resp_out, uint16_t *resp_len_out) {
/* Flash-backed table */
for (uint8_t i = 0; i < m_static_resp_count; i++) {
nfc_tag_14a_4_static_response_t *e = &m_static_resp[i];
@@ -259,7 +259,7 @@ static void nfc_tag_14a_4_state_handler(uint8_t *data, uint16_t szBytes) {
m_dbg_iblocks_rx++;
m_dbg_last_rx_pcb = pcb;
NRF_LOG_INFO("14A4 I-block #%d: reader_blk=%d m_block_num=%d apdu_len=%d",
m_dbg_iblocks_rx, reader_blknum, m_block_num, apdu_len);
m_dbg_iblocks_rx, reader_blknum, m_block_num, apdu_len);
/* Block number check per ISO14443-4 §7.5.3.3:
* If block number matches expected, process new APDU.
@@ -292,10 +292,10 @@ static void nfc_tag_14a_4_state_handler(uint8_t *data, uint16_t szBytes) {
uint8_t *static_resp = NULL;
uint16_t static_len = 0;
bool _found = find_static_response(m_apdu_buf, apdu_len,
&static_resp, &static_len);
&static_resp, &static_len);
m_dbg_last_match = _found ? 1 : 0;
NRF_LOG_INFO("14A4 find_static: found=%d static_len=%d resp_count=%d",
_found, static_len, m_static_resp_count);
_found, static_len, m_static_resp_count);
if (_found) {
m_dbg_iblocks_tx++;
memcpy(m_resp_buf, static_resp, static_len);
@@ -350,7 +350,7 @@ void nfc_tag_14a_4_reset_handler(void) {
}
void nfc_tag_14a_4_get_debug_counters(uint8_t *rx, uint8_t *tx,
uint8_t *last_pcb, uint8_t *last_match) {
uint8_t *last_pcb, uint8_t *last_match) {
*rx = m_dbg_iblocks_rx;
*tx = m_dbg_iblocks_tx;
*last_pcb = m_dbg_last_rx_pcb;
@@ -379,7 +379,7 @@ int nfc_tag_14a_4_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffe
int info_size = sizeof(nfc_tag_14a_4_information_t);
if (buffer->length < info_size) {
NRF_LOG_ERROR("14A-4 loadcb: buffer too small (%d < %d)",
buffer->length, info_size);
buffer->length, info_size);
return info_size;
}
m_tag_information = (nfc_tag_14a_4_information_t *)buffer->buffer;
@@ -398,9 +398,9 @@ int nfc_tag_14a_4_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffe
};
nfc_tag_14a_set_handler(&handler);
NRF_LOG_INFO("14A-4 loadcb OK: SAK=%02x uid_sz=%d static_resp=%d",
m_tag_information->res_coll.sak[0],
m_tag_information->res_coll.size,
m_static_resp_count);
m_tag_information->res_coll.sak[0],
m_tag_information->res_coll.size,
m_static_resp_count);
return info_size;
}
@@ -34,7 +34,8 @@ typedef struct __attribute__((packed)) {
uint8_t cmd[NFC_14A_4_MAX_STATIC_CMD_LEN];
uint8_t resp_len;
uint8_t resp[NFC_14A_4_MAX_STATIC_RESP_LEN];
} nfc_tag_14a_4_static_response_t;
}
nfc_tag_14a_4_static_response_t;
/**
* Per-slot persistent data layout stored in FDS flash.
@@ -44,7 +45,8 @@ typedef struct __attribute__((packed)) {
nfc_tag_14a_coll_res_entity_t res_coll;
uint8_t static_resp_count;
nfc_tag_14a_4_static_response_t static_resp[NFC_14A_4_MAX_STATIC_RESPONSES];
} nfc_tag_14a_4_information_t;
}
nfc_tag_14a_4_information_t;
/* Anti-collision resource — used by get_coll_res_data in app_cmd.c */
nfc_tag_14a_coll_res_reference_t *nfc_tag_14a_4_get_coll_res(void);
@@ -56,7 +58,7 @@ bool nfc_tag_14a_4_data_factory(uint8_t slot, tag_specific_type_t tag_type);
/* Static response table management (called before hw mode -e) */
void nfc_tag_14a_4_add_static_response(const uint8_t *cmd, uint8_t cmd_len,
const uint8_t *resp, uint16_t resp_len);
const uint8_t *resp, uint16_t resp_len);
void nfc_tag_14a_4_clear_static_responses(void);
/* APDU relay — host-driven responses */
@@ -696,7 +696,7 @@ static bool check_ro_lock_on_page(int block_num) {
//the BL bit only freezes the lock bytes !
return (m_tag_information->memory[2][2] & 8) != 0;
default:
return (m_tag_information->memory[2][2] & 9) != 0;
return (m_tag_information->memory[2][2] & 9) != 0;
}
// bits 0 and 3
} else if (block_num <= MF0ICU1_PAGES) {
@@ -805,26 +805,25 @@ static bool check_ro_lock_on_page(int block_num) {
// the lock configuration. We only check the actual lock bits (L0-L15) in bytes 0-1.
return locked_small_range;
default:
return locked_small_range | locked_large_range;
return locked_small_range | locked_large_range;
}
} else {
//Check the block locking bits to see if we can touch the dynamic locks bytes for NTAG tags
if(block_num == user_memory_end)
{
if (block_num == user_memory_end) {
switch (m_tag_type) {
case TAG_TYPE_NTAG_213:
case TAG_TYPE_NTAG_215:
case TAG_TYPE_NTAG_216: {
uint8_t block_bytes = m_tag_information->memory[user_memory_end][2];
uint16_t block_world = 0;
// Each bit in block_bytes maps to 2 bits in block_world
for (int i = 0; i < 8; i++) {
if (block_bytes & (0x01 << i)) {
block_world |= (0x0003 << (i * 2));
}
}
p_lock_bytes = m_tag_information->memory[user_memory_end];
uint16_t lock_word = (((uint16_t)p_lock_bytes[1]) << 8) | (uint16_t)p_lock_bytes[0];
return (lock_word & block_world) != 0;
@@ -864,7 +863,7 @@ static int handle_write_command(uint8_t block_num, uint8_t *p_data) {
default:
out_of_bounds = block_num >= block_max;
break;
}
}
// Reject out-of-bounds writes (except config pages)
if (out_of_bounds) {
NRF_LOG_ERROR("Write failed: block_num %08x >= block_max %08x", block_num, block_max);
@@ -1007,10 +1006,10 @@ static void handle_pwd_auth_command(uint8_t *p_data) {
if (m_tag_information->config.detection_enable && m_auth_log.count < MF0_NTAG_AUTH_LOG_MAX) {
memcpy(m_auth_log.logs[m_auth_log.count].pwd, &p_data[1], 4);
m_auth_log.count++;
NRF_LOG_INFO("NTAG password: %02x%02x%02x%02x",
NRF_LOG_INFO("NTAG password: %02x%02x%02x%02x",
p_data[1], p_data[2], p_data[3], p_data[4]);
}
if (pwd != supplied_pwd) {
if (auth_lim) {
cnt_data[MF0_NTAG_AUTHLIM_OFF_IN_CTR] &= ~MF0_NTAG_AUTHLIM_MASK_IN_CTR;
@@ -339,7 +339,10 @@ void nfc_tag_mf1_random_nonce(uint8_t nonce[4], bool isNested) {
uint8_t prng_type = m_tag_information->config.prng_type;
if (prng_type == 0) {
// STATIC — always return same nonce (clone-card behaviour)
nonce[0] = 0x01; nonce[1] = 0x02; nonce[2] = 0x03; nonce[3] = 0x04;
nonce[0] = 0x01;
nonce[1] = 0x02;
nonce[2] = 0x03;
nonce[3] = 0x04;
} else if (prng_type == 1) {
// WEAK — real MFC LFSR, advance 32 clocks per call
m_prng_state = prng_successor(m_prng_state, 32);
@@ -73,7 +73,7 @@ typedef struct {
uint8_t mode_gen2_magic: 1;
/**
* Should the NFC peripheral be reset after losing the RF field?
* This configuration can fix the issue where some card readers cause the CU to enter a strange state of no response/incorrect response.
* This configuration can fix the issue where some card readers cause the CU to enter a strange state of no response/incorrect response.
* Once in this state, the device must be restarted to resolve the issue.
* Alternatively, enabling this configuration for resetting the NFC after leaving the rf field can also solve the aforementioned problem.
*/
@@ -336,7 +336,8 @@ bool lf_tag_data_factory(uint8_t slot, tag_specific_type_t tag_type, uint8_t *ta
bool lf_tag_em410x_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
static const uint8_t tag_id_base[LF_EM410X_TAG_ID_SIZE] = {0xDE, 0xAD, 0xBE, 0xEF, 0x88};
static const uint8_t tag_id_electra[LF_EM410X_ELECTRA_TAG_ID_SIZE] = {0xDE, 0xAD, 0xBE, 0xEF, 0x88,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
switch (tag_type) {
case TAG_TYPE_EM410X_ELECTRA:
@@ -365,8 +366,8 @@ bool lf_tag_hidprox_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
* @return Whether the format is successful, if the formatting is successful, it will return to True, otherwise False will be returned
*/
bool lf_tag_ioprox_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
uint8_t tag_id[16] = {
0x01,0xAA,0x30,0x39,0x00,0x78,0x6A,0xA0,0x33,0x09,0xCF,0xEF,0x00,0x00,0x00,0x00
uint8_t tag_id[16] = {
0x01, 0xAA, 0x30, 0x39, 0x00, 0x78, 0x6A, 0xA0, 0x33, 0x09, 0xCF, 0xEF, 0x00, 0x00, 0x00, 0x00
};
return lf_tag_data_factory(slot, tag_type, tag_id, sizeof(tag_id));
}
@@ -358,10 +358,10 @@ const protocol em410x_electra = {
.get_data = (codec_get_data)em410x_get_data,
.modulator = (modulator)em410x_electra_modulator,
.decoder =
{
.start = (decoder_start)em410x_electra_decoder_start,
.feed = (decoder_feed)em410x_electra_decoder_feed,
},
{
.start = (decoder_start)em410x_electra_decoder_start,
.feed = (decoder_feed)em410x_electra_decoder_feed,
},
};
// EM-Micro, EM410x/64 (std)
@@ -373,10 +373,10 @@ const protocol em410x_64 = {
.get_data = (codec_get_data)em410x_get_data,
.modulator = (modulator)em410x_modulator,
.decoder =
{
.start = (decoder_start)em410x_decoder_start,
.feed = (decoder_feed)em410x_decoder_feed,
},
{
.start = (decoder_start)em410x_decoder_start,
.feed = (decoder_feed)em410x_decoder_feed,
},
};
// EM-Micro, EM410x/32
@@ -388,10 +388,10 @@ const protocol em410x_32 = {
.get_data = (codec_get_data)em410x_get_data,
.modulator = (modulator)em410x_modulator,
.decoder =
{
.start = (decoder_start)em410x_decoder_start,
.feed = (decoder_feed)em410x_decoder_feed,
},
{
.start = (decoder_start)em410x_decoder_start,
.feed = (decoder_feed)em410x_decoder_feed,
},
};
// EM-Micro, EM410x/16
@@ -403,10 +403,10 @@ const protocol em410x_16 = {
.get_data = (codec_get_data)em410x_get_data,
.modulator = (modulator)em410x_modulator,
.decoder =
{
.start = (decoder_start)em410x_decoder_start,
.feed = (decoder_feed)em410x_decoder_feed,
},
{
.start = (decoder_start)em410x_decoder_start,
.feed = (decoder_feed)em410x_decoder_feed,
},
};
// Encode EM410X card number to T55xx blocks.
@@ -7,7 +7,7 @@ extern const protocol em410x_32;
extern const protocol em410x_16;
extern const protocol em410x_electra;
extern const protocol* em410x_protocols[];
extern const protocol *em410x_protocols[];
extern size_t em410x_protocols_size;
uint8_t em410x_t55xx_writer(uint8_t* uid, uint32_t* blks);
@@ -228,14 +228,14 @@ const protocol hidprox = {
.get_data = (codec_get_data)hidprox_get_data,
.modulator = (modulator)hidprox_modulator,
.decoder =
{
.start = (decoder_start)hidprox_decoder_start,
.feed = (decoder_feed)hidprox_decoder_feed,
},
{
.start = (decoder_start)hidprox_decoder_start,
.feed = (decoder_feed)hidprox_decoder_feed,
},
};
uint8_t hidprox_t55xx_writer(wiegand_card_t *card, uint32_t *blks) {
blks[0] = T5577_HIDPROX_CONFIG;
hidprox_raw_data(card, &blks[1], &blks[2], &blks[3]);
return HIDPROX_T55XX_BLOCK_COUNT;
}
}
@@ -30,4 +30,4 @@ typedef struct {
extern const protocol hidprox;
uint8_t hidprox_t55xx_writer(wiegand_card_t *card, uint32_t *blks);
uint8_t hidprox_t55xx_writer(wiegand_card_t *card, uint32_t *blks);
@@ -31,8 +31,7 @@ void ioprox_reset_bits(ioprox_codec_t *d) {
d->bit_len = 0;
}
static inline void push_bit(ioprox_codec_t *d, uint8_t bit)
{
static inline void push_bit(ioprox_codec_t *d, uint8_t bit) {
if (d->bit_len < IOPROX_MAX_BITS) {
d->bits[d->bit_len++] = bit;
return;
@@ -42,15 +41,13 @@ static inline void push_bit(ioprox_codec_t *d, uint8_t bit)
d->bits[IOPROX_MAX_BITS - 1] = bit;
}
static inline uint8_t get_bit_inv(const uint8_t *bits, uint16_t pos, bool inv)
{
static inline uint8_t get_bit_inv(const uint8_t *bits, uint16_t pos, bool inv) {
uint8_t b = bits[pos] & 1u;
return inv ? (uint8_t)(b ^ 1u) : b;
}
// Converts a bit array to a 32-bit big-endian integer.
static inline uint32_t bytebits_to_byte(const uint8_t *bits, uint16_t len)
{
static inline uint32_t bytebits_to_byte(const uint8_t *bits, uint16_t len) {
uint32_t val = 0;
for (uint16_t i = 0; i < len; i++) {
val = (val << 1) | (bits[i] & 1u);
@@ -59,8 +56,7 @@ static inline uint32_t bytebits_to_byte(const uint8_t *bits, uint16_t len)
}
// Reads 8 bits MSB-first from bits[start_pos], optionally inverting each bit.
static inline uint8_t bytebits_to_u8_msb_inv(const uint8_t *bits, uint16_t start_pos, bool inv)
{
static inline uint8_t bytebits_to_u8_msb_inv(const uint8_t *bits, uint16_t start_pos, bool inv) {
uint8_t v = 0;
for (int i = 0; i < 8; i++) {
v = (uint8_t)((v << 1) | get_bit_inv(bits, (uint16_t)(start_pos + i), inv));
@@ -81,16 +77,14 @@ bool ioprox_raw8_to_bits(const uint8_t *raw8, ioprox_codec_t *d) {
}
// ioProx checksum: 0xFF - (sum(b1..b5) & 0xFF)
static inline uint8_t ioprox_checksum5(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
{
static inline uint8_t ioprox_checksum5(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5) {
uint16_t sum = (uint16_t)b1 + b2 + b3 + b4 + b5;
return (uint8_t)(0xFFu - (uint8_t)(sum & 0xFFu));
}
// Returns true if 10 starting bits form a valid ioProx preamble.
// Preamble is 9 zeros followed by 1 one (inverted when inv=true).
static bool preamble_match(const uint8_t *d, uint16_t off, bool inv)
{
static bool preamble_match(const uint8_t *d, uint16_t off, bool inv) {
for (int k = 0; k < 9; k++) {
if ((d[off + k] & 1u) != (inv ? 1u : 0u)) return false;
}
@@ -115,8 +109,7 @@ static bool preamble_match(const uint8_t *d, uint16_t off, bool inv)
// [2-3] card number (big-endian)
// [4-11] raw8 frame bytes (for debugging and storage)
// [12-15] reserved (0x00)
static bool decode_and_pack(ioprox_codec_t *d, uint16_t idx, bool inv)
{
static bool decode_and_pack(ioprox_codec_t *d, uint16_t idx, bool inv) {
uint8_t b1, b2, b3, b4, b5, b6;
uint16_t number;
uint32_t raw_block1;
@@ -172,8 +165,7 @@ bool ioprox_decode_raw_to_data(const uint8_t *raw8, uint8_t *output) {
}
// Writes 8 bits of v MSB-first into bits[] starting at position pos.
static void write_bits_msb(uint8_t *bits, uint16_t pos, uint8_t v)
{
static void write_bits_msb(uint8_t *bits, uint16_t pos, uint8_t v) {
for (uint8_t i = 0; i < 8; i++) {
bits[pos + i] = (v >> (7 - i)) & 1;
}
@@ -182,8 +174,7 @@ static void write_bits_msb(uint8_t *bits, uint16_t pos, uint8_t v)
// Encodes ioProx card parameters into the 16-byte output buffer.
// The encoded frame uses 8+1 bit framing (8 data bits + 1 separator per group).
// Returns false if output pointer is NULL.
bool ioprox_encode_params_to_data(uint8_t version, uint8_t facility, uint16_t number, uint8_t *output)
{
bool ioprox_encode_params_to_data(uint8_t version, uint8_t facility, uint16_t number, uint8_t *output) {
if (!output) return false;
uint8_t b0 = 0x00;
@@ -197,12 +188,18 @@ bool ioprox_encode_params_to_data(uint8_t version, uint8_t facility, uint16_t nu
uint8_t bits[64] = {0};
// Pack 7 data bytes using 8+1 framing (data bits + separator)
write_bits_msb(bits, 0, b0); bits[ 8] = 0;
write_bits_msb(bits, 9, b1); bits[17] = 1;
write_bits_msb(bits, 18, b2); bits[26] = 1;
write_bits_msb(bits, 27, b3); bits[35] = 1;
write_bits_msb(bits, 36, b4); bits[44] = 1;
write_bits_msb(bits, 45, b5); bits[53] = 1;
write_bits_msb(bits, 0, b0);
bits[ 8] = 0;
write_bits_msb(bits, 9, b1);
bits[17] = 1;
write_bits_msb(bits, 18, b2);
bits[26] = 1;
write_bits_msb(bits, 27, b3);
bits[35] = 1;
write_bits_msb(bits, 36, b4);
bits[44] = 1;
write_bits_msb(bits, 45, b5);
bits[53] = 1;
write_bits_msb(bits, 54, b6);
bits[62] = 1;
bits[63] = 1;
@@ -227,8 +224,7 @@ bool ioprox_encode_params_to_data(uint8_t version, uint8_t facility, uint16_t nu
// Scans the tail of the bit buffer for a valid ioProx frame.
// To keep CPU load low, only the last ~5 frames (320 bits) are scanned.
// Returns true if a valid frame (checksum OK) was decoded into d->data.
static bool scan_tail(ioprox_codec_t *d)
{
static bool scan_tail(ioprox_codec_t *d) {
// Need at least 128 bits to verify two consecutive 64-bit frames
if (d->bit_len < 128) return false;
@@ -277,8 +273,7 @@ static bool scan_tail(ioprox_codec_t *d)
// --- Protocol callbacks ---
static void *ioprox_codec_alloc(void)
{
static void *ioprox_codec_alloc(void) {
ioprox_codec_t *d = (ioprox_codec_t *)malloc(sizeof(ioprox_codec_t));
if (!d) return NULL;
memset(d, 0, sizeof(*d));
@@ -286,8 +281,7 @@ static void *ioprox_codec_alloc(void)
return d;
}
static void ioprox_codec_free(void *codec)
{
static void ioprox_codec_free(void *codec) {
ioprox_codec_t *d = (ioprox_codec_t *)codec;
if (!d) return;
if (d->modem) {
@@ -297,14 +291,12 @@ static void ioprox_codec_free(void *codec)
free(d);
}
static uint8_t *ioprox_get_data(void *codec)
{
static uint8_t *ioprox_get_data(void *codec) {
ioprox_codec_t *d = (ioprox_codec_t *)codec;
return d->data;
}
static void ioprox_decoder_start(void *codec, uint8_t format_hint)
{
static void ioprox_decoder_start(void *codec, uint8_t format_hint) {
(void)format_hint;
ioprox_codec_t *d = (ioprox_codec_t *)codec;
d->bit_len = 0;
@@ -312,8 +304,7 @@ static void ioprox_decoder_start(void *codec, uint8_t format_hint)
memset(d->data, 0, sizeof(d->data));
}
static bool ioprox_decoder_feed(void *codec, uint16_t val)
{
static bool ioprox_decoder_feed(void *codec, uint16_t val) {
ioprox_codec_t *d = (ioprox_codec_t *)codec;
if (!d || !d->modem) return false;
@@ -340,8 +331,7 @@ static bool ioprox_decoder_feed(void *codec, uint16_t val)
return false;
}
static inline void ioprox_emit_bit(int *k, bool bit)
{
static inline void ioprox_emit_bit(int *k, bool bit) {
if (!bit) {
for (int j = 0; j < LF_FSK2a_PWM_HI_FREQ_LOOP; j++) {
m_ioprox_pwm_seq_vals[*k].channel_0 = LF_FSK2a_PWM_HI_FREQ_TOP_VALUE / 2;
@@ -358,8 +348,7 @@ static inline void ioprox_emit_bit(int *k, bool bit)
}
// FSK2a modulator: converts the 8 raw card bytes into a PWM sequence for LF transmission.
const nrf_pwm_sequence_t *ioprox_modulator(ioprox_codec_t *d, uint8_t *buf)
{
const nrf_pwm_sequence_t *ioprox_modulator(ioprox_codec_t *d, uint8_t *buf) {
(void)d;
// Raw card data starts at buf[4] (bytes 0-3 are decoded fields)
@@ -27,4 +27,4 @@ extern const protocol ioprox;
uint8_t ioprox_t55xx_writer(uint8_t *buf, uint32_t *blks);
bool ioprox_decode_raw_to_data(const uint8_t *raw8, uint8_t *output);
bool ioprox_encode_params_to_data(uint8_t ver, uint8_t fc, uint16_t cn, uint8_t *out);
bool ioprox_encode_params_to_data(uint8_t ver, uint8_t fc, uint16_t cn, uint8_t *out);
@@ -97,7 +97,7 @@ static bool get_bit(pac_codec *d, uint16_t pos) {
// Decode a 10-bit UART frame at bit position 'start'.
// Frame: start(0) + 7 data bits LSB-first + odd parity + stop(1).
static int decode_uart_byte(pac_codec *d, uint16_t start, bool inverted) {
#define RD(pos) (inverted ? !get_bit(d, (pos)) : get_bit(d, (pos)))
#define RD(pos) (inverted ? !get_bit(d, (pos)) : get_bit(d, (pos)))
if (RD(start)) {
return -1;
@@ -123,7 +123,7 @@ static int decode_uart_byte(pac_codec *d, uint16_t start, bool inverted) {
return -1;
}
#undef RD
#undef RD
return byte_val;
}
@@ -393,8 +393,8 @@ const protocol pac = {
.get_data = (codec_get_data)pac_get_data,
.modulator = (modulator)pac_modulator,
.decoder =
{
.start = (decoder_start)pac_decoder_start,
.feed = (decoder_feed)pac_decoder_feed,
},
{
.start = (decoder_start)pac_decoder_start,
.feed = (decoder_feed)pac_decoder_feed,
},
};
@@ -7,14 +7,14 @@
#include "nrf_pwm.h"
#include "tag_base_type.h"
typedef void* (*codec_alloc)(void);
typedef void *(*codec_alloc)(void);
typedef void (*codec_free)(void* codec);
typedef uint8_t* (*codec_get_data)(void* codec);
typedef uint8_t *(*codec_get_data)(void* codec);
typedef void (*decoder_start)(void* codec, uint8_t format);
typedef bool (*decoder_feed)(void* codec, uint16_t val);
typedef nrf_pwm_sequence_t* (*modulator)(void* d, uint8_t* buf);
typedef nrf_pwm_sequence_t *(*modulator)(void* d, uint8_t* buf);
typedef struct {
decoder_start start;
@@ -20,7 +20,7 @@
#define VIKING_T55XX_BLOCK_COUNT (3) // config + 2 data blocks
// Duration between falling edges is...
// Duration between falling edges is...
#define VIKING_READ_TIME1_BASE (0x20) // on 16, off 16 cycles
#define VIKING_READ_TIME2_BASE (0x30) // on 16, off 32 cycles (or on 32 cycles, off 16 cycles)
#define VIKING_READ_TIME3_BASE (0x40) // on 32, off 32 cycles
@@ -94,7 +94,7 @@ static void viking_free(viking_codec *d) {
free(d);
};
static uint8_t *viking_get_data(viking_codec *d) {
static uint8_t *viking_get_data(viking_codec *d) {
return d->data;
};
@@ -111,7 +111,7 @@ static bool viking_decode_feed(viking_codec *d, bool bit) {
if (bit) {
d->raw |= 0x01;
}
if (d->raw_length < (VIKING_RAW_SIZE-2)) {
if (d->raw_length < (VIKING_RAW_SIZE - 2)) {
return false;
}
@@ -132,7 +132,7 @@ static bool viking_decode_feed(viking_codec *d, bool bit) {
// Validate CRC
uint8_t crc = 0x5A;
for (int i = 0; i < VIKING_DATA_SIZE; i++) {
uint8_t data = (d->raw >> ((i+1)*8)) & 0xff;
uint8_t data = (d->raw >> ((i + 1) * 8)) & 0xff;
crc ^= data;
d->data[VIKING_DATA_SIZE - i - 1] |= data;
}
@@ -144,7 +144,7 @@ static bool viking_decoder_feed(viking_codec *d, uint16_t interval) {
bool bits[2] = {0};
int8_t bitlen = 0;
// Hack: due to hardware sometimes not detecting a time2 pulse. Rather than
// Hack: due to hardware sometimes not detecting a time2 pulse. Rather than
// reset when interval is really long, assume there was a time2 pulse.
if (interval > VIKING_READ_TIME3_BASE + VIKING_READ_JITTER_TIME_BASE) {
interval -= VIKING_READ_TIME2_BASE;
@@ -203,10 +203,10 @@ const protocol viking = {
.get_data = (codec_get_data)viking_get_data,
.modulator = (modulator)viking_modulator,
.decoder =
{
.start = (decoder_start)viking_decoder_start,
.feed = (decoder_feed)viking_decoder_feed,
},
{
.start = (decoder_start)viking_decoder_start,
.feed = (decoder_feed)viking_decoder_feed,
},
};
// Encode viking card number to T55xx blocks.
@@ -216,4 +216,4 @@ uint8_t viking_t55xx_writer(uint8_t *uid, uint32_t *blks) {
blks[1] = raw >> 32;
blks[2] = raw & 0xffffffff;
return VIKING_T55XX_BLOCK_COUNT;
}
}

Some files were not shown because too many files have changed in this diff Show More