Ejemplo n.º 1
0
        public static byte[] GetCard()
        {
            ServioCardInfo res = new ServioCardInfo {
                ErrorCore = -1, CardNumber = "", IssuerID = -1
            };
            IntPtr obj = new IntPtr();

            if ((res.ErrorCore = InitInternal(_config, ref obj)) != 0)
            {
                throw new CardShellException("Error Init No" + res, res.ErrorCore);
            }

            IntPtr serialNumberPtr = Marshal.AllocHGlobal(sizeof(SerialnumberT));
            IntPtr cardImagePtr    = Marshal.AllocHGlobal(sizeof(Mf1S70T));

            UnMemory <byte> .SaveInMemArr(new byte[Mf1S70T.Size], ref cardImagePtr);

            if ((res.ErrorCore = ReadCard(obj, serialNumberPtr, cardImagePtr, false)) != 0)
            {
                throw new CardShellException("Error ReadCard No" + res, res.ErrorCore);
            }

            var blocks = UnMemory <Mf1S70T> .ReadInMem(cardImagePtr);

            var card = blocks.GetBlocks();

            return(card);
        }
Ejemplo n.º 2
0
        public static int Init([MarshalAs(UnmanagedType.LPWStr)] string /*IntPtr*/ initStr, IntPtr caps, ref IntPtr obj)
        {
            string text =
                $"!!! Init !!!\tobj:{obj}\tCaps:{caps}\tInitStr:{initStr/*Marshal.PtrToStringBSTR(initStr)*/ ?? "null"}\t";

            WriteToLog(text);
            try
            {
                UnMemory <int> .SaveInMemArr(new[] { 12, 1, 0 }, ref caps);

                var capsR = UnMemory <int> .ReadInMemArr(caps, 3);

                text =
                    $"!!! Init internal Caps !!!\tsize:{capsR[0]}\tVolatileKeySlotCount:{capsR[1]}\tNonvolatileKeySlotCount:{capsR[2]}\t";
                //WriteToLog(text);

                var res = InitializeReader().Result;

                // выделение  памяти под obj
                int pInt = 0;
                UnMemory <int> .SaveInMem(pInt, ref obj);

                _obj = obj;

                if (_keys == null)
                {
                    _keys = new Dictionary <Tuple <int, int>, Tuple <int, bool, byte[]> >();
                }
                else
                {
                    _keys.Clear();
                }

                return(res);
            }
            catch (Exception e)
            {
                WriteToLog($"Init ERROR !!! reader:{_reader} card: {_card}\r\n {e}");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
        }
Ejemplo n.º 3
0
        public static int Anticollision(IntPtr obj, IntPtr serialNumberBuf, int bufSize, IntPtr serialNumberSize)
        {
            string text =
                $"!!! Anticollision !!!\tobj:{obj}\tSerialNumberBuf:{serialNumberBuf}\tBufSize:{bufSize}\tSerialNumberSize:{serialNumberSize}\t";

            WriteToLog(text);

            try
            {
                var uid = _card?.GetUid().Result;
                if (uid != null)
                {
                    byte[] truncUid = new byte[Math.Min(bufSize, uid.Length)];
                    Array.Copy(uid, truncUid, truncUid.Length);
                    UnMemory <int> .SaveInMem(truncUid.Length, ref serialNumberSize);

                    UnMemory <byte> .SaveInMemArr(uid, ref serialNumberBuf);

                    //Marshal.StructureToPtr(memory_object, SerialNumberBuf, true);

                    var uidWrited = UnMemory <byte> .ReadInMemArr(serialNumberBuf, truncUid.Length);

                    var serialNumberSizeWrited = UnMemory <int> .ReadInMem(serialNumberSize);

                    text =
                        $"!!! Anticollision over !!!\tobj:{obj}\tSerialNumberBuf:{serialNumberBuf}\tBufSize:{bufSize}\tuid:{BitConverter.ToString(uid)}\tuid_w:{BitConverter.ToString(uidWrited ?? new byte[] {})}\tSerialNumberSize:{serialNumberSize}\tSerialNumberSize_writed:{serialNumberSizeWrited}\t";
                    WriteToLog(text);
                    return((int)ErrorCodes.E_SUCCESS);
                }

                WriteToLog($"Anticollision ERROR !!! _card:{_card == null}\r\n uid:{uid == null}");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
            catch (Exception e)
            {
                WriteToLog($"Anticollision ERROR !!! {text}\r\n {e}");
                return((int)ErrorCodes.E_GENERIC);
            }
        }
Ejemplo n.º 4
0
        public static int ReadBlock(IntPtr obj, int block, IntPtr buffer)
        {
            string text =
                $"!!! ReadBlock !!!\tobj:{obj}\tBlock:{block}\tBuffer:{buffer}\t";

            WriteToLog(text);
            try
            {
                if (_card != null)
                {
                    int blockInSector;
                    int controlSector;
                    BlockToSectorBlock(block, out controlSector, out blockInSector);

                    var keyA = GetKeyFromCollection(controlSector, 0);
                    var keyB = GetKeyFromCollection(controlSector, 1);

                    text =
                        $"!!! ReadBlock before !!!\tcurrentSector: {controlSector}\tblockInSector: {blockInSector}\tkeyA: {BitConverter.ToString(keyA ?? new byte[] { })}\tkeyB: {BitConverter.ToString(keyB ?? new byte[] { })}";
                    //WriteToLog(text);

                    //TODO удалить
                    if (_cardBadSectors?.Contains(new Tuple <int, int>(controlSector, blockInSector)) ?? false)
                    {
                        byte[] dataBadSectors = ReadFromFileSector(controlSector, blockInSector);

                        if (dataBadSectors.Length > 0 && dataBadSectors.Length < 17)
                        {
                            //WriteToLog($"SaveInMemory from file {dataBadSectors.Length} byte");
                            UnMemory <byte> .SaveInMemArr(dataBadSectors, ref buffer);
                        }

                        //TODO убрать логи чтения и записи
                        //WriteToLog($"Sector '{controlSector}':[{blockInSector}]{dataBadSectors.ByteArrayToString()}");
                        //WriteDataToLog(controlSector, blockInSector, dataBadSectors.ByteArrayToString(), false);
                        return((int)ErrorCodes.E_SUCCESS);
                    }

                    byte[] data = GetData(controlSector, blockInSector, keyA, keyB).Result;
                    if (data.Length > 0 && data.Length < 17)
                    {
                        //WriteToLog($"SaveInMemory {data.Length} byte");
                        UnMemory <byte> .SaveInMemArr(data, ref buffer);

                        //TODO убрать логи чтения и записи
                        //WriteToLog($"Sector '{controlSector}':[{blockInSector}]{data.ByteArrayToString()}");
                        //WriteDataToLog(controlSector, blockInSector, data.ByteArrayToString(), false);

                        return((int)ErrorCodes.E_SUCCESS);
                    }
                    return((int)ErrorCodes.E_GENERIC);
                }
                WriteToLog($"ReadBlock ERROR !!! reader:{_reader} card: {_card}\r\n");
                return((int)ErrorCodes.E_CARDREADER_NOT_INIT);
            }
            catch (Exception e)
            {
                WriteToLog($"ReadBlock ERROR !!! {text}\r\n {e}");
                return((int)ErrorCodes.E_GENERIC);
            }
        }
Ejemplo n.º 5
0
        // ОПЕРАЦИЯ ПО КАРТЕ
        /// <summary>
        /// ОПЕРАЦИЯ ПО КАРТЕ
        /// </summary>
        /// <param name="type">тип операции</param>
        /// <param name="terminal">терминал (азс)</param>
        /// <param name="transact">номер транзакции</param>
        /// <param name="opItem">параметры операции</param>
        /// <returns></returns>
        public static ServioCardInfo CardOperationExecute(CardOperationType type, int terminal, long transact, CardOperationItem opItem)
        {
            ServioCardInfo res = new ServioCardInfo {
                ErrorCore = -1, CardNumber = "", IssuerID = -1
            };
            IntPtr obj = new IntPtr();

            IntPtr serialNumberPtr = Marshal.AllocHGlobal(sizeof(SerialnumberT));
            IntPtr cardImagePtr    = Marshal.AllocHGlobal(sizeof(Mf1S70T));
            IntPtr operationPtr    = CardOperation_Create();

            try
            {
                if ((res.ErrorCore = InitInternal(_config, ref obj)) != (int)ErrorCodes.ESuccess)
                {
                    if (res.ErrorCore == (int)ErrorCodes.ECancel)
                    {
                        return(res);
                    }
                    throw new CardShellException("Error Init No" + res, res.ErrorCore);
                }
                UnMemory <byte> .SaveInMemArr(new byte[Mf1S70T.Size], ref cardImagePtr);

                if ((res.ErrorCore = ReadCard(obj, serialNumberPtr, cardImagePtr, false)) != (int)ErrorCodes.ESuccess)
                {
                    if (res.ErrorCore == (int)ErrorCodes.ECancel)
                    {
                        return(res);
                    }
                    throw new CardShellException("Error ReadCard No" + res, res.ErrorCore);
                }
                var operation = UnMemory <CardOperation> .ReadInMem(operationPtr);

                SerialnumberT serialNumber = UnMemory <SerialnumberT> .ReadInMem(serialNumberPtr);

                operation.Size           = sizeof(CardOperation);
                operation.OpCode         = (byte)OperationSelector(type);
                operation.POS            = terminal;                // Номер АЗС или терминала
                operation.OpTime         = DateTime.Now.ToOADate(); // Реальное время с вашего терминала
                operation.TransactID     = transact;                // Тут желательно ваш код код транзакции указать
                operation.CardType       = (int)CardType.CtPayment; // Предположим что тип карты неизвестен
                operation.SerialNumber   = serialNumber;            // Указать серийный номер, считанный с метки
                operation.IssuerID       = -1;                      // Указать -1, программа сама переопределит. Если указать другой код - программа будет использовать его
                operation.CardNumber     = "";                      // Не указывать !!!
                operation.AddPrefixZeros = 0;                       // Нужна доп. настройка. Аналогичная нашей опции в обработчиках
                operation.ItemCount      = 1;                       // При аутентификации позиции не заполнять.

                IntPtr pItem1 = Marshal.AllocHGlobal(sizeof(CardOperationItem));
                UnMemory <CardOperationItem> .SaveInMem(opItem, ref pItem1);

                IntPtr ppItem1 = Marshal.AllocHGlobal(sizeof(IntPtr));
                UnMemory <IntPtr> .SaveInMem(pItem1, ref ppItem1);

                operation.Items = ppItem1;

                UnMemory <CardOperation> .SaveInMem(operation, ref operationPtr);

                if ((res.ErrorCore =
                         OperationSelector(type, obj, serialNumberPtr, cardImagePtr, operationPtr))
                    != (int)ErrorCodes.ESuccess)
                {
                    throw new CardShellException("Error Operation No" + res, res.ErrorCore);
                }

                // флаг указывает на то что образ карты был изменен в процессе операции
                // и нужно записать его обратно.
                // Нужно для пополнения карты при просмотре информации
                operation = UnMemory <CardOperation> .ReadInMem(operationPtr);

                if (operation.CardImageChanged != 0)
                {
                    // при ошибке можно попробовать повторить операцию несколько раз 3-10...
                    // если не проходит то это патовая ситуация и для её дальнейшего
                    // разрешения нужно сохранить SerialNumber, CardImage и CardInfo
                    // В теории, сохранённый SerialNumber и CardImage можно попробовать
                    // записать обратно даже при перезапуске ПО, но до следующего
                    // обслуживания по карте. Чтобы решить ситуацию можно сохранить CardInfo
                    // чтобы в офисе уменьшили или увеличили баланс карты на соотв. значение.
                    if ((res.ErrorCore = WriteCard(obj, serialNumberPtr, cardImagePtr)) != (int)ErrorCodes.ESuccess)
                    {
                        throw new CardShellException("Error WriteCard No" + res, res.ErrorCore);
                    }
                }

                operation    = (CardOperation)Marshal.PtrToStructure(operationPtr, typeof(CardOperation));
                res.CardInfo = operation.CheckImage;
                return(res);
            }
            finally
            {
                //CardOperation_Free(operationPtr);
                UnMemory.FreeIntPtr(serialNumberPtr);
                UnMemory.FreeIntPtr(cardImagePtr);
                //MarshalHelper.UnMemory.FreeIntPtr(operationPtr);
            }
        }