Updated documentation preparing for refactoring
[speedfreak] / Client / maemo5locationprivate.cpp
1 /*
2  * Maemo5LocationPrivate
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "maemo5locationprivate.h"
10 #include <QDebug>
11
12 /**
13   *Default constructor of this class.
14   *@param Maemo5Location pointer to public interface.
15   */
16 Maemo5LocationPrivate::Maemo5LocationPrivate(Maemo5Location* location):QObject(location)
17 {
18     qDebug() << "__Maemo5LocationPrivate";
19     //Initialize variables
20     gps_online = false;
21     usegps = -1;
22     resetAll();
23     //Get gps control object
24     control = NULL;
25     control = location_gpsd_control_get_default();
26     //create gps device
27     device = NULL;
28     device = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
29
30     g_signal_connect(device, "changed", G_CALLBACK(gps_data_changed), this);
31     g_signal_connect(device, "connected", G_CALLBACK(gps_connected_func), this);
32     g_signal_connect(device, "disconnected", G_CALLBACK(gps_disconnected_func), this);
33
34     g_signal_connect(control, "error-verbose", G_CALLBACK(gps_error_func), this);
35     g_signal_connect(control, "gpsd_running", G_CALLBACK(gpsd_running_func), this);
36     g_signal_connect(control, "gpsd_stopped", G_CALLBACK(gpsd_running_func), this);
37
38 }
39 /**
40   *Destructor of this class. Should be used to release all allocated resources.
41   */
42 Maemo5LocationPrivate::~Maemo5LocationPrivate()
43 {
44     qDebug() << "__~Maemo5LocationPrivate";
45     //delete device;
46     //delete control;
47 }
48
49 /**
50   *This function is used to start to poll with gprs
51   */
52 void Maemo5LocationPrivate::get_acwp()
53 {
54     g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
55     usegps = 0;
56     restart();
57 }
58
59 /**
60   *This function starts to poll via gps interface
61   */
62 void Maemo5LocationPrivate::get_agnss()
63 {
64     g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_AGNSS, NULL);
65     usegps = 1;
66     restart();
67 }
68
69 /**
70   *Stop polling gps
71   */
72 void Maemo5LocationPrivate::stop()
73 {
74     location_gpsd_control_stop(control);
75 }
76
77 /**
78   *Stop and restart polling
79   */
80 void Maemo5LocationPrivate::restart()
81 {
82     location_gpsd_control_stop(control);
83     location_gpsd_control_start(control);
84 }
85
86 /**
87   *This function is called when device managed to connect to the lcoation server.
88   *Function emits gps_connected signal.
89   *@param Pointer to LocationGPSDevice class
90   *@param Pointer to Maemo5LocationPrivate class
91   */
92 void gps_connected_func(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
93 {
94     emit gps->gps_connected();
95 }
96
97 /**
98   *This function is called when device is disconnected from the location server.
99   *Function emits gps_disconnected signal.
100   *Also this function resets all arguments of Maemo5LcoationPrivate class.
101   *@param Pointer to LocationGPSDevice class
102   *@param Pointer to Maemo5LocationPrivate class
103   */
104 void gps_disconnected_func(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
105 {
106     gps->resetAll();
107     emit gps->gps_disconnected();
108 }
109
110 /**
111   *This function is called after the location_gpsd_control_stop has been called.
112   *@param Pointer to LocationGPSDControl class
113   *@param Pointer to Maemo5LocationPrivate class
114   */
115 void gpsd_stopped_func(LocationGPSDControl *control, Maemo5LocationPrivate *gps)
116 {
117     emit gps->gpsd_stopped();
118 }
119
120 /**
121   *This function is called when an error has occurred.
122   *@param Pointer to LocationGPSDControl class
123   *@param error code
124   *@param Pointer to Maemo5LocationPrivate class
125   */
126 void gps_error_func(LocationGPSDControl *control, gint error, Maemo5LocationPrivate *gps)
127 {
128
129     switch (error) {
130       case LOCATION_ERROR_USER_REJECTED_DIALOG:
131         emit gps->gps_error(0);
132         g_debug("User didn't enable requested methods");
133         break;
134       case LOCATION_ERROR_USER_REJECTED_SETTINGS:
135         emit gps->gps_error(1);
136         g_debug("User changed settings, which disabled location");
137         break;
138       case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
139         emit gps->gps_error(2);
140         g_debug("Problems with BT GPS");
141         break;
142       case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
143         emit gps->gps_error(3);
144         g_debug("Requested method is not allowed in offline mode");
145         break;
146       case LOCATION_ERROR_SYSTEM:
147         emit gps->gps_error(4);
148         g_debug("System error");
149         break;
150       }
151 }
152
153 /**
154   *This function is called after the location_gpsd_control_start has been called.
155   *@param Pointer to LocationGPSDControl class
156   *@param Pointer to Maemo5LocationPrivate class
157   */
158 void gpsd_running_func(LocationGPSDControl *control, Maemo5LocationPrivate *gps)
159 {
160     emit gps->gpsd_running();
161 }
162
163 /**
164   *Callback function to catch gps signals.
165   *@param Pointer to LocationGPSDControl class
166   *@param Pointer to Maemo5LocationPrivate class
167   */
168 void gps_data_changed(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
169 {
170     //First check that LocationGpsDeviceFix can be found...this data structure contains the location info.
171     if(gps->device->fix)
172     {
173         //Check that there are fields
174         if(gps->device->fix->fields)
175         {
176             //Store values and emit signal
177             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
178             {
179                 gps->latitude = gps->device->fix->latitude;
180                 gps->longitude = gps->device->fix->longitude;
181                 gps->eph = gps->device->fix->eph;
182             }
183
184
185             gps->satellites_in_use = gps->device->satellites_in_use;
186             gps->satellites_in_view = gps->device->satellites_in_view;
187
188             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET)
189             {
190                 gps->time = gps->device->fix->time;
191                 gps->ept = gps->device->fix->ept;
192             }
193
194             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
195             {
196                 gps->altitude = gps->device->fix->altitude;
197                 gps->epv = gps->device->fix->epv;
198             }
199
200             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
201             {
202                 gps->track = gps->device->fix->track;
203                 gps->epd = gps->device->fix->epd;
204             }
205
206             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
207             {
208                 gps->speed = gps->device->fix->speed;
209                 gps->eps = gps->device->fix->eps;
210             }
211
212             if(gps->device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
213             {
214                 gps->climb = gps->device->fix->climb;
215                 gps->epc = gps->device->fix->epc;
216             }
217
218
219             // Calculate average signal strength of satellites in use
220             int temp = 0;
221             if(gps->satellites_in_use != 0)
222             {
223                 for(int i=0 ; i < gps->satellites_in_use ; i++)
224                 {
225                     LocationGPSDeviceSatellite *view = (LocationGPSDeviceSatellite*) g_ptr_array_index (gps->device->satellites, i);
226                     temp = temp + view->signal_strength;
227                 }
228                 gps->signal_strength = (temp / gps->satellites_in_use);
229             }
230
231
232             if(gps->usegps == 0)
233             {
234                 emit gps->awcp();
235             }
236             else if(gps->usegps == 1)
237             {
238                 emit gps->agnss();
239             }
240             else
241             {
242                 emit gps->locationUpdated();
243             }
244         }
245     }
246
247 }
248
249 /**
250   *Resets all arguments of Maemo5LocationPrivate class.
251   */
252 void Maemo5LocationPrivate::resetAll()
253 {
254     time = 0;
255     latitude = 0;
256     longitude = 0;
257     satellites_in_view = 0;
258     satellites_in_use = 0;
259     ept = 0;
260     eph = 0;
261     altitude = 0;
262     epv = 0;
263     track = 0;
264     epd = 0;
265     speed = 0;
266     eps = 0;
267     climb = 0;
268     epc = 0;
269 }
270
271 /**
272   *Returns distance between two points in kilometers.
273   *@param latitude of first point
274   *@param longitude of first point
275   *@param latitude of second point
276   *@param longitude of second point
277   */
278 double Maemo5LocationPrivate::distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f)
279 {
280     double distance = 0;
281     return distance = location_distance_between(latitude_s, longitude_s, latitude_f, longitude_f);
282 }