poststation-sdk 0.4.1

An API SDK for poststation
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
use core::fmt::Debug;
use std::{
    error::Error,
    fmt::Display,
    future::Future,
    marker::PhantomData,
    net::{IpAddr, Ipv4Addr, SocketAddr},
    path::{Path, PathBuf},
    sync::Arc,
};

use directories::ProjectDirs;
use postcard_rpc::{
    host_client::{
        HostClient, HostErr, MultiSubRxError, MultiSubscription, SchemaReport, TopicReport, WireRx,
        WireSpawn, WireTx,
    },
    standard_icd::{PingEndpoint, WireError, ERROR_PATH},
    Endpoint, Topic,
};
use poststation_api_icd::postsock::{
    Anchor, DeviceData, Direction, GetDevicesEndpoint, GetLogsEndpoint, GetLogsRangeEndpoint,
    GetSchemasEndpoint, GetTopicsEndpoint, Log, LogRangeRequest, LogRequest, ProxyEndpoint,
    ProxyRequest, ProxyResponse, PublishEndpoint, PublishRequest, PublishResponse,
    StartStreamEndpoint, SubscribeTopic, TopicMsg, TopicRequest, TopicStreamMsg,
    TopicStreamRequest, TopicStreamResult, Uuidv7,
};
use rustls::{
    pki_types::{pem::PemObject, CertificateDer, ServerName},
    RootCertStore,
};
use serde::{de::DeserializeOwned, Serialize};
use tokio::{
    io::{split, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadHalf, WriteHalf},
    net::TcpStream,
};

pub use postcard_schema as schema;
pub use poststation_api_icd as icd;
pub use postcard_dyn::Value;
use tokio_rustls::TlsConnector;

// ---

#[non_exhaustive]
#[derive(Debug)]
pub enum ClientError {
    /// The connection to the server has been closed
    ConnectionClosed,
    /// An issue with the protocol between client and server occurred
    Protocol,
    /// An issue with the encoding of messages occurred
    Encoding,
    /// An issue was reported by the server
    Server(String),
    /// An issue occurred between the remote device and the poststation server
    Remote(String),
    /// An issue occurred with dynamic serialization/deserialization
    Dynamic(String),
}

impl From<HostErr<WireError>> for ClientError {
    fn from(value: HostErr<WireError>) -> Self {
        match value {
            HostErr::Wire(_e) => ClientError::Protocol,
            HostErr::BadResponse => ClientError::Protocol,
            HostErr::Postcard(_error) => ClientError::Encoding,
            HostErr::Closed => ClientError::ConnectionClosed,
        }
    }
}

impl Display for ClientError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        <Self as Debug>::fmt(self, f)
    }
}

impl Error for ClientError {}

#[derive(Clone)]
pub struct PoststationClient {
    client: HostClient<WireError>,
}

impl PoststationClient {
    /// Obtain the raw postcard-rpc client used to talk to the poststation server
    ///
    /// Not recommended for direct usage.
    #[doc(hidden)]
    pub fn raw_client(&self) -> &HostClient<WireError> {
        &self.client
    }

    /// Get devices known by the Poststation server
    ///
    /// Returns an Error if connection to the Poststation server has failed.
    pub async fn get_devices(&self) -> Result<Vec<DeviceData>, ClientError> {
        Ok(self.client.send_resp::<GetDevicesEndpoint>(&()).await?)
    }

    /// Get a schema report of the given device
    ///
    /// * Returns `Ok(Some(report))` if the device exists and the operation succeeded
    /// * Returns `Ok(None)` if the operation succeeded, but the given device was not known to Poststation
    /// * Returns an error if the connection with the server failed
    pub async fn get_device_schemas(
        &self,
        serial: u64,
    ) -> Result<Option<SchemaReport>, ClientError> {
        let res = self.client.send_resp::<GetSchemasEndpoint>(&serial).await?;
        Ok(res)
    }

