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