Example #1
0
ret_code_t nfc_t4t_file_control_tlv_parse(nfc_t4t_tlv_block_t * p_file_control_tlv,
                                          uint8_t             * p_raw_data,
                                          uint16_t            * p_len)
{
    ret_code_t err_code;
    uint8_t  * p_offset = p_raw_data;

    if (*p_len < TLV_MIN_TL_FIELD_LEN)
    {
        return NRF_ERROR_INVALID_LENGTH;
    }
    memset(p_file_control_tlv, 0, sizeof(nfc_t4t_tlv_block_t));

    // Handle type field of TLV block.
    p_file_control_tlv->type = *p_offset;
    p_offset                += TLV_TYPE_FIELD_LEN;

    // Handle length field of TLV block.
    if (*p_offset == TLV_LEN_LONG_FORMAT_TOKEN)
    {
        if (*p_len < TLV_MIN_LONG_FORMAT_TL_FIELD_LEN)
        {
            return NRF_ERROR_INVALID_LENGTH;
        }

        p_file_control_tlv->length = uint16_big_decode(p_offset + TLV_LEN_LONG_FORMAT_TOKEN_SIZE);
        p_offset                  += TLV_LEN_LONG_FIELD_LEN;

        if (p_file_control_tlv->length < TLV_LEN_LONG_FORMAT_MIN_VALUE)
        {
            return NRF_ERROR_INVALID_DATA;
        }
    }
    else
    {
        p_file_control_tlv->length = *p_offset;
        p_offset                  += TLV_LEN_SHORT_FIELD_LEN;
    }

    // Calculate the total TLV block size.
    uint16_t tlv_block_len = (p_offset - p_raw_data) + p_file_control_tlv->length;
    if (*p_len < tlv_block_len)
    {
        return NRF_ERROR_INVALID_LENGTH;
    }
    *p_len = tlv_block_len;

    // Validate if type and length fields contain values supported by Type 4 Tag.
    err_code = nfc_t4t_file_control_tl_validate(p_file_control_tlv);
    VERIFY_SUCCESS(err_code);

    // Handle value field of TLV block.
    err_code = nfc_t4t_file_control_value_parse(p_file_control_tlv, p_offset);
    return err_code;
}
Example #2
0
/**
 * @brief Function for reading the length field of a TLV block from the p_raw_data buffer.
 *
 * This function reads the length field of a TLV block and inserts its value into a structure
 * pointed by the p_tlv_buf pointer.
 *
 * @param[in]     p_type_2_tag Pointer to the structure that contains Type 2 Tag data parsed so far.
 * @param[in]     p_raw_data   Pointer to the buffer with a raw data from the tag.
 * @param[in,out] p_l_offset As input: offset of the length field to read. As output: offset of
 *                             the first byte after the length field.
 * @param[out]    p_tlv_buf    Pointer to a @ref tlv_block_t structure where the length will be
 *                             inserted.
 *
 * @retval        NRF_SUCCESS             If the length field at specified offset is correct.
 * @retval        NRF_ERROR_INVALID_DATA  If the length field at specified offset exceeds the data
 *                                        area specified in the Capability Container or has
 *                                        incorrect format.
 *
 */
static ret_code_t type_2_tag_length_extract(type_2_tag_t * p_type_2_tag,
                                            uint8_t      * p_raw_data,
                                            uint16_t     * p_l_offset,
                                            tlv_block_t  * p_tlv_buf)
{
    uint16_t length;

    if (!type_2_tag_is_field_within_data_range(p_type_2_tag, *p_l_offset, TLV_L_SHORT_LENGTH))
    {
        return NRF_ERROR_INVALID_DATA;
    }

    length = p_raw_data[*p_l_offset];

    if (length == TLV_L_FORMAT_FLAG)
    {
        // Check another two bytes.
        if (!type_2_tag_is_field_within_data_range(p_type_2_tag, *p_l_offset, TLV_L_LONG_LENGTH))
        {
            return NRF_ERROR_INVALID_DATA;
        }

        length = uint16_big_decode(&p_raw_data[*p_l_offset + 1]);

        // Long length value cannot be lower than 0xFF.
        if (length < 0xFF)
        {
            return NRF_ERROR_INVALID_DATA;
        }

        p_tlv_buf->length = length;
        *p_l_offset += TLV_L_LONG_LENGTH;

    }
    else
    {
        p_tlv_buf->length = length;
        *p_l_offset += TLV_L_SHORT_LENGTH;
    }

    return NRF_SUCCESS;
}
Example #3
0
/**
 * @brief Function for parsing value field of File Control TLV.
 */
