midi-controller 0.3.0

Generic MIDI controller engine: button/encoder input processing, LED rendering, preset management, and MIDI-CI Property Exchange framing. #![no_std] compatible.
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Minimal MIDI-CI Property Exchange (PE) framing.
//!
//! Handles Set Property Inquiry (sub-ID2 = 0x36) and replies with ACK (0x37).
//! Spec reference: MIDI-CI 1.2, §7 Property Exchange.
//!
//! SysEx7 message layout:
//!   F0 7E <device_id> 0D <sub_id2> <ci_version>
//!   <source_muid: 4 bytes> <dest_muid: 4 bytes>
//!   <request_id>
//!   <header_len_lo> <header_len_hi>
//!   <header_data...>
//!   <num_chunks_lo> <num_chunks_hi>
//!   <chunk_num_lo> <chunk_num_hi>
//!   <body_len_lo> <body_len_hi>
//!   <body_data...>
//!   F7
//!
//! Reply header format: `[status]` (1 byte for Set Reply) or `[status, resource]` (2 bytes for Get Reply).

use heapless::Vec;

const UNIVERSAL_SYSEX: u8 = 0x7E;
const SUB_ID1_MIDI_CI: u8 = 0x0D;

/// Sub-ID2: Inquiry Get Property Data (MIDI-CI 1.2)
pub const PE_GET_INQUIRY: u8 = 0x34;
/// Sub-ID2: Reply to Get Property Data
pub const PE_GET_REPLY: u8 = 0x35;
/// Sub-ID2: Inquiry Set Property Data (MIDI-CI 1.2)
pub const PE_SET_INQUIRY: u8 = 0x36;
/// Sub-ID2: Reply to Set Property Data
pub const PE_SET_REPLY: u8 = 0x37;

/// PE reply status codes (HTTP-inspired).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum PeStatus {
    /// 200 OK — request succeeded.
    Ok = 0x00,
    /// 404 Not Found — resource (preset index) does not exist.
    NotFound = 0x01,
    /// 422 Unprocessable — body could not be deserialized.
    FormatError = 0x02,
    /// 409 Conflict — flash format version mismatch, re-upload required.
    VersionMismatch = 0x03,
}

impl PeStatus {
    pub fn from_byte(b: u8) -> Option<Self> {
        match b {
            0x00 => Some(Self::Ok),
            0x01 => Some(Self::NotFound),
            0x02 => Some(Self::FormatError),
            0x03 => Some(Self::VersionMismatch),
            _ => None,
        }
    }

    pub fn is_ok(self) -> bool {
        self == Self::Ok
    }
}

const CI_VERSION: u8 = 0x02;

/// Check if a SysEx buffer is a MIDI-CI message.
pub fn is_ci_message(buf: &[u8]) -> bool {
    buf.len() >= 15 && buf[0] == 0xF0 && buf[1] == UNIVERSAL_SYSEX && buf[3] == SUB_ID1_MIDI_CI
}

/// Check if the message is a Set Property Inquiry.
pub fn is_set_property(buf: &[u8]) -> bool {
    is_ci_message(buf) && buf[4] == PE_SET_INQUIRY
}

/// Extract the request_id field.
pub fn request_id(buf: &[u8]) -> u8 {
    buf[14]
}

/// Extract the source MUID (4 bytes).
pub fn source_muid(buf: &[u8]) -> [u8; 4] {
    [buf[6], buf[7], buf[8], buf[9]]
}

/// Parsed Set Property Inquiry with resource identifier and body.
pub struct SetPropertyData<'a> {
    /// Resource identifier (preset index) from header.
    pub resource: u8,
    /// Body payload.
    pub body: &'a [u8],
}

