HAP.IID (HAP v0.1.0) View Source

Functions to map IIDs to/from service and characteristic indexes.

Service & characteristic IDs are each encoded as 8 bits within the IID. This makes it deterministic to extract the service and characteristic index from a given IID. This provides 256 possible service indices & 255 possible characteristic indices, which exceeds the limit of 100 services and characteristics as defined in 6.3.1 and 6.3.2 of the HAP specification.

Since IIDs are defined on the range [1, 18446744073709551615] per section 2.6.1 of the HAP specification, we set the lowest bit of the IID to 1, this allowing for service and characteristic indices of 0. As a consequence, any valid IID generated by this module will be in the range [1, 65536]. We also add 1 to the indicated characateristic index when populating the IID, reserving a characteristic index of 0 to represent service IIDs. This arrangement has the property that the IID for a service index of 0 is encoded as a value of 1, which the HAP protocol requires to represent the accessory information service.