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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
22 namespace Tizen.Network.Nfc
24 static internal class Globals
26 internal const string LogTag = "Tizen.Network.Nfc";
29 internal static class NfcConvertUtil
31 internal static byte[] IntLengthIntPtrToByteArray(IntPtr nativeValue, int length)
33 byte[] value = new byte[length];
34 if (nativeValue != IntPtr.Zero)
36 Marshal.Copy(nativeValue, value, 0, length);
41 internal static byte[] UintLengthIntPtrToByteArray(IntPtr nativeValue, uint length)
43 byte[] value = new byte[length];
45 if (nativeValue != IntPtr.Zero)
47 for (int i = 0; i < length; i++)
49 value[i] = Marshal.ReadByte(nativeValue);
50 nativeValue += sizeof(byte);
57 internal partial class NfcManagerImpl : IDisposable
59 private static readonly NfcManagerImpl _instance = new NfcManagerImpl();
60 private static readonly NfcTagAdapter _instanceTagAdapter = new NfcTagAdapter();
61 private static readonly NfcP2pAdapter _instanceP2pAdapter = new NfcP2pAdapter();
62 private static readonly NfcCardEmulationAdapter _instanceCardEmulationAdapter = new NfcCardEmulationAdapter();
64 private Dictionary<IntPtr, Interop.Nfc.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.Nfc.VoidCallback>();
65 private int _requestId = 0;
66 private bool disposed = false;
68 internal static NfcManagerImpl Instance
76 internal NfcTagAdapter TagAdapter
80 return _instanceTagAdapter;
84 internal NfcP2pAdapter P2pAdapter
88 return _instanceP2pAdapter;
92 internal NfcCardEmulationAdapter CardEmulationAdapter
96 return _instanceCardEmulationAdapter;
100 internal bool IsSupported
104 bool support = Interop.Nfc.IsSupported();
110 internal bool IsActivated
114 bool active = Interop.Nfc.IsActivated();
120 internal NfcNdefMessage CachedNdefMessage
124 IntPtr ndef = IntPtr.Zero; ;
125 int ret = Interop.Nfc.GetCachedMessage(out ndef);
126 if (ret != (int)NfcError.None)
128 Log.Error(Globals.LogTag, "Failed to get cached ndef message, Error - " + (NfcError)ret);
131 NfcNdefMessage ndefMessage = new NfcNdefMessage(ndef);
136 internal NfcTagFilterType TagFilterType
140 int type = Interop.Nfc.GetTagFilter();
142 return (NfcTagFilterType)type;
146 Interop.Nfc.SetTagFilter((int)value);
150 internal NfcSecureElementType SecureElementType
155 int ret = Interop.Nfc.GetSecureElementType(out type);
156 if (ret != (int)NfcError.None)
158 Log.Error(Globals.LogTag, "Failed to get secure element type, Error - " + (NfcError)ret);
160 return (NfcSecureElementType)type;
164 int ret = Interop.Nfc.SetSecureElementType((int)value);
165 if (ret != (int)NfcError.None)
167 Log.Error(Globals.LogTag, "Failed to set secure element type, Error - " + (NfcError)ret);
172 internal bool SystemHandlerEnabled
176 bool systemhandler = Interop.Nfc.IsSystemHandlerEnabled();
178 return systemhandler;
182 int ret = Interop.Nfc.SetSystemHandlerEnable(value);
183 if (ret != (int)NfcError.None)
185 Log.Error(Globals.LogTag, "Failed to enable system handler, Error - " + (NfcError)ret);
190 private NfcManagerImpl()
200 public void Dispose()
203 GC.SuppressFinalize(this);
206 private void Dispose(bool disposing)
213 // Free managed objects.
215 //Free unmanaged objects
220 private void Initialize()
222 int ret = Interop.Nfc.Initialize();
223 if (ret != (int)NfcError.None)
225 Log.Error(Globals.LogTag, "Failed to Initialize Nfc, Error - " + (NfcError)ret);
226 NfcErrorFactory.ThrowNfcException(ret);
230 private void Deinitialize()
232 int ret = Interop.Nfc.Deinitialize();
233 if (ret != (int)NfcError.None)
235 Log.Error(Globals.LogTag, "Failed to Deinitialize Nfc, Error - " + (NfcError)ret);
239 internal Task SetActivationAsync(bool activation)
241 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
242 IntPtr id = IntPtr.Zero;
245 id = (IntPtr)_requestId++;
246 _callback_map[id] = (error, key) =>
248 Log.Debug(Globals.LogTag, "nfc activated");
249 if (error != (int)NfcError.None)
251 Log.Error(Globals.LogTag, "Error occurs during Nfc activating, " + (NfcError)error);
252 task.SetException(new InvalidOperationException("Error occurs during Nfc activating, " + (NfcError)error));
254 task.SetResult(true);
257 _callback_map.Remove(key);
261 int ret = Interop.Nfc.SetActivation(activation, _callback_map[id], id);
262 if (ret != (int)NfcError.None)
264 Log.Error(Globals.LogTag, "Failed to activate nfc, Error - " + (NfcError)ret);
265 NfcErrorFactory.ThrowNfcException(ret);