/// Extract resource identifier and body from a Set Property Inquiry.
pub fn extract_set_property(buf: &[u8]) -> Option<SetPropertyData<'_>> {
    if !is_set_property(buf) {
        return None;
    }
    if buf.len() < 16 {
        return None;
    }

    let mut pos = 15;

    // header_len (2 bytes, 7-bit LSB encoding)
    if pos + 2 > buf.len() {
        return None;
    }
    let header_len = (buf[pos] as usize) | ((buf[pos + 1] as usize) << 7);
    pos += 2;

    // resource = first header byte (preset index), or 0 if no header
    let resource = if header_len > 0 && pos < buf.len() {
        buf[pos]
    } else {
        0
    };
    pos += header_len;

    // num_chunks + chunk_num (4 bytes)
    if pos + 4 > buf.len() {
        return None;
    }
    pos += 4;

    // body_len (2 bytes, 7-bit LSB encoding)
    if pos + 2 > buf.len() {
        return None;
    }
    let body_len = (buf[pos] as usize) | ((buf[pos + 1] as usize) << 7);
    pos += 2;

    if pos + body_len > buf.len() {
        return None;
    }
    Some(SetPropertyData {
        resource,
        body: &buf[pos..pos + body_len],
    })
}

/// Legacy helper: extract just the body (ignores resource).
pub fn extract_body(buf: &[u8]) -> Option<&[u8]> {
    extract_set_property(buf).map(|d| d.body)
}

/// Build a Set Property Reply with status.
pub fn build_set_reply(
    device_muid: [u8; 4],
    dest_muid: [u8; 4],
    req_id: u8,
    status: PeStatus,
) -> Vec<u8, 32> {
    let mut msg: Vec<u8, 32> = Vec::new();
    let _ = msg.push(0xF0);
    let _ = msg.push(UNIVERSAL_SYSEX);
    let _ = msg.push(0x7F); // device_id: function block
    let _ = msg.push(SUB_ID1_MIDI_CI);
    let _ = msg.push(PE_SET_REPLY);
    let _ = msg.push(CI_VERSION);
    for &b in &device_muid {
        let _ = msg.push(b);
    }
    for &b in &dest_muid {
        let _ = msg.push(b);
    }
    let _ = msg.push(req_id);
    // header_len = 1 (status byte)
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(status as u8);
    // num_chunks = 1, chunk_num = 1
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    // body_len = 0
    let _ = msg.push(0x00);
    let _ = msg.push(0x00);
    let _ = msg.push(0xF7);
    msg
}

/// Build a Set Property Inquiry message (CLI → device).
/// `resource` is the preset index (carried in header).
/// `body` must contain only 7-bit safe bytes.
pub fn build_set_inquiry(
    source_muid: [u8; 4],
    dest_muid: [u8; 4],
    req_id: u8,
    resource: u8,
    body: &[u8],
) -> Vec<u8, 350> {
    let mut msg: Vec<u8, 350> = Vec::new();
    let _ = msg.push(0xF0);
    let _ = msg.push(UNIVERSAL_SYSEX);
    let _ = msg.push(0x7F);
    let _ = msg.push(SUB_ID1_MIDI_CI);
    let _ = msg.push(PE_SET_INQUIRY);
    let _ = msg.push(CI_VERSION);
    for &b in &source_muid {
        let _ = msg.push(b);
    }
    for &b in &dest_muid {
        let _ = msg.push(b);
    }
    let _ = msg.push(req_id);
    // header_len = 1 (resource byte)
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(resource);
    // num_chunks = 1, chunk_num = 1
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    // body (mcoded7 encoded)
    let mut encoded_body = [0u8; 300];
    let enc_len = encode_mcoded7(body, &mut encoded_body);
    let _ = msg.push((enc_len & 0x7F) as u8);
    let _ = msg.push(((enc_len >> 7) & 0x7F) as u8);
    for &b in &encoded_body[..enc_len] {
        let _ = msg.push(b);
    }
    let _ = msg.push(0xF7);
    msg
}

/// Check if the message is a Get Property Inquiry.
pub fn is_get_property(buf: &[u8]) -> bool {
    is_ci_message(buf) && buf[4] == PE_GET_INQUIRY
}

/// Check if the message is a Get Property Reply.
pub fn is_get_reply(buf: &[u8]) -> bool {
    is_ci_message(buf) && buf[4] == PE_GET_REPLY
}

/// Extract status from a Set Property Reply or Get Property Reply.
/// Status is the first header byte in replies.
pub fn extract_reply_status(buf: &[u8]) -> Option<PeStatus> {
    if buf.len() < 16 || !is_ci_message(buf) {
        return None;
    }
    let sub_id2 = buf[4];
    if sub_id2 != PE_SET_REPLY && sub_id2 != PE_GET_REPLY {
        return None;
    }
    let pos = 15;
    if pos + 2 > buf.len() {
        return None;
    }
    let header_len = (buf[pos] as usize) | ((buf[pos + 1] as usize) << 7);
    if header_len == 0 {
        // Legacy reply without status — treat as Ok
        return Some(PeStatus::Ok);
    }
    if pos + 2 + 1 > buf.len() {
        return None;
    }
    PeStatus::from_byte(buf[pos + 2])
}

