1 // Copyright 2016 by Samsung Electronics, Inc.,
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
10 using System.Collections.Generic;
11 using System.Runtime.InteropServices;
13 namespace Tizen.Location
16 /// A class which contains the functionality for obtaining information about Gps satellites in range and in use.
18 public class GpsSatellite
20 private int _interval = 120;
21 private Locator _locator;
22 private EventHandler<SatelliteStatusChangedEventArgs> _satelliteStatusChanged;
23 private IntPtr _handle = IntPtr.Zero;
26 /// The time interval between callback updates.
27 /// Should be in the range [1~120] seconds.
33 Log.Info(Globals.LogTag, "Getting the Callback Interval");
38 Log.Info(Globals.LogTag, "Setting the Callback Interval");
39 if (value >= 0 && value <= 120)
42 if (_satelliteStatusChanged != null)
44 SetSatelliteStatusChangeCallback();
48 Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
49 LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
56 /// The NMEAData from the Satellite.
62 Log.Info(Globals.LogTag, "Getting NMEAData");
67 private string GetNmea()
70 Interop.GpsSatellite.GetNMEAData(_handle, out value);
77 /// The Count of Active satellites.
79 public int ActiveCount
83 return (int)GetActiveCount();
87 private uint GetActiveCount()
89 Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
93 Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
98 /// The Count of satellites in view.
100 public int InViewCount
104 return (int)GetInViewCount();
108 private uint GetInViewCount()
110 Log.Info(Globals.LogTag, "Getting the In view count of satellites");
114 Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
119 /// The list of satellites/last recorded satellites in view.
121 public IList<SatelliteInformation> Satellites
125 return GetSatellites();
129 private IList<SatelliteInformation> GetSatellites()
131 List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
132 Log.Info(Globals.LogTag, "Getting the list of satellites");
133 Interop.GpsSatellite.SatelliteStatusinfomationCallback callback = (uint azimuth, uint elevation, uint prn, uint snr, bool isActive, IntPtr userData) =>
135 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, isActive);
136 satelliteList.Add(satellite);
139 Interop.GpsSatellite.GetForEachSatelliteInView(_handle, callback, IntPtr.Zero);
140 return satelliteList;
144 /// The constructor of GpsSatellite class.
145 /// <param name="locator"> Locator object initilized using Gps.</param>
147 public GpsSatellite(Locator locator)
149 Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
150 LocationType method = locator.LocationType;
151 if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
154 _handle = _locator.GetHandle();
158 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
159 LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
164 /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
165 /// The callback will be invoked periodically (every Interval seconds).
167 public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
171 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
172 if (_satelliteStatusChanged == null)
174 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
175 SetSatelliteStatusChangeCallback();
177 _satelliteStatusChanged += value;
181 Log.Info(Globals.LogTag, "SatelliteStatusUpdated remoove called");
182 _satelliteStatusChanged -= value;
183 if (_satelliteStatusChanged == null)
185 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
186 UnSetSatelliteStatusChangeCallback();
191 private void SetSatelliteStatusChangeCallback()
193 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
194 GCHandle handle = GCHandle.Alloc(this);
195 int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, SatelliteStatusChangedCallback, _interval, GCHandle.ToIntPtr(handle));
196 if (((LocationError)ret != LocationError.None))
198 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
199 LocationErrorFactory.ThrowLocationException(ret);
203 private void UnSetSatelliteStatusChangeCallback()
205 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
206 int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
207 if (((LocationError)ret != LocationError.None))
209 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
210 LocationErrorFactory.ThrowLocationException(ret);
214 private void SatelliteStatusChangedCallback(uint numActive, uint numInView, int timestamp, IntPtr userData)
216 Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
217 DateTime timeStamp = DateTime.Now;
221 DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
222 timeStamp = start.ToLocalTime();
225 _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
230 /// A class which contains the information of the Satellite under consideration.
232 public class SatelliteInformation
235 /// Class Constructor for SatelliteInformation class.
237 /// <param name="azimuth"> The azimuth value of the satellite in degrees.</param>
238 /// <param name="elevation"> The elevation of the satellite in meters.</param>
239 /// <param name="prn"> The Prn value of the satellite.</param>
240 /// <param name="snr"> The SNR value of the satellite in dB.</param>
241 /// <param name="isActive"> The flag signaling if satellite is in use.</param>
242 public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool isActive)
245 Elevation = elevation;
252 /// The Azimuth information of the Satellite.
254 public uint Azimuth { get; private set; }
257 /// The Elevation information of the Satellite.
259 public uint Elevation { get; private set; }
262 /// The PRN of the Satellite.
264 public uint Prn { get; private set; }
267 /// The SNR of the Satellite.
269 public uint Snr { get; private set; }
272 /// The operational status of the Satellite.
274 public bool Active { get; private set; }