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 /// The class for Smartcard reader information. It allows applications to handle the reader information.
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 the 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 a 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;
78 /// SmartcardReader destructor.
91 GC.SuppressFinalize(this);
94 private void Dispose(bool disposing)
101 // Free managed objects.
102 foreach (SmartcardSession session in _sessionList)
105 _sessionList.Remove(session);
108 //Free unmanaged objects
112 internal int GetHandle()
114 return _readerHandle;
117 internal int GetSession()
123 /// Connects to a secure element in the given reader.
125 /// <since_tizen> 3 </since_tizen>
126 /// <returns>The SmartcardSession object.</returns>
127 public SmartcardSession OpenSession()
129 int ret = Interop.Smartcard.Reader.ReaderOpenSession(_readerHandle, out _session);
130 if (ret != (int)SmartcardError.None)
132 Log.Error(Globals.LogTag, "Failed to get session handle, Error - " + (SmartcardError)ret);
135 SmartcardSession session = new SmartcardSession(this, _session);
136 _sessionList.Add(session);
141 /// Closes all the sessions opened on the given reader.
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()
148 int ret = Interop.Smartcard.Reader.ReaderCloseSessions(_readerHandle);
149 if (ret != (int)SmartcardError.None)
151 Log.Error(Globals.LogTag, "Failed to close sessions, Error - " + (SmartcardError)ret);
152 SmartcardErrorFactory.ThrowSmartcardException(ret);
155 foreach (SmartcardSession session in _sessionList)