Ejemplo n.º 1
0
        //
        // Write Multi Ndef Tab
        //

        private void WriteMultNdef(object send, RoutedEventArgs e)
        {
            NdefMessage message = new NdefMessage();

            foreach (Row row in table)
            {
                if (row.Selected.Equals("Text"))
                {
                    NdefTextRecord temp = new NdefTextRecord()
                    {
                        TextEncoding = NdefTextRecord.TextEncodingType.Utf8,
                        LanguageCode = "en",
                        Text         = row.Content ?? ""
                    };
                    message.Add(temp);
                }
                else
                {
                    message.Add(new NdefUriRecord()
                    {
                        Uri = row.Content ?? ""
                    });
                }
            }

            ShowPendingStatus("Waiting for tap");

            Command  cmd           = new WriteCustomNdef((byte)timeout.Value, (bool)lockCheckBox.IsChecked, message);
            Callback repeatCommand = null;
            bool     repeat        = (bool)repeatMultiNdefWrite.IsChecked;
            Action   sendCommand   = () => tappy.SendCommand(cmd, ResponseCallback + repeatCommand);

            repeatCommand = (ResponseFrame frame, Exception exc) =>
            {
                if (repeat)
                {
                    if (CheckForErrorsOrTimeout(frame, exc))
                    {
                        return;
                    }
                    Thread.Sleep(1000);
                    ShowPendingStatus("Waiting for tap");
                    Dispatcher.BeginInvoke(sendCommand);
                }
            };

            tappy.SendCommand(cmd, ResponseCallback + repeatCommand);
        }
