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
//! MAP client — OBEX session setup, SETPATH sequencing, and request dispatch.
use bytes::Bytes;
use formats::bmessage::BMessage;
use formats::xml::FolderListing;
use futures::{SinkExt, StreamExt};
use obex_core::{
client::{ObexClient, ObexError},
headers::Header,
};
use obex_core::{wrap, ObexTransport};
use tokio::io::{AsyncRead, AsyncWrite};
use crate::{
folders::Folder,
messages::{ListMessagesFilter, MessageEntry},
params::{
get_message_params, push_message_params, set_message_status_params,
set_notification_registration_params, INDICATOR_DELETED_STATUS, INDICATOR_READ_STATUS,
},
MapError, MessageStatus,
};
const MAP_UUID: [u8; 16] = [
0xbb, 0x58, 0x2b, 0x40, 0x42, 0x0c, 0x11, 0xdb, 0xb0, 0xde, 0x08, 0x00, 0x20, 0x0c, 0x9a, 0x66,
];
const MAX_BODY_BYTES: usize = 4 * 1024 * 1024;
/// MAP session over an OBEX transport. Owns the OBEX state machine and the framed I/O.
///
/// Obtain via [`MapClient::connect`].
pub struct MapClient<T> {
obex: ObexClient,
transport: ObexTransport<T>,
// SETPATH depth confirmed by server; 0 = root, max 3 (telecom/msg/<folder>).
depth: u8,
}
impl<T: AsyncRead + AsyncWrite + Unpin> MapClient<T> {
/// Sends the MAP UUID as the OBEX `Target` header and validates the server's response.
///
/// # Errors
///
/// Returns [`MapError`] if the transport fails, packet encoding fails, the server rejects
/// the connection, or the response omits the `ConnectionId` header.
pub async fn connect(stream: T) -> Result<Self, MapError> {
let mut transport = wrap(stream);
let mut obex = ObexClient::new();
let req = ObexClient::connect_request(&MAP_UUID)?;
transport.send(req).await?;
let rsp = Self::recv(&mut transport).await?;
obex.handle_connect_response(&rsp)?;
Ok(Self { obex, transport, depth: 0 })
}
/// `segment` is a single path component, not a slash-joined path — iOS requires one
/// SETPATH per level. Does not validate that `segment` names an existing folder.
///
/// # Errors
///
/// Returns [`MapError`] if the request fails to encode, the transport closes, or the
/// server returns a non-OK response.
pub(crate) async fn setpath(&mut self, segment: &str) -> Result<(), MapError> {
let req = self.obex.setpath_request(segment)?;
self.transport.send(req).await?;
let rsp_bytes = Self::recv(&mut self.transport).await?;
let rsp = ObexClient::parse_response(&rsp_bytes)?;
if !rsp.opcode.is_ok() {
return Err(MapError::ServerError(rsp.opcode.to_byte()));
}
self.depth = self.depth.saturating_add(1);
Ok(())
}
/// Decrements `depth` on success. Does not check whether depth is already zero.
///
/// # Errors
///
/// Returns [`MapError`] if the request fails to encode, the transport closes, or the
/// server returns a non-OK response.
pub(crate) async fn setpath_up(&mut self) -> Result<(), MapError> {
debug_assert!(self.depth > 0, "setpath_up called at root");
let req = self.obex.setpath_backup_request()?;
self.transport.send(req).await?;
let rsp_bytes = Self::recv(&mut self.transport).await?;
let rsp = ObexClient::parse_response(&rsp_bytes)?;
if !rsp.opcode.is_ok() {
return Err(MapError::ServerError(rsp.opcode.to_byte()));
}
self.depth = self.depth.saturating_sub(1);
Ok(())
}
/// No-op when already at root (`depth == 0`). Does not navigate anywhere after resetting.
///
/// # Errors
///
/// Returns [`MapError`] if any backup SETPATH fails to encode, the transport closes, or the
/// server returns a non-OK response.
pub(crate) async fn reset_to_root(&mut self) -> Result<(), MapError> {
for _ in 0..self.depth {
self.setpath_up().await?;
}
Ok(())
}
/// If already inside a subfolder (e.g. after a prior `set_folder` call), backs up to root
/// first, then navigates `telecom` → `msg` → folder. iOS requires one SETPATH per level;
/// a single slash-joined path is rejected.
///
/// # Errors
///
/// Returns [`MapError`] if any SETPATH fails to encode, the transport closes, or the
/// server returns a non-OK response for any step.
pub async fn set_folder(&mut self, folder: Folder) -> Result<(), MapError> {
self.reset_to_root().await?;
for segment in ["telecom", "msg", folder.as_str()] {
self.setpath(segment).await?;
}
Ok(())
}
/// Caller must navigate to the target folder via [`Self::set_folder`] before calling this.
/// Sends a `GetMessagesListing` GET with no Name header — the device lists the current OBEX
/// working directory. Accumulates body chunks across CONTINUE responses before parsing.
///
/// # Errors
///
/// Returns [`MapError`] if the transport fails, the server rejects the request, or the
/// response XML is malformed.
pub async fn list_messages(
&mut self,
filter: &ListMessagesFilter,
) -> Result<Vec<MessageEntry>, MapError> {
let req = self.obex.get_request(
b"x-bt/MAP-msg-listing\x00",
None,
Some(filter.to_app_params()?),
)?;
self.transport.send(req).await?;
let body = self.collect_body().await?;
Ok(crate::xml::parse_message_listing(&body)?)
}
/// Sends a `GetMessage` GET with `Type: x-bt/message` and `Charset=UTF-8`. Accumulates body
/// chunks across CONTINUE responses before parsing.
///
/// # Errors
///
/// Returns [`MapError::InvalidInput`] if `handle` is empty or contains CR or LF — it lands
/// verbatim in the OBEX Name header. Returns [`MapError`] if the transport fails, the server
/// rejects the request, the response body exceeds 4 MiB, is not valid UTF-8, or the bMessage
/// is malformed.
pub async fn get_message(&mut self, handle: &str) -> Result<BMessage, MapError> {
if handle.is_empty() {
return Err(MapError::InvalidInput("handle must not be empty"));
}
if handle.contains(['\r', '\n']) {
return Err(MapError::InvalidInput("handle must not contain CR or LF"));
}
let req =
self.obex.get_request(b"x-bt/message\x00", Some(handle), Some(get_message_params()))?;
self.transport.send(req).await?;
let body = self.collect_body().await?;
let text = std::str::from_utf8(&body).map_err(|_| MapError::InvalidEncoding)?;
Ok(BMessage::parse(text)?)
}
/// Sends `text` as an outbound SMS to `phone` via MAP `PushMessage`. Returns the opaque
/// handle assigned by the remote, suitable for passing to `set_message_status`. Caller must
/// have navigated to outbox via `set_folder(Folder::Outbox)` first.
///
/// # Errors
///
/// Returns [`MapError`] if encoding fails, the transport closes, the server rejects the
/// request, or the OK response contains no Name header.
pub async fn push_message(&mut self, phone: &str, text: &str) -> Result<String, MapError> {
if phone.contains(['\r', '\n']) {
return Err(MapError::InvalidInput("phone must not contain CR or LF"));
}
let body = BMessage::outbound_sms(phone, text).encode();
let body_bytes = body.as_bytes();
let len = u32::try_from(body_bytes.len()).map_err(|_| ObexError::BodyTooLarge)?;
let req = self.obex.put_final_request(
b"x-bt/message\x00",
vec![
Header::Name(String::new()),
Header::Length(len),
Header::AppParams(push_message_params()),
Header::EndOfBody(Bytes::copy_from_slice(body_bytes)),
],
)?;
self.transport.send(req).await?;
let rsp_bytes = Self::recv(&mut self.transport).await?;
let rsp = ObexClient::parse_response(&rsp_bytes)?;
if !rsp.opcode.is_ok() {
return Err(MapError::ServerError(rsp.opcode.to_byte()));
}
rsp.header_name().ok_or(MapError::MissingHandle)
}
/// Marks the message identified by `handle` as `status` (read or unread) via MAP
/// `SetMessageStatus`. Caller must navigate to the containing folder via `set_folder` first.
///
/// # Errors
///
/// Returns [`MapError::InvalidInput`] if `handle` is empty or contains CR or LF.
/// Returns [`MapError::ServerError`] if the remote returns a non-OK response.
/// Returns [`MapError::Obex`] or [`MapError::Transport`] on lower-layer failure.
pub async fn set_message_status_read(
&mut self,
handle: &str,
status: MessageStatus,
) -> Result<(), MapError> {
let value = match status {
MessageStatus::Read => 0x01_u8,
MessageStatus::Unread => 0x00_u8,
};
self.do_set_message_status(handle, INDICATOR_READ_STATUS, value).await
}
/// Marks the message identified by `handle` as deleted (`true`) or undeleted (`false`) via
/// MAP `SetMessageStatus`. Caller must navigate to the containing folder via `set_folder` first.
///
/// # Errors
///
/// Returns [`MapError::InvalidInput`] if `handle` is empty or contains CR or LF.
/// Returns [`MapError::ServerError`] if the remote returns a non-OK response.
/// Returns [`MapError::Obex`] or [`MapError::Transport`] on lower-layer failure.
pub async fn set_message_status_deleted(
&mut self,
handle: &str,
deleted: bool,
) -> Result<(), MapError> {
self.do_set_message_status(handle, INDICATOR_DELETED_STATUS, u8::from(deleted)).await
}
/// Returns the MAP folder listing for the current object store level via `GetFolderListing`
/// GET with Type `x-obex/folder-listing`. Folders are in device-reported document order.
///
/// # Errors
///
/// Returns [`MapError::ServerError`] if the remote returns a non-OK response.
/// Returns [`MapError::FolderListing`] if the response body is malformed XML.
/// Returns [`MapError::Obex`] or [`MapError::Transport`] on lower-layer failure.
pub async fn get_folder_listing(&mut self) -> Result<FolderListing, MapError> {
let req = self.obex.get_request(b"x-obex/folder-listing\x00", None, None)?;
self.transport.send(req).await?;
let body = self.collect_body().await?;
Ok(FolderListing::parse(&body)?)
}
/// When `enable` is `true`, the phone will connect to the MNS channel and push event reports.
/// When `false`, it stops. The caller must keep the MAP session alive while notifications are active.
///
/// # Errors
///
/// Returns [`MapError::ServerError`] if the remote returns a non-OK response.
/// Returns [`MapError::Obex`] or [`MapError::Transport`] on lower-layer failure.
pub async fn set_notification_registration(&mut self, enable: bool) -> Result<(), MapError> {
let req = self.obex.put_final_request(
b"x-bt/MAP-NotificationRegistration\x00",
vec![
Header::AppParams(set_notification_registration_params(enable)),
Header::EndOfBody(Bytes::new()),
],
)?;
self.transport.send(req).await?;
let rsp_bytes = Self::recv(&mut self.transport).await?;
let rsp = ObexClient::parse_response(&rsp_bytes)?;
if !rsp.opcode.is_ok() {
return Err(MapError::ServerError(rsp.opcode.to_byte()));
}
Ok(())
}
async fn do_set_message_status(
&mut self,
handle: &str,
indicator: u8,
value: u8,
) -> Result<(), MapError> {
if handle.is_empty() {
return Err(MapError::InvalidInput("handle must not be empty"));
}
if handle.contains(['\r', '\n']) {
return Err(MapError::InvalidInput("handle must not contain CR or LF"));
}
let req = self.obex.put_final_request(
b"x-bt/messageStatus\x00",
vec![
Header::Name(handle.to_owned()),
Header::AppParams(Bytes::from(set_message_status_params(indicator, value))),
],
)?;
self.transport.send(req).await?;
let rsp_bytes = Self::recv(&mut self.transport).await?;
let rsp = ObexClient::parse_response(&rsp_bytes)?;
if !rsp.opcode.is_ok() {
return Err(MapError::ServerError(rsp.opcode.to_byte()));
}
Ok(())
}
async fn collect_body(&mut self) -> Result<Vec<u8>, MapError> {
let mut body = Vec::with_capacity(512);
loop {
let rsp_bytes = Self::recv(&mut self.transport).await?;
let rsp = ObexClient::parse_response(&rsp_bytes)?;
if rsp.opcode.is_continue() {
if let Some(chunk) = rsp.body_payload() {
let new_len =
body.len().checked_add(chunk.len()).ok_or(MapError::ResponseTooLarge)?;
if new_len > MAX_BODY_BYTES {
return Err(MapError::ResponseTooLarge);
}
body.extend_from_slice(chunk);
}
let cont = self.obex.get_continue_request()?;
self.transport.send(cont).await?;
} else if rsp.opcode.is_ok() {
if let Some(chunk) = rsp.body_payload() {
let new_len =
body.len().checked_add(chunk.len()).ok_or(MapError::ResponseTooLarge)?;
if new_len > MAX_BODY_BYTES {
return Err(MapError::ResponseTooLarge);
}
body.extend_from_slice(chunk);
}
break;
} else {
return Err(MapError::ServerError(rsp.opcode.to_byte()));
}
}
Ok(body)
}
/// Reads from the transport until the remote closes the stream, discarding all received
/// packets. Returns `Ok(())` on clean close. Does not send OBEX DISCONNECT and does not
/// parse received packet opcodes.
///
/// # Errors
///
/// Returns [`MapError::Transport`] on a framing error from the underlying codec.
pub async fn hold(&mut self) -> Result<(), MapError> {
loop {
match self.transport.next().await {
None => return Ok(()),
Some(Ok(_)) => {}
Some(Err(e)) => return Err(MapError::Transport(e)),
}
}
}
async fn recv(transport: &mut ObexTransport<T>) -> Result<Bytes, MapError> {
transport.next().await.ok_or(MapError::UnexpectedEof)?.map_err(MapError::Transport)
}
}