2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Location
24 /// This class contains the functionality for obtaining information about GPS satellites in the range and in use.
26 /// <since_tizen> 3 </since_tizen>
27 public class GpsSatellite
29 private int _interval = 1;
30 private Locator _locator;
31 private EventHandler<SatelliteStatusChangedEventArgs> _satelliteStatusChanged;
32 private IntPtr _handle = IntPtr.Zero;
34 private Interop.GpsSatellite.SatelliteStatuschangedCallback _satelliteStatuschangedCallback;
35 private Interop.GpsSatellite.SatelliteStatusinfomationCallback _satelliteStatusinfomationCallback;
38 /// The time interval between callback updates.
39 /// Should be in the range of 1~120 seconds.
41 /// <since_tizen> 3 </since_tizen>
42 /// <exception cref="ArgumentException">Thrown when an an invalid argument is used.</exception>
47 Log.Info(Globals.LogTag, "Getting the Callback Interval");
52 Log.Info(Globals.LogTag, "Setting the Callback Interval");
53 if (value > 0 && value <= 120)
59 Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
60 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
66 /// The NMEA data from the satellite.
68 /// <since_tizen> 3 </since_tizen>
69 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
70 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
71 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the location.</exception>
72 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
77 Log.Info(Globals.LogTag, "Getting NMEAData");
82 private string GetNmea()
85 int ret = Interop.GpsSatellite.GetNMEAData(_handle, out value);
86 if (((LocationError)ret != LocationError.None))
88 Log.Error(Globals.LogTag, "Error getting the NMEAData," + (LocationError)ret);
89 throw LocationErrorFactory.ThrowLocationException(ret);
97 /// The count of active satellites.
99 /// <since_tizen> 3 </since_tizen>
100 /// <privilege>http://tizen.org/privilege/location</privilege>
101 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
102 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
103 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the location.</exception>
104 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
105 public int ActiveCount
109 return (int)GetActiveCount();
113 private uint GetActiveCount()
115 Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
119 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
120 if (((LocationError)ret != LocationError.None))
122 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
123 throw LocationErrorFactory.ThrowLocationException(ret);
129 /// The count of satellites in view.
131 /// <since_tizen> 3 </since_tizen>
132 /// <privilege>http://tizen.org/privilege/location</privilege>
133 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
134 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
135 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the location.</exception>
136 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
137 public int InViewCount
141 return (int)GetInViewCount();
145 private uint GetInViewCount()
147 Log.Info(Globals.LogTag, "Getting the In view count of satellites");
151 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
152 if (((LocationError)ret != LocationError.None))
154 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
155 throw LocationErrorFactory.ThrowLocationException(ret);
161 /// The list of satellites or last recorded satellites in view.
163 /// <since_tizen> 3 </since_tizen>
164 /// <privilege>http://tizen.org/privilege/location</privilege>
165 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
166 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
167 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the location.</exception>
168 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
169 public IList<SatelliteInformation> Satellites
173 return GetSatellites();
177 private IList<SatelliteInformation> GetSatellites()
179 List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
180 Log.Info(Globals.LogTag, "Getting the list of satellites");
182 _satelliteStatusinfomationCallback = (azimuth, elevation, prn, snr, active, userData) =>
184 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, active);
185 satelliteList.Add(satellite);
189 int ret = Interop.GpsSatellite.GetForEachSatelliteInView(_handle, _satelliteStatusinfomationCallback, IntPtr.Zero);
190 if (((LocationError)ret != LocationError.None))
192 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
193 throw LocationErrorFactory.ThrowLocationException(ret);
195 return satelliteList;
199 /// The constructor of the GpsSatellite class.
201 /// <since_tizen> 3 </since_tizen>
202 /// <param name="locator">The locator object initilized using GPS.</param>
203 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
204 public GpsSatellite(Locator locator)
206 Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
209 Log.Error(Globals.LogTag, "locator is null");
210 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
213 LocationType method = locator.LocationType;
214 if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
217 _handle = _locator.GetHandle();
221 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
222 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
227 /// The SatelliteStatusUpdated event is raised whenever the satellite information is updated.
228 /// The callback will be invoked periodically (every Interval seconds).
230 /// <since_tizen> 3 </since_tizen>
231 /// <privilege>http://tizen.org/privilege/location</privilege>
232 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
233 /// <exception cref="UnauthorizedAccessException">Thrown when the application has no privilege to use the location.</exception>
234 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
235 public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
239 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
240 if (_satelliteStatusChanged == null)
242 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
243 SetSatelliteStatusChangeCallback();
245 _satelliteStatusChanged += value;
249 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Remove called");
250 _satelliteStatusChanged -= value;
251 if (_satelliteStatusChanged == null)
253 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
254 UnSetSatelliteStatusChangeCallback();
259 private void SetSatelliteStatusChangeCallback()
261 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
262 if (_satelliteStatuschangedCallback == null)
264 _satelliteStatuschangedCallback = (numActive, numInView, timestamp, userData) =>
266 Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
267 DateTime timeStamp = DateTime.Now;
271 DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
272 timeStamp = start.ToLocalTime();
274 _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
278 GCHandle handle = GCHandle.Alloc(this);
279 int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, _satelliteStatuschangedCallback, _interval, GCHandle.ToIntPtr(handle));
280 if (((LocationError)ret != LocationError.None))
282 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
283 throw LocationErrorFactory.ThrowLocationException(ret);
287 private void UnSetSatelliteStatusChangeCallback()
289 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
290 int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
291 if (((LocationError)ret != LocationError.None))
293 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
294 throw LocationErrorFactory.ThrowLocationException(ret);
300 /// This class contains the information of the satellite under consideration.
302 /// <since_tizen> 3 </since_tizen>
303 public class SatelliteInformation
306 /// The Class constructor for the SatelliteInformation class.
308 /// <since_tizen> 3 </since_tizen>
309 /// <param name="azimuth">The azimuth value of the satellite in degrees.</param>
310 /// <param name="elevation">The elevation of the satellite in meters.</param>
311 /// <param name="prn">The PRN value of the satellite.</param>
312 /// <param name="snr">The SNR value of the satellite in dB.</param>
313 /// <param name="active">The flag signaling if the satellite is in use.</param>
314 public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool active)
317 Elevation = elevation;
324 /// The azimuth information of the satellite.
326 /// <since_tizen> 3 </since_tizen>
327 public uint Azimuth { get; private set; }
330 /// The elevation information of the satellite.
332 /// <since_tizen> 3 </since_tizen>
333 public uint Elevation { get; private set; }
336 /// The PRN of the satellite.
338 /// <since_tizen> 3 </since_tizen>
339 public uint Prn { get; private set; }
342 /// The SNR of the satellite.
344 /// <since_tizen> 3 </since_tizen>
345 public uint Snr { get; private set; }
348 /// The operational status of the satellite.
350 /// <since_tizen> 3 </since_tizen>
351 public bool Active { get; private set; }