    /// Obtain the most recent `count` logs from the given device
    ///
    /// * Returns `Ok(Some(logs))` if the device exists and the operation succeeded
    /// * Returns `Ok(None)` if the operation succeeded, but the given device was not known to Poststation
    /// * Returns an error if the connection with the server failed
    pub async fn get_device_logs(
        &self,
        serial: u64,
        count: u32,
    ) -> Result<Option<Vec<Log>>, ClientError> {
        Ok(self
            .client
            .send_resp::<GetLogsEndpoint>(&LogRequest { serial, count })
            .await?)
    }

    /// Obtain the up to `count` logs from the given device
    ///
    /// While [`get_device_logs()`][Self::get_device_logs] gets the NEWEST logs, this method can be used
    /// to obtain logs before or after a given "anchor" point. This is often handy for pagination of results,
    /// allowing for getting N logs before or after a given point.
    ///
    /// [Anchor] can be either a unix millisecond timestamp, or the UUID of a specific log event.
    ///
    /// * Returns `Ok(Some(logs))` if the device exists and the operation succeeded
    /// * Returns `Ok(None)` if the operation succeeded, but the given device was not known to Poststation
    /// * Returns an error if the connection with the server failed
    pub async fn get_device_logs_range(
        &self,
        serial: u64,
        count: u32,
        dir: Direction,
        anchor: Anchor,
    ) -> Result<Option<Vec<Log>>, ClientError> {
        Ok(self
            .client
            .send_resp::<GetLogsRangeEndpoint>(&LogRangeRequest {
                serial,
                count,
                anchor,
                direction: dir,
            })
            .await?)
    }

    /// Get the last `count` topic-out messages for a given `path`
    ///
    /// This function returns the raw, serialized form of messages. You will need to deserialize
    /// the messages using `postcard` and the known type of the message.
    ///
    /// * Returns `Ok(Some(msgs))` if the device exists and the operation succeeded
    /// * Returns `Ok(None)` if the operation succeeded, but the given device was not known to Poststation
    /// * Returns an error if the connection with the server failed
    pub async fn get_device_topics_out_by_path_raw(
        &self,
        serial: u64,
        path: &str,
        count: u32,
    ) -> Result<Option<Vec<TopicMsg>>, ClientError> {
        let schemas = self.get_device_schemas(serial).await?;
        let Some(schemas) = schemas else {
            return Ok(None);
        };

        // find key
        let res = schemas
            .topics_out
            .iter()
            .find(|t| t.path.as_str() == path)
            .map(|t| t.key);
        let Some(key) = res else { return Ok(None) };

        Ok(self
            .client
            .send_resp::<GetTopicsEndpoint>(&TopicRequest {
                serial,
                count,
                path: path.to_string(),
                key,
            })
            .await?)
    }