static ret_code_t nfc_t4t_file_control_value_parse(nfc_t4t_tlv_block_t * p_file_control_tlv,
                                                   uint8_t             * p_value_buff)
{
    nfc_t4t_file_control_val_t * p_control_tlv_val;

    // Handle File Identifier field.
    p_control_tlv_val          = &p_file_control_tlv->value;
    p_control_tlv_val->file_id = uint16_big_decode(p_value_buff);
    p_value_buff              += FILE_CONTROL_FILE_ID_FIELD_SIZE;

    switch (p_control_tlv_val->file_id)
    {
        case FILE_ID_INVALID_VALUE_0:
        case FILE_ID_INVALID_VALUE_1:
        case FILE_ID_INVALID_VALUE_2:
        case FILE_ID_INVALID_VALUE_3:
        case FILE_ID_INVALID_VALUE_4:
        case FILE_ID_INVALID_VALUE_5:
            return NRF_ERROR_INVALID_DATA;

        default:
            break;
    }

    // Handle Max file size field.
    switch (p_file_control_tlv->type)
    {
        case NDEF_FILE_CONTROL_TLV:
            p_control_tlv_val->max_file_size = uint16_big_decode(p_value_buff);
            p_value_buff                    += NDEF_FILE_MAX_SIZE_FIELD_SIZE;
            NFC_T4T_FILE_CONTROL_MAX_SIZE_FIELD_RANGE_VERIFY(p_control_tlv_val->max_file_size,
                                                             NDEF_FILE_MAX_SIZE_MIN_VAL,
                                                             NDEF_FILE_MAX_SIZE_MAX_VAL);
            break;

        case PROPRIETARY_FILE_CONTROL_TLV:
            p_control_tlv_val->max_file_size = uint16_big_decode(p_value_buff);
            p_value_buff                    += PROPRIETARY_FILE_MAX_SIZE_FIELD_SIZE;
            NFC_T4T_FILE_CONTROL_MAX_SIZE_FIELD_RANGE_VERIFY(p_control_tlv_val->max_file_size,
                                                             PROPRIETARY_FILE_MAX_SIZE_MIN_VAL,
                                                             PROPRIETARY_FILE_MAX_SIZE_MAX_VAL);
            break;

        case EXTENDED_NDEF_FILE_CONTROL_TLV:
            p_control_tlv_val->max_file_size = uint32_big_decode(p_value_buff);
            p_value_buff                    += EXTENDED_NDEF_FILE_MAX_SIZE_FIELD_SIZE;
            NFC_T4T_FILE_CONTROL_MAX_SIZE_FIELD_RANGE_VERIFY(p_control_tlv_val->max_file_size,
                                                             EXTENDED_NDEF_FILE_MAX_SIZE_MIN_VAL,
                                                             EXTENDED_NDEF_FILE_MAX_SIZE_MAX_VAL);
            break;
    }

    // Handle read access condition field.
    p_control_tlv_val->read_access = *p_value_buff;
    p_value_buff                  += FILE_CONTROL_READ_ACCESS_FIELD_SIZE;

    // Handle write access condition field.
    p_control_tlv_val->write_access = *p_value_buff;

    return NRF_SUCCESS;
}