Release 4.0.0-preview1-00051
[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     /// A class for NFC Tag mode. It allows applications to handle 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 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         ~NfcTagAdapter()
59         {
60             Dispose(false);
61         }
62
63         public void Dispose()
64         {
65             Dispose(true);
66             GC.SuppressFinalize(this);
67         }
68
69         private void Dispose(bool disposing)
70         {
71             if (disposed)
72                 return;
73
74             if (disposing)
75             {
76                 // Free managed objects.
77                 UnregisterTagDiscoveredEvent();
78             }
79             //Free unmanaged objects
80             disposed = true;
81         }
82
83         /// <summary>
84         /// Gets current connected tag.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         /// <returns>NfcTag object.</returns>
88         /// <privilege>http://tizen.org/privilege/nfc</privilege>
89         /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
90         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
91         public NfcTag GetConnectedTag()
92         {
93             IntPtr tagHandle = IntPtr.Zero;
94             int ret = Interop.Nfc.GetConnectedTag(out tagHandle);
95
96             if (ret != (int)NfcError.None)
97             {
98                 Log.Error(Globals.LogTag, "Failed to get connected tag, Error - " + (NfcError)ret);
99                 NfcErrorFactory.ThrowNfcException(ret);
100             }
101             _tag = new NfcTag(tagHandle);
102
103             return _tag;
104         }
105
106         private void RegisterTagDiscoveredEvent()
107         {
108             _tagDiscoveredCallback = (int type, IntPtr tagHandle, IntPtr userData) =>
109             {
110                 NfcDiscoveredType tagType = (NfcDiscoveredType)type;
111                 TagDiscoveredEventArgs e = new TagDiscoveredEventArgs(tagType, tagHandle);
112                 _tagDiscovered.SafeInvoke(null, e);
113             };
114
115             int ret = Interop.Nfc.SetTagDiscoveredCallback(_tagDiscoveredCallback, IntPtr.Zero);
116             if (ret != (int)NfcError.None)
117             {
118                 Log.Error(Globals.LogTag, "Failed to set tag discovered callback, Error - " + (NfcError)ret);
119             }
120         }
121
122         private void UnregisterTagDiscoveredEvent()
123         {
124             Interop.Nfc.UnsetTagDiscoveredCallback();
125         }
126     }
127 }