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 = 1;
29 private Locator _locator;
30 private EventHandler<SatelliteStatusChangedEventArgs> _satelliteStatusChanged;
31 private IntPtr _handle = IntPtr.Zero;
33 private Interop.GpsSatellite.SatelliteStatuschangedCallback _satelliteStatuschangedCallback;
34 private Interop.GpsSatellite.SatelliteStatusinfomationCallback _satelliteStatusinfomationCallback;
37 /// The time interval between callback updates.
38 /// Should be in the range [1~120] seconds.
40 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
41 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
46 Log.Info(Globals.LogTag, "Getting the Callback Interval");
51 Log.Info(Globals.LogTag, "Setting the Callback Interval");
52 if (value > 0 && value <= 120)
58 Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
59 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
65 /// The NMEAData from the Satellite.
67 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
68 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
69 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
70 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
75 Log.Info(Globals.LogTag, "Getting NMEAData");
80 private string GetNmea()
83 int ret = Interop.GpsSatellite.GetNMEAData(_handle, out value);
84 if (((LocationError)ret != LocationError.None))
86 Log.Error(Globals.LogTag, "Error getting the NMEAData," + (LocationError)ret);
87 throw LocationErrorFactory.ThrowLocationException(ret);
95 /// The Count of Active satellites.
97 /// <privilege>http://tizen.org/privilege/location</privilege>
98 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
99 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
100 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
101 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
102 public int ActiveCount
106 return (int)GetActiveCount();
110 private uint GetActiveCount()
112 Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
116 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
117 if (((LocationError)ret != LocationError.None))
119 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
120 throw LocationErrorFactory.ThrowLocationException(ret);
126 /// The Count of satellites in view.
128 /// <privilege>http://tizen.org/privilege/location</privilege>
129 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
130 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
131 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
132 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
133 public int InViewCount
137 return (int)GetInViewCount();
141 private uint GetInViewCount()
143 Log.Info(Globals.LogTag, "Getting the In view count of satellites");
147 int ret = Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
148 if (((LocationError)ret != LocationError.None))
150 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
151 throw LocationErrorFactory.ThrowLocationException(ret);
157 /// The list of satellites/last recorded satellites in view.
159 /// <privilege>http://tizen.org/privilege/location</privilege>
160 /// <exception cref="InvalidOperationException">Thrown when the operation is invalid for the current state.</exception>
161 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
162 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
163 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
164 public IList<SatelliteInformation> Satellites
168 return GetSatellites();
172 private IList<SatelliteInformation> GetSatellites()
174 List<SatelliteInformation> satelliteList = new List<SatelliteInformation>();
175 Log.Info(Globals.LogTag, "Getting the list of satellites");
177 if (_satelliteStatusinfomationCallback == null)
179 _satelliteStatusinfomationCallback = (azimuth, elevation, prn, snr, active, userData) =>
181 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, active);
182 satelliteList.Add(satellite);
187 int ret = Interop.GpsSatellite.GetForEachSatelliteInView(_handle, _satelliteStatusinfomationCallback, IntPtr.Zero);
188 if (((LocationError)ret != LocationError.None))
190 Log.Error(Globals.LogTag, "Error getting the satellite" + (LocationError)ret);
191 throw LocationErrorFactory.ThrowLocationException(ret);
193 return satelliteList;
197 /// The constructor of GpsSatellite class.
199 /// <param name="locator"> Locator object initilized using Gps.</param>
200 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
201 public GpsSatellite(Locator locator)
203 Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
206 Log.Error(Globals.LogTag, "locator is null");
207 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
210 LocationType method = locator.LocationType;
211 if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
214 _handle = _locator.GetHandle();
218 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
219 throw LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
224 /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
225 /// The callback will be invoked periodically (every Interval seconds).
227 /// <privilege>http://tizen.org/privilege/location</privilege>
228 /// <exception cref="ArgumentException">Thrown when an invalid argument is used.</exception>
229 /// <exception cref="UnauthroizedAccessException">Thrown when the app has no privilege to use the location.</exception>
230 /// <exception cref="NotSupportedException">Thrown when the location is not supported.</exception>
231 public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
235 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
236 if (_satelliteStatusChanged == null)
238 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
239 SetSatelliteStatusChangeCallback();
241 _satelliteStatusChanged += value;
245 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Remove called");
246 _satelliteStatusChanged -= value;
247 if (_satelliteStatusChanged == null)
249 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
250 UnSetSatelliteStatusChangeCallback();
255 private void SetSatelliteStatusChangeCallback()
257 Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback");
258 if (_satelliteStatuschangedCallback == null)
260 _satelliteStatuschangedCallback = (numActive, numInView, timestamp, 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();
270 _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
274 GCHandle handle = GCHandle.Alloc(this);
275 int ret = Interop.GpsSatellite.SetSatelliteStatusChangedCallback(_handle, _satelliteStatuschangedCallback, _interval, GCHandle.ToIntPtr(handle));
276 if (((LocationError)ret != LocationError.None))
278 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
279 throw LocationErrorFactory.ThrowLocationException(ret);
283 private void UnSetSatelliteStatusChangeCallback()
285 Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
286 int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
287 if (((LocationError)ret != LocationError.None))
289 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
290 throw LocationErrorFactory.ThrowLocationException(ret);
296 /// A class which contains the information of the Satellite under consideration.
298 public class SatelliteInformation
301 /// Class Constructor for SatelliteInformation class.
303 /// <param name="azimuth"> The azimuth value of the satellite in degrees.</param>
304 /// <param name="elevation"> The elevation of the satellite in meters.</param>
305 /// <param name="prn"> The Prn value of the satellite.</param>
306 /// <param name="snr"> The SNR value of the satellite in dB.</param>
307 /// <param name="active"> The flag signaling if satellite is in use.</param>
308 public SatelliteInformation(uint azimuth, uint elevation, uint prn, uint snr, bool active)
311 Elevation = elevation;
318 /// The Azimuth information of the Satellite.
320 public uint Azimuth { get; private set; }
323 /// The Elevation information of the Satellite.
325 public uint Elevation { get; private set; }
328 /// The PRN of the Satellite.
330 public uint Prn { get; private set; }
333 /// The SNR of the Satellite.
335 public uint Snr { get; private set; }
338 /// The operational status of the Satellite.
340 public bool Active { get; private set; }