From b2b054f2fdff925d83fab8203cc185cf272aed17 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Fri, 1 Jul 2011 22:34:31 +0200 Subject: [PATCH] tlv: Add tlv parsing routine --- include/tlv.h | 1 + plugins/nfctype2.c | 42 +----------------------------------------- src/tlv.c | 49 ++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 44 insertions(+), 48 deletions(-) diff --git a/include/tlv.h b/include/tlv.h index 060d7ec..51fd70b 100644 --- a/include/tlv.h +++ b/include/tlv.h @@ -32,5 +32,6 @@ uint16_t near_tlv_length(uint8_t *tlv); uint8_t *near_tlv_next(uint8_t *tlv); uint8_t *near_tlv_data(uint8_t *tlv); +int near_tlv_parse(struct near_tag *tag, near_tag_read_cb cb, uint8_t *data, uint16_t length); #endif diff --git a/plugins/nfctype2.c b/plugins/nfctype2.c index 2e121c5..aa69024 100644 --- a/plugins/nfctype2.c +++ b/plugins/nfctype2.c @@ -68,46 +68,6 @@ struct type2_tag { struct near_tag *tag; }; -static int data_parse(struct type2_tag *tag, uint8_t *data, uint16_t length) -{ - uint8_t *tlv = data, t; - - DBG(""); - - while(1) { - t = tlv[0]; - - DBG("tlv 0x%x", tlv[0]); - - switch (t) { - case TLV_NDEF: - DBG("NDEF found %d bytes long", near_tlv_length(tlv)); - - near_ndef_parse(tag->tag, near_tlv_data(tlv), - near_tlv_length(tlv)); - - break; - case TLV_END: - break; - } - - if (t == TLV_END) - break; - - tlv = near_tlv_next(tlv); - - if (tlv - data >= length) - break; - } - - DBG("Done"); - - if (tag->cb) - tag->cb(tag->adapter_idx, 0); - - return 0; -} - static int data_recv(uint8_t *resp, int length, void *data) { struct type2_tag *tag = data; @@ -142,7 +102,7 @@ static int data_recv(uint8_t *resp, int length, void *data) DBG("Done reading"); - data_parse(tag, nfc_data, data_length); + near_tlv_parse(tag->tag, tag->cb, nfc_data, data_length); g_free(tag); diff --git a/src/tlv.c b/src/tlv.c index 0359cd5..2952450 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -25,13 +25,6 @@ #include "near.h" -#define TLV_NULL 0x00 -#define TLV_LOCK 0x01 -#define TLV_MEM 0x02 -#define TLV_NDEF 0x03 -#define TLV_PROP 0xfd -#define TLV_END 0xfe - uint16_t near_tlv_length(uint8_t *tlv) { uint16_t length; @@ -79,3 +72,45 @@ uint8_t *near_tlv_data(uint8_t *tlv) /* T (1 byte) + L (1 or 3 bytes) */ return tlv + 1 + l_length; } + +int near_tlv_parse(struct near_tag *tag, near_tag_read_cb cb, + uint8_t *data, uint16_t length) +{ + uint8_t *tlv = data, t; + uint32_t adapter_idx = near_tag_get_adapter_idx(tag); + + DBG(""); + + while(1) { + t = tlv[0]; + + DBG("tlv 0x%x", tlv[0]); + + switch (t) { + case TLV_NDEF: + DBG("NDEF found %d bytes long", near_tlv_length(tlv)); + + near_ndef_parse(tag, near_tlv_data(tlv), + near_tlv_length(tlv)); + + break; + case TLV_END: + break; + } + + if (t == TLV_END) + break; + + tlv = near_tlv_next(tlv); + + if (tlv - data >= length) + break; + } + + DBG("Done"); + + if (cb) + cb(adapter_idx, 0); + + return 0; +} -- 2.7.4