tlv: TLV utilities API
authorSamuel Ortiz <sameo@linux.intel.com>
Fri, 1 Jul 2011 20:24:44 +0000 (22:24 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Fri, 21 Oct 2011 06:54:04 +0000 (23:54 -0700)
Makefile.am
include/tlv.h [new file with mode: 0644]
src/near.h
src/tlv.c [new file with mode: 0644]

index c05451d..02d3d16 100644 (file)
@@ -5,7 +5,7 @@ includedir = @includedir@/near
 
 include_HEADERS = include/types.h include/log.h include/plugin.h \
                        include/tag.h include/adapter.h include/ndef.h \
-                       include/target.h
+                       include/target.h include/tlv.h
 
 nodist_include_HEADERS = include/version.h
 
@@ -31,7 +31,8 @@ libexec_PROGRAMS = src/neard
 src_neard_SOURCES = $(gdbus_sources) $(gweb_sources) $(builtin_sources) \
                        src/main.c src/error.c src/near.h src/log.c \
                        src/dbus.c src/manager.c src/adapter.c src/target.c \
-                       src/tag.c src/plugin.c src/netlink.c src/ndef.c
+                       src/tag.c src/plugin.c src/netlink.c src/ndef.c \
+                       src/tlv.c
 
 src_neard_LDADD = $(builtin_libadd) @GLIB_LIBS@ @DBUS_LIBS@ @NETLINK_LIBS@ -lresolv -ldl
 
diff --git a/include/tlv.h b/include/tlv.h
new file mode 100644 (file)
index 0000000..52810a2
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ *
+ *  neard - Near Field Communication manager
+ *
+ *  Copyright (C) 2011  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __NEAR_TLV_H
+#define __NEAR_TLV_H
+
+uint16_t near_tlv_length(uint8_t *tlv);
+uint8_t *near_tlv_next(uint8_t *tlv);
+uint8_t *near_tlv_data(uint8_t *tlv);
+
+#endif
index 7483b56..32a382d 100644 (file)
@@ -119,6 +119,8 @@ struct near_tag *__near_tag_new(uint32_t adapter_idx, uint32_t target_idx,
 void __near_tag_free(struct near_tag *tag);
 int __near_tag_read(struct near_target *target, near_tag_read_cb cb);
 
+#include <near/tlv.h>
+
 int __near_netlink_get_adapters(void);
 int __near_netlink_start_poll(int idx, uint32_t protocols);
 int __near_netlink_stop_poll(int idx);
diff --git a/src/tlv.c b/src/tlv.c
new file mode 100644 (file)
index 0000000..0359cd5
--- /dev/null
+++ b/src/tlv.c
@@ -0,0 +1,81 @@
+/*
+ *
+ *  neard - Near Field Communication manager
+ *
+ *  Copyright (C) 2011  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#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;
+
+       if (tlv[0] == TLV_NULL || tlv[0] == TLV_END)
+               length = 0;
+       else if (tlv[1] == 0xff)
+               length = *(uint16_t *)(tlv + 2);
+       else
+               length = tlv[1];
+
+       return length;
+}
+
+uint8_t *near_tlv_next(uint8_t *tlv)
+{
+       uint16_t length;
+       uint8_t l_length;
+
+       length = near_tlv_length(tlv);
+       if (length > 0xfe)
+               l_length = 3;
+       else if (length == 0)
+               l_length = 0;
+       else
+               l_length = 1;
+
+       /* T (1 byte) + L (1 or 3 bytes) + V */
+       return tlv + 1 + l_length + length;
+}
+
+uint8_t *near_tlv_data(uint8_t *tlv)
+{
+       uint16_t length;
+       uint8_t l_length;
+
+       length = near_tlv_length(tlv);
+       if (length > 0xfe)
+               l_length = 3;
+       else if (length == 0)
+               l_length = 0;
+       else
+               l_length = 1;
+
+       /* T (1 byte) + L (1 or 3 bytes) */
+       return tlv + 1 + l_length;
+}