public RtdHandoverSelector(RtdAlternativeCarrier alternative_carrier, AbsoluteUri absolute_uri) : base(Ndef.NDEF_HEADER_TNF_NFC_RTD_WKN, "Hs")
        {
            version = 0x12;

            _alternative_carriers    = new RtdAlternativeCarrier[1];
            _alternative_carriers[0] = alternative_carrier;

            _related_absolute_uris    = new AbsoluteUri[1];
            _related_absolute_uris[0] = absolute_uri;
        }
Ejemplo n.º 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);
        }