C files: same style as for pm3 repo

This commit is contained in:
Philippe Teuwen
2023-08-23 00:22:22 +02:00
parent 37c99cece0
commit e3ff2d0d95
60 changed files with 1107 additions and 1216 deletions
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -5,7 +5,7 @@
#include "dataframe.h"
typedef data_frame_tx_t* (*cmd_processor)(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data);
typedef data_frame_tx_t *(*cmd_processor)(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data);
typedef struct {
uint16_t cmd;
+29 -31
View File
@@ -201,11 +201,11 @@ static void system_off_enter(void) {
// Configure RAM hibernation hold
uint32_t ram8_retention = // RAM8 Each section has 32KB capacity
// POWER_RAM_POWER_S0RETENTION_On << POWER_RAM_POWER_S0RETENTION_Pos ;
// POWER_RAM_POWER_S1RETENTION_On << POWER_RAM_POWER_S1RETENTION_Pos |
// POWER_RAM_POWER_S2RETENTION_On << POWER_RAM_POWER_S2RETENTION_Pos |
// POWER_RAM_POWER_S3RETENTION_On << POWER_RAM_POWER_S3RETENTION_Pos |
// POWER_RAM_POWER_S4RETENTION_On << POWER_RAM_POWER_S4RETENTION_Pos |
// POWER_RAM_POWER_S0RETENTION_On << POWER_RAM_POWER_S0RETENTION_Pos ;
// POWER_RAM_POWER_S1RETENTION_On << POWER_RAM_POWER_S1RETENTION_Pos |
// POWER_RAM_POWER_S2RETENTION_On << POWER_RAM_POWER_S2RETENTION_Pos |
// POWER_RAM_POWER_S3RETENTION_On << POWER_RAM_POWER_S3RETENTION_Pos |
// POWER_RAM_POWER_S4RETENTION_On << POWER_RAM_POWER_S4RETENTION_Pos |
POWER_RAM_POWER_S5RETENTION_On << POWER_RAM_POWER_S5RETENTION_Pos;
ret = sd_power_ram_power_set(8, ram8_retention);
APP_ERROR_CHECK(ret);
@@ -222,7 +222,7 @@ static void system_off_enter(void) {
}
} else {
// close all led.
uint32_t* p_led_array = hw_get_led_array();
uint32_t *p_led_array = hw_get_led_array();
for (uint8_t i = 0; i < RGB_LIST_NUM; i++) {
nrf_gpio_pin_clear(p_led_array[i]);
}
@@ -296,7 +296,7 @@ static void system_off_enter(void) {
app_timer_stop_all();
// 检查是否存在低频场,解决休眠时有非常强的场信号一直使比较器处于高电平输入状态从而无法产生上升沿而无法唤醒系统的问题。
if(lf_is_field_exists()) {
if (lf_is_field_exists()) {
// 关闭比较器
nrf_drv_lpcomp_disable();
// 设置reset原因,重启后需要拿到此原因,避免误判唤醒源
@@ -357,8 +357,7 @@ static void check_wakeup_src(void) {
// Button wake-up boot animation
uint8_t animation_config = settings_get_animation_config();
if (animation_config == SettingsAnimationModeFull)
{
if (animation_config == SettingsAnimationModeFull) {
ledblink2(color, !dir, 11);
ledblink2(color, dir, 11);
ledblink2(color, !dir, dir ? slot : 7 - slot);
@@ -476,12 +475,12 @@ static void offline_status_blink_color(uint8_t blink_color) {
uint8_t color = get_color_by_slot(slot);
uint32_t* p_led_array = hw_get_led_array();
uint32_t *p_led_array = hw_get_led_array();
set_slot_light_color(blink_color);
for (uint8_t i = 0; i < RGB_LIST_NUM; i++) {
if(i == slot) {
if (i == slot) {
continue;
}
nrf_gpio_pin_set(p_led_array[i]);
@@ -508,7 +507,7 @@ static void btn_fn_copy_ic_uid(void) {
tag_specific_type_t tag_type[2];
tag_emulation_get_specific_type_by_slot(slot_now, tag_type);
nfc_tag_14a_coll_res_entity_t* antres;
nfc_tag_14a_coll_res_entity_t *antres;
bool is_reader_mode_now = get_device_mode() == DEVICE_MODE_READER;
// first, we need switch to reader mode.
@@ -519,14 +518,14 @@ static void btn_fn_copy_ic_uid(void) {
NRF_LOG_INFO("Start reader mode to offline copy.")
}
switch(tag_type[1]) {
switch (tag_type[1]) {
case TAG_TYPE_EM410X:
uint8_t status;
uint8_t id_buffer[5] = { 0x00 };
status = PcdScanEM410X(id_buffer);
if(status == LF_TAG_OK) {
tag_data_buffer_t* buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X);
if (status == LF_TAG_OK) {
tag_data_buffer_t *buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X);
memcpy(buffer->buffer, id_buffer, LF_EM410X_TAG_ID_SIZE);
tag_emulation_load_by_buffer(TAG_TYPE_EM410X, false);
NRF_LOG_INFO("Offline LF uid copied")
@@ -546,8 +545,8 @@ static void btn_fn_copy_ic_uid(void) {
offline_status_error();
}
tag_data_buffer_t* buffer = get_buffer_by_tag_type(tag_type[0]);
switch(tag_type[0]) {
tag_data_buffer_t *buffer = get_buffer_by_tag_type(tag_type[0]);
switch (tag_type[0]) {
case TAG_TYPE_MIFARE_Mini:
case TAG_TYPE_MIFARE_1024:
case TAG_TYPE_MIFARE_2048:
@@ -611,24 +610,23 @@ exit:
/**@brief Execute the corresponding logic based on the functional settings of the buttons.
*/
static void run_button_function_by_settings(settings_button_function_t sbf) {
switch (sbf)
{
case SettingsButtonCycleSlot:
cycle_slot(false);
break;
case SettingsButtonCycleSlotDec:
cycle_slot(true);
break;
switch (sbf) {
case SettingsButtonCycleSlot:
cycle_slot(false);
break;
case SettingsButtonCycleSlotDec:
cycle_slot(true);
break;
#if defined(PROJECT_CHAMELEON_ULTRA)
case SettingsButtonCloneIcUid:
btn_fn_copy_ic_uid();
break;
case SettingsButtonCloneIcUid:
btn_fn_copy_ic_uid();
break;
#endif
default:
NRF_LOG_ERROR("Unsupported button function")
break;
default:
NRF_LOG_ERROR("Unsupported button function")
break;
}
}
+45 -85
View File
@@ -83,8 +83,7 @@ volatile bool g_is_low_battery_shutdown = false;
* @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
* the device. It also sets the permissions and appearance.
*/
static void gap_params_init(void)
{
static void gap_params_init(void) {
uint32_t err_code;
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
@@ -113,10 +112,8 @@ static void gap_params_init(void)
* @param[in] p_bas Battery Service structure.
* @param[in] p_evt Event received from the Battery Service.
*/
static void on_bas_evt(ble_bas_t * p_bas, ble_bas_evt_t * p_evt)
{
switch (p_evt->evt_type)
{
static void on_bas_evt(ble_bas_t *p_bas, ble_bas_evt_t *p_evt) {
switch (p_evt->evt_type) {
case BLE_BAS_EVT_NOTIFICATION_ENABLED:
break; // BLE_BAS_EVT_NOTIFICATION_ENABLED
@@ -136,13 +133,11 @@ static void on_bas_evt(ble_bas_t * p_bas, ble_bas_evt_t * p_evt)
* @param[in] p_evt Nordic UART Service event.
*/
/**@snippet [Handling the data received over BLE] */
static void nus_data_handler(ble_nus_evt_t * p_evt)
{
if (p_evt->type == BLE_NUS_EVT_RX_DATA)
{
static void nus_data_handler(ble_nus_evt_t *p_evt) {
if (p_evt->type == BLE_NUS_EVT_RX_DATA) {
NRF_LOG_DEBUG("Received data from BLE NUS.");
NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
data_frame_receive((uint8_t*)(p_evt->params.rx_data.p_data), p_evt->params.rx_data.length);
data_frame_receive((uint8_t *)(p_evt->params.rx_data.p_data), p_evt->params.rx_data.length);
}
}
/**@snippet [Handling the data received over BLE] */
@@ -167,8 +162,8 @@ void nus_data_reponse(uint8_t *p_data, uint16_t length) {
continue;
}
if ((err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_NOT_FOUND)) {
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_NOT_FOUND)) {
APP_ERROR_CHECK(err_code);
}
@@ -186,19 +181,16 @@ bool is_nus_working(void) {
*
* @param[in] nrf_error Error code containing information about what went wrong.
*/
static void nrf_qwr_error_handler(uint32_t nrf_error)
{
static void nrf_qwr_error_handler(uint32_t nrf_error) {
APP_ERROR_HANDLER(nrf_error);
}
__INLINE uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max)
{
__INLINE uint32_t map(uint32_t x, uint32_t in_min, uint32_t in_max, uint32_t out_min, uint32_t out_max) {
return (uint32_t)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
}
//电池电压到百分比计算
uint32_t BATVOL2PERCENT(uint16_t VOL)
{
uint32_t BATVOL2PERCENT(uint16_t VOL) {
//100% 4.20V 1
//90 % 4.06V 80%-100% 白
//80 % 3.98V 1
@@ -238,33 +230,22 @@ uint32_t BATVOL2PERCENT(uint16_t VOL)
#define P5VOL 3230
if(VOL > P80VOL)
{
if (VOL > P80VOL) {
//80-100
return map(VOL, P80VOL, P100VOL, 80, 100);
}
else if(VOL > P60VOL)
{
} else if (VOL > P60VOL) {
//60-80
return map(VOL, P60VOL, P80VOL, 60, 80);
}
else if(VOL > P40VOL)
{
} else if (VOL > P40VOL) {
//40-60
return map(VOL, P40VOL, P60VOL, 40, 60);
}
else if(VOL > P20VOL)
{
} else if (VOL > P20VOL) {
//20-60
return map(VOL, P20VOL, P40VOL, 20, 40);
}
else if(VOL > P5VOL)
{
} else if (VOL > P5VOL) {
//5-20
return map(VOL, P5VOL, P20VOL, 5, 20);
}
else
{
} else {
//<5
return 0;
}
@@ -272,8 +253,7 @@ uint32_t BATVOL2PERCENT(uint16_t VOL)
/**@brief Function for initializing services that will be used by the application.
*/
static void services_init(void)
{
static void services_init(void) {
uint32_t err_code;
// -------------------------------------------------------------
@@ -326,12 +306,10 @@ static void services_init(void)
*
* @param[in] p_evt Event received from the Connection Parameters Module.
*/
static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
{
static void on_conn_params_evt(ble_conn_params_evt_t *p_evt) {
uint32_t err_code;
if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
{
if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED) {
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
APP_ERROR_CHECK(err_code);
}
@@ -341,15 +319,13 @@ static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
*
* @param[in] nrf_error Error code containing information about what went wrong.
*/
static void conn_params_error_handler(uint32_t nrf_error)
{
static void conn_params_error_handler(uint32_t nrf_error) {
APP_ERROR_HANDLER(nrf_error);
}
/**@brief Function for initializing the Connection Parameters module.
*/
static void conn_params_init(void)
{
static void conn_params_init(void) {
uint32_t err_code;
ble_conn_params_init_t cp_init;
@@ -374,10 +350,8 @@ static void conn_params_init(void)
*
* @param[in] ble_adv_evt Advertising event.
*/
static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
{
switch (ble_adv_evt)
{
static void on_adv_evt(ble_adv_evt_t ble_adv_evt) {
switch (ble_adv_evt) {
case BLE_ADV_EVT_FAST:
NRF_LOG_INFO("BLE_ADV_EVT_FAST");
break;
@@ -394,12 +368,10 @@ static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
* @param[in] p_ble_evt Bluetooth stack event.
* @param[in] p_context Unused.
*/
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
static void ble_evt_handler(ble_evt_t const *p_ble_evt, void *p_context) {
ret_code_t err_code;
switch (p_ble_evt->header.evt_id)
{
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED:
sleep_timer_stop();
@@ -419,17 +391,16 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
sleep_timer_start(SLEEP_DELAY_MS_BLE_DISCONNECTED);
break;
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
{
case BLE_GAP_EVT_PHY_UPDATE_REQUEST: {
NRF_LOG_DEBUG("PHY update request.");
ble_gap_phys_t const phys =
{
ble_gap_phys_t const phys = {
.rx_phys = BLE_GAP_PHY_AUTO,
.tx_phys = BLE_GAP_PHY_AUTO,
};
err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
APP_ERROR_CHECK(err_code);
} break;
}
break;
case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
// Pairing not supported
@@ -469,8 +440,7 @@ static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
*
* @details This function initializes the SoftDevice and the BLE event interrupt.
*/
static void ble_stack_init(void)
{
static void ble_stack_init(void) {
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
@@ -492,10 +462,8 @@ static void ble_stack_init(void)
/**@brief Function for handling events from the GATT library. */
void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
{
if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
{
void gatt_evt_handler(nrf_ble_gatt_t *p_gatt, nrf_ble_gatt_evt_t const *p_evt) {
if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED)) {
m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
}
@@ -506,8 +474,7 @@ void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
/**@brief Function for initializing the GATT library. */
void gatt_init(void)
{
void gatt_init(void) {
ret_code_t err_code;
err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
@@ -520,8 +487,7 @@ void gatt_init(void)
/**@brief Function for initializing the Advertising functionality.
*/
static void advertising_init(void)
{
static void advertising_init(void) {
uint32_t err_code;
ble_advertising_init_t init;
@@ -548,8 +514,7 @@ static void advertising_init(void)
/**
* @brief Function for starting advertising.
*/
void advertising_start(void)
{
void advertising_start(void) {
uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
}
@@ -561,10 +526,8 @@ void advertising_start(void)
* @details This function will fetch the conversion result from the ADC, convert the value into
* percentage and send it to peer.
*/
void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
void saadc_event_handler(nrf_drv_saadc_evt_t const *p_event) {
if (p_event->type == NRF_DRV_SAADC_EVT_DONE) {
nrf_saadc_value_t adc_result;
uint32_t err_code;
@@ -581,12 +544,11 @@ void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
// 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 != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
)
{
(err_code != NRF_ERROR_INVALID_STATE) &&
(err_code != NRF_ERROR_RESOURCES) &&
(err_code != NRF_ERROR_BUSY) &&
(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
) {
APP_ERROR_HANDLER(err_code);
}
@@ -603,8 +565,7 @@ void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
/**@brief Function for configuring ADC to do battery level conversion.
*/
static void adc_configure(void)
{
static void adc_configure(void) {
ret_code_t err_code = nrf_drv_saadc_init(NULL, saadc_event_handler);
APP_ERROR_CHECK(err_code);
@@ -627,8 +588,7 @@ static void adc_configure(void)
* @param[in] p_context Pointer used for passing some arbitrary information (context) from the
* app_start_timer() call to the timeout handler.
*/
static void battery_level_meas_timeout_handler(void * p_context)
{
static void battery_level_meas_timeout_handler(void *p_context) {
UNUSED_PARAMETER(p_context);
ret_code_t err_code;
+3 -6
View File
@@ -4,20 +4,17 @@
//初始化延迟函数
void bsp_delay_init(void)
{
void bsp_delay_init(void) {
}
//延时nms
//注意nms的范围
void bsp_delay_ms(uint16_t nms)
{
void bsp_delay_ms(uint16_t nms) {
nrf_delay_us(nms * 1000);
}
//延时nus
//nus为要延时的us数.
void bsp_delay_us(uint32_t nus)
{
void bsp_delay_us(uint32_t nus) {
nrf_delay_us(nus);
}
+1 -1
View File
@@ -4,7 +4,7 @@
#include "stdint.h"
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
void bsp_delay_init(void);
+5 -6
View File
@@ -25,7 +25,7 @@ static volatile enum {
* 1
* 2
*/
autotimer* bsp_obtain_timer(uint32_t start_value) {
autotimer *bsp_obtain_timer(uint32_t start_value) {
uint8_t i;
for (i = 0; i < TIMER_BSP_COUNT; i++) {
if (bsptimers[i].busy == 0) {
@@ -40,8 +40,8 @@ autotimer* bsp_obtain_timer(uint32_t start_value) {
/*
*
*/
inline uint8_t bsp_set_timer(autotimer* timer,uint32_t start_value) {
if(timer->busy == 0) return 0;
inline uint8_t bsp_set_timer(autotimer *timer, uint32_t start_value) {
if (timer->busy == 0) return 0;
timer->time = start_value;
return 1;
}
@@ -50,7 +50,7 @@ inline uint8_t bsp_set_timer(autotimer* timer,uint32_t start_value) {
*
*
*/
inline void bsp_return_timer(autotimer* timer) {
inline void bsp_return_timer(autotimer *timer) {
timer->busy = 0;
timer->time = 0;
}
@@ -59,8 +59,7 @@ inline void bsp_return_timer(autotimer* timer) {
* @param arg
* @return
*/
void timer_app_callback(void *arg)
{
void timer_app_callback(void *arg) {
UNUSED_PARAMETER(arg);
for (g_timer_fori = 0; g_timer_fori < TIMER_BSP_COUNT; g_timer_fori++) {
if (bsptimers[g_timer_fori].busy == 1) {
+3 -3
View File
@@ -27,9 +27,9 @@ void bsp_timer_uninit(void);
void bsp_timer_start(void);
void bsp_timer_stop(void);
void bsp_return_timer(autotimer* timer);
autotimer* bsp_obtain_timer(uint32_t start_value);
uint8_t bsp_set_timer(autotimer* timer,uint32_t start_value);
void bsp_return_timer(autotimer *timer);
autotimer *bsp_obtain_timer(uint32_t start_value);
uint8_t bsp_set_timer(autotimer *timer, uint32_t start_value);
#endif
+2 -3
View File
@@ -4,10 +4,9 @@
static nrf_drv_wdt_channel_id m_channel_id;
static void wdt_event_handler(void)
{
static void wdt_event_handler(void) {
//NOTE: The max amount of time we can spend in WDT interrupt is two cycles of 32768[Hz] clock - after that, reset occurs
uint32_t* p_led_array = hw_get_led_array();
uint32_t *p_led_array = hw_get_led_array();
for (uint8_t i = 0; i < RGB_LIST_NUM; i++) {
nrf_gpio_pin_clear(p_led_array[i]);
}
+1 -1
View File
@@ -2,7 +2,7 @@
#define __BSP_WDT_H__
#ifdef __cplusplus
extern "C" {
extern "C" {
#endif
void bsp_wdt_init(void);
+33 -33
View File
@@ -2,38 +2,38 @@
// CRC查表
static uint16_t crc_table[256] = {
0x0000,0x1189,0x2312,0x329B,0x4624,0x57AD,0x6536,0x74BF,
0x8C48,0x9DC1,0xAF5A,0xBED3,0xCA6C,0xDBE5,0xE97E,0xF8F7,
0x1081,0x0108,0x3393,0x221A,0x56A5,0x472C,0x75B7,0x643E,
0x9CC9,0x8D40,0xBFDB,0xAE52,0xDAED,0xCB64,0xF9FF,0xE876,
0x2102,0x308B,0x0210,0x1399,0x6726,0x76AF,0x4434,0x55BD,
0xAD4A,0xBCC3,0x8E58,0x9FD1,0xEB6E,0xFAE7,0xC87C,0xD9F5,
0x3183,0x200A,0x1291,0x0318,0x77A7,0x662E,0x54B5,0x453C,
0xBDCB,0xAC42,0x9ED9,0x8F50,0xFBEF,0xEA66,0xD8FD,0xC974,
0x4204,0x538D,0x6116,0x709F,0x0420,0x15A9,0x2732,0x36BB,
0xCE4C,0xDFC5,0xED5E,0xFCD7,0x8868,0x99E1,0xAB7A,0xBAF3,
0x5285,0x430C,0x7197,0x601E,0x14A1,0x0528,0x37B3,0x263A,
0xDECD,0xCF44,0xFDDF,0xEC56,0x98E9,0x8960,0xBBFB,0xAA72,
0x6306,0x728F,0x4014,0x519D,0x2522,0x34AB,0x0630,0x17B9,
0xEF4E,0xFEC7,0xCC5C,0xDDD5,0xA96A,0xB8E3,0x8A78,0x9BF1,
0x7387,0x620E,0x5095,0x411C,0x35A3,0x242A,0x16B1,0x0738,
0xFFCF,0xEE46,0xDCDD,0xCD54,0xB9EB,0xA862,0x9AF9,0x8B70,
0x8408,0x9581,0xA71A,0xB693,0xC22C,0xD3A5,0xE13E,0xF0B7,
0x0840,0x19C9,0x2B52,0x3ADB,0x4E64,0x5FED,0x6D76,0x7CFF,
0x9489,0x8500,0xB79B,0xA612,0xD2AD,0xC324,0xF1BF,0xE036,
0x18C1,0x0948,0x3BD3,0x2A5A,0x5EE5,0x4F6C,0x7DF7,0x6C7E,
0xA50A,0xB483,0x8618,0x9791,0xE32E,0xF2A7,0xC03C,0xD1B5,
0x2942,0x38CB,0x0A50,0x1BD9,0x6F66,0x7EEF,0x4C74,0x5DFD,
0xB58B,0xA402,0x9699,0x8710,0xF3AF,0xE226,0xD0BD,0xC134,
0x39C3,0x284A,0x1AD1,0x0B58,0x7FE7,0x6E6E,0x5CF5,0x4D7C,
0xC60C,0xD785,0xE51E,0xF497,0x8028,0x91A1,0xA33A,0xB2B3,
0x4A44,0x5BCD,0x6956,0x78DF,0x0C60,0x1DE9,0x2F72,0x3EFB,
0xD68D,0xC704,0xF59F,0xE416,0x90A9,0x8120,0xB3BB,0xA232,
0x5AC5,0x4B4C,0x79D7,0x685E,0x1CE1,0x0D68,0x3FF3,0x2E7A,
0xE70E,0xF687,0xC41C,0xD595,0xA12A,0xB0A3,0x8238,0x93B1,
0x6B46,0x7ACF,0x4854,0x59DD,0x2D62,0x3CEB,0x0E70,0x1FF9,
0xF78F,0xE606,0xD49D,0xC514,0xB1AB,0xA022,0x92B9,0x8330,
0x7BC7,0x6A4E,0x58D5,0x495C,0x3DE3,0x2C6A,0x1EF1,0x0F78,
0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD,
0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C,
0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A,
0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9,
0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7,
0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036,
0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134,
0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3,
0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1,
0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330,
0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78,
};
@@ -44,7 +44,7 @@ static uint16_t crc_table[256] = {
* @param output
*
*/
void calc_14a_crc_lut(uint8_t* data, int length, uint8_t* output) {
void calc_14a_crc_lut(uint8_t *data, int length, uint8_t *output) {
// 取巧,强制指针类型转换
uint16_t *crc = (uint16_t *)output;
// 赋予多项式初始值
+1 -1
View File
@@ -3,6 +3,6 @@
#include <stdint.h>
void calc_14a_crc_lut(uint8_t* data, int length, uint8_t* output);
void calc_14a_crc_lut(uint8_t *data, int length, uint8_t *output);
#endif
+2 -4
View File
@@ -9,8 +9,7 @@
* @retval :
*
*/
void num_to_bytes(uint64_t n, uint8_t len, uint8_t* dest)
{
void num_to_bytes(uint64_t n, uint8_t len, uint8_t *dest) {
while (len--) {
dest[len] = (uint8_t)n;
n >>= 8;
@@ -24,8 +23,7 @@ void num_to_bytes(uint64_t n, uint8_t len, uint8_t* dest)
* @retval :
*
*/
uint64_t bytes_to_num(uint8_t* src, uint8_t len)
{
uint64_t bytes_to_num(uint8_t *src, uint8_t len) {
uint64_t num = 0;
while (len--) {
num = (num << 8) | (*src);
+2 -2
View File
@@ -4,7 +4,7 @@
#include <stdint.h>
// num & bytes
void num_to_bytes(uint64_t n, uint8_t len, uint8_t* dest);
uint64_t bytes_to_num(uint8_t* src, uint8_t len);
void num_to_bytes(uint64_t n, uint8_t len, uint8_t *dest);
uint64_t bytes_to_num(uint8_t *src, uint8_t len);
#endif
+20 -20
View File
@@ -274,26 +274,26 @@ static const uint8_t TableC0[32] = {
static const uint8_t TableC7[32] = {
/* fc with Input {4,3,2,1,0} = (0,0,0,0,0) to (1,1,1,1,1) */
FC(0, 0, 0, 0, 0) << 7, FC(0, 0, 0, 0, 1) << 7, FC(0, 0, 0, 1, 0) << 7, FC(0, 0, 0, 1, 1) << 7,
FC(0, 0, 1, 0, 0) << 7, FC(0, 0, 1, 0, 1) << 7, FC(0, 0, 1, 1, 0) << 7, FC(0, 0, 1, 1, 1) << 7,
FC(0, 1, 0, 0, 0) << 7, FC(0, 1, 0, 0, 1) << 7, FC(0, 1, 0, 1, 0) << 7, FC(0, 1, 0, 1, 1) << 7,
FC(0, 1, 1, 0, 0) << 7, FC(0, 1, 1, 0, 1) << 7, FC(0, 1, 1, 1, 0) << 7, FC(0, 1, 1, 1, 1) << 7,
FC(1, 0, 0, 0, 0) << 7, FC(1, 0, 0, 0, 1) << 7, FC(1, 0, 0, 1, 0) << 7, FC(1, 0, 0, 1, 1) << 7,
FC(1, 0, 1, 0, 0) << 7, FC(1, 0, 1, 0, 1) << 7, FC(1, 0, 1, 1, 0) << 7, FC(1, 0, 1, 1, 1) << 7,
FC(1, 1, 0, 0, 0) << 7, FC(1, 1, 0, 0, 1) << 7, FC(1, 1, 0, 1, 0) << 7, FC(1, 1, 0, 1, 1) << 7,
FC(1, 1, 1, 0, 0) << 7, FC(1, 1, 1, 0, 1) << 7, FC(1, 1, 1, 1, 0) << 7, FC(1, 1, 1, 1, 1) << 7
FC(0, 0, 1, 0, 0) << 7, FC(0, 0, 1, 0, 1) << 7, FC(0, 0, 1, 1, 0) << 7, FC(0, 0, 1, 1, 1) << 7,
FC(0, 1, 0, 0, 0) << 7, FC(0, 1, 0, 0, 1) << 7, FC(0, 1, 0, 1, 0) << 7, FC(0, 1, 0, 1, 1) << 7,
FC(0, 1, 1, 0, 0) << 7, FC(0, 1, 1, 0, 1) << 7, FC(0, 1, 1, 1, 0) << 7, FC(0, 1, 1, 1, 1) << 7,
FC(1, 0, 0, 0, 0) << 7, FC(1, 0, 0, 0, 1) << 7, FC(1, 0, 0, 1, 0) << 7, FC(1, 0, 0, 1, 1) << 7,
FC(1, 0, 1, 0, 0) << 7, FC(1, 0, 1, 0, 1) << 7, FC(1, 0, 1, 1, 0) << 7, FC(1, 0, 1, 1, 1) << 7,
FC(1, 1, 0, 0, 0) << 7, FC(1, 1, 0, 0, 1) << 7, FC(1, 1, 0, 1, 0) << 7, FC(1, 1, 0, 1, 1) << 7,
FC(1, 1, 1, 0, 0) << 7, FC(1, 1, 1, 0, 1) << 7, FC(1, 1, 1, 1, 0) << 7, FC(1, 1, 1, 1, 1) << 7
};
/* Special table for nibble processing (e.g. ack), feedback at bit 3 */
static const uint8_t TableC3[32] = {
/* fc with Input {4,3,2,1,0} = (0,0,0,0,0) to (1,1,1,1,1) */
FC(0, 0, 0, 0, 0) << 3, FC(0, 0, 0, 0, 1) << 3, FC(0, 0, 0, 1, 0) << 3, FC(0, 0, 0, 1, 1) << 3,
FC(0, 0, 1, 0, 0) << 3, FC(0, 0, 1, 0, 1) << 3, FC(0, 0, 1, 1, 0) << 3, FC(0, 0, 1, 1, 1) << 3,
FC(0, 1, 0, 0, 0) << 3, FC(0, 1, 0, 0, 1) << 3, FC(0, 1, 0, 1, 0) << 3, FC(0, 1, 0, 1, 1) << 3,
FC(0, 1, 1, 0, 0) << 3, FC(0, 1, 1, 0, 1) << 3, FC(0, 1, 1, 1, 0) << 3, FC(0, 1, 1, 1, 1) << 3,
FC(1, 0, 0, 0, 0) << 3, FC(1, 0, 0, 0, 1) << 3, FC(1, 0, 0, 1, 0) << 3, FC(1, 0, 0, 1, 1) << 3,
FC(1, 0, 1, 0, 0) << 3, FC(1, 0, 1, 0, 1) << 3, FC(1, 0, 1, 1, 0) << 3, FC(1, 0, 1, 1, 1) << 3,
FC(1, 1, 0, 0, 0) << 3, FC(1, 1, 0, 0, 1) << 3, FC(1, 1, 0, 1, 0) << 3, FC(1, 1, 0, 1, 1) << 3,
FC(1, 1, 1, 0, 0) << 3, FC(1, 1, 1, 0, 1) << 3, FC(1, 1, 1, 1, 0) << 3, FC(1, 1, 1, 1, 1) << 3
FC(0, 0, 1, 0, 0) << 3, FC(0, 0, 1, 0, 1) << 3, FC(0, 0, 1, 1, 0) << 3, FC(0, 0, 1, 1, 1) << 3,
FC(0, 1, 0, 0, 0) << 3, FC(0, 1, 0, 0, 1) << 3, FC(0, 1, 0, 1, 0) << 3, FC(0, 1, 0, 1, 1) << 3,
FC(0, 1, 1, 0, 0) << 3, FC(0, 1, 1, 0, 1) << 3, FC(0, 1, 1, 1, 0) << 3, FC(0, 1, 1, 1, 1) << 3,
FC(1, 0, 0, 0, 0) << 3, FC(1, 0, 0, 0, 1) << 3, FC(1, 0, 0, 1, 0) << 3, FC(1, 0, 0, 1, 1) << 3,
FC(1, 0, 1, 0, 0) << 3, FC(1, 0, 1, 0, 1) << 3, FC(1, 0, 1, 1, 0) << 3, FC(1, 0, 1, 1, 1) << 3,
FC(1, 1, 0, 0, 0) << 3, FC(1, 1, 0, 0, 1) << 3, FC(1, 1, 0, 1, 0) << 3, FC(1, 1, 0, 1, 1) << 3,
FC(1, 1, 1, 0, 0) << 3, FC(1, 1, 1, 0, 1) << 3, FC(1, 1, 1, 1, 0) << 3, FC(1, 1, 1, 1, 1) << 3
};
/* Filter Output Macros */
@@ -652,8 +652,8 @@ void Crypto1Auth(uint8_t EncryptedReaderNonce[NONCE_SIZE]) {
/* Bit 0 */
Feedback = CRYPTO1_FILTER_OUTPUT_B0_24(Odd0, Odd1, Odd2);
Feedback = Crypto1LFSRbyteFeedback(Even0, Even1, Even2, Odd0, Odd1, Odd2)
^ Feedback
^ In;
^ Feedback
^ In;
In >>= 1;
SHIFT24(Even0, Even1, Even2, Feedback);
@@ -685,8 +685,8 @@ void Crypto1Auth(uint8_t EncryptedReaderNonce[NONCE_SIZE]) {
/* Bit 4 */
Feedback = CRYPTO1_FILTER_OUTPUT_B0_24(Odd0, Odd1, Odd2);
Feedback = Crypto1LFSRbyteFeedback(Even0, Even1, Even2, Odd0, Odd1, Odd2)
^ Feedback
^ In;
^ Feedback
^ In;
In >>= 1;
SHIFT24(Even0, Even1, Even2, Feedback);
@@ -701,8 +701,8 @@ void Crypto1Auth(uint8_t EncryptedReaderNonce[NONCE_SIZE]) {
/* Bit 6 */
Feedback = CRYPTO1_FILTER_OUTPUT_B0_24(Odd0, Odd1, Odd2);
Feedback = Crypto1LFSRbyteFeedback(Even0, Even1, Even2, Odd0, Odd1, Odd2)
^ Feedback
^ In;
^ Feedback
^ In;
In >>= 1;
SHIFT24(Even0, Even1, Even2, Feedback);
@@ -360,12 +360,11 @@ void nfc_tag_14a_tx_nbit_delay_window(uint8_t data, uint32_t bits) {
/**
* 14a监听到PCD过来的数据处理的封装函数
*/
void nfc_tag_14a_data_process(uint8_t *p_data)
{
void nfc_tag_14a_data_process(uint8_t *p_data) {
// 统计一下当前收到的bit数
uint16_t szDataBits = (NRF_NFCT->RXD.AMOUNT & (NFCT_RXD_AMOUNT_RXDATABITS_Msk | NFCT_RXD_AMOUNT_RXDATABYTES_Msk));
// 防冲撞可能要用上的资源
nfc_tag_14a_coll_res_referen_t* auto_coll_res = m_tag_handler.get_coll_res != NULL ? m_tag_handler.get_coll_res() : NULL;
nfc_tag_14a_coll_res_referen_t *auto_coll_res = m_tag_handler.get_coll_res != NULL ? m_tag_handler.get_coll_res() : NULL;
// 我也不知道为什么,这里CPU必须要空跑一段周期,数据才能正常收到。
// 如果接收数据有任何问题,请尝试恢复此处,这个是2021年发现的问题,但是2022年又消失了
@@ -461,7 +460,7 @@ void nfc_tag_14a_data_process(uint8_t *p_data)
}
}
// 匹配UID长度,为uid的返回数据做准备
switch(*auto_coll_res->size) {
switch (*auto_coll_res->size) {
case NFC_TAG_14A_UID_SINGLE_SIZE: {
if (level == NFC_TAG_14A_CASCADE_LEVEL_1) { // 首次级联,只有一次
// 4字节的标签最多只能一次级联
@@ -655,7 +654,7 @@ void nfc_tag_14a_event_callback(nrfx_nfct_evt_t const *p_event) {
}
case NRFX_NFCT_EVT_ERROR: {
// 根据错误原因,进行日志打印,以帮助开发时排查可能性的BUG
switch(p_event->params.error.reason) {
switch (p_event->params.error.reason) {
case NRFX_NFCT_ERROR_FRAMEDELAYTIMEOUT: {
// 如果我们在通信窗口中回应了标签但是却是没有及时回应,那就需要进行报错打印
// 如果此错误非常频繁的出现,则可能是MCU处理速度没跟上,此时开发者就需要优化代码了
@@ -690,7 +689,7 @@ void nfc_tag_14a_set_state(nfc_tag_14a_state_t state) {
* 14A的处理器注册函数
* @param handler
*/
void nfc_tag_14a_set_handler(nfc_tag_14a_handler_t* handler) {
void nfc_tag_14a_set_handler(nfc_tag_14a_handler_t *handler) {
if (handler != NULL) {
// 直接取出传入的实现赋值到我们的全局对象即可
m_tag_handler.cb_reset = handler->cb_reset;
@@ -733,6 +732,6 @@ void nfc_tag_14a_sense_switch(bool enable) {
bool is_valid_uid_size(uint8_t uid_length) {
return uid_length == NFC_TAG_14A_UID_SINGLE_SIZE ||
uid_length == NFC_TAG_14A_UID_DOUBLE_SIZE ||
uid_length == NFC_TAG_14A_UID_TRIPLE_SIZE;
uid_length == NFC_TAG_14A_UID_DOUBLE_SIZE ||
uid_length == NFC_TAG_14A_UID_TRIPLE_SIZE;
}
@@ -73,17 +73,17 @@ typedef struct {
// 防冲突资源的封装引用,纯引用空间占用比较小
typedef struct {
nfc_tag_14a_uid_size* size;
uint8_t* atqa;
uint8_t* sak;
uint8_t* uid;
nfc_14a_ats_t* ats;
nfc_tag_14a_uid_size *size;
uint8_t *atqa;
uint8_t *sak;
uint8_t *uid;
nfc_14a_ats_t *ats;
} nfc_tag_14a_coll_res_referen_t;
// 通信接管需要实现的回调函数
typedef void (*nfc_tag_14a_reset_handler_t)(void);
typedef void (*nfc_tag_14a_state_handler_t)(uint8_t* data, uint16_t szBits);
typedef nfc_tag_14a_coll_res_referen_t* (*nfc_tag_14a_coll_handler_t)(void);
typedef void (*nfc_tag_14a_state_handler_t)(uint8_t *data, uint16_t szBits);
typedef nfc_tag_14a_coll_res_referen_t *(*nfc_tag_14a_coll_handler_t)(void);
// 14a通信接管者需要实现的接口
typedef struct {
@@ -106,7 +106,7 @@ uint8_t nfc_tag_14a_unwrap_frame(const uint8_t *pbtFrame, const size_t szFrameBi
// 14a通信控制
void nfc_tag_14a_sense_switch(bool enable);
void nfc_tag_14a_set_handler(nfc_tag_14a_handler_t* handler);
void nfc_tag_14a_set_handler(nfc_tag_14a_handler_t *handler);
void nfc_tag_14a_set_state(nfc_tag_14a_state_t state);
void nfc_tag_14a_tx_bytes(uint8_t *data, uint32_t bytes, bool appendCrc);
void nfc_tag_14a_tx_bytes_delay_freerun(uint8_t *data, uint32_t bytes, bool appendCrc);
@@ -181,11 +181,11 @@ static nfc_tag_mf1_std_state_machine_t m_mf1_state = MF1_STATE_UNAUTH;
// 保存当前的GEN1A状态
static nfc_tag_mf1_gen1a_state_machine_t m_gen1a_state = GEN1A_STATE_DISABLE;
// 指向标签信息的数据结构指针
static nfc_tag_mf1_information_t* m_tag_information = NULL;
static nfc_tag_mf1_information_t *m_tag_information = NULL;
// 定义并且使用影子防冲撞资源
static nfc_tag_14a_coll_res_referen_t m_shadow_coll_res;
// 指向标签扇区中的尾部块(控制数据块)
static nfc_tag_mf1_trailer_info_t* m_tag_trailer_info = NULL;
static nfc_tag_mf1_trailer_info_t *m_tag_trailer_info = NULL;
// 定义并且使用mf1专用通信缓冲区
static nfc_tag_mf1_tx_buffer_t m_tag_tx_buffer;
// 保存当前正在模拟的MF1的具体类型
@@ -388,7 +388,7 @@ void append_mf1_auth_log_step3(bool is_auth_success) {
/** @brief mf1获得验证日志
* @param count:
*/
nfc_tag_mf1_auth_log_t* get_mf1_auth_log(uint32_t* count) {
nfc_tag_mf1_auth_log_t *get_mf1_auth_log(uint32_t *count) {
// 先传递验证的日志条目总数出去
*count = m_auth_log.count;
// 直接返回日志数组的头部指针就好了
@@ -397,7 +397,7 @@ nfc_tag_mf1_auth_log_t* get_mf1_auth_log(uint32_t* count) {
static int get_block_max_by_tag_type(tag_specific_type_t tag_type) {
int block_max;
switch(tag_type) {
switch (tag_type) {
case TAG_TYPE_MIFARE_Mini:
block_max = 20;
break;
@@ -433,7 +433,7 @@ void mf1_prng_by_bytes(uint8_t *nonces, uint32_t n) {
* @param szBits
* @param state
*/
void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
void nfc_tag_mf1_state_handler(uint8_t *p_data, uint16_t szDataBits) {
// 处理特殊指令,比如兼容mifare gen1a标签
if (szDataBits <= 8) {
// 只有启用了GEN1A模式的情况下才允许后门指令的响应
@@ -469,7 +469,7 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
case MF1_STATE_UNAUTH: { // 未验证状态,通信是开放性的
if (szDataBits == 32) { // 32位,可能是指令
if (nfc_tag_14a_checks_crc(p_data, 4)) {
switch(p_data[0]) {
switch (p_data[0]) {
case CMD_AUTH_A:
case CMD_AUTH_B: {
uint8_t BlockAuth = p_data[1];
@@ -498,7 +498,7 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
KeyInUse = p_data[0] & 1;
// 获得指定的扇区访问控制字节,此处我们直接取巧,将内存转为结构体,让编译器帮我们维护指针的指向
m_tag_trailer_info = (nfc_tag_mf1_trailer_info_t*)m_tag_information->memory[BlockEnd];
m_tag_trailer_info = (nfc_tag_mf1_trailer_info_t *)m_tag_information->memory[BlockEnd];
// 生成随机数
nfc_tag_mf1_random_nonce(CardNonce, false);
@@ -549,9 +549,9 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
crypto1_deinit(pcs);
// 加载密钥流
crypto1_init(pcs,
// 根据当前的指令类型选择验证A或者B秘钥
bytes_to_num(KeyInUse ? m_tag_trailer_info->keyb : m_tag_trailer_info->keya, 6)
);
// 根据当前的指令类型选择验证A或者B秘钥
bytes_to_num(KeyInUse ? m_tag_trailer_info->keyb : m_tag_trailer_info->keya, 6)
);
// 设置密钥流
crypto1_word(pcs, bytes_to_num(UID_BY_CASCADE_LEVEL, 4) ^ bytes_to_num(CardNonce, 4), 0);
#endif
@@ -688,7 +688,7 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
// 清空一下buffer,避免缓存的数据影响到后续操作
memset(m_tag_tx_buffer.tx_raw_buffer, 0x00, sizeof(m_tag_tx_buffer.tx_raw_buffer));
// 让这块数据区域变成我们需要的尾部块类型
nfc_tag_mf1_trailer_info_t* respTrailerInfo = (nfc_tag_mf1_trailer_info_t*)m_tag_tx_buffer.tx_raw_buffer;
nfc_tag_mf1_trailer_info_t *respTrailerInfo = (nfc_tag_mf1_trailer_info_t *)m_tag_tx_buffer.tx_raw_buffer;
// 尾部块的读取有以下条件限制:
// 1、要始终可以复制GPBGlobal Public Byte)也就是控制位最后一个字节
// 2、秘钥A永远无法被读取!
@@ -792,9 +792,9 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
status = ACK_VALUE;
}
#ifdef NFC_MF1_FAST_SIM
nfc_tag_14a_tx_nbit(status ^ Crypto1Nibble(), 4);
nfc_tag_14a_tx_nbit(status ^ Crypto1Nibble(), 4);
#else
nfc_tag_14a_tx_nbit(mf_crypto1_encrypt4bit(pcs, status), 4);
nfc_tag_14a_tx_nbit(mf_crypto1_encrypt4bit(pcs, status), 4);
#endif
break;
}
@@ -827,7 +827,7 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
KeyInUse = p_data[0] & 1;
// 获得指定的扇区访问控制字节,此处我们直接取巧,将内存转为结构体,让编译器帮我们维护指针的指向
m_tag_trailer_info = (nfc_tag_mf1_trailer_info_t*)m_tag_information->memory[BlockEnd];
m_tag_trailer_info = (nfc_tag_mf1_trailer_info_t *)m_tag_information->memory[BlockEnd];
// 生成随机数
nfc_tag_mf1_random_nonce(CardNonce, true);
@@ -884,9 +884,9 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
crypto1_deinit(pcs);
// 加载密钥流
crypto1_init(pcs,
// 根据当前的指令类型选择验证A或者B秘钥
bytes_to_num(KeyInUse ? m_tag_trailer_info->keyb : m_tag_trailer_info->keya, 6)
);
// 根据当前的指令类型选择验证A或者B秘钥
bytes_to_num(KeyInUse ? m_tag_trailer_info->keyb : m_tag_trailer_info->keya, 6)
);
// 进行随机数加密
uint8_t m_auth_nt_keystream[4];
num_to_bytes(bytes_to_num(UID_BY_CASCADE_LEVEL, 4) ^ bytes_to_num(CardNonce, 4), 4, m_auth_nt_keystream);
@@ -1048,11 +1048,11 @@ void nfc_tag_mf1_state_handler(uint8_t* p_data, uint16_t szDataBits) {
/**
* @brief mifare标签必要的防冲突资源
*/
nfc_tag_14a_coll_res_referen_t* get_mifare_coll_res() {
nfc_tag_14a_coll_res_referen_t *get_mifare_coll_res() {
// 根据当前的互通配置,选择性的返回其中配置的数据,假设开启了数据互通,那么我们还需要确保当前模拟的卡是4BYTE的
if (m_tag_information->config.use_mf1_coll_res && m_tag_information->res_coll.size == NFC_TAG_14A_UID_SINGLE_SIZE) {
// 获得数据区域的厂商信息
nfc_tag_mf1_factory_info_t* block0_factory_info = (nfc_tag_mf1_factory_info_t*)m_tag_information->memory[0];
nfc_tag_mf1_factory_info_t *block0_factory_info = (nfc_tag_mf1_factory_info_t *)m_tag_information->memory[0];
m_shadow_coll_res.sak = block0_factory_info->sak; // 替换sak
m_shadow_coll_res.atqa = block0_factory_info->atqa; // 替换atqa
m_shadow_coll_res.uid = block0_factory_info->uid; // 替换uid
@@ -1099,7 +1099,7 @@ static int get_information_size_by_tag_type(tag_specific_type_t type, bool auth_
* @param buffer
* @return 0
*/
int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t* buffer) {
int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer) {
if (m_tag_type != TAG_TYPE_UNKNOWN) {
if (m_tag_information->config.mode_block_write == NFC_TAG_MF1_WRITE_SHADOW) {
NRF_LOG_INFO("The mf1 is shadow write mode.");
@@ -1116,7 +1116,7 @@ int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t* buffer)
* @param type
* @param buffer
*/
int nfc_tag_mf1_data_loadcb(tag_specific_type_t type, tag_data_buffer_t* buffer) {
int nfc_tag_mf1_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffer) {
// 确保外部容量足够转换为信息结构体
int info_size = get_information_size_by_tag_type(type, false);
if (buffer->length >= info_size) {
@@ -83,7 +83,8 @@ typedef struct __attribute__((aligned(4))) {
nfc_tag_14a_coll_res_entity_t res_coll;
nfc_tag_mf1_configure_t config;
uint8_t memory[NFC_TAG_MF1_BLOCK_MAX][NFC_TAG_MF1_DATA_SIZE];
} nfc_tag_mf1_information_t;
}
nfc_tag_mf1_information_t;
// 4Byte卡片的出厂固化的0块结构
typedef struct {
@@ -135,15 +136,15 @@ typedef struct {
} nfc_tag_mf1_auth_log_t;
nfc_tag_mf1_auth_log_t* get_mf1_auth_log(uint32_t* count);
int nfc_tag_mf1_data_loadcb(tag_specific_type_t type, tag_data_buffer_t* buffer);
int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t* buffer);
nfc_tag_mf1_auth_log_t *get_mf1_auth_log(uint32_t *count);
int nfc_tag_mf1_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffer);
int nfc_tag_mf1_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer);
bool nfc_tag_mf1_data_factory(uint8_t slot, tag_specific_type_t tag_type);
void nfc_tag_mf1_set_detection_enable(bool enable);
bool nfc_tag_mf1_is_detection_enable(void);
void nfc_tag_mf1_detection_log_clear(void);
uint32_t nfc_tag_mf1_detection_log_count(void);
nfc_tag_14a_coll_res_referen_t* get_mifare_coll_res(void);
nfc_tag_14a_coll_res_referen_t *get_mifare_coll_res(void);
void nfc_tag_mf1_set_gen1a_magic_mode(bool enable);
bool nfc_tag_mf1_is_gen1a_magic_mode(void);
void nfc_tag_mf1_set_gen2_magic_mode(bool enable);
@@ -70,7 +70,7 @@ const uint8_t ntagVersion[8] = {0x00, 0x04, 0x04, 0x02, 0x01, 0x00, 0x11, 0x03};
uint8_t ntagPwdOK[2] = {0x80, 0x80};
// 指向标签信息的数据结构指针
static nfc_tag_ntag_information_t* m_tag_information = NULL;
static nfc_tag_ntag_information_t *m_tag_information = NULL;
// 定义并且使用影子防冲撞资源
static nfc_tag_14a_coll_res_referen_t m_shadow_coll_res;
// 定义并且使用ntag专用通信缓冲区
@@ -80,7 +80,7 @@ static tag_specific_type_t m_tag_type;
static int get_block_max_by_tag_type(tag_specific_type_t tag_type) {
int block_max;
switch(tag_type) {
switch (tag_type) {
case TAG_TYPE_NTAG_213:
block_max = NTAG213_PAGES;
break;
@@ -97,7 +97,7 @@ static int get_block_max_by_tag_type(tag_specific_type_t tag_type) {
static int get_block_cfg_by_tag_type(tag_specific_type_t tag_type) {
int block_max;
switch(tag_type) {
switch (tag_type) {
case TAG_TYPE_NTAG_213:
block_max = NTAG213_CONFIG_AREA_START_ADDRESS;
break;
@@ -112,31 +112,31 @@ static int get_block_cfg_by_tag_type(tag_specific_type_t tag_type) {
return block_max;
}
void nfc_tag_ntag_state_handler(uint8_t* p_data, uint16_t szDataBits) {
void nfc_tag_ntag_state_handler(uint8_t *p_data, uint16_t szDataBits) {
uint8_t command = p_data[0];
uint8_t block_num = p_data[1];
switch(command) {
switch (command) {
case CMD_GET_VERSION:
memcpy(m_tag_tx_buffer.tx_buffer, ntagVersion, 8);
switch (m_tag_type) {
case TAG_TYPE_NTAG_213:
m_tag_tx_buffer.tx_buffer[6] = NTAG213_VERSION;
break;
default:
case TAG_TYPE_NTAG_215:
m_tag_tx_buffer.tx_buffer[6] = NTAG215_VERSION;
break;
case TAG_TYPE_NTAG_216:
m_tag_tx_buffer.tx_buffer[6] = NTAG216_VERSION;
break;
case TAG_TYPE_NTAG_213:
m_tag_tx_buffer.tx_buffer[6] = NTAG213_VERSION;
break;
default:
case TAG_TYPE_NTAG_215:
m_tag_tx_buffer.tx_buffer[6] = NTAG215_VERSION;
break;
case TAG_TYPE_NTAG_216:
m_tag_tx_buffer.tx_buffer[6] = NTAG216_VERSION;
break;
}
nfc_tag_14a_tx_bytes(m_tag_tx_buffer.tx_buffer, 8, true);
break;
case CMD_READ:
if (block_num < get_block_max_by_tag_type(m_tag_type)) {
for (int block = 0; block < 4; block++) {
memcpy(m_tag_tx_buffer.tx_buffer + block*4, m_tag_information->memory[block_num+block], NFC_TAG_NTAG_DATA_SIZE);
memcpy(m_tag_tx_buffer.tx_buffer + block * 4, m_tag_information->memory[block_num + block], NFC_TAG_NTAG_DATA_SIZE);
}
nfc_tag_14a_tx_bytes(m_tag_tx_buffer.tx_buffer, BYTES_PER_READ, true);
} else {
@@ -150,7 +150,7 @@ void nfc_tag_ntag_state_handler(uint8_t* p_data, uint16_t szDataBits) {
break;
}
for (int block = block_num; block <= end_block_num; block++) {
memcpy(m_tag_tx_buffer.tx_buffer + (block - block_num)*4, m_tag_information->memory[block], NFC_TAG_NTAG_DATA_SIZE);
memcpy(m_tag_tx_buffer.tx_buffer + (block - block_num) * 4, m_tag_information->memory[block], NFC_TAG_NTAG_DATA_SIZE);
}
nfc_tag_14a_tx_bytes(m_tag_tx_buffer.tx_buffer, (end_block_num - block_num + 1) * NFC_TAG_NTAG_DATA_SIZE, true);
break;
@@ -171,7 +171,7 @@ void nfc_tag_ntag_state_handler(uint8_t* p_data, uint16_t szDataBits) {
break;
}
/* Authenticate the user */
//RESET AUTHLIM COUNTER, CURRENTLY NOT IMPLEMENTED
//RESET AUTHLIM COUNTER, CURRENTLY NOT IMPLEMENTED
// TODO
/* Send the PACK value back */
if (m_tag_information->config.mode_uid_magic) {
@@ -189,7 +189,7 @@ void nfc_tag_ntag_state_handler(uint8_t* p_data, uint16_t szDataBits) {
return;
}
nfc_tag_14a_coll_res_referen_t* get_ntag_coll_res() {
nfc_tag_14a_coll_res_referen_t *get_ntag_coll_res() {
// 使用单独的防冲突信息,而不是使用扇区中的信息
m_shadow_coll_res.sak = m_tag_information->res_coll.sak;
m_shadow_coll_res.atqa = m_tag_information->res_coll.atqa;
@@ -213,7 +213,7 @@ static int get_information_size_by_tag_type(tag_specific_type_t type) {
* @param buffer
* @return 0
*/
int nfc_tag_ntag_data_savecb(tag_specific_type_t type, tag_data_buffer_t* buffer) {
int nfc_tag_ntag_data_savecb(tag_specific_type_t type, tag_data_buffer_t *buffer) {
if (m_tag_type != TAG_TYPE_UNKNOWN) {
// 根据当前标签类型保存对应大小的数据
return get_information_size_by_tag_type(type);
@@ -222,7 +222,7 @@ int nfc_tag_ntag_data_savecb(tag_specific_type_t type, tag_data_buffer_t* buffer
}
}
int nfc_tag_ntag_data_loadcb(tag_specific_type_t type, tag_data_buffer_t* buffer) {
int nfc_tag_ntag_data_loadcb(tag_specific_type_t type, tag_data_buffer_t *buffer) {
int info_size = get_information_size_by_tag_type(type);
if (buffer->length >= info_size) {
// 将数据缓冲区强转为ntag结构类型
@@ -231,8 +231,8 @@ int nfc_tag_ntag_data_loadcb(tag_specific_type_t type, tag_data_buffer_t* buffer
m_tag_type = type;
// 注册14a通信管理接口
nfc_tag_14a_handler_t handler_for_14a = {
.get_coll_res = get_ntag_coll_res,
.cb_state = nfc_tag_ntag_state_handler,
.get_coll_res = get_ntag_coll_res,
.cb_state = nfc_tag_ntag_state_handler,
.cb_reset = nfc_tag_ntag_reset_handler,
};
nfc_tag_14a_set_handler(&handler_for_14a);
@@ -266,8 +266,8 @@ bool nfc_tag_ntag_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
memcpy(p_ntag_information->memory[block], default_p2, NFC_TAG_NTAG_DATA_SIZE);
}
}
// default ntag auto ant-collision res
// default ntag auto ant-collision res
p_ntag_information->res_coll.atqa[0] = 0x44;
p_ntag_information->res_coll.atqa[1] = 0x00;
p_ntag_information->res_coll.sak[0] = 0x00;
@@ -280,11 +280,11 @@ bool nfc_tag_ntag_data_factory(uint8_t slot, tag_specific_type_t tag_type) {
p_ntag_information->res_coll.uid[6] = 0x64;
p_ntag_information->res_coll.size = NFC_TAG_14A_UID_DOUBLE_SIZE;
p_ntag_information->res_coll.ats.length = 0;
// default ntag config
p_ntag_information->config.mode_uid_magic = true;
p_ntag_information->config.detection_enable = false;
// save data to flash
tag_sense_type_t sense_type = get_sense_type_from_tag_type(tag_type);
fds_slot_record_map_t map_info;

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