2 * Maemo5LocationPrivate
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
9 #include "maemo5locationprivate.h"
13 * Default constructor of this class.
15 * @param Maemo5Location pointer to public interface.
17 Maemo5LocationPrivate::Maemo5LocationPrivate(Maemo5Location* location):QObject(location)
19 qDebug() << "__Maemo5LocationPrivate";
20 //Initialize variables
24 //Get gps control object
26 control = location_gpsd_control_get_default();
29 device = (LocationGPSDevice*) g_object_new(LOCATION_TYPE_GPS_DEVICE, NULL);
31 g_signal_connect(device, "changed", G_CALLBACK(gps_data_changed), this);
32 g_signal_connect(device, "connected", G_CALLBACK(gps_connected_func), this);
33 g_signal_connect(device, "disconnected", G_CALLBACK(gps_disconnected_func), this);
35 g_signal_connect(control, "error-verbose", G_CALLBACK(gps_error_func), this);
36 g_signal_connect(control, "gpsd_running", G_CALLBACK(gpsd_running_func), this);
37 g_signal_connect(control, "gpsd_stopped", G_CALLBACK(gpsd_running_func), this);
41 * Destructor of this class. Should be used to release all allocated resources.
43 Maemo5LocationPrivate::~Maemo5LocationPrivate()
45 qDebug() << "__~Maemo5LocationPrivate";
51 * This function is used to start to poll with gprs
53 void Maemo5LocationPrivate::get_acwp()
55 g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_ACWP, NULL);
61 * This function starts to poll via gps interface
63 void Maemo5LocationPrivate::get_agnss()
65 g_object_set(G_OBJECT(control), "preferred-method", LOCATION_METHOD_AGNSS, NULL);
73 void Maemo5LocationPrivate::stop()
75 location_gpsd_control_stop(control);
79 * Stop and restart polling
81 void Maemo5LocationPrivate::restart()
83 location_gpsd_control_stop(control);
84 location_gpsd_control_start(control);
88 * This function is called when device managed to connect to the lcoation server.
89 * Function emits gps_connected signal.
91 * @param Pointer to LocationGPSDevice class
92 * @param Pointer to Maemo5LocationPrivate class
94 void gps_connected_func(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
96 emit gps->gps_connected();
100 * This function is called when device is disconnected from the location server.
101 * Function emits gps_disconnected signal.
102 * Also this function resets all arguments of Maemo5LcoationPrivate class.
104 * @param Pointer to LocationGPSDevice class
105 * @param Pointer to Maemo5LocationPrivate class
107 void gps_disconnected_func(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
110 emit gps->gps_disconnected();
114 * This function is called after the location_gpsd_control_stop has been called.
116 * @param Pointer to LocationGPSDControl class
117 * @param Pointer to Maemo5LocationPrivate class
119 void gpsd_stopped_func(LocationGPSDControl *control, Maemo5LocationPrivate *gps)
121 emit gps->gpsd_stopped();
125 * This function is called when an error has occurred.
127 * @param Pointer to LocationGPSDControl class
129 * @param Pointer to Maemo5LocationPrivate class
131 void gps_error_func(LocationGPSDControl *control, gint error, Maemo5LocationPrivate *gps)
135 case LOCATION_ERROR_USER_REJECTED_DIALOG:
136 emit gps->gps_error(0);
137 g_debug("User didn't enable requested methods");
139 case LOCATION_ERROR_USER_REJECTED_SETTINGS:
140 emit gps->gps_error(1);
141 g_debug("User changed settings, which disabled location");
143 case LOCATION_ERROR_BT_GPS_NOT_AVAILABLE:
144 emit gps->gps_error(2);
145 g_debug("Problems with BT GPS");
147 case LOCATION_ERROR_METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
148 emit gps->gps_error(3);
149 g_debug("Requested method is not allowed in offline mode");
151 case LOCATION_ERROR_SYSTEM:
152 emit gps->gps_error(4);
153 g_debug("System error");
159 * This function is called after the location_gpsd_control_start has been called.
161 * @param Pointer to LocationGPSDControl class
162 * @param Pointer to Maemo5LocationPrivate class
164 void gpsd_running_func(LocationGPSDControl *control, Maemo5LocationPrivate *gps)
166 emit gps->gpsd_running();
170 * Callback function to catch gps signals.
172 * @param Pointer to LocationGPSDControl class
173 * @param Pointer to Maemo5LocationPrivate class
175 void gps_data_changed(LocationGPSDevice *device, Maemo5LocationPrivate *gps)
177 //First check that LocationGpsDeviceFix can be found...this data structure contains the location info.
180 //Check that there are fields
181 if(gps->device->fix->fields)
183 //Store values and emit signal
184 if(gps->device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
186 gps->latitude = gps->device->fix->latitude;
187 gps->longitude = gps->device->fix->longitude;
188 gps->eph = gps->device->fix->eph;
192 gps->satellites_in_use = gps->device->satellites_in_use;
193 gps->satellites_in_view = gps->device->satellites_in_view;
195 if(gps->device->fix->fields & LOCATION_GPS_DEVICE_TIME_SET)
197 gps->time = gps->device->fix->time;
198 gps->ept = gps->device->fix->ept;
201 if(gps->device->fix->fields & LOCATION_GPS_DEVICE_ALTITUDE_SET)
203 gps->altitude = gps->device->fix->altitude;
204 gps->epv = gps->device->fix->epv;
207 if(gps->device->fix->fields & LOCATION_GPS_DEVICE_TRACK_SET)
209 gps->track = gps->device->fix->track;
210 gps->epd = gps->device->fix->epd;
213 if(gps->device->fix->fields & LOCATION_GPS_DEVICE_SPEED_SET)
215 gps->speed = gps->device->fix->speed;
216 gps->eps = gps->device->fix->eps;
219 if(gps->device->fix->fields & LOCATION_GPS_DEVICE_CLIMB_SET)
221 gps->climb = gps->device->fix->climb;
222 gps->epc = gps->device->fix->epc;
226 // Calculate average signal strength of satellites in use
228 if(gps->satellites_in_use != 0)
230 for(int i=0 ; i < gps->satellites_in_use ; i++)
232 LocationGPSDeviceSatellite *view = (LocationGPSDeviceSatellite*) g_ptr_array_index (gps->device->satellites, i);
233 temp = temp + view->signal_strength;
235 gps->signal_strength = (temp / gps->satellites_in_use);
243 else if(gps->usegps == 1)
249 emit gps->locationUpdated();
257 * Resets all arguments of Maemo5LocationPrivate class.
259 void Maemo5LocationPrivate::resetAll()
264 satellites_in_view = 0;
265 satellites_in_use = 0;
279 * Get distance between two points in kilometers.
281 * @param double latitude of first point
282 * @param double longitude of first point
283 * @param double latitude of second point
284 * @param double longitude of second point
285 * @return double distance
287 double Maemo5LocationPrivate::distance_between_two_points(double latitude_s, double longitude_s, double latitude_f, double longitude_f)
290 return distance = location_distance_between(latitude_s, longitude_s, latitude_f, longitude_f);