chessnut-move 1.0.2

Typed, transport-independent SDK for Chessnut Move boards.
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
// Copyright 2026 Daymon Littrell-Reyes
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Allocation-free synchronous session built on a blocking transport.

use crate::protocol::{BoardEvent, Command};
use crate::transport::{
  BoardError, BoardState, DecodedNotification, Notification, NotificationSource,
  decode_notification,
};

/// A blocking I/O adapter for a connected Chessnut Move board.
///
/// # Examples
///
/// ```
/// use core::convert::Infallible;
/// use chessnut_move::protocol::Command;
/// use chessnut_move::transport::{
///     BlockingTransport, Notification, NotificationSource,
/// };
///
/// struct MyTransport;
///
/// impl BlockingTransport for MyTransport {
///     type Error = Infallible;
///
///     fn subscribe(
///         &mut self,
///         _source: NotificationSource,
///     ) -> Result<(), Self::Error> {
///         Ok(())
///     }
///
///     fn write_command(&mut self, _command: &Command) -> Result<(), Self::Error> {
///         Ok(())
///     }
///
///     fn next_notification<'a>(
///         &'a mut self,
///         buffer: &'a mut [u8],
///     ) -> Result<Notification<'a>, Self::Error> {
///         buffer[..34].fill(0);
///         Ok(Notification::new(
///             NotificationSource::Position,
///             &buffer[..34],
///         ))
///     }
/// }
/// ```
pub trait BlockingTransport {
  /// Error returned by Bluetooth or adapter operations.
  type Error;

  /// Enables notifications for one protocol source.
  ///
  /// # Errors
  ///
  /// Returns [`Self::Error`] when the adapter cannot enable the corresponding
  /// [GATT characteristic][NotificationSource::characteristic].
  fn subscribe(&mut self, source: NotificationSource) -> Result<(), Self::Error>;

  /// Disables notifications for one protocol source.
  ///
  /// The default implementation performs no operation.
  ///
  /// # Errors
  ///
  /// Returns [`Self::Error`] when the adapter cannot disable the corresponding
  /// [GATT characteristic][NotificationSource::characteristic].
  fn unsubscribe(&mut self, _source: NotificationSource) -> Result<(), Self::Error> {
    Ok(())
  }

  /// Writes an encoded command using its
  /// [required write kind][Command::write_kind].
  ///
  /// # Errors
  ///
  /// Returns [`Self::Error`] when the command cannot be written.
  fn write_command(&mut self, command: &Command) -> Result<(), Self::Error>;

  /// Receives the next notification into the supplied buffer.
  ///
  /// The returned [`Notification`] must borrow its bytes from `buffer`.
  /// Implementations must return an error instead of truncating a notification
  /// that exceeds the buffer.
  ///
  /// # Errors
  ///
  /// Returns [`Self::Error`] when notification delivery ends, Bluetooth I/O
  /// fails, the source is unknown, or the supplied buffer is too small.
  fn next_notification<'a>(
    &'a mut self,
    buffer: &'a mut [u8],
  ) -> Result<Notification<'a>, Self::Error>;

  /// Closes transport-owned resources after subscriptions are disabled.
  ///
  /// The default implementation performs no operation.
  ///
  /// # Errors
  ///
  /// Returns [`Self::Error`] when transport cleanup fails.
  fn close(&mut self) -> Result<(), Self::Error> {
    Ok(())
  }
}

/// An owning, allocation-free blocking session with a Chessnut Move board.
///
/// The session reuses a fixed notification buffer. Dropping a session does not
/// call [`BlockingTransport::unsubscribe`] or [`BlockingTransport::close`];
/// call [`BlockingBoard::shutdown`] when cleanup is required.
///
/// # Examples
///
/// The transport must already represent a connected board:
///
/// ```no_run
/// use chessnut_move::protocol::{BoardEvent, Command, LedPattern};
/// use chessnut_move::transport::{
///     BlockingBoard, BlockingTransport, BoardError,
/// };
///
/// fn run<T: BlockingTransport>(
///     transport: T,
/// ) -> Result<(), BoardError<T::Error>> {
///     let mut board = BlockingBoard::new(transport);
///     board.initialize()?;
///
///     board.send(&Command::set_leds(&LedPattern::default()))?;
///     match board.next_event()? {
///         BoardEvent::PositionChanged(position) => {
///             println!("position: {position:?}");
///         }
///         BoardEvent::BatteryStatus(_) | BoardEvent::PieceStatus(_) => {}
///     }
///
///     board.shutdown()
/// }
/// ```
pub struct BlockingBoard<T> {
  state: BoardState<T>,
}

