-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
peripheral.rs
294 lines (268 loc) · 9.99 KB
/
peripheral.rs
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
use crate::{
api::{
self, BDAddr, Characteristic, PeripheralProperties, Service, ValueNotification, WriteType,
},
Error, Result,
};
use async_trait::async_trait;
use futures::stream::Stream;
use jni::{
objects::{GlobalRef, JList, JObject},
JNIEnv,
};
use jni_utils::{
arrays::byte_array_to_vec, exceptions::try_block, future::JSendFuture, stream::JSendStream,
task::JPollResult, uuid::JUuid,
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "serde")]
use serde_cr as serde;
use std::{
collections::BTreeSet,
convert::TryFrom,
fmt::{self, Debug, Display, Formatter},
pin::Pin,
sync::{Arc, Mutex},
};
use super::jni::{
global_jvm,
objects::{JBluetoothGattCharacteristic, JBluetoothGattService, JPeripheral},
};
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_cr")
)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PeripheralId(pub(super) BDAddr);
impl Display for PeripheralId {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
fn get_poll_result<'a: 'b, 'b>(
env: &'b JNIEnv<'a>,
result: JPollResult<'a, 'b>,
) -> Result<JObject<'a>> {
try_block(env, || Ok(Ok(result.get()?)))
.catch("io/github/gedgygedgy/rust/future/FutureException", |ex| {
let cause = env
.call_method(ex, "getCause", "()Ljava/lang/Throwable;", &[])?
.l()?;
if env.is_instance_of(
cause,
"com/nonpolynomial/btleplug/android/impl/NotConnectedException",
)? {
Ok(Err(Error::NotConnected))
} else if env.is_instance_of(
cause,
"com/nonpolynomial/btleplug/android/impl/PermissionDeniedException",
)? {
Ok(Err(Error::PermissionDenied))
} else {
env.throw(ex)?;
Err(jni::errors::Error::JavaException)
}
})
.result()?
}
struct PeripheralShared {
services: BTreeSet<Service>,
characteristics: BTreeSet<Characteristic>,
properties: Option<PeripheralProperties>,
}
#[derive(Clone)]
pub struct Peripheral {
addr: BDAddr,
internal: GlobalRef,
shared: Arc<Mutex<PeripheralShared>>,
}
impl Peripheral {
pub(crate) fn new(env: &JNIEnv, adapter: JObject, addr: BDAddr) -> Result<Self> {
let obj = JPeripheral::new(env, adapter, addr)?;
Ok(Self {
addr,
internal: env.new_global_ref(obj)?,
shared: Arc::new(Mutex::new(PeripheralShared {
services: BTreeSet::new(),
characteristics: BTreeSet::new(),
properties: None,
})),
})
}
pub(crate) fn report_properties(&self, mut properties: PeripheralProperties) {
let mut guard = self.shared.lock().unwrap();
guard.properties = Some(properties);
}
fn with_obj<T, E>(
&self,
f: impl FnOnce(&JNIEnv, JPeripheral) -> std::result::Result<T, E>,
) -> std::result::Result<T, E>
where
E: From<::jni::errors::Error>,
{
let env = global_jvm().get_env()?;
let obj = JPeripheral::from_env(&env, self.internal.as_obj())?;
f(&env, obj)
}
async fn set_characteristic_notification(
&self,
characteristic: &Characteristic,
enable: bool,
) -> Result<()> {
let future = self.with_obj(|env, obj| {
let uuid_obj = JUuid::new(env, characteristic.uuid)?;
JSendFuture::try_from(obj.set_characteristic_notification(uuid_obj, enable)?)
})?;
let result_ref = future.await?;
self.with_obj(|env, _obj| {
let result = JPollResult::from_env(env, result_ref.as_obj())?;
get_poll_result(env, result).map(|_| {})
})
}
}
impl Debug for Peripheral {
fn fmt(&self, fmt: &mut Formatter) -> std::result::Result<(), std::fmt::Error> {
write!(fmt, "{:?}", self.internal.as_obj())
}
}
#[async_trait]
impl api::Peripheral for Peripheral {
/// Returns the unique identifier of the peripheral.
fn id(&self) -> PeripheralId {
PeripheralId(self.addr)
}
fn address(&self) -> BDAddr {
self.addr
}
async fn properties(&self) -> Result<Option<PeripheralProperties>> {
let guard = self.shared.lock().unwrap();
Ok((&guard.properties).clone())
}
fn characteristics(&self) -> BTreeSet<Characteristic> {
let guard = self.shared.lock().unwrap();
(&guard.characteristics).clone()
}
async fn is_connected(&self) -> Result<bool> {
self.with_obj(|_env, obj| Ok(obj.is_connected()?))
}
async fn connect(&self) -> Result<()> {
let future = self.with_obj(|_env, obj| JSendFuture::try_from(obj.connect()?))?;
let result_ref = future.await?;
self.with_obj(|env, _obj| {
let result = JPollResult::from_env(env, result_ref.as_obj())?;
get_poll_result(env, result).map(|_| {})
})
}
async fn disconnect(&self) -> Result<()> {
let future = self.with_obj(|_env, obj| JSendFuture::try_from(obj.disconnect()?))?;
let result_ref = future.await?;
self.with_obj(|env, _obj| {
let result = JPollResult::from_env(env, result_ref.as_obj())?;
get_poll_result(env, result).map(|_| {})
})
}
/// The set of services we've discovered for this device. This will be empty until
/// `discover_services` is called.
fn services(&self) -> BTreeSet<Service> {
let guard = self.shared.lock().unwrap();
(&guard.services).clone()
}
async fn discover_services(&self) -> Result<()> {
let future = self.with_obj(|_env, obj| JSendFuture::try_from(obj.discover_services()?))?;
let result_ref = future.await?;
self.with_obj(|env, _obj| {
use std::iter::FromIterator;
let result = JPollResult::from_env(env, result_ref.as_obj())?;
let obj = get_poll_result(env, result)?;
let list = JList::from_env(env, obj)?;
let mut peripheral_services = Vec::new();
let mut peripheral_characteristics = Vec::new();
for service in list.iter()? {
let service = JBluetoothGattService::from_env(env, service)?;
let mut characteristics = BTreeSet::new();
for characteristic in service.get_characteristics()? {
characteristics.insert(Characteristic {
service_uuid: service.get_uuid()?,
uuid: characteristic.get_uuid()?,
properties: characteristic.get_properties()?,
});
peripheral_characteristics.push(Characteristic {
service_uuid: service.get_uuid()?,
uuid: characteristic.get_uuid()?,
properties: characteristic.get_properties()?,
});
}
peripheral_services.push(Service {
uuid: service.get_uuid()?,
primary: service.is_primary()?,
characteristics,
})
}
let mut guard = self.shared.lock().unwrap();
guard.services = BTreeSet::from_iter(peripheral_services.clone());
guard.characteristics = BTreeSet::from_iter(peripheral_characteristics.clone());
Ok(())
})
}
async fn write(
&self,
characteristic: &Characteristic,
data: &[u8],
write_type: WriteType,
) -> Result<()> {
let future = self.with_obj(|env, obj| {
let uuid = JUuid::new(env, characteristic.uuid)?;
let data_obj = jni_utils::arrays::slice_to_byte_array(env, data)?;
let write_type = match write_type {
WriteType::WithResponse => 2,
WriteType::WithoutResponse => 1,
};
JSendFuture::try_from(obj.write(uuid, data_obj.into(), write_type)?)
})?;
let result_ref = future.await?;
self.with_obj(|env, _obj| {
let result = JPollResult::from_env(env, result_ref.as_obj())?;
get_poll_result(env, result).map(|_| {})
})
}
async fn read(&self, characteristic: &Characteristic) -> Result<Vec<u8>> {
let future = self.with_obj(|env, obj| {
let uuid = JUuid::new(env, characteristic.uuid)?;
JSendFuture::try_from(obj.read(uuid)?)
})?;
let result_ref = future.await?;
self.with_obj(|env, _obj| {
let result = JPollResult::from_env(env, result_ref.as_obj())?;
let bytes = get_poll_result(env, result)?;
Ok(byte_array_to_vec(env, bytes.into_inner())?)
})
}
async fn subscribe(&self, characteristic: &Characteristic) -> Result<()> {
self.set_characteristic_notification(characteristic, true)
.await
}
async fn unsubscribe(&self, characteristic: &Characteristic) -> Result<()> {
self.set_characteristic_notification(characteristic, false)
.await
}
async fn notifications(&self) -> Result<Pin<Box<dyn Stream<Item = ValueNotification> + Send>>> {
use futures::stream::StreamExt;
let stream = self.with_obj(|_env, obj| JSendStream::try_from(obj.get_notifications()?))?;
let stream = stream
.map(|item| match item {
Ok(item) => {
let env = global_jvm().get_env()?;
let item = item.as_obj();
let characteristic = JBluetoothGattCharacteristic::from_env(&env, item)?;
let uuid = characteristic.get_uuid()?;
let value = characteristic.get_value()?;
Ok(ValueNotification { uuid, value })
}
Err(err) => Err(err),
})
.filter_map(|item| async { item.ok() });
Ok(Box::pin(stream))
}
}