Fix XML Doc Build Warning for Nfc
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcTag.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.Runtime.InteropServices;
19 using System.Collections.Generic;
20 using System.Threading.Tasks;
21
22 namespace Tizen.Network.Nfc
23 {
24     /// <summary>
25     /// The class for managing the Tag information.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class NfcTag : IDisposable
29     {
30         private bool disposed = false;
31         private IntPtr _tagHandle = IntPtr.Zero;
32
33         /// <summary>
34         /// The type of the NFC tag.
35         /// </summary>
36         /// <since_tizen> 3 </since_tizen>
37         public NfcTagType Type
38         {
39             get
40             {
41                 int type;
42                 int ret = Interop.Nfc.Tag.GetType(_tagHandle, out type);
43                 if (ret != (int)NfcError.None)
44                 {
45                     Log.Error(Globals.LogTag, "Failed to get tag type, Error - " + (NfcError)ret);
46                 }
47                 return (NfcTagType)type;
48             }
49         }
50
51         /// <summary>
52         /// Whether the given NFC tag supports the NDEF messages.
53         /// </summary>
54         /// <since_tizen> 3 </since_tizen>
55         public bool IsSupportNdef
56         {
57             get
58             {
59                 bool isSupport;
60                 int ret = Interop.Nfc.Tag.IsSupportNdef(_tagHandle, out isSupport);
61                 if (ret != (int)NfcError.None)
62                 {
63                     Log.Error(Globals.LogTag, "Failed to get support state, Error - " + (NfcError)ret);
64                 }
65                 return isSupport;
66
67             }
68         }
69
70         /// <summary>
71         /// The maximum NDEF message size that can be stored in the NFC tag.
72         /// </summary>
73         /// <since_tizen> 3 </since_tizen>
74         public uint MaximumNdefSize
75         {
76             get
77             {
78                 uint maxSize;
79                 int ret = Interop.Nfc.Tag.GetMaximumNdefSize(_tagHandle, out maxSize);
80                 if (ret != (int)NfcError.None)
81                 {
82                     Log.Error(Globals.LogTag, "Failed to get max ndef size, Error - " + (NfcError)ret);
83                 }
84                 return maxSize;
85             }
86         }
87
88         /// <summary>
89         /// The size of the NDEF message stored in the tag.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         public uint NdefSize
93         {
94             get
95             {
96                 uint ndefSize;
97                 int ret = Interop.Nfc.Tag.GetNdefSize(_tagHandle, out ndefSize);
98                 if (ret != (int)NfcError.None)
99                 {
100                     Log.Error(Globals.LogTag, "Failed to get ndef size, Error - " + (NfcError)ret);
101                 }
102                 return ndefSize;
103             }
104         }
105
106         internal NfcTag(IntPtr handle)
107         {
108             _tagHandle = handle;
109         }
110
111         /// <summary>
112         /// NfcTag destructor.
113         /// </summary>
114         ~NfcTag()
115         {
116             Dispose(false);
117         }
118
119         /// <summary>
120         /// Dispose
121         /// </summary>
122         public void Dispose()
123         {
124             Dispose(true);
125             GC.SuppressFinalize(this);
126         }
127
128         private void Dispose(bool disposing)
129         {
130             if (disposed)
131                 return;
132
133             if (disposing)
134             {
135                 // Free managed objects.
136             }
137             //Free unmanaged objects
138             disposed = true;
139         }
140
141         /// <summary>
142         /// Retrieves all the tag information.
143         /// </summary>
144         /// <since_tizen> 3 </since_tizen>
145         /// <returns>The list of the NfcTagInformation objects.</returns>
146         /// <privilege>http://tizen.org/privilege/nfc</privilege>
147         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
148         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
149         public IEnumerable<NfcTagInformation> ForeachInformation()
150         {
151             List<NfcTagInformation> infoList = new List<NfcTagInformation>();
152             Interop.Nfc.TagInformationCallback callback = (IntPtr key, IntPtr infoValue, int valueSize, IntPtr userData) =>
153             {
154                 if (key != IntPtr.Zero && infoValue != IntPtr.Zero)
155                 {
156                     NfcTagInformation tagInfo = new NfcTagInformation(Marshal.PtrToStringAnsi(key), new byte[valueSize]);
157
158                     Marshal.Copy(infoValue, tagInfo.InformationValue, 0, valueSize);
159
160                     infoList.Add(tagInfo);
161
162                     return true;
163                 }
164                 return false;
165             };
166
167             int ret = Interop.Nfc.Tag.ForeachInformation(_tagHandle, callback, IntPtr.Zero);
168             if (ret != (int)NfcError.None)
169             {
170                 Log.Error(Globals.LogTag, "Failed to get all Tag information, Error - " + (NfcError)ret);
171                 NfcErrorFactory.ThrowNfcException(ret);
172             }
173
174             return infoList;
175         }
176
177         /// <summary>
178         /// Transceives the data of the raw format card.
179         /// </summary>
180         /// <since_tizen> 3 </since_tizen>
181         /// <param name="buffer">The binary data for a parameter or additional commands.</param>
182         /// <privilege>http://tizen.org/privilege/nfc</privilege>
183         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
184         /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
185         /// <exception cref="InvalidOperationException">Thrown when the the method fails due to an invalid operation.</exception>
186         public Task<byte[]> TransceiveAsync(byte[] buffer)
187         {
188             var task = new TaskCompletionSource<byte[]>();
189
190             byte[] resultBuffer = null;
191             Interop.Nfc.TagTransceiveCompletedCallback callback = (int result, IntPtr resultData, int dataSize, IntPtr userData) =>
192             {
193                 if (result == (int)NfcError.None)
194                 {
195                     resultBuffer = new byte[dataSize];
196                     Marshal.Copy(resultData, resultBuffer, 0, dataSize);
197                     task.SetResult(resultBuffer);
198                 }
199                 return;
200             };
201
202             int ret = Interop.Nfc.Tag.Transceive(_tagHandle, buffer, buffer.Length, callback, IntPtr.Zero);
203             if (ret != (int)NfcError.None)
204             {
205                 Log.Error(Globals.LogTag, "Failed to transceive data, Error - " + (NfcError)ret);
206                 NfcErrorFactory.ThrowNfcException(ret);
207             }
208
209             return task.Task;
210         }
211
212         /// <summary>
213         /// Reads the NDEF formatted data from the NFC tag.
214         /// </summary>
215         /// <since_tizen> 3 </since_tizen>
216         /// <privilege>http://tizen.org/privilege/nfc</privilege>
217         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
218         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
219         public Task<NfcNdefMessage> ReadNdefMessageAsync()
220         {
221             var task = new TaskCompletionSource<NfcNdefMessage>();
222
223             NfcNdefMessage ndefMsg = null;
224             Interop.Nfc.TagReadCompletedCallback callback = (int result, IntPtr ndefMessage, IntPtr userData) =>
225             {
226                 if (result == (int)NfcError.None)
227                 {
228                     ndefMsg = new NfcNdefMessage(ndefMessage);
229                     task.SetResult(ndefMsg);
230
231                     return true;
232                 }
233                 return false;
234             };
235
236             int ret = Interop.Nfc.Tag.ReadNdef(_tagHandle, callback, IntPtr.Zero);
237             if (ret != (int)NfcError.None)
238             {
239                 Log.Error(Globals.LogTag, "Failed to read ndef message, Error - " + (NfcError)ret);
240                 NfcErrorFactory.ThrowNfcException(ret);
241             }
242
243             return task.Task;
244         }
245
246         /// <summary>
247         /// Writes the NDEF formatted data.
248         /// </summary>
249         /// <since_tizen> 3 </since_tizen>
250         /// <param name="ndefMessage">The NfcNdefMessage object.</param>
251         /// <privilege>http://tizen.org/privilege/nfc</privilege>
252         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
253         /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
254         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
255         public Task<NfcError> WriteNdefMessageAsync(NfcNdefMessage ndefMessage)
256         {
257             var task = new TaskCompletionSource<NfcError>();
258
259             Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
260             {
261                 task.SetResult((NfcError)result);
262                 return;
263             };
264
265             int ret = Interop.Nfc.Tag.WriteNdef(_tagHandle, ndefMessage.GetHandle(), callback, IntPtr.Zero);
266             if (ret != (int)NfcError.None)
267             {
268                 Log.Error(Globals.LogTag, "Failed to write ndef message, Error - " + (NfcError)ret);
269                 NfcErrorFactory.ThrowNfcException(ret);
270             }
271
272             return task.Task;
273         }
274
275         /// <summary>
276         /// Formats the detected tag that can store the NDEF message.
277         /// </summary>
278         /// <since_tizen> 3 </since_tizen>
279         /// <param name="keyValue">The key value that may need to format the tag.</param>
280         /// <privilege>http://tizen.org/privilege/nfc</privilege>
281         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
282         /// <exception cref="ArgumentException">Thrown when method fails due to an invalid parameter.</exception>
283         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
284         public Task<NfcError> FormatNdefMessageAsync(byte[] keyValue)
285         {
286             var task = new TaskCompletionSource<NfcError>();
287
288             Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
289             {
290                 task.SetResult((NfcError)result);
291                 return;
292             };
293
294             int ret = Interop.Nfc.Tag.FormatNdef(_tagHandle, keyValue, keyValue.Length, callback, IntPtr.Zero);
295             if (ret != (int)NfcError.None)
296             {
297                 Log.Error(Globals.LogTag, "Failed to format ndef message, Error - " + (NfcError)ret);
298                 NfcErrorFactory.ThrowNfcException(ret);
299             }
300
301             return task.Task;
302         }
303     }
304 }