HAP (HomeKit Accessory Protocol)
Rust implementation of the Apple HomeKit Accessory Protocol (HAP).
This crate supports all HomeKit services and characteristics currently implemented by Apple and provides the ability to create custom characteristics, services and accessories.
The HomeKit Accessory Protocol supports transports over IP and Bluetooth LE. Currently only the transport over IP is implemented in this crate. Accessories are exposed by the implemented HAP Accessory HTTP server and announced via built-in mDNS.
HomeKit Data Model
The HAP defines HomeKit enabled devices as virtual accessories
that are composed of services
that are composed of characteristics
.
Characteristics hold values of various data types as well as optional metadata like max/min values or units. Services group characteristics and represent features of the accessory. Every accessory consists of at least one accessory information service
and any number of additional services. For example a custom ceiling fan accessory may consist of an accessory information Service
, a fan service
and a lightbulb service
.
Ceiling Fan Accessory
|
|-- Accessory Information Service
| |-- Identify Characteristic
| |-- Manufacturer Characteristic
| |-- Model Characteristic
| |-- Name Characteristic
| |-- Serial Characteristic
|
|-- Fan Service
| |-- On Characteristic
| |-- Rotation Direction Characteristic
| |-- Rotation Speed Characteristic
|
|-- Lightbulb Service
| |-- On Characteristic
| |-- Brightness Characteristic
| |-- Hue Characteristic
| |-- Saturation Characteristic
This crate provides a pre-built accessory for every service predefined by Apple in the HomeKit Accessory Simulator as well as others like Television. Custom characteristics and services can be created, assembled and used alongside the predefined ones.
For a full list of the predefined characteristics, services and accessories, see the docs or Apple's official specification.
Usage Examples
Creating a simple lightbulb accessory and starting the IP server:
use ;
async
Setting sync callbacks to react to remote value reads and updates:
use CharacteristicCallbacks;
lightbulb.lightbulb.on.on_read;
lightbulb.lightbulb.on.on_update;
Setting async callbacks to react to remote value reads and updates:
use AsyncCharacteristicCallbacks;
lightbulb.lightbulb.on.on_read_async;
lightbulb.lightbulb.on.on_update_async;
Setting a characteristic value directly:
use ;
lightbulb.lightbulb.on.set_value.await.unwrap;
TODOs
- IP Transport
- Default Accessories
- Lock Accessory
- Television Accessory
- Camera Streams
- IP Camera Accessory
- Video Doorbell Accessory
- BLE Transport
Development
Codegen is handled by the codegen
crate in the workspace. Generated files are checked in. To run the code generation, do:
cargo run --package hap-codegen
License
HAP is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.