Example #1
0
        protected DokanError EraseCard()
        {
            try
            {
                IChip chip = rfidListener.GetChip();
                if (chip == null)
                {
                    return(DokanError.ErrorNotReady);
                }

                IStorageCardService storage = chip.GetService(CardServiceType.CST_STORAGE) as IStorageCardService;
                if (storage != null)
                {
                    storage.Erase();
                    ResetCache();
                    return(DokanError.ErrorSuccess);
                }
                else
                {
                    return(DokanError.ErrorNotImplemented);
                }
            }
            catch (Exception ex)
            {
                log.Error("DeleteDirectory error", ex);
                rfidListener.ResetCard();
                return(DokanError.ErrorError);
            }
        }
        protected DokanError WritePayload(byte[] payload, string extension)
        {
            // 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
            {
                IStorageCardService storage = chip.GetService(CardServiceType.CST_STORAGE) as IStorageCardService;
                if (storage != null)
                {
                    storage.Erase();
                }
                nfcsvc.CreateNFCApplication(1, null);

                NdefMessage ndef = new NdefMessage();
                switch (extension)
                {
                case "txt":
                    log.Info("Adding Text Record");
                    ndef.AddRawTextRecord(payload);
                    break;

                case "url":
                    string lnk    = Encoding.UTF8.GetString(payload);
                    int    urlpos = lnk.IndexOf("URL=");
                    if (urlpos > -1)
                    {
                        urlpos += 4;
                        int end = lnk.IndexOf(Environment.NewLine, urlpos);
                        if (end < 0)
                        {
                            end = lnk.Length - urlpos;
                        }
                        else
                        {
                            end -= urlpos;
                        }
                        string url = lnk.Substring(urlpos, end).Trim();
                        log.Info(String.Format("Adding Uri Record `{0}`", url));
                        ndef.AddUriRecord(url, UriType.NO_PREFIX);
                    }
                    else
                    {
                        log.Warn("Cannot found URL on link file content.");
                    }
                    break;

                default:
                    string mime = System.Web.MimeMapping.GetMimeMapping("dummy." + extension);
                    log.Info(String.Format("Adding Mime Record `{0}`", mime));
                    ndef.AddMimeMediaRecord(mime, Encoding.ASCII.GetString(payload));       // This shouldn't be a string here
                    break;
                }
                nfcsvc.WriteNDEFFile(ndef);

                ResetCache();
            }
            catch (COMException ex)
            {
                log.Info("NDEF write error", ex);
            }

            return(DokanError.ErrorSuccess);
        }