示例#1
0
        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));
        }
示例#2
0
        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;
                }
            }
        }
示例#3
0
        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);
        }
示例#4
0
        public static byte[] prepare_initiator_data(nfc_modulation nm)/*, out ppbtInitiatorData ,// out int pszInitiatorData  //-- ppbtInitiatorData.Length --*/
        {
            //pszInitiatorData = 0;
            byte[] ppbtInitiatorData = null;
            switch (nm.nmt)
            {
            case nfc_modulation_type.NMT_ISO14443B:
            {
                // Application Family Identifier (AFI) must equals 0x00 in order to wakeup all ISO14443-B PICCs (see ISO/IEC 14443-3)

                ppbtInitiatorData = new byte[1] {
                    0x00
                };
                //pszInitiatorData = 1;
            }
            break;

            case nfc_modulation_type.NMT_ISO14443BI:
            {
                // APGEN
                ppbtInitiatorData = new byte[4] {
                    0x01, 0x0b, 0x3f, 0x80
                };
                //pszInitiatorData = 4;
            }
            break;

            case nfc_modulation_type.NMT_ISO14443B2SR:
            {
                // Get_UID
                ppbtInitiatorData = new byte[1] {
                    0x0b
                };
                //pszInitiatorData = 1;
            }
            break;

            case nfc_modulation_type.NMT_ISO14443B2CT:
            {
                // SELECT-ALL
                ppbtInitiatorData = new byte[3] {
                    0x9F, 0xFF, 0xFF
                };
                //pszInitiatorData = 3;
            }
            break;

            case nfc_modulation_type.NMT_FELICA:
            {
                // polling payload must be present (see ISO/IEC 18092 11.2.2.5)
                ppbtInitiatorData = new byte[5] {
                    0x00, 0xff, 0xff, 0x01, 0x00
                };
                //pszInitiatorData = 5;
            }
            break;

            case nfc_modulation_type.NMT_ISO14443A:
            case nfc_modulation_type.NMT_JEWEL:
            case nfc_modulation_type.NMT_DEP:
                ppbtInitiatorData = null;
                //pszInitiatorData = 0;
                break;
            }
            return(ppbtInitiatorData);
        }