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 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
64 /// The NMEAData from the Satellite.
70 Log.Info(Globals.LogTag, "Getting NMEAData");
75 private string GetNmea()
78 int ret = Interop.GpsSatellite.GetNMEAData(_handle, out value);
79 if (((LocationError)ret != LocationError.None))
81 Log.Error(Globals.LogTag, "Error getting the NMEAData," + (LocationError)ret);
82 throw LocationErrorFactory.ThrowLocationException(ret);
90 /// The Count of Active satellites.
92 public int ActiveCount
96 return (int)GetActiveCount();
100 private uint GetActiveCount()
102 Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
106 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
107 if (((LocationError)ret != LocationError.None))
109 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
110 throw LocationErrorFactory.ThrowLocationException(ret);
116 /// The Count of satellites in view.
118 public int InViewCount
122 return (int)GetInViewCount();
126 private uint GetInViewCount()
128 Log.Info(Globals.LogTag, "Getting the In view count of satellites");
132 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
133 if (((LocationError)ret != LocationError.None))
135 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
136 throw LocationErrorFactory.ThrowLocationException(ret);
142 /// The list of satellites/last recorded satellites in view.
144 public IList<SatelliteInformation> Satellites
148 return GetSatellites();
152 private IList<SatelliteInformation> GetSatellites()
154 List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
155 Log.Info(Globals.LogTag, "Getting the list of satellites");
156 Interop.GpsSatellite.SatelliteStatusinfomationCallback callback = (uint azimuth, uint elevation, uint prn, uint snr, bool isActive, IntPtr userData) =>
158 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, isActive);
159 satelliteList.Add(satellite);
162 int ret = Interop.GpsSatellite.GetForEachSatelliteInView(_handle, callback, IntPtr.Zero);
163 if (((LocationError)ret != LocationError.None))
165 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
166 throw LocationErrorFactory.ThrowLocationException(ret);
168 return satelliteList;
172 /// The constructor of GpsSatellite class.
173 /// <param name="locator"> Locator object initilized using Gps.</param>
175 public GpsSatellite(Locator locator)
177 Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
178 LocationType method = locator.LocationType;
179 if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
182 _handle = _locator.GetHandle();
186 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
187 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
192 /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
193 /// The callback will be invoked periodically (every Interval seconds).
195 public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
199 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
200 if (_satelliteStatusChanged == null)
202 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
203 SetSatelliteStatusChangeCallback();
205 _satelliteStatusChanged += value;
209 Log.Info(Globals.LogTag, "SatelliteStatusUpdated remoove called");
210 _satelliteStatusChanged -= value;
211 if (_satelliteStatusChanged == null)
213 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
214 UnSetSatelliteStatusChangeCallback();
219 private void SetSatelliteStatusChangeCallback()
221 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
222 GCHandle handle = GCHandle.Alloc(this);
223 int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, SatelliteStatusChangedCallback, _interval, GCHandle.ToIntPtr(handle));
224 if (((LocationError)ret != LocationError.None))
226 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
227 throw LocationErrorFactory.ThrowLocationException(ret);
231 private void UnSetSatelliteStatusChangeCallback()
233 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
234 int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
235 if (((LocationError)ret != LocationError.None))
237 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
238 throw LocationErrorFactory.ThrowLocationException(ret);
242 private void SatelliteStatusChangedCallback(uint numActive, uint numInView, int timestamp, IntPtr userData)
244 Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
245 DateTime timeStamp = DateTime.Now;
249 DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
250 timeStamp = start.ToLocalTime();
253 _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
258 /// A class which contains the information of the Satellite under consideration.
260 public class SatelliteInformation
263 /// Class Constructor for SatelliteInformation class.
265 /// <param name="azimuth"> The azimuth value of the satellite in degrees.</param>
266 /// <param name="elevation"> The elevation of the satellite in meters.</param>
267 /// <param name="prn"> The Prn value of the satellite.</param>
268 /// <param name="snr"> The SNR value of the satellite in dB.</param>
269 /// <param name="isActive"> The flag signaling if satellite is in use.</param>
270 public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool isActive)
273 Elevation = elevation;
280 /// The Azimuth information of the Satellite.
282 public uint Azimuth { get; private set; }
285 /// The Elevation information of the Satellite.
287 public uint Elevation { get; private set; }
290 /// The PRN of the Satellite.
292 public uint Prn { get; private set; }
295 /// The SNR of the Satellite.
297 public uint Snr { get; private set; }
300 /// The operational status of the Satellite.
302 public bool Active { get; private set; }