    /// Get the last `count` topic-out messages for a given `path`
    ///
    /// This function returns the messages in JSON form.
    ///
    /// * Returns `Ok(Some(msgs))` if the device exists and the operation succeeded
    /// * Returns `Ok(None)` if the operation succeeded, but the given device was not known to Poststation
    /// * Returns an error if the connection with the server failed
    pub async fn get_device_topics_out_by_path_json(
        &self,
        serial: u64,
        path: &str,
        count: u32,
    ) -> Result<Option<Vec<(Uuidv7, Value)>>, ClientError> {
        let schemas = self.get_device_schemas(serial).await?;
        let Some(schemas) = schemas else {
            return Ok(None);
        };

        // find key
        let res = schemas.topics_out.iter().find(|t| t.path.as_str() == path);
        let Some(schema) = res else { return Ok(None) };

        let raws = self
            .client
            .send_resp::<GetTopicsEndpoint>(&TopicRequest {
                serial,
                count,
                path: path.to_string(),
                key: schema.key,
            })
            .await?;
        let Some(raws) = raws else {
            return Ok(None);
        };

        let res = raws
            .into_iter()
            .map(|tm| {
                let msg = postcard_dyn::from_slice_dyn(&schema.ty, &tm.msg)
                    .map_err(|_| ClientError::Encoding)?;
                Result::<_, ClientError>::Ok((tm.uuidv7, msg))
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Some(res))
    }

    /// Proxy to an endpoint, using shared types
    ///
    /// This method should be used when the types used by the remote server are known.
    ///
    /// This method is the equivalent of `send_resp` in postcard-rpc. The message will be
    /// sent to poststation, and proxied to the remote device. The response will then be
    /// forwarded back from poststation to the client.
    ///
    /// Returns an error if the connection between the client and poststation failed, or
    /// if the connection between poststation and the remote device failed.
    pub async fn proxy_endpoint<E>(
        &self,
        serial: u64,
        seq_no: u32,
        body: &E::Request,
    ) -> Result<E::Response, ClientError>
    where
        E: Endpoint,
        E::Request: Serialize,
        E::Response: DeserializeOwned,
    {
        let Some(schemas) = self.get_device_schemas(serial).await? else {
            return Err(ClientError::Server("endpoint not found".into()));
        };

        // find key
        // TODO: Don't compare the types because the names don't match even though we've
        // type-punned
        let res = schemas.endpoints.iter().find(|e| {
            e.path.as_str() == E::PATH && e.req_key == E::REQ_KEY && e.resp_key == E::RESP_KEY
        });
        let Some(schema) = res else {
            return Err(ClientError::Server("endpoint not found".into()));
        };
        let Ok(body) = postcard::to_stdvec(body) else {
            return Err(ClientError::Encoding);
        };

        let req = ProxyRequest {
            serial,
            path: schema.path.clone(),
            req_key: schema.req_key,
            resp_key: schema.resp_key,
            seq_no,
            req_body: body,
        };

        let resp = self.client.send_resp::<ProxyEndpoint>(&req).await;

        // client to poststation comms
        let resp = resp?;

        // poststation to remote comms
        let resp = match resp {
            ProxyResponse::Ok { body, .. } => body,
            ProxyResponse::WireErr { body, .. } => {
                return Err(ClientError::Remote(format!("WireErr: {body:?}")))
            }
            ProxyResponse::OtherErr(e) => {
                return Err(ClientError::Remote(format!("Other Server Err: '{e}'")))
            }
        };

        let resp = postcard::from_bytes::<E::Response>(&resp);

        match resp {
            Ok(v) => Ok(v),
            Err(_e) => Err(ClientError::Encoding),
        }
    }

    /// Proxy to an endpoint, WITHOUT using shared types
    ///
    /// This method should be used when the types used by the remote server are NOT known.
    ///
    /// This method is the equivalent of `send_resp` in postcard-rpc. The message will be
    /// sent to poststation, and proxied to the remote device. The response will then be
    /// forwarded back from poststation to the client.
    ///
    /// Returns an error if the connection between the client and poststation failed, or
    /// if the connection between poststation and the remote device failed. This method
    /// also returns an error if the provided `Value` does not match the schema reported
    /// by the remote device.
    pub async fn proxy_endpoint_json(
        &self,
        serial: u64,
        path: &str,
        seq_no: u32,
        body: Value,
    ) -> Result<Value, ClientError> {
        let Ok(Some(schemas)) = self.get_device_schemas(serial).await else {
            return Err(ClientError::Server("endpoint not found".into()));
        };

        // find key
        let res = schemas.endpoints.iter().find(|e| e.path.as_str() == path);
        let Some(schema) = res else {
            return Err(ClientError::Server("endpoint not found".into()));
        };

        let Ok(body) = postcard_dyn::to_stdvec_dyn(&schema.req_ty, &body) else {
            return Err(ClientError::Dynamic(
                "provided JSON does not match the expected schema for this endpoint".into(),
            ));
        };
        let req = ProxyRequest {
            serial,
            path: schema.path.clone(),
            req_key: schema.req_key,
            resp_key: schema.resp_key,
            seq_no,
            req_body: body,
        };

        let resp = self.client.send_resp::<ProxyEndpoint>(&req).await;

        // client to poststation comms
        let resp = resp?;

        // poststation to remote comms
        let resp = match resp {
            ProxyResponse::Ok { body, .. } => body,
            ProxyResponse::WireErr { body, .. } => {
                return Err(ClientError::Remote(format!("WireErr: {body:?}")))
            }
            ProxyResponse::OtherErr(e) => {
                return Err(ClientError::Remote(format!("Other Server Err: '{e}'")))
            }
        };

        let resp = postcard_dyn::from_slice_dyn(&schema.resp_ty, &resp);

        match resp {
            Ok(v) => Ok(v),
            Err(e) => Err(ClientError::Dynamic(format!("Decode error: '{e:?}'"))),
        }
    }

    /// Publish a `topic-in` message to a given device, WITHOUT shared types
    ///
    /// This is the equivalent to `publish` in `postcard-rpc`.
    ///
    /// Returns an error if the connection between the client and poststation, or the connection
    /// between poststation and the remote device has failed. Also returns an error if the provided
    /// `Value` does not match the schema reported by the remote device.
    pub async fn publish_topic_json(
        &self,
        serial: u64,
        path: &str,
        seq_no: u32,
        body: Value,
    ) -> Result<(), ClientError> {
        let Ok(Some(schemas)) = self.get_device_schemas(serial).await else {
            return Err(ClientError::Server("topic not found".into()));
        };

        // find key
        let res = schemas.topics_in.iter().find(|e| e.path.as_str() == path);
        let Some(schema) = res else {
            return Err(ClientError::Server("topic not found".into()));
        };

        let Ok(body) = postcard_dyn::to_stdvec_dyn(&schema.ty, &body) else {
            return Err(ClientError::Dynamic(
                "provided JSON does not match the schema for this topic".into(),
            ));
        };
        let req = PublishRequest {
            serial,
            path: schema.path.clone(),
            topic_key: schema.key,
            seq_no,
            topic_body: body,
        };

        let resp = self.client.send_resp::<PublishEndpoint>(&req).await;

        let resp = resp?;

        match resp {
            PublishResponse::Sent => Ok(()),
            PublishResponse::OtherErr(e) => Err(ClientError::Server(e)),
        }
    }

    /// Publish a `topic-in` message to a given device, WITH shared types
    ///
    /// Returns an error if the connection between the client and poststation, or the connection
    /// between poststation and the remote device has failed.
    pub async fn publish_topic<T>(
        &self,
        serial: u64,
        seq_no: u32,
        body: &T::Message,
    ) -> Result<(), ClientError>
    where
        T: Topic,
        T::Message: Serialize,
    {
        let Ok(Some(schemas)) = self.get_device_schemas(serial).await else {
            return Err(ClientError::Server("topic not found".into()));
        };

        // find key
        // TODO: Don't compare the types because the names don't match even though we've
        // type-punned
        let res = schemas
            .topics_in
            .iter()
            .find(|t| t.path.as_str() == T::PATH && t.key == T::TOPIC_KEY);
        let Some(schema) = res else {
            return Err(ClientError::Server("topic not found".into()));
        };

        let Ok(body) = postcard::to_stdvec(body) else {
            return Err(ClientError::Encoding);
        };
        let req = PublishRequest {
            serial,
            path: schema.path.clone(),
            topic_key: schema.key,
            seq_no,
            topic_body: body,
        };

        let resp = self.client.send_resp::<PublishEndpoint>(&req).await;

        let resp = resp?;

        match resp {
            PublishResponse::Sent => Ok(()),
            PublishResponse::OtherErr(e) => Err(ClientError::Server(e)),
        }
    }

    /// Listen to a given topic path, receiving a subscription that yields live messages
    pub async fn stream_topic_json(
        &self,
        serial: u64,
        path: &str,
    ) -> Result<JsonStreamListener, ClientError> {
        let Ok(Some(schemas)) = self.get_device_schemas(serial).await else {
            return Err(ClientError::Server("topic not found".into()));
        };

        // find key
        let res = schemas
            .topics_out
            .iter()
            .find(|e| e.path.as_str() == path)
            .cloned();
        let Some(schema) = res else {
            return Err(ClientError::Server("topic not found".into()));
        };

        let sub = self
            .client
            .subscribe_multi::<SubscribeTopic>(64)
            .await
            .map_err(|_| ClientError::ConnectionClosed)?;

        let res = self
            .client
            .send_resp::<StartStreamEndpoint>(&TopicStreamRequest {
                serial,
                path: path.to_string(),
                key: schema.key,
            })
            .await;

        let stream_id = match res? {
            TopicStreamResult::Started(id) => id,
            TopicStreamResult::DeviceDisconnected => {
                return Err(ClientError::Server("Device Disconnected".into()))
            }
            TopicStreamResult::NoDeviceKnown => {
                return Err(ClientError::Server("No Device Known".into()))
            }
            TopicStreamResult::NoSuchTopic => {
                return Err(ClientError::Server("No Such Topic".into()))
            }
        };

        Ok(JsonStreamListener {
            schema,
            sub,
            stream_id,
        })
    }

    /// Listen to a given topic path, receiving a subscription that yields live messages
    pub async fn stream_topic<T>(&self, serial: u64) -> Result<StreamListener<T>, ClientError>
    where
        T: Topic,
        T::Message: DeserializeOwned,
    {
        let Ok(Some(schemas)) = self.get_device_schemas(serial).await else {
            return Err(ClientError::Server("topic not found".into()));
        };

        // find key
        let res = schemas
            .topics_out
            .iter()
            .find(|e| e.path.as_str() == T::PATH && e.key == T::TOPIC_KEY)
            .cloned();
        let Some(schema) = res else {
            return Err(ClientError::Server("topic not found".into()));
        };

        let sub = self
            .client
            .subscribe_multi::<SubscribeTopic>(64)
            .await
            .map_err(|_| ClientError::ConnectionClosed)?;

        let res = self
            .client
            .send_resp::<StartStreamEndpoint>(&TopicStreamRequest {
                serial,
                path: T::PATH.to_string(),
                key: schema.key,
            })
            .await;

        let stream_id = match res? {
            TopicStreamResult::Started(id) => id,
            TopicStreamResult::DeviceDisconnected => {
                return Err(ClientError::Server("Device Disconnected".into()))
            }
            TopicStreamResult::NoDeviceKnown => {
                return Err(ClientError::Server("No Device Known".into()))
            }
            TopicStreamResult::NoSuchTopic => {
                return Err(ClientError::Server("No Such Topic".into()))
            }
        };

        Ok(StreamListener {
            sub,
            stream_id,
            _pd: PhantomData,
        })
    }
}

pub struct JsonStreamListener {
    stream_id: Uuidv7,
    schema: TopicReport,
    sub: MultiSubscription<TopicStreamMsg>,
}

impl JsonStreamListener {
    /// Receive a single message from this subscription
    ///
    /// Returns None if the connection has been closed
    pub async fn recv(&mut self) -> Option<Value> {
        loop {
            let msg = match self.sub.recv().await {
                Ok(m) => m,
                Err(MultiSubRxError::IoClosed) => return None,
                Err(MultiSubRxError::Lagged(n)) => {
                    tracing::warn!(stream_id = ?self.stream_id, lags = n, "Stream lagged");
                    continue;
                }
            };

            let TopicStreamMsg { stream_id, msg } = msg;
            if stream_id != self.stream_id {
                continue;
            }

            let Ok(msg) = postcard_dyn::from_slice_dyn(&self.schema.ty, &msg) else {
                continue;
            };
            return Some(msg);
        }
    }
}

pub struct StreamListener<T>
where
    T: Topic,
    T::Message: DeserializeOwned,
{
    stream_id: Uuidv7,
    sub: MultiSubscription<TopicStreamMsg>,
    _pd: PhantomData<fn() -> T>,
}

impl<T> StreamListener<T>
where
    T: Topic,
    T::Message: DeserializeOwned,
{
    /// Receive a single message from this subscription
    ///
    /// Returns None if the connection has been closed
    pub async fn recv(&mut self) -> Option<T::Message> {
        loop {
            let msg = match self.sub.recv().await {
                Ok(m) => m,
                Err(MultiSubRxError::IoClosed) => return None,
                Err(MultiSubRxError::Lagged(n)) => {
                    tracing::warn!(stream_id = ?self.stream_id, lags = n, "Stream lagged");
                    continue;
                }
            };

            let TopicStreamMsg { stream_id, msg } = msg;
            if stream_id != self.stream_id {
                continue;
            }

            let Ok(msg) = postcard::from_bytes(&msg) else {
                continue;
            };
            return Some(msg);
        }
    }
}

/// Connect to a server configured in "insecure" mode
///
/// "Insecure" is not the default setting. Your poststation server must
/// be configured to "insecure" to allow this.
pub async fn connect_insecure(port: u16) -> Result<PoststationClient, ConnectError> {
    // Insecure can only be located on localhost
    let socket = TcpStream::connect(SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port))
        .await
        .map_err(|_| ConnectError::Connection)?;
    let addr = socket.peer_addr().map_err(|_| ConnectError::Connection)?;
    socket
        .set_nodelay(true)
        .map_err(|_| ConnectError::Connection)?;
    let (rx, tx) = split(socket);

