Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcSecureElement.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 managing the Secure Element information.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class NfcSecureElement : IDisposable
28     {
29         private IntPtr _secureElementHandle = IntPtr.Zero;
30         private bool disposed = false;
31
32         internal NfcSecureElement(IntPtr handle)
33         {
34             _secureElementHandle = handle;
35         }
36
37         ~NfcSecureElement()
38         {
39             Dispose(false);
40         }
41
42         public void Dispose()
43         {
44             Dispose(true);
45             GC.SuppressFinalize(this);
46         }
47
48         private void Dispose(bool disposing)
49         {
50             if (disposed)
51                 return;
52
53             if (disposing)
54             {
55                 // Free managed objects.
56             }
57             //Free unmanaged objects
58             disposed = true;
59         }
60
61         /// <summary>
62         /// Send APDU(Application Protocol Data Unit) response to CLF(Contactless Front-end).
63         /// </summary>
64         /// <since_tizen> 3 </since_tizen>
65         /// <param name="response">The bytes array of response data.</param>
66         /// <param name="responseLength">The size of response bytes array.</param>
67         /// <privilege>http://tizen.org/privilege/nfc.cardemulation</privilege>
68         /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
69         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
70         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
71         public void HceSendApduResponse(byte[] response, uint responseLength)
72         {
73             int ret = Interop.Nfc.CardEmulation.HceSendApduRespondse(_secureElementHandle, response, responseLength);
74             if (ret != (int)NfcError.None)
75             {
76                 Log.Error(Globals.LogTag, "Failed to hcd send apdu response, Error - " + (NfcError)ret);
77                 NfcErrorFactory.ThrowNfcException(ret);
78             }
79         }
80
81         internal IntPtr GetHandle()
82         {
83             return _secureElementHandle;
84         }
85     }
86 }