GithubHelp home page GithubHelp logo

rushidesai / hap-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ewilken/hap-rs

0.0 1.0 0.0 899 KB

Rust implementation of the Apple HomeKit Accessory Protocol (HAP)

License: Apache License 2.0

Rust 100.00%

hap-rs's Introduction

HAP (HomeKit Accessory Protocol)

CI crates.io docs.rs license: MIT/Apache-2.0

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 hap::{
    accessory::{lightbulb::LightbulbAccessory, AccessoryCategory, AccessoryInformation},
    server::{IpServer, Server},
    storage::FileStorage,
    tokio,
    Config,
    MacAddress,
    Pin,
};

#[tokio::main]
async fn main() {
    let lightbulb = LightbulbAccessory::new(1, AccessoryInformation {
        name: "Acme Lightbulb".into(),
        ..Default::default()
    })
    .unwrap();

    let config = Config {
        pin: Pin::new([1, 1, 1, 2, 2, 3, 3, 3]).unwrap(),
        name: "Acme Lightbulb".into(),
        device_id: MacAddress::new([10, 20, 30, 40, 50, 60]),
        category: AccessoryCategory::Lightbulb,
        ..Default::default()
    };
    let storage = FileStorage::current_dir().await.unwrap();

    let mut server = IpServer::new(config, storage).unwrap();
    server.add_accessory(lightbulb).await.unwrap();

    let handle = server.run_handle();

    std::env::set_var("RUST_LOG", "hap=debug");
    env_logger::init();

    handle.await;
}

Setting sync callbacks to react to remote value reads and updates:

use hap::characteristic::CharacteristicCallbacks;

lightbulb.lightbulb.on.on_read(Some(|| {
    println!("on characteristic read");
    None
}));

lightbulb.lightbulb.on.on_update(Some(|current_val: &bool, new_val: &bool| {
    println!("on characteristic updated from {} to {}", current_val, new_val);
}));

Setting async callbacks to react to remote value reads and updates:

use hap::characteristic::AsyncCharacteristicCallbacks;

lightbulb.lightbulb.on.on_read_async(Some(|| {
    async {
        println!("on characteristic read (async)");
        None
    }
    .boxed()
}));

lightbulb.lightbulb.on.on_update_async(Some(|current_val: bool, new_val: bool| {
    async move {
        println!("on characteristic updated from {} to {} (async)", current_val, new_val);
    }
    .boxed()
}));

Setting a characteristic value directly:

use hap::{
    characteristic::HapCharacteristic,
    serde_json::Value,
};

lightbulb.lightbulb.on.set_value(Value::Bool(true)).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

at your option.

hap-rs's People

Contributors

ewilken avatar jmhodges avatar laibulle avatar m1cha avatar messense avatar roba1993 avatar voider1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.