[ndnSIM] lp: Add GeoTag

Change-Id: Ie58aefaf7c8f1b60292bfe01302586f81603949c
diff --git a/ndn-cxx/lp/fields.hpp b/ndn-cxx/lp/fields.hpp
index 2fbfcae..d943880 100644
--- a/ndn-cxx/lp/fields.hpp
+++ b/ndn-cxx/lp/fields.hpp
@@ -25,6 +25,7 @@
 #include "ndn-cxx/lp/field-decl.hpp"
 
 #include "ndn-cxx/lp/cache-policy.hpp"
+#include "ndn-cxx/lp/geo-tag.hpp"
 #include "ndn-cxx/lp/nack-header.hpp"
 #include "ndn-cxx/lp/prefix-announcement-header.hpp"
 
@@ -122,6 +123,11 @@
                   NonNegativeIntegerTag> HopCountTagField;
 BOOST_CONCEPT_ASSERT((Field<HopCountTagField>));
 
+typedef FieldDecl<field_location_tags::Header,
+                  GeoTag,
+                  tlv::GeoTag> GeoTagField;
+BOOST_CONCEPT_ASSERT((Field<GeoTagField>));
+
 /** \brief Declare the Fragment field.
  *
  *  The fragment (i.e. payload) is the bytes between two provided iterators. During encoding,
@@ -149,7 +155,8 @@
   TxSequenceField,
   NonDiscoveryField,
   PrefixAnnouncementField,
-  HopCountTagField
+  HopCountTagField,
+  GeoTagField
   > FieldSet;
 
 } // namespace lp
diff --git a/ndn-cxx/lp/geo-tag.cpp b/ndn-cxx/lp/geo-tag.cpp
new file mode 100644
index 0000000..4832ada
--- /dev/null
+++ b/ndn-cxx/lp/geo-tag.cpp
@@ -0,0 +1,92 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2019 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "ndn-cxx/lp/geo-tag.hpp"
+#include "ndn-cxx/lp/tlv.hpp"
+
+namespace ndn {
+namespace lp {
+
+GeoTag::GeoTag(const Block& block)
+{
+  wireDecode(block);
+}
+
+template<encoding::Tag TAG>
+size_t
+GeoTag::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+  size_t length = 0;
+  length += prependDoubleBlock(encoder, tlv::GeoTagPos, std::get<2>(m_pos));
+  length += prependDoubleBlock(encoder, tlv::GeoTagPos, std::get<1>(m_pos));
+  length += prependDoubleBlock(encoder, tlv::GeoTagPos, std::get<0>(m_pos));
+  length += encoder.prependVarNumber(length);
+  length += encoder.prependVarNumber(tlv::GeoTag);
+  return length;
+}
+
+template size_t
+GeoTag::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
+
+template size_t
+GeoTag::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
+
+const Block&
+GeoTag::wireEncode() const
+{
+  if (m_wire.hasWire()) {
+    return m_wire;
+  }
+
+  EncodingEstimator estimator;
+  size_t estimatedSize = wireEncode(estimator);
+
+  EncodingBuffer buffer(estimatedSize, 0);
+  wireEncode(buffer);
+
+  m_wire = buffer.block();
+
+  return m_wire;
+}
+
+void
+GeoTag::wireDecode(const Block& wire)
+{
+  if (wire.type() != tlv::GeoTag) {
+    NDN_THROW(ndn::tlv::Error("expecting GeoTag block"));
+  }
+
+  m_wire = wire;
+  m_wire.parse();
+
+  if (m_wire.elements().size() < 3 ||
+      m_wire.elements()[0].type() != tlv::GeoTagPos ||
+      m_wire.elements()[1].type() != tlv::GeoTagPos ||
+      m_wire.elements()[2].type() != tlv::GeoTagPos) {
+    NDN_THROW(ndn::tlv::Error("Unexpected input while decoding GeoTag"));
+  }
+  m_pos = {encoding::readDouble(m_wire.elements()[0]),
+           encoding::readDouble(m_wire.elements()[1]),
+           encoding::readDouble(m_wire.elements()[2])};
+}
+
+} // namespace lp
+} // namespace ndn
diff --git a/ndn-cxx/lp/geo-tag.hpp b/ndn-cxx/lp/geo-tag.hpp
new file mode 100644
index 0000000..74c9312
--- /dev/null
+++ b/ndn-cxx/lp/geo-tag.hpp
@@ -0,0 +1,102 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2019 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_CXX_LP_GEO_TAG_HPP
+#define NDN_CXX_LP_GEO_TAG_HPP
+
+#include "ndn-cxx/encoding/block-helpers.hpp"
+#include "ndn-cxx/encoding/encoding-buffer.hpp"
+#include "ndn-cxx/tag.hpp"
+
+namespace ndn {
+namespace lp {
+
+/**
+ * \brief represents a GeoTag header field
+ */
+class GeoTag : public Tag
+{
+public:
+  static constexpr int
+  getTypeId() noexcept
+  {
+    return 0x60000001;
+  }
+
+  GeoTag() = default;
+
+  explicit
+  GeoTag(std::tuple<double, double, double> pos)
+    : m_pos(pos)
+  {
+  }
+
+  explicit
+  GeoTag(const Block& block);
+
+  /**
+   * \brief prepend GeoTag to encoder
+   */
+  template<encoding::Tag TAG>
+  size_t
+  wireEncode(EncodingImpl<TAG>& encoder) const;
+
+  /**
+   * \brief encode GeoTag into wire format
+   */
+  const Block&
+  wireEncode() const;
+
+  /**
+   * \brief get GeoTag from wire format
+   */
+  void
+  wireDecode(const Block& wire);
+
+public: // get & set GeoTag
+  /**
+   * \return position x of GeoTag
+   */
+  std::tuple<double, double, double>
+  getPos() const
+  {
+    return m_pos;
+  }
+
+  /**
+   * \brief set position x
+   */
+  GeoTag*
+  setPosX(std::tuple<double, double, double> pos)
+  {
+    m_pos = pos;
+    return this;
+  }
+
+private:
+  std::tuple<double, double, double> m_pos = {0.0, 0.0, 0.0};
+  mutable Block m_wire;
+};
+
+} // namespace lp
+} // namespace ndn
+
+#endif // NDN_CXX_LP_GEOTAG_HPP
diff --git a/ndn-cxx/lp/tags.hpp b/ndn-cxx/lp/tags.hpp
index 54a189b..603ffab 100644
--- a/ndn-cxx/lp/tags.hpp
+++ b/ndn-cxx/lp/tags.hpp
@@ -24,6 +24,7 @@
 
 #include "ndn-cxx/lp/cache-policy.hpp"
 #include "ndn-cxx/lp/empty-value.hpp"
+#include "ndn-cxx/lp/geo-tag.hpp"
 #include "ndn-cxx/lp/prefix-announcement-header.hpp"
 #include "ndn-cxx/tag.hpp"
 
@@ -79,6 +80,13 @@
  */
 typedef SimpleTag<uint64_t, 0x60000000> HopCountTag;
 
+/** \class GeoTag
+ *  \brief a packet tag for GeoTag field
+ *
+ * This tag can be attached to Interest, Data, Nack.
+ */
+class GeoTag; // 0x60000001, defined directly in geo-tag.hpp
+
 } // namespace lp
 } // namespace ndn
 
diff --git a/ndn-cxx/lp/tlv.hpp b/ndn-cxx/lp/tlv.hpp
index 08678f7..2ac0238 100644
--- a/ndn-cxx/lp/tlv.hpp
+++ b/ndn-cxx/lp/tlv.hpp
@@ -36,6 +36,8 @@
   FragIndex = 82,
   FragCount = 83,
   HopCountTag = 84,
+  GeoTag = 85,
+  GeoTagPos = 85, // inner fields inside GeoTag
   PitToken = 98,
   Nack = 800,
   NackReason = 801,