tlv: Add tlv parsing routine
authorSamuel Ortiz <sameo@linux.intel.com>
Fri, 1 Jul 2011 20:34:31 +0000 (22:34 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 21 Oct 2011 06:54:04 +0000 (23:54 -0700)
include/tlv.h
plugins/nfctype2.c
src/tlv.c

index 060d7ec..51fd70b 100644 (file)
@@ -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
index 2e121c5..aa69024 100644 (file)
@@ -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);
 
index 0359cd5..2952450 100644 (file)
--- a/src/tlv.c
+++ b/src/tlv.c
 
 #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;
+}