Ejemplo n.º 2
0
        private async void DoWriteTag()
        {
            WritingTag = true;
            Result     = "";

            NdefLibrary.Ndef.NdefMessage message = new NdefMessage();
            NdefTextRecord record = new NdefTextRecord();

            record.LanguageCode = "en";
            record.TextEncoding = NdefTextRecord.TextEncodingType.Utf8;
            record.Text         = Message;
            message.Add(record);

            var result = await _writeTask.WriteTag(message);

            if (result.DidSucceed)
            {
                Result = "Wrote Message to tag with ID " + result.NFCTag.Id;
            }
            else
            {
                switch (result.ReasonForFailure)

                {
                case FailureReasons.TagReadOnly:
                    Result = "The tag was read only";
                    break;

                case FailureReasons.TagTooSmall:
                    Result = "The tag was too small for this message";

                    break;

                case FailureReasons.ErrorDuringWrite:
                    Result = "An error occured whislt trying to write the tag";

                    break;

                case FailureReasons.UnableToFormatTag:
                    Result = "The tag was not formatted. Please format it first";

                    break;

                case FailureReasons.Unkown:
                    Result = "An unkown error occured";

                    break;

                case FailureReasons.TagLostDuringWrite:
                    Result = "The tag was removed whilst it was been written to";

                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            WritingTag = false;
        }
Ejemplo n.º 3
0
        public static NdefMessage makeTextNDEFRecord(string NDEFPayload)
        {
            var ndefRecord = new NdefTextRecord()
            {
                Text = NDEFPayload
            };

            NdefMessage ndefMessage = new NdefMessage();

            ndefMessage.Add(ndefRecord);
            return(ndefMessage);
        }
Ejemplo n.º 4
0
        private async void DoWriteTag()
        {

            WritingTag = true;
            Result = "";
            
            NdefLibrary.Ndef.NdefMessage message = new NdefMessage();
            NdefTextRecord record = new NdefTextRecord();
            record.LanguageCode = "en";
            record.TextEncoding = NdefTextRecord.TextEncodingType.Utf8;
            record.Text = Message;
            message.Add(record);

            var result = await _writeTask.WriteTag(message);
            if (result.DidSucceed)
            {
                Result = "Wrote Message to tag with ID " + result.NFCTag.Id;
            }
            else
            {
                switch(result.ReasonForFailure)

                {                    
                    case FailureReasons.TagReadOnly:
                        Result = "The tag was read only";
                        break;
                    case FailureReasons.TagTooSmall:
                        Result = "The tag was too small for this message";

                        break;
                    case FailureReasons.ErrorDuringWrite:
                        Result = "An error occured whislt trying to write the tag";

                        break;
                    case FailureReasons.UnableToFormatTag:
                        Result = "The tag was not formatted. Please format it first";

                        break;
                    case FailureReasons.Unkown:
                        Result = "An unkown error occured";

                        break;
                    case FailureReasons.TagLostDuringWrite:
                        Result = "The tag was removed whilst it was been written to";

                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }
            WritingTag = false;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Reverse function to parseRecords() - this one takes
        /// the information stored in the individual record instances and assembles
        /// it into the payload of the base class.
        /// </summary>
        /// <remarks>
        /// As the URI is mandatory, the payload will not be assembled
        /// if no URI is defined.
        /// </remarks>
        /// <returns>Whether assembling the payload was successful.</returns>
        private bool AssemblePayload()
        {
            // Uri is mandatory - don't assemble the payload if it's not set
            if (RecordUri == null) return false;

            // URI (mandatory)
            var message = new NdefMessage { RecordUri };

            // Title(s) (optional)
            if (Titles != null && Titles.Count > 0)
                message.AddRange(Titles);

            // Action (optional)
            if (ActionInUse())
                message.Add(_recordAction);

            // Size (optional)
            if (SizeInUse())
                message.Add(_recordSize);

            // Mime Type (optional)
            if (MimeTypeInUse())
                message.Add(_recordMimeType);

            // Image (optional)
            if (ImageInUse())
                message.Add(_recordImage);

            SetPayloadAndParse(message.ToByteArray(), false);

            return true;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Create the payload based on the data stored in the properties. Usually called
        /// automatically when changing properties, but you might need to call this manually
        /// if you modify the list of alternative carrier records directly without using
        /// accessor method provided by this class.
        /// </summary>
        /// <exception cref="NdefException">Thrown if unable to assemble the payload.
        /// The exception message contains further details about the issue.</exception>
        public void AssemblePayload()
        {
            if (_handoverVersion == null)
            {
                throw new NdefException(NdefExceptionMessages.ExHandoverInvalidVersion);
            }

            // Convert child records to message
            var childMsg = new NdefMessage();
            if (_handoverAlternativeCarrierRecords != null) childMsg.AddRange(_handoverAlternativeCarrierRecords);
            if (_handoverErrorRecord != null) childMsg.Add(_handoverErrorRecord);

            var childBytes = childMsg.ToByteArray();

            var newPayload = new byte[childBytes.Length + 1];

            // Frist byte: handover version
            newPayload[0] = _handoverVersion.Version;

            // Rest of the payload: child message containing Alternative Carrier records + Error record
            Array.Copy(childBytes, 0, newPayload, 1, childBytes.Length);

            _payload = newPayload;
        }
Ejemplo n.º 7
0
        private bool writeCard(string mac)
        {
            NdefMessage msg    = new NdefMessage();
            NdefRecord  rec_hs = new NdefRecord();              //Handover(static)
            NdefRecord  rec_ac = new NdefRecord();              //rec_hsのペイロードになる
            NdefRecord  rec_cr = new NdefRecord();              //Config Record

            byte[] TYPE_HS    = { (byte)'H', (byte)'s' };
            byte[] TYPE_AC    = { (byte)'a', (byte)'c' };
            byte[] TYPE_CR_BT =
            {
                0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f,
                //a   p     p     l     i     c     a     t     i     o     n     /
                0x76, 0x6e, 0x64, 0x2e, 0x62, 0x6c, 0x75, 0x65, 0x74, 0x6f, 0x6f, 0x74, 0x68, 0x2e, 0x65, 0x70, 0x2e, 0x6f, 0x6f, 0x62
                //v   n     d     .     b     l     u     e     t     o     o     t     h     .     e     p     .     o     o     b
            };

            rec_hs.MB = true;
            rec_hs.ME = false;
            rec_hs.setType(NdefRecord.TNF_TYPE.WKS, TYPE_HS);

            rec_ac.MB = true;
            rec_ac.ME = true;
            rec_ac.setType(NdefRecord.TNF_TYPE.WKS, TYPE_AC);
            rec_ac.Payload = new byte[] { 0x01, 0x01, (byte)'0', 0x00 };
            byte[] rec_ac_byte = rec_ac.getRecord();

            byte[] hs_pl = new byte[1 + rec_ac_byte.Length];
            hs_pl[0] = 0x12;                            //version
            Buffer.BlockCopy(rec_ac_byte, 0, hs_pl, 1, rec_ac_byte.Length);
            rec_hs.Payload = hs_pl;

            rec_cr.MB = false;
            rec_cr.ME = true;
            rec_cr.setType(NdefRecord.TNF_TYPE.MIME, TYPE_CR_BT);
            rec_cr.ID = new byte[] { (byte)'0' };

            byte[] mac_byte = new byte[6];
            for (int i = 0; i < 6; i++)
            {
                mac_byte[i] = Convert.ToByte(mac.Substring((5 - i) * 2, 2), 16);
            }

            string     localname = "hiro99ma";
            List <TLV> tlvs      = new List <TLV> {
                new TLV(0x0d, new byte[] { 0x80, 0x06, 0x04 }),
                new TLV(0x03, new byte[] { 0x01, 0x11 }),
                new TLV(0x09, Encoding.ASCII.GetBytes(localname))
            };
            int tlvs_len = 0;

            foreach (TLV tlv in tlvs)
            {
                tlvs_len += tlv.getLength();
            }
            byte[] tlvs_byte = new byte[2 + 6 + tlvs_len];
            int    pos       = 0;

            //OOB LEN
            pos += 2;

            //MAC
            Buffer.BlockCopy(mac_byte, 0, tlvs_byte, pos, mac_byte.Length);
            pos += 6;

            //TLV
            foreach (TLV tlv in tlvs)
            {
                byte[] tlv_byte = tlv.get();
                Buffer.BlockCopy(tlv_byte, 0, tlvs_byte, pos, tlv_byte.Length);
                pos += tlv_byte.Length;
            }

            //OOB LEN
            ushort oob_len = (ushort)(pos - 2);

            tlvs_byte[0] = (byte)(oob_len & 0xff);
            tlvs_byte[1] = (byte)(oob_len >> 8);

            rec_cr.Payload = tlvs_byte;

            int len2 = rec_hs.getLength() + rec_cr.getLength();

            msg.Add(rec_hs);
            msg.Add(rec_cr);

            byte[] msg_byte = msg.getMessage();

            bool ret = false;

            //ユーザブロック
            int blocks = (msg_byte.Length + nfc.BLOCK_SIZE - 1) / nfc.BLOCK_SIZE;

            for (ushort blk = 0; blk < blocks; blk++)
            {
                ret = mLite.Write(msg_byte, (ushort)(1 + blk), 16 * blk);
            }

            //MCブロック
            if (ret && (mWriteMc != null))
            {
                ret = mLite.Write(mWriteMc, FelicaLite.BLOCK_MC);
            }

            //Type3ヘッダ
            if (ret)
            {
                ret = writeType3Head(msg_byte.Length);
            }

            return(ret);
        }
Ejemplo n.º 8
0
        private bool writeCard(string ssid, string key, int auth, int enc, string mac)
        {
            //Authentication Type:インデックス値→WPS値
            byte auth_val;

            switch (auth)
            {
            case 0:             //OPEN
                auth_val = 0x01;
                break;

            case 1:             //WPA/PSK
                auth_val = 0x02;
                break;

            case 2:             //SHARED
                auth_val = 0x04;
                break;

            case 3:             //WPA
                auth_val = 0x08;
                break;

            case 4:             //WPA2
                auth_val = 0x10;
                break;

            case 5:             //WPA2/PSK
                auth_val = 0x20;
                break;

            default:
                auth_val = 0x00;
                break;
            }

            //Encryption Type:インデックス値→WPS値
            byte enc_val;

            switch (enc)
            {
            case 0:             //NONE
                enc_val = 0x01;
                break;

            case 1:             //WEP
                enc_val = 0x02;
                break;

            case 2:             //TKIP
                enc_val = 0x04;
                break;

            case 3:             //AES
                enc_val = 0x08;
                break;

            default:
                enc_val = 0x00;
                break;
            }

            NdefMessage msg    = new NdefMessage();
            NdefRecord  rec_hs = new NdefRecord();              //Handover(static)
            NdefRecord  rec_ac = new NdefRecord();              //rec_hsのペイロードになる
            NdefRecord  rec_cr = new NdefRecord();              //Config Record

            byte[] TYPE_HS      = { (byte)'H', (byte)'s' };
            byte[] TYPE_AC      = { (byte)'a', (byte)'c' };
            byte[] TYPE_CR_WIFI =
            {
                0x61, 0x70, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f,
                //a   p     p     l     i     c     a     t     i     o     n     /
                0x76, 0x6e, 0x64, 0x2e, 0x77, 0x66, 0x61, 0x2e, 0x77, 0x73, 0x63,
                //v   n     d     .     w     f     a     .     w     s     c
            };

            rec_hs.MB = true;
            rec_hs.ME = false;
            rec_hs.setType(NdefRecord.TNF_TYPE.WKS, TYPE_HS);

            rec_ac.MB = true;
            rec_ac.ME = true;
            rec_ac.setType(NdefRecord.TNF_TYPE.WKS, TYPE_AC);
            rec_ac.Payload = new byte[] { 0x01, 0x01, (byte)'0', 0x00 };
            byte[] rec_ac_byte = rec_ac.getRecord();

            byte[] hs_pl = new byte[1 + rec_ac_byte.Length];
            hs_pl[0] = 0x12;                            //version
            Buffer.BlockCopy(rec_ac_byte, 0, hs_pl, 1, rec_ac_byte.Length);
            rec_hs.Payload = hs_pl;

            rec_cr.MB = false;
            rec_cr.ME = true;
            rec_cr.setType(NdefRecord.TNF_TYPE.MIME, TYPE_CR_WIFI);
            rec_cr.ID = new byte[] { (byte)'0' };

            byte[] ssid_byte = System.Text.Encoding.UTF8.GetBytes(ssid);
            byte[] key_byte  = System.Text.Encoding.UTF8.GetBytes(key);
            byte[] mac_byte  = new byte[6];
            for (int i = 0; i < 6; i++)
            {
                mac_byte[i] = Convert.ToByte(mac.Substring(i * 2, 2), 16);
            }
            List <TLV> tlv_cred = new List <TLV> {
                new TLV(0x1026, 0x0001, new byte[] { 0x01 }),
                new TLV(0x1045, (ushort)ssid_byte.Length, ssid_byte),
                new TLV(0x1003, 0x0002, new byte[] { 0x00, auth_val }),
                new TLV(0x100f, 0x0002, new byte[] { 0x00, enc_val }),
                new TLV(0x1027, (ushort)key.Length, key_byte),
                new TLV(0x1020, 0x0006, mac_byte)
            };
            int tlv_cred_len = 0;

            foreach (TLV tlv in tlv_cred)
            {
                tlv_cred_len += tlv.getLength();
            }
            byte[] tlv_cred_byte = new byte[tlv_cred_len];
            int    pos           = 0;

            foreach (TLV tlv in tlv_cred)
            {
                byte[] tlv_byte = tlv.get();
                Buffer.BlockCopy(tlv_byte, 0, tlv_cred_byte, pos, tlv_byte.Length);
                pos += tlv_byte.Length;
            }

            List <TLV> tlvs = new List <TLV> {
                new TLV(0x104a, 0x0001, new byte[] { 0x10 }),
                new TLV(0x100e, (ushort)tlv_cred_byte.Length, tlv_cred_byte),
                new TLV(0x1049, 0x0006, new byte[] { 0x00, 0x37, 0x2a, 0x00, 0x01, 0x20 })
            };
            int tlvs_len = 0;

            foreach (TLV tlv in tlvs)
            {
                tlvs_len += tlv.getLength();
            }
            byte[] tlvs_byte = new byte[tlvs_len];
            pos = 0;
            foreach (TLV tlv in tlvs)
            {
                byte[] tlv_byte = tlv.get();
                Buffer.BlockCopy(tlv_byte, 0, tlvs_byte, pos, tlv_byte.Length);
                pos += tlv_byte.Length;
            }
            rec_cr.Payload = tlvs_byte;

            int len2 = rec_hs.getLength() + rec_cr.getLength();

            //Nexus7のバグなのか知らないが、113~127byteのNDEFデータは読めない
            if ((113 <= len2) && (len2 <= 127))
            {
                MessageBox.Show("Nexus7 cannot Read this NDEF data.\nSo padding with Empty NDEF Record");

                //MEはTNF_EMPTYにやらせる
                //msg.Add()の時点でバイトデータに変換しているので、それより前にfalse設定する
                rec_cr.ME = false;
            }

            msg.Add(rec_hs);
            msg.Add(rec_cr);
            if (!rec_cr.ME)
            {
                //とりあえず15byte足せば、113~127byteの範囲ではなくなるので逃げられると思う
                NdefRecord empty1 = new NdefRecord();                   //3byte
                NdefRecord empty2 = new NdefRecord();                   //3byte

                empty2.ME = true;

                //3byte x 5 = 15byte
                msg.Add(empty1);
                msg.Add(empty1);
                msg.Add(empty1);
                msg.Add(empty1);
                msg.Add(empty2);
            }

            byte[] msg_byte = msg.getMessage();

            bool ret = false;

            //ユーザブロック
            int blocks = (msg_byte.Length + nfc.BLOCK_SIZE - 1) / nfc.BLOCK_SIZE;

            for (ushort blk = 0; blk < blocks; blk++)
            {
                ret = mLite.Write(msg_byte, (ushort)(1 + blk), 16 * blk);
            }

            //MCブロック
            if (ret && (mWriteMc != null))
            {
                ret = mLite.Write(mWriteMc, FelicaLite.BLOCK_MC);
            }

            //Type3ヘッダ
            if (ret)
            {
                ret = writeType3Head(msg_byte.Length);
                //ret = writeType3Head(128);
            }

            return(ret);
        }