summaryrefslogtreecommitdiff
path: root/Tizen.Network.Nfc/Tizen.Network.Nfc/NfcManagerImpl.cs
blob: 702f1b2d35509a0967c6177771998bba640c05b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading.Tasks;

namespace Tizen.Network.Nfc
{
    static internal class Globals
    {
        internal const string LogTag = "Tizen.Network.Nfc";
    }

    internal static class NfcConvertUtil
    {
        internal static byte[] IntLengthIntPtrToByteArray(IntPtr nativeValue, int length)
        {
            byte[] value = new byte[length];
            if (nativeValue != IntPtr.Zero)
            {
                Marshal.Copy(nativeValue, value, 0, length);
            }
            return value;
        }

        internal static byte[] UintLengthIntPtrToByteArray(IntPtr nativeValue, uint length)
        {
            byte[] value = new byte[length];

            if (nativeValue != IntPtr.Zero)
            {
                for (int i = 0; i < length; i++)
                {
                    value[i] = Marshal.ReadByte(nativeValue);
                    nativeValue += sizeof(byte);
                }
            }
            return value;
        }
    }

    internal partial class NfcManagerImpl : IDisposable
    {
        private static readonly NfcManagerImpl _instance = new NfcManagerImpl();
        private static readonly NfcTagAdapter _instanceTagAdapter = new NfcTagAdapter();
        private static readonly NfcP2pAdapter _instanceP2pAdapter = new NfcP2pAdapter();
        private static readonly NfcCardEmulationAdapter _instanceCardEmulationAdapter = new NfcCardEmulationAdapter();

        private Dictionary<IntPtr, Interop.Nfc.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.Nfc.VoidCallback>();
        private int _requestId = 0;
        private bool disposed = false;

        internal static NfcManagerImpl Instance
        {
            get
            {
                return _instance;
            }
        }

        internal NfcTagAdapter TagAdapter
        {
            get
            {
                return _instanceTagAdapter;
            }
        }

        internal NfcP2pAdapter P2pAdapter
        {
            get
            {
                return _instanceP2pAdapter;
            }
        }

        internal NfcCardEmulationAdapter CardEmulationAdapter
        {
            get
            {
                return _instanceCardEmulationAdapter;
            }
        }

        internal bool IsSupported
        {
            get
            {
                bool support = Interop.Nfc.IsSupported();

                return support;
            }
        }

        internal bool IsActivated
        {
            get
            {
                bool active = Interop.Nfc.IsActivated();

                return active;
            }
        }

        internal NfcNdefMessage CachedNdefMessage
        {
            get
            {
                IntPtr ndef = IntPtr.Zero; ;
                int ret = Interop.Nfc.GetCachedMessage(out ndef);
                if (ret != (int)NfcError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to get cached ndef message, Error - " + (NfcError)ret);
                }

                NfcNdefMessage ndefMessage = new NfcNdefMessage(ndef);
                return ndefMessage;
            }
        }

        internal NfcTagFilterType TagFilterType
        {
            get
            {
                int type = Interop.Nfc.GetTagFilter();

                return (NfcTagFilterType)type;
            }
            set
            {
                Interop.Nfc.SetTagFilter((int)value);
            }
        }

        internal NfcSecureElementType SecureElementType
        {
            get
            {
                int type;
                int ret = Interop.Nfc.GetSecureElementType(out type);
                if (ret != (int)NfcError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to get secure element type, Error - " + (NfcError)ret);
                }
                return (NfcSecureElementType)type;
            }
            set
            {
                int ret = Interop.Nfc.SetSecureElementType((int)value);
                if (ret != (int)NfcError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to set secure element type, Error - " + (NfcError)ret);
                }
            }
        }

        internal bool SystemHandlerEnabled
        {
            get
            {
                bool systemhandler = Interop.Nfc.IsSystemHandlerEnabled();

                return systemhandler;
            }
            set
            {
                int ret = Interop.Nfc.SetSystemHandlerEnable(value);
                if (ret != (int)NfcError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to enable system handler, Error - " + (NfcError)ret);
                }
            }
        }

        private NfcManagerImpl()
        {
            Initialize();
        }

        ~NfcManagerImpl()
        {
            Dispose(false);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
            {
                // Free managed objects.
            }
            //Free unmanaged objects
            Deinitialize();
            disposed = true;
        }

        private void Initialize()
        {
            int ret = Interop.Nfc.Initialize();
            if (ret != (int)NfcError.None)
            {
                Log.Error(Globals.LogTag, "Failed to Initialize Nfc, Error - " + (NfcError)ret);
                NfcErrorFactory.ThrowNfcException(ret);
            }
        }

        private void Deinitialize()
        {
            int ret = Interop.Nfc.Deinitialize();
            if (ret != (int)NfcError.None)
            {
                Log.Error(Globals.LogTag, "Failed to Deinitialize Nfc, Error - " + (NfcError)ret);
            }
        }

        internal Task SetActivationAsync(bool activation)
        {
            TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
            IntPtr id = IntPtr.Zero;
            lock (_callback_map)
            {
                id = (IntPtr)_requestId++;
                _callback_map[id] = (error, key) =>
                {
                    Log.Debug(Globals.LogTag, "nfc activated");
                    if (error != (int)NfcError.None)
                    {
                        Log.Error(Globals.LogTag, "Error occurs during Nfc activating, " + (NfcError)error);
                        task.SetException(new InvalidOperationException("Error occurs during Nfc activating, " + (NfcError)error));
                    }
                    task.SetResult(true);
                    lock (_callback_map)
                    {
                        _callback_map.Remove(key);
                    }
                };

                int ret = Interop.Nfc.SetActivation(activation, _callback_map[id], id);
                if (ret != (int)NfcError.None)
                {
                    Log.Error(Globals.LogTag, "Failed to activate nfc, Error - " + (NfcError)ret);
                    NfcErrorFactory.ThrowNfcException(ret);
                }
            }
            return task.Task;
        }
    }
}