diff --git a/docs/protocol.md b/docs/protocol.md index 28b8e425..d097ef29 100644 --- a/docs/protocol.md +++ b/docs/protocol.md @@ -50,7 +50,7 @@ E.g. LRC3(DATA) == LRC3(whole frame) Each command and response have their own payload formats. -Standard response status is `STATUS_DEVICE_SUCCESS` for general commands, `HF_TAG_OK` for HF commands and `LF_TAG_OK` for LF commands. +Standard response status is `STATUS_SUCCESS` for general commands, `STATUS_HF_TAG_OK` for HF commands and `STATUS_LF_TAG_OK` for LF commands. See [Guidelines](#new-data-payloads-guidelines-for-developers) for more info. Beware, slots in protocol count from 0 to 7 (and from 1 to 8 in the CLI...). @@ -144,7 +144,7 @@ Notes: the returned string is the output of `git describe --abbrev=7 --dirty --a * CLI: cf `hw slot list` ### 1020: WIPE_FDS * Command: no data -* Response: no data. Status is `STATUS_DEVICE_SUCCESS` or `STATUS_FLASH_WRITE_FAIL`. The device will reboot shortly after this command. +* Response: no data. Status is `STATUS_SUCCESS` or `STATUS_FLASH_WRITE_FAIL`. The device will reboot shortly after this command. * CLI: cf `hw factory_reset` ### 1021: DELETE_SLOT_TAG_NICK * Command: 2 bytes. `slot_number|sense_type` with `slot_number` between 0 and 7 and `sense_type` according to `tag_sense_type_t` enum. @@ -226,7 +226,7 @@ Notes: wait about 5 seconds after wake-up, before querying the battery status, e * CLI: cf `hf 14a scan` Notes: -* remind that if no tag is present, status will be `HF_TAG_NO` and Response empty. +* remind that if no tag is present, status will be `STATUS_HF_TAG_NO` and Response empty. * at the moment, the firmware supports only one tag, but get your client ready for more! * `atslen` must not be confused with `ats[0]`==`TL`. So `atslen|ats` = `00` means no ATS while `0100` would be an empty ATS. ### 2001: MF1_DETECT_SUPPORT @@ -269,7 +269,7 @@ Notes: ### 2007: MF1_AUTH_ONE_KEY_BLOCK * Command: 8 bytes: `type|block|key[6]`. Key as 6 bytes. Type=0x60 for key A, 0x61 for key B. * Response: no data -* Status will be `HF_TAG_OK` if auth succeeded, else `MF_ERR_AUTH` +* Status will be `STATUS_HF_TAG_OK` if auth succeeded, else `STATUS_MF_ERR_AUTH` * CLI: cf `hf mf nested` ### 2008: MF1_READ_ONE_BLOCK * Command: 8 bytes: `type|block|key[6]`. Key as 6 bytes. Type=0x60 for key A, 0x61 for key B. @@ -397,7 +397,7 @@ Be verbose, explicit and reuse conventions, in order to enhance code maintainabi - Avoid hardcoding offsets, use `sizeof()`, `offsetof(struct, field)` in C and `struct.calcsize()` in Python - For complex bitfield structs, exceptionally you can use ctypes in Python. Beware ctypes.BigEndianStructure bitfield will be parsed in the firmware in the reverse order, from LSB to MSB. ### Guideline: Status -If single byte of data to return, still use a 1-byte `data`, not `status`. Standard response status is `STATUS_DEVICE_SUCCESS` for general commands, `HF_TAG_OK` for HF commands and `LF_TAG_OK` for LF commands. If the response status is different than those, the response data is empty. Response status are generic and cover things like tag disappearance or tag non-conformities with the ISO standard. If a command needs more specific response status, it is added in the first byte of the data, to avoid cluttering the 1-byte general status enum with command-specific statuses. See e.g. [MF1_DARKSIDE_ACQUIRE](#2004-mf1_darkside_acquire). +If single byte of data to return, still use a 1-byte `data`, not `status`. Standard response status is `STATUS_SUCCESS` for general commands, `STATUS_HF_TAG_OK` for HF commands and `STATUS_LF_TAG_OK` for LF commands. If the response status is different than those, the response data is empty. Response status are generic and cover things like tag disappearance or tag non-conformities with the ISO standard. If a command needs more specific response status, it is added in the first byte of the data, to avoid cluttering the 1-byte general status enum with command-specific statuses. See e.g. [MF1_DARKSIDE_ACQUIRE](#2004-mf1_darkside_acquire). ### Guideline: unambiguous types - Use unambiguous types such as `uint16_t`, not `int` or `enum`. Cast explicitly `int` and `enum` to `uint_t` of proper size - Use Network byte order for 16b and 32b integers diff --git a/firmware/application/src/app_cmd.c b/firmware/application/src/app_cmd.c index 7998ec97..00e3f54e 100644 --- a/firmware/application/src/app_cmd.c +++ b/firmware/application/src/app_cmd.c @@ -39,18 +39,18 @@ static data_frame_tx_t *cmd_processor_get_app_version(uint16_t cmd, uint16_t sta } PACKED payload; payload.version_major = APP_FW_VER_MAJOR; payload.version_minor = APP_FW_VER_MINOR; - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_get_git_version(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, strlen(GIT_VERSION), (uint8_t *)GIT_VERSION); + return data_frame_make(cmd, STATUS_SUCCESS, strlen(GIT_VERSION), (uint8_t *)GIT_VERSION); } static data_frame_tx_t *cmd_processor_get_device_model(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t resp_data = hw_get_device_type(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(resp_data), &resp_data); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(resp_data), &resp_data); } @@ -62,7 +62,7 @@ static data_frame_tx_t *cmd_processor_change_device_mode(uint16_t cmd, uint16_t if (data[0] == 1) { #if defined(PROJECT_CHAMELEON_ULTRA) reader_mode_enter(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); #else return data_frame_make(cmd, STATUS_NOT_IMPLEMENTED, 0, NULL); #endif @@ -70,13 +70,13 @@ static data_frame_tx_t *cmd_processor_change_device_mode(uint16_t cmd, uint16_t #if defined(PROJECT_CHAMELEON_ULTRA) tag_mode_enter(); #endif - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } } static data_frame_tx_t *cmd_processor_get_device_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t resp_data = (get_device_mode() == DEVICE_MODE_READER); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(resp_data), &resp_data); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(resp_data), &resp_data); } static data_frame_tx_t *cmd_processor_enter_bootloader(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -90,7 +90,7 @@ static data_frame_tx_t *cmd_processor_enter_bootloader(uint16_t cmd, uint16_t st // Never into here... while (1) __NOP(); // For the compiler to be happy... - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_device_chip_id(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -100,7 +100,7 @@ static data_frame_tx_t *cmd_processor_get_device_chip_id(uint16_t cmd, uint16_t } PACKED payload; payload.chip_LSW = U32HTONL(NRF_FICR->DEVICEID[0]); payload.chip_HSW = U32HTONL(NRF_FICR->DEVICEID[1]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_get_device_address(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -115,7 +115,7 @@ static data_frame_tx_t *cmd_processor_get_device_address(uint16_t cmd, uint16_t } PACKED payload; payload.device_address_LSW = U32HTONL(NRF_FICR->DEVICEADDR[0]); payload.device_address_HSW = U16HTONS(NRF_FICR->DEVICEADDR[1] | 0xC000); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_save_settings(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -139,7 +139,7 @@ static data_frame_tx_t *cmd_processor_get_device_settings(uint16_t cmd, uint16_t settings[5] = settings_get_long_button_press_config('B'); // long B button press mode settings[6] = settings_get_ble_pairing_enable(); // is device require pairing memcpy(settings + 7, settings_get_ble_connect_key(), BLE_PAIRING_KEY_LEN); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 7 + BLE_PAIRING_KEY_LEN, settings); + return data_frame_make(cmd, STATUS_SUCCESS, 7 + BLE_PAIRING_KEY_LEN, settings); } static data_frame_tx_t *cmd_processor_set_animation_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -147,12 +147,12 @@ static data_frame_tx_t *cmd_processor_set_animation_mode(uint16_t cmd, uint16_t return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } settings_set_animation_config(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_animation_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t animation_mode = settings_get_animation_config(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &animation_mode); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &animation_mode); } static data_frame_tx_t *cmd_processor_get_battery_info(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -162,7 +162,7 @@ static data_frame_tx_t *cmd_processor_get_battery_info(uint16_t cmd, uint16_t st } PACKED payload; payload.voltage = U16HTONS(batt_lvl_in_milli_volts); payload.percent = percentage_batt_lvl; - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_get_button_press_config(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -170,7 +170,7 @@ static data_frame_tx_t *cmd_processor_get_button_press_config(uint16_t cmd, uint return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } uint8_t button_press_config = settings_get_button_press_config(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(button_press_config), &button_press_config); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(button_press_config), &button_press_config); } static data_frame_tx_t *cmd_processor_set_button_press_config(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -178,7 +178,7 @@ static data_frame_tx_t *cmd_processor_set_button_press_config(uint16_t cmd, uint return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } settings_set_button_press_config(data[0], data[1]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_long_button_press_config(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -186,7 +186,7 @@ static data_frame_tx_t *cmd_processor_get_long_button_press_config(uint16_t cmd, return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } uint8_t button_press_config = settings_get_long_button_press_config(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(button_press_config), &button_press_config); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(button_press_config), &button_press_config); } static data_frame_tx_t *cmd_processor_set_long_button_press_config(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -194,12 +194,12 @@ static data_frame_tx_t *cmd_processor_set_long_button_press_config(uint16_t cmd, return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } settings_set_long_button_press_config(data[0], data[1]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_ble_pairing_enable(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t is_enable = settings_get_ble_pairing_enable(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &is_enable); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &is_enable); } static data_frame_tx_t *cmd_processor_set_ble_pairing_enable(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -207,7 +207,7 @@ static data_frame_tx_t *cmd_processor_set_ble_pairing_enable(uint16_t cmd, uint1 return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } settings_set_ble_pairing_enable(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } #if defined(PROJECT_CHAMELEON_ULTRA) @@ -215,7 +215,7 @@ static data_frame_tx_t *cmd_processor_set_ble_pairing_enable(uint16_t cmd, uint1 static data_frame_tx_t *cmd_processor_hf14a_scan(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { picc_14a_tag_t taginfo; status = pcd_14a_reader_scan_auto(&taginfo); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } // uidlen[1]|uid[uidlen]|atqa[2]|sak[1]|atslen[1]|ats[atslen] @@ -231,7 +231,7 @@ static data_frame_tx_t *cmd_processor_hf14a_scan(uint16_t cmd, uint16_t status, payload[offset++] = taginfo.ats_len; memcpy(&payload[offset], taginfo.ats, taginfo.ats_len); offset += taginfo.ats_len; - return data_frame_make(cmd, HF_TAG_OK, offset, payload); + return data_frame_make(cmd, STATUS_HF_TAG_OK, offset, payload); } static data_frame_tx_t *cmd_processor_mf1_detect_support(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -242,10 +242,10 @@ static data_frame_tx_t *cmd_processor_mf1_detect_support(uint16_t cmd, uint16_t static data_frame_tx_t *cmd_processor_mf1_detect_prng(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t type; status = check_prng_type((mf1_prng_type_t *)&type); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } - return data_frame_make(cmd, HF_TAG_OK, sizeof(type), &type); + return data_frame_make(cmd, STATUS_HF_TAG_OK, sizeof(type), &type); } // We have a reusable payload structure. @@ -265,11 +265,11 @@ static data_frame_tx_t *cmd_processor_mf1_static_nested_acquire(uint16_t cmd, ui nested_common_payload_t *payload = (nested_common_payload_t *)data; status = static_nested_recover_key(bytes_to_num(payload->key_known, 6), payload->block_known, payload->type_known, payload->block_target, payload->type_target, &sncs); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } // mf1_static_nested_core_t is PACKED and comprises only bytes so we can use it directly - return data_frame_make(cmd, HF_TAG_OK, sizeof(sncs), (uint8_t *)(&sncs)); + return data_frame_make(cmd, STATUS_HF_TAG_OK, sizeof(sncs), (uint8_t *)(&sncs)); } static data_frame_tx_t *cmd_processor_mf1_darkside_acquire(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -282,13 +282,13 @@ static data_frame_tx_t *cmd_processor_mf1_darkside_acquire(uint16_t cmd, uint16_ DarksideCore_t dc; } PACKED payload; status = darkside_recover_key(data[1], data[0], data[2], data[3], &payload.dc, (mf1_darkside_status_t *)&payload.darkside_status); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } if (payload.darkside_status != DARKSIDE_OK) { - return data_frame_make(cmd, HF_TAG_OK, sizeof(payload.darkside_status), &payload.darkside_status); + return data_frame_make(cmd, STATUS_HF_TAG_OK, sizeof(payload.darkside_status), &payload.darkside_status); } - return data_frame_make(cmd, HF_TAG_OK, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_HF_TAG_OK, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_mf1_detect_nt_dist(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -309,11 +309,11 @@ static data_frame_tx_t *cmd_processor_mf1_detect_nt_dist(uint16_t cmd, uint16_t payload_t *payload = (payload_t *)data; uint32_t distance; status = nested_distance_detect(payload->block_known, payload->type_known, payload->key_known, payload_resp.uid, &distance); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } payload_resp.distance = U32HTONL(distance); - return data_frame_make(cmd, HF_TAG_OK, sizeof(payload_resp), (uint8_t *)&payload_resp); + return data_frame_make(cmd, STATUS_HF_TAG_OK, sizeof(payload_resp), (uint8_t *)&payload_resp); } static data_frame_tx_t *cmd_processor_mf1_nested_acquire(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -324,11 +324,11 @@ static data_frame_tx_t *cmd_processor_mf1_nested_acquire(uint16_t cmd, uint16_t nested_common_payload_t *payload = (nested_common_payload_t *)data; status = nested_recover_key(bytes_to_num(payload->key_known, 6), payload->block_known, payload->type_known, payload->block_target, payload->type_target, ncs); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } // mf1_nested_core_t is PACKED and comprises only bytes so we can use it directly - return data_frame_make(cmd, HF_TAG_OK, sizeof(ncs), (uint8_t *)(&ncs)); + return data_frame_make(cmd, STATUS_HF_TAG_OK, sizeof(ncs), (uint8_t *)(&ncs)); } static data_frame_tx_t *cmd_processor_mf1_auth_one_key_block(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -360,11 +360,11 @@ static data_frame_tx_t *cmd_processor_mf1_read_one_block(uint16_t cmd, uint16_t payload_t *payload = (payload_t *)data; uint8_t block[16] = { 0x00 }; status = auth_key_use_522_hw(payload->block, payload->type, payload->key); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } status = pcd_14a_reader_mf1_read(payload->block, block); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } return data_frame_make(cmd, status, sizeof(block), block); @@ -383,7 +383,7 @@ static data_frame_tx_t *cmd_processor_mf1_write_one_block(uint16_t cmd, uint16_t payload_t *payload = (payload_t *)data; status = auth_key_use_522_hw(payload->block, payload->type, payload->key); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } status = pcd_14a_reader_mf1_write(payload->block, payload->block_data); @@ -450,10 +450,10 @@ static data_frame_tx_t *cmd_processor_hf14a_raw(uint16_t cmd, uint16_t status, u static data_frame_tx_t *cmd_processor_em410x_scan(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t id_buffer[5] = { 0x00 }; status = PcdScanEM410X(id_buffer); - if (status != LF_TAG_OK) { + if (status != STATUS_LF_TAG_OK) { return data_frame_make(cmd, status, 0, NULL); } - return data_frame_make(cmd, LF_TAG_OK, sizeof(id_buffer), id_buffer); + return data_frame_make(cmd, STATUS_LF_TAG_OK, sizeof(id_buffer), id_buffer); } static data_frame_tx_t *cmd_processor_em410x_write_to_t55XX(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -479,7 +479,7 @@ static data_frame_tx_t *cmd_processor_set_active_slot(uint16_t cmd, uint16_t sta return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } change_slot_auto(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_set_slot_tag_type(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -497,7 +497,7 @@ static data_frame_tx_t *cmd_processor_set_slot_tag_type(uint16_t cmd, uint16_t s return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } tag_emulation_change_type(payload->num_slot, tag_type); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_delete_slot_sense_type(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -513,7 +513,7 @@ static data_frame_tx_t *cmd_processor_delete_slot_sense_type(uint16_t cmd, uint1 } tag_emulation_delete_data(payload->num_slot, payload->sense_type); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_set_slot_data_default(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -530,7 +530,7 @@ static data_frame_tx_t *cmd_processor_set_slot_data_default(uint16_t cmd, uint16 if (payload->num_slot >= TAG_MAX_SLOT_NUM || !is_tag_specific_type_valid(tag_type)) { return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } - status = tag_emulation_factory_data(payload->num_slot, tag_type) ? STATUS_DEVICE_SUCCESS : STATUS_NOT_IMPLEMENTED; + status = tag_emulation_factory_data(payload->num_slot, tag_type) ? STATUS_SUCCESS : STATUS_NOT_IMPLEMENTED; return data_frame_make(cmd, status, 0, NULL); } @@ -562,17 +562,17 @@ static data_frame_tx_t *cmd_processor_set_slot_enable(uint16_t cmd, uint16_t sta change_slot_auto(slot_prev); } } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_slot_data_config_save(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { tag_emulation_save(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_active_slot(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t slot = tag_emulation_get_slot(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &slot); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &slot); } static data_frame_tx_t *cmd_processor_get_slot_info(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -588,12 +588,12 @@ static data_frame_tx_t *cmd_processor_get_slot_info(uint16_t cmd, uint16_t statu payload[slot].lf_tag_type = U16HTONS(tag_types.tag_lf); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_wipe_fds(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { bool success = fds_wipe(); - status = success ? STATUS_DEVICE_SUCCESS : STATUS_FLASH_WRITE_FAIL; + status = success ? STATUS_SUCCESS : STATUS_FLASH_WRITE_FAIL; delayed_reset(50); return data_frame_make(cmd, status, 0, NULL); } @@ -605,7 +605,7 @@ static data_frame_tx_t *cmd_processor_em410x_set_emu_id(uint16_t cmd, uint16_t s tag_data_buffer_t *buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X); memcpy(buffer->buffer, data, LF_EM410X_TAG_ID_SIZE); tag_emulation_load_by_buffer(TAG_TYPE_EM410X, false); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_em410x_get_emu_id(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -617,14 +617,14 @@ static data_frame_tx_t *cmd_processor_em410x_get_emu_id(uint16_t cmd, uint16_t s tag_data_buffer_t *buffer = get_buffer_by_tag_type(TAG_TYPE_EM410X); uint8_t responseData[LF_EM410X_TAG_ID_SIZE]; memcpy(responseData, buffer->buffer, LF_EM410X_TAG_ID_SIZE); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, LF_EM410X_TAG_ID_SIZE, responseData); + return data_frame_make(cmd, STATUS_SUCCESS, LF_EM410X_TAG_ID_SIZE, responseData); } static data_frame_tx_t *cmd_processor_hf14a_get_anti_coll_data(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { tag_slot_specific_type_t tag_types; tag_emulation_get_specific_types_by_slot(tag_emulation_get_slot(), &tag_types); if (tag_types.tag_hf == TAG_TYPE_UNDEFINED) { - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); // no data in slot, don't send garbage + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); // no data in slot, don't send garbage } nfc_tag_14a_coll_res_reference_t *info = get_saved_mifare_coll_res(); // uidlen[1]|uid[uidlen]|atqa[2]|sak[1]|atslen[1]|ats[atslen] @@ -644,7 +644,7 @@ static data_frame_tx_t *cmd_processor_hf14a_get_anti_coll_data(uint16_t cmd, uin } else { payload[offset++] = 0; } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, offset, payload); + return data_frame_make(cmd, STATUS_SUCCESS, offset, payload); } static data_frame_tx_t *cmd_processor_mf1_set_detection_enable(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -653,12 +653,12 @@ static data_frame_tx_t *cmd_processor_mf1_set_detection_enable(uint16_t cmd, uin } nfc_tag_mf1_detection_log_clear(); nfc_tag_mf1_set_detection_enable(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_mf1_get_detection_enable(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t is_enable = nfc_tag_mf1_is_detection_enable(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, (uint8_t *)(&is_enable)); + return data_frame_make(cmd, STATUS_SUCCESS, 1, (uint8_t *)(&is_enable)); } static data_frame_tx_t *cmd_processor_mf1_get_detection_count(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -667,7 +667,7 @@ static data_frame_tx_t *cmd_processor_mf1_get_detection_count(uint16_t cmd, uint count = 0; } uint32_t payload = U32HTONL(count); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(uint32_t), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(uint32_t), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_mf1_get_detection_log(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -685,7 +685,7 @@ static data_frame_tx_t *cmd_processor_mf1_get_detection_log(uint16_t cmd, uint16 } resp = (uint8_t *)(logs + index); length = MIN(count - index, NETDATA_MAX_DATA_LENGTH / sizeof(nfc_tag_mf1_auth_log_t)) * sizeof(nfc_tag_mf1_auth_log_t); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, length, resp); + return data_frame_make(cmd, STATUS_SUCCESS, length, resp); } static data_frame_tx_t *cmd_processor_mf1_write_emu_block_data(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -703,7 +703,7 @@ static data_frame_tx_t *cmd_processor_mf1_write_emu_block_data(uint16_t cmd, uin uint8_t *p_block = &data[i]; memcpy(info->memory[j], p_block, NFC_TAG_MF1_DATA_SIZE); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_mf1_read_emu_block_data(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -719,7 +719,7 @@ static data_frame_tx_t *cmd_processor_mf1_read_emu_block_data(uint16_t cmd, uint for (int i = 0, j = block_index; i < result_length; i += NFC_TAG_MF1_DATA_SIZE, j++) { memcpy(&result_buffer[i], info->memory[j], NFC_TAG_MF1_DATA_SIZE); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, result_length, result_buffer); + return data_frame_make(cmd, STATUS_SUCCESS, result_length, result_buffer); } static data_frame_tx_t *cmd_processor_hf14a_set_anti_coll_data(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -745,7 +745,7 @@ static data_frame_tx_t *cmd_processor_hf14a_set_anti_coll_data(uint16_t cmd, uin offset ++; memcpy(info->ats->data, &data[offset], info->ats->length); offset += info->ats->length; - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_set_slot_tag_nick(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -768,7 +768,7 @@ static data_frame_tx_t *cmd_processor_set_slot_tag_nick(uint16_t cmd, uint16_t s if (!ret) { return data_frame_make(cmd, STATUS_FLASH_WRITE_FAIL, 0, NULL); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_slot_tag_nick(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -789,7 +789,7 @@ static data_frame_tx_t *cmd_processor_get_slot_tag_nick(uint16_t cmd, uint16_t s if (!ret) { return data_frame_make(cmd, STATUS_FLASH_READ_FAIL, 0, NULL); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, buffer[0], &buffer[1]); + return data_frame_make(cmd, STATUS_SUCCESS, buffer[0], &buffer[1]); } static data_frame_tx_t *cmd_processor_delete_slot_tag_nick(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -808,7 +808,7 @@ static data_frame_tx_t *cmd_processor_delete_slot_tag_nick(uint16_t cmd, uint16_ if (!ret) { return data_frame_make(cmd, STATUS_FLASH_WRITE_FAIL, 0, NULL); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_mf1_get_emulator_config(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -818,12 +818,12 @@ static data_frame_tx_t *cmd_processor_mf1_get_emulator_config(uint16_t cmd, uint mf1_info[2] = nfc_tag_mf1_is_gen2_magic_mode(); mf1_info[3] = nfc_tag_mf1_is_use_mf1_coll_res(); mf1_info[4] = nfc_tag_mf1_get_write_mode(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 5, mf1_info); + return data_frame_make(cmd, STATUS_SUCCESS, 5, mf1_info); } static data_frame_tx_t *cmd_processor_mf1_get_gen1a_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t mode = nfc_tag_mf1_is_gen1a_magic_mode(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &mode); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &mode); } static data_frame_tx_t *cmd_processor_mf1_set_gen1a_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -831,12 +831,12 @@ static data_frame_tx_t *cmd_processor_mf1_set_gen1a_mode(uint16_t cmd, uint16_t return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } nfc_tag_mf1_set_gen1a_magic_mode(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_mf1_get_gen2_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t mode = nfc_tag_mf1_is_gen2_magic_mode(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &mode); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &mode); } static data_frame_tx_t *cmd_processor_mf1_set_gen2_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -844,12 +844,12 @@ static data_frame_tx_t *cmd_processor_mf1_set_gen2_mode(uint16_t cmd, uint16_t s return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } nfc_tag_mf1_set_gen2_magic_mode(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_mf1_get_block_anti_coll_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t mode = nfc_tag_mf1_is_use_mf1_coll_res(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &mode); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &mode); } static data_frame_tx_t *cmd_processor_mf1_set_block_anti_coll_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -857,12 +857,12 @@ static data_frame_tx_t *cmd_processor_mf1_set_block_anti_coll_mode(uint16_t cmd, return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } nfc_tag_mf1_set_use_mf1_coll_res(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_mf1_get_write_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { uint8_t mode = nfc_tag_mf1_get_write_mode(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 1, &mode); + return data_frame_make(cmd, STATUS_SUCCESS, 1, &mode); } static data_frame_tx_t *cmd_processor_mf1_set_write_mode(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -870,7 +870,7 @@ static data_frame_tx_t *cmd_processor_mf1_set_write_mode(uint16_t cmd, uint16_t return data_frame_make(cmd, STATUS_PAR_ERR, 0, NULL); } nfc_tag_mf1_set_write_mode(data[0]); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_get_enabled_slots(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -882,11 +882,11 @@ static data_frame_tx_t *cmd_processor_get_enabled_slots(uint16_t cmd, uint16_t s payload[slot].enabled_hf = tag_emulation_slot_is_enabled(slot, TAG_SENSE_HF); payload[slot].enabled_lf = tag_emulation_slot_is_enabled(slot, TAG_SENSE_LF); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, sizeof(payload), (uint8_t *)&payload); + return data_frame_make(cmd, STATUS_SUCCESS, sizeof(payload), (uint8_t *)&payload); } static data_frame_tx_t *cmd_processor_get_ble_connect_key(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, BLE_PAIRING_KEY_LEN, settings_get_ble_connect_key()); + return data_frame_make(cmd, STATUS_SUCCESS, BLE_PAIRING_KEY_LEN, settings_get_ble_connect_key()); } static data_frame_tx_t *cmd_processor_set_ble_connect_key(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { @@ -906,13 +906,13 @@ static data_frame_tx_t *cmd_processor_set_ble_connect_key(uint16_t cmd, uint16_t } // Key is valid, we can update to config settings_set_ble_connect_key(data); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } static data_frame_tx_t *cmd_processor_delete_all_ble_bonds(uint16_t cmd, uint16_t status, uint16_t length, uint8_t *data) { advertising_stop(); delete_bonds_all(); - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, 0, NULL); + return data_frame_make(cmd, STATUS_SUCCESS, 0, NULL); } #if defined(PROJECT_CHAMELEON_ULTRA) @@ -1053,7 +1053,7 @@ data_frame_tx_t *cmd_processor_get_device_capabilities(uint16_t cmd, uint16_t st commands[i] = U16HTONS(m_data_cmd_map[i].cmd); } - return data_frame_make(cmd, STATUS_DEVICE_SUCCESS, count * sizeof(uint16_t), (uint8_t *)commands); + return data_frame_make(cmd, STATUS_SUCCESS, count * sizeof(uint16_t), (uint8_t *)commands); } /** diff --git a/firmware/application/src/app_main.c b/firmware/application/src/app_main.c index 8baca7b6..011f4ba7 100644 --- a/firmware/application/src/app_main.c +++ b/firmware/application/src/app_main.c @@ -620,7 +620,7 @@ static void btn_fn_copy_ic_uid(void) { case TAG_TYPE_EM410X: status = PcdScanEM410X(id_buffer); - if (status == LF_TAG_OK) { + if (status == 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); @@ -680,7 +680,7 @@ static void btn_fn_copy_ic_uid(void) { status = pcd_14a_reader_scan_auto(&tag); pcd_14a_reader_antenna_off(); - if (status == HF_TAG_OK) { + if (status == STATUS_HF_TAG_OK) { // copy uid antres->size = tag.uid_len; memcpy(antres->uid, tag.uid, tag.uid_len); diff --git a/firmware/application/src/app_status.h b/firmware/application/src/app_status.h index 574768df..b0c05f64 100644 --- a/firmware/application/src/app_status.h +++ b/firmware/application/src/app_status.h @@ -5,21 +5,21 @@ ///////////////////////////////////////////////////////////////////// // 14a status ///////////////////////////////////////////////////////////////////// -#define HF_TAG_OK (0x00) // IC card operation successful -#define HF_TAG_NO (0x01) // No IC card found -#define HF_ERR_STAT (0x02) // IC Card communication error -#define HF_ERR_CRC (0x03) // IC Card communication verification error -#define HF_COLLISION (0x04) // IC card conflict -#define HF_ERR_BCC (0x05) // IC card BCC error -#define MF_ERR_AUTH (0x06) // MF card verification failed -#define HF_ERR_PARITY (0x07) // IC card parity error -#define HF_ERR_ATS (0x08) // ATS should be present but card NAKed +#define STATUS_HF_TAG_OK (0x00) // IC card operation successful +#define STATUS_HF_TAG_NO (0x01) // No IC card found +#define STATUS_HF_ERR_STAT (0x02) // IC Card communication error +#define STATUS_HF_ERR_CRC (0x03) // IC Card communication verification error +#define STATUS_HF_COLLISION (0x04) // IC card conflict +#define STATUS_HF_ERR_BCC (0x05) // IC card BCC error +#define STATUS_MF_ERR_AUTH (0x06) // MF card verification failed +#define STATUS_HF_ERR_PARITY (0x07) // IC card parity error +#define STATUS_HF_ERR_ATS (0x08) // ATS should be present but card NAKed ///////////////////////////////////////////////////////////////////// // lf status ///////////////////////////////////////////////////////////////////// -#define LF_TAG_OK (0x40) // Some of the low -frequency cards are successful! -#define EM410X_TAG_NO_FOUND (0x41) // Can't search for valid EM410X tags +#define STATUS_LF_TAG_OK (0x40) // Some of the low -frequency cards are successful! +#define STATUS_EM410X_TAG_NO_FOUND (0x41) // Can't search for valid EM410X tags ///////////////////////////////////////////////////////////////////// @@ -28,7 +28,7 @@ #define STATUS_PAR_ERR (0x60) // The parameter errors transferred by the BLE instruction, or call the parameter error transmitted by certain functions #define STATUS_DEVICE_MODE_ERROR (0x66) // The mode of the current device is wrong, and the corresponding API cannot be called #define STATUS_INVALID_CMD (0x67) // Invalid instruction -#define STATUS_DEVICE_SUCCESS (0x68) // Device -related operations successfully executed +#define STATUS_SUCCESS (0x68) // Device -related operations successfully executed #define STATUS_NOT_IMPLEMENTED (0x69) // Calling some unrealized operations, which belongs to the missed error of the developer #define STATUS_FLASH_WRITE_FAIL (0x70) // Flash writing failed #define STATUS_FLASH_READ_FAIL (0x71) // Flash read failed diff --git a/firmware/application/src/rfid/reader/hf/mf1_toolbox.c b/firmware/application/src/rfid/reader/hf/mf1_toolbox.c index d6820721..f82b7425 100644 --- a/firmware/application/src/rfid/reader/hf/mf1_toolbox.c +++ b/firmware/application/src/rfid/reader/hf/mf1_toolbox.c @@ -138,7 +138,7 @@ static uint8_t send_cmd(struct Crypto1State *pcs, uint8_t encrypted, uint8_t cmd } // There is a problem with communication, do not continue the next task - if (*status != HF_TAG_OK) { + if (*status != STATUS_HF_TAG_OK) { return len; } @@ -184,7 +184,7 @@ int authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyT len = send_cmd(pcs, isNested, keyType, blockNo, &status, answer, parity, U8ARR_BIT_LEN(answer)); if (len != 32) { NRF_LOG_INFO("No 32 data recv on send_cmd: %d\r\n", len); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // Save the tag nonce (nt) @@ -232,15 +232,15 @@ int authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyT ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0, 0); if (ntpp == BYTES4_TO_U32(answer)) { // Successful verification! - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } else { // fail - return MF_ERR_AUTH; + return STATUS_MF_ERR_AUTH; } } // fail! - return MF_ERR_AUTH; + return STATUS_MF_ERR_AUTH; } /** @@ -279,15 +279,15 @@ static uint8_t darkside_select_nonces(picc_14a_tag_t *tag, uint8_t block, uint8_ // 2. Moderate power -off time, don't be too long, it will affect efficiency, and don't be too short. reset_radio_field_with_delay(); // After the power is completely disconnected, we will select the card quickly and compress the verification time as much as possible. - if (pcd_14a_reader_fast_select(tag) != HF_TAG_OK) { + if (pcd_14a_reader_fast_select(tag) != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Tag can't select!\n"); - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, tag_auth, 4, tag_resp, &len, U8ARR_BIT_LEN(tag_resp)); // After finding the card, start collecting random numbers - if (status != HF_TAG_OK || len != 32) { + if (status != STATUS_HF_TAG_OK || len != 32) { NRF_LOG_INFO("Get nt failed.\n"); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // Converted to the type of U32 and cache nt_list[i] = bytes_to_num(tag_resp, 4); @@ -321,14 +321,14 @@ static uint8_t darkside_select_nonces(picc_14a_tag_t *tag, uint8_t block, uint8_ if (max == 0) { NRF_LOG_INFO("Can't sync nt.\n"); *darkside_status = DARKSIDE_CANT_FIX_NT; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // NT is fixed successfully, the one with the highest number of times we take out // NRF_LOG_INFO("Sync nt: %"PRIu32", max = %d\n", nt_list[m], max); if (nt) *nt = nt_list[m]; // Only when the caller needs to get NT *darkside_status = DARKSIDE_OK; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -374,10 +374,10 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, bool led_toggle = false; // We need to confirm the use of a certain card first - if (pcd_14a_reader_scan_auto(p_tag_info) == HF_TAG_OK) { + if (pcd_14a_reader_scan_auto(p_tag_info) == STATUS_HF_TAG_OK) { uid_cur = get_u32_tag_uid(p_tag_info); } else { - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } // Verification instructions need to add CRC16 @@ -401,7 +401,7 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, // Then you need to fix a random number that may appear status = darkside_select_nonces(p_tag_info, targetBlk, targetTyp, &nt_ori, darkside_status); - if ((status != HF_TAG_OK) || (*darkside_status != DARKSIDE_OK)) { + if ((status != STATUS_HF_TAG_OK) || (*darkside_status != DARKSIDE_OK)) { //The fixed random number failed, and the next step cannot be performed return status; } @@ -414,7 +414,7 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, if (uid_ori != uid_cur) { *darkside_status = DARKSIDE_TAG_CHANGED; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } } // Always collect different NACK under a large cycle @@ -437,17 +437,17 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, reset_radio_field_with_delay(); //After the power is completely disconnected, we will select the card quickly and compress the verification time as much as possible. - if (pcd_14a_reader_fast_select(p_tag_info) != HF_TAG_OK) { + if (pcd_14a_reader_fast_select(p_tag_info) != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Tag can't select!\n"); - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, tag_auth, 4, dat_recv, &len, U8ARR_BIT_LEN(dat_recv)); // After finding the card, start collecting random numbers - if (status != HF_TAG_OK || len != 32) { + if (status != STATUS_HF_TAG_OK || len != 32) { NRF_LOG_INFO("Get nt failed.\n"); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } //The byte array of the conversion response is 10 in NT @@ -462,7 +462,7 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, if (++resync_count == ntSyncMax) { NRF_LOG_INFO("Can't fix nonce."); *darkside_status = DARKSIDE_CANT_FIX_NT; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // When the clock is not synchronized, the following operation is meaningless @@ -500,7 +500,7 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, // however we dont feed key w uid it the prng.. NRF_LOG_INFO("Auth Ok, you are so lucky!\n"); *darkside_status = DARKSIDE_LUCKY_AUTH_OK; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // Receive answer. This will be a 4 Bit NACK when the 8 parity bits are OK after decoding @@ -529,7 +529,7 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, if (par == 0) { // tried all 256 possible parities without success. Card doesn't send NACK. NRF_LOG_INFO("Card doesn't send NACK.\r\n"); *darkside_status = DARKSIDE_NO_NAK_SENT; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } } else { par = ((par + 1) & 0x1F) | par_low; @@ -555,7 +555,7 @@ uint8_t darkside_recover_key(uint8_t targetBlk, uint8_t targetTyp, // NRF_LOG_INFO("Darkside done!\n"); *darkside_status = DARKSIDE_OK; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -587,19 +587,19 @@ uint8_t check_tag_response_nt(picc_14a_tag_t *tag, uint32_t *nt) { pcd_14a_reader_halt_tag(); // We will choose a fast card, and we will be compressed to verify as much as possible - if (pcd_14a_reader_fast_select(tag) != HF_TAG_OK) { + if (pcd_14a_reader_fast_select(tag) != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Tag can't select\r\n"); - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } // Send instructions and get NT return *nt = send_cmd(pcs, AUTH_FIRST, PICC_AUTHENT1A, 0x03, &status, dat_recv, par_recv, U8ARR_BIT_LEN(dat_recv)); if (*nt != 32) { // NRF_LOG_INFO("No 32 data recv on send_cmd: %d\n", *nt); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } *nt = bytes_to_num(dat_recv, 4); - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -614,8 +614,8 @@ uint8_t check_std_mifare_nt_support(void) { uint32_t nt1 = 0; // Find card, search on the field - if (pcd_14a_reader_scan_auto(p_tag_info) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_scan_auto(p_tag_info) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } // Get NT and return status @@ -633,13 +633,13 @@ uint8_t check_static_prng(bool *is_static) { uint8_t status; // Find card, search on the field - if (pcd_14a_reader_scan_auto(p_tag_info) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_scan_auto(p_tag_info) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } // Get NT in the first wave status = check_tag_response_nt(p_tag_info, &nt1); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } @@ -650,13 +650,13 @@ uint8_t check_static_prng(bool *is_static) { // Get NT in the second wave status = check_tag_response_nt(p_tag_info, &nt2); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } // Detect whether the random number is static *is_static = (nt1 == nt2); - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -673,12 +673,12 @@ uint8_t check_prng_type(mf1_prng_type_t *prng_type) { // If the judgment process is found, it is found that the StaticNested detection cannot be completed // Then return the state directly, no need to perform the following judgment logic. - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } if (is_static) { *prng_type = PRNG_STATIC; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // Non -Static card, you can continue to run down logic @@ -689,13 +689,13 @@ uint8_t check_prng_type(mf1_prng_type_t *prng_type) { pcd_14a_reader_halt_tag(); // Card search operation - if (pcd_14a_reader_scan_auto(p_tag_info) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_scan_auto(p_tag_info) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } //Get NT, just get it once status = check_tag_response_nt(p_tag_info, &nt1); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } @@ -712,7 +712,7 @@ uint8_t check_prng_type(mf1_prng_type_t *prng_type) { // ------------------------------------ // end - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -785,19 +785,19 @@ static uint8_t measure_distance(uint64_t u64Key, uint8_t block, uint8_t type, ui // Reset card communication pcd_14a_reader_halt_tag(); // We will choose a fast card, and we will be compressed to verify as much as possible - if (pcd_14a_reader_fast_select(p_tag_info) != HF_TAG_OK) { + if (pcd_14a_reader_fast_select(p_tag_info) != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Tag can't select\r\n"); - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } // Perform the first verification in order to obtain the unblocked NT1 - if (authex(pcs, uid, block, type, u64Key, AUTH_FIRST, &nt1) != HF_TAG_OK) { + if (authex(pcs, uid, block, type, u64Key, AUTH_FIRST, &nt1) != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Auth failed 1\r\n"); - return MF_ERR_AUTH; + return STATUS_MF_ERR_AUTH; } // Met the nested verification to obtain the encrypted NT2_ENC - if (authex(pcs, uid, block, type, u64Key, AUTH_NESTED, &nt2) != HF_TAG_OK) { + if (authex(pcs, uid, block, type, u64Key, AUTH_NESTED, &nt2) != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Auth failed 2\r\n"); - return MF_ERR_AUTH; + return STATUS_MF_ERR_AUTH; } // Determine whether the two random numbers are the same, under normal circumstances, // We can't bring the same random number, because PRNG is updating chip at any time @@ -805,7 +805,7 @@ static uint8_t measure_distance(uint64_t u64Key, uint8_t block, uint8_t type, ui if (nt1 == nt2) { NRF_LOG_INFO("StaticNested: %08x vs %08x\n", nt1, nt2); *distance = 0; - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // After the measurement is completed, store in the buffer distances[index++] = measure_nonces(nt1, nt2); @@ -815,7 +815,7 @@ static uint8_t measure_distance(uint64_t u64Key, uint8_t block, uint8_t type, ui //The final calculation of the distance between the two NTs and spread it directly *distance = measure_median(distances, DIST_NR); // You need to return the OK value to successfully log in - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -841,16 +841,16 @@ static uint8_t nested_recover_core(mf1_nested_core_t *pnc, uint64_t keyKnown, ui // Reset card communication pcd_14a_reader_halt_tag(); // Quickly select the card to complete the verification steps to collect NT1 and NT2_ENC - if (pcd_14a_reader_scan_auto(p_tag_info) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_scan_auto(p_tag_info) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } //The first step verification, basic verification does not require nested and encrypted - if (authex(pcs, uid, blkKnown, typKnown, keyKnown, AUTH_FIRST, &nt1) != HF_TAG_OK) { - return MF_ERR_AUTH; + if (authex(pcs, uid, blkKnown, typKnown, keyKnown, AUTH_FIRST, &nt1) != STATUS_HF_TAG_OK) { + return STATUS_MF_ERR_AUTH; } // Then there is nested verification if (send_cmd(pcs, AUTH_NESTED, targetType, targetBlock, &status, answer, parity, U8ARR_BIT_LEN(answer)) != 32) { - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; }; // The first verified explicitly random number num_to_bytes(nt1, 4, pnc->nt1); @@ -861,7 +861,7 @@ static uint8_t nested_recover_core(mf1_nested_core_t *pnc, uint64_t keyKnown, ui pnc->par |= ((oddparity8(answer[0]) != parity[0]) << 0); pnc->par |= ((oddparity8(answer[1]) != parity[1]) << 1); pnc->par |= ((oddparity8(answer[2]) != parity[2]) << 2); - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -872,14 +872,14 @@ static uint8_t nested_recover_core(mf1_nested_core_t *pnc, uint64_t keyKnown, ui * @param :targetBlock : The target sector that requires a Nested attack * @param :targetType : The target key type requires the Nested attack * @param :ncs : Nested core structure array, save related communication data -* @retval :The attack success return HF_TAG_OK, else return the error code +* @retval :The attack success return STATUS_HF_TAG_OK, else return the error code * */ uint8_t nested_recover_key(uint64_t keyKnown, uint8_t blkKnown, uint8_t typKnown, uint8_t targetBlock, uint8_t targetType, mf1_nested_core_t ncs[SETS_NR]) { uint8_t m, res; // all operations must be based on the card res = pcd_14a_reader_scan_auto(p_tag_info); - if (res != HF_TAG_OK) { + if (res != STATUS_HF_TAG_OK) { return res; } //Then collect the specified number of random array @@ -892,11 +892,11 @@ uint8_t nested_recover_key(uint64_t keyKnown, uint8_t blkKnown, uint8_t typKnown targetBlock, targetType ); - if (res != HF_TAG_OK) { + if (res != STATUS_HF_TAG_OK) { return res; } } - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -909,11 +909,11 @@ uint8_t nested_recover_key(uint64_t keyKnown, uint8_t blkKnown, uint8_t typKnown * */ uint8_t nested_distance_detect(uint8_t block, uint8_t type, uint8_t *key, uint8_t *uid, uint32_t *distance) { - uint8_t status = HF_TAG_OK; + uint8_t status = STATUS_HF_TAG_OK; *distance = 0; //Must ensure that there is a card on the court status = pcd_14a_reader_scan_auto(p_tag_info); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } else { // At least the card exists, you can copy the UID to the buffer first @@ -934,7 +934,7 @@ uint8_t nested_distance_detect(uint8_t block, uint8_t type, uint8_t *key, uint8_ * @param :targetBlock : Target sectors that require nested attacks * @param :targetType : Target key types that require nested attacks * @param :nestedAgain : StaticNested enhanced vulnerability, which can obtain two sets of encrypted random numbers based on nested verification of known keys -* @retval : Successfully collected and returned to HF_TAG_OK, otherwise an error code will be returned. +* @retval : Successfully collected and returned to STATUS_HF_TAG_OK, otherwise an error code will be returned. * */ uint8_t static_nested_recover_core(uint8_t *p_nt1, uint8_t *p_nt2, uint64_t keyKnown, uint8_t blkKnown, uint8_t typKnown, uint8_t targetBlock, uint8_t targetType, uint8_t nestedAgain) { @@ -946,28 +946,28 @@ uint8_t static_nested_recover_core(uint8_t *p_nt1, uint8_t *p_nt2, uint64_t keyK uint32_t uid, nt1, nt2; uid = get_u32_tag_uid(p_tag_info); pcd_14a_reader_halt_tag(); - if (pcd_14a_reader_fast_select(p_tag_info) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_fast_select(p_tag_info) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } status = authex(pcs, uid, blkKnown, typKnown, keyKnown, AUTH_FIRST, &nt1); - if (status != HF_TAG_OK) { - return MF_ERR_AUTH; + if (status != STATUS_HF_TAG_OK) { + return STATUS_MF_ERR_AUTH; } if (nestedAgain) { status = authex(pcs, uid, blkKnown, typKnown, keyKnown, AUTH_NESTED, NULL); - if (status != HF_TAG_OK) { - return MF_ERR_AUTH; + if (status != STATUS_HF_TAG_OK) { + return STATUS_MF_ERR_AUTH; } } len = send_cmd(pcs, AUTH_NESTED, targetType, targetBlock, &status, answer, parity, U8ARR_BIT_LEN(answer)); if (len != 32) { NRF_LOG_INFO("No 32 data recv on sendcmd: %d\r\n", len); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } nt2 = bytes_to_num(answer, 4); num_to_bytes(nt1, 4, p_nt1); num_to_bytes(nt2, 4, p_nt2); - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -979,25 +979,25 @@ uint8_t static_nested_recover_core(uint8_t *p_nt1, uint8_t *p_nt2, uint64_t keyK * @param :targetBlock : Target sectors that require nested attacks * @param :targetType : Target key type that require nested attacks * @param :sncs : StaticNested Decrypting Core Structure Array -* @retval : Successfully collected and returned to HF_TAG_OK, otherwise an error code will be returned. +* @retval : Successfully collected and returned to STATUS_HF_TAG_OK, otherwise an error code will be returned. * */ uint8_t static_nested_recover_key(uint64_t keyKnown, uint8_t blkKnown, uint8_t typKnown, uint8_t targetBlock, uint8_t targetType, mf1_static_nested_core_t *sncs) { uint8_t res; res = pcd_14a_reader_scan_auto(p_tag_info); - if (res != HF_TAG_OK) { + if (res != STATUS_HF_TAG_OK) { return res; } get_4byte_tag_uid(p_tag_info, sncs->uid); res = static_nested_recover_core(sncs->core[0].nt1, sncs->core[0].nt2, keyKnown, blkKnown, typKnown, targetBlock, targetType, false); - if (res != HF_TAG_OK) { + if (res != STATUS_HF_TAG_OK) { return res; } res = static_nested_recover_core(sncs->core[1].nt1, sncs->core[1].nt2, keyKnown, blkKnown, typKnown, targetBlock, targetType, true); - if (res != HF_TAG_OK) { + if (res != STATUS_HF_TAG_OK) { return res; } - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -1007,8 +1007,8 @@ uint8_t static_nested_recover_key(uint64_t keyKnown, uint8_t blkKnown, uint8_t t */ uint8_t auth_key_use_522_hw(uint8_t block, uint8_t type, uint8_t *key) { // Each verification of a block must re -find a card - if (pcd_14a_reader_scan_auto(p_tag_info) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_scan_auto(p_tag_info) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } // After finding the card, we start to verify! return pcd_14a_reader_mf1_auth(p_tag_info, type, block, key); diff --git a/firmware/application/src/rfid/reader/hf/rc522.c b/firmware/application/src/rfid/reader/hf/rc522.c index 6a6bf49f..9b2d1117 100644 --- a/firmware/application/src/rfid/reader/hf/rc522.c +++ b/firmware/application/src/rfid/reader/hf/rc522.c @@ -257,7 +257,7 @@ uint16_t pcd_14a_reader_timeout_get() { * @retval : Status value mi_ok, successful */ uint8_t pcd_14a_reader_bytes_transfer(uint8_t Command, uint8_t *pIn, uint8_t InLenByte, uint8_t *pOut, uint16_t *pOutLenBit, uint16_t maxOutLenBit) { - uint8_t status = HF_ERR_STAT; + uint8_t status = STATUS_HF_ERR_STAT; uint8_t waitFor = 0x00; uint8_t lastBits = 0; uint8_t n = 0; @@ -290,7 +290,7 @@ uint8_t pcd_14a_reader_bytes_transfer(uint8_t Command, uint8_t *pIn, uint8_t In if (pOut == NULL) { // If the developer does not need to receive data, then return directly after the sending! while ((read_register_single(Status2Reg) & 0x07) == 0x03); - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } bsp_set_timer(g_timeout_auto_timer, 0); // Before starting the operation, return to zero over time counting @@ -316,24 +316,24 @@ uint8_t pcd_14a_reader_bytes_transfer(uint8_t Command, uint8_t *pIn, uint8_t In if (pcd_err_val & 0x01) { // ProtocolErr Error only appears in the following two cases: if (Command == PCD_AUTHENT) { // During the execution of the MFAUTHENT command, if the number of bytes received by a data stream, the position of the place // Therefore, we need to deal with it well, assuming that there are problems during the verification process, then we need to think that this is normal - status = MF_ERR_AUTH; + status = STATUS_MF_ERR_AUTH; } else { // If the SOF is wrong, the position is set up and the receiver is automatically cleared during the start -up stage, which is effective at the rate of 106kbd NRF_LOG_INFO("Protocol error\n"); - status = HF_ERR_STAT; + status = STATUS_HF_ERR_STAT; } } else if (pcd_err_val & 0x02) { // Detecting whether there are even strange errors NRF_LOG_INFO("Parity error\n"); - status = HF_ERR_PARITY; + status = STATUS_HF_ERR_PARITY; } else if (pcd_err_val & 0x04) { // Detect whether there are CRC errors NRF_LOG_INFO("CRC error\n"); - status = HF_ERR_CRC; + status = STATUS_HF_ERR_CRC; } else if (pcd_err_val & 0x08) { // There is a conflict to detect the label NRF_LOG_INFO("Collision tag\n"); - status = HF_COLLISION; + status = STATUS_HF_COLLISION; } else { // There are other unrepaired abnormalities NRF_LOG_INFO("HF error: 0x%0x2\n", pcd_err_val); - status = HF_ERR_STAT; + status = STATUS_HF_ERR_STAT; } } else { // Occasionally occur @@ -351,25 +351,25 @@ uint8_t pcd_14a_reader_bytes_transfer(uint8_t Command, uint8_t *pIn, uint8_t In // Read all the data in FIFO read_register_buffer(FIFODataReg, pOut, n); // Transmission instructions can be considered success when reading normal data! - status = HF_TAG_OK; + status = STATUS_HF_TAG_OK; } else { NRF_LOG_INFO("pcd_14a_reader_bytes_transfer receive response overflow: %d, max = %d\n", *pOutLenBit, maxOutLenBit); // We can't pass the problem with problems, which is meaningless for the time being *pOutLenBit = 0; // Since there is a problem with the data, let's notify the upper layer and inform me - status = HF_ERR_STAT; + status = STATUS_HF_ERR_STAT; } } else { // Non -transmitted instructions, the execution is completed without errors and considered success! - status = HF_TAG_OK; + status = STATUS_HF_TAG_OK; } } } else { - status = HF_TAG_NO; + status = STATUS_HF_TAG_NO; // NRF_LOG_INFO("Tag lost(timeout).\n"); } - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { // If there are certain operations, // We may need to remove MFCrypto1On This register logo, // Because it may be because of the error encryption communication caused by verification @@ -445,7 +445,7 @@ uint8_t pcd_14a_reader_bits_transfer(uint8_t *pTx, uint16_t szTxBits, uint8_t * clear_register_mask(MfRxReg, 0x10); // Enable Qiqi school inspection // Simply judge the length of data transmission - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { // NRF_LOG_INFO("pcd_14a_reader_bytes_transfer error status: %d\n", status); return status; } @@ -465,7 +465,7 @@ uint8_t pcd_14a_reader_bits_transfer(uint8_t *pTx, uint16_t szTxBits, uint8_t * NRF_LOG_INFO("pcd_14a_reader_bits_transfer decode parity data overflow: %d, max = %d\n", *pRxLenBit, szRxLenBitMax); // There must be an overflow here, and the length of the data that is valid is reset to avoid misjudgment from external calls. *pRxLenBit = 0; - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // The process of the separation and dissection process of the unprecedented verification and the data @@ -479,25 +479,25 @@ uint8_t pcd_14a_reader_bits_transfer(uint8_t *pTx, uint16_t szTxBits, uint8_t * pRxPar[i - 1] = (buffer[i] & (1 << (i - 1))) >> (i - 1); } } - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** * @brief : ISO14443-A Fast Select * @param :tag:tag info buffer -* @retval :if return HF_TAG_OK,the tag is selected. +* @retval :if return STATUS_HF_TAG_OK,the tag is selected. */ uint8_t pcd_14a_reader_fast_select(picc_14a_tag_t *tag) { uint8_t resp[5] = {0}; // theoretically. A usual RATS will be much smaller uint8_t uid_resp[4] = {0}; uint8_t sak = 0x04; // cascade uid - uint8_t status = HF_TAG_OK; + uint8_t status = STATUS_HF_TAG_OK; uint8_t cascade_level = 0; uint16_t len; // Wakeup - if (pcd_14a_reader_atqa_request(resp, NULL, U8ARR_BIT_LEN(resp)) != HF_TAG_OK) { - return HF_TAG_NO; + if (pcd_14a_reader_atqa_request(resp, NULL, U8ARR_BIT_LEN(resp)) != STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_NO; } // OK we will select at least at cascade 1, lets see if first byte of UID was 0x88 in @@ -523,9 +523,9 @@ uint8_t pcd_14a_reader_fast_select(picc_14a_tag_t *tag) { crc_14a_append(sel_uid, 7); // calculate and add CRC status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, sel_uid, sizeof(sel_uid), resp, &len, U8ARR_BIT_LEN(resp)); // Receive the SAK - if (status != HF_TAG_OK || !len) { + if (status != STATUS_HF_TAG_OK || !len) { // printf("SAK Err: %d, %d\r\n", status, recv_len); - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } sak = resp[0]; @@ -539,7 +539,7 @@ uint8_t pcd_14a_reader_fast_select(picc_14a_tag_t *tag) { uid_resp[2] = uid_resp[3]; } } - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -558,9 +558,9 @@ uint8_t pcd_14a_reader_scan_once(picc_14a_tag_t *tag) { } // wake - if (pcd_14a_reader_atqa_request(tag->atqa, NULL, U8ARR_BIT_LEN(tag->atqa)) != HF_TAG_OK) { - // NRF_LOG_INFO("pcd_14a_reader_atqa_request HF_TAG_NO\r\n"); - return HF_TAG_NO; + if (pcd_14a_reader_atqa_request(tag->atqa, NULL, U8ARR_BIT_LEN(tag->atqa)) != STATUS_HF_TAG_OK) { + // NRF_LOG_INFO("pcd_14a_reader_atqa_request STATUS_HF_TAG_NO\r\n"); + return STATUS_HF_TAG_NO; } uint8_t resp[DEF_FIFO_LENGTH] = {0}; // theoretically. A usual RATS will be much smaller @@ -585,7 +585,7 @@ uint8_t pcd_14a_reader_scan_once(picc_14a_tag_t *tag) { status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, sel_all, sizeof(sel_all), resp, &len, U8ARR_BIT_LEN(resp)); // There is a label collision, we need to solve the collision - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { // The collision still has to be collided. Do n't have this during the decryption process. // So do not solve the collision for the time being, but directly inform the user that the user guarantees that there is only one card in the field NRF_LOG_INFO("Err at tag collision.\n"); @@ -607,16 +607,16 @@ uint8_t pcd_14a_reader_scan_once(picc_14a_tag_t *tag) { uint8_t bcc = sel_uid[2] ^ sel_uid[3] ^ sel_uid[4] ^ sel_uid[5]; // calculate BCC if (sel_uid[6] != bcc) { NRF_LOG_INFO("BCC%d incorrect, got 0x%02x, expected 0x%02x\n", cascade_level, sel_uid[6], bcc); - return HF_ERR_BCC; + return STATUS_HF_ERR_BCC; } crc_14a_append(sel_uid, 7); // calculate and add CRC // send 9x 70 Choose a card status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, sel_uid, sizeof(sel_uid), resp, &len, U8ARR_BIT_LEN(resp)); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Err at sak receive.\n"); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // Sak received by buffer @@ -648,24 +648,24 @@ uint8_t pcd_14a_reader_scan_once(picc_14a_tag_t *tag) { ats_size -= 2; // size returned by pcd_14a_reader_ats_request includes CRC if (ats_size > 254) { NRF_LOG_INFO("Invalid ATS > 254!"); - return HF_ERR_ATS; + return STATUS_HF_ERR_ATS; } tag->ats_len = ats_size; // We do not validate ATS here as we want to report ATS as it is without breaking 14a scan if (tag->ats[0] != ats_size - 1) { NRF_LOG_INFO("Invalid ATS! First byte doesn't match received length"); - // return HF_ERR_ATS; + // return STATUS_HF_ERR_ATS; } - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { NRF_LOG_INFO("Tag SAK claimed to support ATS but tag NAKd RATS"); - // return HF_ERR_ATS; + // return STATUS_HF_ERR_ATS; } /* * FIXME: If there is an issue here, it will cause the label to lose its selected state. * It is necessary to reselect the card after the issue occurs here. */ } - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -678,14 +678,14 @@ uint8_t pcd_14a_reader_scan_auto(picc_14a_tag_t *tag) { // The first card search status = pcd_14a_reader_scan_once(tag); - if (status == HF_TAG_OK) { - return HF_TAG_OK; + if (status == STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_OK; } // Second card search status = pcd_14a_reader_scan_once(tag); - if (status == HF_TAG_OK) { - return HF_TAG_OK; + if (status == STATUS_HF_TAG_OK) { + return STATUS_HF_TAG_OK; } // More than the number of upper limits @@ -704,7 +704,7 @@ uint8_t pcd_14a_reader_ats_request(uint8_t *pAts, uint16_t *szAts, uint16_t szAt status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, rats, sizeof(rats), pAts, szAts, szAtsBitMax); - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { *szAts = 0; NRF_LOG_INFO("Err at ats receive.\n"); return status; @@ -713,7 +713,7 @@ uint8_t pcd_14a_reader_ats_request(uint8_t *pAts, uint16_t *szAts, uint16_t szAt // NRF_LOG_INFO("Length: %d\n", *szAts); if (*szAts > 0) { *szAts = *szAts / 8; } - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -724,7 +724,7 @@ uint8_t pcd_14a_reader_ats_request(uint8_t *pAts, uint16_t *szAts, uint16_t szAt uint8_t pcd_14a_reader_atqa_request(uint8_t *resp, uint8_t *resp_par, uint16_t resp_max_bit) { uint16_t len = 0; uint8_t retry = 0; - uint8_t status = HF_TAG_OK; + uint8_t status = STATUS_HF_TAG_OK; uint8_t wupa[] = { PICC_REQALL }; // 0x26 - REQA 0x52 - WAKE-UP // we may need several tries if we did send an unknown command or a wrong authentication before... @@ -736,13 +736,13 @@ uint8_t pcd_14a_reader_atqa_request(uint8_t *resp, uint8_t *resp_par, uint16_t r // normal ATQA It is 2 bytes, that is, 16bit, // We need to judge whether the data received is correct - if (status == HF_TAG_OK && len == 16) { + if (status == STATUS_HF_TAG_OK && len == 16) { // You can confirm that at least one 14A card exists in the current field - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // No card - return HF_TAG_NO; + return STATUS_HF_TAG_NO; } /** @@ -765,21 +765,21 @@ uint8_t pcd_14a_reader_gen1a_unlock(void) { // Unlock the first step, send 7bit 0x40 unlock = PICC_MAGICWUPC1; status = pcd_14a_reader_bits_transfer(&unlock, 7, NULL, recvbuf, NULL, &rx_length, U8ARR_BIT_LEN(recvbuf)); - if (!(status == HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { + if (!(status == STATUS_HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { NRF_LOG_INFO("UNLOCK(MAGICWUPC1) FAILED! Length: %d, Status: %02x\n", rx_length, status); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // Step in the second step, send a complete byte 0x43 unlock = PICC_MAGICWUPC2; status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, &unlock, 1, recvbuf, &rx_length, U8ARR_BIT_LEN(recvbuf)); - if (!(status == HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { + if (!(status == STATUS_HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { NRF_LOG_INFO("UNLOCK(MAGICWUPC2) FAILED! Length: %d, Status: %02x\n", rx_length, status); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // There is no problem with unlocking twice. We default this unlock operation successfully! - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -807,19 +807,19 @@ uint8_t pcd_14a_reader_gen1a_uplock(void) { uint8_t recvbuf[1] = { 0x00 }; status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, uplock_1, sizeof(uplock_1), recvbuf, &rx_length, U8ARR_BIT_LEN(recvbuf)); - if (!(status == HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { + if (!(status == STATUS_HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { NRF_LOG_INFO("UPLOCK1(UFUID) FAILED!\n"); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, uplock_2, sizeof(uplock_2), recvbuf, &rx_length, U8ARR_BIT_LEN(recvbuf)); - if (!(status == HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { + if (!(status == STATUS_HF_TAG_OK && rx_length == 4 && recvbuf[0] == 0x0A)) { NRF_LOG_INFO("UPLOCK2(UFUID) FAILED!\n"); - return HF_ERR_STAT; + return STATUS_HF_ERR_STAT; } // Successful card sealing - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } /** @@ -830,7 +830,7 @@ uint8_t pcd_14a_reader_gen1a_uplock(void) { * ucaddr: block address * pKEY: password * PSNR: Card serial number, 4 bytes -* @retval : The status value HF_TAG_OK is successful, tag_errauth fails, and other returns indicate some abnormalities related to communication errors! +* @retval : The status value STATUS_HF_TAG_OK is successful, tag_errauth fails, and other returns indicate some abnormalities related to communication errors! */ uint8_t pcd_14a_reader_mf1_auth(picc_14a_tag_t *tag, uint8_t type, uint8_t addr, uint8_t *pKey) { uint8_t dat_buff[12] = { type, addr }; @@ -844,11 +844,11 @@ uint8_t pcd_14a_reader_mf1_auth(picc_14a_tag_t *tag, uint8_t type, uint8_t addr, // In order to improve compatibility, we directly judge the implementation of the execution PCD_AUTHENT // After the instruction, whether the communication plus position in Status2reg is placed. if (read_register_single(Status2Reg) & 0x08) { - return HF_TAG_OK; + return STATUS_HF_TAG_OK; } // Other situations are considered failure! - return MF_ERR_AUTH; + return STATUS_MF_ERR_AUTH; } /** @@ -875,14 +875,14 @@ uint8_t pcd_14a_reader_mf1_read_by_cmd(uint8_t cmd, uint8_t addr, uint8_t *p) { crc_14a_append(dat_buff, 2); // Then initiate communication status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, dat_buff, 4, dat_buff, &len, U8ARR_BIT_LEN(dat_buff)); - if (status == HF_TAG_OK) { + if (status == STATUS_HF_TAG_OK) { if (len == 0x90 /* 0x90 = 144bits */) { // 16 -byte length CRC data, in order not to waste the CPU performance, // We can let 522 Calculate crc_14a_calculate(dat_buff, 16, crc_buff); // Check the CRC to avoid data errors if ((crc_buff[0] != dat_buff[16]) || (crc_buff[1] != dat_buff[17])) { - status = HF_ERR_CRC; + status = STATUS_HF_ERR_CRC; } // Although CRC After checking the problem, but we can still pass back // Read the card data, because developers may have special usage @@ -890,7 +890,7 @@ uint8_t pcd_14a_reader_mf1_read_by_cmd(uint8_t cmd, uint8_t addr, uint8_t *p) { } else { // The data passed back is wrong, which may be an environmental factors or cards that do not comply with specifications! // Or the control bit affects reading! - status = HF_ERR_STAT; + status = STATUS_HF_ERR_STAT; } } return status; @@ -928,16 +928,16 @@ uint8_t pcd_14a_reader_mf1_write_by_cmd(uint8_t cmd, uint8_t addr, uint8_t *p) { // Request to write a card, at this time, the card should reply to ACK status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, dat_buff, 4, dat_buff, &dat_len, U8ARR_BIT_LEN(dat_buff)); // The communication fails, the reason is returned directly - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } // The communication was successful, but the operation was rejected by the card! if ((dat_len != 4) || ((dat_buff[0] & 0x0F) != 0x0A)) { // NRF_LOG_INFO("1 status = %d, datalen = %d, data = %02x\n", status, dat_len, dat_buff[0]); - status = HF_ERR_STAT; + status = STATUS_HF_ERR_STAT; } // The communication was successful, the card accepted the card writing operation - if (status == HF_TAG_OK) { + if (status == STATUS_HF_TAG_OK) { // 1. Copy data and calculate CRC memcpy(dat_buff, p, 16); crc_14a_calculate(dat_buff, 16, &dat_buff[16]); @@ -948,14 +948,14 @@ uint8_t pcd_14a_reader_mf1_write_by_cmd(uint8_t cmd, uint8_t addr, uint8_t *p) { // 2. Transfer the final card writing data to complete the writing card status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, dat_buff, 18, dat_buff, &dat_len, U8ARR_BIT_LEN(dat_buff)); // The communication fails, the reason is returned directly - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { return status; } // The communication is successful, we need to determine whether the card is successfully processed after receiving the data // And reply ACK if ((dat_len != 4) || ((dat_buff[0] & 0x0F) != 0x0A)) { // NRF_LOG_INFO("2 status = %d, datalen = %d, data = %02x\n", status, dat_len, dat_buff[0]); - status = HF_ERR_STAT; + status = STATUS_HF_ERR_STAT; } } return status; @@ -983,7 +983,7 @@ uint8_t pcd_14a_reader_halt_tag(void) { // Prepare the molding data directly, and calculate a ghost CRC uint8_t data[] = { PICC_HALT, 0x00, 0x57, 0xCD }; status = pcd_14a_reader_bytes_transfer(PCD_TRANSCEIVE, data, 4, data, &unLen, U8ARR_BIT_LEN(data)); - return status == HF_TAG_NO && unLen == 0; + return status == STATUS_HF_TAG_NO && unLen == 0; } /** @@ -1205,7 +1205,7 @@ inline void pcd_14a_reader_crc_computer(uint8_t use522CalcCRC) { uint8_t pcd_14a_reader_raw_cmd(bool openRFField, bool waitResp, bool appendCrc, bool autoSelect, bool keepField, bool checkCrc, uint16_t waitRespTimeout, uint16_t szDataSendBits, uint8_t *pDataSend, uint8_t *pDataRecv, uint16_t *pszDataRecv, uint16_t szDataRecvBitMax) { // Status code, default is OK. - uint8_t status = HF_TAG_OK; + uint8_t status = STATUS_HF_TAG_OK; // Reset recv length. *pszDataRecv = 0; @@ -1244,7 +1244,7 @@ uint8_t pcd_14a_reader_raw_cmd(bool openRFField, bool waitResp, bool appendCrc, picc_14a_tag_t ti; status = pcd_14a_reader_scan_once(&ti); // Determine whether the card search was successful - if (status != HF_TAG_OK) { + if (status != STATUS_HF_TAG_OK) { pcd_14a_reader_antenna_off(); return status; } @@ -1296,7 +1296,7 @@ uint8_t pcd_14a_reader_raw_cmd(bool openRFField, bool waitResp, bool appendCrc, if (pDataRecv[finalRecvBytes - 2] != crc_buff[0] || pDataRecv[finalRecvBytes - 1] != crc_buff[1]) { // We have found an error in CRC verification and need to inform the upper computer! *pszDataRecv = 0; - status = HF_ERR_CRC; + status = STATUS_HF_ERR_CRC; } else { // If the CRC needs to be verified by the device and the device determines that the CRC is normal, // we will return the data without CRC diff --git a/firmware/application/src/rfid/reader/lf/lf_reader_main.c b/firmware/application/src/rfid/reader/lf/lf_reader_main.c index c3dab392..cf37c3be 100644 --- a/firmware/application/src/rfid/reader/lf/lf_reader_main.c +++ b/firmware/application/src/rfid/reader/lf/lf_reader_main.c @@ -19,9 +19,9 @@ uint32_t g_timeout_readem_ms = 500; * Search EM410X tag */ uint8_t PcdScanEM410X(uint8_t *uid) { - uint8_t ret = EM410X_TAG_NO_FOUND; + uint8_t ret = STATUS_EM410X_TAG_NO_FOUND; if (em410x_read(uid, g_timeout_readem_ms) == 1) { - ret = LF_TAG_OK; + ret = STATUS_LF_TAG_OK; } return ret; } @@ -32,8 +32,8 @@ uint8_t PcdScanEM410X(uint8_t *uid) { uint8_t check_write_ok(uint8_t *uid, uint8_t *newuid, uint8_t on_uid_diff_return) { // After the card is written, we need to read it once, // If the data I read is incorrect, it means that the writing fails - if (PcdScanEM410X(newuid) != LF_TAG_OK) { - return EM410X_TAG_NO_FOUND; + if (PcdScanEM410X(newuid) != STATUS_LF_TAG_OK) { + return STATUS_EM410X_TAG_NO_FOUND; } // If you read the card number the same // Explanation is successful (maybe) @@ -43,7 +43,7 @@ uint8_t check_write_ok(uint8_t *uid, uint8_t *newuid, uint8_t on_uid_diff_return uid[2] == newuid[2] && uid[3] == newuid[3] && uid[4] == newuid[4]) { - return LF_TAG_OK; + return STATUS_LF_TAG_OK; } // If you find the card, the card number is wrong, // Then we will return the abnormal value of the inlet @@ -102,7 +102,7 @@ uint8_t PcdWriteT55XX(uint8_t *uid, uint8_t *newkey, uint8_t *old_keys, uint8_t // Read the verification and return the results of the card writing // Do not read it here, you can check it by the upper machine - return LF_TAG_OK; + return STATUS_LF_TAG_OK; } /** diff --git a/firmware/application/src/settings.c b/firmware/application/src/settings.c index 4afc4a05..738e7ac4 100644 --- a/firmware/application/src/settings.c +++ b/firmware/application/src/settings.c @@ -137,7 +137,7 @@ uint8_t settings_save_config(void) { NRF_LOG_INFO("Config did not change."); } - return STATUS_DEVICE_SUCCESS; + return STATUS_SUCCESS; } uint8_t settings_get_animation_config() { diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 3c7f7956..b28a07b1 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -15,11 +15,10 @@ import chameleon_com import chameleon_cmd -import chameleon_status from chameleon_utils import ArgumentParserNoExit, ArgsParserError, UnexpectedResponseError from chameleon_utils import CLITree from chameleon_utils import CR, CG, CB, CC, CY, CM, C0 -from chameleon_enum import Command, SlotNumber, TagSenseType, TagSpecificType +from chameleon_enum import Command, Status, SlotNumber, TagSenseType, TagSpecificType from chameleon_enum import MifareClassicWriteMode, MifareClassicPrngType, MifareClassicDarksideStatus, MfcKeyType from chameleon_enum import AnimationMode, ButtonType, ButtonPressFunction @@ -51,7 +50,8 @@ def check_tools(): tools = [x+'.exe' for x in tools] missing_tools = [tool for tool in tools if not (default_cwd / tool).exists()] if len(missing_tools) > 0: - print(f'{CR}Warning, tools {", ".join(missing_tools)} not found. Corresponding commands will not work as intended.{C0}') + print(f'{CR}Warning, tools {", ".join(missing_tools)} not found. ' + f'Corresponding commands will not work as intended.{C0}') class BaseCLIUnit: @@ -1999,11 +1999,14 @@ def on_exec(self, args: argparse.Namespace): print(f" Command: {response.cmd} {command.name}") except ValueError: print(f" Command: {response.cmd} (unknown)") + status_string = f" Status: {response.status:#02x}" - if response.status in chameleon_status.Device: - status_string += f" {chameleon_status.Device[response.status]}" - if response.status in chameleon_status.message: - status_string += f": {chameleon_status.message[response.status]}" + try: + status = Status(response.status) + status_string += f" {status.name}" + status_string += f": {str(status)}" + except ValueError: + pass print(status_string) print(f" Data (HEX): {response.data.hex()}") diff --git a/software/script/chameleon_cmd.py b/software/script/chameleon_cmd.py index 07c3cf55..3d0a5955 100644 --- a/software/script/chameleon_cmd.py +++ b/software/script/chameleon_cmd.py @@ -2,14 +2,14 @@ import ctypes import chameleon_com -import chameleon_status from chameleon_utils import expect_response -from chameleon_enum import Command, SlotNumber, TagSenseType, TagSpecificType +from chameleon_enum import Command, Status, SlotNumber, TagSenseType, TagSpecificType from chameleon_enum import MifareClassicDarksideStatus from chameleon_enum import ButtonType, ButtonPressFunction CURRENT_VERSION_SETTINGS = 5 + class ChameleonCMD: """ Chameleon cmd function @@ -21,52 +21,52 @@ def __init__(self, chameleon: chameleon_com.ChameleonCom): """ self.device = chameleon - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_app_version(self): """ Get firmware version number(application) """ resp = self.device.send_cmd_sync(Command.GET_APP_VERSION) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = struct.unpack('!BB', resp.data) # older protocol, must upgrade! if resp.status == 0 and resp.data == b'\x00\x01': print("Chameleon does not understand new protocol. Please update firmware") return chameleon_com.Response(cmd=Command.GET_APP_VERSION, - status=chameleon_status.Device.STATUS_NOT_IMPLEMENTED) + status=Status.NOT_IMPLEMENTED) return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_device_chip_id(self): """ Get device chip id """ resp = self.device.send_cmd_sync(Command.GET_DEVICE_CHIP_ID) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data.hex() return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_device_address(self): """ Get device address """ resp = self.device.send_cmd_sync(Command.GET_DEVICE_ADDRESS) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data.hex() return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_git_version(self) -> str: resp = self.device.send_cmd_sync(Command.GET_GIT_VERSION) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data.decode('utf-8') return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_device_mode(self): resp = self.device.send_cmd_sync(Command.GET_DEVICE_MODE) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data, = struct.unpack('!?', resp.data) return resp @@ -78,7 +78,7 @@ def is_device_reader_mode(self) -> bool: return self.get_device_mode() # Note: Will return NOT_IMPLEMENTED if one tries to set reader mode on Lite - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def change_device_mode(self, mode): data = struct.pack('!B', mode) return self.device.send_cmd_sync(Command.CHANGE_DEVICE_MODE, data) @@ -91,14 +91,14 @@ def set_device_reader_mode(self, reader_mode: bool = True): """ self.change_device_mode(reader_mode) - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def hf14a_scan(self): """ 14a tags in the scanning field :return: """ resp = self.device.send_cmd_sync(Command.HF14A_SCAN) - if resp.status == chameleon_status.Device.HF_TAG_OK: + if resp.status == Status.HF_TAG_OK: # uidlen[1]|uid[uidlen]|atqa[2]|sak[1]|atslen[1]|ats[atslen] offset = 0 data = [] @@ -119,20 +119,20 @@ def mf1_detect_support(self): :return: """ resp = self.device.send_cmd_sync(Command.MF1_DETECT_SUPPORT) - return resp.status == chameleon_status.Device.HF_TAG_OK + return resp.status == Status.HF_TAG_OK - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_detect_prng(self): """ detect mifare Class of classic nt vulnerabilities :return: """ resp = self.device.send_cmd_sync(Command.MF1_DETECT_PRNG) - if resp.status == chameleon_status.Device.HF_TAG_OK: + if resp.status == Status.HF_TAG_OK: resp.data = resp.data[0] return resp - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_detect_nt_dist(self, block_known, type_known, key_known): """ Detect the random number distance of the card @@ -140,12 +140,12 @@ def mf1_detect_nt_dist(self, block_known, type_known, key_known): """ data = struct.pack('!BB6s', type_known, block_known, key_known) resp = self.device.send_cmd_sync(Command.MF1_DETECT_NT_DIST, data) - if resp.status == chameleon_status.Device.HF_TAG_OK: + if resp.status == Status.HF_TAG_OK: uid, dist = struct.unpack('!II', resp.data) resp.data = {'uid': uid, 'dist': dist} return resp - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_nested_acquire(self, block_known, type_known, key_known, block_target, type_target): """ Collect the key NT parameters needed for Nested decryption @@ -153,12 +153,12 @@ def mf1_nested_acquire(self, block_known, type_known, key_known, block_target, t """ data = struct.pack('!BB6sBB', type_known, block_known, key_known, type_target, block_target) resp = self.device.send_cmd_sync(Command.MF1_NESTED_ACQUIRE, data) - if resp.status == chameleon_status.Device.HF_TAG_OK: + if resp.status == Status.HF_TAG_OK: resp.data = [{'nt': nt, 'nt_enc': nt_enc, 'par': par} for nt, nt_enc, par in struct.iter_unpack('!IIB', resp.data)] return resp - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_darkside_acquire(self, block_target, type_target, first_recover: int or bool, sync_max): """ Collect the key parameters needed for Darkside decryption @@ -170,7 +170,7 @@ def mf1_darkside_acquire(self, block_target, type_target, first_recover: int or """ data = struct.pack('!BBBB', type_target, block_target, first_recover, sync_max) resp = self.device.send_cmd_sync(Command.MF1_DARKSIDE_ACQUIRE, data, timeout=sync_max * 10) - if resp.status == chameleon_status.Device.HF_TAG_OK: + if resp.status == Status.HF_TAG_OK: if resp.data[0] == MifareClassicDarksideStatus.OK: darkside_status, uid, nt1, par, ks1, nr, ar = struct.unpack('!BIIQQII', resp.data) resp.data = (darkside_status, {'uid': uid, 'nt1': nt1, 'par': par, 'ks1': ks1, 'nr': nr, 'ar': ar}) @@ -178,7 +178,7 @@ def mf1_darkside_acquire(self, block_target, type_target, first_recover: int or resp.data = (resp.data[0],) return resp - @expect_response([chameleon_status.Device.HF_TAG_OK, chameleon_status.Device.MF_ERR_AUTH]) + @expect_response([Status.HF_TAG_OK, Status.MF_ERR_AUTH]) def mf1_auth_one_key_block(self, block, type_value, key): """ Verify the mf1 key, only verify the specified type of key for a single sector @@ -189,10 +189,10 @@ def mf1_auth_one_key_block(self, block, type_value, key): """ data = struct.pack('!BB6s', type_value, block, key) resp = self.device.send_cmd_sync(Command.MF1_AUTH_ONE_KEY_BLOCK, data) - resp.data = resp.status == chameleon_status.Device.HF_TAG_OK + resp.data = resp.status == Status.HF_TAG_OK return resp - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_read_one_block(self, block, type_value, key): """ read one mf1 block @@ -204,7 +204,7 @@ def mf1_read_one_block(self, block, type_value, key): data = struct.pack('!BB6s', type_value, block, key) return self.device.send_cmd_sync(Command.MF1_READ_ONE_BLOCK, data) - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_write_one_block(self, block, type_value, key, block_data): """ Write mf1 single block @@ -216,10 +216,10 @@ def mf1_write_one_block(self, block, type_value, key, block_data): """ data = struct.pack('!BB6s16s', type_value, block, key, block_data) resp = self.device.send_cmd_sync(Command.MF1_WRITE_ONE_BLOCK, data) - resp.data = resp.status == chameleon_status.Device.HF_TAG_OK + resp.data = resp.status == Status.HF_TAG_OK return resp - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def hf14a_raw(self, options, resp_timeout_ms=100, data=[], bitlen=None): """ Send raw cmd to 14a tag @@ -261,7 +261,7 @@ class CStruct(ctypes.BigEndianStructure): data = bytes(cs)+struct.pack(f'!HH{len(data)}s', resp_timeout_ms, bitlen, bytearray(data)) return self.device.send_cmd_sync(Command.HF14A_RAW, data, timeout=(resp_timeout_ms / 1000) + 1) - @expect_response(chameleon_status.Device.HF_TAG_OK) + @expect_response(Status.HF_TAG_OK) def mf1_static_nested_acquire(self, block_known, type_known, key_known, block_target, type_target): """ Collect the key NT parameters needed for StaticNested decryption @@ -269,7 +269,7 @@ def mf1_static_nested_acquire(self, block_known, type_known, key_known, block_ta """ data = struct.pack('!BB6sBB', type_known, block_known, key_known, type_target, block_target) resp = self.device.send_cmd_sync(Command.MF1_STATIC_NESTED_ACQUIRE, data) - if resp.status == chameleon_status.Device.HF_TAG_OK: + if resp.status == Status.HF_TAG_OK: resp.data = { 'uid': struct.unpack('!I', resp.data[0:4])[0], 'nts': [ @@ -281,7 +281,7 @@ def mf1_static_nested_acquire(self, block_known, type_known, key_known, block_ta } return resp - @expect_response(chameleon_status.Device.LF_TAG_OK) + @expect_response(Status.LF_TAG_OK) def em410x_scan(self): """ Read the card number of EM410X @@ -289,7 +289,7 @@ def em410x_scan(self): """ return self.device.send_cmd_sync(Command.EM410X_SCAN) - @expect_response(chameleon_status.Device.LF_TAG_OK) + @expect_response(Status.LF_TAG_OK) def em410x_write_to_t55xx(self, id_bytes: bytes): """ Write EM410X card number into T55XX @@ -303,30 +303,30 @@ def em410x_write_to_t55xx(self, id_bytes: bytes): data = struct.pack(f'!5s4s{4*len(old_keys)}s', id_bytes, new_key, b''.join(old_keys)) return self.device.send_cmd_sync(Command.EM410X_WRITE_TO_T55XX, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_slot_info(self): """ Get slots info :return: """ resp = self.device.send_cmd_sync(Command.GET_SLOT_INFO) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = [{'hf': hf, 'lf': lf} for hf, lf in struct.iter_unpack('!HH', resp.data)] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_active_slot(self): """ Get selected slot :return: """ resp = self.device.send_cmd_sync(Command.GET_ACTIVE_SLOT) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data[0] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_active_slot(self, slot_index: SlotNumber): """ Set the card slot currently active for use @@ -337,7 +337,7 @@ def set_active_slot(self, slot_index: SlotNumber): data = struct.pack('!B', SlotNumber.to_fw(slot_index)) return self.device.send_cmd_sync(Command.SET_ACTIVE_SLOT, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_slot_tag_type(self, slot_index: SlotNumber, tag_type: TagSpecificType): """ Set the label type of the simulated card of the current card slot @@ -351,7 +351,7 @@ def set_slot_tag_type(self, slot_index: SlotNumber, tag_type: TagSpecificType): data = struct.pack('!BH', SlotNumber.to_fw(slot_index), tag_type) return self.device.send_cmd_sync(Command.SET_SLOT_TAG_TYPE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def delete_slot_sense_type(self, slot_index: SlotNumber, sense_type: TagSenseType): """ Delete a sense type for a specific slot. @@ -362,7 +362,7 @@ def delete_slot_sense_type(self, slot_index: SlotNumber, sense_type: TagSenseTyp data = struct.pack('!BB', SlotNumber.to_fw(slot_index), sense_type) return self.device.send_cmd_sync(Command.DELETE_SLOT_SENSE_TYPE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_slot_data_default(self, slot_index: SlotNumber, tag_type: TagSpecificType): """ Set the data of the simulated card in the specified card slot as the default data @@ -375,7 +375,7 @@ def set_slot_data_default(self, slot_index: SlotNumber, tag_type: TagSpecificTyp data = struct.pack('!BH', SlotNumber.to_fw(slot_index), tag_type) return self.device.send_cmd_sync(Command.SET_SLOT_DATA_DEFAULT, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_slot_enable(self, slot_index: SlotNumber, sense_type: TagSenseType, enabled: bool): """ Set whether the specified card slot is enabled @@ -387,7 +387,7 @@ def set_slot_enable(self, slot_index: SlotNumber, sense_type: TagSenseType, enab data = struct.pack('!BBB', SlotNumber.to_fw(slot_index), sense_type, enabled) return self.device.send_cmd_sync(Command.SET_SLOT_ENABLE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def em410x_set_emu_id(self, id: bytes): """ Set the card number simulated by EM410x @@ -399,14 +399,14 @@ def em410x_set_emu_id(self, id: bytes): data = struct.pack('5s', id) return self.device.send_cmd_sync(Command.EM410X_SET_EMU_ID, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def em410x_get_emu_id(self): """ Get the simulated EM410x card id """ return self.device.send_cmd_sync(Command.EM410X_GET_EMU_ID) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_set_detection_enable(self, enabled: bool): """ Set whether to enable the detection of the current card slot @@ -416,18 +416,18 @@ def mf1_set_detection_enable(self, enabled: bool): data = struct.pack('!B', enabled) return self.device.send_cmd_sync(Command.MF1_SET_DETECTION_ENABLE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_get_detection_count(self): """ Get the statistics of the current detection records :return: """ resp = self.device.send_cmd_sync(Command.MF1_GET_DETECTION_COUNT) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data, = struct.unpack('!I', resp.data) return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_get_detection_log(self, index: int): """ Get detection logs from the specified index position @@ -436,7 +436,7 @@ def mf1_get_detection_log(self, index: int): """ data = struct.pack('!I', index) resp = self.device.send_cmd_sync(Command.MF1_GET_DETECTION_LOG, data) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: # convert result_list = [] pos = 0 @@ -455,7 +455,7 @@ def mf1_get_detection_log(self, index: int): resp.data = result_list return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_write_emu_block_data(self, block_start: int, block_data: bytes): """ Set the block data of the analog card of MF1 @@ -467,7 +467,7 @@ def mf1_write_emu_block_data(self, block_start: int, block_data: bytes): data = struct.pack(f'!B{len(block_data)}s', block_start, block_data) return self.device.send_cmd_sync(Command.MF1_WRITE_EMU_BLOCK_DATA, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_read_emu_block_data(self, block_start: int, block_count: int): """ Gets data for selected block range @@ -475,7 +475,7 @@ def mf1_read_emu_block_data(self, block_start: int, block_count: int): data = struct.pack('!BB', block_start, block_count) return self.device.send_cmd_sync(Command.MF1_READ_EMU_BLOCK_DATA, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def hf14a_set_anti_coll_data(self, uid: bytes, atqa: bytes, sak: bytes, ats: bytes = b''): """ Set anti-collision data of current HF slot (UID/SAK/ATQA/ATS) @@ -488,7 +488,7 @@ def hf14a_set_anti_coll_data(self, uid: bytes, atqa: bytes, sak: bytes, ats: byt data = struct.pack(f'!B{len(uid)}s2s1sB{len(ats)}s', len(uid), uid, atqa, sak, len(ats), ats) return self.device.send_cmd_sync(Command.HF14A_SET_ANTI_COLL_DATA, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_slot_tag_nick(self, slot: SlotNumber, sense_type: TagSenseType, name: bytes): """ Set the nick name of the slot @@ -501,7 +501,7 @@ def set_slot_tag_nick(self, slot: SlotNumber, sense_type: TagSenseType, name: by data = struct.pack(f'!BB{len(name)}s', SlotNumber.to_fw(slot), sense_type, name) return self.device.send_cmd_sync(Command.SET_SLOT_TAG_NICK, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_slot_tag_nick(self, slot: SlotNumber, sense_type: TagSenseType): """ Get the nick name of the slot @@ -513,7 +513,7 @@ def get_slot_tag_nick(self, slot: SlotNumber, sense_type: TagSenseType): data = struct.pack('!BB', SlotNumber.to_fw(slot), sense_type) return self.device.send_cmd_sync(Command.GET_SLOT_TAG_NICK, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def delete_slot_tag_nick(self, slot: SlotNumber, sense_type: TagSenseType): """ Delete the nick name of the slot @@ -525,7 +525,7 @@ def delete_slot_tag_nick(self, slot: SlotNumber, sense_type: TagSenseType): data = struct.pack('!BB', SlotNumber.to_fw(slot), sense_type) return self.device.send_cmd_sync(Command.DELETE_SLOT_TAG_NICK, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_get_emulator_config(self): """ Get array of Mifare Classic emulators settings: @@ -537,7 +537,7 @@ def mf1_get_emulator_config(self): :return: """ resp = self.device.send_cmd_sync(Command.MF1_GET_EMULATOR_CONFIG) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: b1, b2, b3, b4, b5 = struct.unpack('!????B', resp.data) resp.data = {'detection': b1, 'gen1a_mode': b2, @@ -546,7 +546,7 @@ def mf1_get_emulator_config(self): 'write_mode': b5} return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_set_gen1a_mode(self, enabled: bool): """ Set gen1a magic mode @@ -554,7 +554,7 @@ def mf1_set_gen1a_mode(self, enabled: bool): data = struct.pack('!B', enabled) return self.device.send_cmd_sync(Command.MF1_SET_GEN1A_MODE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_set_gen2_mode(self, enabled: bool): """ Set gen2 magic mode @@ -562,7 +562,7 @@ def mf1_set_gen2_mode(self, enabled: bool): data = struct.pack('!B', enabled) return self.device.send_cmd_sync(Command.MF1_SET_GEN2_MODE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_set_block_anti_coll_mode(self, enabled: bool): """ Set 0 block anti-collision data @@ -570,7 +570,7 @@ def mf1_set_block_anti_coll_mode(self, enabled: bool): data = struct.pack('!B', enabled) return self.device.send_cmd_sync(Command.MF1_SET_BLOCK_ANTI_COLL_MODE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def mf1_set_write_mode(self, mode: int): """ Set write mode @@ -578,7 +578,7 @@ def mf1_set_write_mode(self, mode: int): data = struct.pack('!B', mode) return self.device.send_cmd_sync(Command.MF1_SET_WRITE_MODE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def slot_data_config_save(self): """ Update the configuration and data of the card slot to flash. @@ -593,27 +593,27 @@ def enter_bootloader(self): """ self.device.send_cmd_auto(Command.ENTER_BOOTLOADER, close=True) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_animation_mode(self): """ Get animation mode value """ resp = self.device.send_cmd_sync(Command.GET_ANIMATION_MODE) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data[0] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_enabled_slots(self): """ Get enabled slots """ resp = self.device.send_cmd_sync(Command.GET_ENABLED_SLOTS) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = [{'hf': hf, 'lf': lf} for hf, lf in struct.iter_unpack('!BB', resp.data)] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_animation_mode(self, value: int): """ Set animation mode value @@ -621,56 +621,56 @@ def set_animation_mode(self, value: int): data = struct.pack('!B', value) return self.device.send_cmd_sync(Command.SET_ANIMATION_MODE, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def reset_settings(self): """ Reset settings stored in flash memory """ resp = self.device.send_cmd_sync(Command.RESET_SETTINGS) - resp.data = resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS + resp.data = resp.status == Status.SUCCESS return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def save_settings(self): """ Store settings to flash memory """ resp = self.device.send_cmd_sync(Command.SAVE_SETTINGS) - resp.data = resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS + resp.data = resp.status == Status.SUCCESS return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def wipe_fds(self): """ Reset to factory settings """ resp = self.device.send_cmd_sync(Command.WIPE_FDS) - resp.data = resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS + resp.data = resp.status == Status.SUCCESS self.device.close() return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_battery_info(self): """ Get battery info """ resp = self.device.send_cmd_sync(Command.GET_BATTERY_INFO) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = struct.unpack('!HB', resp.data) return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_button_press_config(self, button: ButtonType): """ Get config of button press function """ data = struct.pack('!B', button) resp = self.device.send_cmd_sync(Command.GET_BUTTON_PRESS_CONFIG, data) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data[0] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_button_press_config(self, button: ButtonType, function: ButtonPressFunction): """ Set config of button press function @@ -678,18 +678,18 @@ def set_button_press_config(self, button: ButtonType, function: ButtonPressFunct data = struct.pack('!BB', button, function) return self.device.send_cmd_sync(Command.SET_BUTTON_PRESS_CONFIG, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_long_button_press_config(self, button: ButtonType): """ Get config of long button press function """ data = struct.pack('!B', button) resp = self.device.send_cmd_sync(Command.GET_LONG_BUTTON_PRESS_CONFIG, data) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data[0] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_long_button_press_config(self, button: ButtonType, function: ButtonPressFunction): """ Set config of long button press function @@ -697,7 +697,7 @@ def set_long_button_press_config(self, button: ButtonType, function: ButtonPress data = struct.pack('!BB', button, function) return self.device.send_cmd_sync(Command.SET_LONG_BUTTON_PRESS_CONFIG, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_ble_connect_key(self, key: str): """ Set config of ble connect key @@ -711,21 +711,21 @@ def set_ble_connect_key(self, key: str): data = struct.pack('6s', data_bytes) return self.device.send_cmd_sync(Command.SET_BLE_PAIRING_KEY, data) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_ble_pairing_key(self): """ Get config of ble connect key """ return self.device.send_cmd_sync(Command.GET_BLE_PAIRING_KEY) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def delete_all_ble_bonds(self): """ From peer manager delete all bonds. """ return self.device.send_cmd_sync(Command.DELETE_ALL_BLE_BONDS) - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_device_capabilities(self): """ Get list of commands that client understands @@ -735,13 +735,13 @@ def get_device_capabilities(self): except chameleon_com.CMDInvalidException: print("Chameleon does not understand get_device_capabilities command. Please update firmware") return chameleon_com.Response(cmd=Command.GET_DEVICE_CAPABILITIES, - status=chameleon_status.Device.STATUS_NOT_IMPLEMENTED) + status=Status.NOT_IMPLEMENTED) else: - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = [x[0] for x in struct.iter_unpack('!H', resp.data)] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_device_model(self): """ Get device model @@ -750,11 +750,11 @@ def get_device_model(self): """ resp = self.device.send_cmd_sync(Command.GET_DEVICE_MODEL) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data = resp.data[0] return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_device_settings(self): """ Get all possible settings @@ -769,7 +769,7 @@ def get_device_settings(self): settings[7:13] = settings_get_ble_pairing_key(); // BLE pairing key """ resp = self.device.send_cmd_sync(Command.GET_DEVICE_SETTINGS) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: if resp.data[0] > CURRENT_VERSION_SETTINGS: raise ValueError("Settings version in app older than Chameleon. " "Please upgrade client") @@ -788,14 +788,14 @@ def get_device_settings(self): 'ble_pairing_key': ble_pairing_key} return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def hf14a_get_anti_coll_data(self): """ Get anti-collision data from current HF slot (UID/SAK/ATQA/ATS) :return: """ resp = self.device.send_cmd_sync(Command.HF14A_GET_ANTI_COLL_DATA) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS and len(resp.data) > 0: + if resp.status == Status.SUCCESS and len(resp.data) > 0: # uidlen[1]|uid[uidlen]|atqa[2]|sak[1]|atslen[1]|ats[atslen] offset = 0 uidlen, = struct.unpack_from('!B', resp.data, offset) @@ -807,18 +807,18 @@ def hf14a_get_anti_coll_data(self): resp.data = {'uid': uid, 'atqa': atqa, 'sak': sak, 'ats': ats} return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def get_ble_pairing_enable(self): """ Is ble pairing enable? :return: True if pairing is enable, False if pairing disabled """ resp = self.device.send_cmd_sync(Command.GET_BLE_PAIRING_ENABLE) - if resp.status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + if resp.status == Status.SUCCESS: resp.data, = struct.unpack('!?', resp.data) return resp - @expect_response(chameleon_status.Device.STATUS_DEVICE_SUCCESS) + @expect_response(Status.SUCCESS) def set_ble_pairing_enable(self, enabled: bool): data = struct.pack('!B', enabled) return self.device.send_cmd_sync(Command.SET_BLE_PAIRING_ENABLE, data) diff --git a/software/script/chameleon_com.py b/software/script/chameleon_com.py index d03176a1..d4fa7241 100644 --- a/software/script/chameleon_com.py +++ b/software/script/chameleon_com.py @@ -3,9 +3,8 @@ import threading import time import serial -import chameleon_status from chameleon_utils import CR, CG, CB, CC, CY, CM, C0 -from chameleon_enum import Command +from chameleon_enum import Command, Status # each thread is waiting for its data for 100 ms before looping again THREAD_BLOCKING_TIMEOUT = 0.1 @@ -201,13 +200,13 @@ def thread_data_receive(self): command_string = f"{data_cmd} {command.name}" except ValueError: command_string = f"{data_cmd} (unknown)" - if data_status in chameleon_status.Device: - status_string = chameleon_status.Device[data_status] - if data_status == chameleon_status.Device.STATUS_DEVICE_SUCCESS: + try: + status_string = str(Status(data_status)) + if data_status == Status.SUCCESS: status_string = f'{CG}{status_string:30}{C0}' else: status_string = f'{CR}{status_string:30}{C0}' - else: + except ValueError: status_string = f"{CR}{data_status:30x}{C0}" print(f'<= {CC}{command_string:40}{C0}{status_string}' f'{CY}{data_response.hex() if data_response is not None else ""}{C0}') @@ -321,7 +320,12 @@ def send_cmd_auto(self, cmd: int, data: bytearray = None, status: int = 0, callb del self.wait_response_map[cmd] # make data frame if DEBUG: - cmd_string = f'{cmd:4} {cmd.name}{f"[{status:04x}]" if status != 0 else ""}' + try: + command = Command(cmd) + command_name = f"{command.name}" + except ValueError: + command_name = "(UNKNOWN)" + cmd_string = f'{cmd:4} {command_name}{f"[{status:04x}]" if status != 0 else ""}' print(f'=> {CC}{cmd_string:40}{C0}' f'{CY}{data.hex() if data is not None else ""}{C0}') data_frame = self.make_data_frame_bytes(cmd, data, status) @@ -361,7 +365,7 @@ def send_cmd_sync(self, cmd: int, data: bytearray or bytes or list or int = None # ok, data received. data_response = self.wait_response_map[cmd]['response'] del self.wait_response_map[cmd] - if data_response.status == chameleon_status.Device.STATUS_INVALID_CMD: + if data_response.status == Status.INVALID_CMD: raise CMDInvalidException(f"Device unsupported cmd: {cmd}") return data_response diff --git a/software/script/chameleon_enum.py b/software/script/chameleon_enum.py index cdcea902..7bac615b 100644 --- a/software/script/chameleon_enum.py +++ b/software/script/chameleon_enum.py @@ -97,6 +97,74 @@ class Command(enum.IntEnum): EM410X_GET_EMU_ID = 5001 +@enum.unique +class Status(enum.IntEnum): + HF_TAG_OK = 0x00 # IC card operation is successful + HF_TAG_NO = 0x01 # IC card not found + HF_ERR_STAT = 0x02 # Abnormal IC card communication + HF_ERR_CRC = 0x03 # IC card communication verification abnormal + HF_COLLISION = 0x04 # IC card conflict + HF_ERR_BCC = 0x05 # IC card BCC error + MF_ERR_AUTH = 0x06 # MF card verification failed + HF_ERR_PARITY = 0x07 # IC card parity error + HF_ERR_ATS = 0x08 # ATS should be present but card NAKed, or ATS too large + + # Some operations with low frequency cards succeeded! + LF_TAG_OK = 0x40 + # Unable to search for a valid EM410X label + EM410X_TAG_NO_FOUND = 0x41 + + # The parameters passed by the BLE instruction are wrong, or the parameters passed + # by calling some functions are wrong + PAR_ERR = 0x60 + # The mode of the current device is wrong, and the corresponding API cannot be called + DEVICE_MODE_ERROR = 0x66 + INVALID_CMD = 0x67 + SUCCESS = 0x68 + NOT_IMPLEMENTED = 0x69 + FLASH_WRITE_FAIL = 0x70 + FLASH_READ_FAIL = 0x71 + + def __str__(self): + if self == Status.HF_TAG_OK: + return "HF tag operation succeeded" + elif self == Status.HF_TAG_NO: + return "HF tag no found or lost" + elif self == Status.HF_ERR_STAT: + return "HF tag status error" + elif self == Status.HF_ERR_CRC: + return "HF tag data crc error" + elif self == Status.HF_COLLISION: + return "HF tag collision" + elif self == Status.HF_ERR_BCC: + return "HF tag uid bcc error" + elif self == Status.MF_ERR_AUTH: + return "HF tag auth fail" + elif self == Status.HF_ERR_PARITY: + return "HF tag data parity error" + elif self == Status.HF_ERR_ATS: + return "HF tag was supposed to send ATS but didn't" + elif self == Status.LF_TAG_OK: + return "LF tag operation succeeded" + elif self == Status.EM410X_TAG_NO_FOUND: + return "EM410x tag no found" + elif self == Status.PAR_ERR: + return "API request fail, param error" + elif self == Status.DEVICE_MODE_ERROR: + return "API request fail, device mode error" + elif self == Status.INVALID_CMD: + return "API request fail, cmd invalid" + elif self == Status.SUCCESS: + return "Device operation succeeded" + elif self == Status.NOT_IMPLEMENTED: + return "Some api not implemented" + elif self == Status.FLASH_WRITE_FAIL: + return "Flash write failed" + elif self == Status.FLASH_READ_FAIL: + return "Flash read failed" + return "Invalid status" + + @enum.unique class SlotNumber(enum.IntEnum): SLOT_1 = 1 diff --git a/software/script/chameleon_status.py b/software/script/chameleon_status.py deleted file mode 100644 index da467f50..00000000 --- a/software/script/chameleon_status.py +++ /dev/null @@ -1,73 +0,0 @@ -class MetaDevice(type): - def __iter__(self): - for attr in dir(self): - if not attr.startswith("__"): - yield attr - - def __contains__(self, item): - for field in self.__dict__: - val = self.__dict__[field] - if isinstance(val, int): - if val == item: - return True - return False - - def __getitem__(self, item): - for field in self.__dict__: - val = self.__dict__[field] - if isinstance(val, int): - if val == item: - return field - return False - - -class Device(metaclass=MetaDevice): - HF_TAG_OK = 0x00 # IC card operation is successful - HF_TAG_NO = 0x01 # IC card not found - HF_ERR_STAT = 0x02 # Abnormal IC card communication - HF_ERR_CRC = 0x03 # IC card communication verification abnormal - HF_COLLISION = 0x04 # IC card conflict - HF_ERR_BCC = 0x05 # IC card BCC error - MF_ERR_AUTH = 0x06 # MF card verification failed - HF_ERR_PARITY = 0x07 # IC card parity error - HF_ERR_ATS = 0x08 # ATS should be present but card NAKed, or ATS too large - - # Some operations with low frequency cards succeeded! - LF_TAG_OK = 0x40 - # Unable to search for a valid EM410X label - EM410X_TAG_NO_FOUND = 0x41 - - # The parameters passed by the BLE instruction are wrong, or the parameters passed - # by calling some functions are wrong - STATUS_PAR_ERR = 0x60 - # The mode of the current device is wrong, and the corresponding API cannot be called - STATUS_DEVICE_MODE_ERROR = 0x66 - STATUS_INVALID_CMD = 0x67 - STATUS_DEVICE_SUCCESS = 0x68 - STATUS_NOT_IMPLEMENTED = 0x69 - STATUS_FLASH_WRITE_FAIL = 0x70 - STATUS_FLASH_READ_FAIL = 0x71 - - -message = { - Device.HF_TAG_OK: "HF tag operation succeeded", - Device.HF_TAG_NO: "HF tag no found or lost", - Device.HF_ERR_STAT: "HF tag status error", - Device.HF_ERR_CRC: "HF tag data crc error", - Device.HF_COLLISION: "HF tag collision", - Device.HF_ERR_BCC: "HF tag uid bcc error", - Device.MF_ERR_AUTH: "HF tag auth fail", - Device.HF_ERR_PARITY: "HF tag data parity error", - Device.HF_ERR_ATS: "HF tag was supposed to send ATS but didn't", - - Device.LF_TAG_OK: "LF tag operation succeeded", - Device.EM410X_TAG_NO_FOUND: "EM410x tag no found", - - Device.STATUS_PAR_ERR: "API request fail, param error", - Device.STATUS_DEVICE_MODE_ERROR: "API request fail, device mode error", - Device.STATUS_INVALID_CMD: "API request fail, cmd invalid", - Device.STATUS_DEVICE_SUCCESS: "Device operation succeeded", - Device.STATUS_NOT_IMPLEMENTED: "Some api not implemented", - Device.STATUS_FLASH_WRITE_FAIL: "Flash write failed", - Device.STATUS_FLASH_READ_FAIL: "Flash read failed" -} diff --git a/software/script/chameleon_utils.py b/software/script/chameleon_utils.py index 36e1bd9d..9bf2a02c 100644 --- a/software/script/chameleon_utils.py +++ b/software/script/chameleon_utils.py @@ -6,7 +6,7 @@ from prompt_toolkit.completion.base import Completion from prompt_toolkit.document import Document -import chameleon_status +from chameleon_enum import Status # Colorama shorthands CR = colorama.Fore.RED @@ -110,12 +110,11 @@ def decorator(func): def error_throwing_func(*args, **kwargs): ret = func(*args, **kwargs) if ret.status not in accepted_responses: - if ret.status in chameleon_status.Device and ret.status in chameleon_status.message: - raise UnexpectedResponseError( - chameleon_status.message[ret.status]) - else: - raise UnexpectedResponseError( - f"Unexpected response and unknown status {ret.status}") + try: + status_string = str(Status(ret.status)) + except ValueError: + status_string = f"Unexpected response and unknown status {ret.status}" + raise UnexpectedResponseError(status_string) return ret.data