Ejemplo n.º 1
0
        protected override bool WriteContent(byte[] ndef_content)
        {
            Trace.WriteLine("Writing the NFC Forum type 2 Tag");

            if (ndef_content == null)
            {
                return(false);
            }

            /* Get the new NDEF TLV to store into the Tag */
            NfcTlv ndef_tlv = new NfcTlv(NDEF_MESSAGE_TLV, ndef_content);

            /* Remove the Terminator TLV (if some) */
            while ((_tlvs.Count > 0) && (_tlvs[_tlvs.Count - 1].T == TERMINATOR_TLV))
            {
                _tlvs.RemoveAt(_tlvs.Count - 1);
            }

            /* Where shall I put the NDEF TLV in the Tag ? */
            if (_tlvs.Count == 0)
            {
                _tlvs.Add(ndef_tlv);
            }
            else
            {
                for (int i = 0; i < _tlvs.Count; i++)
                {
                    if (_tlvs[i].T == NDEF_MESSAGE_TLV)
                    {
                        /* Replace this one */
                        _tlvs[i] = ndef_tlv;
                        ndef_tlv = null;
                        break;
                    }
                }

                if (ndef_tlv != null)
                {
                    /* No NDEF in the Tag beforehand? Let's add it the the end */
                    _tlvs.Add(ndef_tlv);
                }
            }

            CardBuffer actual_content = new CardBuffer();

            for (int i = 0; i < _tlvs.Count; i++)
            {
                actual_content.Append(_tlvs[i].Serialize());
            }

            if (actual_content.Length > Capacity())
            {
                Trace.WriteLine("The size of the content (with its TLVs) is bigger than the tag's capacity");
                return(false);
            }

            if ((actual_content.Length + 2) < Capacity())
            {
                /* Add a Terminator at the end */
                Trace.WriteLine("We add a TERMINATOR TLV at the end of the Tag");
                actual_content.Append((new NfcTlv(TERMINATOR_TLV, null)).Serialize());
            }

            /* And now write */
            ushort page = 4;

            for (long i = 0; i < actual_content.Length; i += 4)
            {
                byte[] buffer = new byte[4];

                for (long j = 0; j < 4; j++)
                {
                    if ((i + j) < actual_content.Length)
                    {
                        buffer[j] = actual_content.GetByte(i + j);
                    }
                }

                if (!WriteBinary(_channel, page, buffer))
                {
                    return(false);
                }
                page++;
            }

            return(true);
        }
Ejemplo n.º 2
0
        private bool ParseUserData(byte[] user_data, ref byte[] ndef_data)
        {
            byte[] buffer           = user_data;
            byte[] remaining_buffer = null;

            _tlvs.Clear();

            while (buffer != null)
            {
                NfcTlv tlv = NfcTlv.Unserialize(buffer, ref remaining_buffer);
                buffer = remaining_buffer;

                if (tlv == null)
                {
                    Trace.WriteLine("An invalid content has been found (not a T,L,V)");
                    break;
                }

                switch (tlv.T)
                {
                case NDEF_MESSAGE_TLV:
                    Trace.WriteLine("Found a NDEF TLV");
                    if (ndef_data != null)
                    {
                        Trace.WriteLine("The Tag has already a NDEF, ignoring this one");
                    }
                    else
                    {
                        ndef_data = tlv.V;
                    }
                    break;

                case LOCK_CONTROL_TLV:
                    Trace.WriteLine("Found a LOCK CONTROL TLV");
                    break;

                case MEMORY_CONTROL_TLV:
                    Trace.WriteLine("Found a MEMORY CONTROL TLV");
                    break;

                case PROPRIETARY_TLV:
                    Trace.WriteLine("Found a PROPRIETARY TLV");
                    break;

                case TERMINATOR_TLV:
                    Trace.WriteLine("Found a TERMINATOR TLV");
                    /* After a terminator... we terminate */
                    buffer = null;
                    break;

                case NULL_TLV:
                    /* Terminate here */
                    buffer = null;
                    break;

                default:
                    Trace.WriteLine("Found an unsupported TLV (T=" + tlv.T + ")");
                    return(false);
                }

                if (tlv.T != NULL_TLV)
                {
                    _tlvs.Add(tlv);
                }
            }

            return(true);
        }