ntrip-client 0.2.1

NTRIP client
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! NTRIP Client implementation

use std::sync::Arc;

use base64::{engine::general_purpose, Engine as _};
use futures::Stream;
use http::{header::USER_AGENT, HeaderMap, HeaderValue, Method};
use rtcm_rs::{Message, MessageFrame};
use rustls::pki_types::ServerName;
use tokio::{
    io::{AsyncRead, AsyncReadExt as _, AsyncWrite, AsyncWriteExt},
    net::TcpStream,
    select,
    sync::{
        broadcast::Sender as BroadcastSender,
        mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
    },
    task::JoinHandle,
};
use tokio_rustls::TlsConnector;
use tracing::{debug, error, trace, warn};

use crate::{
    config::{NtripConfig, NtripCredentials},
    snip::ServerInfo,
    NtripClientError,
};

/// NTRIP Client, used to connect to an NTRIP (RTCM) service.
/// When "mounted", the [NtripHandle] allows real-time messaging
/// through a [Stream] channel.
///
/// ```
/// use tokio::select;
/// use tokio::sync; // broadcast channel
/// use futures::StreamExt; // real-time channel
///
/// use ntrip_client::{
///     NtripClient,
///     NtripConfig,
///     RtcmProvider,
///     NtripCredentials,
///     NtripClientError,
/// };
///
/// async fn basic_listener() -> Result<(), anyhow::Error> {
///
///     // this network does not require SSL
///     let config = NtripConfig::from_provider(RtcmProvider::Centipede);
///
///     // adapt your credentials to the network
///     let creds = NtripCredentials::default()
///         .with_username("centipede")
///         .with_password("password");
///
///     // client definition
///     let mut client = NtripClient::new(config, creds)
///         .await?;
///
///     // list available mountpoints
///     let mountpoints = client.list_mounts()
///         .await?;
///
///     for remote in mountpoints.services {
///         println!("{} - {}", remote.name, remote.details);
///     }
///
///     // subscribe to remote server
///     let mut handle = client.mount("VALDM").await?;
///
///     // listening
///     loop {
///         select! {
///             message = handle.next() => match message {
///                 Some(msg) => {
///                     println!("received RTCM message: {:?}", msg);
///                 },
///                 None => {
///                     println!("End of stream!");
///                     break;
///                 },
///             },
///         }
///     }
///
///     Ok(())
/// }
///
/// basic_listener();
/// ```
pub struct NtripClient {
    config: NtripConfig,
    creds: NtripCredentials,
}

/// [NtripHandle] is the Mount handle, it implements [Stream]
/// which is how you can receive messages in real-time.
pub struct NtripHandle<RX = UnboundedReceiver<(Message, Vec<u8>)>> {
    _rx_handle: tokio::task::JoinHandle<()>,
    ntrip_rx: RX,
    exit_tx: BroadcastSender<()>,
}

impl NtripClient {
    pub async fn new(
        config: NtripConfig,
        creds: NtripCredentials,
    ) -> Result<Self, NtripClientError> {
        Ok(NtripClient { config, creds })
    }

    /// List available mounts on the NTRIP server
    pub async fn list_mounts(&mut self) -> Result<ServerInfo, NtripClientError> {
        let client = reqwest::Client::builder()
            .http1_ignore_invalid_headers_in_responses(true)
            .http09_responses()
            .user_agent(format!(
                "NTRIP {}/{}",
                env!("CARGO_PKG_NAME"),
                env!("CARGO_PKG_VERSION")
            ))
            .build()?;

        // TODO: auth etc.
        let proto = if self.config.use_tls { "https" } else { "http" };

        let req = client
            .request(
                Method::GET,
                format!("{}://{}:{}", proto, self.config.host, self.config.port),
            )
            .header("Ntrip-Version", "NTRIP/2.0")
            .build()?;

        let res = client.execute(req).await?;

        trace!("Fetched NTRIP response: {:?}", res.status());

        let body = res.text().await?;

        let lines = body.lines().collect::<Vec<&str>>();

        let snip_info = ServerInfo::parse(lines.iter().cloned());

        Ok(snip_info)
    }