    let client = HostClient::<WireError>::new_with_wire(
        TcpCommsTx { tx },
        TcpCommsRx {
            rx,
            addr,
            buf: vec![],
        },
        TcpSpawn,
        postcard_rpc::header::VarSeqKind::Seq4,
        ERROR_PATH,
        64,
    );

    let res = client
        .send_resp::<PingEndpoint>(&42)
        .await
        .map_err(|_| ConnectError::Protocol)?;

    if res != 42 {
        return Err(ConnectError::Protocol);
    }

    Ok(PoststationClient { client })
}

#[non_exhaustive]
#[derive(Debug)]
pub enum ConnectError {
    // Failed to load CA Certificate for server
    CaCertificate,
    // Failed to connect to poststation server
    Connection,
    // Protocol check failed
    Protocol,
}

impl Display for ConnectError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        <Self as Debug>::fmt(self, f)
    }
}

impl Error for ConnectError {}

/// Connect to a server configured with Self Signed TLS certificates (default)
///
/// This function can only be used on the same device as the poststation server.
///
/// If you are connecting to a remote device, you will need to obtain the CA cert
/// from the poststation server, and use [`connect_with_ca_pem`] to connect
pub async fn connect<T: tokio::net::ToSocketAddrs>(
    addr: T,
) -> Result<PoststationClient, ConnectError> {
    // If we are on the same machine as the Poststation server, we can load the CA cert from the
    // working folder of poststation
    let Some(dirs) = ProjectDirs::from("com.onevariable", "onevariable", "poststation") else {
        return Err(ConnectError::CaCertificate);
    };
    let data_dir = dirs.data_dir();
    let mut pem_path = PathBuf::from(data_dir);
    pem_path.push("ca-cert.pem");
    connect_with_ca_pem(addr, &pem_path).await
}