/// Extract body from a Get Property Reply.
pub fn extract_get_body(buf: &[u8]) -> Option<&[u8]> {
    if !is_get_reply(buf) || buf.len() < 16 {
        return None;
    }
    let mut pos = 15;
    if pos + 2 > buf.len() {
        return None;
    }
    let header_len = (buf[pos] as usize) | ((buf[pos + 1] as usize) << 7);
    pos += 2 + header_len;
    // num_chunks + chunk_num
    if pos + 4 > buf.len() {
        return None;
    }
    pos += 4;
    // body_len
    if pos + 2 > buf.len() {
        return None;
    }
    let body_len = (buf[pos] as usize) | ((buf[pos + 1] as usize) << 7);
    pos += 2;
    if pos + body_len > buf.len() {
        return None;
    }
    Some(&buf[pos..pos + body_len])
}

/// Extract the resource identifier from a Get Property Inquiry.
pub fn extract_get_resource(buf: &[u8]) -> Option<u8> {
    if !is_get_property(buf) || buf.len() < 16 {
        return None;
    }
    let pos = 15;
    if pos + 2 > buf.len() {
        return None;
    }
    let header_len = (buf[pos] as usize) | ((buf[pos + 1] as usize) << 7);
    if header_len > 0 && pos + 2 < buf.len() {
        Some(buf[pos + 2])
    } else {
        Some(0)
    }
}

/// Build a Get Property Inquiry message (CLI → device).
pub fn build_get_inquiry(
    source_muid: [u8; 4],
    dest_muid: [u8; 4],
    req_id: u8,
    resource: u8,
) -> Vec<u8, 32> {
    let mut msg: Vec<u8, 32> = Vec::new();
    let _ = msg.push(0xF0);
    let _ = msg.push(UNIVERSAL_SYSEX);
    let _ = msg.push(0x7F);
    let _ = msg.push(SUB_ID1_MIDI_CI);
    let _ = msg.push(PE_GET_INQUIRY);
    let _ = msg.push(CI_VERSION);
    for &b in &source_muid {
        let _ = msg.push(b);
    }
    for &b in &dest_muid {
        let _ = msg.push(b);
    }
    let _ = msg.push(req_id);
    // header_len = 1 (resource byte)
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(resource);
    // num_chunks = 1, chunk_num = 1
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    // body_len = 0
    let _ = msg.push(0x00);
    let _ = msg.push(0x00);
    let _ = msg.push(0xF7);
    msg
}

/// Build a Get Property Reply with status and body data.
pub fn build_get_reply(
    device_muid: [u8; 4],
    dest_muid: [u8; 4],
    req_id: u8,
    resource: u8,
    status: PeStatus,
    body: &[u8],
) -> Vec<u8, 350> {
    let mut msg: Vec<u8, 350> = Vec::new();
    let _ = msg.push(0xF0);
    let _ = msg.push(UNIVERSAL_SYSEX);
    let _ = msg.push(0x7F);
    let _ = msg.push(SUB_ID1_MIDI_CI);
    let _ = msg.push(PE_GET_REPLY);
    let _ = msg.push(CI_VERSION);
    for &b in &device_muid {
        let _ = msg.push(b);
    }
    for &b in &dest_muid {
        let _ = msg.push(b);
    }
    let _ = msg.push(req_id);
    // header_len = 2 (status + resource)
    let _ = msg.push(0x02);
    let _ = msg.push(0x00);
    let _ = msg.push(status as u8);
    let _ = msg.push(resource);
    // num_chunks = 1, chunk_num = 1
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    let _ = msg.push(0x01);
    let _ = msg.push(0x00);
    // body (mcoded7 encoded)
    let mut encoded_body = [0u8; 300];
    let enc_len = encode_mcoded7(body, &mut encoded_body);
    let _ = msg.push((enc_len & 0x7F) as u8);
    let _ = msg.push(((enc_len >> 7) & 0x7F) as u8);
    for &b in &encoded_body[..enc_len] {
        let _ = msg.push(b);
    }
    let _ = msg.push(0xF7);
    msg
}

