Modify api documentation
[platform/core/csapi/tizenfx.git] / src / Tizen.Location / Tizen.Location / GpsSatellite.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.Collections.Generic;
19 using System.Runtime.InteropServices;
20
21 namespace Tizen.Location
22 {
23     /// <summary>
24     /// A class which contains the functionality for obtaining information about Gps satellites in range and in use.
25     /// </summary>
26     public class GpsSatellite
27     {
28         private int _interval = 1;
29         private Locator _locator;
30         private EventHandler<SatelliteStatusChangedEventArgs> _satelliteStatusChanged;
31         private IntPtr _handle = IntPtr.Zero;
32
33         private Interop.GpsSatellite.SatelliteStatuschangedCallback _satelliteStatuschangedCallback;
34         private Interop.GpsSatellite.SatelliteStatusinfomationCallback _satelliteStatusinfomationCallback;
35
36         /// <summary>
37         /// The time interval between callback updates.
38         /// Should be in the range [1~120] seconds.
39         /// </summary>
40         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
41         /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
42         public int Interval
43         {
44             get
45             {
46                 Log.Info(Globals.LogTag, "Getting the Callback Interval");
47                 return _interval;
48             }
49             set
50             {
51                 Log.Info(Globals.LogTag, "Setting the Callback Interval");
52                 if (value > 0 && value <= 120)
53                 {
54                     _interval = value;
55                 }
56                 else
57                 {
58                     Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
59                     throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
60                 }
61             }
62         }
63
64         /// <summary>
65         /// The NMEAData from the Satellite.
66         /// </summary>
67         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
68         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
69         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
70         /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
71         public string Nmea
72         {
73             get
74             {
75                 Log.Info(Globals.LogTag, "Getting NMEAData");
76                 return GetNmea();
77             }
78         }
79
80         private string GetNmea()
81         {
82             string value = null;
83             int ret = Interop.GpsSatellite.GetNMEAData(_handle, out value);
84             if (((LocationError)ret != LocationError.None))
85             {
86                 Log.Error(Globals.LogTag, "Error getting the NMEAData," + (LocationError)ret);
87                 throw LocationErrorFactory.ThrowLocationException(ret);
88             }
89
90             return value;
91         }
92
93
94         /// <summary>
95         /// The Count of Active satellites.
96         /// </summary>
97         /// <privilege>http://tizen.org/privilege/location</privilege>
98         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
99         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
100         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
101         /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
102         public int ActiveCount
103         {
104             get
105             {
106                 return (int)GetActiveCount();
107             }
108         }
109
110         private uint GetActiveCount()
111         {
112             Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
113             uint numActive = 0;
114             uint numInView;
115             int timestamp;
116             int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
117             if (((LocationError)ret != LocationError.None))
118             {
119                 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
120                 throw LocationErrorFactory.ThrowLocationException(ret);
121             }
122             return numActive;
123         }
124
125         /// <summary>
126         /// The Count of satellites in view.
127         /// </summary>
128         /// <privilege>http://tizen.org/privilege/location</privilege>
129         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
130         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
131         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
132         /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
133         public int InViewCount
134         {
135             get
136             {
137                 return (int)GetInViewCount();
138             }
139         }
140
141         private uint GetInViewCount()
142         {
143             Log.Info(Globals.LogTag, "Getting the In view count of satellites");
144             uint numActive;
145             uint numInView = 0;
146             int timestamp;
147             int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
148             if (((LocationError)ret != LocationError.None))
149             {
150                 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
151                 throw LocationErrorFactory.ThrowLocationException(ret);
152             }
153             return numInView;
154         }
155
156         /// <summary>
157         /// The list of satellites/last recorded satellites in view.
158         /// </summary>
159         /// <privilege>http://tizen.org/privilege/location</privilege>
160         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
161         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
162         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
163         /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
164         public IList<SatelliteInformation> Satellites
165         {
166             get
167             {
168                 return GetSatellites();
169             }
170         }
171
172         private IList<SatelliteInformation> GetSatellites()
173         {
174             List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
175             Log.Info(Globals.LogTag, "Getting the list of satellites");
176
177             if (_satelliteStatusinfomationCallback == null)
178             {
179                 _satelliteStatusinfomationCallback = (azimuth, elevation, prn, snr, active, userData) =>
180                 {
181                     SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, active);
182                     satelliteList.Add(satellite);
183                     return true;
184                 };
185             }
186
187             int ret = Interop.GpsSatellite.GetForEachSatelliteInView(_handle, _satelliteStatusinfomationCallback, IntPtr.Zero);
188             if (((LocationError)ret != LocationError.None))
189             {
190                 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
191                 throw LocationErrorFactory.ThrowLocationException(ret);
192             }
193             return satelliteList;
194         }
195
196         /// <summary>
197         /// The constructor of GpsSatellite class.
198         /// </summary>
199         /// <param name="locator"> Locator object initilized using Gps.</param>
200         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
201         public GpsSatellite(Locator locator)
202         {
203             Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
204             if (locator == null)
205             {
206                 Log.Error(Globals.LogTag, "locator is null");
207                 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
208             }
209
210             LocationType method = locator.LocationType;
211             if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
212             {
213                 _locator = locator;
214                 _handle = _locator.GetHandle();
215             }
216             else
217             {
218                 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
219                 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
220             }
221         }
222
223         /// <summary>
224         /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
225         /// The callback will be invoked periodically (every Interval seconds).
226         /// </summary>
227         /// <privilege>http://tizen.org/privilege/location</privilege>
228         /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
229         /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
230         /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
231         public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
232         {
233             add
234             {
235                 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
236                 if (_satelliteStatusChanged == null)
237                 {
238                     Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
239                     SetSatelliteStatusChangeCallback();
240                 }
241                 _satelliteStatusChanged += value;
242             }
243             remove
244             {
245                 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Remove called");
246                 _satelliteStatusChanged -= value;
247                 if (_satelliteStatusChanged == null)
248                 {
249                     Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
250                     UnSetSatelliteStatusChangeCallback();
251                 }
252             }
253         }
254
255         private void SetSatelliteStatusChangeCallback()
256         {
257             Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
258             if (_satelliteStatuschangedCallback == null)
259             {
260                 _satelliteStatuschangedCallback = (numActive, numInView, timestamp, userData) =>
261                 {
262                     Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
263                     DateTime timeStamp = DateTime.Now;
264
265                     if (timestamp != 0)
266                     {
267                         DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
268                         timeStamp = start.ToLocalTime();
269                     }
270                     _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
271                 };
272             }
273
274             GCHandle handle = GCHandle.Alloc(this);
275             int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, _satelliteStatuschangedCallback, _interval, GCHandle.ToIntPtr(handle));
276             if (((LocationError)ret != LocationError.None))
277             {
278                 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
279                 throw LocationErrorFactory.ThrowLocationException(ret);
280             }
281         }
282
283         private void UnSetSatelliteStatusChangeCallback()
284         {
285             Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
286             int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
287             if (((LocationError)ret != LocationError.None))
288             {
289                 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
290                 throw LocationErrorFactory.ThrowLocationException(ret);
291             }
292         }
293     }
294
295     /// <summary>
296     /// A class which contains the information of the Satellite under consideration.
297     /// </summary>
298     public class SatelliteInformation
299     {
300         /// <summary>
301         /// Class Constructor for SatelliteInformation class.
302         /// </summary>
303         /// <param name="azimuth"> The azimuth value of the satellite in degrees.</param>
304         /// <param name="elevation"> The elevation of the satellite in meters.</param>
305         /// <param name="prn"> The Prn value of the satellite.</param>
306         /// <param name="snr"> The SNR value of the satellite in dB.</param>
307         /// <param name="active"> The flag signaling if satellite is in use.</param>
308         public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool active)
309         {
310             Azimuth = azimuth;
311             Elevation = elevation;
312             Prn = prn;
313             Snr = snr;
314             Active = active;
315         }
316
317         /// <summary>
318         /// The Azimuth information of the Satellite.
319         /// </summary>
320         public uint Azimuth { get; private set; }
321
322         /// <summary>
323         /// The Elevation information of the Satellite.
324         /// </summary>
325         public uint Elevation { get; private set; }
326
327         /// <summary>
328         /// The PRN of the Satellite.
329         /// </summary>
330         public uint Prn { get; private set; }
331
332         /// <summary>
333         /// The SNR of the Satellite.
334         /// </summary>
335         public uint Snr { get; private set; }
336
337         /// <summary>
338         /// The operational status of the Satellite.
339         /// </summary>
340         public bool Active { get; private set; }
341     }
342 }