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
//! MIDI Capability Inquiry defines system exclusive messages for discovering other devices
//! and managing their connections.
use crate::muid::MUID;

/// Metadata required for MIDI CI Discovery
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct DeviceDiscovery {
    /// The [MUID] of the device
    pub muid: MUID,
    /// A device manufacturer identifier
    pub manufacturer: [u8; 3],
    /// A device family identifier
    pub family: [u8; 2],
    /// A device model identifier
    pub model: [u8; 2],
    /// The software/firmware/hardware revision number
    pub revision: u32,
    /// Bitflags to notate CI support
    pub ci_support: u8,
    /// Maximum size of a sysex message the device can receive. At least 128 bytes.
    pub max_sysex_size: u32,
}

/// Used to handle MUID collisions.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct InvalidateMUID {
    /// The target MUID to invalidate.
    pub target: MUID,
}

/// Default response when receiving a message not understood
///
/// Used if:
/// - Received a CI message the device does not understand
/// - Received a CI message with unsupported MIDI CI version
/// - Received a malformed CI message
/// - Received a profile enable/disable message for a profile that the device does not support
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct NotAcknowledged;

/// Single byte representing the MIDI version used by a protocol
#[repr(u8)]
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum MidiVersion {
    /// MIDI version 1.
    Midi1 = 0x01,

    /// MIDI version 2.
    Midi2 = 0x02,
}

/// 3 bytes identifying a protocol used in protocol negotation
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct Protocol {
    /// The MIDI version of the protocol to use.
    pub midi_version: MidiVersion,

    /// The version of the protocol to use.
    pub version: u8,

    /// Any extensions of the protocol to use.
    pub extensions: u8,
}

impl Protocol {
    /// Default midi1 protocol
    pub fn midi1() -> Self {
        Self::new(MidiVersion::Midi1)
    }

    /// Default midi2 protocol
    pub fn midi2() -> Self {
        Self::default()
    }

    /// Create a new protocol
    pub fn new(mv: MidiVersion) -> Self {
        Self {
            midi_version: mv,
            version: 0,
            extensions: 0,
        }
    }

    /// Notate the device supports the jitter reduction extension
    pub fn with_jitter_reduction(mut self) -> Self {
        self.extensions |= 0b0000_00001;
        self
    }

    /// Notate the device supports UMPs larger than 64 bits. Only applicable for MIDI 1.
    pub fn with_large_packets(mut self) -> Self {
        debug_assert_eq!(self.midi_version, MidiVersion::Midi1);
        self.extensions |= 0b0000_0010;
        self
    }

    /// Add an additional version number. NOTE: is not MIDI 1 vs MIDI 2.
    pub fn with_additional_version(mut self, vers: u8) -> Self {
        self.version = vers;
        self
    }
}

impl Default for Protocol {
    fn default() -> Self {
        Self::new(MidiVersion::Midi2)
    }
}

/// Message to begin initializing protocol negotation. The spec allows for multiple protocls
/// to be supported, this crate only supports a single preferred protocol until const generics
/// are stabilized, as Rust does not support VLA members in structs.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct InitiateProtocolNegotiation {
    /// The authority to use.
    pub authority: u8,

    /// Which version of the protocol to request.
    pub preferred: Protocol,
}

impl InitiateProtocolNegotiation {
    /// Construct a new protocol negotation message
    pub fn new(auth: AuthorityLevel) -> Self {
        Self {
            authority: auth as u8,
            preferred: Protocol::default(),
        }
    }

    /// annotate additional authority level. Must be less than 16
    pub fn with_additional_authority(mut self, a: u8) -> Self {
        debug_assert!(a < 16);
        self.authority |= a;
        self
    }

    /// annotate a different preferred protocol
    pub fn with_preferred_protocol(mut self, p: Protocol) -> Self {
        self.preferred = p;
        self
    }
}

impl DeviceDiscovery {
    /// Construct a new device with a MUID. This should be sufficiently
    /// unique and does not need to be persistent.
    pub fn new(muid: MUID) -> Self {
        Self {
            muid,
            manufacturer: [0; 3],
            family: [0; 2],
            model: [0; 2],
            revision: 0,
            ci_support: 0,
            max_sysex_size: 128,
        }
    }

    /// Add a unique manufacturer code.
    pub fn with_manufacturer_code(mut self, manu: [u8; 3]) -> Self {
        self.manufacturer = manu;
        self
    }

    /// Add a device family code
    pub fn with_family_code(mut self, family: [u8; 2]) -> Self {
        self.family = family;
        self
    }

    /// Add a device model code
    pub fn with_model_code(mut self, model: [u8; 2]) -> Self {
        self.model = model;
        self
    }

    /// Add a software/firmware/hardware revision number
    pub fn with_revision(mut self, revision: u32) -> Self {
        self.revision = revision;
        self
    }

    /// Define the maximum length of sysex message the device may
    /// receive. Must be at least 128.
    pub fn with_max_sysex_length(mut self, len: u32) -> Self {
        debug_assert!(len >= 128);
        self.max_sysex_size = len;
        self
    }

    /// Notate the device supports protocol negotiation
    pub fn with_protocol_negotiation(mut self) -> Self {
        self.ci_support |= 0b0000_0010;
        self
    }

    /// Notate the device supports profile configuration.
    pub fn with_profile_configuration(mut self) -> Self {
        self.ci_support |= 0b0000_0100;
        self
    }

    /// Notate the device supports property exchange.
    pub fn with_property_exchange(mut self) -> Self {
        self.ci_support |= 0b0000_1000;
        self
    }
}

/// Sent when a device requests a new protocol for communication
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub struct SetNewProtocol {
    /// The authority to use.
    pub authority: u8,

    /// Which protocol to set.
    pub protocol: Protocol,
}

impl SetNewProtocol {
    /// Construct a new SetNewProtocol message
    pub fn new(p: Protocol, a: AuthorityLevel) -> Self {
        Self {
            protocol: p,
            authority: a as u8,
        }
    }
    /// Notate additional authority for the request. Must be less than 16
    pub fn with_additional_authority(mut self, a: u8) -> Self {
        debug_assert!(a < 16);
        self.authority |= a;
        self
    }
}

/// Helper trait for capability inquiry messages.
pub trait CapabilityInquiryMessage {
    /// The authority level of the message
    fn authority(&self) -> AuthorityLevel {
        AuthorityLevel::ReservedLower
    }

    /// The category of a CI message
    // fn category(&self) -> Category;

    /// The subcategory (type) of CI message
    fn subcategory(&self) -> u8;

    /// Source of the message
    fn source(&self) -> MUID;

    /// Destination address of the message
    fn dest(&self) -> MUID;

    /// Used for targeting a specific MIDI channel on a device, value 0x7f corresponds
    /// to the entire MIDI port.
    fn device_id(&self) -> u8 {
        0x7f
    }

    /// The data contents of the message.
    fn data(&self) -> &'_ [u8];
}

/// Represents the various management authority levels required by some MIDI specifications.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum AuthorityLevel {
    /// Reserved for future use.
    ReservedUpper = 0x70,

    /// Owner of many devices, eg a PC or router
    NodeServer = 0x60,

    /// TODO
    Gateway = 0x50,

    /// A MIDI device that translates between protocols.
    Translator = 0x40,

    /// A distinct MIDI endpoint.
    Endpoint = 0x30,

    /// Processors like arpeggiators, sequencers, etc
    EventProcessor = 0x20,

    /// TODO
    Transport = 0x10,

    /// Reserved for future use
    ReservedLower = 0x00,
}