Devuan fork of gpsd
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

89 lines
2.7 KiB

  1. /* net_dgpsip.c -- gather and dispatch DGPS data from DGPSIP servers
  2. *
  3. * This file is Copyright (c) 2010-2018 by the GPSD project
  4. * SPDX-License-Identifier: BSD-2-clause
  5. */
  6. #include "gpsd_config.h" /* must be before all includes */
  7. #include <sys/types.h>
  8. #include <stdio.h>
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <fcntl.h>
  13. #include <netdb.h>
  14. #include <sys/socket.h>
  15. #include <unistd.h>
  16. #include "gpsd.h"
  17. int dgpsip_open(struct gps_device_t *device, const char *dgpsserver)
  18. /* open a connection to a DGPSIP server */
  19. {
  20. char *colon, *dgpsport = "rtcm-sc104";
  21. int opts;
  22. device->dgpsip.reported = false;
  23. if ((colon = strchr(dgpsserver, ':')) != NULL) {
  24. dgpsport = colon + 1;
  25. *colon = '\0';
  26. }
  27. if (!getservbyname(dgpsport, "tcp"))
  28. dgpsport = DEFAULT_RTCM_PORT;
  29. device->gpsdata.gps_fd =
  30. netlib_connectsock(AF_UNSPEC, dgpsserver, dgpsport, "tcp");
  31. // cppcheck-suppress pointerPositive
  32. if (device->gpsdata.gps_fd >= 0) {
  33. char hn[256], buf[BUFSIZ];
  34. GPSD_LOG(LOG_PROG, &device->context->errout,
  35. "connection to DGPS server %s established.\n",
  36. dgpsserver);
  37. (void)gethostname(hn, sizeof(hn));
  38. /* greeting required by some RTCM104 servers; others will ignore it */
  39. (void)snprintf(buf, sizeof(buf), "HELO %s gpsd %s\r\nR\r\n", hn,
  40. VERSION);
  41. if (write(device->gpsdata.gps_fd, buf, strlen(buf)) != (ssize_t) strlen(buf))
  42. GPSD_LOG(LOG_ERROR, &device->context->errout,
  43. "hello to DGPS server %s failed\n",
  44. dgpsserver);
  45. } else
  46. GPSD_LOG(LOG_ERROR, &device->context->errout,
  47. "can't connect to DGPS server %s, netlib error %d.\n",
  48. dgpsserver, device->gpsdata.gps_fd);
  49. opts = fcntl(device->gpsdata.gps_fd, F_GETFL);
  50. if (opts >= 0)
  51. (void)fcntl(device->gpsdata.gps_fd, F_SETFL, opts | O_NONBLOCK);
  52. device->servicetype = service_dgpsip;
  53. return device->gpsdata.gps_fd;
  54. }
  55. void dgpsip_report(struct gps_context_t *context,
  56. struct gps_device_t *gps,
  57. struct gps_device_t *dgpsip)
  58. /* may be time to ship a usage report to the DGPSIP server */
  59. {
  60. /*
  61. * 10 is an arbitrary number, the point is to have gotten several good
  62. * fixes before reporting usage to our DGPSIP server.
  63. */
  64. if (context->fixcnt > 10 && !dgpsip->dgpsip.reported) {
  65. dgpsip->dgpsip.reported = true;
  66. if (dgpsip->gpsdata.gps_fd > -1) {
  67. char buf[BUFSIZ];
  68. (void)snprintf(buf, sizeof(buf), "R %0.8f %0.8f %0.2f\r\n",
  69. gps->gpsdata.fix.latitude,
  70. gps->gpsdata.fix.longitude,
  71. gps->gpsdata.fix.altMSL);
  72. if (write(dgpsip->gpsdata.gps_fd, buf, strlen(buf)) ==
  73. (ssize_t) strlen(buf))
  74. GPSD_LOG(LOG_IO, &context->errout, "=> dgps %s\n", buf);
  75. else
  76. GPSD_LOG(LOG_IO, &context->errout, "write to dgps FAILED\n");
  77. }
  78. }
  79. }