Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NfcService.cs
10using System.Globalization;
11
13{
17 [Singleton]
18 public class NfcService : INfcService
19 {
23 public NfcService()
24 : base()
25 {
26 }
27
32 public async Task TagDetected(INfcTag Tag)
33 {
34 try
35 {
36 string TagId = Hashes.BinaryToString(Tag.ID).ToUpper(CultureInfo.InvariantCulture);
37 NfcTagReference TagReference = await NfcTagReference.FindByTagId(TagId);
38
39 foreach (INfcInterface Interface in Tag.Interfaces)
40 {
41 // Some NFC devices allow all interfaces to be open, others not. So when browsing interfaces we must assure only
42 // one interface is open at a time.
43 foreach (INfcInterface Interface2 in Tag.Interfaces)
44 {
45 if (Interface2 == Interface)
46 await Interface2.OpenIfClosed();
47 else
48 Interface2.CloseIfOpen();
49 }
50
51 if (Interface is IIsoDepInterface IsoDep)
52 {
53 // ISO 14443-4
54
55 string Mrz = await RuntimeSettings.GetAsync("NFC.LastMrz", string.Empty);
56
57 if (!string.IsNullOrEmpty(Mrz) &&
59 {
60 // §4.3, §D.3, https://www.icao.int/publications/Documents/9303_p11_cons_en.pdf
61
62 byte[]? Challenge = await IsoDep.GetChallenge();
63 if (Challenge is not null && DocInfo is not null)
64 {
65 byte[] ChallengeResponse = DocInfo.CalcChallengeResponse(Challenge);
66 byte[]? Response = await IsoDep.ExternalAuthenticate(ChallengeResponse);
67
68 // TODO
69 }
70 }
71 }
72 else if (Interface is INdefInterface Ndef)
73 {
74 bool CanMakeReadOnly = await Ndef.CanMakeReadOnly();
75 bool IsWritable = await Ndef.IsWritable();
76 INdefRecord[] Records = await Ndef.GetMessage();
77
78 if (Records.Length == 0 && IsWritable)
79 {
80 await ProgramNfc(Items => Ndef.SetMessage(Items));
81 // TODO: Make read-only if able
82 }
83 else
84 {
85 foreach (INdefRecord Record in Records)
86 {
87 if (Record is INdefUriRecord UriRecord)
88 {
89 if (!string.IsNullOrEmpty(Constants.UriSchemes.GetScheme(UriRecord.Uri)))
90 {
91 if (!await App.AuthenticateUser(AuthenticationPurpose.NfcTagDetected))
92 return;
93
94 if (await App.OpenUrlAsync(UriRecord.Uri))
95 return;
96 }
97 }
98 }
99
100 // TODO: Open NFC view
101 }
102 }
103 else if (Interface is INdefFormatableInterface NdefFormatable)
104 {
105 await ProgramNfc(Items => NdefFormatable.Format(false, Items));
106 // TODO: Make read-only if able
107 }
108 else if (Interface is INfcAInterface NfcA)
109 {
110 byte[] Atqa = await NfcA.GetAtqa();
111 short Sqk = await NfcA.GetSqk();
112
113 // TODO
114 }
115 else if (Interface is INfcBInterface NfcB)
116 {
117 byte[] ApplicationData = await NfcB.GetApplicationData();
118 byte[] ProtocolInfo = await NfcB.GetProtocolInfo();
119
120 // TODO
121 }
122 else if (Interface is INfcFInterface NfcF)
123 {
124 byte[] Manufacturer = await NfcF.GetManufacturer();
125 byte[] SystemCode = await NfcF.GetSystemCode();
126
127 // TODO
128 }
129 else if (Interface is INfcVInterface NfcV)
130 {
131 sbyte DsfId = await NfcV.GetDsfId();
132 short ResponseFlags = await NfcV.GetResponseFlags();
133
134 // TODO
135 }
136 else if (Interface is INfcBarcodeInterface Barcode)
137 {
138 byte[] Data = await Barcode.ReadAllData();
139
140 // TODO
141 }
142 else if (Interface is IMifareUltralightInterface MifareUltralight)
143 {
144 byte[] Data = await MifareUltralight.ReadAllData();
145
146 // TODO
147 }
148 else if (Interface is IMifareClassicInterface MifareClassic)
149 {
150 byte[] Data = await MifareClassic.ReadAllData();
151
152 // TODO
153 }
154 }
155 }
156 catch (Exception ex)
157 {
159 }
160 }
161
162 public delegate Task<bool> WriteItems(object[] Items);
163
169 public static async Task<bool> ProgramNfc(WriteItems Callback)
170 {
171 IUiService Nav = App.Instantiate<IUiService>();
172 if (Nav.CurrentPage is BaseContentPage ContentPage &&
173 ContentPage.ViewModel<BaseViewModel>() is ILinkableView LinkableView &&
174 LinkableView.IsLinkable)
175 {
176 string? Link = LinkableView.Link;
177 string Title = await LinkableView.Title;
178
179 List<object> Items = [];
180
181 if (LinkableView.EncodeAppLinks)
182 Items.Add(Title);
183
184 if (!string.IsNullOrEmpty(Link))
185 Items.Add(new Uri(Link));
186
187 if (LinkableView.EncodeAppLinks)
188 {
189 Items.Add(new Uri(Constants.References.AndroidApp));
190 Items.Add(new Uri(Constants.References.IPhoneApp));
191 }
192
193 if (LinkableView.HasMedia)
194 Items.Add(new KeyValuePair<byte[], string>(LinkableView.Media!, LinkableView.MediaContentType!));
195
196 if (!await App.AuthenticateUser(AuthenticationPurpose.NfcTagDetected))
197 return false;
198
199 bool Ok = await Callback([.. Items]);
200
201 if (!Ok && Items[^1] is KeyValuePair<byte[], string>)
202 {
203 Items.RemoveAt(Items.Count - 1);
204 Ok = await Callback([.. Items]);
205 }
206
207 if (!Ok)
208 {
209 while (Items.Count > 2)
210 Items.RemoveAt(2);
211
212 Ok = await Callback([.. Items]);
213
214 if (!Ok)
215 {
216 Items.RemoveAt(0);
217 Ok = await Callback([.. Items]);
218 }
219 }
220
221 if (Ok)
222 {
224 ServiceRef.Localizer[nameof(AppResources.SuccessTitle)],
225 ServiceRef.Localizer[nameof(AppResources.TagEngraved), Title]);
226
227 return true;
228 }
229 else
230 {
232 ServiceRef.Localizer[nameof(AppResources.ErrorTitle)],
233 ServiceRef.Localizer[nameof(AppResources.TagNotEngraved), Title]);
234
235 return false;
236 }
237 }
238 else
239 return false;
240 }
241
242 }
243}
Contains NFC Extensions for Basic Access Control.
static bool ParseMrz(string MRZ, out DocumentInformation? Info)
Derives Basic Access Control Keys from the second row of the Machine-Readable string in passport (MRZ...
Contains parsed information from a machine-readable document information string.
The Application class, representing an instance of the Neuro-Access app.
Definition: App.xaml.cs:69
static Task< bool > AuthenticateUser(AuthenticationPurpose Purpose, bool Force=false)
Authenticates the user using the configured authentication method.
Definition: App.xaml.cs:981
static Task< bool > OpenUrlAsync(string Url)
Opens an URL in the application.
Definition: App.xaml.cs:919
References to external resources
Definition: Constants.cs:717
const string IPhoneApp
Resource where iPhone App can be downloaded.
Definition: Constants.cs:726
const string AndroidApp
Resource where Android App can be downloaded.
Definition: Constants.cs:721
static ? string GetScheme(string Url)
Gets the predefined scheme from an IoT Code
Definition: Constants.cs:146
A set of never changing property constants and helpful values.
Definition: Constants.cs:7
Near-Field Communication (NFC) Service.
Definition: NfcService.cs:19
NfcService()
Near-Field Communication (NFC) Service.
Definition: NfcService.cs:23
static async Task< bool > ProgramNfc(WriteItems Callback)
Programs an NFC tag.
Definition: NfcService.cs:169
async Task TagDetected(INfcTag Tag)
Method called when a new NFC Tag has been detected.
Definition: NfcService.cs:32
Contains information about a contact.
static Task< NfcTagReference > FindByTagId(CaseInsensitiveString TagId)
Finds information about a contact, given its Bare JID.
Base class that references services in the app.
Definition: ServiceRef.cs:31
static IUiService UiService
Service serializing and managing UI-related tasks.
Definition: ServiceRef.cs:55
static IStringLocalizer Localizer
Localization service
Definition: ServiceRef.cs:235
A base class for all view models, inheriting from the BindableObject. NOTE: using this class requir...
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
ISO DEP interface, for communication with an NFC Tag.
Mifare Classic interface, for communication with an NFC Tag.
Mifare Ultralight interface, for communication with an NFC Tag.
NDEF Formatable interface, for communication with an NFC Tag.
NDEF interface, for communication with an NFC Tag.
NFC A interface, for communication with an NFC Tag.
NFC B interface, for communication with an NFC Tag.
NFC Barcode interface, for communication with an NFC Tag.
NFC F interface, for communication with an NFC Tag.
Specific Interface (technology) for communication with an NFC Tag.
void CloseIfOpen()
Closes the interface, if connected.
Task OpenIfClosed()
Connects the interface, if not connected.
Interface for an NFC Tag.
Definition: INfcTag.cs:9
byte[] ID
NFC Tag ID
Definition: INfcTag.cs:14
INfcInterface[] Interfaces
Communication interfaces available on the NFC Tag.
Definition: INfcTag.cs:22
NFC V interface, for communication with an NFC Tag.
Interface for NDEF records
Definition: INdefRecord.cs:7
Interface for NDEF URI records
Interface for the Near-Field Communication (NFC) Service.
Definition: INfcService.cs:11
Service serializing and managing UI-related tasks.
Definition: IUiService.cs:14
Task DisplayException(Exception Exception, string? Title=null)
Displays an alert/message box to the user.
Task< bool > DisplayAlert(string Title, string Message, string? Accept=null, string? Cancel=null)
Displays an alert/message box to the user.
Interface for linkable views.
Definition: ILinkableView.cs:7
bool IsLinkable
If the current view is linkable.
AuthenticationPurpose
Purpose for requesting the user to authenticate itself.