Release 4.0.0-preview1-00295
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcTagAdapter.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.Nfc
22 {
23     /// <summary>
24     /// The class for the NFC tag mode. It allows applications to handle the Tag informations.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     /// <privilege>http://tizen.org/privilege/nfc</privilege>
28     public class NfcTagAdapter : IDisposable
29     {
30         private NfcTag _tag;
31         private bool disposed = false;
32
33         private event EventHandler<TagDiscoveredEventArgs> _tagDiscovered;
34
35         private Interop.Nfc.TagDiscoveredCallback _tagDiscoveredCallback;
36
37         /// <summary>
38         /// The event for receiving the tag discovered notification.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         public event EventHandler<TagDiscoveredEventArgs> TagDiscovered
42         {
43             add
44             {
45                 _tagDiscovered += value;
46             }
47             remove
48             {
49                 _tagDiscovered -= value;
50             }
51         }
52
53         internal NfcTagAdapter()
54         {
55             RegisterTagDiscoveredEvent();
56         }
57
58         /// <summary>
59         /// NfcTagAdapter destructor.
60         /// </summary>
61         ~NfcTagAdapter()
62         {
63             Dispose(false);
64         }
65
66         /// <summary>
67         /// Dispose
68         /// </summary>
69         public void Dispose()
70         {
71             Dispose(true);
72             GC.SuppressFinalize(this);
73         }
74
75         private void Dispose(bool disposing)
76         {
77             if (disposed)
78                 return;
79
80             if (disposing)
81             {
82                 // Free managed objects.
83                 UnregisterTagDiscoveredEvent();
84             }
85             //Free unmanaged objects
86             disposed = true;
87         }
88
89         /// <summary>
90         /// Gets the current connected tag.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <returns>The NfcTag object.</returns>
94         /// <privilege>http://tizen.org/privilege/nfc</privilege>
95         /// <exception cref="NotSupportedException">Thrown when the NFC is not supported.</exception>
96         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
97         public NfcTag GetConnectedTag()
98         {
99             IntPtr tagHandle = IntPtr.Zero;
100             int ret = Interop.Nfc.GetConnectedTag(out tagHandle);
101
102             if (ret != (int)NfcError.None)
103             {
104                 Log.Error(Globals.LogTag, "Failed to get connected tag, Error - " + (NfcError)ret);
105                 NfcErrorFactory.ThrowNfcException(ret);
106             }
107             _tag = new NfcTag(tagHandle);
108
109             return _tag;
110         }
111
112         private void RegisterTagDiscoveredEvent()
113         {
114             _tagDiscoveredCallback = (int type, IntPtr tagHandle, IntPtr userData) =>
115             {
116                 NfcDiscoveredType tagType = (NfcDiscoveredType)type;
117                 TagDiscoveredEventArgs e = new TagDiscoveredEventArgs(tagType, tagHandle);
118                 _tagDiscovered.SafeInvoke(null, e);
119             };
120
121             int ret = Interop.Nfc.SetTagDiscoveredCallback(_tagDiscoveredCallback, IntPtr.Zero);
122             if (ret != (int)NfcError.None)
123             {
124                 Log.Error(Globals.LogTag, "Failed to set tag discovered callback, Error - " + (NfcError)ret);
125             }
126         }
127
128         private void UnregisterTagDiscoveredEvent()
129         {
130             Interop.Nfc.UnsetTagDiscoveredCallback();
131         }
132     }
133 }