Remove unnecessary files
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcP2pAdapter.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 P2P(Peer to Peer) mode. It allows applications to handle P2P informations.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     /// <privilege>http://tizen.org/privilege/nfc</privilege>
28     public class NfcP2pAdapter : IDisposable
29     {
30         private NfcP2p _p2pTarget;
31         private bool disposed = false;
32
33         private event EventHandler<P2pTargetDiscoveredEventArgs> _p2pTargetDiscovered;
34
35         private Interop.Nfc.P2pTargetDiscoveredCallback _p2pTargetDiscoveredCallback;
36
37         /// <summary>
38         /// The event for  receiving NFC peer-to-peer target discovered notification.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         public event EventHandler<P2pTargetDiscoveredEventArgs> P2pTargetDiscovered
42         {
43             add
44             {
45                 _p2pTargetDiscovered += value;
46             }
47             remove
48             {
49                 _p2pTargetDiscovered -= value;
50             }
51         }
52
53         internal NfcP2pAdapter()
54         {
55             RegisterP2pTargetDiscoveredEvent();
56         }
57
58         ~NfcP2pAdapter()
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                 UnregisterP2pTargetDiscoveredEvent();
78             }
79             //Free unmanaged objects
80             disposed = true;
81         }
82
83         /// <summary>
84         /// Gets current connected p2p target.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         /// <returns>NfcP2p 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 NfcP2p GetConnectedTarget()
92         {
93             IntPtr targetHandle = IntPtr.Zero;
94             int ret = Interop.Nfc.GetConnectedTarget(out targetHandle);
95
96             if (ret != (int)NfcError.None)
97             {
98                 Log.Error(Globals.LogTag, "Failed to get connected p2p target, Error - " + (NfcError)ret);
99                 NfcErrorFactory.ThrowNfcException(ret);
100             }
101             _p2pTarget = new NfcP2p(targetHandle);
102
103             return _p2pTarget;
104         }
105
106         private void RegisterP2pTargetDiscoveredEvent()
107         {
108             _p2pTargetDiscoveredCallback = (int type, IntPtr p2pTargetHandle, IntPtr userData) =>
109             {
110                 NfcDiscoveredType tagType = (NfcDiscoveredType)type;
111                 P2pTargetDiscoveredEventArgs e = new P2pTargetDiscoveredEventArgs(tagType, p2pTargetHandle);
112                 _p2pTargetDiscovered.SafeInvoke(null, e);
113             };
114
115             int ret = Interop.Nfc.SetP2pTargetDiscoveredCallback(_p2pTargetDiscoveredCallback, IntPtr.Zero);
116             if (ret != (int)NfcError.None)
117             {
118                 Log.Error(Globals.LogTag, "Failed to set p2p target discovered callback, Error - " + (NfcError)ret);
119             }
120         }
121
122         private void UnregisterP2pTargetDiscoveredEvent()
123         {
124             Interop.Nfc.UnsetP2pTargetDiscoveredCallback();
125         }
126     }
127 }