Something went wrong. Try again.
Bundle Protocol Version 7 (RFC 9171)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
module V = Cbor.Value
type status_item = Not_asserted | Asserted of int64 option
type status_report = { received : status_item; forwarded : status_item; delivered : status_item; deleted : status_item; reason : int; source : Message.eid; creation_timestamp : Message.timestamp; fragment : (int64 * int64) option;}
let status_report_type = 1
type t = | Status_report of status_report | Record of { type_code : int; content : Cbor.Value.t }
(* {1 Encoding} *)
let cbor_of_item = function | Not_asserted -> V.Array [ V.Bool false ] | Asserted None -> V.Array [ V.Bool true ] | Asserted (Some time) -> V.Array [ V.Bool true; V.int64 time ]
let cbor_of_status_report r = let info = V.Array (List.map cbor_of_item [ r.received; r.forwarded; r.delivered; r.deleted ]) in let fragment = match r.fragment with | None -> [] | Some (offset, length) -> [ V.int64 offset; V.int64 length ] in V.Array ([ info; V.int r.reason; Message.cbor_of_eid r.source; Message.cbor_of_timestamp r.creation_timestamp; ] @ fragment)
let to_cbor = function | Status_report r -> V.Array [ V.int status_report_type; cbor_of_status_report r ] | Record { type_code; content } -> if type_code < 0 || type_code = status_report_type then Fmt.invalid_arg "Admin_record: record type code %d" type_code else V.Array [ V.int type_code; content ]
(* {1 Decoding} *)
open Result.Syntax
let err fmt = Fmt.kstr Result.error fmt
(* A CBOR unsigned integer: major type 0, so never negative. *)let uint64 what v = match V.to_int64 v with | Some n when Int64.compare n 0L >= 0 -> Ok n | Some _ | None -> err "%s must be an unsigned integer" what
let uint what v = let* n = uint64 what v in if Int64.compare n (Int64.of_int max_int) > 0 then err "%s is too large" what else Ok (Int64.to_int n)
let item_of_cbor = function | V.Array [ V.Bool false ] -> Ok Not_asserted | V.Array [ V.Bool true ] -> Ok (Asserted None) | V.Array [ V.Bool true; time ] -> let* time = uint64 "status time" time in Ok (Asserted (Some time)) | _ -> err "a status item must be [false], [true] or [true, time]"
let status_information v = match V.to_array v with | Some (a :: b :: c :: d :: _) -> let* a = item_of_cbor a in let* b = item_of_cbor b in let* c = item_of_cbor c in let* d = item_of_cbor d in Ok (a, b, c, d) | Some _ | None -> err "status information must be an array of at least 4 items"
let fragment_of_cbor offset length = let* offset = uint64 "fragment offset" offset in let* length = uint64 "fragment length" length in Ok (Some (offset, length))
let report info reason source ts fragment = let* received, forwarded, delivered, deleted = status_information info in let* reason = uint "reason code" reason in let* source = Message.eid_of_cbor source in let* creation_timestamp = Message.timestamp_of_cbor ts in let* fragment = fragment in Ok { received; forwarded; delivered; deleted; reason; source; creation_timestamp; fragment; }
let status_report_of_cbor v = match V.to_array v with | Some [ info; reason; source; ts ] -> report info reason source ts (Ok None) | Some [ info; reason; source; ts; offset; length ] -> report info reason source ts (fragment_of_cbor offset length) | Some _ | None -> err "a status report must be an array of 4 or 6 items"
let of_cbor v = match V.to_array v with | Some [ code; content ] -> let* type_code = uint "record type code" code in if type_code = status_report_type then let* r = status_report_of_cbor content in Ok (Status_report r) else Ok (Record { type_code; content }) | Some _ | None -> err "an administrative record must be an array of 2 items"
let codec = Cbor.Codec.conv of_cbor to_cbor Cbor.anylet encode r = Cbor.to_string codec r
let decode s = match Cbor.of_string codec s with | Ok r -> Ok r | Error e -> Error (Cbor.Error.to_string e)
(* {1 Equality} *)
let equal_item a b = match (a, b) with | Not_asserted, Not_asserted -> true | Asserted t, Asserted t' -> Option.equal Int64.equal t t' | (Not_asserted | Asserted _), _ -> false
let equal_status_report a b = equal_item a.received b.received && equal_item a.forwarded b.forwarded && equal_item a.delivered b.delivered && equal_item a.deleted b.deleted && Int.equal a.reason b.reason && Message.equal_eid a.source b.source && Message.compare_timestamp a.creation_timestamp b.creation_timestamp = 0 && Option.equal (fun (o, l) (o', l') -> Int64.equal o o' && Int64.equal l l') a.fragment b.fragment
let equal a b = match (a, b) with | Status_report r, Status_report r' -> equal_status_report r r' | Record r, Record r' -> Int.equal r.type_code r'.type_code && V.equal r.content r'.content | (Status_report _ | Record _), _ -> false
(* {1 Formatting} *)
let pp_item ppf = function | Not_asserted -> Fmt.string ppf "-" | Asserted None -> Fmt.string ppf "yes" | Asserted (Some t) -> Fmt.pf ppf "at %Ld" t
let pp ppf = function | Status_report r -> Fmt.pf ppf "@[<hov 2>status-report(received=%a;@ forwarded=%a;@ delivered=%a;@ \ deleted=%a;@ reason=%d;@ source=%a;@ timestamp=%a%a)@]" pp_item r.received pp_item r.forwarded pp_item r.delivered pp_item r.deleted r.reason Message.pp_eid r.source Message.pp_timestamp r.creation_timestamp (Fmt.option (fun ppf (o, l) -> Fmt.pf ppf ";@ fragment=%Ld+%Ld" o l)) r.fragment | Record { type_code; content } -> Fmt.pf ppf "record(%d, %a)" type_code V.pp content