Skip to main content

simplersble/
service.rs

1use std::sync::Arc;
2use std::pin::Pin;
3use std::mem;
4
5use super::ffi;
6use crate::characteristic::InnerCharacteristic;
7use crate::characteristic::Characteristic;
8pub struct InnerService {
9    internal: cxx::UniquePtr<ffi::RustyService>,
10}
11
12impl InnerService {
13    pub(crate) fn new(wrapper: &mut ffi::RustyServiceWrapper) -> Pin<Box<Self>> {
14        let this = Self {
15            internal: cxx::UniquePtr::<ffi::RustyService>::null(),
16        };
17
18        let mut this_boxed = Box::pin(this);
19        mem::swap(&mut this_boxed.internal, &mut wrapper.internal);
20
21        return this_boxed;
22    }
23
24    pub fn uuid(&self) -> String {
25        return self.internal.uuid();
26    }
27
28    pub fn data(&self) -> Vec<u8> {
29        return self.internal.data();
30    }
31
32    pub fn characteristics(&self) -> Vec<Characteristic> {
33        let mut characteristics = Vec::<Characteristic>::new();
34
35        for characteristic_wrapper in self.internal.characteristics().iter_mut() {
36            characteristics.push(InnerCharacteristic::new(characteristic_wrapper).into());
37        }
38
39        return characteristics;
40    }
41}
42
43unsafe impl Sync for InnerService {}
44unsafe impl Send for InnerService {}
45
46#[derive(Clone)]
47pub struct Service {
48    inner: Arc<Pin<Box<InnerService>>>,
49}
50
51impl Service {
52    pub fn uuid(&self) -> String {
53        return self.inner.uuid();
54    }
55
56    pub fn data(&self) -> Vec<u8> {
57        return self.inner.data();
58    }
59
60    pub fn characteristics(&self) -> Vec<Characteristic> {
61        return self.inner.characteristics();
62    }
63
64}
65
66impl From<Pin<Box<InnerService>>> for Service {
67    fn from(service: Pin<Box<InnerService>>) -> Self {
68        return Service {
69            inner: Arc::new(service),
70        };
71    }
72}
73
74unsafe impl Send for Service {}
75unsafe impl Sync for Service {}