public RtdSmartPoster(byte[] payload) : base(Ndef.NDEF_HEADER_TNF_NFC_RTD_WKN, "Sp")
        {
            int  offset     = 0;
            Ndef ndef       = null;
            bool terminated = true;

            while (Ndef.Parse(payload, ref offset, ref ndef, ref terminated))
            {
                if (ndef is RtdUri)
                {
                    Trace.WriteLine("Got a new URI");
                    Uri = (RtdUri)ndef;
                }
                else
                if (ndef is RtdText)
                {
                    Trace.WriteLine("Got a new Text");
                    Title.Add((RtdText)ndef);
                }
                else
                if (ndef is RtdSmartPosterAction)
                {
                    Trace.WriteLine("Got a new SmartPoster Action");
                    Action = (RtdSmartPosterAction)ndef;
                }
                else
                if (ndef is RtdSmartPosterTargetIcon)
                {
                    Trace.WriteLine("Got a new SmartPoster Icon");
                    TargetIcon = (RtdSmartPosterTargetIcon)ndef;
                }
                else
                if (ndef is RtdSmartPosterTargetType)
                {
                    Trace.WriteLine("Got a new SmartPoster MIME type");
                    TargetType = (RtdSmartPosterTargetType)ndef;
                }
                else
                if (ndef is RtdSmartPosterTargetSize)
                {
                    Trace.WriteLine("Got a new SmartPoster Size");
                    TargetSize = (RtdSmartPosterTargetSize)ndef;
                }
                else
                {
                    Trace.WriteLine("Got an unknown child in the SmartPoster");
                }

                if (terminated)
                {
                    break;
                }
            }
        }
Beispiel #2
0
        public static bool Parse(byte[] buffer, ref int offset, ref Ndef ndef, ref bool terminated)
        {
            if (offset > buffer.Length)
            {
                return(false);
            }

            terminated = false;
            ndef       = null;

            /*  Header */
            if (offset + 1 > buffer.Length)
            {
                Trace.WriteLine("NDEF truncated after 'Header' byte");
                return(false);
            }
            byte header = buffer[offset++];

            if (header == 0)
            {
                Trace.WriteLine("Empty byte?");
                return(false);
            }

            /* Type length		*/
            if (offset + 1 > buffer.Length)
            {
                Trace.WriteLine("NDEF truncated after 'Type Length' byte");
                return(false);
            }
            int type_length = buffer[offset++];

            /* Payload length	*/
            int payload_length = 0;

            if ((header & NDEF_HEADER_SHORT_RECORD) != 0)
            {
                if (offset + 1 > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Payload Length' byte");
                    return(false);
                }
                payload_length = buffer[offset++];
            }
            else
            {
                if (offset + 4 > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Payload Length' dword");
                    return(false);
                }
                payload_length  = buffer[offset++]; payload_length *= 0x00000100;
                payload_length += buffer[offset++]; payload_length *= 0x00000100;
                payload_length += buffer[offset++]; payload_length *= 0x00000100;
                payload_length += buffer[offset++];
            }

            /*  ID Length			*/
            int id_length = 0;

            if ((header & NDEF_HEADER_ID_LENGTH_PRESENT) != 0)
            {
                if (offset + 1 > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'ID Length' byte");
                    return(false);
                }
                id_length = buffer[offset++];
            }

            /* Type */
            byte[] type = null;
            if (type_length > 0)
            {
                if (offset + type_length > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Type' bytes");
                    return(false);
                }
                type = new byte[type_length];
                for (int i = 0; i < type_length; i++)
                {
                    type[i] = buffer[offset++];
                }
            }

            /* ID */
            byte[] id = null;
            if (id_length > 0)
            {
                if (offset + id_length > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'ID' bytes");
                    return(false);
                }
                id = new byte[id_length];
                for (int i = 0; i < id_length; i++)
                {
                    id[i] = buffer[offset++];
                }
            }

            /* Payload */
            byte[] payload = null;
            if (payload_length > 0)
            {
                if (offset + payload_length > buffer.Length)
                {
                    Trace.WriteLine("NDEF truncated after 'Payload' bytes");
                    return(false);
                }
                payload = new byte[payload_length];
                for (int i = 0; i < payload_length; i++)
                {
                    payload[i] = buffer[offset++];
                }
            }

            /* OK */
            string type_s = CardBuffer.StringFromBytes(type);

            switch (header & NDEF_HEADER_TNF_MASK)
            {
            case NDEF_HEADER_TNF_EMPTY:
                break;

            case NDEF_HEADER_TNF_NFC_RTD_WKN:
                if (type_s.Equals("Sp"))
                {
                    Trace.WriteLine("Found a SmartPoster");
                    ndef = new RtdSmartPoster(payload);
                }
                else
                if (type_s.Equals("U"))
                {
                    Trace.WriteLine("Found an URI");
                    ndef = new RtdUri(payload);
                }
                else
                if (type_s.Equals("T"))
                {
                    Trace.WriteLine("Found a Text");
                    ndef = new RtdText(payload);
                }
                else
                if (type_s.Equals("act"))
                {
                    Trace.WriteLine("Found an Action");
                    ndef = new RtdSmartPosterAction(payload);
                }
                else
                if (type_s.Equals("s"))
                {
                    Trace.WriteLine("Found a Size");
                    ndef = new RtdSmartPosterTargetSize(payload);
                }
                else
                if (type_s.Equals("t"))
                {
                    Trace.WriteLine("Found a MIME-Type");
                    ndef = new RtdSmartPosterTargetType(payload);
                }
                else
                if (type_s.Equals("Hs"))
                {
                    Trace.WriteLine("Found a Handover Selector");
                    ndef = new RtdHandoverSelector(payload, ref buffer, ref offset);
                }
                else
                if (type_s.Equals("ac"))
                {
                    Trace.WriteLine("Found a Alternative Carrier");
                    ndef = new RtdAlternativeCarrier(payload);
                }
                else
                {
                    Trace.WriteLine("Found an unknown RTD : " + type_s);
                }
                break;

            case NDEF_HEADER_TNF_MEDIA_TYPE:
                if (type_s.ToLower().Equals("text/x-vcard"))
                {
                    Trace.WriteLine("Found a vCard");
                    ndef = new RtdVCard(payload);
                }
                else
                {
                    Trace.WriteLine("Found a MIME Media : " + type_s);
                    ndef = new RtdMedia(type_s, payload);
                }
                break;

            case NDEF_HEADER_TNF_ABSOLUTE_URI:
                if (type_s.Equals("U"))
                {
                    Trace.WriteLine("Found an absolute URI");
                    ndef = new AbsoluteUri(id, payload);
                }
                break;

            case NDEF_HEADER_TNF_NFC_RTD_EXT:
                Trace.WriteLine("Found TNF urn:nfc:ext");
                break;

            case NDEF_HEADER_TNF_UNKNOWN:
                Trace.WriteLine("Found TNF unknown");
                break;

            case NDEF_HEADER_TNF_UNCHANGED:
                Trace.WriteLine("Found TNF unchanged");
                break;

            case NDEF_HEADER_TNF_RESERVED:
                Trace.WriteLine("Found TNF reserved");
                break;

            default:
                return(false);                        // Invalid
            }

            if (ndef == null)
            {
                ndef = new Ndef(header, type, id, payload);
            }

            if (offset >= buffer.Length)
            {
                Trace.WriteLine("Done!");
                terminated = true;
            }

            return(true);
        }