Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcManagerImpl.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
21
22 namespace Tizen.Network.Nfc
23 {
24     static internal class Globals
25     {
26         internal const string LogTag = "Tizen.Network.Nfc";
27     }
28
29     internal static class NfcConvertUtil
30     {
31         internal static byte[] IntLengthIntPtrToByteArray(IntPtr nativeValue, int length)
32         {
33             byte[] value = new byte[length];
34             if (nativeValue != IntPtr.Zero)
35             {
36                 Marshal.Copy(nativeValue, value, 0, length);
37             }
38             return value;
39         }
40
41         internal static byte[] UintLengthIntPtrToByteArray(IntPtr nativeValue, uint length)
42         {
43             byte[] value = new byte[length];
44
45             if (nativeValue != IntPtr.Zero)
46             {
47                 for (int i = 0; i < length; i++)
48                 {
49                     value[i] = Marshal.ReadByte(nativeValue);
50                     nativeValue += sizeof(byte);
51                 }
52             }
53             return value;
54         }
55     }
56
57     internal partial class NfcManagerImpl : IDisposable
58     {
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();
63
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;
67
68         internal static NfcManagerImpl Instance
69         {
70             get
71             {
72                 return _instance;
73             }
74         }
75
76         internal NfcTagAdapter TagAdapter
77         {
78             get
79             {
80                 return _instanceTagAdapter;
81             }
82         }
83
84         internal NfcP2pAdapter P2pAdapter
85         {
86             get
87             {
88                 return _instanceP2pAdapter;
89             }
90         }
91
92         internal NfcCardEmulationAdapter CardEmulationAdapter
93         {
94             get
95             {
96                 return _instanceCardEmulationAdapter;
97             }
98         }
99
100         internal bool IsSupported
101         {
102             get
103             {
104                 bool support = Interop.Nfc.IsSupported();
105
106                 return support;
107             }
108         }
109
110         internal bool IsActivated
111         {
112             get
113             {
114                 bool active = Interop.Nfc.IsActivated();
115
116                 return active;
117             }
118         }
119
120         internal NfcNdefMessage CachedNdefMessage
121         {
122             get
123             {
124                 IntPtr ndef = IntPtr.Zero; ;
125                 int ret = Interop.Nfc.GetCachedMessage(out ndef);
126                 if (ret != (int)NfcError.None)
127                 {
128                     Log.Error(Globals.LogTag, "Failed to get cached ndef message, Error - " + (NfcError)ret);
129                 }
130
131                 NfcNdefMessage ndefMessage = new NfcNdefMessage(ndef);
132                 return ndefMessage;
133             }
134         }
135
136         internal NfcTagFilterType TagFilterType
137         {
138             get
139             {
140                 int type = Interop.Nfc.GetTagFilter();
141
142                 return (NfcTagFilterType)type;
143             }
144             set
145             {
146                 Interop.Nfc.SetTagFilter((int)value);
147             }
148         }
149
150         internal NfcSecureElementType SecureElementType
151         {
152             get
153             {
154                 int type;
155                 int ret = Interop.Nfc.GetSecureElementType(out type);
156                 if (ret != (int)NfcError.None)
157                 {
158                     Log.Error(Globals.LogTag, "Failed to get secure element type, Error - " + (NfcError)ret);
159                 }
160                 return (NfcSecureElementType)type;
161             }
162             set
163             {
164                 int ret = Interop.Nfc.SetSecureElementType((int)value);
165                 if (ret != (int)NfcError.None)
166                 {
167                     Log.Error(Globals.LogTag, "Failed to set secure element type, Error - " + (NfcError)ret);
168                 }
169             }
170         }
171
172         internal bool SystemHandlerEnabled
173         {
174             get
175             {
176                 bool systemhandler = Interop.Nfc.IsSystemHandlerEnabled();
177
178                 return systemhandler;
179             }
180             set
181             {
182                 int ret = Interop.Nfc.SetSystemHandlerEnable(value);
183                 if (ret != (int)NfcError.None)
184                 {
185                     Log.Error(Globals.LogTag, "Failed to enable system handler, Error - " + (NfcError)ret);
186                 }
187             }
188         }
189
190         private NfcManagerImpl()
191         {
192             Initialize();
193         }
194
195         ~NfcManagerImpl()
196         {
197             Dispose(false);
198         }
199
200         public void Dispose()
201         {
202             Dispose(true);
203             GC.SuppressFinalize(this);
204         }
205
206         private void Dispose(bool disposing)
207         {
208             if (disposed)
209                 return;
210
211             if (disposing)
212             {
213                 // Free managed objects.
214             }
215             //Free unmanaged objects
216             Deinitialize();
217             disposed = true;
218         }
219
220         private void Initialize()
221         {
222             int ret = Interop.Nfc.Initialize();
223             if (ret != (int)NfcError.None)
224             {
225                 Log.Error(Globals.LogTag, "Failed to Initialize Nfc, Error - " + (NfcError)ret);
226                 NfcErrorFactory.ThrowNfcException(ret);
227             }
228         }
229
230         private void Deinitialize()
231         {
232             int ret = Interop.Nfc.Deinitialize();
233             if (ret != (int)NfcError.None)
234             {
235                 Log.Error(Globals.LogTag, "Failed to Deinitialize Nfc, Error - " + (NfcError)ret);
236             }
237         }
238
239         internal Task SetActivationAsync(bool activation)
240         {
241             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
242             IntPtr id = IntPtr.Zero;
243             lock (_callback_map)
244             {
245                 id = (IntPtr)_requestId++;
246                 _callback_map[id] = (error, key) =>
247                 {
248                     Log.Debug(Globals.LogTag, "nfc activated");
249                     if (error != (int)NfcError.None)
250                     {
251                         Log.Error(Globals.LogTag, "Error occurs during Nfc activating, " + (NfcError)error);
252                         task.SetException(new InvalidOperationException("Error occurs during Nfc activating, " + (NfcError)error));
253                     }
254                     task.SetResult(true);
255                     lock (_callback_map)
256                     {
257                         _callback_map.Remove(key);
258                     }
259                 };
260
261                 int ret = Interop.Nfc.SetActivation(activation, _callback_map[id], id);
262                 if (ret != (int)NfcError.None)
263                 {
264                     Log.Error(Globals.LogTag, "Failed to activate nfc, Error - " + (NfcError)ret);
265                     NfcErrorFactory.ThrowNfcException(ret);
266                 }
267             }
268             return task.Task;
269         }
270     }
271 }