示例#1
0
 void MessageToNdef(byte[] message)
 {
     /* Parse the received data - it must be an NDEF message */
     if (!Ndef.Parse(message, NdefFound))
     {
         Trace.WriteLine("SNEP Server: got an invalid message");
     }
 }
        public RtdHandoverSelector(byte[] payload, ref byte[] buffer, ref int next_ndef_starting_point) : base(Ndef.NDEF_HEADER_TNF_NFC_RTD_WKN, "Hs")
        {
            /* Take care of Payload	*/
            int  offset     = 1;
            Ndef ndef       = null;
            bool terminated = true;

            if ((payload != null) && (payload.Length > 0))
            {
                version = payload[0];
            }

            if (version < 0x10)
            {
                Trace.WriteLine("Incompatible version: " + String.Format("{0:x02}", version));
                offset = payload.Length - 1;                 /* so that it won't be parsed	*/
            }

            List <RtdAlternativeCarrier> altenative_carriers_list = new List <RtdAlternativeCarrier>();

            while (Ndef.Parse(payload, ref offset, ref ndef, ref terminated))
            {
                if (ndef is RtdAlternativeCarrier)
                {
                    Trace.WriteLine("Got a new Alternative Carrier");
                    altenative_carriers_list.Add((RtdAlternativeCarrier)ndef);
                }

                if (terminated)
                {
                    break;
                }
            }
            _alternative_carriers = altenative_carriers_list.ToArray();


            /* Take care of following NDEFs	*/
            terminated = true;
            List <AbsoluteUri> related_absolute_uris_list = new List <AbsoluteUri>();

            while (Ndef.Parse(buffer, ref next_ndef_starting_point, ref ndef, ref terminated))
            {
                if (ndef is AbsoluteUri)
                {
                    Trace.WriteLine("Got a new Absolute Uri");
                    related_absolute_uris_list.Add((AbsoluteUri)ndef);
                }

                if (terminated)
                {
                    break;
                }
            }
            _related_absolute_uris = related_absolute_uris_list.ToArray();
        }
        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;
                }
            }
        }
示例#4
0
        /**m* SpringCard.NFC/Ndef.Parse
         *
         * SYNOPSIS
         *   public static bool Parse(byte [] buffer, NdefFoundCallback callback)
         *   public static bool Parse(byte[] buffer, ref int offset, ref Ndef ndef, ref bool terminated)
         *	 public static Ndef[] Parse(byte[] buffer)
         *
         * DESCRIPTION
         *   Analyses a bytes array to retrieve one or several NDEFs in it
         *
         * SEE ALSO
         *   Ndef.Parse
         *
         **/
        public static bool Parse(byte[] buffer, NdefFoundCallback callback)
        {
            int  offset     = 0;
            Ndef ndef       = null;
            bool terminated = true;

            while (Ndef.Parse(buffer, ref offset, ref ndef, ref terminated))
            {
                if (callback != null)
                {
                    callback(ndef);
                }

                if (terminated)
                {
                    return(true);
                }
            }

            Trace.WriteLine("Parsing failed at offset " + offset);
            return(false);
        }
示例#5
0
        public static Ndef[] Parse(byte[] buffer)
        {
            int         offset     = 0;
            List <Ndef> ndefs      = new List <Ndef>();
            Ndef        ndef       = null;
            bool        terminated = true;

            while (Ndef.Parse(buffer, ref offset, ref ndef, ref terminated))
            {
                ndefs.Add(ndef);

                if (terminated)
                {
                    break;
                }
            }

            if (ndefs.Count == 0)
            {
                return(null);
            }

            return(ndefs.ToArray());
        }
