コード例 #1
0
ファイル: Program.cs プロジェクト: Kebechet/sharp-free-fare
        static void print_nfc_target(nfc_target pnt, bool verbose)
        {
            IntPtr s = IntPtr.Zero;

            SharpNFC.PInvoke.Functions.str_nfc_target(ref s, pnt, verbose);
            Console.WriteLine(Marshal.PtrToStringAnsi(s));
        }
コード例 #2
0
ファイル: NFCDevice.cs プロジェクト: JetYuen/SmartCampus
        public int Pool(List <nfc_modulation> modulation, byte poolCount, byte poolingInterval, out nfc_target nfc_target)
        {
            var target    = new nfc_target();
            var modArr    = modulation.ToArray();
            var intResult = Functions.nfc_initiator_poll_target(DevicePointer, modArr, (uint)modArr.Length, poolCount, poolingInterval, out target);

            nfc_target = target;

            return(intResult);
        }
コード例 #3
0
ファイル: NFCDevice.cs プロジェクト: JetYuen/SmartCampus
        public string str_target(nfc_target nfcTarget)
        {
            string s    = "                                   ";
            int    size = Marshal.SizeOf(nfcTarget);
            IntPtr ptr  = Marshal.AllocHGlobal(size);

            Marshal.StructureToPtr(nfcTarget, ptr, false);
            MvcApplication2.PInvoke.Functions.str_nfc_target(ref s, ptr, false);
            Marshal.FreeHGlobal(ptr);
            return(s);
        }
コード例 #4
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
        /* NFC target: act as tag (i.e. MIFARE Classic) or NFC target device. */

        /** @ingroup target
         * @brief Initialize NFC device as an emulated tag
         * @return Returns received bytes count on success, otherwise returns libnfc's error code
         *
         * @param pnd \a nfc_device struct pointer that represent currently used device
         * @param pnt pointer to \a nfc_target struct that represents the wanted emulated target
         *
         * @note \a pnt can be updated by this function: if you set NBR_UNDEFINED
         * and/or NDM_UNDEFINED (ie. for DEP mode), these fields will be updated.
         *
         * @param[out] pbtRx Rx buffer pointer
         * @param[out] szRx received bytes count
         * @param timeout in milliseconds
         *
         * This function initializes NFC device in \e target mode in order to emulate a
         * tag using the specified \a nfc_target_mode_t.
         * - Crc is handled by the device (NP_HANDLE_CRC = true)
         * - Parity is handled the device (NP_HANDLE_PARITY = true)
         * - Cryto1 cipher is disabled (NP_ACTIVATE_CRYPTO1 = false)
         * - Auto-switching in ISO14443-4 mode is enabled (NP_AUTO_ISO14443_4 = true)
         * - Easy framing is disabled (NP_EASY_FRAMING = false)
         * - Invalid frames are not accepted (NP_ACCEPT_INVALID_FRAMES = false)
         * - Multiple frames are not accepted (NP_ACCEPT_MULTIPLE_FRAMES = false)
         * - RF field is dropped
         *
         * @warning Be aware that this function will wait (hang) until a command is
         * received that is not part of the anti-collision. The RATS command for
         * example would wake up the emulator. After this is received, the send and
         * receive functions can be used.
         *
         * If timeout equals to 0, the function blocks indefinitely (until an error is raised or function is completed)
         * If timeout equals to -1, the default timeout will be used
         */
        public static int nfc_target_init(nfc_device pnd, nfc_target pnt, ref byte[] pbtRx, int szRx, int timeout)
        {
            int res = 0;

            // Disallow invalid frame
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_ACCEPT_INVALID_FRAMES, false)) < 0)
            {
                return(res);
            }
            // Disallow multiple frames
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
            {
                return(res);
            }
            // Make sure we reset the CRC and parity to chip handling.
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_HANDLE_CRC, true)) < 0)
            {
                return(res);
            }
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_HANDLE_PARITY, true)) < 0)
            {
                return(res);
            }
            // Activate auto ISO14443-4 switching by default
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_AUTO_ISO14443_4, true)) < 0)
            {
                return(res);
            }
            // Activate "easy framing" feature by default
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_EASY_FRAMING, true)) < 0)
            {
                return(res);
            }
            // Deactivate the CRYPTO1 cipher, it may could cause problems when still active
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_ACTIVATE_CRYPTO1, false)) < 0)
            {
                return(res);
            }
            // Drop explicitely the field
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_ACTIVATE_FIELD, false)) < 0)
            {
                return(res);
            }
            pnd.last_error = 0;
            return(pnd.driver.target_init(pnd, pnt, ref pbtRx, szRx, timeout));
        }
