Skip to content

Commit

Permalink
NFC: iso14443_4a improvements. Canvas: extended icon draw. (#3918)
Browse files Browse the repository at this point in the history
* Now 4a listener invokes upper level callback on Halt and FieldOff
* Added new method for drawing mirrored XBM bitmaps
* iso14443_4a poller logic enhanced
* Function renamed accroding to review suggestions
* Rename #2
* Api adjustements
* Correct API bump

Co-authored-by: あく <[email protected]>
  • Loading branch information
RebornedBrain and skotopes authored Oct 14, 2024
1 parent 0f83141 commit 0902fd4
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 10 deletions.
14 changes: 13 additions & 1 deletion applications/services/gui/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,21 @@ void canvas_draw_xbm(
size_t height,
const uint8_t* bitmap) {
furi_check(canvas);
canvas_draw_xbm_ex(canvas, x, y, width, height, IconRotation0, bitmap);
}

void canvas_draw_xbm_ex(
Canvas* canvas,
int32_t x,
int32_t y,
size_t width,
size_t height,
IconRotation rotation,
const uint8_t* bitmap_data) {
furi_check(canvas);
x += canvas->offset_x;
y += canvas->offset_y;
canvas_draw_u8g2_bitmap(&canvas->fb, x, y, width, height, bitmap, IconRotation0);
canvas_draw_u8g2_bitmap(&canvas->fb, x, y, width, height, bitmap_data, rotation);
}

void canvas_draw_glyph(Canvas* canvas, int32_t x, int32_t y, uint16_t ch) {
Expand Down
20 changes: 20 additions & 0 deletions applications/services/gui/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,26 @@ void canvas_draw_xbm(
size_t height,
const uint8_t* bitmap);

/** Draw rotated XBM bitmap
*
* @param canvas Canvas instance
* @param x x coordinate
* @param y y coordinate
* @param[in] width bitmap width
* @param[in] height bitmap height
* @param[in] rotation bitmap rotation
* @param bitmap pointer to XBM bitmap data
*/

void canvas_draw_xbm_ex(
Canvas* canvas,
int32_t x,
int32_t y,
size_t width,
size_t height,
IconRotation rotation,
const uint8_t* bitmap_data);

/** Draw dot at x,y
*
* @param canvas Canvas instance
Expand Down
90 changes: 83 additions & 7 deletions lib/nfc/helpers/iso14443_4_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,42 @@

#include <furi.h>

#define ISO14443_4_BLOCK_PCB (1U << 1)
#define ISO14443_4_BLOCK_PCB_I (0U)
#define ISO14443_4_BLOCK_PCB_R (5U << 5)
#define ISO14443_4_BLOCK_PCB_S (3U << 6)
#define ISO14443_4_BLOCK_PCB (1U << 1)
#define ISO14443_4_BLOCK_PCB_MASK (0x03)

#define ISO14443_4_BLOCK_PCB_I (0U)
#define ISO14443_4_BLOCK_PCB_I_NAD_OFFSET (2)
#define ISO14443_4_BLOCK_PCB_I_CID_OFFSET (3)
#define ISO14443_4_BLOCK_PCB_I_CHAIN_OFFSET (4)
#define ISO14443_4_BLOCK_PCB_I_NAD_MASK (1U << ISO14443_4_BLOCK_PCB_I_NAD_OFFSET)
#define ISO14443_4_BLOCK_PCB_I_CID_MASK (1U << ISO14443_4_BLOCK_PCB_I_CID_OFFSET)
#define ISO14443_4_BLOCK_PCB_I_CHAIN_MASK (1U << ISO14443_4_BLOCK_PCB_I_CHAIN_OFFSET)

#define ISO14443_4_BLOCK_PCB_R_MASK (5U << 5)
#define ISO14443_4_BLOCK_PCB_R_NACK_OFFSET (4)
#define ISO14443_4_BLOCK_PCB_R_CID_OFFSET (3)
#define ISO14443_4_BLOCK_PCB_R_CID_MASK (1U << ISO14443_4_BLOCK_PCB_R_CID_OFFSET)
#define ISO14443_4_BLOCK_PCB_R_NACK_MASK (1U << ISO14443_4_BLOCK_PCB_R_NACK_OFFSET)

#define ISO14443_4_BLOCK_PCB_S_MASK (3U << 6)
#define ISO14443_4_BLOCK_PCB_S_CID_OFFSET (3)
#define ISO14443_4_BLOCK_PCB_S_WTX_DESELECT_OFFSET (4)
#define ISO14443_4_BLOCK_PCB_S_CID_MASK (1U << ISO14443_4_BLOCK_PCB_R_CID_OFFSET)
#define ISO14443_4_BLOCK_PCB_S_WTX_DESELECT_MASK (3U << ISO14443_4_BLOCK_PCB_S_WTX_DESELECT_OFFSET)

#define ISO14443_4_BLOCK_PCB_BITS_ACTIVE(pcb, mask) (((pcb) & mask) == mask)

#define ISO14443_4_BLOCK_PCB_IS_R_BLOCK(pcb) \
ISO14443_4_BLOCK_PCB_BITS_ACTIVE(pcb, ISO14443_4_BLOCK_PCB_R_MASK)

#define ISO14443_4_BLOCK_PCB_IS_S_BLOCK(pcb) \
ISO14443_4_BLOCK_PCB_BITS_ACTIVE(pcb, ISO14443_4_BLOCK_PCB_S_MASK)

#define ISO14443_4_BLOCK_PCB_IS_CHAIN_ACTIVE(pcb) \
ISO14443_4_BLOCK_PCB_BITS_ACTIVE(pcb, ISO14443_4_BLOCK_PCB_I_CHAIN_MASK)

#define ISO14443_4_BLOCK_PCB_R_NACK_ACTIVE(pcb) \
ISO14443_4_BLOCK_PCB_BITS_ACTIVE(pcb, ISO14443_4_BLOCK_PCB_R_NACK_MASK)

struct Iso14443_4Layer {
uint8_t pcb;
Expand All @@ -31,9 +63,31 @@ void iso14443_4_layer_free(Iso14443_4Layer* instance) {

void iso14443_4_layer_reset(Iso14443_4Layer* instance) {
furi_assert(instance);
instance->pcb_prev = 0;
instance->pcb = ISO14443_4_BLOCK_PCB_I | ISO14443_4_BLOCK_PCB;
}

void iso14443_4_layer_set_i_block(Iso14443_4Layer* instance, bool chaining, bool CID_present) {
uint8_t block_pcb = instance->pcb & ISO14443_4_BLOCK_PCB_MASK;
instance->pcb = ISO14443_4_BLOCK_PCB_I | (chaining << ISO14443_4_BLOCK_PCB_I_CHAIN_OFFSET) |
(CID_present << ISO14443_4_BLOCK_PCB_I_CID_OFFSET) | block_pcb;
}

void iso14443_4_layer_set_r_block(Iso14443_4Layer* instance, bool acknowledged, bool CID_present) {
furi_assert(instance);
uint8_t block_pcb = instance->pcb & ISO14443_4_BLOCK_PCB_MASK;
instance->pcb = ISO14443_4_BLOCK_PCB_R_MASK |
(!acknowledged << ISO14443_4_BLOCK_PCB_R_NACK_OFFSET) |
(CID_present << ISO14443_4_BLOCK_PCB_R_CID_OFFSET) | block_pcb;
}

void iso14443_4_layer_set_s_block(Iso14443_4Layer* instance, bool deselect, bool CID_present) {
furi_assert(instance);
uint8_t des_wtx = !deselect ? (ISO14443_4_BLOCK_PCB_S_WTX_DESELECT_MASK) : 0;
instance->pcb = ISO14443_4_BLOCK_PCB_S_MASK | des_wtx |
(CID_present << ISO14443_4_BLOCK_PCB_S_CID_OFFSET) | ISO14443_4_BLOCK_PCB;
}

void iso14443_4_layer_encode_block(
Iso14443_4Layer* instance,
const BitBuffer* input_data,
Expand All @@ -46,6 +100,11 @@ void iso14443_4_layer_encode_block(
iso14443_4_layer_update_pcb(instance);
}

static inline uint8_t iso14443_4_layer_get_response_pcb(const BitBuffer* block_data) {
const uint8_t* data = bit_buffer_get_data(block_data);
return data[0];
}

bool iso14443_4_layer_decode_block(
Iso14443_4Layer* instance,
BitBuffer* output_data,
Expand All @@ -55,9 +114,26 @@ bool iso14443_4_layer_decode_block(
bool ret = false;

do {
if(!bit_buffer_starts_with_byte(block_data, instance->pcb_prev)) break;
bit_buffer_copy_right(output_data, block_data, 1);
ret = true;
if(ISO14443_4_BLOCK_PCB_IS_R_BLOCK(instance->pcb_prev)) {
const uint8_t response_pcb = iso14443_4_layer_get_response_pcb(block_data);
ret = (ISO14443_4_BLOCK_PCB_IS_R_BLOCK(response_pcb)) &&
(!ISO14443_4_BLOCK_PCB_R_NACK_ACTIVE(response_pcb));
instance->pcb &= ISO14443_4_BLOCK_PCB_MASK;
iso14443_4_layer_update_pcb(instance);
} else if(ISO14443_4_BLOCK_PCB_IS_CHAIN_ACTIVE(instance->pcb_prev)) {
const uint8_t response_pcb = iso14443_4_layer_get_response_pcb(block_data);
ret = (ISO14443_4_BLOCK_PCB_IS_R_BLOCK(response_pcb)) &&
(!ISO14443_4_BLOCK_PCB_R_NACK_ACTIVE(response_pcb));
instance->pcb &= ~(ISO14443_4_BLOCK_PCB_I_CHAIN_MASK);
} else if(ISO14443_4_BLOCK_PCB_IS_S_BLOCK(instance->pcb_prev)) {
ret = bit_buffer_starts_with_byte(block_data, instance->pcb_prev);
if(bit_buffer_get_size_bytes(block_data) > 1)
bit_buffer_copy_right(output_data, block_data, 1);
} else {
if(!bit_buffer_starts_with_byte(block_data, instance->pcb_prev)) break;
bit_buffer_copy_right(output_data, block_data, 1);
ret = true;
}
} while(false);

return ret;
Expand Down
4 changes: 4 additions & 0 deletions lib/nfc/helpers/iso14443_4_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ void iso14443_4_layer_free(Iso14443_4Layer* instance);

void iso14443_4_layer_reset(Iso14443_4Layer* instance);

void iso14443_4_layer_set_i_block(Iso14443_4Layer* instance, bool chaining, bool CID_present);
void iso14443_4_layer_set_r_block(Iso14443_4Layer* instance, bool acknowledged, bool CID_present);
void iso14443_4_layer_set_s_block(Iso14443_4Layer* instance, bool deselect, bool CID_present);

void iso14443_4_layer_encode_block(
Iso14443_4Layer* instance,
const BitBuffer* input_data,
Expand Down
9 changes: 9 additions & 0 deletions lib/nfc/protocols/iso14443_4a/iso14443_4a_listener.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ static NfcCommand iso14443_4a_listener_run(NfcGenericEvent event, void* context)
iso14443_3a_event->type == Iso14443_3aListenerEventTypeHalted ||
iso14443_3a_event->type == Iso14443_3aListenerEventTypeFieldOff) {
instance->state = Iso14443_4aListenerStateIdle;

instance->iso14443_4a_event.type = iso14443_3a_event->type ==
Iso14443_3aListenerEventTypeHalted ?
Iso14443_4aListenerEventTypeHalted :
Iso14443_4aListenerEventTypeFieldOff;

if(instance->callback) {
command = instance->callback(instance->generic_event, instance->context);
}
command = NfcCommandContinue;
}

Expand Down
1 change: 1 addition & 0 deletions lib/nfc/protocols/iso14443_4a/iso14443_4a_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef struct Iso14443_4aListener Iso14443_4aListener;

typedef enum {
Iso14443_4aListenerEventTypeHalted,
Iso14443_4aListenerEventTypeFieldOff,
Iso14443_4aListenerEventTypeReceivedData,
} Iso14443_4aListenerEventType;

Expand Down
63 changes: 63 additions & 0 deletions lib/nfc/protocols/iso14443_4a/iso14443_4a_poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,69 @@ Iso14443_4aError iso14443_4a_poller_send_block(
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer);

/**
* @brief Transmit and receive Iso14443_4a chained block in poller mode. Also it
* automatically modifies PCB packet byte with appropriate bits then resets them back
*
* Must ONLY be used inside the callback function.
*
* The rx_buffer will be filled with any data received as a response to data
* sent from tx_buffer. The fwt parameter is calculated during activation procedure.
*
* @param[in, out] instance pointer to the instance to be used in the transaction.
* @param[in] tx_buffer pointer to the buffer containing the data to be transmitted.
* @param[out] rx_buffer pointer to the buffer to be filled with received data.
* @return Iso14443_4aErrorNone on success, an error code on failure.
*/
Iso14443_4aError iso14443_4a_poller_send_chain_block(
Iso14443_4aPoller* instance,
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer);

/**
* @brief Transmit Iso14443_4a R-block in poller mode. This block never contains
* data, but can contain CID and NAD, therefore in tx_buffer only two bytes can be added.
* The first one will represent CID, the second one will represent NAD.
*
* Must ONLY be used inside the callback function.
*
* The rx_buffer will be filled with R-block repsonse
*
* @param[in, out] instance pointer to the instance to be used in the transaction.
* @param[in] acknowledged Sets appropriate bit in PCB byte. True - ACK, false - NAK
* @param[in] tx_buffer pointer to the buffer containing the data to be transmitted.
* @param[out] rx_buffer pointer to the buffer to be filled with received data.
* @return Iso14443_4aErrorNone on success, an error code on failure.
*/
Iso14443_4aError iso14443_4a_poller_send_receive_ready_block(
Iso14443_4aPoller* instance,
bool acknowledged,
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer);

/**
* @brief Transmit Iso14443_4a S-block in poller mode. S-block used to exchange control
* information between the card and the reader. Two different types of S-blocks
* are defined:
* - Waiting time extension containing a 1 byte long INF field and (deselect = false)
* - DESELECT containing no INF field (deselect = true)
*
* Must ONLY be used inside the callback function.
*
* The rx_buffer will be filled with R-block repsonse
*
* @param[in, out] instance pointer to the instance to be used in the transaction.
* @param[in] deselect Sets appropriate bit in PCB byte.
* @param[in] tx_buffer pointer to the buffer containing the data to be transmitted.
* @param[out] rx_buffer pointer to the buffer to be filled with received data.
* @return Iso14443_4aErrorNone on success, an error code on failure.
*/
Iso14443_4aError iso14443_4a_poller_send_supervisory_block(
Iso14443_4aPoller* instance,
bool deselect,
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer);

/**
* @brief Send HALT command to the card.
*
Expand Down
31 changes: 31 additions & 0 deletions lib/nfc/protocols/iso14443_4a/iso14443_4a_poller_i.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,34 @@ Iso14443_4aError iso14443_4a_poller_send_block(

return error;
}

Iso14443_4aError iso14443_4a_poller_send_chain_block(
Iso14443_4aPoller* instance,
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer) {
iso14443_4_layer_set_i_block(instance->iso14443_4_layer, true, false);
Iso14443_4aError error = iso14443_4a_poller_send_block(instance, tx_buffer, rx_buffer);
return error;
}

Iso14443_4aError iso14443_4a_poller_send_receive_ready_block(
Iso14443_4aPoller* instance,
bool acknowledged,
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer) {
bool CID_present = bit_buffer_get_size_bytes(tx_buffer) != 0;
iso14443_4_layer_set_r_block(instance->iso14443_4_layer, acknowledged, CID_present);
Iso14443_4aError error = iso14443_4a_poller_send_block(instance, tx_buffer, rx_buffer);
return error;
}

Iso14443_4aError iso14443_4a_poller_send_supervisory_block(
Iso14443_4aPoller* instance,
bool deselect,
const BitBuffer* tx_buffer,
BitBuffer* rx_buffer) {
bool CID_present = bit_buffer_get_size_bytes(tx_buffer) != 0;
iso14443_4_layer_set_s_block(instance->iso14443_4_layer, deselect, CID_present);
Iso14443_4aError error = iso14443_4a_poller_send_block(instance, tx_buffer, rx_buffer);
return error;
}
3 changes: 2 additions & 1 deletion targets/f18/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,77.0,,
Version,+,77.1,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
Header,+,applications/services/cli/cli.h,,
Expand Down Expand Up @@ -746,6 +746,7 @@ Function,+,canvas_draw_str,void,"Canvas*, int32_t, int32_t, const char*"
Function,+,canvas_draw_str_aligned,void,"Canvas*, int32_t, int32_t, Align, Align, const char*"
Function,+,canvas_draw_triangle,void,"Canvas*, int32_t, int32_t, size_t, size_t, CanvasDirection"
Function,+,canvas_draw_xbm,void,"Canvas*, int32_t, int32_t, size_t, size_t, const uint8_t*"
Function,+,canvas_draw_xbm_ex,void,"Canvas*, int32_t, int32_t, size_t, size_t, IconRotation, const uint8_t*"
Function,+,canvas_get_font_params,const CanvasFontParameters*,"const Canvas*, Font"
Function,+,canvas_glyph_width,size_t,"Canvas*, uint16_t"
Function,+,canvas_height,size_t,const Canvas*
Expand Down
6 changes: 5 additions & 1 deletion targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,77.0,,
Version,+,77.1,,
Header,+,applications/drivers/subghz/cc1101_ext/cc1101_ext_interconnect.h,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/bt/bt_service/bt_keys_storage.h,,
Expand Down Expand Up @@ -823,6 +823,7 @@ Function,+,canvas_draw_str,void,"Canvas*, int32_t, int32_t, const char*"
Function,+,canvas_draw_str_aligned,void,"Canvas*, int32_t, int32_t, Align, Align, const char*"
Function,+,canvas_draw_triangle,void,"Canvas*, int32_t, int32_t, size_t, size_t, CanvasDirection"
Function,+,canvas_draw_xbm,void,"Canvas*, int32_t, int32_t, size_t, size_t, const uint8_t*"
Function,+,canvas_draw_xbm_ex,void,"Canvas*, int32_t, int32_t, size_t, size_t, IconRotation, const uint8_t*"
Function,+,canvas_get_font_params,const CanvasFontParameters*,"const Canvas*, Font"
Function,+,canvas_glyph_width,size_t,"Canvas*, uint16_t"
Function,+,canvas_height,size_t,const Canvas*
Expand Down Expand Up @@ -2125,6 +2126,9 @@ Function,+,iso14443_4a_load,_Bool,"Iso14443_4aData*, FlipperFormat*, uint32_t"
Function,+,iso14443_4a_poller_halt,Iso14443_4aError,Iso14443_4aPoller*
Function,+,iso14443_4a_poller_read_ats,Iso14443_4aError,"Iso14443_4aPoller*, Iso14443_4aAtsData*"
Function,+,iso14443_4a_poller_send_block,Iso14443_4aError,"Iso14443_4aPoller*, const BitBuffer*, BitBuffer*"
Function,+,iso14443_4a_poller_send_chain_block,Iso14443_4aError,"Iso14443_4aPoller*, const BitBuffer*, BitBuffer*"
Function,+,iso14443_4a_poller_send_receive_ready_block,Iso14443_4aError,"Iso14443_4aPoller*, _Bool, const BitBuffer*, BitBuffer*"
Function,+,iso14443_4a_poller_send_supervisory_block,Iso14443_4aError,"Iso14443_4aPoller*, _Bool, const BitBuffer*, BitBuffer*"
Function,+,iso14443_4a_reset,void,Iso14443_4aData*
Function,+,iso14443_4a_save,_Bool,"const Iso14443_4aData*, FlipperFormat*"
Function,+,iso14443_4a_set_uid,_Bool,"Iso14443_4aData*, const uint8_t*, size_t"
Expand Down

0 comments on commit 0902fd4

Please sign in to comment.