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 NFC CardEmulation mode. It allows applications to handle Card Emulation informations.
26 /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
27 public class NfcCardEmulationAdapter : IDisposable
29 private bool disposed = false;
31 private event EventHandler<SecureElementEventArgs> _secureElementEvent;
32 private event EventHandler<SecureElementTranscationEventArgs> _secureElementTransactionEvent;
33 private event EventHandler<HostCardEmulationEventArgs> _hostCardEmulationEvent;
35 private Interop.Nfc.SecureElementEventCallback _secureElementEventCallback;
36 private Interop.Nfc.SecureElementTransactionEventCallback _secureElementTransactionEventCallback;
37 private Interop.Nfc.HostCardEmulationEventCallback _hostCardEmulationEventCallback;
40 /// Event that is called when receiving Secure Element (SIM/UICC(Universal Integrated Circuit Card)) event.
42 public event EventHandler<SecureElementEventArgs> SecureElementEvent
46 if (_secureElementEvent == null)
48 RegisterSecureElementEvent();
50 _secureElementEvent += value;
54 _secureElementEvent -= value;
55 if (_secureElementEvent == null)
57 UnregisterSecureElementEvent();
63 /// Event that is called when receiving Secure Element(SIM/UICC(Universal Integrated Circuit Card)) transaction event for 'ESE(SmartMX)' type.
65 public event EventHandler<SecureElementTranscationEventArgs> EseSecureElementTransactionEvent
69 if (_secureElementTransactionEvent == null)
71 RegisterSecureElementTransactionEvent(NfcSecureElementType.EmbeddedSE);
73 _secureElementTransactionEvent += value;
77 _secureElementTransactionEvent -= value;
78 if (_secureElementTransactionEvent == null)
80 UnregisterSecureElementTransactionEvent(NfcSecureElementType.EmbeddedSE);
86 /// Event that is called when receiving Secure Element(SIM/UICC(Universal Integrated Circuit Card)) transaction event for 'UICC' type.
88 public event EventHandler<SecureElementTranscationEventArgs> UiccSecureElementTransactionEvent
92 if (_secureElementTransactionEvent == null)
94 RegisterSecureElementTransactionEvent(NfcSecureElementType.Uicc);
96 _secureElementTransactionEvent += value;
100 _secureElementTransactionEvent -= value;
101 if (_secureElementTransactionEvent == null)
103 UnregisterSecureElementTransactionEvent(NfcSecureElementType.Uicc);
109 /// Event that is called when when receiving HCE(Host Card Emulation) event.
111 public event EventHandler<HostCardEmulationEventArgs> HostCardEmulationEvent
115 if (_hostCardEmulationEvent == null)
117 RegisterHostCardEmulationEvent();
119 _hostCardEmulationEvent += value;
123 _hostCardEmulationEvent -= value;
124 if (_hostCardEmulationEvent == null)
126 UnregisterHostCardEmulationEvent();
131 internal NfcCardEmulationAdapter()
135 ~NfcCardEmulationAdapter()
140 public void Dispose()
143 GC.SuppressFinalize(this);
146 private void Dispose(bool disposing)
153 // Free managed objects.
155 //Free unmanaged objects
160 /// Enable card emulation mode.
162 public void EnableCardEmulation()
164 int ret = Interop.Nfc.CardEmulation.EnableCardEmulation();
165 if (ret != (int)NfcError.None)
167 Log.Error(Globals.LogTag, "Failed to enable card emulation mode, Error - " + (NfcError)ret);
168 NfcErrorFactory.ThrowNfcException(ret);
173 /// Disable card emulation mode.
175 public void DisableCardEmulation()
177 int ret = Interop.Nfc.CardEmulation.DisableCardEmulatiion();
178 if (ret != (int)NfcError.None)
180 Log.Error(Globals.LogTag, "Failed to disable card emulation mode, Error - " + (NfcError)ret);
181 NfcErrorFactory.ThrowNfcException(ret);
186 /// Get the current card emulation mode.
188 /// <returns>Enumeration value of NfcSecureElementCardEmulationMode.</returns>
189 public NfcSecureElementCardEmulationMode GetCardEmulationMode()
192 int ret = Interop.Nfc.CardEmulation.GetCardEmulationMode(out mode);
193 if (ret != (int)NfcError.None)
195 Log.Error(Globals.LogTag, "Failed to get card emulation mode, Error - " + (NfcError)ret);
198 return (NfcSecureElementCardEmulationMode)mode;
202 /// Give the priority to the foreground application when dispatching transaction event.
204 public void EnableTransactionForegroundDispatch()
206 int ret = Interop.Nfc.EnableTransactionForegroundDispatch();
207 if (ret != (int)NfcError.None)
209 Log.Error(Globals.LogTag, "Failed to enable foreground dispatch, Error - " + (NfcError)ret);
210 NfcErrorFactory.ThrowNfcException(ret);
215 /// Disable foreground dispatch for "EVT_TRANSACTION" to the givin application.
217 public void DisableTransactionForegroundDispatch()
219 int ret = Interop.Nfc.DisableTransactionForegroundDispatch();
220 if (ret != (int)NfcError.None)
222 Log.Error(Globals.LogTag, "Failed to disable foreground dispatch, Error - " + (NfcError)ret);
223 NfcErrorFactory.ThrowNfcException(ret);
228 /// Gets the state whether an application to call this api is currently the activated handler for specific AID.
230 /// <returns>'True' when application is currently the activated handler, otherwise 'False'.</returns>
231 /// <param name="seType">The type of Secure Element.</param>
232 /// <param name="aid">Application Id, specified in ISO/IEC 7816-4.</param>
233 public bool IsActivatedHandlerForAid(NfcSecureElementType seType, string aid)
235 bool isActivatedHandle = false;
236 int ret = Interop.Nfc.CardEmulation.IsActivatedHandlerForAid((int)seType, aid, out isActivatedHandle);
237 if (ret != (int)NfcError.None)
239 Log.Error(Globals.LogTag, "Failed to check activated handle for aid, Error - " + (NfcError)ret);
240 NfcErrorFactory.ThrowNfcException(ret);
243 return isActivatedHandle;
247 /// Gets the state whether an application to call this api is currently the activated handler for category.
249 /// <returns>'True' when application is currently the activated handler, otherwise 'False'.</returns>
250 /// <param name="seType">The type of Secure Element.</param>
251 /// <param name="category">Enumeration value of category.</param>
252 public bool IsActivatedHandlerForCategory(NfcSecureElementType seType, NfcCardEmulationCategoryType category)
254 bool isActivatedHandle = false;
255 int ret = Interop.Nfc.CardEmulation.IsActivatedHandlerForCategory((int)seType, (int)category, out isActivatedHandle);
256 if (ret != (int)NfcError.None)
258 Log.Error(Globals.LogTag, "Failed to check activated handle for category, Error - " + (NfcError)ret);
259 NfcErrorFactory.ThrowNfcException(ret);
262 return isActivatedHandle;
266 /// Registers a AID for a specific category.
268 /// <param name="seType">The type of Secure Element.</param>
269 /// <param name="category">Enumeration value of category.</param>
270 /// <param name="aid">Application Id, specified in ISO/IEC 7816-4.</param>
271 public void RegisterAid(NfcSecureElementType seType, NfcCardEmulationCategoryType category, string aid)
273 int ret = Interop.Nfc.CardEmulation.RegisterAid((int)seType, (int)category, aid);
274 if (ret != (int)NfcError.None)
276 Log.Error(Globals.LogTag, "Failed to register aid, Error - " + (NfcError)ret);
277 NfcErrorFactory.ThrowNfcException(ret);
282 /// Unregisters a previously registered AID for the specified category.
284 /// <param name="seType">The type of Secure Element.</param>
285 /// <param name="category">Enumeration value of category.</param>
286 /// <param name="aid">Application Id, specified in ISO/IEC 7816-4.</param>
287 public void UnregisterAid(NfcSecureElementType seType, NfcCardEmulationCategoryType category, string aid)
289 int ret = Interop.Nfc.CardEmulation.UnregisterAid((int)seType, (int)category, aid);
290 if (ret != (int)NfcError.None)
292 Log.Error(Globals.LogTag, "Failed to unregister aid, Error - " + (NfcError)ret);
293 NfcErrorFactory.ThrowNfcException(ret);
298 /// Sets the application as a preferred handler.
300 public void SetPreferredApplication()
302 int ret = Interop.Nfc.CardEmulation.SetPreferredHandler();
303 if (ret != (int)NfcError.None)
305 Log.Error(Globals.LogTag, "Failed to set preferred handler, Error - " + (NfcError)ret);
306 NfcErrorFactory.ThrowNfcException(ret);
311 /// Unsets the application as a preferred handler.
313 public void UnsetPreferredApplication()
315 int ret = Interop.Nfc.CardEmulation.UnsetPreferredHandler();
316 if (ret != (int)NfcError.None)
318 Log.Error(Globals.LogTag, "Failed to unset preferred handler, Error - " + (NfcError)ret);
319 NfcErrorFactory.ThrowNfcException(ret);
324 /// Retrieves all registered AID.
326 /// <returns>List of NfcRegisteredAidInformation objects.</returns>
327 /// <param name="seType">The type of Secure Element.</param>
328 /// <param name="category">Enumeration value of category.</param>
329 public IEnumerable<NfcRegisteredAidInformation> ForeachRegisteredAidInformations(NfcSecureElementType seType, NfcCardEmulationCategoryType category)
331 List<NfcRegisteredAidInformation> infoList = new List<NfcRegisteredAidInformation>();
332 Interop.Nfc.SecureElementRegisteredAidCallback callback = (int type, IntPtr aid, bool readOnly, IntPtr userData) =>
334 if (aid != IntPtr.Zero)
336 NfcRegisteredAidInformation aidInfo = new NfcRegisteredAidInformation();
338 aidInfo.SeType = (NfcSecureElementType)type;
339 aidInfo.Aid = Marshal.PtrToStringAnsi(aid);
340 aidInfo.ReadOnly = readOnly;
342 infoList.Add(aidInfo);
346 int ret = Interop.Nfc.CardEmulation.ForeachRegisterdAids((int)seType, (int)category, callback, IntPtr.Zero);
347 if (ret != (int)NfcError.None)
349 Log.Error(Globals.LogTag, "Failed to get all registerd aid informations, Error - " + (NfcError)ret);
350 NfcErrorFactory.ThrowNfcException(ret);
356 private void RegisterSecureElementEvent()
358 _secureElementEventCallback = (int eventType, IntPtr userData) =>
360 NfcSecureElementEvent _eventType = (NfcSecureElementEvent)eventType;
361 SecureElementEventArgs e = new SecureElementEventArgs(_eventType);
362 _secureElementEvent.SafeInvoke(null, e);
365 int ret = Interop.Nfc.SetSecureElementEventCallback(_secureElementEventCallback, IntPtr.Zero);
366 if (ret != (int)NfcError.None)
368 Log.Error(Globals.LogTag, "Failed to set secure element event callback, Error - " + (NfcError)ret);
372 private void UnregisterSecureElementEvent()
374 Interop.Nfc.UnsetSecureElementEventCallback();
377 private void RegisterSecureElementTransactionEvent(NfcSecureElementType seType)
379 _secureElementTransactionEventCallback = (int type, IntPtr aid, int aidSize, IntPtr param, int paramSize, IntPtr userData) =>
381 NfcSecureElementType _secureElementType = (NfcSecureElementType)type;
382 byte[] _aid = NfcConvertUtil.IntLengthIntPtrToByteArray(aid, aidSize);
383 byte[] _param = NfcConvertUtil.IntLengthIntPtrToByteArray(param, paramSize);
384 SecureElementTranscationEventArgs e = new SecureElementTranscationEventArgs(_secureElementType, _aid, _param);
385 _secureElementTransactionEvent.SafeInvoke(null, e);
388 int ret = Interop.Nfc.SetSecureElementTransactionEventCallback((int)seType, _secureElementTransactionEventCallback, IntPtr.Zero);
389 if (ret != (int)NfcError.None)
391 Log.Error(Globals.LogTag, "Failed to set secure element transaction event callback, Error - " + (NfcError)ret);
395 private void UnregisterSecureElementTransactionEvent(NfcSecureElementType seType)
397 Interop.Nfc.UnsetSecureElementTransactionEventCallback((int)seType);
400 private void RegisterHostCardEmulationEvent()
402 _hostCardEmulationEventCallback = (IntPtr handle, int eventType, IntPtr apdu, uint apduLen, IntPtr userData) =>
404 IntPtr _seHandle = handle;
405 NfcHceEvent _hcdEventType = (NfcHceEvent)eventType;
406 byte[] _apdu = NfcConvertUtil.UintLengthIntPtrToByteArray(apdu, apduLen);
407 HostCardEmulationEventArgs e = new HostCardEmulationEventArgs(_seHandle, _hcdEventType, _apdu);
408 _hostCardEmulationEvent.SafeInvoke(null, e);
411 int ret = Interop.Nfc.SetHostCardEmulationEventCallback(_hostCardEmulationEventCallback, IntPtr.Zero);
412 if (ret != (int)NfcError.None)
414 Log.Error(Globals.LogTag, "Failed to set host card emulation event callback, Error - " + (NfcError)ret);
415 NfcErrorFactory.ThrowNfcException(ret);
419 private void UnregisterHostCardEmulationEvent()
421 Interop.Nfc.UnsetHostCardEmulationEventCallback();