/// Connect to a server with the given TLS CA certificate
pub async fn connect_with_ca_pem<T: tokio::net::ToSocketAddrs>(
    addr: T,
    ca_path: &Path,
) -> Result<PoststationClient, ConnectError> {
    let mut root_cert_store = RootCertStore::empty();
    root_cert_store
        .add(CertificateDer::from_pem_file(ca_path).map_err(|_| ConnectError::CaCertificate)?)
        .map_err(|_| ConnectError::CaCertificate)?;
    let config = rustls::ClientConfig::builder()
        .with_root_certificates(root_cert_store)
        .with_no_client_auth();
    let connector = TlsConnector::from(Arc::new(config));
    let stream = TcpStream::connect(addr)
        .await
        .map_err(|_| ConnectError::Connection)?;
    stream
        .set_nodelay(false)
        .map_err(|_| ConnectError::Connection)?;
    let addr = stream.peer_addr().map_err(|_| ConnectError::Connection)?;
    let stream = connector
        .connect(ServerName::IpAddress(addr.ip().into()), stream)
        .await
        .map_err(|_| ConnectError::Connection)?;
    let (rx, tx) = split(stream);

    let client = HostClient::<WireError>::new_with_wire(
        TcpCommsTx { tx },
        TcpCommsRx {
            rx,
            addr,
            buf: vec![],
        },
        TcpSpawn,
        postcard_rpc::header::VarSeqKind::Seq4,
        ERROR_PATH,
        64,
    );

    let res = client
        .send_resp::<PingEndpoint>(&42)
        .await
        .map_err(|_| ConnectError::Protocol)?;

    if res != 42 {
        return Err(ConnectError::Protocol);
    }

    Ok(PoststationClient { client })
}