impl<T> BlockingBoard<T> {
  /// Creates a board session around a connected transport.
  ///
  /// This constructor performs no I/O. Call [`BlockingBoard::initialize`]
  /// before receiving events.
  ///
  /// # Examples
  ///
  /// ```
  /// use chessnut_move::transport::{BlockingBoard, BlockingTransport};
  ///
  /// fn open_session<T: BlockingTransport>(transport: T) -> BlockingBoard<T> {
  ///     BlockingBoard::new(transport)
  /// }
  /// ```
  pub const fn new(transport: T) -> Self {
    Self {
      state: BoardState::new(transport),
    }
  }

  /// Returns a shared reference to the underlying transport.
  pub const fn transport(&self) -> &T {
    &self.state.transport
  }

  /// Returns a mutable reference to the underlying transport.
  ///
  /// Direct operations can change subscription or connection state expected by
  /// the session.
  pub fn transport_mut(&mut self) -> &mut T {
    &mut self.state.transport
  }

  /// Consumes the session and returns its transport without shutting it down.
  pub fn into_transport(self) -> T {
    self.state.transport
  }
}

impl<T: BlockingTransport> BlockingBoard<T> {
  /// Initializes subscriptions and enables realtime position updates.
  ///
  /// Initialization subscribes to position notifications, writes
  /// [`Command::enable_realtime_updates`], and then subscribes to command
  /// responses. Completed steps are not rolled back when a later step fails.
  ///
  /// # Errors
  ///
  /// Returns [`BoardError::Transport`] when subscribing or writing the
  /// realtime-update command fails.
  pub fn initialize(&mut self) -> Result<(), BoardError<T::Error>> {
    debug_event!(session = "blocking", "initializing board session");
    self
      .state
      .transport
      .subscribe(NotificationSource::Position)
      .map_err(|error| {
        warn_event!(
          session = "blocking",
          stage = "subscribe_position",
          "board initialization failed"
        );
        BoardError::Transport(error)
      })?;
    self.send(&Command::enable_realtime_updates())?;
    self
      .state
      .transport
      .subscribe(NotificationSource::CommandResponse)
      .map_err(|error| {
        warn_event!(
          session = "blocking",
          stage = "subscribe_command_response",
          "board initialization failed"
        );
        BoardError::Transport(error)
      })?;
    debug_event!(session = "blocking", "board session initialized");
    Ok(())
  }

  /// Writes a command to the board.
  ///
  /// This method confirms only the transport write. Commands with protocol
  /// responses are received separately through [`BlockingBoard::next_event`].
  ///
  /// # Errors
  ///
  /// Returns [`BoardError::Transport`] when the transport cannot write the
  /// command.
  pub fn send(&mut self, command: &Command) -> Result<(), BoardError<T::Error>> {
    trace_event!(
      session = "blocking",
      command_len = command.bytes().len(),
      write_kind = ?command.write_kind(),
      "writing board command"
    );
    self
      .state
      .transport
      .write_command(command)
      .map_err(|error| {
        warn_event!(session = "blocking", "board command write failed");
        BoardError::Transport(error)
      })
  }

  /// Blocks until the next observable board event is decoded.
  ///
  /// Session-control acknowledgements are consumed internally and are not
  /// returned as events.
  ///
  /// # Errors
  ///
  /// Returns [`BoardError::Transport`] when notification delivery fails, or
  /// [`BoardError::Decode`] when a notification is malformed or unsupported.
  pub fn next_event(&mut self) -> Result<BoardEvent, BoardError<T::Error>> {
    loop {
      let notification = self
        .state
        .transport
        .next_notification(&mut self.state.notification_buffer)
        .map_err(|error| {
          warn_event!(session = "blocking", "notification receive failed");
          BoardError::Transport(error)
        })?;
      trace_event!(
        session = "blocking",
        source = ?notification.source(),
        notification_len = notification.bytes().len(),
        "received board notification"
      );

      match decode_notification(notification)? {
        DecodedNotification::Event(event) => return Ok(event),
        DecodedNotification::RealtimeUpdatesAcknowledged => {}
      }
    }
  }

