Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:homekit

Apple Homekit

设计理念

  1. 预设 HomeKit Accessory Profiles,以统一不同的厂商、产品,这种做法类似蓝牙
  2. 预设的 Services 包括
    1. Garage door openers
    2. Lights
    3. Door locks
    4. Thermostats
    5. IP camera controls
    6. Switches
    7. 也可自定义 Custom (Not Recognized by Siri)
  3. 预设的 Characteristics 包括
    1. Power state
    2. Lock state
    3. Target state
    4. Brightness
    5. Model number
    6. Current temperature
    7. Custom (Not Recognized by Siri)

Hardware Accessory

开发 HomeKit-enabled hardware accessory 需要 MFi License (“Made for iPhone/iPod/iPad”),所以相关文档是无法随意获取到的。

但是 #WWDC14 的 Session 701,Designing Accessories for iOS and OS X,提供了一些 HomeKit Hardware 开发的信息。

  1. 协议叫做 HAP (HomeKit Accessory Protocol)
  2. 通信:
    1. HAP Layers
    2. HAP 目前支持 BLE 和 IP 两种通信方式
    3. 其中 BLE 是自带 服务发现 等服务级别的协议规范的,而 IP 不带,所以就有了上图中 JSON 上面那个绿块
      1. Remote access through HomeKit
      2. Bonjour for accessory discovery
      3. A single IP accessory may present multiple “accessories”
        1. Enables bridges
        2. Bridge is responsible for translating to and from HAP
      4. RESTful API to interact with accessories, services, and characteristics
    4. 而 HomeKit 对 BLE 的上层也做了很多限制,好像是使得 HomeKit 设备不兼容普通 BLE 了
      1. Remote access through HomeKit
      2. Apple-defined advertisement data for HAP
      3. Accessories are not identifiable from their advertisement data
      4. HAP security instead of Bluetooth Low Energy pairing
    5. Security
      1. 概念
        1. Bi-directional authentication
        2. Per-session encryption
        3. End-to-end encryption
        4. Initial setup secured directly between iOS and accessory
        5. Perfect forward secrecy
        6. Standard cryptography
      2. 技术
        AlgorithmDescription
        Secure Remote Password (SRP)Encrypts and authenticates initial pairing key exchange
        Ed25519Long-term keys for pairing and authentication
        Curve25519Encrypts initial authentication for each session
        HKDF-SHA-512Per-session ephemeral encryption key derivation
        ChaCha20-Poly1305Encrypts and authenticates HAP data
    6. Common functionality definitions
      1. Services
      2. Characteristics
  3. 数据:使用统一的 HomeKit Accessory Profiles(见上节),例子:
    accessory : {
        service1 : “public.hap.accessory-information” {
           characteristic : “serial-number”
           characteristic : “identify”
        }
        service2 : “public.hap.garage-door-opener” {
           characteristic : “target-state”
           characteristic : “current-state”
           characteristic : “obstruction-detected”
        }
        service3 : “public.hap.lightbulb{
           characteristic : “on”
        }
    }

701_designing_accessories_for_ios_and_os_x.pdf

Software App

Core Concepts

  1. common database,所有 accessories 会将数据存在同一 database 中,而不同的 apps 也是读/写同一 homekit database
  2. remote access :?:
  3. Home 的结构自上而下为:
    1. Home Manager
      1. Entry point
      2. Common database
      3. Manages homes
      4. Primary home
      5. Add or remove homes
      6. Notifies of changes
    2. Home
      1. Contains rooms, accessories, etc.
        NSString *homeName = self.home.name;
        NSArray *allRooms = self.home.rooms;
        NSArray *allAccessories = self.home.accessories;
      2. Notifies of changes
      3. Uniquely named
      4. Recognized by Siri
    3. Room
      1. Contains accessories
        NSArray *accessoriesInRoom = self.room.accessories;   
      2. Notifies of changes
      3. Uniquely named with a home
      4. Recognized by Siri
    4. Accessory
      1. Corresponds to physical device(s)
      2. Assigned to a room
        HMRoom *room = self.accessory.room;
        NSArray *services = self.accessory.services;   
      3. Accesses device state
      4. Notifies of changes
      5. Uniquely named with a home
      6. Recognized by Siri
    5. Services
      1. Represents a function of an accessory
      2. Contains characteristics of the service
      3. May have a name
        1. A “light bulb” service has a name
        2. A “firmware update” service does not
        3. Don’t expose unnamed services
        4. Names must be unique within a home
      4. Recognized by Siri
        1. Named and Apple-defined service types
    6. Characteristics
      1. Parameter of a service that allows interaction
      2. Provides information to describe the characteristic
      3. Uses completion handler blocks for results
        [self.characteristic readValueWithCompletionHandler:^(NSError *error) {
            if (error == nil) {
               id value = self.characteristic.value;
            }
        }];
        !
        [self.characteristic writeValue:@42 withCompletionHandler:^(NSError *error) {
            if (error != nil) {
               // unable to write value. check error for why
            }
        }];
      4. Recognized by Siri
        1. Apple-defined
      5. Read-only
        1. e.g. Current temperature
      6. Read-write
        1. e.g. Target temperature
      7. Write-only
        1. e.g. Identify