    /// 'Mount' the [NtripClient] from remote $url/$mount service point.
    /// On success, you can then start listening to messages from the server.
    ///
    /// ## Input
    /// - mount: readable remote mount point (server name)
    /// - exit_tx: [BroadcastSender] is passed to allow graceful exit on errors
    ///
    /// ## Output
    /// - [NtripHandle] which implements [Stream] to receive messages in real-time.
    pub async fn mount(
        &mut self,
        mount: impl ToString,
    ) -> Result<NtripHandle<UnboundedReceiver<(Message, Vec<u8>)>>, NtripClientError> {
        let (ntrip_tx, ntrip_rx) = unbounded_channel();

        let (_rx_handle, exit_tx) = self.mount_internal(mount, ntrip_tx).await?;

        Ok(NtripHandle {
            _rx_handle: _rx_handle,
            ntrip_rx: ntrip_rx,
            exit_tx: exit_tx,
        })
    }

    /// 'Mount' the [NtripClient] from remote $url/$mount service point.
    /// On success, you can then start listening to messages from the server.
    ///
    /// ## Input
    /// - mount: readable remote mount point (server name)
    /// - exit_tx: [BroadcastSender] is passed to allow graceful exit on errors
    ///
    /// ## Output
    /// - [NtripHandle<()>] which will route received messages throught the provided channel
    pub async fn mount_with_sink(
        &mut self,
        mount: impl ToString,
        ntrip_tx: UnboundedSender<(Message, Vec<u8>)>,
    ) -> Result<NtripHandle<()>, NtripClientError> {
        let (_rx_handle, exit_tx) = self.mount_internal(mount, ntrip_tx).await?;

        Ok(NtripHandle {
            _rx_handle: _rx_handle,
            ntrip_rx: (),
            exit_tx: exit_tx,
        })
    }

    /// 'Mount' the [NtripClient] from remote $url/$mount service point.
    /// On success, you can then start listening to messages from the server.
    ///
    /// ## Input
    /// - mount: readable remote mount point (server name)
    /// - exit_tx: [BroadcastSender] is passed to allow graceful exit on errors
    ///
    /// ## Output
    /// - [NtripHandle] which will route received messages throught the provided channel
    async fn mount_internal(
        &mut self,
        mount: impl ToString,
        ntrip_tx: UnboundedSender<(Message, Vec<u8>)>,
    ) -> Result<(JoinHandle<()>, BroadcastSender<()>), NtripClientError> {
        debug!(
            "Connecting to NTRIP server {}/{}",
            self.config.to_url(),
            mount.to_string()
        );

        let (exit_tx, _exit_rx) = tokio::sync::broadcast::channel(1);

        let sock = TcpStream::connect(&self.config.to_url()).await?;

        let rx_handle = match self.config.use_tls {
            true => {
                debug!("Using TLS connection");

                let mut root_cert_store = rustls::RootCertStore::empty();
                root_cert_store.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());

                let tls_config = rustls::ClientConfig::builder()
                    .with_root_certificates(root_cert_store)
                    .with_no_client_auth();
                let connector = TlsConnector::from(Arc::new(tls_config));
                let dnsname = ServerName::try_from(self.config.host.clone())?;

                let tls_sock = connector.connect(dnsname, sock).await?;

                Self::handle_connection(
                    &self.config,
                    &self.creds,
                    &mount.to_string(),
                    ntrip_tx,
                    exit_tx.clone(),
                    tls_sock,
                )
                .await?
            },
            false => {
                debug!("Using plain TCP connection");

                Self::handle_connection(
                    &self.config,
                    &self.creds,
                    &mount.to_string(),
                    ntrip_tx,
                    exit_tx.clone(),
                    sock,
                )
                .await?
            },
        };

