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.
37 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
38 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
43 Log.Info(Globals.LogTag, "Getting the Callback Interval");
48 Log.Info(Globals.LogTag, "Setting the Callback Interval");
49 if (value > 0 && value <= 120)
55 Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
56 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
62 /// The NMEAData from the Satellite.
64 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
65 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
66 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location</exception>
67 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
72 Log.Info(Globals.LogTag, "Getting NMEAData");
77 private string GetNmea()
80 int ret = Interop.GpsSatellite.GetNMEAData(_handle, out value);
81 if (((LocationError)ret != LocationError.None))
83 Log.Error(Globals.LogTag, "Error getting the NMEAData," + (LocationError)ret);
84 throw LocationErrorFactory.ThrowLocationException(ret);
92 /// The Count of Active satellites.
94 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
95 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
96 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location</exception>
97 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
98 public int ActiveCount
102 return (int)GetActiveCount();
106 private uint GetActiveCount()
108 Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
112 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
113 if (((LocationError)ret != LocationError.None))
115 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
116 throw LocationErrorFactory.ThrowLocationException(ret);
122 /// The Count of satellites in view.
124 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
125 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
126 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location</exception>
127 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
128 public int InViewCount
132 return (int)GetInViewCount();
136 private uint GetInViewCount()
138 Log.Info(Globals.LogTag, "Getting the In view count of satellites");
142 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
143 if (((LocationError)ret != LocationError.None))
145 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
146 throw LocationErrorFactory.ThrowLocationException(ret);
152 /// The list of satellites/last recorded satellites in view.
154 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state</exception>
155 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
156 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location</exception>
157 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
158 public IList<SatelliteInformation> Satellites
162 return GetSatellites();
166 private IList<SatelliteInformation> GetSatellites()
168 List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
169 Log.Info(Globals.LogTag, "Getting the list of satellites");
170 Interop.GpsSatellite.SatelliteStatusinfomationCallback callback = (uint azimuth, uint elevation, uint prn, uint snr, bool isActive, IntPtr userData) =>
172 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, isActive);
173 satelliteList.Add(satellite);
176 int ret = Interop.GpsSatellite.GetForEachSatelliteInView(_handle, callback, IntPtr.Zero);
177 if (((LocationError)ret != LocationError.None))
179 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
180 throw LocationErrorFactory.ThrowLocationException(ret);
182 return satelliteList;
186 /// The constructor of GpsSatellite class.
187 /// <param name="locator"> Locator object initilized using Gps.</param>
189 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
190 public GpsSatellite(Locator locator)
192 Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
193 LocationType method = locator.LocationType;
194 if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
197 _handle = _locator.GetHandle();
201 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
202 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
207 /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
208 /// The callback will be invoked periodically (every Interval seconds).
210 /// <exception cref="ArgumentException">Thrown when an invalid argument is used</exception>
211 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location</exception>
212 /// <exception cref="NotSupportedException">Thrown when the location is not supported</exception>
213 public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
217 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
218 if (_satelliteStatusChanged == null)
220 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
221 SetSatelliteStatusChangeCallback();
223 _satelliteStatusChanged += value;
227 Log.Info(Globals.LogTag, "SatelliteStatusUpdated remoove called");
228 _satelliteStatusChanged -= value;
229 if (_satelliteStatusChanged == null)
231 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
232 UnSetSatelliteStatusChangeCallback();
237 private void SetSatelliteStatusChangeCallback()
239 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
240 GCHandle handle = GCHandle.Alloc(this);
241 int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, SatelliteStatusChangedCallback, _interval, GCHandle.ToIntPtr(handle));
242 if (((LocationError)ret != LocationError.None))
244 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
245 throw LocationErrorFactory.ThrowLocationException(ret);
249 private void UnSetSatelliteStatusChangeCallback()
251 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
252 int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
253 if (((LocationError)ret != LocationError.None))
255 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
256 throw LocationErrorFactory.ThrowLocationException(ret);
260 private void SatelliteStatusChangedCallback(uint numActive, uint numInView, int timestamp, IntPtr userData)
262 Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
263 DateTime timeStamp = DateTime.Now;
267 DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
268 timeStamp = start.ToLocalTime();
271 _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
276 /// A class which contains the information of the Satellite under consideration.
278 public class SatelliteInformation
281 /// Class Constructor for SatelliteInformation class.
283 /// <param name="azimuth"> The azimuth value of the satellite in degrees.</param>
284 /// <param name="elevation"> The elevation of the satellite in meters.</param>
285 /// <param name="prn"> The Prn value of the satellite.</param>
286 /// <param name="snr"> The SNR value of the satellite in dB.</param>
287 /// <param name="isActive"> The flag signaling if satellite is in use.</param>
288 public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool isActive)
291 Elevation = elevation;
298 /// The Azimuth information of the Satellite.
300 public uint Azimuth { get; private set; }
303 /// The Elevation information of the Satellite.
305 public uint Elevation { get; private set; }
308 /// The PRN of the Satellite.
310 public uint Prn { get; private set; }
313 /// The SNR of the Satellite.
315 public uint Snr { get; private set; }
318 /// The operational status of the Satellite.
320 public bool Active { get; private set; }