Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Smartcard / Tizen.Network.Smartcard / SmartcardReader.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.Smartcard
22 {
23     /// <summary>
24     /// A class for Smartcard reader informations. It allows applications to handle reader informations.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     /// <privilege>http://tizen.org/privilege/secureelement</privilege>
28     public class SmartcardReader : IDisposable
29     {
30         private int _readerHandle = -1;
31         private int _session;
32         private bool disposed = false;
33         private List<SmartcardSession> _sessionList = new List<SmartcardSession>();
34
35         /// <summary>
36         /// The name of reader.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         public string Name
40         {
41             get
42             {
43                 IntPtr strPtr;
44                 int ret = Interop.Smartcard.Reader.ReaderGetName(_readerHandle, out strPtr);
45                 if (ret != (int)SmartcardError.None)
46                 {
47                     Log.Error(Globals.LogTag, "Failed to get reader name, Error - " + (SmartcardError)ret);
48                     return "";
49                 }
50                 return Marshal.PtrToStringAnsi(strPtr);
51             }
52         }
53
54         /// <summary>
55         /// The existence of secure element.
56         /// </summary>
57         /// <since_tizen> 3 </since_tizen>
58         public bool IsSecureElementPresent
59         {
60             get
61             {
62                 bool isPresent;
63                 int ret = Interop.Smartcard.Reader.ReaderIsSecureElementPresent(_readerHandle, out isPresent);
64                 if (ret != (int)SmartcardError.None)
65                 {
66                     Log.Error(Globals.LogTag, "Failed to get present, Error - " + (SmartcardError)ret);
67                 }
68                 return isPresent;
69             }
70         }
71
72         internal SmartcardReader(int handle)
73         {
74             _readerHandle = handle;
75         }
76
77         ~SmartcardReader()
78         {
79             Dispose(false);
80         }
81
82         public void Dispose()
83         {
84             Dispose(true);
85             GC.SuppressFinalize(this);
86         }
87
88         private void Dispose(bool disposing)
89         {
90             if (disposed)
91                 return;
92
93             if (disposing)
94             {
95                 // Free managed objects.
96                 foreach (SmartcardSession session in _sessionList)
97                 {
98                     session.Dispose();
99                     _sessionList.Remove(session);
100                 }
101             }
102             //Free unmanaged objects
103             disposed = true;
104         }
105
106         internal int GetHandle()
107         {
108             return _readerHandle;
109         }
110
111         internal int GetSession()
112         {
113             return _session;
114         }
115
116         /// <summary>
117         /// Connects to a Secure Element in the given reader.
118         /// </summary>
119         /// <since_tizen> 3 </since_tizen>
120         /// <returns>The SmartcardSession object.</returns>
121         public SmartcardSession OpenSession()
122         {
123             int ret = Interop.Smartcard.Reader.ReaderOpenSession(_readerHandle, out _session);
124             if (ret != (int)SmartcardError.None)
125             {
126                 Log.Error(Globals.LogTag, "Failed to get session handle, Error - " + (SmartcardError)ret);
127             }
128
129             SmartcardSession session = new SmartcardSession(this, _session);
130             _sessionList.Add(session);
131             return session;
132         }
133
134         /// <summary>
135         /// Closes all the sessions opened on the given reader.
136         /// </summary>
137         /// <since_tizen> 3 </since_tizen>
138         /// <exception cref="NotSupportedException">Thrown when Smartcard is not supported.</exception>
139         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
140         public void CloseSessions()
141         {
142             int ret = Interop.Smartcard.Reader.ReaderCloseSessions(_readerHandle);
143             if (ret != (int)SmartcardError.None)
144             {
145                 Log.Error(Globals.LogTag, "Failed to close sessions, Error - " + (SmartcardError)ret);
146                 SmartcardErrorFactory.ThrowSmartcardException(ret);
147             }
148
149             foreach (SmartcardSession session in _sessionList)
150             {
151                 session.Close();
152             }
153         }
154     }
155 }