コード例 #5
0
ファイル: NFCDevice.cs プロジェクト: Kebechet/sharp-free-fare
        public int Pool(List <nfc_modulation> modulation, byte poolCount, byte poolingInterval, out nfc_target nfc_target)
        {
            //var ptrArray = new IntPtr[modulation.Count];
            //for (int i = 0; i < modulation.Count; i++)
            //{
            //    IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(modulation[i]));
            //    Marshal.StructureToPtr(modulation[i], ptr, false);
            //    ptrArray[i] = ptr;
            //}

            var target = new nfc_target();
            //var targetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(target));

            var modArr    = modulation.ToArray();
            var intResult = Functions.nfc_initiator_poll_target(DevicePointer, modArr, (uint)modArr.Length, poolCount, poolingInterval, out target);

            nfc_target = target;

            return(intResult);
        }
コード例 #6
0
ファイル: Global.asax.cs プロジェクト: JetYuen/SmartCampus
        static private void ThreadSmartCard()
        {
            var gpio23 = TinyGPIO.Export(23);

            gpio23.Direction = (GPIODirection)GPIODirection.Out;
            var gpio25 = TinyGPIO.Export(25);

            gpio25.Direction = (GPIODirection)GPIODirection.Out;

            List <string> deviceNameList = new List <string>();
            NFCContext    nfcContext     = new NFCContext();
            NFCDevice     nfcDevice      = nfcContext.OpenDevice(null);

            deviceNameList = nfcContext.ListDeviceNames();
            int                   rtn                = nfcDevice.initDevice();
            nfc_target            nfcTarget          = new nfc_target();
            List <nfc_modulation> nfc_modulationList = new List <nfc_modulation>();
            nfc_modulation        nfcModulation      = new nfc_modulation();

            nfcModulation.nbr = nfc_baud_rate.NBR_106;
            nfcModulation.nmt = nfc_modulation_type.NMT_ISO14443A;
            nfc_modulationList.Add(nfcModulation);

            string currentSignalRStr = null;
            string currentConsoleStr = null;
            string signalRStr;
            string consoleStr;
            string state = "---";
            Person p;

            for (; ;)
            {
                gpio23.Value = 0;

                Thread.Sleep(100);
                rtn = nfcDevice.Pool(nfc_modulationList, 1, 2, out nfcTarget);
                if (rtn < 0)
                {
                    consoleStr   = "NFC Targert Not Found!";
                    signalRStr   = "---";
                    gpio23.Value = 0;
                    gpio25.Value = 0;
                }
                else
                {
                    signalRStr = string.Join(
                        separator: "",
                        values: nfcTarget.nti.abtUid.Take((int)nfcTarget.nti.szUidLen).Select(b => b.ToString("X2").ToLower())
                        );
                    consoleStr = string.Format("NFC Target Found: uid is [{0}]", signalRStr);


                    if (File.Exists("App_Data/people.json"))
                    {
                        using (StreamReader r = new StreamReader("App_Data/people.json"))
                        {
                            string        json  = r.ReadToEnd();
                            List <Person> items = JsonConvert.DeserializeObject <List <Person> >(json);
                            p = items.FirstOrDefault(x => x.Card == signalRStr);
                            Console.WriteLine(p.Name);
                            Console.WriteLine(p.Date);
                            Console.WriteLine(p.PCNum);
                            Console.WriteLine(p.State);

                            NFC.Instance.UpdateDateStatus(p);
                            NFC.Instance.UpdateNameStatus(p);
                            NFC.Instance.UpdatePCNumStatus(p);
                            NFC.Instance.UpdateTimeStatus(p);
                            Thread.Sleep(100);
                        }
                    }
                }

                if (signalRStr != state)
                {
                    if (signalRStr != currentSignalRStr)
                    {
                        NFC.Instance.CardIDCheck(signalRStr);
                        currentSignalRStr = signalRStr;
                        gpio23.Value      = 1;
                        gpio25.Value      = 1;
                        Thread.Sleep(100);
                    }
                    else
                    {
                        gpio23.Value = 0;
                    }
                }
                else
                {
                    gpio23.Value = 0;
                    NFC.Instance.CardIDCheck(signalRStr);
                    currentSignalRStr = signalRStr;
                }

                if (consoleStr != currentConsoleStr)
                {
                    DateTime     now = DateTime.Now;
                    FileStream   ostrm;
                    StreamWriter writer;
                    TextWriter   oldOut = Console.Out;
                    try
                    {
                        ostrm  = new FileStream("Record.txt", FileMode.OpenOrCreate, FileAccess.Write);
                        writer = new StreamWriter(ostrm);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Cannot open Record.txt for writing");
                        Console.WriteLine(e.Message);
                        return;
                    }
                    Console.SetOut(writer);
                    Console.WriteLine(now);
                    Console.WriteLine(consoleStr);
                    Console.SetOut(oldOut);
                    writer.Close();
                    ostrm.Close();
                    Console.WriteLine(now);
                    Console.WriteLine(consoleStr);
                    currentConsoleStr = consoleStr;
                }
            }
        }