        Ok((rx_handle, exit_tx))
    }

    pub async fn handle_connection(
        config: &NtripConfig,
        creds: &NtripCredentials,
        mount: &str,
        ntrip_tx: UnboundedSender<(Message, Vec<u8>)>,
        exit_tx: BroadcastSender<()>,
        mut sock: impl AsyncRead + AsyncWrite + Unpin + Send + 'static,
    ) -> Result<JoinHandle<()>, NtripClientError> {
        // Setup HTTP headers
        let mut headers = HeaderMap::new();
        headers.append(
            USER_AGENT,
            HeaderValue::from_str(&format!(
                "NTRIP {}/{}",
                env!("CARGO_PKG_NAME"),
                env!("CARGO_PKG_VERSION")
            ))?,
        );

        headers.append("Ntrip-Version", HeaderValue::from_static("NTRIP/2.0"));
        headers.append("Accept", HeaderValue::from_static("*/*"));
        headers.append("Connection", HeaderValue::from_static("close"));

        // If we have credentials, add the Authorization header
        if !creds.user.is_empty() {
            let auth = general_purpose::STANDARD.encode(format!("{}:{}", creds.user, creds.pass));
            headers.append(
                "Authorization",
                HeaderValue::from_str(&format!("Basic {}", auth))?,
            );
        }

        trace!("Headers: {:#?}", headers);

        // Write HTTP request
        trace!("Write HTTP request");
        sock.write_all(format!("GET /{} HTTP/1.0\r\n", mount).as_bytes())
            .await?;
        sock.write_all(format!("Host: {}\r\n", config.to_url()).as_bytes())
            .await?;

        // Write HTTP headers
        trace!("Writing headers");
        for h in headers.iter() {
            sock.write_all(format!("{}: {}\r\n", h.0.as_str(), h.1.to_str()?).as_bytes())
                .await?;
        }

        sock.write_all(b"\r\n").await?;
        sock.flush().await?;

        trace!("Reading response");
        let mut buff = Vec::with_capacity(1024);

        // Perform a first read to get the response status
        let n = sock.read_buf(&mut buff).await?;
        trace!("Read {} bytes, current buffer {} bytes", n, buff.len());

        // Parse out response status
        let r = String::from_utf8_lossy(&buff[..n]);
        match r.lines().next() {
            Some(status) if status.contains("200 OK") => {
                trace!("Got 200 OK response");
            },
            Some(status) => {
                error!("NTRIP server returned error: {}", status);
                return Err(NtripClientError::ResponseError(status.to_string()));
            },
            None => {
                error!("NTRIP server returned empty response");
                return Err(NtripClientError::ResponseError("empty response".into()));
            },
        }

        // Flush buffer until the first RTCM message (0xd3)
        if let Some(i) = buff.iter().enumerate().find(|(_i, b)| **b == 0xd3) {
            trace!(
                "Trimming buffer to next potential frame start at index {}",
                i.0
            );
            let _ = buff.drain(..i.0);
        }

        // Spawn a task to handle incoming NTRIP data

        let mut exit_rx = exit_tx.subscribe();
        let rx_handle = tokio::task::spawn(async move {
            // Track parse errors so we can drop data (or abort) if needed
            let mut error_count = 0;

            'listener: loop {
                select! {
                    n = sock.read_buf(&mut buff) => match n {
                        Ok(n) => {
                            trace!("Read {} bytes, current buffer {} bytes", n, buff.len());
                            trace!("Appended {:02x?}", &buff[buff.len()-n..][..n]);

                            // Handle zero length read (connection closed)
                            if n == 0 {
                                warn!("Zero length response");
                                break 'listener;
                            }

                            // Trim any non-message data from the start of the buffer
                            if buff[0] != 0xd3 {
                                if let Some(i) = buff.iter().enumerate().find(|(_i, b)| **b == 0xd3) {
                                    warn!("Trimming buffer to next potential frame start at index {}", i.0);
                                    buff.drain(..i.0);

                                    assert_eq!(buff[0], 0xd3);
                                }
                            }

                            // While we have enough data for a header,
                            // parse out RTCM messages
                            while buff.len() > 6 {
                                // Attempt to parse frames
                                match MessageFrame::new(&buff[..]) {
                                    Ok(f) => {
                                        // Parse out message from frame
                                        let m = f.get_message();

                                        trace!("Parsed RTCM message: {:?} (consumed {} bytes)", m, f.frame_len());

                                        // Emit message
                                        let raw_data = buff[..f.frame_len()].to_vec();
                                        ntrip_tx.send((m, raw_data)).unwrap();

                                        // Remove parsed data from the buffer
                                        let _ = buff.drain(..f.frame_len());

                                        // Reset error counter
                                        error_count = 0;
                                    },
                                    Err(e) => {
                                        warn!("RTCM parse error: {} (count: {})", e, error_count);

                                        // Update error counter
                                        error_count += 1;

                                        // If we keep getting errors, abort the connection
                                        if error_count >= 5 {
                                            error!("Too many parse errors, closing connection");
                                            break 'listener;
                                        }

                                        break;
                                    }
                                }
                            }
                        },
                        Err(e) => {
                            error!("socket read error: {}", e);
                            break;
                        },
                    },
                    _ = exit_rx.recv() => {
                        error!("Exiting NTRIP read loop on signal");
                        break;
                    }
                }
            }

            warn!("NTRIP read loop exiting");

            if !buff.is_empty() {
                warn!("Dropping {} bytes of unparsed data", buff.len());

                if let Ok(s) = String::from_utf8(buff) {
                    debug!("Unparsed data:\r\n{}", s);
                }
            }
        });

        Ok(rx_handle)
    }
}

