HAP -- HomeKit Accessory Protocol for OCaml#
Implementation of the HomeKit Accessory Protocol for controlling HomeKit accessories. Includes mDNS discovery, SRP-6a pair setup, Curve25519 pair verify, and ChaCha20-Poly1305 encrypted sessions.
Installation#
Install with opam:
$ opam install hap
If opam cannot find the package, it may not yet be released in the public
opam-repository. Add the overlay repository, then install it:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install hap
Usage#
let run () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let net = Eio.Stdenv.net env in
let clock = Eio.Stdenv.clock env in
let fs = Eio.Stdenv.fs env in
(* Discover HomeKit accessories on the network. *)
match Hap.discover ~sw ~net ~clock ~timeout:5.0 () with
| [] -> ()
| (a : Hap.accessory_info) :: _ ->
(* Pair with an accessory (one-time setup). *)
(match
Hap.pair_setup ~net ~sw ~clock ~ip:a.ip ~port:a.port ~pin:"031-45-154"
with
| Ok _pairing -> ()
| Error (`Msg m) -> failwith m);
(* Control an outlet (uses the saved pairing). *)
match Hap.turn_on_outlet ~net ~sw ~clock ~fs a.ip with
| Ok () -> ()
| Error (`Msg m) -> failwith m
API#
Discovery#
Hap.discover-- Find HomeKit accessories via mDNSHap.accessory_info-- Get accessory details from mDNS recordHap.category_name-- Human-readable accessory category
Pairing#
Hap.pair_setup-- SRP-6a pair setup (one-time, requires PIN)Hap.pair_verify-- Curve25519 pair verify (establishes encrypted session)
Pairing Storage#
Hap.save_pairing_by_id-- Persist pairing to filesystemHap.pairing_by_id-- Load pairing by accessory IDHap.pairing_for_ip-- Find pairing by IP address
Control#
Hap.accessories-- List accessory services and characteristicsHap.characteristics-- Read characteristic valuesHap.put_characteristic-- Write a characteristic valueHap.turn_on_outlet/turn_off_outlet/toggle_outlet-- Out let shortcuts
TLV#
Hap.Tlv-- HAP TLV8 encoding/decodingHap.Tlv_type-- TLV type constantsHap.Hap_error-- HAP error codes
Licence#
ISC