Skip to content

Commit 3cd24c4

Browse files
committed
Move GPS thread function into gps/mzd_gps.cpp, instead of main.cpp
1 parent 509b9fd commit 3cd24c4

File tree

3 files changed

+104
-92
lines changed

3 files changed

+104
-92
lines changed

mazda/gps/mzd_gps.cpp

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1-
2-
#include <dbus/dbus.h>
31
#include <dbus-c++/dbus.h>
42
#include <memory>
3+
#include <atomic>
4+
#include <condition_variable>
5+
#include <ctime>
56

67
#include "../dbus/generated_cmu.h"
78

89
#define LOGTAG "mazda-gps"
910

1011
#include "hu_uti.h"
12+
#include "hu_aap.h"
13+
14+
#include "config.h"
15+
1116

1217
#include "mzd_gps.h"
18+
#include "../main.h"
1319

1420
#define SERVICE_BUS_ADDRESS "unix:path=/tmp/dbus_service_socket"
21+
// Check the content folder. sd_nav still exists without the card installed
22+
#define SD_CARD_PATH "/tmp/mnt/sd_nav/content"
1523

1624
enum LDSControl
1725
{
@@ -59,7 +67,7 @@ void GPSLDSControl::ReadStatus(const int32_t& commandReply, const int32_t& statu
5967

6068
void mzd_gps2_start(DBus::Connection& serviceBus)
6169
{
62-
if (gps_client != NULL)
70+
if (gps_client != nullptr)
6371
return;
6472

6573
try
@@ -80,7 +88,7 @@ void mzd_gps2_start(DBus::Connection& serviceBus)
8088

8189
bool mzd_gps2_get(GPSData& data)
8290
{
83-
if (gps_client == NULL)
91+
if (gps_client == nullptr)
8492
return false;
8593

8694
try
@@ -146,3 +154,89 @@ bool GPSData::IsSame(const GPSData& other) const
146154
int32_t(horizontalAccuracy * 1E7) == int32_t(other.horizontalAccuracy * 1E7) &&
147155
int32_t(verticalAccuracy * 1E7) == int32_t(other.verticalAccuracy * 1E7);
148156
}
157+
158+
void gps_thread_func(std::condition_variable *quitcv, std::mutex *quitmutex, DBus::Connection *serviceBus, std::atomic<bool> *exiting)
159+
{
160+
GPSData data, newData;
161+
uint64_t oldTs = 0;
162+
int debugLogCount = 0;
163+
mzd_gps2_start(*serviceBus);
164+
165+
//Not sure if this is actually required but the built-in Nav code on CMU does it
166+
mzd_gps2_set_enabled(true);
167+
168+
config::readConfig();
169+
while (!exiting->load(std::memory_order_relaxed))
170+
{
171+
logd("Getting GPS Data");
172+
if (config::carGPS && mzd_gps2_get(newData) && !data.IsSame(newData))
173+
{
174+
data = newData;
175+
timeval tv{};
176+
gettimeofday(&tv, nullptr);
177+
uint64_t timestamp = tv.tv_sec * 1000000 + tv.tv_usec;
178+
if (debugLogCount < 50) //only print the first 50 to avoid spamming the log and breaking the opera text box
179+
{
180+
logd("GPS data: %d %d %f %f %d %f %f %f %f \n",data.positionAccuracy, data.uTCtime, data.latitude, data.longitude, data.altitude, data.heading, data.velocity, data.horizontalAccuracy, data.verticalAccuracy);
181+
logd("Delta %f\n", (timestamp - oldTs)/1000000.0);
182+
debugLogCount++;
183+
}
184+
oldTs = timestamp;
185+
186+
g_hu->hu_queue_command([data, timestamp](IHUConnectionThreadInterface& s)
187+
{
188+
HU::SensorEvent sensorEvent;
189+
HU::SensorEvent::LocationData* location = sensorEvent.add_location_data();
190+
//AA uses uS and the gps data just has seconds, just use the current time to get more precision so AA can
191+
//interpolate better
192+
location->set_timestamp(timestamp);
193+
location->set_latitude(static_cast<int32_t>(data.latitude * 1E7));
194+
location->set_longitude(static_cast<int32_t>(data.longitude * 1E7));
195+
196+
// If the sd card exists then reverse heading. This should only be used on installs that have the
197+
// reversed heading issue.
198+
double newHeading = data.heading;
199+
200+
if (config::reverseGPS)
201+
{
202+
const char* sdCardFolder;
203+
sdCardFolder = SD_CARD_PATH;
204+
struct stat sb{};
205+
206+
if (stat(sdCardFolder, &sb) == 0 && S_ISDIR(sb.st_mode))
207+
{
208+
newHeading = data.heading + 180;
209+
if (newHeading >= 360)
210+
{
211+
newHeading = newHeading - 360;
212+
}
213+
}
214+
}
215+
216+
location->set_bearing(static_cast<int32_t>(newHeading * 1E6));
217+
//assuming these are the same units as the Android Location API (the rest are)
218+
double velocityMetersPerSecond = data.velocity * 0.277778; //convert km/h to m/s
219+
location->set_speed(static_cast<int32_t>(velocityMetersPerSecond * 1E3));
220+
221+
location->set_altitude(static_cast<int32_t>(data.altitude * 1E2));
222+
location->set_accuracy(static_cast<int32_t>(data.horizontalAccuracy * 1E3));
223+
224+
s.hu_aap_enc_send_message(0, AA_CH_SEN, HU_SENSOR_CHANNEL_MESSAGE::SensorEvent, sensorEvent);
225+
});
226+
}
227+
logd("Done Getting GPS Data");
228+
229+
{
230+
std::unique_lock<std::mutex> lk(*quitmutex);
231+
//The timestamps on the GPS events are in seconds, but based on logging the data actually changes faster with the same timestamp
232+
if (quitcv->wait_for(lk, std::chrono::milliseconds(250)) == std::cv_status::no_timeout)
233+
{
234+
break;
235+
}
236+
}
237+
}
238+
239+
mzd_gps2_set_enabled(false);
240+
logd("Exiting");
241+
242+
}

mazda/gps/mzd_gps.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdint.h>
1+
#include <cstdint>
22

33
struct GPSData
44
{
@@ -22,3 +22,6 @@ void mzd_gps2_set_enabled(bool bEnabled);
2222

2323
void mzd_gps2_stop();
2424

25+
void gps_thread_func(std::condition_variable *quitcv, std::mutex *quitmutex, DBus::Connection *serviceBus, std::atomic<bool> *exiting);
26+
27+

mazda/main.cpp

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242

4343
#define HMI_BUS_ADDRESS "unix:path=/tmp/dbus_hmi_socket"
4444
#define SERVICE_BUS_ADDRESS "unix:path=/tmp/dbus_service_socket"
45-
// Check the content folder. sd_nav still exists without the card installed
46-
#define SD_CARD_PATH "/tmp/mnt/sd_nav/content"
45+
4746

4847
__asm__(".symver realpath1,realpath1@GLIBC_2.11.1");
4948

@@ -94,91 +93,7 @@ static void nightmode_thread_func(std::condition_variable &quitcv, std::mutex &q
9493
}
9594
}
9695

97-
static void gps_thread_func(std::condition_variable& quitcv, std::mutex& quitmutex, DBus::Connection& serviceBus)
98-
{
99-
GPSData data, newData;
100-
uint64_t oldTs = 0;
101-
int debugLogCount = 0;
102-
mzd_gps2_start(serviceBus);
103-
104-
//Not sure if this is actually required but the built-in Nav code on CMU does it
105-
mzd_gps2_set_enabled(true);
106-
107-
config::readConfig();
108-
while (!exiting)
109-
{
110-
logd("Getting GPS Data");
111-
if (config::carGPS && mzd_gps2_get(newData) && !data.IsSame(newData))
112-
{
113-
data = newData;
114-
timeval tv;
115-
gettimeofday(&tv, nullptr);
116-
uint64_t timestamp = tv.tv_sec * 1000000 + tv.tv_usec;
117-
if (debugLogCount < 50) //only print the first 50 to avoid spamming the log and breaking the opera text box
118-
{
119-
logd("GPS data: %d %d %f %f %d %f %f %f %f \n",data.positionAccuracy, data.uTCtime, data.latitude, data.longitude, data.altitude, data.heading, data.velocity, data.horizontalAccuracy, data.verticalAccuracy);
120-
logd("Delta %f\n", (timestamp - oldTs)/1000000.0);
121-
debugLogCount++;
122-
}
123-
oldTs = timestamp;
124-
125-
g_hu->hu_queue_command([data, timestamp](IHUConnectionThreadInterface& s)
126-
{
127-
HU::SensorEvent sensorEvent;
128-
HU::SensorEvent::LocationData* location = sensorEvent.add_location_data();
129-
//AA uses uS and the gps data just has seconds, just use the current time to get more precision so AA can
130-
//interpolate better
131-
location->set_timestamp(timestamp);
132-
location->set_latitude(static_cast<int32_t>(data.latitude * 1E7));
133-
location->set_longitude(static_cast<int32_t>(data.longitude * 1E7));
134-
135-
// If the sd card exists then reverse heading. This should only be used on installs that have the
136-
// reversed heading issue.
137-
double newHeading = data.heading;
138-
139-
if (config::reverseGPS)
140-
{
141-
const char* sdCardFolder;
142-
sdCardFolder = SD_CARD_PATH;
143-
struct stat sb;
144-
145-
if (stat(sdCardFolder, &sb) == 0 && S_ISDIR(sb.st_mode))
146-
{
147-
newHeading = data.heading + 180;
148-
if (newHeading >= 360)
149-
{
150-
newHeading = newHeading - 360;
151-
}
152-
}
153-
}
154-
155-
location->set_bearing(static_cast<int32_t>(newHeading * 1E6));
156-
//assuming these are the same units as the Android Location API (the rest are)
157-
double velocityMetersPerSecond = data.velocity * 0.277778; //convert km/h to m/s
158-
location->set_speed(static_cast<int32_t>(velocityMetersPerSecond * 1E3));
15996

160-
location->set_altitude(static_cast<int32_t>(data.altitude * 1E2));
161-
location->set_accuracy(static_cast<int32_t>(data.horizontalAccuracy * 1E3));
162-
163-
s.hu_aap_enc_send_message(0, AA_CH_SEN, HU_SENSOR_CHANNEL_MESSAGE::SensorEvent, sensorEvent);
164-
});
165-
}
166-
logd("Done Getting GPS Data");
167-
168-
{
169-
std::unique_lock<std::mutex> lk(quitmutex);
170-
//The timestamps on the GPS events are in seconds, but based on logging the data actually changes faster with the same timestamp
171-
if (quitcv.wait_for(lk, std::chrono::milliseconds(250)) == std::cv_status::no_timeout)
172-
{
173-
break;
174-
}
175-
}
176-
}
177-
178-
mzd_gps2_set_enabled(false);
179-
logd("Exiting");
180-
181-
}
18297

18398
void shutdown(int signum){
18499
exiting = true;
@@ -241,7 +156,7 @@ int run(int mode, DBus::Connection& serviceBus, DBus::Connection& hmiBus){
241156
callbacks.connected = true;
242157

243158
std::thread nm_thread([&quitcv, &quitmutex](){ nightmode_thread_func(quitcv, quitmutex); } );
244-
std::thread gp_thread([&quitcv, &quitmutex, &serviceBus](){ gps_thread_func(quitcv, quitmutex, serviceBus); } );
159+
std::thread gp_thread(gps_thread_func, &quitcv, &quitmutex, &serviceBus, &exiting);
245160
std::thread *hud_thread = nullptr;
246161
if(hud_installed()){
247162
hud_thread = new std::thread([&quitcv, &quitmutex, &hudmutex](){ hud_thread_func(quitcv, quitmutex, hudmutex, exiting); } );

0 commit comments

Comments
 (0)