public void execute(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice, bool noConnection = false)
        {
            if (noConnection == false)
            {
                BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Can't get device information(errorCode : {0}).", result);
                    return;
                }
            }


            List <KeyValuePair <string, Action <IntPtr, UInt32, bool> > > functionList = getFunctionList(sdkContext, deviceID, isMasterDevice);

            if (functionList.Count > 0)
            {
                int  selection;
                bool running = true;

                while (running)
                {
                    Console.WriteLine("+-----------------------------------------------------------+");
                    int idx = 1;
                    for (; idx <= functionList.Count; ++idx)
                    {
                        Console.WriteLine("|{0,3}. {1,-54}|", idx, functionList[idx - 1].Key);
                    }
                    Console.WriteLine("|{0,3}. Exit                                                  |", idx);
                    Console.WriteLine("+-----------------------------------------------------------+");

                    Console.WriteLine("What would you like to do?");
                    Console.Write(">>>> ");

                    if (Util.GetInput(out selection))
                    {
                        if (selection > 0 && selection <= functionList.Count)
                        {
                            functionList[selection - 1].Value(sdkContext, deviceID, isMasterDevice);
                        }
                        else if (selection == functionList.Count + 1)
                        {
                            running = false;
                        }
                        else
                        {
                            Console.WriteLine("Invalid parameter : {0}", selection);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        void getImageLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            BS2SimpleDeviceInfo deviceInfo;
            int            structSize        = Marshal.SizeOf(typeof(BS2Event));
            UInt16         imageLogEventCode = (UInt16)BS2EventCodeEnum.DEVICE_TCP_CONNECTED;
            BS2EventConfig eventConfig       = Util.AllocateStructure <BS2EventConfig>();

            eventConfig.numImageEventFilter = 1;
            eventConfig.imageEventFilter[0].mainEventCode = (byte)(imageLogEventCode >> 8);
            eventConfig.imageEventFilter[0].scheduleID    = (UInt32)BS2ScheduleIDEnum.ALWAYS;

            Console.WriteLine("Trying to get the device[{0}] information.", deviceID);
            BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Can't get device information(errorCode : {0}).", result);
                return;
            }

            Console.WriteLine("Trying to activate image log.");
            result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext, deviceID, ref eventConfig);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            Console.WriteLine("Trying to clear log for quick test.");
            result = (BS2ErrorCode)API.BS2_ClearLog(sdkContext, deviceID);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            Console.WriteLine("Trying to disconnect device[{0}] for quick test.", deviceID);
            result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext, deviceID);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            Thread.Sleep(500); //waiting for socket close

            Console.WriteLine("Trying to connect device[{0}].", deviceID);
            result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString(), deviceInfo.port, out deviceID);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            IntPtr outEventLogObjs = IntPtr.Zero;
            UInt32 outNumEventLogs = 0;

            result = (BS2ErrorCode)API.BS2_GetLog(sdkContext, deviceID, 0, 1024, out outEventLogObjs, out outNumEventLogs);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            if (outNumEventLogs > 0)
            {
                IntPtr curEventLogObjs = outEventLogObjs;
                for (int idx = 0; idx < outNumEventLogs; idx++)
                {
                    BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, typeof(BS2Event));
                    if (Convert.ToBoolean(eventLog.image))
                    {
                        Console.WriteLine("Trying to get image log[{0}].", eventLog.id);

                        IntPtr imageObj  = IntPtr.Zero;
                        UInt32 imageSize = 0;

                        result = (BS2ErrorCode)API.BS2_GetImageLog(sdkContext, deviceID, eventLog.id, out imageObj, out imageSize);
                        if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                        {
                            Console.WriteLine("Got error({0}).", result);
                        }
                        else
                        {
                            int        written = 0;
                            FileStream file    = new FileStream(String.Format("{0}.jpg", eventLog.id), FileMode.Create, FileAccess.Write);

                            Console.WriteLine("Trying to save image log[{0}].", eventLog.id);
                            WriteFile(file.Handle, imageObj, (int)imageSize, out written, IntPtr.Zero);
                            file.Close();

                            if (written != imageSize)
                            {
                                Console.WriteLine("Got error({0}).", result);
                            }
                            else
                            {
                                Console.WriteLine("Successfully saved the image log[{0}].", eventLog.id);
                                Process.Start(file.Name);
                            }
                        }
                        break;
                    }

                    curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize);
                }

                API.BS2_ReleaseObject(outEventLogObjs);
            }

            eventConfig.numImageEventFilter = 0;

            Console.WriteLine("Trying to deactivate image log.");
            result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext, deviceID, ref eventConfig);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }
        }
        public void writeCard(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            BS2SimpleDeviceInfo deviceInfo;
            BS2SmartCardData    smartCard = Util.AllocateStructure <BS2SmartCardData>();

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Can't get device information(errorCode : {0}).", result);
                return;
            }

            Console.WriteLine("choose the format for the card: [2: SecureCredential(default), 3: AccessOn]");
            Console.Write(">>>> ");
            smartCard.header.cardType = Util.GetInput((byte)BS2CardTypeEnum.SECURE);

            if (Convert.ToBoolean(deviceInfo.pinSupported))
            {
                Console.WriteLine("Do you want to set pin code? [y/N]");
                Console.Write(">>>> ");
                if (!Util.IsNo())
                {
                    Console.WriteLine("Enter the pin code which you want to set");
                    Console.Write(">>>> ");
                    string pinCodeStr = Console.ReadLine();
                    IntPtr pinChar    = Marshal.StringToHGlobalAnsi(pinCodeStr);
                    IntPtr pinCode    = Marshal.AllocHGlobal(BS2Environment.BS2_PIN_HASH_SIZE);
                    //result = (BS2ErrorCode)API.BS2_MakePinCode(sdkContext, pinCodeStr, pinCode);
                    result = (BS2ErrorCode)API.BS2_MakePinCode(sdkContext, pinChar, pinCode);

                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        Console.WriteLine("Got error({0}).", result);
                        Marshal.FreeHGlobal(pinCode);
                        return;
                    }

                    Marshal.Copy(pinCode, smartCard.credentials.pin, 0, BS2Environment.BS2_PIN_HASH_SIZE);
                    Marshal.FreeHGlobal(pinChar);
                    Marshal.FreeHGlobal(pinCode);
                }
            }

            if (Convert.ToBoolean(deviceInfo.fingerSupported))
            {
                Console.WriteLine("How many fingerprint templates do you want to set? [0(default)-{0}]", BS2Environment.BS2_SMART_CARD_MAX_TEMPLATE_COUNT);
                Console.Write(">>>> ");
                smartCard.header.numOfTemplate = Util.GetInput((byte)0);
            }
            else
            {
                smartCard.header.numOfTemplate = 0;
            }

            Array.Clear(smartCard.credentials.templateData, 0, BS2Environment.BS2_SMART_CARD_MAX_TEMPLATE_COUNT * BS2Environment.BS2_FINGER_TEMPLATE_SIZE);
            if (smartCard.header.numOfTemplate > 0)
            {
                Console.WriteLine("Enter the size of template which you want to set. [{0}(default)-{1}]", BS2Environment.BS2_SMART_CARD_MIN_TEMPLATE_SIZE, BS2Environment.BS2_FINGER_TEMPLATE_SIZE);
                Console.Write(">>>> ");
                smartCard.header.templateSize = Util.GetInput((UInt16)BS2Environment.BS2_SMART_CARD_MIN_TEMPLATE_SIZE);

                BS2Fingerprint fingerprint = Util.AllocateStructure <BS2Fingerprint>();
                fingerprint.index = 0;

                UInt32 outquality;
                for (byte idx = 0; idx < smartCard.header.numOfTemplate; ++idx)
                {
                    Console.WriteLine("Place your finger on the device for fingerprint template[{0}] extraction", idx);
                    result = (BS2ErrorCode)API.BS2_ScanFingerprintEx(sdkContext, deviceID, ref fingerprint, 0, (UInt32)BS2FingerprintQualityEnum.QUALITY_STANDARD, (byte)BS2FingerprintTemplateFormatEnum.FORMAT_SUPREMA, out outquality, null);
                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        if (result == BS2ErrorCode.BS_SDK_ERROR_EXTRACTION_LOW_QUALITY || result == BS2ErrorCode.BS_SDK_ERROR_CAPTURE_LOW_QUALITY)
                        {
                            Console.WriteLine("Bad fingerprint quality. Tty agin.");
                            continue;
                        }
                        else
                        {
                            Console.WriteLine("Got error({0}).", result);
                            return;
                        }
                    }

                    // Fixes a problem when issuing AOC cards.
                    //Array.Copy(fingerprint.data, 0, smartCard.credentials.templateData, idx*smartCard.header.templateSize, smartCard.header.templateSize);
                    Array.Copy(fingerprint.data, 0, smartCard.credentials.templateData, idx * BS2Environment.BS2_FINGER_TEMPLATE_SIZE, BS2Environment.BS2_FINGER_TEMPLATE_SIZE);
                }

                Console.WriteLine("Is it duress finger? [0 : Normal(default), 1 : Duress]");
                Console.Write(">>>> ");
                smartCard.header.duressMask = Util.GetInput((byte)BS2FingerprintFlagEnum.NORMAL);
            }
            else
            {
                smartCard.header.templateSize = (UInt16)BS2Environment.BS2_SMART_CARD_MIN_TEMPLATE_SIZE;
                smartCard.header.duressMask   = (byte)BS2FingerprintFlagEnum.NORMAL;
            }

            Console.WriteLine("Enter the issue count which you want to set");
            Console.Write(">>>> ");
            smartCard.header.issueCount = (UInt16)Util.GetInput();

            Console.WriteLine("Enter the card id which you want to write on the card");
            Console.Write(">>>> ");

            UInt64 cardID    = 0;
            string cardIDStr = Console.ReadLine();

            if (cardIDStr.Length > BS2Environment.BS2_CARD_DATA_SIZE)
            {
                Console.WriteLine("Card id should less than {0} words.", BS2Environment.BS2_CARD_DATA_SIZE);
                return;
            }
            else if (!UInt64.TryParse(cardIDStr, out cardID) || cardID == 0)
            {
                Console.WriteLine("Invalid card id");
                return;
            }
            else
            {
                byte[] cardIDArray = BitConverter.GetBytes(cardID);
                if (!BitConverter.IsLittleEndian)
                {
                    Array.Reverse(cardIDArray);
                }

                Array.Clear(smartCard.cardID, 0, BS2Environment.BS2_CARD_DATA_SIZE);

                for (int idx = 0; idx < cardIDArray.Length; ++idx)
                {
                    smartCard.cardID[BS2Environment.BS2_CARD_DATA_SIZE - idx - 1] = cardIDArray[idx];
                }
            }

            if ((BS2CardTypeEnum)smartCard.header.cardType == BS2CardTypeEnum.ACCESS)
            {
                Console.WriteLine("Enter the ID of the access group which you want to remove: [ID_1,ID_2 ...]");
                Console.Write(">>>> ");
                char[]   delimiterChars = { ' ', ',', '.', ':', '\t' };
                string[] accessGroupIDs = Console.ReadLine().Split(delimiterChars);
                int      idx            = 0;
                Array.Clear(smartCard.accessOnData.accessGroupID, 0, BS2Environment.BS2_SMART_CARD_MAX_ACCESS_GROUP_COUNT);

                foreach (string accessGroupID in accessGroupIDs)
                {
                    if (accessGroupID.Length > 0)
                    {
                        UInt16 item;
                        if (UInt16.TryParse(accessGroupID, out item))
                        {
                            smartCard.accessOnData.accessGroupID[idx++] = item;
                            if (idx >= BS2Environment.BS2_SMART_CARD_MAX_ACCESS_GROUP_COUNT)
                            {
                                break;
                            }
                        }
                    }
                }

                Console.WriteLine("Enter start time which you want to set. [default(Today), yyyy-MM-dd HH:mm:ss]");
                Console.Write(">>>> ");
                if (!Util.GetTimestamp("yyyy-MM-dd HH:mm:ss", 0, out smartCard.accessOnData.startTime))
                {
                    return;
                }

                Console.WriteLine("Enter end time which you want to set. [default(Today), yyyy-MM-dd HH:mm:ss]");
                Console.Write(">>>> ");
                if (!Util.GetTimestamp("yyyy-MM-dd HH:mm:ss", 0, out smartCard.accessOnData.endTime))
                {
                    return;
                }

                // card auth mode
                //Console.WriteLine("Enter card authentication mode which you want to set:");
                //Console.WriteLine(" [2: Card only, 3: Card+Biometric, 4: Card+PIN, 5: Card+Biometric/PIN, 6: Card+Biometric+PIN]");
                //Console.Write(">>>> ");
                //smartCard.header.cardAuthMode = Util.GetInput((byte)BS2CardAuthModeEnum.NONE);
            }

            result = computeCRC(smartCard, out smartCard.header.hdrCRC, out smartCard.header.cardCRC);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Can't compute CRC16({0})", result);
            }
            else
            {
                Console.WriteLine("Trying to write card.");
                result = (BS2ErrorCode)API.BS2_WriteCard(sdkContext, deviceID, ref smartCard);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                }
            }
        }