コード例 #7
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
 /** @ingroup initiator
  * @brief Check target presence
  * @return Returns 0 on success, otherwise returns libnfc's error code.
  *
  * @param pnd \a nfc_device struct pointer that represent currently used device
  * @param pnt a \a nfc_target struct pointer where desired target information was stored (optionnal, can be \e NULL).
  * This function tests if \a nfc_target (or last selected tag if \e NULL) is currently present on NFC device.
  * @warning The target have to be selected before check its presence
  * @warning To run the test, one or more commands will be sent to target
  */
 public static int nfc_initiator_target_is_present(nfc_device pnd, nfc_target pnt)
 {
     pnd.last_error = 0;
     return(pnd.driver.initiator_target_is_present(pnd, pnt));
 }
コード例 #8
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
 nfc_initiator_select_dep_target(nfc_device pnd, nfc_dep_mode ndm, nfc_baud_rate nbr, nfc_dep_info pndiInitiator, ref nfc_target pnt, int timeout)
 {
     pnd.last_error = 0;
     return(pnd.driver.initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, ref pnt, timeout));
 }
コード例 #9
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
        nfc_initiator_poll_dep_target(nfc_device pnd, nfc_dep_mode ndm, nfc_baud_rate nbr, nfc_dep_info pndiInitiator, nfc_target pnt, int timeout)
        {
            const int period         = 300;
            int       remaining_time = timeout;
            int       res;
            int       result          = 0;
            bool      bInfiniteSelect = pnd.bInfiniteSelect;

            pnt = null;
            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_INFINITE_SELECT, true)) < 0)
            {
                return(res);
            }
            while (remaining_time > 0)
            {
                if ((res = nfc_initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, ref pnt, period)) < 0)
                {
                    if (res != NFC_ETIMEOUT)
                    {
                        result = res;
                        break;
                    }
                }
                if (res == 1)
                {
                    result = res;
                    break;
                }
                remaining_time -= period;
            }
            if (!bInfiniteSelect)
            {
                if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_INFINITE_SELECT, false)) < 0)
                {
                    return(res);
                }
            }
            return(result);
        }
