|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 by Jonathan Naylor G4KLX |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "MobileGPS.h" |
| 20 | + |
| 21 | +#include <cstdio> |
| 22 | +#include <cassert> |
| 23 | +#include <cstring> |
| 24 | +#include <cmath> |
| 25 | + |
| 26 | +CMobileGPS::CMobileGPS(const std::string& address, unsigned int port, CDMRNetwork* network) : |
| 27 | +m_idTimer(1000U, 60U), |
| 28 | +m_address(), |
| 29 | +m_port(port), |
| 30 | +m_socket(), |
| 31 | +m_network(network) |
| 32 | +{ |
| 33 | + assert(!address.empty()); |
| 34 | + assert(port > 0U); |
| 35 | + assert(network != NULL); |
| 36 | + |
| 37 | + m_address = CUDPSocket::lookup(address); |
| 38 | +} |
| 39 | + |
| 40 | +CMobileGPS::~CMobileGPS() |
| 41 | +{ |
| 42 | +} |
| 43 | + |
| 44 | +bool CMobileGPS::open() |
| 45 | +{ |
| 46 | + bool ret = m_socket.open(); |
| 47 | + if (!ret) |
| 48 | + return false; |
| 49 | + |
| 50 | + m_idTimer.start(); |
| 51 | + |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +void CMobileGPS::clock(unsigned int ms) |
| 56 | +{ |
| 57 | + m_idTimer.clock(ms); |
| 58 | + |
| 59 | + if (m_idTimer.hasExpired()) { |
| 60 | + pollGPS(); |
| 61 | + m_idTimer.start(); |
| 62 | + } |
| 63 | + |
| 64 | + sendReport(); |
| 65 | +} |
| 66 | + |
| 67 | +void CMobileGPS::close() |
| 68 | +{ |
| 69 | + m_socket.close(); |
| 70 | +} |
| 71 | + |
| 72 | +bool CMobileGPS::pollGPS() |
| 73 | +{ |
| 74 | + return m_socket.write((unsigned char*)"MMDVMHost", 9U, m_address, m_port); |
| 75 | +} |
| 76 | + |
| 77 | +void CMobileGPS::sendReport() |
| 78 | +{ |
| 79 | + // Grab GPS data if it's available |
| 80 | + unsigned char buffer[200U]; |
| 81 | + in_addr address; |
| 82 | + unsigned int port; |
| 83 | + int ret = m_socket.read(buffer, 200U, address, port); |
| 84 | + if (ret <= 0) |
| 85 | + return; |
| 86 | + |
| 87 | + buffer[ret] = '\0'; |
| 88 | + |
| 89 | + // Parse the GPS data |
| 90 | + char* pLatitude = ::strtok((char*)buffer, ",\n"); // Latitude |
| 91 | + char* pLongitude = ::strtok(NULL, ",\n"); // Longitude |
| 92 | + |
| 93 | + if (pLatitude == NULL || pLongitude == NULL) |
| 94 | + return; |
| 95 | + |
| 96 | + float latitude = ::atof(pLatitude); |
| 97 | + float longitude = ::atof(pLongitude); |
| 98 | + |
| 99 | + m_network->writeHomePosition(latitude, longitude); |
| 100 | +} |
| 101 | + |
0 commit comments