Ejemplo n.º 1
0
 public async Task SendAPDUCommand(Reader reader, ApduCommand command)
 {
     try
     {
         await SendAPDUCommand(reader, command, OnAPDUCommand);
     }
     catch (Exception e)
     {
         Console.WriteLine("Can not send Apdu command:" + e.Message);
     }
 }
Ejemplo n.º 2
0
        public async Task ReadNdefMessage(Reader reader, NdefMessage message, byte userMemoryPage = 4)
        {
            byte[] com = { 0xff, 0xb0, 0x00, userMemoryPage, 0x00 };

            ApduCommand command = new ApduCommand(com);

            List <byte[]> receivedByte = new List <byte[]>();
            ExecStatus    success      = ExecStatus.Error;
            byte          length       = 0;
            int           index        = 0;

            await SendAPDUCommand(reader, command, (s, r, c, rec) =>
            {
                if (s == ExecStatus.Success)
                {
                    receivedByte.Add(rec);
                }
                success = s;
            });

            if (success != ExecStatus.Success || receivedByte[index][0] != 0x03)
            {
                if (Global.SyncContext != null)
                {
                    Global.SyncContext.Post((state) =>
                    {
                        ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state;
                        if (OnReadNdefMessage != null)
                        {
                            OnReadNdefMessage(false, t.Item1, t.Item2);
                        }
                    }, new ValueTuple <Reader, NdefMessage>(reader, message));
                }
                else
                {
                    if (OnReadNdefMessage != null)
                    {
                        OnReadNdefMessage(false, reader, message);
                    }
                }
                return;
            }

            length = (byte)(receivedByte[index][1] + 2);

            int currentLength = length - 16;
            int numRead       = 1;

            while (currentLength > 0)
            {
                com[3] += 0x04;
                command = new ApduCommand(com);

                success = ExecStatus.Error;

                await SendAPDUCommand(reader, command, (s, r, c, rec) =>
                {
                    if (s == ExecStatus.Success)
                    {
                        receivedByte.Add(rec);
                    }
                    success = s;
                    numRead++;
                });

                if (success == ExecStatus.Success)
                {
                    if (Global.SyncContext != null)
                    {
                        Global.SyncContext.Post((state) =>
                        {
                            ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state;
                            if (OnReadNdefMessage != null)
                            {
                                OnReadNdefMessage(false, t.Item1, t.Item2);
                            }
                        }, new ValueTuple <Reader, NdefMessage>(reader, message));
                    }
                    else
                    {
                        if (OnReadNdefMessage != null)
                        {
                            OnReadNdefMessage(false, reader, message);
                        }
                    }
                    return;
                }

                currentLength -= 16;
            }

            byte[] binary = new byte[numRead * 16];

            for (int i = 0; i < receivedByte.Count; i++)
            {
                Array.Copy(receivedByte[i], 0, binary, 16 * i, 16);
            }

            message.FromBinaryData(binary);

            if (Global.SyncContext != null)
            {
                Global.SyncContext.Post((state) =>
                {
                    ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state;
                    if (OnReadNdefMessage != null)
                    {
                        OnReadNdefMessage(true, t.Item1, t.Item2);
                    }
                }, new ValueTuple <Reader, NdefMessage>(reader, message));
            }
            else
            {
                if (OnReadNdefMessage != null)
                {
                    OnReadNdefMessage(true, reader, message);
                }
            }
        }
Ejemplo n.º 3
0
        public async Task WriteNdefMessage(Reader reader, NdefMessage message, byte userMemoryPage = 4, byte writeUnitSize = 4)
        {
            byte[] binary = message.ToBinaryData();
            int    length = binary.Length;

            ApduCommand command;
            int         currentIndex = 0;
            int         numWrite     = 0;

            while (length > 0)
            {
                byte[] com = new byte[6 + writeUnitSize];

                com[0] = 0xff;
                com[1] = 0xd6;
                com[2] = 0x00;

                com[3] = (byte)(userMemoryPage + numWrite);
                com[4] = writeUnitSize;

                int l = writeUnitSize;

                if (length < l)
                {
                    l = (byte)length;
                }
                Array.Copy(binary, currentIndex, com, 5, l);

                command = new ApduCommand(com);

                ExecStatus success = ExecStatus.Error;

                await SendAPDUCommand(reader, command, (s, r, c, rec) =>
                {
                    success = s;
                });

                if (success == ExecStatus.Error)
                {
                    if (Global.SyncContext != null)
                    {
                        Global.SyncContext.Post((state) =>
                        {
                            ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state;
                            if (OnWriteNdefMessage != null)
                            {
                                OnWriteNdefMessage(false, t.Item1, t.Item2);
                            }
                        }, new ValueTuple <Reader, NdefMessage>(reader, message));
                    }
                    else
                    {
                        if (OnWriteNdefMessage != null)
                        {
                            OnWriteNdefMessage(false, reader, message);
                        }
                    }
                    return;
                }


                length       -= writeUnitSize;
                currentIndex += writeUnitSize;
                numWrite++;
            }

            if (Global.SyncContext != null)
            {
                Global.SyncContext.Post((state) =>
                {
                    ValueTuple <Reader, NdefMessage> t = (ValueTuple <Reader, NdefMessage>)state;
                    if (OnWriteNdefMessage != null)
                    {
                        OnWriteNdefMessage(true, t.Item1, t.Item2);
                    }
                }, new ValueTuple <Reader, NdefMessage>(reader, message));
            }
            else
            {
                if (OnWriteNdefMessage != null)
                {
                    OnWriteNdefMessage(true, reader, message);
                }
            }
        }