pub enum TcpCommsRxError {
    RxOverflow,
    ConnError,
}

impl Debug for TcpCommsRxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("oops")
    }
}

impl Display for TcpCommsRxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("oops")
    }
}

impl Error for TcpCommsRxError {}

struct TcpCommsRx<T: AsyncRead + Send + 'static> {
    addr: SocketAddr,
    buf: Vec<u8>,
    rx: ReadHalf<T>,
}

impl<T: AsyncRead + Send + 'static> TcpCommsRx<T> {
    async fn receive_inner(&mut self) -> Result<Vec<u8>, TcpCommsRxError> {
        let mut rx_buf = [0u8; 1024];
        'frame: loop {
            if self.buf.len() > (1024 * 1024) {
                tracing::warn!(?self.addr, "Refusing to collect >1MiB, terminating");
                self.buf.clear();
                return Err(TcpCommsRxError::RxOverflow);
            }

            // Do we have a message already?
            if let Some(pos) = self.buf.iter().position(|b| *b == 0) {
                // we found the end of a message, attempt to decode it
                let mut split = self.buf.split_off(pos + 1);
                core::mem::swap(&mut self.buf, &mut split);

                // Can we decode the cobs?
                let res = cobs::decode_vec(&split);
                let Ok(msg) = res else {
                    tracing::warn!(?self.addr, discarded = split.len(), "Discarding bad message (cobs)");
                    continue 'frame;
                };

                return Ok(msg);
            }

            // No message yet, let's try and receive some data
            let Ok(used) = self.rx.read(&mut rx_buf).await else {
                tracing::warn!(?self.addr, "Closing");
                return Err(TcpCommsRxError::ConnError);
            };
            if used == 0 {
                tracing::warn!(?self.addr, "Closing");
                return Err(TcpCommsRxError::ConnError);
            }
            self.buf.extend_from_slice(&rx_buf[..used]);
        }
    }
}

