2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 namespace Tizen.Network.Nfc
24 /// A class for the NFC CardEmulation mode. It allows applications to handle Card Emulation informations.
26 /// <since_tizen> 3 </since_tizen>
27 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
28 public class NfcCardEmulationAdapter : IDisposable
30 private bool disposed = false;
32 private event EventHandler<SecureElementEventArgs> _secureElementEvent;
33 private event EventHandler<SecureElementTranscationEventArgs> _secureElementTransactionEvent;
34 private event EventHandler<HostCardEmulationEventArgs> _hostCardEmulationEvent;
36 private Interop.Nfc.SecureElementEventCallback _secureElementEventCallback;
37 private Interop.Nfc.SecureElementTransactionEventCallback _secureElementTransactionEventCallback;
38 private Interop.Nfc.HostCardEmulationEventCallback _hostCardEmulationEventCallback;
41 /// An event that is called when receiving the Secure Element (SIM/UICC(Universal Integrated Circuit Card)) event.
43 /// <since_tizen> 3 </since_tizen>
44 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
45 public event EventHandler<SecureElementEventArgs> SecureElementEvent
49 if (_secureElementEvent == null)
51 RegisterSecureElementEvent();
53 _secureElementEvent += value;
57 _secureElementEvent -= value;
58 if (_secureElementEvent == null)
60 UnregisterSecureElementEvent();
66 /// An event that is called when receiving the Secure Element (SIM/UICC (Universal Integrated Circuit Card)) transaction event for the 'ESE(SmartMX)' type.
68 /// <since_tizen> 3 </since_tizen>
69 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
70 public event EventHandler<SecureElementTranscationEventArgs> EseSecureElementTransactionEvent
74 if (_secureElementTransactionEvent == null)
76 RegisterSecureElementTransactionEvent(NfcSecureElementType.EmbeddedSE);
78 _secureElementTransactionEvent += value;
82 _secureElementTransactionEvent -= value;
83 if (_secureElementTransactionEvent == null)
85 UnregisterSecureElementTransactionEvent(NfcSecureElementType.EmbeddedSE);
91 /// An event that is called when receiving the Secure Element (SIM/UICC (Universal Integrated Circuit Card)) transaction event for the 'UICC' type.
93 /// <since_tizen> 3 </since_tizen>
94 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
95 public event EventHandler<SecureElementTranscationEventArgs> UiccSecureElementTransactionEvent
99 if (_secureElementTransactionEvent == null)
101 RegisterSecureElementTransactionEvent(NfcSecureElementType.Uicc);
103 _secureElementTransactionEvent += value;
107 _secureElementTransactionEvent -= value;
108 if (_secureElementTransactionEvent == null)
110 UnregisterSecureElementTransactionEvent(NfcSecureElementType.Uicc);
116 /// An event that is called when receiving the HCE (Host Card Emulation) event.
118 /// <since_tizen> 3 </since_tizen>
119 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
120 public event EventHandler<HostCardEmulationEventArgs> HostCardEmulationEvent
124 if (_hostCardEmulationEvent == null)
126 RegisterHostCardEmulationEvent();
128 _hostCardEmulationEvent += value;
132 _hostCardEmulationEvent -= value;
133 if (_hostCardEmulationEvent == null)
135 UnregisterHostCardEmulationEvent();
140 internal NfcCardEmulationAdapter()
144 ~NfcCardEmulationAdapter()
149 public void Dispose()
152 GC.SuppressFinalize(this);
155 private void Dispose(bool disposing)
162 // Free managed objects.
164 //Free unmanaged objects
169 /// Enables the card emulation mode.
171 /// <since_tizen> 3 </since_tizen>
172 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
173 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
174 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
175 public void EnableCardEmulation()
177 int ret = Interop.Nfc.CardEmulation.EnableCardEmulation();
178 if (ret != (int)NfcError.None)
180 Log.Error(Globals.LogTag, "Failed to enable card emulation mode, Error - " + (NfcError)ret);
181 NfcErrorFactory.ThrowNfcException(ret);
186 /// Disables the card emulation mode.
188 /// <since_tizen> 3 </since_tizen>
189 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
190 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
191 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
192 public void DisableCardEmulation()
194 int ret = Interop.Nfc.CardEmulation.DisableCardEmulatiion();
195 if (ret != (int)NfcError.None)
197 Log.Error(Globals.LogTag, "Failed to disable card emulation mode, Error - " + (NfcError)ret);
198 NfcErrorFactory.ThrowNfcException(ret);
203 /// Gets the current card emulation mode.
205 /// <since_tizen> 3 </since_tizen>
206 /// <returns>Enumeration value for the NfcSecureElementCardEmulationMode.</returns>
207 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
208 public NfcSecureElementCardEmulationMode GetCardEmulationMode()
211 int ret = Interop.Nfc.CardEmulation.GetCardEmulationMode(out mode);
212 if (ret != (int)NfcError.None)
214 Log.Error(Globals.LogTag, "Failed to get card emulation mode, Error - " + (NfcError)ret);
217 return (NfcSecureElementCardEmulationMode)mode;
221 /// Gives the priority to the foreground application when dispatching the transaction event.
223 /// <since_tizen> 3 </since_tizen>
224 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
225 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
226 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
227 public void EnableTransactionForegroundDispatch()
229 int ret = Interop.Nfc.EnableTransactionForegroundDispatch();
230 if (ret != (int)NfcError.None)
232 Log.Error(Globals.LogTag, "Failed to enable foreground dispatch, Error - " + (NfcError)ret);
233 NfcErrorFactory.ThrowNfcException(ret);
238 /// Disables the foreground dispatch for the "EVT_TRANSACTION" to the given application.
240 /// <since_tizen> 3 </since_tizen>
241 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
242 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
243 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
244 public void DisableTransactionForegroundDispatch()
246 int ret = Interop.Nfc.DisableTransactionForegroundDispatch();
247 if (ret != (int)NfcError.None)
249 Log.Error(Globals.LogTag, "Failed to disable foreground dispatch, Error - " + (NfcError)ret);
250 NfcErrorFactory.ThrowNfcException(ret);
255 /// Gets the state, whether an application to call this API is currently the activated handler for the specific AID.
257 /// <since_tizen> 3 </since_tizen>
258 /// <returns>'True' when application is currently the activated handler, otherwise 'False'.</returns>
259 /// <param name="seType">The type of the Secure Element.</param>
260 /// <param name="aid">The application ID specified in the ISO/IEC 7816-4.</param>
261 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
262 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
263 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
264 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
265 public bool IsActivatedHandlerForAid(NfcSecureElementType seType, string aid)
267 bool isActivatedHandle = false;
268 int ret = Interop.Nfc.CardEmulation.IsActivatedHandlerForAid((int)seType, aid, out isActivatedHandle);
269 if (ret != (int)NfcError.None)
271 Log.Error(Globals.LogTag, "Failed to check activated handle for aid, Error - " + (NfcError)ret);
272 NfcErrorFactory.ThrowNfcException(ret);
275 return isActivatedHandle;
279 /// Gets the state, whether an application to call this API is currently the activated handler for the category.
281 /// <since_tizen> 3 </since_tizen>
282 /// <returns>'True' when application is currently the activated handler, otherwise 'False'.</returns>
283 /// <param name="seType">The type of the secure element.</param>
284 /// <param name="category">Enumeration value of the category.</param>
285 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
286 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
287 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
288 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
289 public bool IsActivatedHandlerForCategory(NfcSecureElementType seType, NfcCardEmulationCategoryType category)
291 bool isActivatedHandle = false;
292 int ret = Interop.Nfc.CardEmulation.IsActivatedHandlerForCategory((int)seType, (int)category, out isActivatedHandle);
293 if (ret != (int)NfcError.None)
295 Log.Error(Globals.LogTag, "Failed to check activated handle for category, Error - " + (NfcError)ret);
296 NfcErrorFactory.ThrowNfcException(ret);
299 return isActivatedHandle;
303 /// Registers the AID for a specific category.
305 /// <since_tizen> 3 </since_tizen>
306 /// <param name="seType">The type of the secure element.</param>
307 /// <param name="category">Enumeration value of the category.</param>
308 /// <param name="aid">The application ID specified in the ISO/IEC 7816-4.</param>
309 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
310 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
311 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
312 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
313 public void RegisterAid(NfcSecureElementType seType, NfcCardEmulationCategoryType category, string aid)
315 int ret = Interop.Nfc.CardEmulation.RegisterAid((int)seType, (int)category, aid);
316 if (ret != (int)NfcError.None)
318 Log.Error(Globals.LogTag, "Failed to register aid, Error - " + (NfcError)ret);
319 NfcErrorFactory.ThrowNfcException(ret);
324 /// Unregisters a previously registered AID for the specified category.
326 /// <since_tizen> 3 </since_tizen>
327 /// <param name="seType">The type of the secure element.</param>
328 /// <param name="category">Enumeration value of the category.</param>
329 /// <param name="aid">The application ID specified in the ISO/IEC 7816-4.</param>
330 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
331 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
332 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
333 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
334 public void UnregisterAid(NfcSecureElementType seType, NfcCardEmulationCategoryType category, string aid)
336 int ret = Interop.Nfc.CardEmulation.UnregisterAid((int)seType, (int)category, aid);
337 if (ret != (int)NfcError.None)
339 Log.Error(Globals.LogTag, "Failed to unregister aid, Error - " + (NfcError)ret);
340 NfcErrorFactory.ThrowNfcException(ret);
345 /// Sets the application as a preferred handler.
347 /// <since_tizen> 3 </since_tizen>
348 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
349 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
350 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
351 public void SetPreferredApplication()
353 int ret = Interop.Nfc.CardEmulation.SetPreferredHandler();
354 if (ret != (int)NfcError.None)
356 Log.Error(Globals.LogTag, "Failed to set preferred handler, Error - " + (NfcError)ret);
357 NfcErrorFactory.ThrowNfcException(ret);
362 /// Unsets the application as a preferred handler.
364 /// <since_tizen> 3 </since_tizen>
365 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
366 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
367 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
368 public void UnsetPreferredApplication()
370 int ret = Interop.Nfc.CardEmulation.UnsetPreferredHandler();
371 if (ret != (int)NfcError.None)
373 Log.Error(Globals.LogTag, "Failed to unset preferred handler, Error - " + (NfcError)ret);
374 NfcErrorFactory.ThrowNfcException(ret);
379 /// Retrieves all registered AIDs.
381 /// <since_tizen> 3 </since_tizen>
382 /// <returns>The list of NfcRegisteredAidInformation objects.</returns>
383 /// <param name="seType">The type of the secure element.</param>
384 /// <param name="category">Enumeration value of the category.</param>
385 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
386 /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
387 /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
388 /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
389 public IEnumerable<NfcRegisteredAidInformation> GetRegisteredAidInformation(NfcSecureElementType seType, NfcCardEmulationCategoryType category)
391 List<NfcRegisteredAidInformation> infoList = new List<NfcRegisteredAidInformation>();
392 Interop.Nfc.SecureElementRegisteredAidCallback callback = (int type, IntPtr aid, bool readOnly, IntPtr userData) =>
394 if (aid != IntPtr.Zero)
396 NfcRegisteredAidInformation aidInfo = new NfcRegisteredAidInformation((NfcSecureElementType)type, Marshal.PtrToStringAnsi(aid), readOnly);
398 infoList.Add(aidInfo);
402 int ret = Interop.Nfc.CardEmulation.ForeachRegisterdAids((int)seType, (int)category, callback, IntPtr.Zero);
403 if (ret != (int)NfcError.None)
405 Log.Error(Globals.LogTag, "Failed to get all registerd aid informations, Error - " + (NfcError)ret);
406 NfcErrorFactory.ThrowNfcException(ret);
412 private void RegisterSecureElementEvent()
414 _secureElementEventCallback = (int eventType, IntPtr userData) =>
416 NfcSecureElementEvent _eventType = (NfcSecureElementEvent)eventType;
417 SecureElementEventArgs e = new SecureElementEventArgs(_eventType);
418 _secureElementEvent.SafeInvoke(null, e);
421 int ret = Interop.Nfc.SetSecureElementEventCallback(_secureElementEventCallback, IntPtr.Zero);
422 if (ret != (int)NfcError.None)
424 Log.Error(Globals.LogTag, "Failed to set secure element event callback, Error - " + (NfcError)ret);
428 private void UnregisterSecureElementEvent()
430 Interop.Nfc.UnsetSecureElementEventCallback();
433 private void RegisterSecureElementTransactionEvent(NfcSecureElementType seType)
435 _secureElementTransactionEventCallback = (int type, IntPtr aid, int aidSize, IntPtr param, int paramSize, IntPtr userData) =>
437 NfcSecureElementType _secureElementType = (NfcSecureElementType)type;
438 byte[] _aid = NfcConvertUtil.IntLengthIntPtrToByteArray(aid, aidSize);
439 byte[] _param = NfcConvertUtil.IntLengthIntPtrToByteArray(param, paramSize);
440 SecureElementTranscationEventArgs e = new SecureElementTranscationEventArgs(_secureElementType, _aid, _param);
441 _secureElementTransactionEvent.SafeInvoke(null, e);
444 int ret = Interop.Nfc.SetSecureElementTransactionEventCallback((int)seType, _secureElementTransactionEventCallback, IntPtr.Zero);
445 if (ret != (int)NfcError.None)
447 Log.Error(Globals.LogTag, "Failed to set secure element transaction event callback, Error - " + (NfcError)ret);
451 private void UnregisterSecureElementTransactionEvent(NfcSecureElementType seType)
453 Interop.Nfc.UnsetSecureElementTransactionEventCallback((int)seType);
456 private void RegisterHostCardEmulationEvent()
458 _hostCardEmulationEventCallback = (IntPtr handle, int eventType, IntPtr apdu, uint apduLen, IntPtr userData) =>
460 IntPtr _seHandle = handle;
461 NfcHceEvent _hcdEventType = (NfcHceEvent)eventType;
462 byte[] _apdu = NfcConvertUtil.UintLengthIntPtrToByteArray(apdu, apduLen);
463 HostCardEmulationEventArgs e = new HostCardEmulationEventArgs(_seHandle, _hcdEventType, _apdu);
464 _hostCardEmulationEvent.SafeInvoke(null, e);
467 int ret = Interop.Nfc.SetHostCardEmulationEventCallback(_hostCardEmulationEventCallback, IntPtr.Zero);
468 if (ret != (int)NfcError.None)
470 Log.Error(Globals.LogTag, "Failed to set host card emulation event callback, Error - " + (NfcError)ret);
471 NfcErrorFactory.ThrowNfcException(ret);
475 private void UnregisterHostCardEmulationEvent()
477 Interop.Nfc.UnsetHostCardEmulationEventCallback();