Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Smartcard / Tizen.Network.Smartcard / SmartcardManagerImpl.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.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.Threading.Tasks;
21
22 namespace Tizen.Network.Smartcard
23 {
24     static internal class Globals
25     {
26         internal const string LogTag = "Tizen.Network.Smartcard";
27     }
28
29     internal class SmartcardManagerImpl : IDisposable
30     {
31         private static readonly SmartcardManagerImpl _instance = new SmartcardManagerImpl();
32         private List<SmartcardReader> _readerList = new List<SmartcardReader>();
33         private bool disposed = false;
34
35         internal static SmartcardManagerImpl Instance
36         {
37             get
38             {
39                 return _instance;
40             }
41         }
42
43         private SmartcardManagerImpl()
44         {
45             initialize();
46         }
47
48         ~SmartcardManagerImpl()
49         {
50             Dispose(false);
51         }
52
53         public void Dispose()
54         {
55             Dispose(true);
56             GC.SuppressFinalize(this);
57         }
58
59         private void Dispose(bool disposing)
60         {
61             if (disposed)
62                 return;
63
64             if (disposing)
65             {
66                 // Free managed objects.
67                 foreach (SmartcardReader reader in _readerList)
68                 {
69                     reader.Dispose();
70                     _readerList.Remove(reader);
71                 }
72             }
73             //Free unmanaged objects
74             deinitialize();
75             disposed = true;
76         }
77
78         private void initialize()
79         {
80             int ret = Interop.Smartcard.Initialize();
81             if (ret != (int)SmartcardError.None)
82             {
83                 Log.Error(Globals.LogTag, "Failed to initialize smartcard, Error - " + (SmartcardError)ret);
84                 SmartcardErrorFactory.ThrowSmartcardException(ret);
85             }
86         }
87
88         private void deinitialize()
89         {
90             int ret = Interop.Smartcard.Deinitialize();
91             if (ret != (int)SmartcardError.None)
92             {
93                 Log.Error(Globals.LogTag, "Failed to deinitialize smartcard, Error - " + (SmartcardError)ret);
94             }
95         }
96
97         internal IEnumerable<SmartcardReader> GetReaders()
98         {
99             IntPtr readerPtr;
100             int len = 0;
101
102             int ret = Interop.Smartcard.GetReaders(out readerPtr, out len);
103             if (ret != (int)SmartcardError.None)
104             {
105                 Log.Error(Globals.LogTag, "Failed to remove with AP, Error - " + (SmartcardError)ret);
106                 SmartcardErrorFactory.ThrowSmartcardException(ret);
107             }
108
109             for (int i = 0; i < len; i++)
110             {
111                 int readerID = Marshal.ReadInt32(readerPtr);
112
113                 SmartcardReader readerItem = new SmartcardReader(readerID);
114                 _readerList.Add(readerItem);
115                 readerPtr += sizeof(int);
116             }
117
118             return _readerList;
119         }
120     }
121 }