Exemplo n.º 1
0
        protected DokanError ReadPayload(out byte[] payload, out string extension)
        {
            payload   = null;
            extension = String.Empty;

            // Make sure the chip is still here
            if (!rfidListener.ReconnectOnCard())
            {
                log.Error("Card reconnection failed");
                return(DokanError.ErrorNotReady);
            }

            IChip        chip = rfidListener.GetChip();
            ICardService svc  = chip.GetService(CardServiceType.CST_NFC_TAG);
            IDESFireEV1NFCTag4CardService nfcsvc = svc as IDESFireEV1NFCTag4CardService;

            if (nfcsvc == null)
            {
                log.Error("No NFC service.");
                // If no NFC service for this chip, we must fail
                return(DokanError.ErrorNotReady);
            }

            try
            {
                NdefMessage ndef = nfcsvc.ReadNDEFFile();
                if (ndef != null)
                {
                    if (ndef.GetRecordCount() > 0)
                    {
                        // For now, only support for one Ndef record per card
                        object[] records = ndef.Records as object[];
                        if (records != null)
                        {
                            INdefRecord record     = records[0] as INdefRecord;
                            object      payloadobj = record.Payload;
                            if (payloadobj != null)
                            {
                                payload = payloadobj as byte[];
                                payload = ParsePayload(record, payload, out extension);
                            }
                            else
                            {
                                log.Error("Bad record payload");
                            }
                        }
                    }
                    else
                    {
                        log.Info("No NDEF record in the message");
                    }
                }
                else
                {
                    log.Info("No NDEF message on this card");
                }
            }
            catch (COMException ex)
            {
                log.Info("NDEF read error", ex);
            }

            return(DokanError.ErrorSuccess);
        }
Exemplo n.º 2
0
        protected byte[] ParsePayload(INdefRecord record, byte[] payload, out string extension)
        {
            // Parse payload data because current liblogicalaccess implementation is too basic
            // This is stupid, end-user shouldn't know about NDEF structure
            // It should be done on liblogicalaccess side!

            extension = String.Empty;
            byte[] data  = null;
            object otype = record.Type;

            if (otype != null)
            {
                byte[] type    = (byte[])otype;
                string strType = System.Text.Encoding.UTF8.GetString(type).ToLower();

                log.Info(String.Format("Ndef Record type: {0}", strType));

                if (type.Length == 1 && type[0] == 0x54)
                {
                    if (payload.Length > 0)
                    {
                        string encodingname = "us-ascii";
                        if (payload[0] != 0)
                        {
                            encodingname = System.Text.Encoding.ASCII.GetString(payload, 1, payload[0]);
                        }
                        Encoding encoding = Encoding.GetEncoding(encodingname);
                        if (encoding == null)
                        {
                            encoding     = Encoding.ASCII;
                            encodingname = encoding.EncodingName;
                        }
                        int length = payload.Length - 1 - payload[0];
                        if (length > 0)
                        {
                            data = new byte[length];
                            Array.Copy(payload, payload[0] + 1, data, 0, length);
                        }
                    }
                    // Text record type
                    extension = "txt";
                }
                else if (type.Length == 1 && type[0] == 0x55)
                {
                    if (payload.Length > 0)
                    {
                        UriType uriType = UriType.NO_PREFIX;
                        string  uri     = String.Empty;
                        if (payload[0] != 0)
                        {
                            uriType = (UriType)payload[0];
                        }

                        int length = payload.Length - 1;
                        if (length != 0)
                        {
                            uri = System.Text.Encoding.UTF8.GetString(payload, 1, length);
                        }

                        switch (uriType)
                        {
                        case UriType.HTTP:
                            uri = "http://" + uri;
                            break;

                        case UriType.HTTP_WWW:
                            uri = "http://www." + uri;
                            break;

                        case UriType.HTTPS:
                            uri = "https://" + uri;
                            break;

                        case UriType.HTTPS_WWW:
                            uri = "https://www." + uri;
                            break;

                        case UriType.MAIL_TO:
                            uri = "mailto://" + uri;
                            break;

                        case UriType.TEL:
                            uri = "tel://" + uri;
                            break;

                        case UriType.URI_FILE:
                            uri = "file://" + uri;
                            break;
                        }
                        data = System.Text.Encoding.UTF8.GetBytes(uri);
                    }
                    // Uri record type
                    extension = "url";
                }
                else if (record.TNF == TNF.TNF_MIME_MEDIA)
                {
                    data      = payload;
                    extension = GetDefaultExtension(strType);
                }
            }

            return(data);
        }
Exemplo n.º 3
0
        protected byte[] ParsePayload(INdefRecord record, byte[] payload, out string extension)
        {
            // Parse payload data because current liblogicalaccess implementation is too basic
            // This is stupid, end-user shouldn't know about NDEF structure
            // It should be done on liblogicalaccess side!

            extension = String.Empty;
            byte[] data = null;
            object otype = record.Type;
            if (otype != null)
            {
                byte[] type = (byte[])otype;
                string strType = System.Text.Encoding.UTF8.GetString(type).ToLower();

                log.Info(String.Format("Ndef Record type: {0}", strType));

                if (type.Length == 1 && type[0] == 0x54)
                {
                    if (payload.Length > 0)
                    {
                        string encodingname = "us-ascii";
                        if (payload[0] != 0)
                            encodingname = System.Text.Encoding.ASCII.GetString(payload, 1, payload[0]);
                        Encoding encoding = Encoding.GetEncoding(encodingname);
                        if (encoding == null)
                        {
                            encoding = Encoding.ASCII;
                            encodingname = encoding.EncodingName;
                        }
                        int length = payload.Length - 1 - payload[0];
                        if (length > 0)
                        {
                            data = new byte[length];
                            Array.Copy(payload, payload[0] + 1, data, 0, length);
                        }
                    }
                    // Text record type
                    extension = "txt";
                }
                else if (type.Length == 1 && type[0] == 0x55)
                {
                    if (payload.Length > 0)
                    {
                        UriType uriType = UriType.NO_PREFIX;
                        string uri = String.Empty;
                        if (payload[0] != 0)
                            uriType = (UriType)payload[0];

                        int length = payload.Length - 1;
                        if (length != 0)
                            uri = System.Text.Encoding.UTF8.GetString(payload, 1, length);

                        switch (uriType)
                        {
                            case UriType.HTTP:
                                uri = "http://" + uri;
                                break;
                            case UriType.HTTP_WWW:
                                uri = "http://www." + uri;
                                break;
                            case UriType.HTTPS:
                                uri = "https://" + uri;
                                break;
                            case UriType.HTTPS_WWW:
                                uri = "https://www." + uri;
                                break;
                            case UriType.MAIL_TO:
                                uri = "mailto://" + uri;
                                break;
                            case UriType.TEL:
                                uri = "tel://" + uri;
                                break;
                            case UriType.URI_FILE:
                                uri = "file://" + uri;
                                break;
                        }
                        data = System.Text.Encoding.UTF8.GetBytes(uri);
                    }
                    // Uri record type
                    extension = "url";
                }
                else if (record.TNF == TNF.TNF_MIME_MEDIA)
                {
                    data = payload;
                    extension = GetDefaultExtension(strType);
                }
            }

            return data;
        }