f310bd7cd8eb4173ccce468d5fbc62147a866898
[platform/core/csapi/tizenfx.git] / src / Tizen.Location.Geofence / Tizen.Location.Geofence / Fence.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
19 namespace Tizen.Location.Geofence
20 {
21     /// <summary>
22     /// Geofence defines a virtual perimeter for a real-world geographic area.
23     /// If you create a geofence, you can trigger some activities when a device enters (or exits) the geofences defined by you.
24     /// You can create a geofence with the information of the Geopoint, Wi-Fi, or BT.
25     /// <list>
26     /// <item>Geopoint: Geofence is specified by the coordinates (Latitude and Longitude) and radius.</item>
27     /// <item>WIFI: Geofence is specified by the BSSID of the Wi-Fi access point.</item>
28     /// <item>BT: Geofence is specified by the Bluetooth address.</item>
29     /// </list>
30     /// The Basic service set identifier (BSSID) is the MAC address of the wireless access point (WAP) generated by combining the 24-bit Organization Unique Identifier (the manufacturer's identity)
31     /// and the manufacturer's assigned 24-bit identifier for the radio chipset in the WAP.
32     /// </summary>
33     /// <since_tizen> 3 </since_tizen>
34     public class Fence : IDisposable
35     {
36         private bool _disposed = false;
37
38         internal IntPtr Handle
39         {
40             get;
41             set;
42         }
43
44         internal Fence(IntPtr handle)
45         {
46             Handle = handle;
47         }
48
49         /// <summary>
50         /// The destructor of the Fence class.
51         /// </summary>
52         /// <since_tizen> 3 </since_tizen>
53         ~Fence()
54         {
55             Dispose(false);
56         }
57
58         /// <summary>
59         /// Gets the type of geofence.
60         /// </summary>
61         /// <since_tizen> 3 </since_tizen>
62         public FenceType Type
63         {
64             get
65             {
66                 FenceType val;
67                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceType(Handle, out val);
68                 if (ret != GeofenceError.None)
69                 {
70                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get GeofenceType");
71                 }
72
73                 return val;
74             }
75         }
76
77         /// <summary>
78         /// Gets the ID of the place.
79         /// </summary>
80         /// <since_tizen> 3 </since_tizen>
81         public int PlaceId
82         {
83             get
84             {
85                 int result = -1;
86                 GeofenceError ret = (GeofenceError)Interop.Geofence.FencePlaceID(Handle, out result);
87                 if (ret != GeofenceError.None)
88                 {
89                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get PlaceId");
90                 }
91
92                 return result;
93             }
94         }
95
96         /// <summary>
97         /// Gets the longitude of geofence.
98         /// </summary>
99         /// <since_tizen> 3 </since_tizen>
100         public double Longitude
101         {
102             get
103             {
104                 double result = -1;
105                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLongitude(Handle, out result);
106                 if (ret != GeofenceError.None)
107                 {
108                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Longitude");
109                 }
110
111                 return result;
112
113             }
114         }
115
116         /// <summary>
117         /// Gets the latitude of geofence.
118         /// </summary>
119         /// <since_tizen> 3 </since_tizen>
120         public double Latitude
121         {
122             get
123             {
124                 double result = -1;
125                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceLatitude(Handle, out result);
126                 if (ret != GeofenceError.None)
127                 {
128                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Latitude");
129                 }
130
131                 return result;
132             }
133         }
134
135         /// <summary>
136         /// Gets the radius of geofence.
137         /// </summary>
138         /// <since_tizen> 3 </since_tizen>
139         public int Radius
140         {
141             get
142             {
143                 int result = -1;
144                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceRadius(Handle, out result);
145                 if (ret != GeofenceError.None)
146                 {
147                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Radius");
148                 }
149
150                 return result;
151             }
152         }
153
154         /// <summary>
155         /// Gets the address of geofence.
156         /// </summary>
157         /// <since_tizen> 3 </since_tizen>
158         public string Address
159         {
160             get
161             {
162                 string result = "";
163                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceAddress(Handle, out result);
164                 if (ret != GeofenceError.None)
165                 {
166                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Adress");
167                 }
168
169                 return result;
170             }
171         }
172
173         /// <summary>
174         /// Gets the BSSID of geofence.
175         /// </summary>
176         /// <since_tizen> 3 </since_tizen>
177         public string Bssid
178         {
179             get
180             {
181                 string result = "";
182                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceBSSID(Handle, out result);
183                 if (ret != GeofenceError.None)
184                 {
185                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Bssid");
186                 }
187
188                 return result;
189             }
190         }
191
192         /// <summary>
193         /// Gets the SSID of geofence.
194         /// </summary>
195         /// <since_tizen> 3 </since_tizen>
196         public string Ssid
197         {
198             get
199             {
200                 string result = "";
201                 GeofenceError ret = (GeofenceError)Interop.Geofence.FenceSSID(Handle, out result);
202                 if (ret != GeofenceError.None)
203                 {
204                     Tizen.Log.Error(GeofenceErrorFactory.LogTag, "Failed to get Ssid");
205                 }
206
207                 return result;
208             }
209         }
210
211         /// <summary>
212         /// Creates a geopoint type of the new geofence.
213         /// </summary>
214         /// <since_tizen> 4 </since_tizen>
215         /// <param name="placeId">The current place ID.</param>
216         /// <param name="latitude">Specifies the value of latitude of the geofence [-90.0 ~ 90.0] \(degrees).</param>
217         /// <param name="longitude">Specifies the value of longitude of the geofence [-180.0 ~ 180.0] \(degrees).</param>
218         /// <param name="radius">Specifies the value of radius of the geofence [100 ~ 500](meter).</param>
219         /// <param name="address">Specifies the value of the address.</param>
220         /// <returns>The newly created geofence instance.</returns>
221         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
222         /// <exception cref="InvalidOperationException">In case of any system error.</exception>
223         /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
224         public static Fence CreateGPSFence(int placeId, double latitude, double longitude, int radius, string address)
225         {
226             IntPtr handle = IntPtr.Zero;
227             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateGPSFence(placeId, latitude, longitude, radius,address, out handle);
228             if (ret != GeofenceError.None)
229             {
230                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from GPS Data for " + placeId);
231             }
232
233             return new Fence(handle);
234         }
235
236         /// <summary>
237         /// Creates a Wi-Fi type of the new geofence.
238         /// </summary>
239         /// <since_tizen> 3 </since_tizen>
240         /// <param name="placeId">The current place ID.</param>
241         /// <param name="bssid">Specifies the value of BSSID of the Wi-Fi MAC address.</param>
242         /// <param name="ssid"> Specifies the value of SSID of the Wi-Fi device.</param>
243         /// <returns>The newly created geofence instance.</returns>
244         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
245         /// <exception cref="InvalidOperationException">In case of any system error.</exception>
246         /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
247         public static Fence CreateWifiFence(int placeId, string bssid, string ssid)
248         {
249             IntPtr handle = IntPtr.Zero;
250             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateWiFiFence(placeId, bssid, ssid, out handle);
251             if (ret != GeofenceError.None)
252             {
253                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Wifi Data for " + placeId);
254             }
255
256             return new Fence(handle);
257         }
258
259         /// <summary>
260         /// Creates a Bluetooth type of the new geofence.
261         /// </summary>
262         /// <since_tizen> 3 </since_tizen>
263         /// <param name="placeId">The current place ID.</param>
264         /// <param name="bssid">Specifies the value of BSSID of BT MAC address.</param>
265         /// <param name="ssid"> Specifies the value of SSID of BT Device.</param>
266         /// <returns>The newly created geofence instance.</returns>
267         /// <exception cref="ArgumentException">In case of an invalid parameter.</exception>
268         /// <exception cref="InvalidOperationException">In case of any system error.</exception>
269         /// <exception cref="NotSupportedException">In case the geofence is not supported.</exception>
270         public static Fence CreateBTFence(int placeId, string bssid, string ssid)
271         {
272             IntPtr handle = IntPtr.Zero;
273             GeofenceError ret = (GeofenceError)Interop.Geofence.CreateBTFence(placeId, bssid, ssid, out handle);
274             if (ret != GeofenceError.None)
275             {
276                 throw GeofenceErrorFactory.CreateException(ret, "Failed to create Geofence from Bluetooth Data for " + placeId);
277             }
278
279             return new Fence(handle);
280         }
281
282         /// <summary>
283         /// The overloaded Dispose API for destroying the fence handle.
284         /// </summary>
285         /// <since_tizen> 3 </since_tizen>
286         public void Dispose()
287         {
288             Dispose(true);
289             GC.SuppressFinalize(this);
290         }
291
292         /// <summary>
293         /// Dispose.
294         /// </summary>
295         /// <since_tizen> 3 </since_tizen>
296         protected virtual void Dispose(bool disposing)
297         {
298             if (_disposed)
299                 return;
300
301             if (Handle != IntPtr.Zero)
302             {
303                 Interop.Geofence.Destroy(Handle);
304                 Handle = IntPtr.Zero;
305             }
306
307             _disposed = true;
308         }
309     }
310 }