From e0d1082a9323e899623de080550e5d1ef3db9121 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 21 Aug 2013 11:50:24 +0200 Subject: [PATCH] add NDEFMessage --- CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj | 4 +- .../Model/Utility/NDEF/NDEFRecord.cs | 92 --------- .../CampusAppWP8/Pages/Dev/NFC.xaml.cs | 13 +- .../CampusAppWP8/Utility/NDEF/NDEFMessage.cs | 86 +++++++++ .../CampusAppWP8/Utility/NDEF/NDEFRecord.cs | 180 ++++++++++++++++++ .../Utility/NDEF/NDEFShortRecord.cs | 60 ++++++ 6 files changed, 336 insertions(+), 99 deletions(-) delete mode 100644 CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs create mode 100644 CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs create mode 100644 CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs create mode 100644 CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index a2653e83..977c79f6 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -116,7 +116,9 @@ - + + + diff --git a/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs b/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs deleted file mode 100644 index a9bfbbff..00000000 --- a/CampusAppWP8/CampusAppWP8/Model/Utility/NDEF/NDEFRecord.cs +++ /dev/null @@ -1,92 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CampusAppWP8.Model.Utility.NDEF -{ - public class NDEFRecord - { - private const byte typeSize = 0x01; - public enum NDEFFlags - { - UNSET = 0x00, - MBSET = 0x80, - MESET = 0x40, - CFSET = 0x20, - SRSET = 0x10, - ILSET = 0x08 - } - - public enum TNFVAL - { - EMPTY = 0x00, - WKT = 0x01, - MEDIATYPE = 0x02, - URI = 0x03, - NFCE = 0x04, - UNKOWN = 0x05, - UNCHANGED = 0x06, - RESERVED = 0x07 - } - - public enum TYPEVAL - { - EMPTY = 0x00, - URL = 0x55, - TEXT = 0x54, - } - - public NDEFRecord() - { - - } - public NDEFRecord(byte[] array) - { - this.Payload = Encoding.UTF8.GetString(array, 4, array.Length - 4); - } - public NDEFFlags MB { get; set; } - public NDEFFlags ME { get; set; } - public NDEFFlags CF { get; set; } - public NDEFFlags SR { get; set; } - public NDEFFlags IL { get; set; } - public TNFVAL TNF { get; set; } - public TYPEVAL Type { get; set; } - public byte FormatFlags - { - get - { - return (byte)((byte)this.TNF | ((byte)this.MB) | ((byte)this.ME) | ((byte)this.CF) | ((byte)this.SR) | ((byte)this.IL)); - } - } - - - public byte Record = 0xD1; - - public string Payload = "nokia.com"; - - public byte[] toByteArray() - { - MemoryStream m = new MemoryStream(); - byte[] payloadAr = Encoding.UTF8.GetBytes(Payload); - byte[] array = new byte[payloadAr.Length + 4]; - - array[0] = this.FormatFlags; - array[1] = NDEFRecord.typeSize; - array[2] = (byte)(Payload.Length); - array[3] = (byte)this.Type; - - int i = 4; - foreach (byte b in payloadAr) - { - array[i] = b; - i++; - } - return array; - } - - - } -} diff --git a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs index 6549e8a8..d5a2fb86 100644 --- a/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/Pages/Dev/NFC.xaml.cs @@ -9,7 +9,7 @@ using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using Windows.Networking.Proximity; using System.Runtime.InteropServices.WindowsRuntime; -using CampusAppWP8.Model.Utility.NDEF; +using CampusAppWP8.Utility.NDEF; namespace CampusAppWP8.Pages.Dev @@ -60,15 +60,16 @@ namespace CampusAppWP8.Pages.Dev this.device.StopSubscribingForMessage(message.SubscriptionId); - NDEFRecord ndef = new NDEFRecord(); + /* NDEFRecord ndef = new NDEFRecord(); ndef.TNF = NDEFRecord.TNFVAL.URI; ndef.MB = NDEFRecord.NDEFFlags.MBSET; ndef.ME = NDEFRecord.NDEFFlags.MESET; ndef.SR = NDEFRecord.NDEFFlags.SRSET; ndef.Type = NDEFRecord.TYPEVAL.TEXT; - ndef.toByteArray(); - ndef.Payload = "test"; - device.PublishBinaryMessage("NDEF:WriteTag", ndef.toByteArray().AsBuffer(), publishHandler); + ndef.ToByteArray(); + ndef.Payload = "test";*/ + NDEFMessage ndef = new NDEFMessage("test", NDEFMessage.TYPEVAL.TEXT); + device.PublishBinaryMessage("NDEF:WriteTag", ndef.ToByteArray().AsBuffer(), publishHandler); } private void publishHandler(ProximityDevice sender, long messageId) @@ -84,7 +85,7 @@ namespace CampusAppWP8.Pages.Dev var NDefMessage = message.Data; byte[] testa = NDefMessage.ToArray(); - NDEFRecord test = new NDEFRecord(testa); + // NDEFRecord test = new NDEFRecord(testa); } } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs new file mode 100644 index 00000000..9dac3ec4 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFMessage.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CampusAppWP8.Utility.NDEF +{ + public class NDEFMessage + { + private List records; + + public NDEFMessage(string content, TYPEVAL type) + { + this.records = new List(); + float recordsCount = content.Length / NDEFRecord.MaxRecordPayLoad; + NDEFRecord tmpRecord = null; + + for (int i = 0; recordsCount > 0; i++) + { + tmpRecord = new NDEFShortRecord(); + tmpRecord.Type = type; + tmpRecord.Payload = content.Substring(i * 255, 255); + if (i == 0) + { + tmpRecord.MB = NDEFRecord.NDEFFlags.MBSET; + } + this.records.Add(tmpRecord); + recordsCount--; + } + + this.records[this.records.IndexOf(tmpRecord)].ME = NDEFRecord.NDEFFlags.MESET; + } + + 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); + } + } + + + /// 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, + } + + public string GetContent() + { + string result = string.Empty; + + foreach (NDEFRecord record in this.records) + { + result += record.Payload; + } + + return result; + } + + public byte[] ToByteArray() + { + + MemoryStream ms = new MemoryStream(); + foreach (NDEFRecord record in this.records) + { + ms.Write(record.ToByteArray(), 0, record.RecordSize); + } + return ms.ToArray(); + } + } +} diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs new file mode 100644 index 00000000..50f9b46e --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFRecord.cs @@ -0,0 +1,180 @@ +//----------------------------------------------------------------------- +// +// 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; + + /// Size of the type. + protected int HeaderSize; + + #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. + 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 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; + } + } + + #endregion + + #region Methods + + /// Converts the record to a byte array. + /// Stubbfel, 21.08.2013. + /// This object as a byte[]. + public abstract byte[] ToByteArray(); + + #endregion + } +} diff --git a/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs new file mode 100644 index 00000000..02796f30 --- /dev/null +++ b/CampusAppWP8/CampusAppWP8/Utility/NDEF/NDEFShortRecord.cs @@ -0,0 +1,60 @@ +//----------------------------------------------------------------------- +// +// 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. + public NDEFShortRecord(byte[] array, int index = 0) + : base(array) + { + this.HeaderSize = 4; + this.Type = (NDEFMessage.TYPEVAL)array[index + 3]; + int payLoadSize = array[index + 2]; + this.Payload = Encoding.UTF8.GetString(array, index + this.HeaderSize, 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(Payload); + byte[] array = new byte[payloadAr.Length + this.HeaderSize]; + + array[0] = this.FormatFlags; + array[1] = NDEFRecord.TypeSize; + array[2] = (byte)(Payload.Length); + array[3] = (byte)this.Type; + + int i = this.HeaderSize; + foreach (byte b in payloadAr) + { + array[i] = b; + i++; + } + return array; + } + } +}