0eb894ec1d983af3e922fc8a66bded47f389cdff
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcNdefRecord.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
21 namespace Tizen.Network.Nfc
22 {
23     /// <summary>
24     /// A class for the NDEF Record information. It allows applications to use the NDEF Record information.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class NfcNdefRecord : IDisposable
28     {
29         private bool disposed = false;
30         private IntPtr _recordHandle = IntPtr.Zero;
31
32         /// <summary>
33         /// The record ID.
34         /// </summary>
35         /// <since_tizen> 3 </since_tizen>
36         public byte[] Id
37         {
38             get
39             {
40                 IntPtr id;
41                 int idLength;
42                 int ret = Interop.Nfc.NdefRecord.GetId(_recordHandle, out id, out idLength);
43                 if (ret != (int)NfcError.None)
44                 {
45                     Log.Error(Globals.LogTag, "Failed to get id, Error - " + (NfcError)ret);
46                     return null;
47                 }
48
49                 return NfcConvertUtil.IntLengthIntPtrToByteArray(id, idLength);
50             }
51         }
52
53         /// <summary>
54         /// The record payload.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         public byte[] Payload
58         {
59             get
60             {
61                 IntPtr payload;
62                 uint payloadLength;
63                 int ret = Interop.Nfc.NdefRecord.GetPayload(_recordHandle, out payload, out payloadLength);
64                 if (ret != (int)NfcError.None)
65                 {
66                     Log.Error(Globals.LogTag, "Failed to get payload, Error - " + (NfcError)ret);
67                     return null;
68                 }
69
70                 return NfcConvertUtil.UintLengthIntPtrToByteArray(payload, payloadLength);
71             }
72         }
73
74         /// <summary>
75         /// The record type.
76         /// </summary>
77         /// <since_tizen> 3 </since_tizen>
78         public byte[] Type
79         {
80             get
81             {
82                 IntPtr type;
83                 int typeSize;
84                 int ret = Interop.Nfc.NdefRecord.GetType(_recordHandle, out type, out typeSize);
85                 if (ret != (int)NfcError.None)
86                 {
87                     Log.Error(Globals.LogTag, "Failed to get payload, Error - " + (NfcError)ret);
88                     return null;
89                 }
90
91                 return NfcConvertUtil.IntLengthIntPtrToByteArray(type, typeSize);
92             }
93         }
94
95         /// <summary>
96         /// The record TNF (Type Name Format) value.
97         /// </summary>
98         /// <since_tizen> 3 </since_tizen>
99         public NfcRecordTypeNameFormat Tnf
100         {
101             get
102             {
103                 int tnf;
104                 int ret = Interop.Nfc.NdefRecord.GetTnf(_recordHandle, out tnf);
105                 if (ret != (int)NfcError.None)
106                 {
107                     Log.Error(Globals.LogTag, "Failed to get tnf, Error - " + (NfcError)ret);
108                 }
109                 return (NfcRecordTypeNameFormat)tnf;
110             }
111         }
112
113         /// <summary>
114         /// The text of the text type NDEF record.
115         /// </summary>
116         /// <since_tizen> 3 </since_tizen>
117         public string Text
118         {
119             get
120             {
121                 string text;
122                 int ret = Interop.Nfc.NdefRecord.GetText(_recordHandle, out text);
123                 if (ret != (int)NfcError.None)
124                 {
125                     Log.Error(Globals.LogTag, "Failed to get text, Error - " + (NfcError)ret);
126                 }
127                 return text;
128             }
129         }
130
131         /// <summary>
132         /// The language code of the text type NDEF record.
133         /// </summary>
134         /// <since_tizen> 3 </since_tizen>
135         public string LanguageCode
136         {
137             get
138             {
139                 string languageCode;
140                 int ret = Interop.Nfc.NdefRecord.GetLanguageCode(_recordHandle, out languageCode);
141                 if (ret != (int)NfcError.None)
142                 {
143                     Log.Error(Globals.LogTag, "Failed to get language code, Error - " + (NfcError)ret);
144                 }
145                 return languageCode;
146             }
147         }
148
149         /// <summary>
150         /// The encoding type of the text type NDEF record.
151         /// </summary>
152         /// <since_tizen> 3 </since_tizen>
153         public NfcEncodeType EncodeType
154         {
155             get
156             {
157                 int encodeType;
158                 int ret = Interop.Nfc.NdefRecord.GetEncodeType(_recordHandle, out encodeType);
159                 if (ret != (int)NfcError.None)
160                 {
161                     Log.Error(Globals.LogTag, "Failed to get encode type, Error - " + (NfcError)ret);
162                 }
163                 return (NfcEncodeType)encodeType;
164             }
165         }
166
167         /// <summary>
168         /// The URI of the URI type NDEF record.
169         /// </summary>
170         /// <since_tizen> 3 </since_tizen>
171         public string Uri
172         {
173             get
174             {
175                 string uri;
176                 int ret = Interop.Nfc.NdefRecord.GetUri(_recordHandle, out uri);
177                 if (ret != (int)NfcError.None)
178                 {
179                     Log.Error(Globals.LogTag, "Failed to get uri, Error - " + (NfcError)ret);
180                 }
181                 return uri;
182             }
183         }
184
185         /// <summary>
186         /// The mime type of the mime type NDEF record.
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         public string MimeType
190         {
191             get
192             {
193                 string mimeType;
194                 int ret = Interop.Nfc.NdefRecord.GetMimeType(_recordHandle, out mimeType);
195                 if (ret != (int)NfcError.None)
196                 {
197                     Log.Error(Globals.LogTag, "Failed to get mime type, Error - " + (NfcError)ret);
198                 }
199                 return mimeType;
200             }
201         }
202
203         /// <summary>
204         /// Creates a record with a given parameter value.
205         /// </summary>
206         /// <since_tizen> 3 </since_tizen>
207         /// <param name="format">The type name format.</param>
208         /// <param name="type">The specified type name.</param>
209         /// <param name="id">The record ID.</param>
210         /// <param name="payload">The payload of this record.</param>
211         /// <param name="paloadLength">The byte size of the payload.</param>
212         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
213         /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
214         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
215         public NfcNdefRecord(NfcRecordTypeNameFormat format, byte[] type, byte[] id, byte[] payload, uint paloadLength)
216         {
217             int ret = Interop.Nfc.NdefRecord.Create(out _recordHandle, (int)format, type, type.Length, id, id.Length, payload, paloadLength);
218
219             if (ret != (int)NfcError.None)
220             {
221                 Log.Error(Globals.LogTag, "Failed to create Ndef record, Error - " + (NfcError)ret);
222                 NfcErrorFactory.ThrowNfcException(ret);
223             }
224         }
225
226         /// <summary>
227         /// Creates a record with the text type payload.
228         /// </summary>
229         /// <since_tizen> 3 </since_tizen>
230         /// <param name="text">The encoded text.</param>
231         /// <param name="languageCode">The language code string value followed by the IANA [RFC 3066] (ex: en-US, ko-KR).</param>
232         /// <param name="encode">The encoding type.</param>
233         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
234         /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
235         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
236         public NfcNdefRecord(string text, string languageCode, NfcEncodeType encode)
237         {
238             int ret = Interop.Nfc.NdefRecord.CreateText(out _recordHandle, text, languageCode, (int)encode);
239
240             if (ret != (int)NfcError.None)
241             {
242                 Log.Error(Globals.LogTag, "Failed to create ndef Text record, Error - " + (NfcError)ret);
243                 NfcErrorFactory.ThrowNfcException(ret);
244             }
245         }
246
247         /// <summary>
248         /// Creates a record with the URI type payload.
249         /// </summary>
250         /// <since_tizen> 3 </since_tizen>
251         /// <param name="uri">The URI string that will be stored in the payload.</param>
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 NfcNdefRecord(string uri)
256         {
257             int ret = Interop.Nfc.NdefRecord.CreateUri(out _recordHandle, uri);
258
259             if (ret != (int)NfcError.None)
260             {
261                 Log.Error(Globals.LogTag, "Failed to create ndef Uri record, Error - " + (NfcError)ret);
262                 NfcErrorFactory.ThrowNfcException(ret);
263             }
264         }
265
266         /// <summary>
267         /// Creates a record with the mime type payload.
268         /// </summary>
269         /// <since_tizen> 3 </since_tizen>
270         /// <param name="mimeType">The mime type [RFC 2046] (ex. text/plain, image/jpeg ). This value is stored in the type field.</param>
271         /// <param name="data">The data in the form of the bytes array.</param>
272         /// <param name="dataSize">The size of the data.</param>
273         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
274         /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
275         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
276         public NfcNdefRecord(string mimeType, byte[] data, uint dataSize)
277         {
278             int ret = Interop.Nfc.NdefRecord.CreateMime(out _recordHandle, mimeType, data, dataSize);
279
280             if (ret != (int)NfcError.None)
281             {
282                 Log.Error(Globals.LogTag, "Failed to create ndef Mime record, Error - " + (NfcError)ret);
283                 NfcErrorFactory.ThrowNfcException(ret);
284             }
285         }
286
287         /// <summary>
288         /// NfcNdefRecord destructor.
289         /// </summary>
290         ~NfcNdefRecord()
291         {
292             Dispose(false);
293         }
294
295         /// <summary>
296         /// Dispose
297         /// </summary>
298         public void Dispose()
299         {
300             Dispose(true);
301             GC.SuppressFinalize(this);
302         }
303
304         private void Dispose(bool disposing)
305         {
306             if (disposed)
307                 return;
308
309             if (disposing)
310             {
311                 // Free managed objects.
312                 int ret = Interop.Nfc.NdefRecord.Destroy(_recordHandle);
313
314                 if (ret != (int)NfcError.None)
315                 {
316                     Log.Error(Globals.LogTag, "Failed to destroy ndef record, Error - " + (NfcError)ret);
317                 }
318             }
319             //Free unmanaged objects
320             disposed = true;
321         }
322
323         internal IntPtr GetHandle()
324         {
325             return _recordHandle;
326         }
327     }
328 }