HomeKit Accessory Protocol (HAP) for OCaml
0
fork

Configure Feed

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

OCaml 93.3%
Dune 1.9%
Other 4.7%
55 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-hap https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-hap
git@git.recoil.org:gazagnaire.org/ocaml-hap git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-hap

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

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 mDNS
  • Hap.accessory_info -- Get accessory details from mDNS record
  • Hap.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 filesystem
  • Hap.pairing_by_id -- Load pairing by accessory ID
  • Hap.pairing_for_ip -- Find pairing by IP address

Control#

  • Hap.accessories -- List accessory services and characteristics
  • Hap.characteristics -- Read characteristic values
  • Hap.put_characteristic -- Write a characteristic value
  • Hap.turn_on_outlet / turn_off_outlet / toggle_outlet -- Out let shortcuts

TLV#

  • Hap.Tlv -- HAP TLV8 encoding/decoding
  • Hap.Tlv_type -- TLV type constants
  • Hap.Hap_error -- HAP error codes

Licence#

ISC