922594998678c51555f8bc4bc2b3387759ac8482
[platform/core/csapi/tizenfx.git] / src / Tizen.Location / Tizen.Location / GpsSatellite.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
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.
8
9 using System;
10 using System.Collections.Generic;
11 using System.Runtime.InteropServices;
12
13 namespace Tizen.Location
14 {
15     /// <summary>
16     /// A class which contains the functionality for obtaining information about Gps satellites in range and in use.
17     /// </summary>
18     public class GpsSatellite
19     {
20         private int _interval = 120;
21         private Locator _locator;
22         private EventHandler<SatelliteStatusChangedEventArgs> _satelliteStatusChanged;
23         private IntPtr _handle = IntPtr.Zero;
24
25         /// <summary>
26         /// The time interval between callback updates.
27         /// Should be in the range [1~120] seconds.
28         /// </summary>
29         public int Interval
30         {
31             get
32             {
33                 Log.Info(Globals.LogTag, "Getting the Callback Interval");
34                 return _interval;
35             }
36             set
37             {
38                 Log.Info(Globals.LogTag, "Setting the Callback Interval");
39                 if (value >= 0 && value <= 120)
40                 {
41                     _interval = value;
42                     if (_satelliteStatusChanged != null)
43                     {
44                         SetSatelliteStatusChangeCallback();
45                     }
46                     else
47                     {
48                         Log.Error(Globals.LogTag, "Error Setting the Callback Interval");
49                         LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
50                     }
51                 }
52             }
53         }
54
55         /// <summary>
56         /// The NMEAData from the Satellite.
57         /// </summary>
58         public string Nmea
59         {
60             get
61             {
62                 Log.Info(Globals.LogTag, "Getting NMEAData");
63                 return GetNmea();
64             }
65         }
66
67         private string GetNmea()
68         {
69             string value = null;
70             Interop.GpsSatellite.GetNMEAData(_handle, out value);
71
72             return value;
73         }
74
75
76         /// <summary>
77         /// The Count of Active satellites.
78         /// </summary>
79         public int ActiveCount
80         {
81             get
82             {
83                 return (int)GetActiveCount();
84             }
85         }
86
87         private uint GetActiveCount()
88         {
89             Log.Info(Globals.LogTag, "Getting the ActiveCount of satellites");
90             uint numActive = 0;
91             uint numInView;
92             int timestamp;
93             Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
94             return numActive;
95         }
96
97         /// <summary>
98         /// The Count of satellites in view.
99         /// </summary>
100         public int InViewCount
101         {
102             get
103             {
104                 return (int)GetInViewCount();
105             }
106         }
107
108         private uint GetInViewCount()
109         {
110             Log.Info(Globals.LogTag, "Getting the In view count of satellites");
111             uint numActive;
112             uint numInView = 0;
113             int timestamp;
114             Interop.GpsSatellite.GetSatelliteStatus(_handle, out numActive, out numInView, out timestamp);
115             return numInView;
116         }
117
118         /// <summary>
119         /// The list of satellites/last recorded satellites in view.
120         /// </summary>
121         public IList<SatelliteInformation> Satellites
122         {
123             get
124             {
125                 return GetSatellites();
126             }
127         }
128
129         private IList<SatelliteInformation> GetSatellites()
130         {
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) =>
134             {
135                 SatelliteInformation satellite = new SatelliteInformation(azimuth, elevation, prn, snr, isActive);
136                 satelliteList.Add(satellite);
137                 return true;
138             };
139             Interop.GpsSatellite.GetForEachSatelliteInView(_handle, callback, IntPtr.Zero);
140             return satelliteList;
141         }
142
143         /// <summary>
144         /// The constructor of GpsSatellite class.
145         /// <param name="locator"> Locator object initilized using Gps.</param>
146         /// </summary>
147         public GpsSatellite(Locator locator)
148         {
149             Log.Info(Globals.LogTag, "Calling GpsSatellite constructor");
150             LocationType method = locator.LocationType;
151             if (method.Equals(LocationType.Gps) || method.Equals(LocationType.Hybrid))
152             {
153                 _locator = locator;
154                 _handle = _locator.GetHandle();
155             }
156             else
157             {
158                 Log.Error(Globals.LogTag, "Error constructing GpsSatellite class");
159                 LocationErrorFactory.ThrowLocationException((int)LocationError.InvalidParameter);
160             }
161         }
162
163         /// <summary>
164         /// (event) SatelliteStatusUpdated is raised whenever satellite information is updated.
165         /// The callback will be invoked periodically (every Interval seconds).
166         /// </summary>
167         public event EventHandler<SatelliteStatusChangedEventArgs> SatelliteStatusUpdated
168         {
169             add
170             {
171                 Log.Info(Globals.LogTag, "SatelliteStatusUpdated Add called");
172                 if (_satelliteStatusChanged == null)
173                 {
174                     Log.Info(Globals.LogTag, "SetSatelliteStatusChangeCallback called");
175                     SetSatelliteStatusChangeCallback();
176                 }
177                 _satelliteStatusChanged += value;
178             }
179             remove
180             {
181                 Log.Info(Globals.LogTag, "SatelliteStatusUpdated remoove called");
182                 _satelliteStatusChanged -= value;
183                 if (_satelliteStatusChanged == null)
184                 {
185                     Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback called");
186                     UnSetSatelliteStatusChangeCallback();
187                 }
188             }
189         }
190
191         private void SetSatelliteStatusChangeCallback()
192         {
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))
197             {
198                 Log.Error(Globals.LogTag, "Error in setting satellite status changed callback," + (LocationError)ret);
199                 LocationErrorFactory.ThrowLocationException(ret);
200             }
201         }
202
203         private void UnSetSatelliteStatusChangeCallback()
204         {
205             Log.Info(Globals.LogTag, "UnSetSatelliteStatusChangeCallback");
206             int ret = Interop.GpsSatellite.UnSetSatelliteStatusChangedCallback(_handle);
207             if (((LocationError)ret != LocationError.None))
208             {
209                 Log.Error(Globals.LogTag, "Error in Getting Unsetting satellite status changed callback," + (LocationError)ret);
210                 LocationErrorFactory.ThrowLocationException(ret);
211             }
212         }
213
214         private void SatelliteStatusChangedCallback(uint numActive, uint numInView, int timestamp, IntPtr userData)
215         {
216             Log.Info(Globals.LogTag, "Inside SatelliteStatusChangedCallback");
217             DateTime timeStamp = DateTime.Now;
218
219             if (timestamp != 0)
220             {
221                 DateTime start = DateTime.SpecifyKind(new DateTime(1970, 1, 1).AddSeconds(timestamp), DateTimeKind.Utc);
222                 timeStamp = start.ToLocalTime();
223             }
224
225             _satelliteStatusChanged?.Invoke(_handle, new SatelliteStatusChangedEventArgs(numActive, numInView, timeStamp));
226         }
227     }
228
229     /// <summary>
230     /// A class which contains the information of the Satellite under consideration.
231     /// </summary>
232     public class SatelliteInformation
233     {
234         /// <summary>
235         /// Class Constructor for SatelliteInformation class.
236         /// </summary>
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)
243         {
244             Azimuth = azimuth;
245             Elevation = elevation;
246             Prn = prn;
247             Snr = snr;
248             Active = isActive;
249         }
250
251         /// <summary>
252         /// The Azimuth information of the Satellite.
253         /// </summary>
254         public uint Azimuth { get; private set; }
255
256         /// <summary>
257         /// The Elevation information of the Satellite.
258         /// </summary>
259         public uint Elevation { get; private set; }
260
261         /// <summary>
262         /// The PRN of the Satellite.
263         /// </summary>
264         public uint Prn { get; private set; }
265
266         /// <summary>
267         /// The SNR of the Satellite.
268         /// </summary>
269         public uint Snr { get; private set; }
270
271         /// <summary>
272         /// The operational status of the Satellite.
273         /// </summary>
274         public bool Active { get; private set; }
275     }
276 }