Release 4.0.0-preview1-00295
[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     /// The class for the 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 the 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         /// <summary>
59         /// NfcP2pAdapter destructor
60         /// </summary>
61         ~NfcP2pAdapter()
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                 UnregisterP2pTargetDiscoveredEvent();
84             }
85             //Free unmanaged objects
86             disposed = true;
87         }
88
89         /// <summary>
90         /// Gets the current connected P2P target.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <returns>The NfcP2p 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 NfcP2p GetConnectedTarget()
98         {
99             IntPtr targetHandle = IntPtr.Zero;
100             int ret = Interop.Nfc.GetConnectedTarget(out targetHandle);
101
102             if (ret != (int)NfcError.None)
103             {
104                 Log.Error(Globals.LogTag, "Failed to get connected p2p target, Error - " + (NfcError)ret);
105                 NfcErrorFactory.ThrowNfcException(ret);
106             }
107             _p2pTarget = new NfcP2p(targetHandle);
108
109             return _p2pTarget;
110         }
111
112         private void RegisterP2pTargetDiscoveredEvent()
113         {
114             _p2pTargetDiscoveredCallback = (int type, IntPtr p2pTargetHandle, IntPtr userData) =>
115             {
116                 NfcDiscoveredType tagType = (NfcDiscoveredType)type;
117                 P2pTargetDiscoveredEventArgs e = new P2pTargetDiscoveredEventArgs(tagType, p2pTargetHandle);
118                 _p2pTargetDiscovered.SafeInvoke(null, e);
119             };
120
121             int ret = Interop.Nfc.SetP2pTargetDiscoveredCallback(_p2pTargetDiscoveredCallback, IntPtr.Zero);
122             if (ret != (int)NfcError.None)
123             {
124                 Log.Error(Globals.LogTag, "Failed to set p2p target discovered callback, Error - " + (NfcError)ret);
125             }
126         }
127
128         private void UnregisterP2pTargetDiscoveredEvent()
129         {
130             Interop.Nfc.UnsetP2pTargetDiscoveredCallback();
131         }
132     }
133 }