CCSDS AOS (Advanced Orbiting Systems) Transfer Frame for satellite downlinks
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

aos: refactor decode into helpers (merlint E005)

Extract packed_to_header and check_fecf_value from the 65-line decode
function. Now 40 lines.

+43 -42
+43 -42
lib/aos.ml
··· 394 394 395 395 (* {1 Frame decode/encode via frame_codec} *) 396 396 397 + let packed_to_header pf = 398 + if pf.pf_version > 1 then Error (Invalid_version pf.pf_version) 399 + else 400 + match 401 + of_packed_header 402 + { 403 + version = pf.pf_version; 404 + scid = pf.pf_scid; 405 + vcid = pf.pf_vcid; 406 + vcfc = pf.pf_vcfc; 407 + replay_flag = pf.pf_replay_flag; 408 + vc_count_flag = pf.pf_vc_count_flag; 409 + spare = pf.pf_spare; 410 + vc_count_cycle = pf.pf_vc_count_cycle; 411 + } 412 + with 413 + | Error `Invalid_scid -> Error (Invalid_scid pf.pf_scid) 414 + | Error `Invalid_vcid -> Error (Invalid_vcid pf.pf_vcid) 415 + | Ok header -> Ok header 416 + 417 + let check_fecf_value buf frame_len fecf = 418 + match fecf with 419 + | Some actual -> 420 + let computed = compute_fecf (String.sub buf 0 (frame_len - 2)) in 421 + if computed <> actual then 422 + Error (Fecf_mismatch { expected = computed; actual }) 423 + else Ok () 424 + | None -> Ok () 425 + 397 426 let decode ?(frame_len = 0) ?(insert_zone_len = 0) ?(expect_ocf = true) 398 427 ?(expect_fecf = true) ?(check_fecf = true) buf = 399 428 let buf_len = String.length buf in ··· 417 446 else 418 447 match Wire.Codec.decode_with frame_codec env frame_buf 0 with 419 448 | Error _ -> Error (Truncated { need = frame_len; have = buf_len }) 420 - | Ok pf -> ( 421 - if pf.pf_version > 1 then Error (Invalid_version pf.pf_version) 422 - else 423 - match 424 - of_packed_header 425 - { 426 - version = pf.pf_version; 427 - scid = pf.pf_scid; 428 - vcid = pf.pf_vcid; 429 - vcfc = pf.pf_vcfc; 430 - replay_flag = pf.pf_replay_flag; 431 - vc_count_flag = pf.pf_vc_count_flag; 432 - spare = pf.pf_spare; 433 - vc_count_cycle = pf.pf_vc_count_cycle; 434 - } 435 - with 436 - | Error `Invalid_scid -> Error (Invalid_scid pf.pf_scid) 437 - | Error `Invalid_vcid -> Error (Invalid_vcid pf.pf_vcid) 438 - | Ok header -> 439 - let insert_zone = 440 - if insert_zone_len > 0 then Some pf.pf_insert_zone else None 441 - in 442 - let frame = 443 - { 444 - header; 445 - insert_zone; 446 - data = pf.pf_data; 447 - ocf = pf.pf_ocf; 448 - fecf = pf.pf_fecf; 449 - } 450 - in 451 - if expect_fecf && check_fecf then 452 - match pf.pf_fecf with 453 - | Some actual -> 454 - let computed = 455 - compute_fecf (String.sub buf 0 (frame_len - 2)) 456 - in 457 - if computed <> actual then 458 - Error (Fecf_mismatch { expected = computed; actual }) 459 - else Ok frame 460 - | None -> Ok frame 461 - else Ok frame) 449 + | Ok pf -> 450 + let ( let* ) = Result.bind in 451 + let* header = packed_to_header pf in 452 + let insert_zone = 453 + if insert_zone_len > 0 then Some pf.pf_insert_zone else None 454 + in 455 + let frame = 456 + { header; insert_zone; data = pf.pf_data; ocf = pf.pf_ocf; 457 + fecf = pf.pf_fecf } 458 + in 459 + if expect_fecf && check_fecf then 460 + let* () = check_fecf_value buf frame_len pf.pf_fecf in 461 + Ok frame 462 + else Ok frame 462 463 463 464 let encode_header buf off hdr = 464 465 let packed = to_packed_header hdr in