From 531e3161f932c4d98fbeee76904f14628c12fd1e Mon Sep 17 00:00:00 2001 From: Rizo Isrof Date: Thu, 1 Oct 2020 13:30:13 +0100 Subject: [PATCH 1/2] Fix example server. --- examples/Coap_demo.ml | 32 +++++++++++++++++++------------- examples/dune | 10 ++++------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/examples/Coap_demo.ml b/examples/Coap_demo.ml index 85adebf..58273a4 100644 --- a/examples/Coap_demo.ml +++ b/examples/Coap_demo.ml @@ -23,15 +23,17 @@ let msg2_data = let msg4_data = "\x44\x02\xd9\xb9\x74\x6f\x6b\x31\x31\x48\x43\xa9\x8a\xc7\x71\x41\xff\x73\x6f\x6d\x65\x74\x68\x69\x6e\x67" -let recode expected = +let recode expected_str = + let expected = Coap_core.Message.buffer_of_string expected_str in let* msg = Coap_core.Message.decode expected in Format.printf "recode: msg=%a@." Coap_core.Message.pp msg; let actual = Coap_core.Message.encode msg in - if not (String.equal expected actual) then begin - let expected = Cstruct.of_string expected in - let actual = Cstruct.of_string actual in + let actual_str = Coap_core.Message.buffer_to_string actual in + if not (String.equal expected_str actual_str) then begin + let expected_cstr = Cstruct.of_bigarray expected in + let actual_cstr = Cstruct.of_bigarray actual in Format.printf "recode failed@. - %a@, + %a@]@." - Cstruct.hexdump_pp expected Cstruct.hexdump_pp actual; + Cstruct.hexdump_pp expected_cstr Cstruct.hexdump_pp actual_cstr; exit 1 end else Ok () @@ -43,16 +45,20 @@ let test_message_encoder () = run begin Ok () end - -let () = run begin - let () = Coap_server_unix.start begin function +let () = + Coap_server_lwt.start begin function | Ok req -> + Format.eprintf "--- REQUEST MESSAGE ---@.%a@.@." Coap_core.Message.pp req; let token = Coap_core.Message.token req in - Coap_core.Message.make ~code:(Response `Content) ~token "Hello, world!" + let payload = Coap_core.Message.buffer_of_string "Hello, world!" in + let message = Coap_core.Message.make ~code:(Response `Content) ~token payload in + Lwt.return message | Error e -> Format.eprintf "[ERROR] %a@." Coap_core.pp_error e; - Coap_core.Message.make ~code:(Response `Internal_server_error) "Oh no!" - end in - Ok () -end + let payload = Coap_core.Message.buffer_of_string "Oh no!" in + let message = Coap_core.Message.make ~code:(Response `Internal_server_error) payload in + Lwt.return message + end + |> Lwt_main.run + diff --git a/examples/dune b/examples/dune index f26dfdf..3c7d1c7 100644 --- a/examples/dune +++ b/examples/dune @@ -1,7 +1,5 @@ - (executable - (name Coap_demo) - (modules Coap_demo) - (libraries coap-core coap-server-unix fmt) - (preprocess future_syntax)) - + (name Coap_demo) + (modules Coap_demo) + (libraries coap-core coap-server-lwt fmt) + (preprocess future_syntax)) From 0c66e022a99de168cbf4e00cc77f03382e3c5491 Mon Sep 17 00:00:00 2001 From: Rizo Isrof Date: Thu, 1 Oct 2020 13:30:46 +0100 Subject: [PATCH 2/2] Add observe option support. --- coap-core/Coap_core.mli | 1 + coap-core/Coap_message.ml | 39 ++++++++++++++++++++++++++++++++++----- coap/Coap.mli | 1 + 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/coap-core/Coap_core.mli b/coap-core/Coap_core.mli index 3e5d806..c2474fd 100644 --- a/coap-core/Coap_core.mli +++ b/coap-core/Coap_core.mli @@ -80,6 +80,7 @@ module Message : sig | Uri_host of string | Etag of string | If_none_match + | Observe of [ `Register | `Deregister | `Sequnce of int ] | Uri_port of int | Location_path of string | Uri_path of string diff --git a/coap-core/Coap_message.ml b/coap-core/Coap_message.ml index 978a476..ded7503 100644 --- a/coap-core/Coap_message.ml +++ b/coap-core/Coap_message.ml @@ -269,6 +269,7 @@ type option = | Uri_host of string | Etag of string | If_none_match + | Observe of [ `Register | `Deregister | `Sequnce of int ] | Uri_port of int | Location_path of string | Uri_path of string @@ -331,12 +332,27 @@ module Option = struct invalid_arg ("option length("^ string_of_int length ^") > 4") + let observe_of_int n = + match n with + | 0 -> `Register + | 1 -> `Deregister + | n -> `Sequnce n + + + let observe_to_int n = + match n with + | `Register -> 0 + | `Deregister -> 1 + | `Sequnce n -> n + + let decode n value length = match n with | 1 -> If_match (Cstruct.to_string value) | 3 -> Uri_host (Cstruct.to_string value) | 4 -> Etag (Cstruct.to_string value) | 5 -> If_none_match + | 6 -> Observe (observe_of_int (decode_int value length)) | 7 -> Uri_port (decode_int value length) | 8 -> Location_path (Cstruct.to_string value) | 11 -> Uri_path (Cstruct.to_string value) @@ -382,6 +398,7 @@ module Option = struct | Uri_host x -> (3, x) | Etag x -> (4, x) | If_none_match -> (5, "") + | Observe x -> (6, encode_int (observe_to_int x)) | Uri_port x -> (7, encode_int x) | Location_path x -> (8, x) | Uri_path x -> (11, x) @@ -402,6 +419,7 @@ module Option = struct | Uri_host _ -> 3 | Etag _ -> 4 | If_none_match -> 5 + | Observe _ -> 6 | Uri_port _ -> 7 | Location_path _ -> 8 | Uri_path _ -> 11 @@ -420,6 +438,7 @@ module Option = struct | 3 -> "Uri_host" | 4 -> "Etag" | 5 -> "If_none_mat" + | 6 -> "Observe" | 7 -> "Uri_port" | 8 -> "Location_path" | 11 -> "Uri_path" @@ -435,6 +454,12 @@ module Option = struct let value_length self = + let uint_length n = + if n <= 0xFF then 1 else + if n <= 0xFFFF then 2 else + if n <= 0xFFFFFF then 3 else + 4 + in match self with | If_none_match -> 0 | If_match x @@ -447,14 +472,11 @@ module Option = struct | Proxy_scheme x | Uri_query x -> String.length x | Content_format _ -> 666 (* FIXME *) + | Observe x -> uint_length (observe_to_int x) | Uri_port x | Max_age x | Accept x - | Size1 x -> - if x <= 0xFF then 1 else - if x <= 0xFFFF then 2 else - if x <= 0xFFFFFF then 3 else - 4 + | Size1 x -> uint_length x let length self = @@ -468,6 +490,12 @@ module Option = struct compare (number a) (number b) + let pp_observe f observe = + match observe with + | `Register -> Format.fprintf f "Register" + | `Deregister -> Format.fprintf f "Deregister" + | `Sequnce n -> Format.fprintf f "@[(Sequence %d)@]" n + let pp f self = let p fmt = Format.fprintf f fmt in match self with @@ -475,6 +503,7 @@ module Option = struct | Uri_host x -> p "@[(Uri_host %S)@]" x | Etag x -> p "@[(Etag %S)@]" x | If_none_match -> p "If_none_match" + | Observe x -> p "@[(Option %a)@]" pp_observe x | Uri_port x -> p "@[(Uri_port %d)@]" x | Location_path x -> p "@[(Location_path %S)@]" x | Uri_path x -> p "@[(Uri_path %S)@]" x diff --git a/coap/Coap.mli b/coap/Coap.mli index 0d86bb0..fa24e4c 100644 --- a/coap/Coap.mli +++ b/coap/Coap.mli @@ -85,6 +85,7 @@ module Message : sig | Uri_host of string | Etag of string | If_none_match + | Observe of [ `Register | `Deregister | `Sequnce of int ] | Uri_port of int | Location_path of string | Uri_path of string