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