| 1 |
/******************************************************************************* |
| 2 |
Copyright(c) 2017 Jasem Mutlaq. All rights reserved. |
| 3 |
|
| 4 |
INDI GPS NMEA Driver |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it |
| 7 |
under the terms of the GNU General Public License as published by the Free |
| 8 |
Software Foundation; either version 2 of the License, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 |
more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public License |
| 17 |
along with this library; see the file COPYING.LIB. If not, write to |
| 18 |
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 |
Boston, MA 02110-1301, USA. |
| 20 |
|
| 21 |
The full GNU General Public License is included in this distribution in the |
| 22 |
file called LICENSE. |
| 23 |
*******************************************************************************/ |
| 24 |
|
| 25 |
#include "gpsnmea_driver.h" |
| 26 |
#include "minmea.h" |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
|
| 30 |
#include <connectionplugins/connectiontcp.h> |
| 31 |
#include <indicom.h> |
| 32 |
#include <libnova/julian_day.h> |
| 33 |
#include <libnova/sidereal_time.h> |
| 34 |
|
| 35 |
#include <memory> |
| 36 |
#include <unistd.h> |
| 37 |
#include <errno.h> |
| 38 |
#include <time.h> |
| 39 |
#include <string.h> |
| 40 |
#include <pthread.h> |
| 41 |
|
| 42 |
#define MAX_NMEA_PARSES 50 // Read 50 streams before giving up |
| 43 |
#define MAX_TIMEOUT_COUNT 5 // Maximum timeout before auto-connect |
| 44 |
|
| 45 |
// We declare an auto pointer to GPSD. |
| 46 |
static std::unique_ptr<GPSNMEA> gpsnema(new GPSNMEA()); |
| 47 |
|
| 48 |
GPSNMEA::GPSNMEA() |
| 49 |
{ |
| 50 |
setVersion(GPSNMEA_VERSION_MAJOR, GPSNMEA_VERSION_MINOR); |
| 51 |
} |
| 52 |
|
| 53 |
const char *GPSNMEA::getDefaultName() |
| 54 |
{ |
| 55 |
return "GPS NMEA"; |
| 56 |
} |
| 57 |
|
| 58 |
bool GPSNMEA::initProperties() |
| 59 |
{ |
| 60 |
// We init parent properties first |
| 61 |
INDI::GPS::initProperties(); |
| 62 |
|
| 63 |
IUFillText(&GPSstatusT[0], "GPS_FIX", "Fix Mode", nullptr); |
| 64 |
IUFillTextVector(&GPSstatusTP, GPSstatusT, 1, getDeviceName(), "GPS_STATUS", "GPS Status", MAIN_CONTROL_TAB, IP_RO, |
| 65 |
60, IPS_IDLE); |
| 66 |
|
| 67 |
tcpConnection = new Connection::TCP(this); |
| 68 |
tcpConnection->setDefaultHost("192.168.1.1"); |
| 69 |
tcpConnection->setDefaultPort(50000); |
| 70 |
tcpConnection->registerHandshake([&]() |
| 71 |
{ |
| 72 |
PortFD = tcpConnection->getPortFD(); |
| 73 |
return isNMEA(); |
| 74 |
}); |
| 75 |
|
| 76 |
registerConnection(tcpConnection); |
| 77 |
|
| 78 |
addDebugControl(); |
| 79 |
|
| 80 |
setDriverInterface(GPS_INTERFACE | AUX_INTERFACE); |
| 81 |
|
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
bool GPSNMEA::updateProperties() |
| 86 |
{ |
| 87 |
// Call parent update properties first |
| 88 |
INDI::GPS::updateProperties(); |
| 89 |
|
| 90 |
if (isConnected()) |
| 91 |
{ |
| 92 |
defineProperty(&GPSstatusTP); |
| 93 |
|
| 94 |
pthread_create(&nmeaThread, nullptr, &GPSNMEA::parseNMEAHelper, this); |
| 95 |
} |
| 96 |
else |
| 97 |
{ |
| 98 |
// We're disconnected |
| 99 |
deleteProperty(GPSstatusTP.name); |
| 100 |
} |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
IPState GPSNMEA::updateGPS() |
| 105 |
{ |
| 106 |
IPState rc = IPS_BUSY; |
| 107 |
|
| 108 |
pthread_mutex_lock(&lock); |
| 109 |
if (locationPending == false && timePending == false) |
| 110 |
{ |
| 111 |
rc = IPS_OK; |
| 112 |
locationPending = true; |
| 113 |
timePending = true; |
| 114 |
} |
| 115 |
pthread_mutex_unlock(&lock); |
| 116 |
|
| 117 |
return rc; |
| 118 |
} |
| 119 |
|
| 120 |
bool GPSNMEA::isNMEA() |
| 121 |
{ |
| 122 |
char line[MINMEA_MAX_LENGTH]; |
| 123 |
|
| 124 |
int bytes_read = 0; |
| 125 |
int tty_rc = tty_nread_section(PortFD, line, MINMEA_MAX_LENGTH, 0xA, 3, &bytes_read); |
| 126 |
if (tty_rc < 0) |
| 127 |
{ |
| 128 |
LOGF_ERROR("Error getting device readings: %s", strerror(errno)); |
| 129 |
return false; |
| 130 |
} |
| 131 |
line[bytes_read] = '\0'; |
| 132 |
|
| 133 |
return (minmea_sentence_id(line, false) != MINMEA_INVALID); |
| 134 |
} |
| 135 |
|
| 136 |
bool GPSNMEA::setSystemTime(time_t& raw_time) |
| 137 |
{ |
| 138 |
#ifdef __linux__ |
| 139 |
#if defined(__GNU_LIBRARY__) |
| 140 |
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ > 30) |
| 141 |
timespec sTime = {}; |
| 142 |
sTime.tv_sec = raw_time; |
| 143 |
clock_settime(CLOCK_REALTIME, &sTime); |
| 144 |
#else |
| 145 |
stime(&raw_time); |
| 146 |
#endif |
| 147 |
#else |
| 148 |
stime(&raw_time); |
| 149 |
#endif |
| 150 |
return true; |
| 151 |
#else |
| 152 |
(void)raw_time; |
| 153 |
return false; |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
void* GPSNMEA::parseNMEAHelper(void *obj) |
| 158 |
{ |
| 159 |
static_cast<GPSNMEA*>(obj)->parseNEMA(); |
| 160 |
return nullptr; |
| 161 |
} |
| 162 |
|
| 163 |
void GPSNMEA::parseNEMA() |
| 164 |
{ |
| 165 |
static char ts[32] = {0}; |
| 166 |
|
| 167 |
char line[MINMEA_MAX_LENGTH]; |
| 168 |
|
| 169 |
while (isConnected()) |
| 170 |
{ |
| 171 |
int bytes_read = 0; |
| 172 |
int tty_rc = tty_nread_section(PortFD, line, MINMEA_MAX_LENGTH, 0xA, 3, &bytes_read); |
| 173 |
if (tty_rc < 0) |
| 174 |
{ |
| 175 |
if (tty_rc == TTY_OVERFLOW) |
| 176 |
{ |
| 177 |
LOG_WARN("Overflow detected. Possible remote GPS disconnection. Disconnecting driver..."); |
| 178 |
INDI::GPS::setConnected(false); |
| 179 |
updateProperties(); |
| 180 |
break; |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
char errmsg[MAXRBUF]; |
| 185 |
tty_error_msg(tty_rc, errmsg, MAXRBUF); |
| 186 |
if (tty_rc == TTY_TIME_OUT || errno == ECONNREFUSED) |
| 187 |
{ |
| 188 |
if (errno == ECONNREFUSED) |
| 189 |
{ |
| 190 |
// sleep for 10 seconds |
| 191 |
tcpConnection->Disconnect(); |
| 192 |
usleep(10 * 1e6); |
| 193 |
tcpConnection->Connect(); |
| 194 |
PortFD = tcpConnection->getPortFD(); |
| 195 |
} |
| 196 |
else if (timeoutCounter++ > MAX_TIMEOUT_COUNT) |
| 197 |
{ |
| 198 |
LOG_WARN("Timeout limit reached, reconnecting..."); |
| 199 |
|
| 200 |
tcpConnection->Disconnect(); |
| 201 |
// sleep for 5 seconds |
| 202 |
usleep(5 * 1e6); |
| 203 |
tcpConnection->Connect(); |
| 204 |
PortFD = tcpConnection->getPortFD(); |
| 205 |
timeoutCounter = 0; |
| 206 |
} |
| 207 |
} |
| 208 |
continue; |
| 209 |
} |
| 210 |
} |
| 211 |
line[bytes_read] = '\0'; |
| 212 |
|
| 213 |
LOGF_DEBUG("%s", line); |
| 214 |
switch (minmea_sentence_id(line, false)) |
| 215 |
{ |
| 216 |
case MINMEA_SENTENCE_RMC: |
| 217 |
{ |
| 218 |
struct minmea_sentence_rmc frame; |
| 219 |
if (minmea_parse_rmc(&frame, line)) |
| 220 |
{ |
| 221 |
if (frame.valid) |
| 222 |
{ |
| 223 |
LocationN[LOCATION_LATITUDE].value = minmea_tocoord(&frame.latitude); |
| 224 |
LocationN[LOCATION_LONGITUDE].value = minmea_tocoord(&frame.longitude); |
| 225 |
if (LocationN[LOCATION_LONGITUDE].value < 0) |
| 226 |
LocationN[LOCATION_LONGITUDE].value += 360; |
| 227 |
|
| 228 |
struct timespec timesp; |
| 229 |
time_t raw_time; |
| 230 |
struct tm *utc, *local; |
| 231 |
|
| 232 |
if (minmea_gettime(×p, &frame.date, &frame.time) == -1) |
| 233 |
break; |
| 234 |
|
| 235 |
raw_time = timesp.tv_sec; |
| 236 |
utc = gmtime(&raw_time); |
| 237 |
strftime(ts, 32, "%Y-%m-%dT%H:%M:%S", utc); |
| 238 |
IUSaveText(&TimeT[0], ts); |
| 239 |
|
| 240 |
setSystemTime(raw_time); |
| 241 |
|
| 242 |
local = localtime(&raw_time); |
| 243 |
snprintf(ts, 32, "%4.2f", (local->tm_gmtoff / 3600.0)); |
| 244 |
IUSaveText(&TimeT[1], ts); |
| 245 |
|
| 246 |
pthread_mutex_lock(&lock); |
| 247 |
locationPending = false; |
| 248 |
timePending = false; |
| 249 |
LOG_DEBUG("Threaded Location and Time updates complete."); |
| 250 |
pthread_mutex_unlock(&lock); |
| 251 |
} |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
LOG_DEBUG("$xxRMC sentence is not parsed"); |
| 256 |
} |
| 257 |
} |
| 258 |
break; |
| 259 |
|
| 260 |
case MINMEA_SENTENCE_GGA: |
| 261 |
{ |
| 262 |
struct minmea_sentence_gga frame; |
| 263 |
if (minmea_parse_gga(&frame, line)) |
| 264 |
{ |
| 265 |
if (frame.fix_quality == 1) |
| 266 |
{ |
| 267 |
LocationN[LOCATION_LATITUDE].value = minmea_tocoord(&frame.latitude); |
| 268 |
LocationN[LOCATION_LONGITUDE].value = minmea_tocoord(&frame.longitude); |
| 269 |
if (LocationN[LOCATION_LONGITUDE].value < 0) |
| 270 |
LocationN[LOCATION_LONGITUDE].value += 360; |
| 271 |
|
| 272 |
LocationN[LOCATION_ELEVATION].value = minmea_tofloat(&frame.altitude); |
| 273 |
|
| 274 |
struct timespec timesp; |
| 275 |
time_t raw_time; |
| 276 |
struct tm *utc, *local; |
| 277 |
minmea_date gmt_date; |
| 278 |
|
| 279 |
time(&raw_time); |
| 280 |
utc = gmtime(&raw_time); |
| 281 |
gmt_date.day = utc->tm_mday; |
| 282 |
gmt_date.month = utc->tm_mon + 1; |
| 283 |
gmt_date.year = utc->tm_year; |
| 284 |
|
| 285 |
minmea_gettime(×p, &gmt_date, &frame.time); |
| 286 |
|
| 287 |
raw_time = timesp.tv_sec; |
| 288 |
utc = gmtime(&raw_time); |
| 289 |
strftime(ts, 32, "%Y-%m-%dT%H:%M:%S", utc); |
| 290 |
IUSaveText(&TimeT[0], ts); |
| 291 |
setSystemTime(raw_time); |
| 292 |
|
| 293 |
local = localtime(&raw_time); |
| 294 |
snprintf(ts, 32, "%4.2f", (local->tm_gmtoff / 3600.0)); |
| 295 |
IUSaveText(&TimeT[1], ts); |
| 296 |
|
| 297 |
pthread_mutex_lock(&lock); |
| 298 |
timePending = false; |
| 299 |
locationPending = false; |
| 300 |
LOG_DEBUG("Threaded Location and Time updates complete."); |
| 301 |
pthread_mutex_unlock(&lock); |
| 302 |
} |
| 303 |
} |
| 304 |
else |
| 305 |
{ |
| 306 |
LOG_DEBUG("$xxGGA sentence is not parsed"); |
| 307 |
} |
| 308 |
} |
| 309 |
break; |
| 310 |
|
| 311 |
case MINMEA_SENTENCE_GSA: |
| 312 |
{ |
| 313 |
struct minmea_sentence_gsa frame; |
| 314 |
if (minmea_parse_gsa(&frame, line)) |
| 315 |
{ |
| 316 |
if (frame.fix_type == 1) |
| 317 |
{ |
| 318 |
GPSstatusTP.s = IPS_BUSY; |
| 319 |
IUSaveText(&GPSstatusT[0], "NO FIX"); |
| 320 |
} |
| 321 |
else if (frame.fix_type == 2) |
| 322 |
{ |
| 323 |
GPSstatusTP.s = IPS_OK; |
| 324 |
IUSaveText(&GPSstatusT[0], "2D FIX"); |
| 325 |
} |
| 326 |
else if (frame.fix_type == 3) |
| 327 |
{ |
| 328 |
GPSstatusTP.s = IPS_OK; |
| 329 |
IUSaveText(&GPSstatusT[0], "3D FIX"); |
| 330 |
} |
| 331 |
IDSetText(&GPSstatusTP, nullptr); |
| 332 |
|
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
LOG_DEBUG("$xxGSA sentence is not parsed."); |
| 337 |
} |
| 338 |
} |
| 339 |
break; |
| 340 |
case MINMEA_SENTENCE_ZDA: |
| 341 |
{ |
| 342 |
struct minmea_sentence_zda frame; |
| 343 |
if (minmea_parse_zda(&frame, line)) |
| 344 |
{ |
| 345 |
LOGF_DEBUG("$xxZDA: %d:%d:%d %02d.%02d.%d UTC%+03d:%02d", |
| 346 |
frame.time.hours, |
| 347 |
frame.time.minutes, |
| 348 |
frame.time.seconds, |
| 349 |
frame.date.day, |
| 350 |
frame.date.month, |
| 351 |
frame.date.year, |
| 352 |
frame.hour_offset, |
| 353 |
frame.minute_offset); |
| 354 |
|
| 355 |
struct timespec timesp; |
| 356 |
time_t raw_time; |
| 357 |
struct tm *utc, *local; |
| 358 |
|
| 359 |
minmea_gettime(×p, &frame.date, &frame.time); |
| 360 |
|
| 361 |
raw_time = timesp.tv_sec; |
| 362 |
utc = gmtime(&raw_time); |
| 363 |
strftime(ts, 32, "%Y-%m-%dT%H:%M:%S", utc); |
| 364 |
IUSaveText(&TimeT[0], ts); |
| 365 |
setSystemTime(raw_time); |
| 366 |
|
| 367 |
local = localtime(&raw_time); |
| 368 |
snprintf(ts, 32, "%4.2f", (local->tm_gmtoff / 3600.0)); |
| 369 |
IUSaveText(&TimeT[1], ts); |
| 370 |
|
| 371 |
pthread_mutex_lock(&lock); |
| 372 |
timePending = false; |
| 373 |
LOG_DEBUG("Threaded Time update complete."); |
| 374 |
pthread_mutex_unlock(&lock); |
| 375 |
} |
| 376 |
else |
| 377 |
{ |
| 378 |
LOG_DEBUG("$xxZDA sentence is not parsed"); |
| 379 |
} |
| 380 |
} |
| 381 |
break; |
| 382 |
|
| 383 |
case MINMEA_INVALID: |
| 384 |
{ |
| 385 |
//LOG_WARN("$xxxxx sentence is not valid"); |
| 386 |
} break; |
| 387 |
|
| 388 |
default: |
| 389 |
{ |
| 390 |
LOG_DEBUG("$xxxxx sentence is not parsed"); |
| 391 |
} |
| 392 |
break; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
pthread_exit(nullptr); |
| 397 |
} |