From 503d79a89f9a2fbcaddd56df16282ed023875b78 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Tue, 27 Aug 2013 23:18:39 +0200 Subject: [PATCH] import --- import/NDEF/NDEFMessage.cs | 154 +++++++++++++++++++++++++++ import/NDEF/NDEFRecord.cs | 188 +++++++++++++++++++++++++++++++++ import/NDEF/NDEFShortRecord.cs | 63 +++++++++++ import/NFC.xaml | 44 ++++++++ import/NFC.xaml.cs | 127 ++++++++++++++++++++++ 5 files changed, 576 insertions(+) create mode 100644 import/NDEF/NDEFMessage.cs create mode 100644 import/NDEF/NDEFRecord.cs create mode 100644 import/NDEF/NDEFShortRecord.cs create mode 100644 import/NFC.xaml create mode 100644 import/NFC.xaml.cs diff --git a/import/NDEF/NDEFMessage.cs b/import/NDEF/NDEFMessage.cs new file mode 100644 index 0000000..f4038e4 --- /dev/null +++ b/import/NDEF/NDEFMessage.cs @@ -0,0 +1,154 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 21.08.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility.NDEF +{ + using System.Collections.Generic; + using System.IO; + + /// Ndef message. + /// Stubbfel, 21.08.2013. + public class NDEFMessage + { + #region Members + + /// The records. + private List records; + + #endregion + + #region constructors + + /// Initializes a new instance of the NDEFMessage class. + /// Stubbfel, 21.08.2013. + /// The content. + /// The type. + /// (Optional) the tnf. + public NDEFMessage(string content, TYPEVAL type, NDEFRecord.TNFVAL tnf = NDEFRecord.TNFVAL.WKT) + { + this.records = new List(); + float recordsCount = (float)content.Length / NDEFRecord.MaxRecordPayLoad; + NDEFRecord tmpRecord = null; + string praefix = NDEFMessage.GetPraefix(type); + + for (int i = 0; recordsCount > 0; i++) + { + tmpRecord = new NDEFShortRecord(); + tmpRecord.Type = type; + tmpRecord.TNF = tnf; + tmpRecord.PayloadPraefix = praefix; + int recordsize = 255; + if (content.Length < (i + 1) * recordsize) + { + recordsize = content.Length - (i * recordsize); + } + + tmpRecord.Payload = content.Substring(i * 255, recordsize); + if (i == 0) + { + tmpRecord.MB = NDEFRecord.NDEFFlags.MBSET; + } + + this.records.Add(tmpRecord); + recordsCount--; + } + + this.records[this.records.IndexOf(tmpRecord)].ME = NDEFRecord.NDEFFlags.MESET; + } + + /// Initializes a new instance of the NDEFMessage class. + /// Stubbfel, 21.08.2013. + /// The array. + public NDEFMessage(byte[] array) + { + this.records = new List(); + + NDEFRecord tmpRecord = null; + for (int i = 0; i < array.Length; i += tmpRecord.RecordSize) + { + tmpRecord = new NDEFShortRecord(array, i); + + this.records.Add(tmpRecord); + } + } + + #endregion + + #region enum + + /// Values that represent TYPEVAL. + /// Stubbfel, 21.08.2013. + public enum TYPEVAL + { + /// An enum constant representing the empty option. + EMPTY = 0x00, + + /// An enum constant representing the URL option. + URL = 0x55, + + /// An enum constant representing the text option. + TEXT = 0x54, + } + + #endregion + + #region Methods + /// Gets a praefix. + /// Stubbfel, 21.08.2013. + /// The type. + /// The praefix. + public static string GetPraefix(TYPEVAL type) + { + string praefix = string.Empty; + + switch (type) + { + case TYPEVAL.TEXT: + praefix = "\x02" + "de"; + break; + case TYPEVAL.URL: + praefix = "\x01"; + break; + default: + break; + } + + return praefix; + } + + /// Gets the content. + /// Stubbfel, 21.08.2013. + /// The content. + public string GetContent() + { + string result = string.Empty; + + foreach (NDEFRecord record in this.records) + { + result += record.Payload; + } + + return result; + } + + /// Converts this object to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. + public byte[] ToByteArray() + { + MemoryStream ms = new MemoryStream(); + foreach (NDEFRecord record in this.records) + { + ms.Write(record.ToByteArray(), 0, record.RecordSize); + } + + return ms.ToArray(); + } + + #endregion + } +} diff --git a/import/NDEF/NDEFRecord.cs b/import/NDEF/NDEFRecord.cs new file mode 100644 index 0000000..8d3a0c7 --- /dev/null +++ b/import/NDEF/NDEFRecord.cs @@ -0,0 +1,188 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 21.08.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility.NDEF +{ + using System.IO; + using System.Text; + + /// Ndef record of a NDEFMessage. + /// Stubbfel, 21.08.2013. + public abstract class NDEFRecord + { + #region Members + + /// The maximum record pay load. + public const int MaxRecordPayLoad = 255; + + /// Size of the type. + protected const byte TypeSize = 0x01; + + #endregion + + #region Constructors + /// Initializes a new instance of the NDEFRecord class. + /// Stubbfel, 21.08.2013. + public NDEFRecord() + { + } + + /// Initializes a new instance of the NDEFRecord class. + /// Stubbfel, 21.08.2013. + /// The array. + /// (Optional) zero-based index of the. + public NDEFRecord(byte[] array, int index = 0) + { + this.FormatFlags = array[index]; + } + + #endregion + + #region enum + + /// Values that represent NDEFFlags. + /// Stubbfel, 21.08.2013. + public enum NDEFFlags + { + /// An Enum constant representing the UNSET option. + UNSET = 0x00, + + /// An Enum constant representing the Message begin option. + MBSET = 0x80, + + /// An Enum constant representing the Message end option. + MESET = 0x40, + + /// An Enum constant representing the CHUNK FLAG option. + CFSET = 0x20, + + /// An Enum constant representing the Short Record set option. + SRSET = 0x10, + + /// An Enum constant representing the ID length option. + ILSET = 0x08, + + /// An enum constant representing the tnfset option. + TNFSET = 0x03 + } + + /// Values that represent TNFVAL. + /// Stubbfel, 21.08.2013. + public enum TNFVAL + { + /// An enum constant representing the empty option. + EMPTY = 0x00, + + /// An enum constant representing the Well-Know-Type option. + WKT = 0x01, + + /// An enum constant representing the MediaType option. + MEDIATYPE = 0x02, + + /// An enum constant representing the URI option. + URI = 0x03, + + /// An enum constant representing the NFCE option. + NFCE = 0x04, + + /// An enum constant representing the unknow option. + unknow = 0x05, + + /// An enum constant representing the unchanged option. + UNCHANGED = 0x06, + + /// An enum constant representing the reserved option. + RESERVED = 0x07 + } + + #endregion + + #region Properties + /// Gets or sets the MBFlag. + /// The MBFlag. + public NDEFFlags MB { get; set; } + + /// Gets or sets MEFlag. + /// The MEFlag . + public NDEFFlags ME { get; set; } + + /// Gets or sets the CFFlag. + /// The CFFlag. + public NDEFFlags CF { get; set; } + + /// Gets or sets the SRFlag. + /// The SRFlag. + public NDEFFlags SR { get; set; } + + /// Gets or sets the ILFlag. + /// The ILFlag. + public NDEFFlags IL { get; set; } + + /// Gets or sets the TNFField. + /// The TNFField. + public TNFVAL TNF { get; set; } + + /// Gets or sets the type. + /// The type. + public NDEFMessage.TYPEVAL Type { get; set; } + + /// Gets or sets the format flags. + /// The format flags. + public byte FormatFlags + { + get + { + return (byte)((byte)this.TNF | ((byte)this.MB) | ((byte)this.ME) | ((byte)this.CF) | ((byte)this.SR) | ((byte)this.IL)); + } + + protected set + { + this.TNF = (TNFVAL)(value & (byte)NDEFFlags.TNFSET); + this.MB = (NDEFFlags)(value & (byte)NDEFFlags.MBSET); + this.ME = (NDEFFlags)(value & (byte)NDEFFlags.MESET); + this.CF = (NDEFFlags)(value & (byte)NDEFFlags.CFSET); + this.SR = (NDEFFlags)(value & (byte)NDEFFlags.SRSET); + this.IL = (NDEFFlags)(value & (byte)NDEFFlags.ILSET); + } + } + + /// Gets or sets the payload. + /// The payload. + public string Payload { get; set; } + + /// Gets the size of the record. + /// The size of the record. + public int RecordSize + { + get + { + return this.HeaderSize + this.Payload.Length + this.PayloadPraefix.Length; + } + } + + /// Gets or sets the payload praefix. + /// The payload praefix. + public string PayloadPraefix { get; set; } + + /// Gets or sets the size of the header. + /// The size of the header. + protected int HeaderSize { get; set; } + + #endregion + + #region Methods + + /// Converts the record to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. + public abstract byte[] ToByteArray(); + + #endregion + } +} + +// End of Utility\NDEF\NDEFRecord.cs diff --git a/import/NDEF/NDEFShortRecord.cs b/import/NDEF/NDEFShortRecord.cs new file mode 100644 index 0000000..0692789 --- /dev/null +++ b/import/NDEF/NDEFShortRecord.cs @@ -0,0 +1,63 @@ +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 21.08.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility.NDEF +{ + using System.Text; + + /// Ndef short record. + /// Stubbfel, 21.08.2013. + public class NDEFShortRecord : NDEFRecord + { + /// Initializes a new instance of the NDEFShortRecord class. + /// Stubbfel, 21.08.2013. + public NDEFShortRecord() + { + this.HeaderSize = 4; + this.SR = NDEFFlags.SRSET; + this.IL = NDEFFlags.UNSET; + this.CF = NDEFFlags.UNSET; + } + + /// Initializes a new instance of the NDEFShortRecord class. + /// Stubbfel, 21.08.2013. + /// The array. + /// (Optional) zero-based index of the. + public NDEFShortRecord(byte[] array, int index = 0) + : base(array) + { + this.HeaderSize = 4; + this.Type = (NDEFMessage.TYPEVAL)array[index + 3]; + this.PayloadPraefix = NDEFMessage.GetPraefix(this.Type); + int payLoadSize = array[index + 2] - this.PayloadPraefix.Length; + this.Payload = Encoding.UTF8.GetString(array, index + this.HeaderSize + this.PayloadPraefix.Length, payLoadSize); + } + + /// Converts this NDEFShortRecord to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. + public override byte[] ToByteArray() + { + byte[] payloadAr = Encoding.UTF8.GetBytes(this.PayloadPraefix + this.Payload); + byte[] array = new byte[payloadAr.Length + this.HeaderSize]; + + array[0] = this.FormatFlags; + array[1] = NDEFRecord.TypeSize; + array[2] = (byte)(Payload.Length + this.PayloadPraefix.Length); + array[3] = (byte)this.Type; + + int i = this.HeaderSize; + foreach (byte b in payloadAr) + { + array[i] = b; + i++; + } + + return array; + } + } +} diff --git a/import/NFC.xaml b/import/NFC.xaml new file mode 100644 index 0000000..9f7e46c --- /dev/null +++ b/import/NFC.xaml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + +