Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Nfc / Tizen.Network.Nfc / NfcP2p.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 using System.Threading.Tasks;
21
22 namespace Tizen.Network.Nfc
23 {
24     /// <summary>
25     /// A class for managing the p2p target information.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     public class NfcP2p : IDisposable
29     {
30         private IntPtr _p2pTargetHandle = IntPtr.Zero;
31         private bool disposed = false;
32
33         private event EventHandler<P2pDataReceivedEventArgs> _p2pDataReceived;
34
35         private Interop.Nfc.P2pDataReceivedCallback _p2pDataReceivedCallback;
36
37         /// <summary>
38         /// The event for receiving data from NFC peer-to-peer target.
39         /// </summary>
40         /// <since_tizen> 3 </since_tizen>
41         public event EventHandler<P2pDataReceivedEventArgs> P2pDataReceived
42         {
43             add
44             {
45                 _p2pDataReceived += value;
46             }
47             remove
48             {
49                 _p2pDataReceived -= value;
50             }
51         }
52
53         internal NfcP2p(IntPtr handle)
54         {
55             _p2pTargetHandle = handle;
56             RegisterP2pDataReceivedEvent();
57         }
58
59         ~NfcP2p()
60         {
61             Dispose(false);
62         }
63
64         public void Dispose()
65         {
66             Dispose(true);
67             GC.SuppressFinalize(this);
68         }
69
70         private void Dispose(bool disposing)
71         {
72             if (disposed)
73                 return;
74
75             if (disposing)
76             {
77                 // Free managed objects.
78                 UnregisterP2pDataReceivedEvent();
79             }
80             //Free unmanaged objects
81             disposed = true;
82         }
83
84         internal IntPtr GetHandle()
85         {
86             return _p2pTargetHandle;
87         }
88
89         /// <summary>
90         /// Sends data to NFC peer-to-peer target.
91         /// </summary>
92         /// <since_tizen> 3 </since_tizen>
93         /// <param name="ndefMessage">NfcNdefMessage object.</param>
94         /// <privilege>http://tizen.org/privilege/nfc</privilege>
95         /// <exception cref="NotSupportedException">Thrown when Nfc is not supported.</exception>
96         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
97         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
98         public Task<NfcError> SendNdefMessageAsync(NfcNdefMessage ndefMessage)
99         {
100             var task = new TaskCompletionSource<NfcError>();
101
102             Interop.Nfc.VoidCallback callback = (int result, IntPtr userData) =>
103             {
104                 task.SetResult((NfcError)result);
105                 return;
106             };
107
108             int ret = Interop.Nfc.P2p.Send(_p2pTargetHandle, ndefMessage.GetHandle(), callback, IntPtr.Zero);
109             if (ret != (int)NfcError.None)
110             {
111                 Log.Error(Globals.LogTag, "Failed to write ndef message, Error - " + (NfcError)ret);
112                 NfcErrorFactory.ThrowNfcException(ret);
113             }
114
115             return task.Task;
116         }
117
118         private void RegisterP2pDataReceivedEvent()
119         {
120             _p2pDataReceivedCallback = (IntPtr p2pTargetHandle, IntPtr ndefMessageHandle, IntPtr userData) =>
121             {
122                 P2pDataReceivedEventArgs e = new P2pDataReceivedEventArgs(p2pTargetHandle, ndefMessageHandle);
123                 _p2pDataReceived.SafeInvoke(null, e);
124             };
125
126             int ret = Interop.Nfc.P2p.SetDataReceivedCallback(_p2pTargetHandle, _p2pDataReceivedCallback, IntPtr.Zero);
127             if (ret != (int)NfcError.None)
128             {
129                 Log.Error(Globals.LogTag, "Failed to set p2p target discovered callback, Error - " + (NfcError)ret);
130             }
131         }
132
133         private void UnregisterP2pDataReceivedEvent()
134         {
135             Interop.Nfc.P2p.UnsetDataReceivedCallback(_p2pTargetHandle);
136         }
137     }
138
139     /// <summary>
140     /// A class for managing the snep(Simple NDEF Exchange Protocol) information.
141     /// </summary>
142     /// <since_tizen> 3 </since_tizen>
143     public class NfcSnep : IDisposable
144     {
145         private IntPtr _snepHandle = IntPtr.Zero;
146         private bool disposed = false;
147
148         internal NfcSnep(IntPtr handle)
149         {
150             _snepHandle = handle;
151         }
152
153         ~NfcSnep()
154         {
155             Dispose(false);
156         }
157
158         public void Dispose()
159         {
160             Dispose(true);
161             GC.SuppressFinalize(this);
162         }
163
164         private void Dispose(bool disposing)
165         {
166             if (disposed)
167                 return;
168
169             if (disposing)
170             {
171                 // Free managed objects.
172             }
173             //Free unmanaged objects
174             disposed = true;
175         }
176
177         internal IntPtr GetHandle()
178         {
179             return _snepHandle;
180         }
181     }
182 }