Ejemplo n.º 4
0
        public async Task SendAPDUCommand(Reader reader, ApduCommand command, APDUCommandHandler handler)
        {
            await Task.Run(() =>
            {
                byte sw1 = 0;
                byte sw2 = 0;

                if (reader.CurrentCard == null || !IsConnected)
                {
                    if (Global.SyncContext != null)
                    {
                        Global.SyncContext.Post((state) =>
                        {
                            ValueTuple <Reader, ApduCommand, byte[]> t = (ValueTuple <Reader, ApduCommand, byte[]>)state;
                            if (handler != null)
                            {
                                handler(ExecStatus.Error, t.Item1, t.Item2, t.Item3);
                            }
                        }, new ValueTuple <Reader, ApduCommand, byte[]>(reader, command, null));
                    }
                    else
                    {
                        if (OnAPDUCommand != null)
                        {
                            handler(ExecStatus.Error, reader, command, null);
                        }
                    }
                }
                else
                {
                    try
                    {
                        byte maxRecvDataLen;

                        maxRecvDataLen = 64;

                        byte[] recvBuffer;

                        IntPtr hCard;
                        int recvLength;
                        byte[] data;

                        lock (reader.ApduLock)
                        {
                            hCard      = ConnectCard(reader, reader.ShareParam, reader.Protocol);
                            recvBuffer = new byte[maxRecvDataLen + 2];
                            recvLength = TransmitToCard(hCard, command.Data, recvBuffer, reader.Protocol);

                            sw1 = recvBuffer[recvLength - 2];
                            sw2 = recvBuffer[recvLength - 1];

                            command.Sw1 = sw1;
                            command.Sw2 = sw2;

                            if (sw1 != 0x90 && sw1 != 0x62 && sw1 != 0x63)
                            {
                                throw new APDUException(sw1, sw2);
                            }

                            data = new byte[recvLength - 2];
                            Array.Copy(recvBuffer, data, recvLength - 2);

                            DisconnectCard(hCard, Disposition.Leave);
                        }

                        if (Global.SyncContext != null)
                        {
                            Global.SyncContext.Post((state) =>
                            {
                                ValueTuple <Reader, ApduCommand, byte[]> t = (ValueTuple <Reader, ApduCommand, byte[]>)state;
                                if (handler != null)
                                {
                                    if (command.Sw1 == 0x90)
                                    {
                                        handler(ExecStatus.Success, t.Item1, t.Item2, t.Item3);
                                    }
                                    else
                                    {
                                        handler(ExecStatus.Warning, t.Item1, t.Item2, t.Item3);
                                    }
                                }
                            }, new ValueTuple <Reader, ApduCommand, byte[]>(reader, command, data));
                        }
                        else
                        {
                            if (handler != null)
                            {
                                if (command.Sw1 == 0x90)
                                {
                                    handler(ExecStatus.Success, reader, command, data);
                                }
                                else
                                {
                                    handler(ExecStatus.Warning, reader, command, data);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        command.Exception = e;

                        Console.WriteLine("Can not send Apdu command:" + e.Message);

                        if (Global.SyncContext != null)
                        {
                            Global.SyncContext.Post((state) =>
                            {
                                ValueTuple <Reader, ApduCommand, byte[]> t = (ValueTuple <Reader, ApduCommand, byte[]>)state;
                                if (handler != null)
                                {
                                    handler(ExecStatus.Error, t.Item1, t.Item2, t.Item3);
                                }
                            }, new ValueTuple <Reader, ApduCommand, byte[]>(reader, command, null));
                        }
                        else
                        {
                            if (handler != null)
                            {
                                handler(ExecStatus.Error, reader, command, null);
                            }
                        }
                    }
                }
            });
        }