1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum BooleanOperation {
10 Union,
11 Intersection,
12 Difference,
13}
14
15#[path = "protocol_response.rs"]
16mod response;
17pub use response::{decode_response, KernelResponse, ProtocolError};
18
19pub const VERSION: u32 = 1;
20pub const MAX_PAYLOAD_BYTES: usize = 4096;
22pub const MAX_REQUEST_BYTES: usize = MAX_PAYLOAD_BYTES;
23pub const MAX_RESPONSE_BYTES: usize = MAX_PAYLOAD_BYTES;
24pub const MAX_MESH_CHUNK_SCALARS: u32 = 480;
25pub const MAX_SWEEP_CHUNK_BYTES: usize = 3840;
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub struct WireHandle {
31 pub session: u64,
32 pub slot: u32,
33 pub generation: u32,
34}
35
36impl WireHandle {
37 pub fn to_bytes(self) -> [u8; 16] {
38 let mut bytes = [0; 16];
39 bytes[..8].copy_from_slice(&self.session.to_le_bytes());
40 bytes[8..12].copy_from_slice(&self.slot.to_le_bytes());
41 bytes[12..].copy_from_slice(&self.generation.to_le_bytes());
42 bytes
43 }
44
45 pub fn from_bytes(bytes: &[u8]) -> Result<Self, ProtocolError> {
46 if bytes.len() != 16 {
47 return Err(ProtocolError::ResultSize);
48 }
49 Ok(Self {
50 session: u64::from_le_bytes(bytes[..8].try_into().expect("eight bytes")),
51 slot: u32::from_le_bytes(bytes[8..12].try_into().expect("four bytes")),
52 generation: u32::from_le_bytes(bytes[12..].try_into().expect("four bytes")),
53 })
54 }
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58pub enum MeshStream {
59 Positions,
60 Indices,
61 SourceFaces,
62 CornerNormals,
63}
64impl MeshStream {
65 pub fn code(self) -> u32 {
66 match self {
67 Self::Positions => 1,
68 Self::Indices => 2,
69 Self::SourceFaces => 3,
70 Self::CornerNormals => 4,
71 }
72 }
73 pub fn scalar_bytes(self) -> usize {
74 match self {
75 Self::Positions | Self::CornerNormals => 8,
76 Self::Indices | Self::SourceFaces => 4,
77 }
78 }
79}
80
81#[derive(Debug, Clone)]
84pub enum KernelCommand {
85 AbiVersion,
86 Capabilities,
88 SweepBegin {
90 session: u64,
91 format: u32,
92 length: u32,
93 loops: u32,
94 segments: u32,
95 },
96 SweepChunk {
98 session: u64,
99 upload: u64,
100 offset: u32,
101 bytes: Vec<u8>,
102 },
103 SweepCommit {
106 session: u64,
107 upload: u64,
108 },
109 CopyTransform {
111 session: u64,
112 solid: WireHandle,
113 column_major: [f64; 16],
114 },
115 CopyAffine {
118 session: u64,
119 solid: WireHandle,
120 column_major: [f64; 16],
121 },
122 CreateSession,
123 DestroySession {
124 session: u64,
125 },
126 Box {
127 session: u64,
128 origin: [f64; 3],
129 size: [f64; 3],
130 },
131 Cylinder {
132 session: u64,
133 origin: [f64; 3],
134 radius: f64,
135 height: f64,
136 },
137 Boolean {
138 session: u64,
139 left: WireHandle,
140 right: WireHandle,
141 operation: BooleanOperation,
142 },
143 Volume {
144 session: u64,
145 solid: WireHandle,
146 relative_accuracy: f64,
147 },
148 Manifold {
149 session: u64,
150 solid: WireHandle,
151 },
152 IsEmpty {
155 session: u64,
156 solid: WireHandle,
157 },
158 Release {
159 session: u64,
160 object: WireHandle,
161 },
162 Tessellate {
163 session: u64,
164 solid: WireHandle,
165 chord_metres: f64,
166 angle_degrees: f64,
167 },
168 MeshInfo {
169 session: u64,
170 mesh: WireHandle,
171 },
172 MeshRead {
173 session: u64,
174 mesh: WireHandle,
175 stream: MeshStream,
176 offset: u32,
177 count: u32,
178 },
179}
180
181pub fn encode_request(request_id: u32, command: &KernelCommand) -> Result<Vec<u8>, ProtocolError> {
184 let mut out = Vec::with_capacity(96);
185 out.extend(VERSION.to_le_bytes());
186 out.extend(request_id.to_le_bytes());
187 let opcode: u32 = match command {
188 KernelCommand::AbiVersion => 0,
189 KernelCommand::Capabilities => 12,
190 KernelCommand::SweepBegin { .. } => 13,
191 KernelCommand::SweepChunk { .. } => 14,
192 KernelCommand::SweepCommit { .. } => 15,
193 KernelCommand::CopyTransform { .. } => 17,
194 KernelCommand::CopyAffine { .. } => 18,
195 KernelCommand::CreateSession => 1,
196 KernelCommand::DestroySession { .. } => 2,
197 KernelCommand::Box { .. } => 3,
198 KernelCommand::Cylinder { .. } => 4,
199 KernelCommand::Boolean { .. } => 5,
200 KernelCommand::Volume { .. } => 6,
201 KernelCommand::Manifold { .. } => 7,
202 KernelCommand::IsEmpty { .. } => 19,
203 KernelCommand::Release { .. } => 8,
204 KernelCommand::Tessellate { .. } => 9,
205 KernelCommand::MeshInfo { .. } => 10,
206 KernelCommand::MeshRead { .. } => 11,
207 };
208 out.extend(opcode.to_le_bytes());
209 encode_arguments(command, &mut out)?;
210 if out.len() > MAX_REQUEST_BYTES {
211 return Err(ProtocolError::PayloadSize);
212 }
213 Ok(out)
214}
215
216fn encode_arguments(command: &KernelCommand, out: &mut Vec<u8>) -> Result<(), ProtocolError> {
217 use KernelCommand::*;
218 match command {
219 AbiVersion | Capabilities | CreateSession => {}
220 SweepBegin {
221 session,
222 format,
223 length,
224 loops,
225 segments,
226 } => {
227 out.extend(session.to_le_bytes());
228 for value in [format, length, loops, segments] {
229 out.extend(value.to_le_bytes());
230 }
231 }
232 SweepChunk {
233 session,
234 upload,
235 offset,
236 bytes,
237 } => {
238 let length = u32::try_from(bytes.len()).map_err(|_| ProtocolError::PayloadSize)?;
239 if bytes.is_empty()
240 || bytes.len() > MAX_SWEEP_CHUNK_BYTES
241 || offset.checked_add(length).is_none()
242 {
243 return Err(ProtocolError::PayloadSize);
244 }
245 out.extend(session.to_le_bytes());
246 out.extend(upload.to_le_bytes());
247 out.extend(offset.to_le_bytes());
248 out.extend(length.to_le_bytes());
249 out.extend(bytes);
250 }
251 SweepCommit { session, upload } => {
252 out.extend(session.to_le_bytes());
253 out.extend(upload.to_le_bytes());
254 }
255 CopyTransform {
256 session,
257 solid,
258 column_major,
259 }
260 | CopyAffine {
261 session,
262 solid,
263 column_major,
264 } => {
265 write_solid(out, *session, *solid);
266 for value in column_major {
267 out.extend(value.to_le_bytes());
268 }
269 }
270 DestroySession { session } => out.extend(session.to_le_bytes()),
271 Box {
272 session,
273 origin,
274 size,
275 } => {
276 out.extend(session.to_le_bytes());
277 for value in origin.iter().chain(size) {
278 out.extend(value.to_le_bytes());
279 }
280 }
281 Cylinder {
282 session,
283 origin,
284 radius,
285 height,
286 } => {
287 out.extend(session.to_le_bytes());
288 for value in origin.iter().chain([radius, height]) {
289 out.extend(value.to_le_bytes());
290 }
291 }
292 Boolean {
293 session,
294 left,
295 right,
296 operation,
297 } => {
298 out.extend(session.to_le_bytes());
299 out.extend(left.to_bytes());
300 out.extend(right.to_bytes());
301 let code: u32 = match operation {
302 BooleanOperation::Union => 1,
303 BooleanOperation::Intersection => 2,
304 BooleanOperation::Difference => 3,
305 };
306 out.extend(code.to_le_bytes());
307 }
308 Volume {
309 session,
310 solid,
311 relative_accuracy,
312 } => {
313 write_solid(out, *session, *solid);
314 out.extend(relative_accuracy.to_le_bytes());
315 }
316 Manifold { session, solid } | IsEmpty { session, solid } => {
317 write_solid(out, *session, *solid)
318 }
319 Release { session, object } => write_solid(out, *session, *object),
320 Tessellate {
321 session,
322 solid,
323 chord_metres,
324 angle_degrees,
325 } => {
326 write_solid(out, *session, *solid);
327 out.extend(chord_metres.to_le_bytes());
328 out.extend(angle_degrees.to_le_bytes());
329 }
330 MeshInfo { session, mesh } => write_solid(out, *session, *mesh),
331 MeshRead {
332 session,
333 mesh,
334 stream,
335 offset,
336 count,
337 } => {
338 if *count > MAX_MESH_CHUNK_SCALARS || offset.checked_add(*count).is_none() {
339 return Err(ProtocolError::MeshChunkSize);
340 }
341 write_solid(out, *session, *mesh);
342 out.extend(stream.code().to_le_bytes());
343 out.extend(offset.to_le_bytes());
344 out.extend(count.to_le_bytes());
345 }
346 }
347 Ok(())
348}
349
350fn write_solid(out: &mut Vec<u8>, session: u64, solid: WireHandle) {
351 out.extend(session.to_le_bytes());
352 out.extend(solid.to_bytes());
353}
354
355#[cfg(test)]
356#[path = "protocol_tests.rs"]
357mod tests;