"Initial commit to Gerrit"
[profile/ivi/gpsd.git] / net_gnss_dispatch.c
1 /* net_gnss_dispatch.c -- common interface to a number of Network GNSS services
2  *
3  * This file is Copyright (c) 2010 by the GPSD project
4  * BSD terms apply: see the file COPYING in the distribution root for details.
5  */
6
7 #include <stdlib.h>
8 #include "gpsd_config.h"
9 #include <sys/types.h>
10 #ifndef S_SPLINT_S
11 #ifdef HAVE_SYS_SOCKET_H
12 #include <sys/socket.h>
13 #endif
14 #include <unistd.h>
15 #endif /* S_SPLINT_S */
16 #include <string.h>
17 #include <errno.h>
18
19 #include "gpsd.h"
20
21 #define NETGNSS_DGPSIP  "dgpsip://"
22 #define NETGNSS_NTRIP   "ntrip://"
23
24 /* Where to find the list of DGPSIP correction servers, if there is one */
25 #define DGPSIP_SERVER_LIST      "/usr/share/gpsd/dgpsip-servers"
26
27 bool netgnss_uri_check(char *name)
28 /* is given string a valid URI for GNSS/DGPS service? */
29 {
30     return
31         strncmp(name, NETGNSS_NTRIP, strlen(NETGNSS_NTRIP)) == 0
32         || strncmp(name, NETGNSS_DGPSIP, strlen(NETGNSS_DGPSIP)) == 0;
33 }
34
35
36 /*@ -branchstate */
37 int netgnss_uri_open(struct gps_context_t *context, char *netgnss_service)
38 /* open a connection to a DGNSS service */
39 {
40 #ifdef NTRIP_ENABLE
41     if (strncmp(netgnss_service, NETGNSS_NTRIP, strlen(NETGNSS_NTRIP)) == 0)
42         return ntrip_open(context, netgnss_service + strlen(NETGNSS_NTRIP));
43 #endif
44
45     if (strncmp(netgnss_service, NETGNSS_DGPSIP, strlen(NETGNSS_DGPSIP)) == 0)
46         return dgpsip_open(context, netgnss_service + strlen(NETGNSS_DGPSIP));
47
48 #ifndef REQUIRE_DGNSS_PROTO
49     return dgpsip_open(context, netgnss_service);
50 #else
51     gpsd_report(LOG_ERROR,
52                 "Unknown or unspecified DGNSS protocol for service %s\n",
53                 netgnss_service);
54     return -1;
55 #endif
56 }
57
58 /*@ +branchstate */
59
60 int netgnss_poll(struct gps_context_t *context)
61 /* poll the DGNSS service for a correction report */
62 {
63     if (context->dsock > -1) {
64         ssize_t rtcmbytes =
65             read(context->dsock, context->rtcmbuf, sizeof(context->rtcmbuf));
66         if ((rtcmbytes == -1 && errno != EAGAIN) || (rtcmbytes == 0)) {
67             (void)shutdown(context->dsock, SHUT_RDWR);
68             (void)close(context->dsock);
69             context->rtcmbytes = 0;
70             return -1;
71         } else {
72             context->rtcmbytes = (size_t) rtcmbytes;
73             context->rtcmtime = timestamp();
74         }
75     }
76     return 0;
77 }
78
79 void netgnss_report(struct gps_device_t *session)
80 /* may be time to ship a usage report to the DGNSS service */
81 {
82     if (session->context->netgnss_service == netgnss_dgpsip)
83         dgpsip_report(session);
84 #ifdef NTRIP_ENABLE
85     else if (session->context->netgnss_service == netgnss_ntrip)
86         ntrip_report(session);
87 #endif
88 }
89
90 void netgnss_autoconnect(struct gps_context_t *context, double lat,
91                          double lon)
92 {
93     if (context->netgnss_service == netgnss_dgpsip) {
94         dgpsip_autoconnect(context, lat, lon, DGPSIP_SERVER_LIST);
95     }
96 }
97
98 /* *INDENT-OFF* */
99 void rtcm_relay(struct gps_device_t *session)
100 /* pass a DGNSS connection report to a session */
101 {
102     if (session->gpsdata.gps_fd != -1
103         && session->context->rtcmbytes > 0
104         && session->rtcmtime < session->context->rtcmtime
105         && session->device_type->rtcm_writer != NULL) {
106         if (session->device_type->rtcm_writer(session,
107                                               session->context->rtcmbuf,
108                                               session->context->rtcmbytes) ==
109             0)
110             gpsd_report(LOG_ERROR, "Write to RTCM sink failed\n");
111         else {
112             session->rtcmtime = timestamp();
113             gpsd_report(LOG_IO, "<= DGPS: %zd bytes of RTCM relayed.\n",
114                         session->context->rtcmbytes);
115         }
116     }
117 }
118 /* *INDENT-ON* */
119
120 /* end */