impl<RX> NtripHandle<RX> {
    /// Check whether the NTRIP connection is still active (i.e. the read task is still running)
    pub fn is_running(&self) -> bool {
        !self._rx_handle.is_finished()
    }
}

/// [Stream] NTRIP [Message]'s from an [NtripHandle]
impl Stream for NtripHandle<UnboundedReceiver<(Message, Vec<u8>)>> {
    type Item = (Message, Vec<u8>);

    fn poll_next(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Option<Self::Item>> {
        self.ntrip_rx.poll_recv(cx)
    }
}

impl<RX> Drop for NtripHandle<RX> {
    fn drop(&mut self) {
        let _ = self.exit_tx.send(());
    }
}

#[cfg(test)]
mod tests {
    use std::env;

    use futures::StreamExt;
    use rustls::crypto::CryptoProvider;
    use tracing::debug;

    use super::*;
    use crate::config::NtripCredentials;

    fn setup_logging() {
        let _ = tracing_subscriber::FmtSubscriber::builder()
            .compact()
            .without_time()
            .with_max_level(tracing::level_filters::LevelFilter::DEBUG)
            .try_init();
    }

    #[tokio::test]
    #[ignore = "Requires NTRIP config from the environment"]
    async fn test_ntrip_client() {
        setup_logging();

        // Install the default crypto provider
        CryptoProvider::install_default(rustls::crypto::ring::default_provider()).ok();

        debug!("Connecting to NTRIP server");

        let (exit_tx, _exit_rx) = tokio::sync::broadcast::channel(1);

        let mount = env::var("NTRIP_MOUNT").unwrap_or("ARGOACU".to_string());
        let config = env::var("NTRIP_HOST")
            .unwrap_or("rtk2go".to_string())
            .parse::<NtripConfig>()
            .unwrap();
        let creds = NtripCredentials {
            user: env::var("NTRIP_USER").unwrap_or("user".into()),
            pass: env::var("NTRIP_PASS").unwrap_or("pass".into()),
        };

        let mut client = NtripClient::new(config, creds).await.unwrap();

        let mut h = client.mount(mount.to_string()).await.unwrap();

        for _i in 0..10 {
            let (m, d) = h.next().await.unwrap();
            debug!("Got RTCM message: {:?}", m);
            debug!("Raw data: {:02x?}", d);
        }

        let _ = exit_tx.send(());
    }
}