Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observe #4

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions coap-core/Coap_core.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 34 additions & 5 deletions coap-core/Coap_message.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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 =
Expand All @@ -468,13 +490,20 @@ 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
| If_match x -> p "@[(If_match %S)@]" x
| 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
Expand Down
1 change: 1 addition & 0 deletions coap/Coap.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 19 additions & 13 deletions examples/Coap_demo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()

Expand All @@ -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


10 changes: 4 additions & 6 deletions examples/dune
Original file line number Diff line number Diff line change
@@ -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))