HomeKit Accessory Protocol (HAP) for OCaml
0
fork

Configure Feed

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

claude: complete Err -> Error module rename across call sites

Follow up to the module rename: update the remaining callers that
still referenced [Err] (library [claude.ml{,i}], [client.ml], the test
driver [test.ml]), and fix one stray [^ e] string concatenation in
hermest's CLI that needed [Json.Error.to_string e] now that
[Json.of_string] yields a structured error.

+52 -60
-1
fuzz/dune
··· 5 5 6 6 (executable 7 7 (name fuzz) 8 - (modules fuzz fuzz_hap) 9 8 (libraries hap alcobar)) 10 9 11 10 (rule
-1
lib/dune
··· 12 12 eio 13 13 re 14 14 json 15 - json.bytesrw 16 15 base64 17 16 logs 18 17 fmt
+52 -58
lib/hap.ml
··· 536 536 let pos = Re.Group.stop g 0 in 537 537 let body = String.sub decrypted pos (String.length decrypted - pos) in 538 538 Result.map_error 539 - (fun e -> `Msg e) 540 - (Json_bytesrw.decode_string Json.json body) 539 + (fun e -> `Msg (Json.Error.to_string e)) 540 + (Json.Value.of_string body) 541 541 542 542 (* Get accessories from a session *) 543 543 let accessories ~net ~sw session = ··· 553 553 type char_write = { cw_aid : int; cw_iid : int; cw_value : Json.t } 554 554 555 555 let char_write_codec = 556 - Json.Object.map ~kind:"char_write" (fun aid iid value -> 556 + Json.Codec.Object.map ~kind:"char_write" (fun aid iid value -> 557 557 { cw_aid = aid; cw_iid = iid; cw_value = value }) 558 - |> Json.Object.mem "aid" Json.int ~enc:(fun c -> c.cw_aid) 559 - |> Json.Object.mem "iid" Json.int ~enc:(fun c -> c.cw_iid) 560 - |> Json.Object.mem "value" Json.json ~enc:(fun c -> c.cw_value) 561 - |> Json.Object.finish 558 + |> Json.Codec.Object.mem "aid" Json.Codec.int ~enc:(fun c -> c.cw_aid) 559 + |> Json.Codec.Object.mem "iid" Json.Codec.int ~enc:(fun c -> c.cw_iid) 560 + |> Json.Codec.Object.mem "value" Json.Codec.Value.t ~enc:(fun c -> c.cw_value) 561 + |> Json.Codec.Object.finish 562 562 563 563 type char_write_request = { characteristics : char_write list } 564 564 565 565 let char_write_request_codec = 566 - Json.Object.map ~kind:"char_write_request" (fun characteristics -> 566 + Json.Codec.Object.map ~kind:"char_write_request" (fun characteristics -> 567 567 { characteristics }) 568 - |> Json.Object.mem "characteristics" (Json.list char_write_codec) 568 + |> Json.Codec.Object.mem "characteristics" (Json.Codec.list char_write_codec) 569 569 ~enc:(fun r -> r.characteristics) 570 - |> Json.Object.finish 570 + |> Json.Codec.Object.finish 571 571 572 572 (* Write a characteristic *) 573 573 let put_characteristic ~net ~sw session ~aid ~iid value = 574 574 let req = 575 575 { characteristics = [ { cw_aid = aid; cw_iid = iid; cw_value = value } ] } 576 576 in 577 - let body = 578 - match Json_bytesrw.encode_string char_write_request_codec req with 579 - | Ok s -> s 580 - | Error _ -> "{}" 581 - in 577 + let body = Json.to_string char_write_request_codec req in 582 578 let path = "/characteristics" in 583 579 let req = 584 580 Fmt.str ··· 632 628 } 633 629 634 630 let stored = 635 - Json.Object.map ~kind:"hap.pairing" 631 + Json.Codec.Object.map ~kind:"hap.pairing" 636 632 (fun 637 633 accessory_id 638 634 accessory_ltpk ··· 647 643 controller_ltsk; 648 644 controller_ltpk; 649 645 }) 650 - |> Json.Object.mem "accessory_id" Json.string ~enc:(fun p -> 646 + |> Json.Codec.Object.mem "accessory_id" Json.Codec.string ~enc:(fun p -> 651 647 p.accessory_id) 652 - |> Json.Object.mem "accessory_ltpk" Json.string ~enc:(fun p -> 648 + |> Json.Codec.Object.mem "accessory_ltpk" Json.Codec.string ~enc:(fun p -> 653 649 p.accessory_ltpk) 654 - |> Json.Object.mem "controller_id" Json.string ~enc:(fun p -> 650 + |> Json.Codec.Object.mem "controller_id" Json.Codec.string ~enc:(fun p -> 655 651 p.controller_id) 656 - |> Json.Object.mem "controller_ltsk" Json.string ~enc:(fun p -> 652 + |> Json.Codec.Object.mem "controller_ltsk" Json.Codec.string ~enc:(fun p -> 657 653 p.controller_ltsk) 658 - |> Json.Object.mem "controller_ltpk" Json.string ~enc:(fun p -> 654 + |> Json.Codec.Object.mem "controller_ltpk" Json.Codec.string ~enc:(fun p -> 659 655 p.controller_ltpk) 660 - |> Json.Object.finish 656 + |> Json.Codec.Object.finish 661 657 662 658 let of_pairing (p : pairing) : stored = 663 659 { ··· 681 677 (* Save/load pairing to file *) 682 678 let save_pairing ~fs ~path (pairing : pairing) = 683 679 let stored = Pairing_json.of_pairing pairing in 684 - match 685 - Json_bytesrw.encode_string ~format:Json.Indent Pairing_json.stored stored 686 - with 680 + match Json.to_string ~format:Json.Indent Pairing_json.stored stored with 687 681 | Ok json -> 688 682 Eio.Path.save ~create:(`Or_truncate 0o600) Eio.Path.(fs / path) json 689 683 | Error _ -> () ··· 694 688 else 695 689 begin try 696 690 let content = Eio.Path.load full_path in 697 - match Json_bytesrw.decode_string Pairing_json.stored content with 691 + match Json.of_string Pairing_json.stored content with 698 692 | Ok stored -> Some (Pairing_json.to_pairing stored) 699 693 | Error _ -> None 700 694 with Eio.Io _ -> None ··· 862 856 (** HAP characteristic *) 863 857 864 858 let characteristic = 865 - Json.Object.map ~kind:"hap.characteristic" (fun iid type_ value -> 859 + Json.Codec.Object.map ~kind:"hap.characteristic" (fun iid type_ value -> 866 860 { iid; type_; value }) 867 - |> Json.Object.mem "iid" Json.int ~enc:(fun c -> c.iid) 868 - |> Json.Object.mem "type" Json.string ~enc:(fun c -> c.type_) 869 - |> Json.Object.opt_mem "value" Json.json ~enc:(fun c -> c.value) 870 - |> Json.Object.finish 861 + |> Json.Codec.Object.mem "iid" Json.Codec.int ~enc:(fun c -> c.iid) 862 + |> Json.Codec.Object.mem "type" Json.Codec.string ~enc:(fun c -> c.type_) 863 + |> Json.Codec.Object.opt_mem "value" Json.Codec.Value.t ~enc:(fun c -> 864 + c.value) 865 + |> Json.Codec.Object.finish 871 866 872 867 type service = { 873 868 iid : int; ··· 877 872 (** HAP service *) 878 873 879 874 let service = 880 - Json.Object.map ~kind:"hap.service" (fun iid type_ characteristics -> 875 + Json.Codec.Object.map ~kind:"hap.service" (fun iid type_ characteristics -> 881 876 { iid; type_; characteristics }) 882 - |> Json.Object.mem "iid" Json.int ~enc:(fun s -> s.iid) 883 - |> Json.Object.mem "type" Json.string ~enc:(fun s -> s.type_) 884 - |> Json.Object.mem "characteristics" (Json.list characteristic) 877 + |> Json.Codec.Object.mem "iid" Json.Codec.int ~enc:(fun s -> s.iid) 878 + |> Json.Codec.Object.mem "type" Json.Codec.string ~enc:(fun s -> s.type_) 879 + |> Json.Codec.Object.mem "characteristics" (Json.Codec.list characteristic) 885 880 ~enc:(fun s -> s.characteristics) 886 - |> Json.Object.finish 881 + |> Json.Codec.Object.finish 887 882 888 883 type accessory = { aid : int; services : service list } 889 884 (** HAP accessory *) 890 885 891 886 let accessory = 892 - Json.Object.map ~kind:"hap.accessory" (fun aid services -> 887 + Json.Codec.Object.map ~kind:"hap.accessory" (fun aid services -> 893 888 { aid; services }) 894 - |> Json.Object.mem "aid" Json.int ~enc:(fun a -> a.aid) 895 - |> Json.Object.mem "services" (Json.list service) ~enc:(fun a -> 889 + |> Json.Codec.Object.mem "aid" Json.Codec.int ~enc:(fun a -> a.aid) 890 + |> Json.Codec.Object.mem "services" (Json.Codec.list service) ~enc:(fun a -> 896 891 a.services) 897 - |> Json.Object.finish 892 + |> Json.Codec.Object.finish 898 893 899 894 type accessories_response = { accessories : accessory list } 900 895 (** HAP accessories response *) 901 896 902 897 let accessories_response = 903 - Json.Object.map ~kind:"hap.accessories_response" (fun accessories -> 898 + Json.Codec.Object.map ~kind:"hap.accessories_response" (fun accessories -> 904 899 { accessories }) 905 - |> Json.Object.mem "accessories" (Json.list accessory) ~enc:(fun r -> 906 - r.accessories) 907 - |> Json.Object.finish 900 + |> Json.Codec.Object.mem "accessories" (Json.Codec.list accessory) 901 + ~enc:(fun r -> r.accessories) 902 + |> Json.Codec.Object.finish 908 903 909 904 type char_value = { aid : int; iid : int; value : Json.t option } 910 905 (** HAP characteristics value *) 911 906 912 907 let char_value = 913 - Json.Object.map ~kind:"hap.char_value" (fun aid iid value -> 908 + Json.Codec.Object.map ~kind:"hap.char_value" (fun aid iid value -> 914 909 { aid; iid; value }) 915 - |> Json.Object.mem "aid" Json.int ~enc:(fun c -> c.aid) 916 - |> Json.Object.mem "iid" Json.int ~enc:(fun c -> c.iid) 917 - |> Json.Object.opt_mem "value" Json.json ~enc:(fun c -> c.value) 918 - |> Json.Object.finish 910 + |> Json.Codec.Object.mem "aid" Json.Codec.int ~enc:(fun c -> c.aid) 911 + |> Json.Codec.Object.mem "iid" Json.Codec.int ~enc:(fun c -> c.iid) 912 + |> Json.Codec.Object.opt_mem "value" Json.Codec.Value.t ~enc:(fun c -> 913 + c.value) 914 + |> Json.Codec.Object.finish 919 915 920 916 type characteristics_response = { characteristics : char_value list } 921 917 922 918 let characteristics_response = 923 - Json.Object.map ~kind:"hap.characteristics_response" 919 + Json.Codec.Object.map ~kind:"hap.characteristics_response" 924 920 (fun characteristics -> { characteristics }) 925 - |> Json.Object.mem "characteristics" (Json.list char_value) ~enc:(fun r -> 926 - r.characteristics) 927 - |> Json.Object.finish 921 + |> Json.Codec.Object.mem "characteristics" (Json.Codec.list char_value) 922 + ~enc:(fun r -> r.characteristics) 923 + |> Json.Codec.Object.finish 928 924 end 929 925 930 926 (** {1 High-level control} *) ··· 934 930 let on = "25" (* 00000025-0000-1000-8000-0026BB765291 *) 935 931 end 936 932 937 - (* Decode Json.json via codec *) 933 + (* Decode Json.t via codec *) 938 934 let decode codec json = 939 - match Json_bytesrw.encode_string Json.json json with 935 + match Json.Value.to_string json with 940 936 | Error e -> Error e 941 937 | Ok str -> ( 942 - match Json_bytesrw.decode_string codec str with 943 - | Ok v -> Ok v 944 - | Error e -> Error e) 938 + match Json.of_string codec str with Ok v -> Ok v | Error e -> Error e) 945 939 946 940 (* Find the On characteristic IID from accessories JSON *) 947 941 let on_characteristic_iid json =