/// Encode data using mcoded7 (MIDI-CI spec): every 7 input bytes → 8 output bytes.
/// The first byte of each group carries the high bits (bit 7) of the following 7 bytes.
/// Returns the number of bytes written to `out`.
pub fn encode_mcoded7(data: &[u8], out: &mut [u8]) -> usize {
    let mut pos = 0;
    for chunk in data.chunks(7) {
        let mut high_bits: u8 = 0;
        for (i, &b) in chunk.iter().enumerate() {
            if b & 0x80 != 0 {
                high_bits |= 1 << i;
            }
        }
        out[pos] = high_bits;
        pos += 1;
        for &b in chunk {
            out[pos] = b & 0x7F;
            pos += 1;
        }
    }
    pos
}

/// Decode mcoded7 data back to original bytes.
/// Returns the number of bytes written to `out`.
pub fn decode_mcoded7(data: &[u8], out: &mut [u8]) -> usize {
    let mut pos = 0;
    let mut i = 0;
    while i < data.len() {
        let high_bits = data[i];
        i += 1;
        for bit in 0..7 {
            if i >= data.len() {
                break;
            }
            out[pos] = data[i] | (if high_bits & (1 << bit) != 0 { 0x80 } else { 0 });
            pos += 1;
            i += 1;
        }
    }
    pos
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_set_property() {
        let payload = b"hello";
        let msg = build_set_inquiry(
            [0x10, 0x20, 0x30, 0x40],
            [0x01, 0x02, 0x03, 0x04],
            0x07,
            3,
            payload,
        );

        assert!(is_ci_message(&msg));
        assert!(is_set_property(&msg));
        assert_eq!(request_id(&msg), 0x07);
        assert_eq!(source_muid(&msg), [0x10, 0x20, 0x30, 0x40]);

        let data = extract_set_property(&msg).unwrap();
        assert_eq!(data.resource, 3);
        let mut decoded = [0u8; 32];
        let dec_len = decode_mcoded7(data.body, &mut decoded);
        assert_eq!(&decoded[..dec_len], b"hello");
    }

    #[test]
    fn not_ci_for_opendeck() {
        let buf = [0xF0, 0x00, 0x53, 0x43, 0x00, 0x00, 0x01, 0xF7];
        assert!(!is_ci_message(&buf));
    }

    #[test]
    fn reply_structure() {
        let reply = build_set_reply(
            [0x01, 0x02, 0x03, 0x04],
            [0x10, 0x20, 0x30, 0x40],
            0x07,
            PeStatus::Ok,
        );
        assert_eq!(reply[0], 0xF0);
        assert_eq!(reply[4], PE_SET_REPLY);
        assert_eq!(reply[14], 0x07);
        // header_len = 1, status = 0x00 (Ok)
        assert_eq!(reply[15], 0x01); // header_len_lo
        assert_eq!(reply[17], 0x00); // status byte
        assert_eq!(*reply.last().unwrap(), 0xF7);
        assert_eq!(extract_reply_status(&reply), Some(PeStatus::Ok));
    }

    #[test]
    fn reply_status_not_found() {
        let reply = build_set_reply(
            [0x01, 0x02, 0x03, 0x04],
            [0x10, 0x20, 0x30, 0x40],
            0x01,
            PeStatus::NotFound,
        );
        assert_eq!(extract_reply_status(&reply), Some(PeStatus::NotFound));
    }

    #[test]
    fn get_reply_status_extracted() {
        let reply = build_get_reply(
            [0x01, 0x02, 0x03, 0x04],
            [0x10, 0x20, 0x30, 0x40],
            0x01,
            5,
            PeStatus::NotFound,
            &[],
        );
        assert_eq!(extract_reply_status(&reply), Some(PeStatus::NotFound));
    }

    #[test]
    fn extract_body_with_header() {
        let mut msg: Vec<u8, 128> = Vec::new();
        let _ = msg.push(0xF0);
        let _ = msg.push(UNIVERSAL_SYSEX);
        let _ = msg.push(0x7F);
        let _ = msg.push(SUB_ID1_MIDI_CI);
        let _ = msg.push(PE_SET_INQUIRY);
        let _ = msg.push(CI_VERSION);
        for &b in &[0x10, 0x20, 0x30, 0x40, 0x01, 0x02, 0x03, 0x04] {
            let _ = msg.push(b);
        }
        let _ = msg.push(0x01); // request_id
                                // header_len = 3
        let _ = msg.push(0x03);
        let _ = msg.push(0x00);
        for &b in b"abc" {
            let _ = msg.push(b);
        }
        // chunks
        let _ = msg.push(0x01);
        let _ = msg.push(0x00);
        let _ = msg.push(0x01);
        let _ = msg.push(0x00);
        // body_len = 4
        let _ = msg.push(0x04);
        let _ = msg.push(0x00);
        for &b in b"data" {
            let _ = msg.push(b);
        }
        let _ = msg.push(0xF7);

        assert_eq!(extract_body(&msg).unwrap(), b"data");
    }

    #[test]
    fn get_property_roundtrip() {
        let inquiry =
            build_get_inquiry([0x10, 0x20, 0x30, 0x40], [0x01, 0x02, 0x03, 0x04], 0x05, 7);
        assert!(is_get_property(&inquiry));
        assert!(!is_set_property(&inquiry));
        assert_eq!(extract_get_resource(&inquiry), Some(7));
        assert_eq!(request_id(&inquiry), 0x05);
    }

    #[test]
    fn get_reply_body_extraction() {
        let body = b"preset data here";
        let reply = build_get_reply(
            [0x01, 0x02, 0x03, 0x04],
            [0x10, 0x20, 0x30, 0x40],
            0x03,
            2,
            PeStatus::Ok,
            body,
        );
        assert!(is_get_reply(&reply));
        let raw = extract_get_body(&reply).unwrap();
        let mut decoded = [0u8; 32];
        let dec_len = decode_mcoded7(raw, &mut decoded);
        assert_eq!(&decoded[..dec_len], body);
    }

    #[test]
    fn mcoded7_roundtrip_short() {
        let data = [0x80, 0xFF, 0x00, 0x7F];
        let mut encoded = [0u8; 256];
        let enc_len = encode_mcoded7(&data, &mut encoded);
        let mut decoded = [0u8; 256];
        let dec_len = decode_mcoded7(&encoded[..enc_len], &mut decoded);
        assert_eq!(&decoded[..dec_len], &data);
    }

    #[test]
    fn mcoded7_roundtrip_exact_7() {
        let data = [0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86];
        let mut encoded = [0u8; 256];
        let enc_len = encode_mcoded7(&data, &mut encoded);
        assert_eq!(enc_len, 8); // 7 input → 8 output
        let mut decoded = [0u8; 256];
        let dec_len = decode_mcoded7(&encoded[..enc_len], &mut decoded);
        assert_eq!(&decoded[..dec_len], &data);
    }

    #[test]
    fn mcoded7_roundtrip_14_bytes() {
        let data = [0xFF; 14];
        let mut encoded = [0u8; 256];
        let enc_len = encode_mcoded7(&data, &mut encoded);
        assert_eq!(enc_len, 16); // 14 → 2 groups of 8
        let mut decoded = [0u8; 256];
        let dec_len = decode_mcoded7(&encoded[..enc_len], &mut decoded);
        assert_eq!(&decoded[..dec_len], &data);
    }

    #[test]
    fn mcoded7_all_7bit_safe_is_passthrough_with_prefix() {
        let data = [0x01, 0x7F, 0x00, 0x55];
        let mut encoded = [0u8; 256];
        let enc_len = encode_mcoded7(&data, &mut encoded);
        // High bits byte should be 0 (no high bits set)
        assert_eq!(encoded[0], 0x00);
        assert_eq!(&encoded[1..5], &data);
        assert_eq!(enc_len, 5);
    }

    #[test]
    fn mcoded7_custom_rgb_color() {
        // Simulate a postcard-serialized Custom(255, 128, 0)
        let data = [0xFF, 0x80, 0x00];
        let mut encoded = [0u8; 256];
        let enc_len = encode_mcoded7(&data, &mut encoded);
        let mut decoded = [0u8; 256];
        let dec_len = decode_mcoded7(&encoded[..enc_len], &mut decoded);
        assert_eq!(&decoded[..dec_len], &data);
    }
}