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
|
//! UPP protocol frame structures
use serde::{Deserialize, Serialize};
use std::mem;
/// UPP USB frame header (5 bytes)
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[repr(C, packed)]
pub struct UPPUsbFrame {
pub magic: u16, // 0xBBAA
pub cid: u8, // Camera ID
pub length: u16, // Data length (excluding header)
}
/// UPP camera frame header (7 bytes)
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
#[repr(C, packed)]
pub struct UPPFrameHeader {
pub frame_id: u8, // Frame ID
pub camera_number: u8, // Camera number
pub flags: UPPFlags, // Various flags
pub g_sensor: u32, // G-sensor data
}
/// UPP frame flags
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
#[repr(C, packed)]
pub struct UPPFlags {
pub has_g: bool, // Has G-sensor data (bit 0)
pub button_press: bool, // Button press detected (bit 1)
pub other: u8, // Other flags (bits 2-7, 6 bits total)
}
/// Complete UPP frame
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UPPFrame {
pub header: UPPFrameHeader,
pub data: Vec<u8>,
}
impl UPPUsbFrame {
/// Create a new UPP USB frame
pub fn new(cid: u8, length: u16) -> Self {
Self {
magic: super::UPP_USB_MAGIC,
cid,
length,
}
}
/// Get the total frame size including header
/// Based on C++ POC: length field includes camera header + data, but not USB header
pub fn total_size(&self) -> usize {
mem::size_of::<Self>() + self.length as usize
}
/// Validate the frame magic number
pub fn is_valid(&self) -> bool {
self.magic == super::UPP_USB_MAGIC
}
/// Get the expected payload size (camera header + data)
/// Based on C++ POC: this is what the length field represents
pub fn expected_payload_size(&self) -> usize {
self.length as usize
}
/// Get the expected data size (excluding camera header)
/// Camera header is 7 bytes, so data size is payload - 7
pub fn expected_data_size(&self) -> usize {
if self.length >= 7 {
self.length as usize - 7
} else {
0
}
}
}
impl UPPFrameHeader {
/// Create a new UPP frame header
pub fn new(
frame_id: u8,
camera_number: u8,
has_g: bool,
button_press: bool,
g_sensor: u32,
) -> Self {
Self {
frame_id,
camera_number,
flags: UPPFlags {
has_g,
button_press,
other: 0,
},
g_sensor,
}
}
/// Check if this frame has G-sensor data
pub fn has_g_sensor(&self) -> bool {
self.flags.has_g
}
/// Check if button press was detected
pub fn button_pressed(&self) -> bool {
self.flags.button_press
}
/// Get other flags
pub fn other_flags(&self) -> u8 {
self.flags.other
}
/// Set other flags
pub fn set_other_flags(&mut self, flags: u8) {
self.flags.other = flags & 0x3F; // Only 6 bits
}
/// Get G-sensor data
pub fn g_sensor_data(&self) -> Option<u32> {
if self.has_g_sensor() {
Some(self.g_sensor)
} else {
None
}
}
}
impl UPPFrame {
/// Create a new UPP frame
pub fn new(header: UPPFrameHeader, data: Vec<u8>) -> Self {
Self { header, data }
}
/// Get the total frame size
pub fn total_size(&self) -> usize {
mem::size_of::<UPPFrameHeader>() + self.data.len()
}
/// Get the frame ID
pub fn frame_id(&self) -> u8 {
self.header.frame_id
}
/// Get the camera number
pub fn camera_number(&self) -> u8 {
self.header.camera_number
}
/// Check if button was pressed
pub fn button_pressed(&self) -> bool {
self.header.button_pressed()
}
/// Get G-sensor data if available
pub fn g_sensor_data(&self) -> Option<u32> {
if self.header.has_g_sensor() {
Some(self.header.g_sensor)
} else {
None
}
}
}
impl Default for UPPUsbFrame {
fn default() -> Self {
Self {
magic: super::UPP_USB_MAGIC,
cid: super::UPP_CAMID_7,
length: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_upp_usb_frame_creation() {
let frame = UPPUsbFrame::new(7, 1024);
let magic = frame.magic;
let cid = frame.cid;
let length = frame.length;
assert_eq!(magic, super::super::UPP_USB_MAGIC);
assert_eq!(cid, 7);
assert_eq!(length, 1024);
assert!(frame.is_valid());
}
#[test]
fn test_upp_usb_frame_validation() {
let mut frame = UPPUsbFrame::new(7, 1024);
assert!(frame.is_valid());
frame.magic = 0x1234;
assert!(!frame.is_valid());
}
#[test]
fn test_upp_frame_header_creation() {
let header = UPPFrameHeader::new(1, 0, true, false, 12345);
assert_eq!(header.frame_id, 1);
assert_eq!(header.camera_number, 0);
assert!(header.has_g_sensor());
assert!(!header.button_pressed());
assert_eq!(header.g_sensor_data(), Some(12345));
}
#[test]
fn test_upp_frame_header_flags() {
let mut header = UPPFrameHeader::default();
assert!(!header.has_g_sensor());
assert!(!header.button_pressed());
header.flags.has_g = true;
header.flags.button_press = true;
assert!(header.has_g_sensor());
assert!(header.button_pressed());
}
#[test]
fn test_upp_frame_creation() {
let header = UPPFrameHeader::new(1, 0, false, false, 0);
let data = vec![1, 2, 3, 4, 5];
let frame = UPPFrame::new(header, data.clone());
assert_eq!(frame.frame_id(), 1);
assert_eq!(frame.camera_number(), 0);
assert_eq!(frame.data, data);
assert_eq!(frame.total_size(), mem::size_of::<UPPFrameHeader>() + 5);
}
#[test]
fn test_upp_frame_defaults() {
let frame = UPPFrame::default();
assert_eq!(frame.frame_id(), 0);
assert_eq!(frame.camera_number(), 0);
assert!(frame.data.is_empty());
assert!(!frame.button_pressed());
assert!(frame.g_sensor_data().is_none());
}
#[test]
fn test_upp_flags_other_bits() {
let mut header = UPPFrameHeader::default();
header.set_other_flags(0xFF);
assert_eq!(header.other_flags(), 0x3F); // Only 6 bits should be set
}
#[test]
fn test_memory_layout() {
// Ensure packed structs have correct sizes
assert_eq!(mem::size_of::<UPPUsbFrame>(), 5);
// UPPFrameHeader: frame_id(1) + camera_number(1) + flags(3) + g_sensor(4) = 9 bytes
assert_eq!(mem::size_of::<UPPFrameHeader>(), 9);
}
}
|