Esempio n. 4
0
        bool SearchAndConnectDevice(ref UInt32 deviceID)
        {
            Console.WriteLine("Trying to broadcast on the network");

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_SearchDevices(sdkContext);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error : {0}.", result);
                return(false);
            }

            IntPtr deviceListObj = IntPtr.Zero;
            UInt32 numDevice     = 0;

            const UInt32 LONG_TIME_STANDBY_7S = 7;

            result = (BS2ErrorCode)API.BS2_SetDeviceSearchingTimeout(sdkContext, LONG_TIME_STANDBY_7S);
            if (BS2ErrorCode.BS_SDK_SUCCESS != result)
            {
                Console.WriteLine("Got error : {0}.", result);
                return(false);
            }

            result = (BS2ErrorCode)API.BS2_GetDevices(sdkContext, out deviceListObj, out numDevice);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error : {0}.", result);
                return(false);
            }

            if (numDevice > 0)
            {
                BS2SimpleDeviceInfo deviceInfo;

                Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
                for (UInt32 idx = 0; idx < numDevice; ++idx)
                {
                    deviceID = Convert.ToUInt32(Marshal.ReadInt32(deviceListObj, (int)idx * sizeof(UInt32)));
                    result   = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo);
                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        Console.WriteLine("Can't get device information(errorCode : {0}).", result);
                        return(false);
                    }

                    Console.WriteLine("[{0, 3:##0}] ==> ID[{1, 10}] Type[{2, 16}] Connection mode[{3}] Ip[{4, 16}] port[{5, 5}]",
                                      idx,
                                      deviceID,
                                      API.productNameDictionary[(BS2DeviceTypeEnum)deviceInfo.type],
                                      (BS2ConnectionModeEnum)deviceInfo.connectionMode,
                                      new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString(),
                                      deviceInfo.port);
                }
                Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
                Console.WriteLine("Please, choose the index of the Device which you want to connect to. [-1: quit]");
                Console.Write(">>>> ");

                deviceID = 0;
                Int32 selection = Util.GetInput();

                if (selection >= 0)
                {
                    if (selection < numDevice)
                    {
                        deviceID = Convert.ToUInt32(Marshal.ReadInt32(deviceListObj, (int)selection * sizeof(UInt32)));
                    }
                    else
                    {
                        Console.WriteLine("Invalid selection[{0}]", selection);
                    }
                }

                API.BS2_ReleaseObject(deviceListObj);
                if (deviceID > 0)
                {
                    Console.WriteLine("Trying to connect to device[{0}]", deviceID);
                    result = (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, deviceID);

                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        Console.WriteLine("Can't connect to device(errorCode : {0}).", result);
                        return(false);
                    }

                    Console.WriteLine(">>>> Successfully connected to the device[{0}].", deviceID);
                    return(true);
                }
            }
            else
            {
                Console.WriteLine("There is no device to launch.");
            }

            return(false);
        }