コード例 #10
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
 public static int nfc_initiator_poll_target(nfc_device pnd, nfc_modulation[] pnmTargetTypes, int szModulations, byte uiPollNr, byte uiPeriod, out nfc_target pnt)
 {
     //HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
     pnd.last_error = 0;
     return(pnd.driver.initiator_poll_target(pnd, pnmTargetTypes, szModulations, uiPollNr, uiPeriod, out pnt));
 }
コード例 #11
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
        public static int nfc_initiator_list_passive_targets(nfc_device pnd, nfc_modulation nm, ref nfc_target[] ant /*, int szTargets*/)
        {
            nfc_target nt            = new nfc_target();
            int        szTargetFound = 0;

            byte[] pbtInitData = null;
            //int szInitDataLen = 0;
            int res = 0;

            pnd.last_error = 0;

            // Let the reader only try once to find a tag
            bool bInfiniteSelect = pnd.bInfiniteSelect;

            if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_INFINITE_SELECT, false)) < 0)
            {
                return(res);
            }

            pbtInitData = prepare_initiator_data(nm);
            //szInitDataLen = pbtInitData.Length;
            while (nfc_initiator_select_passive_target(pnd, nm, pbtInitData, pbtInitData.Length, /*out*/ nt) > 0)
            {
                int  i;
                bool seen = false;
                // Check if we've already seen this tag
                for (i = 0; i < szTargetFound; i++)
                {
                    if (MiscTool.CompareType <nfc_target>(ant[i], nt))
                    {
                        seen = true;
                    }
                }
                if (seen)
                {
                    break;
                }

                ant[szTargetFound] = nt;
                szTargetFound++;
                if (ant.Length == szTargetFound)
                {
                    break;
                }
                nfc_initiator_deselect_target(pnd);
                // deselect has no effect on FeliCa and Jewel cards so we'll stop after one...
                // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time
                if ((nm.nmt == nfc_modulation_type.NMT_FELICA) ||
                    (nm.nmt == nfc_modulation_type.NMT_JEWEL) ||
                    (nm.nmt == nfc_modulation_type.NMT_ISO14443BI) ||
                    (nm.nmt == nfc_modulation_type.NMT_ISO14443B2SR) ||
                    (nm.nmt == nfc_modulation_type.NMT_ISO14443B2CT))
                {
                    break;
                }
            }
            if (bInfiniteSelect)
            {
                if ((res = nfc_device_set_property_bool(pnd, nfc_property.NP_INFINITE_SELECT, true)) < 0)
                {
                    return(res);
                }
            }
            return(szTargetFound);
        }
コード例 #12
0
ファイル: nfc.cs プロジェクト: rinscr3003/Libnfc-CSharp
        public static int nfc_initiator_select_passive_target(nfc_device pnd, nfc_modulation nm, byte[] pbtInitData, int szInitData, /*out*/ nfc_target pnt)
        {
            byte[] abtInit = new byte[Math.Max(12, pbtInitData.Length)];
            // byte abtTmpInit[MAX(12, szInitData)];
            int szInit = 0;

            if (pbtInitData == null || szInitData == 0)
            {
                // Provide default values, if any
                abtInit = prepare_initiator_data(nm);
                szInit  = abtInit.Length;
            }
            else if (nm.nmt == nfc_modulation_type.NMT_ISO14443A)
            {
                //abtInit = abtTmpInit;
                iso14443_cascade_uid(pbtInitData, szInitData, ref abtInit, out szInit);
            }
            else
            {
                abtInit = pbtInitData;
                szInit  = szInitData;
            }

            //HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
            pnd.last_error = 0;
            return(pnd.driver.initiator_select_passive_target(pnd, nm, abtInit, szInit, /*out*/ pnt));
        }