Homes (HMHome) Homes(HMHome)是最顶层的容器,展示了用户一般都会认为是单个家庭单位的结构。用户可能有多个离得较远的住所,比如一个经常使用的住所和一个度假别墅。或者他们可能有两个离得比较近的住所,比如一个主要住宅和一个别墅。

Rooms (HMRoom) Rooms (HMRoom)是home的可选部分,并且代表home中单独的room。room并没有任何物理特性–大小、位置等等。对用户来说,它们是简单的有意义的命名,比如“起居室”或者“厨房”。有意义的room名称可以启用类似“Siri,打开起厨房的灯”的指令。

Accessories (HMAccessory) 附属设备被安装在home中,并且被分配给每个room。它们是实际的物理家庭自动化设备,比如一个车库门遥控开关。如果用户没有配置任何room,那么Home Kit将会把附属设备分配给home中特殊的默认room。

Services (HMService) Services (HMService)是由附属配件提供的实际服务。附属配件有用户可控制的服务,比如灯光;也有它们自用的服务,比如框架更新服务。Home Kit更多关注用户可以控制的服务。单个附属配件可能有多个用户可控制的服务。比如大部分车库遥控开关有打开或者关闭车库门的服务,并且在车库门上还有控制灯光的服务。

Zones (HMZone) Zones (HMZone)是home中可选择的room分组。“Upstairs”和“downstairs”可以由zones代表。Zones是完全可选择的,room不需要处于zone中。通过把room添加到zone中,用户可以给Siri发命令,比如“Siri,打开楼下所有的灯。”

Advanced Topics

  1. Adding New Accessories
    1. Accessory Browser (HMAccessoryBrowser)
      self.accessoryBrowser = [[HMAccessoryBrowser alloc] init];
      self.accessoryBrowser.delegate = self;
      
      [self.accessoryBrowser startSearchingForNewAccessories];
      
      ... later ...
      
      [self.accessoryBrowser stopSearchingForNewAccessories];    
    2. Finds new accessories (HMAccessoryBrowser)
      (void)accessoryBrowser:(HMAccessoryBrowser *)browser didFindNewAccessory:
      (HMAccessory *)accessory {
        // a new accessory has been found
      }    
  2. Adding New Accessories to a Home (HMHome)
    [self.home addAccessory:newAccessory completionHandler:^(NSError *error) {
      if (error == nil) {
         // success!
         // assign the accessory to a room and give it a name
      } else {
         // adding accessory failed; check error for why
      }
    }];
  3. Removing New Accessories from View (HMAccessoryBrowser)
    (void)accessoryBrowser:(HMAccessoryBrowser *)browser didRemoveNewAccessory:
    (HMAccessory *)accessory {
      // a new accessory has been added to a home
      // or has gone out of range or has been turned off
    }   
  4. Testing Your App
    1. Simulator
  5. bridge 类型的 Accessory
  6. Initial Setup Review (Recommended flow)
    1. Create a home
      1. User provides names
    2. Add rooms to the home
      1. User provides names
    3. Add accessories
      1. Use an accessory browser
      2. Add accessory to home
      3. User provides name → update name for accessory
      4. User chooses room → assign accessory to room
  7. Zones (HMZone)
    1. Arbitrary grouping of rooms (Example: upstairs)
    2. Rooms can be in any number of zones Uniquely named with a home
    3. Recognized by Siri
  8. Service Groups (HMServiceGroup)
    1. HMServiceGroup (Example: nightlights)
    2. Services can be in any number of groups
    3. Arbitrary grouping of services
    4. Convenient way to control services across accessories
    5. Uniquely named with a home
    6. Recognized by Siri
  9. Action Sets (HMActionSet)
    1. Collection of actions that are executed together
      1. Example: “night” → Light off, Lock doors, Temp down, Switch off, Light off, Close garage, Close gates
    2. Actions executed in undefined order
    3. Uniquely named with a home
    4. Recognized by Siri
  10. Actions (HMCharacteristicWriteAction)
    1. Added to action sets
    2. Writes a value to a characteristic
  11. Triggers (HMTimerTrigger)
    1. Executes an action set
    2. Fires on a specified date
    3. Can repeat
    4. Uniquely named with a home
    5. System executes in background
  12. Action Sets and Triggers
    1. “Script” complex behavior
    2. Quickly control many accessories
it/homekit.txt · Last modified: 2014/10/09 16:19 by admin