Fix XML Doc Build Warning for Smartcard
[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     /// The class for Smartcard reader information. It allows applications to handle the reader information.
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 the 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 a 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         /// <summary>
78         /// SmartcardReader destructor.
79         /// </summary>
80         ~SmartcardReader()
81         {
82             Dispose(false);
83         }
84
85         /// <summary>
86         /// Dispose
87         /// </summary>
88         public void Dispose()
89         {
90             Dispose(true);
91             GC.SuppressFinalize(this);
92         }
93
94         private void Dispose(bool disposing)
95         {
96             if (disposed)
97                 return;
98
99             if (disposing)
100             {
101                 // Free managed objects.
102                 foreach (SmartcardSession session in _sessionList)
103                 {
104                     session.Dispose();
105                     _sessionList.Remove(session);
106                 }
107             }
108             //Free unmanaged objects
109             disposed = true;
110         }
111
112         internal int GetHandle()
113         {
114             return _readerHandle;
115         }
116
117         internal int GetSession()
118         {
119             return _session;
120         }
121
122         /// <summary>
123         /// Connects to a secure element in the given reader.
124         /// </summary>
125         /// <since_tizen> 3 </since_tizen>
126         /// <returns>The SmartcardSession object.</returns>
127         public SmartcardSession OpenSession()
128         {
129             int ret = Interop.Smartcard.Reader.ReaderOpenSession(_readerHandle, out _session);
130             if (ret != (int)SmartcardError.None)
131             {
132                 Log.Error(Globals.LogTag, "Failed to get session handle, Error - " + (SmartcardError)ret);
133             }
134
135             SmartcardSession session = new SmartcardSession(this, _session);
136             _sessionList.Add(session);
137             return session;
138         }
139
140         /// <summary>
141         /// Closes all the sessions opened on the given reader.
142         /// </summary>
143         /// <since_tizen> 3 </since_tizen>
144         /// <exception cref="NotSupportedException">Thrown when the Smartcard is not supported.</exception>
145         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
146         public void CloseSessions()
147         {
148             int ret = Interop.Smartcard.Reader.ReaderCloseSessions(_readerHandle);
149             if (ret != (int)SmartcardError.None)
150             {
151                 Log.Error(Globals.LogTag, "Failed to close sessions, Error - " + (SmartcardError)ret);
152                 SmartcardErrorFactory.ThrowSmartcardException(ret);
153             }
154
155             foreach (SmartcardSession session in _sessionList)
156             {
157                 session.Close();
158             }
159         }
160     }
161 }