示例#6
0
        protected override bool Read()
        {
            Trace.WriteLine("Reading the NFC Forum type 2 Tag");

            ushort page = 0;

            if (!Recognize(_channel, ref _formatted, ref _formattable, ref _locked))
            {
                return(false);
            }

            CardBuffer buffer = new CardBuffer();

            for (page = 0; page < 256; page += 4)
            {
                byte[] data = ReadBinary(_channel, page, READ_4_PAGES);

                if (data == null)
                {
                    break;
                }

                if (page > 0)
                {
                    bool same_as_header = true;
                    for (int i = 0; i < OFFSET_USER_DATA; i++)
                    {
                        if (data[i] != buffer.GetByte(i))
                        {
                            same_as_header = false;
                            break;
                        }
                    }
                    if (same_as_header)
                    {
                        break;
                    }
                }

                buffer.Append(data);
            }

            Trace.WriteLine("Read " + buffer.Length + "B of data from the Tag");

            _raw_data = buffer.GetBytes();

            _capacity = _raw_data.Length;
            if (_capacity <= OFFSET_USER_DATA)
            {
                _capacity = 0;
                return(false);
            }

            if (!_formatted)
            {
                /* Guess the capacity from the read area */
                if ((_capacity > 64) && !_formatted)
                {
                    /* Drop the 16 last bytes if they are not empty (locks on Mifare UltraLight C) */
                    bool locks_found = false;
                    for (long i = _capacity - 16; i < _capacity; i++)
                    {
                        if (_raw_data[i] != 0)
                        {
                            locks_found = true;
                            break;
                        }
                    }
                    if (locks_found)
                    {
                        Trace.WriteLine("Locks found at the end");
                        _capacity -= 16;
                    }
                }
                _capacity -= OFFSET_USER_DATA;
                Trace.WriteLine("The Tag is not formatted, capacity=" + _capacity + "B");
            }
            else
            {
                /* Read the capacity in the CC */
                _capacity = 8 * _raw_data[14];
                Trace.WriteLine("The Tag is formatted, capacity read from the CC=" + _capacity + "B");
            }

            /* Is the tag empty ? */
            _is_empty = true;
            for (long i = 0; i < _capacity; i++)
            {
                if (_raw_data[OFFSET_USER_DATA + i] != 0)
                {
                    _is_empty = false;
                    break;
                }
            }

            if (_is_empty)
            {
                Trace.WriteLine("The Tag is empty");
                return(true);
            }

            byte[] ndef_data = null;

            if (!ParseUserData(buffer.GetBytes(OFFSET_USER_DATA, -1), ref ndef_data))
            {
                Trace.WriteLine("The parsing of the Tag failed");
                return(false);
            }

            if (ndef_data == null)
            {
                Trace.WriteLine("The Tag doesn't contain a NDEF");
                _is_empty = true;
                return(true);
            }

            _is_empty = false;

            Ndef[] t = Ndef.Parse(ndef_data);
            if (t == null)
            {
                Trace.WriteLine("The NDEF is invalid or unsupported");
                return(false);
            }

            Trace.WriteLine(t.Length + " NDEF record(s) found in the Tag");

            /* This NDEF is the new content of the tag */
            Content.Clear();
            for (int i = 0; i < t.Length; i++)
            {
                Content.Add(t[i]);
            }

            return(true);
        }
示例#7
0
        protected override bool Read()
        {
            long ndef_file_size = 0;

            byte[] buffer;

            if (!Recognize(_channel, ref _locked, ref _max_le, ref _max_lc, ref _ndef_file_id, ref ndef_file_size))
            {
                return(false);
            }

            if (ndef_file_size > 2)
            {
                _capacity = ndef_file_size - 2;
            }
            else
            {
                _capacity = 0;
            }

            _formatted = true;

            _application_selected = true;
            _file_selected        = NDEF_CC_FILE_ID;

            if (!SelectFile(_ndef_file_id))
            {
                Trace.WriteLine("Failed to select the NDEF file");
                return(false);
            }

            buffer = ReadBinary(0, 2);
            if (buffer == null)
            {
                Trace.WriteLine("Failed to read from the NDEF file");
                return(false);
            }

            if ((buffer[0] == 0) && (buffer[1] == 0))
            {
                Trace.WriteLine("Tag is empty");
                _is_empty = true;
                return(true);
            }

            _is_empty = false;

            long ndef_announced_size = (long)(buffer[0] * 0x0100 + buffer[1]);

            if ((ndef_announced_size > (ndef_file_size - 2)) || (ndef_announced_size > 0xFFFF))
            {
                Trace.WriteLine("The NDEF file contains an invalid length");
                return(false);
            }

            byte[] content = new byte[ndef_announced_size];

            ushort offset_in_file    = 2;
            long   offset_in_content = 0;

            while (offset_in_content < content.Length)
            {
                if (_max_le > 254)
                {
                    buffer = ReadBinary(offset_in_file, 254);
                }
                else
                {
                    buffer = ReadBinary(offset_in_file, _max_le);
                }

                if ((buffer == null) || (buffer.Length == 0))
                {
                    buffer = ReadBinary(offset_in_file, 0);
                    if ((buffer == null) || (buffer.Length == 0))
                    {
                        Trace.WriteLine("Failed to read the NDEF file at offset " + offset_in_file);
                        return(false);
                    }
                }



                for (int i = 0; i < buffer.Length; i++)
                {
                    if (offset_in_content >= content.Length)
                    {
                        break;
                    }
                    content[offset_in_content++] = buffer[i];
                }

                offset_in_file += (ushort)buffer.Length;

                if ((offset_in_content >= content.Length) || (offset_in_file >= ndef_file_size))
                {
                    break;
                }
            }

            Ndef[] ndefs = Ndef.Parse(content);

            if (ndefs == null)
            {
                Trace.WriteLine("The NDEF is invalid or unsupported");
                return(true);
            }

            Trace.WriteLine(ndefs.Length + " NDEF record(s) found in the tag");

            /* This NDEF is the new content of the tag */
            Content.Clear();
            for (int i = 0; i < ndefs.Length; i++)
            {
                Content.Add(ndefs[i]);
            }

            return(true);
        }