impl<T: AsyncRead + Send + 'static> WireRx for TcpCommsRx<T> {
    type Error = TcpCommsRxError;

    fn receive(&mut self) -> impl Future<Output = Result<Vec<u8>, Self::Error>> + Send {
        self.receive_inner()
    }
}

// ---

pub enum TcpCommsTxError {
    CommsError,
}

impl Debug for TcpCommsTxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("oops")
    }
}

impl Display for TcpCommsTxError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("oops")
    }
}

impl Error for TcpCommsTxError {}

struct TcpCommsTx<T: AsyncWrite + Send + 'static> {
    tx: WriteHalf<T>,
}

impl<T: AsyncWrite + Send + 'static> TcpCommsTx<T> {
    async fn send_inner(&mut self, data: Vec<u8>) -> Result<(), TcpCommsTxError> {
        let mut data = cobs::encode_vec(&data);
        data.push(0);
        self.tx
            .write_all(&data)
            .await
            .map_err(|_| TcpCommsTxError::CommsError)
    }
}

impl<T: AsyncWrite + Send + 'static> WireTx for TcpCommsTx<T> {
    type Error = TcpCommsTxError;

    fn send(&mut self, data: Vec<u8>) -> impl Future<Output = Result<(), Self::Error>> + Send {
        self.send_inner(data)
    }
}

// ---

struct TcpSpawn;

impl WireSpawn for TcpSpawn {
    fn spawn(&mut self, fut: impl Future<Output = ()> + Send + 'static) {
        tokio::spawn(fut);
    }
}