2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 namespace Tizen.Network.Smartcard
24 /// A class for Smartcard reader informations. It allows applications to handle reader informations.
26 /// <since_tizen> 3 </since_tizen>
27 /// <privilege>http://tizen.org/privilege/secureelement</privilege>
28 public class SmartcardReader : IDisposable
30 private int _readerHandle = -1;
32 private bool disposed = false;
33 private List<SmartcardSession> _sessionList = new List<SmartcardSession>();
36 /// The name of reader.
38 /// <since_tizen> 3 </since_tizen>
44 int ret = Interop.Smartcard.Reader.ReaderGetName(_readerHandle, out strPtr);
45 if (ret != (int)SmartcardError.None)
47 Log.Error(Globals.LogTag, "Failed to get reader name, Error - " + (SmartcardError)ret);
50 return Marshal.PtrToStringAnsi(strPtr);
55 /// The existence of secure element.
57 /// <since_tizen> 3 </since_tizen>
58 public bool IsSecureElementPresent
63 int ret = Interop.Smartcard.Reader.ReaderIsSecureElementPresent(_readerHandle, out isPresent);
64 if (ret != (int)SmartcardError.None)
66 Log.Error(Globals.LogTag, "Failed to get present, Error - " + (SmartcardError)ret);
72 internal SmartcardReader(int handle)
74 _readerHandle = handle;
85 GC.SuppressFinalize(this);
88 private void Dispose(bool disposing)
95 // Free managed objects.
96 foreach (SmartcardSession session in _sessionList)
99 _sessionList.Remove(session);
102 //Free unmanaged objects
106 internal int GetHandle()
108 return _readerHandle;
111 internal int GetSession()
117 /// Connects to a Secure Element in the given reader.
119 /// <since_tizen> 3 </since_tizen>
120 /// <returns>The SmartcardSession object.</returns>
121 public SmartcardSession OpenSession()
123 int ret = Interop.Smartcard.Reader.ReaderOpenSession(_readerHandle, out _session);
124 if (ret != (int)SmartcardError.None)
126 Log.Error(Globals.LogTag, "Failed to get session handle, Error - " + (SmartcardError)ret);
129 SmartcardSession session = new SmartcardSession(this, _session);
130 _sessionList.Add(session);
135 /// Closes all the sessions opened on the given reader.
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()
142 int ret = Interop.Smartcard.Reader.ReaderCloseSessions(_readerHandle);
143 if (ret != (int)SmartcardError.None)
145 Log.Error(Globals.LogTag, "Failed to close sessions, Error - " + (SmartcardError)ret);
146 SmartcardErrorFactory.ThrowSmartcardException(ret);
149 foreach (SmartcardSession session in _sessionList)