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
use crate::BLEConnDesc;

pub struct OnWriteArgs<'a> {
  pub(crate) current_data: &'a [u8],
  pub(crate) recv_data: &'a [u8],
  pub(crate) desc: &'a BLEConnDesc,
  pub(crate) reject: bool,
  pub(crate) error_code: u8,
  pub(crate) notify: bool,
}

impl OnWriteArgs<'_> {
  pub fn current_data(&self) -> &[u8] {
    self.current_data
  }

  pub fn recv_data(&self) -> &[u8] {
    self.recv_data
  }

  pub fn desc(&self) -> &BLEConnDesc {
    self.desc
  }

  /// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
  /// A write error (0xFF) is sent to the sender.
  pub fn reject(&mut self) {
    self.reject_with_error_code(0xFF);
  }

  /// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
  /// The argument error code is sent to the sender.
  pub fn reject_with_error_code(&mut self, error_code: u8) {
    self.reject = true;
    self.error_code = error_code;
  }

  pub fn notify(&mut self) {
    self.notify = true;
  }
}

pub struct OnWriteDescriptorArgs<'a> {
  pub(crate) current_data: &'a [u8],
  pub(crate) recv_data: &'a [u8],
  pub(crate) desc: &'a BLEConnDesc,
  pub(crate) reject: bool,
  pub(crate) error_code: u8,
}

impl OnWriteDescriptorArgs<'_> {
  pub fn current_data(&self) -> &[u8] {
    self.current_data
  }

  pub fn recv_data(&self) -> &[u8] {
    self.recv_data
  }

  pub fn desc(&self) -> &BLEConnDesc {
    self.desc
  }

  /// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
  /// A write error (0xFF) is sent to the sender.
  pub fn reject(&mut self) {
    self.reject_with_error_code(0xFF);
  }

  /// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
  /// The argument error code is sent to the sender.
  pub fn reject_with_error_code(&mut self, error_code: u8) {
    self.reject = true;
    self.error_code = error_code;
  }
}