  /// Unsubscribes from board notifications and closes the transport.
  ///
  /// Cleanup stops at the first failed operation. Consuming the session with
  /// [`BlockingBoard::into_transport`] remains available after an error.
  ///
  /// # Errors
  ///
  /// Returns [`BoardError::Transport`] when either unsubscribe operation or
  /// [`BlockingTransport::close`] fails.
  pub fn shutdown(&mut self) -> Result<(), BoardError<T::Error>> {
    debug_event!(session = "blocking", "shutting down board session");
    self
      .state
      .transport
      .unsubscribe(NotificationSource::CommandResponse)
      .map_err(|error| {
        warn_event!(
          session = "blocking",
          stage = "unsubscribe_command_response",
          "board shutdown failed"
        );
        BoardError::Transport(error)
      })?;
    self
      .state
      .transport
      .unsubscribe(NotificationSource::Position)
      .map_err(|error| {
        warn_event!(
          session = "blocking",
          stage = "unsubscribe_position",
          "board shutdown failed"
        );
        BoardError::Transport(error)
      })?;
    self.state.transport.close().map_err(|error| {
      warn_event!(
        session = "blocking",
        stage = "close",
        "board shutdown failed"
      );
      BoardError::Transport(error)
    })?;
    debug_event!(session = "blocking", "board session stopped");
    Ok(())
  }
}

#[cfg(test)]
mod tests {
  use core::convert::Infallible;

  use super::*;
  use crate::protocol::{BatteryStatus, WriteKind};

  struct MockTransport {
    subscriptions: [Option<NotificationSource>; 2],
    subscription_count: usize,
    written: [u8; 35],
    written_len: usize,
    write_kind: Option<WriteKind>,
    notification_count: usize,
  }

  impl MockTransport {
    const fn new() -> Self {
      Self {
        subscriptions: [None; 2],
        subscription_count: 0,
        written: [0; 35],
        written_len: 0,
        write_kind: None,
        notification_count: 0,
      }
    }
  }

  impl BlockingTransport for MockTransport {
    type Error = Infallible;

    fn subscribe(&mut self, source: NotificationSource) -> Result<(), Self::Error> {
      self.subscriptions[self.subscription_count] = Some(source);
      self.subscription_count += 1;
      Ok(())
    }

    fn write_command(&mut self, command: &Command) -> Result<(), Self::Error> {
      self.written[..command.bytes().len()].copy_from_slice(command.bytes());
      self.written_len = command.bytes().len();
      self.write_kind = Some(command.write_kind());
      Ok(())
    }

    fn next_notification<'a>(
      &'a mut self,
      buffer: &'a mut [u8],
    ) -> Result<Notification<'a>, Self::Error> {
      let notification: &[u8] = if self.notification_count == 0 {
        &[0x23, 0x01, 0x00]
      } else {
        &[0x41, 0x03, 0x0c, 0x01, 81]
      };
      self.notification_count += 1;
      buffer[..notification.len()].copy_from_slice(notification);
      Ok(Notification::new(
        NotificationSource::CommandResponse,
        &buffer[..notification.len()],
      ))
    }
  }

  #[test]
  fn initializes_and_decodes_events_without_allocating() {
    let mut board = BlockingBoard::new(MockTransport::new());
    board.initialize().unwrap();

    assert_eq!(
      board.transport().subscriptions,
      [
        Some(NotificationSource::Position),
        Some(NotificationSource::CommandResponse),
      ]
    );
    assert_eq!(
      &board.transport().written[..board.transport().written_len],
      Command::enable_realtime_updates().bytes()
    );
    assert_eq!(board.transport().write_kind, Some(WriteKind::WithResponse));

    assert_eq!(
      board.next_event().unwrap(),
      BoardEvent::BatteryStatus(BatteryStatus {
        charging: true,
        percentage: 81,
      })
    );
  }
}