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 /// A class which contains the functionality for obtaining information about Gps satellites in range and in use.
26 public class GpsSatellite
28 private int _interval = 120;
29 private Locator _locator;
30 private EventHandler<SatelliteStatusChangedEventArgs> _satelliteStatusChanged;
31 private IntPtr _handle = IntPtr.Zero;
34 /// The time interval between callback updates.
35 /// Should be in the range [1~120] seconds.
41 Log.Info(Globals.LogTag, "Getting the Callback Interval");
46 Log.Info(Globals.LogTag, "Setting the Callback Interval");
47 if (value >= 0 && value <= 120)
50 if (_satelliteStatusChanged != null)
52 SetSatelliteStatusChangeCallback();
56 Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
57 LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
64 /// The NMEAData from the Satellite.
70 Log.Info(Globals.LogTag, "Getting NMEAData");
75 private string GetNmea()
78 Interop.GpsSatellite.GetNMEAData(_handle, out value);
85 /// The Count of Active satellites.
87 public int ActiveCount
91 return (int)GetActiveCount();
95 private uint GetActiveCount()
97 Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
101 Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
106 /// The Count of satellites in view.
108 public int InViewCount
112 return (int)GetInViewCount();
116 private uint GetInViewCount()
118 Log.Info(Globals.LogTag, "Getting the In view count of satellites");
122 Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
127 /// The list of satellites/last recorded satellites in view.
129 public IList<SatelliteInformation> Satellites
133 return GetSatellites();
137 private IList<SatelliteInformation> GetSatellites()
139 List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
140 Log.Info(Globals.LogTag, "Getting the list of satellites");
141 Interop.GpsSatellite.SatelliteStatusinfomationCallback callback = (uint azimuth, uint elevation, uint prn, uint snr, bool isActive, IntPtr userData) =>
143 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, isActive);
144 satelliteList.Add(satellite);
147 Interop.GpsSatellite.GetForEachSatelliteInView(_handle, callback, IntPtr.Zero);
148 return satelliteList;
152 /// The constructor of GpsSatellite class.
153 /// <param name="locator"> Locator object initilized using Gps.</param>
155 public GpsSatellite(Locator locator)
157 Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
158 LocationType method = locator.LocationType;
159 if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
162 _handle = _locator.GetHandle();
166 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
167 LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
172 /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
173 /// The callback will be invoked periodically (every Interval seconds).
175 public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
179 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
180 if (_satelliteStatusChanged == null)
182 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
183 SetSatelliteStatusChangeCallback();
185 _satelliteStatusChanged += value;
189 Log.Info(Globals.LogTag, "SatelliteStatusUpdated remoove called");
190 _satelliteStatusChanged -= value;
191 if (_satelliteStatusChanged == null)
193 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
194 UnSetSatelliteStatusChangeCallback();
199 private void SetSatelliteStatusChangeCallback()
201 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
202 GCHandle handle = GCHandle.Alloc(this);
203 int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, SatelliteStatusChangedCallback, _interval, GCHandle.ToIntPtr(handle));
204 if (((LocationError)ret != LocationError.None))
206 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
207 LocationErrorFactory.ThrowLocationException(ret);
211 private void UnSetSatelliteStatusChangeCallback()
213 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
214 int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
215 if (((LocationError)ret != LocationError.None))
217 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
218 LocationErrorFactory.ThrowLocationException(ret);
222 private void SatelliteStatusChangedCallback(uint numActive, uint numInView, int timestamp, IntPtr userData)
224 Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
225 DateTime timeStamp = DateTime.Now;
229 DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
230 timeStamp = start.ToLocalTime();
233 _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
238 /// A class which contains the information of the Satellite under consideration.
240 public class SatelliteInformation
243 /// Class Constructor for SatelliteInformation class.
245 /// <param name="azimuth"> The azimuth value of the satellite in degrees.</param>
246 /// <param name="elevation"> The elevation of the satellite in meters.</param>
247 /// <param name="prn"> The Prn value of the satellite.</param>
248 /// <param name="snr"> The SNR value of the satellite in dB.</param>
249 /// <param name="isActive"> The flag signaling if satellite is in use.</param>
250 public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool isActive)
253 Elevation = elevation;
260 /// The Azimuth information of the Satellite.
262 public uint Azimuth { get; private set; }
265 /// The Elevation information of the Satellite.
267 public uint Elevation { get; private set; }
270 /// The PRN of the Satellite.
272 public uint Prn { get; private set; }
275 /// The SNR of the Satellite.
277 public uint Snr { get; private set; }
280 /// The operational status of the Satellite.
282 public bool Active { get; private set; }