Esempio n. 1
0
        //NfcTarget INfcInitiator.Poll(NfcModulation[] modulations, byte pollNr, byte period)
        //{
        //    this.SetProperty(NfcProperty.NP_ACTIVATE_FIELD, false);
        //    this.SetProperty(NfcProperty.NP_HANDLE_CRC, true);
        //    this.SetProperty(NfcProperty.NP_HANDLE_PARITY, true);
        //    this.SetProperty(NfcProperty.NP_AUTO_ISO14443_4, true);
        //    this.SetProperty(NfcProperty.NP_ACTIVATE_FIELD, true);

        //    NfcTarget[] targets = new NfcTarget[10];
        //    GCHandle h = GCHandle.Alloc(targets, GCHandleType.Pinned);
        //    try
        //    {
        //        int count = NativeMethods.initiator_list_passive_targets(this.m_ptr, modulations[0], h.AddrOfPinnedObject(), targets.Length);
        //        if (count < 0)
        //            NfcException.Raise((NfcError)count);
        //        if (count == 0)
        //            return null;
        //    }
        //    finally
        //    {
        //        h.Free();
        //    }
        //}

        NfcTarget INfcInitiator.Poll(NfcModulation[] modulations, byte pollNr, byte period)
        {
            IntPtr nfcTarget = IntPtr.Zero;
            int    count;

            GCHandle gc = GCHandle.Alloc(modulations, GCHandleType.Pinned);

            try
            {
                int size = Marshal.SizeOf <Interop.nfc_target>();
                nfcTarget = Marshal.AllocHGlobal(size);
                Console.WriteLine("Allocated nfcTarget memory. size={0} ptr={1}", size, nfcTarget);
                count = NativeMethods.initiator_poll_target(this.handle, modulations, (uint)modulations.Length, pollNr, period, nfcTarget);
                Console.WriteLine($"episage nfc returned {count}");
            }
            finally
            {
                Marshal.FreeHGlobal(nfcTarget);
                gc.Free();
            }

            if (count < 0)
            {
                Console.WriteLine("LastError={0}", this.LastError);
                NfcException.Raise((NfcError)count);
            }
            if (count == 0)
            {
                return(null);
            }
            NfcTarget target = new NfcTarget(this, nfcTarget);

            return(target);
        }
Esempio n. 2
0
        public static FreefareTag AsFreefareTag(this NfcTarget target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }
            IntPtr ptrTag = NativeMethods.freefare_tag_new(target.Device.DangerousGetHandle(), target.Handle);

            if (ptrTag == IntPtr.Zero)
            {
                Console.WriteLine("A");
                return(null);
            }
            FreefareTag tag = FreefareTag.Build(ptrTag, true, target.Device, target);

            return(tag);
        }
Esempio n. 3
0
        private static void PollAsync(object state)
        {
            PollData  data   = (PollData)state;
            NfcTarget result = null;

            try
            {
                int    i             = 0;
                Action cancelPolling = () =>
                {
                    var error = NativeMethods.abort_command(data.Initiator.DangerousGetHandle());
                };
                data.CancellationToken.Register(cancelPolling);
                while (result == null && !data.CancellationToken.IsCancellationRequested)
                {
                    result = ((INfcInitiator)data.Initiator).Poll(new[] { data.Modulations[i] }, 1, data.Period);
                    i      = (i + 1) % data.Modulations.Length;
                }
                if (data.CancellationToken.IsCancellationRequested)
                {
                    data.Tcs.TrySetCanceled(data.CancellationToken);
                }
                else
                {
                    data.Tcs.TrySetResult(result);
                }
            }
            catch (Exception ex)
            {
                data.Tcs.TrySetException(ex);
            }
            finally
            {
                data.Tcs.TrySetResult(result);
            }
        }
Esempio n. 4
0
 public static bool TasteMifareClassic4k(this NfcTarget target)
 {
     return(NativeMethods.mifare_classic4k_taste(target.Device.DangerousGetHandle(), target.Handle));
 }
Esempio n. 5
0
 public static bool TasteNTag21x(this NfcTarget target)
 {
     return(NativeMethods.ntag21x_taste(target.Device.DangerousGetHandle(), target.Handle));
 }
Esempio n. 6
0
 internal NfcSelectedTarget(IntPtr targetPtr, NfcDevice device) : base(targetPtr, true)
 {
     this.m_device = device;
     this.m_target = new NfcTarget(this.m_device, targetPtr);
 }