Total rework HAP pkg and HomeKit source

This commit is contained in:
Alexey Khit
2023-07-23 22:22:36 +03:00
parent d73e9f6bcf
commit 6d82b1ce89
31 changed files with 2074 additions and 2015 deletions

View File

@@ -1,6 +1,7 @@
package hap
import (
"crypto/ed25519"
"crypto/rand"
"crypto/sha512"
"encoding/hex"
@@ -10,8 +11,52 @@ import (
"net/http"
)
const (
TXTConfigNumber = "c#" // Current configuration number (ex. 1, 2, 3)
TXTDeviceID = "id" // Device ID of the accessory (ex. 77:75:87:A0:7D:F4)
TXTModel = "md" // Model name of the accessory (ex. MJCTD02YL)
TXTProtoVersion = "pv" // Protocol version string (ex. 1.1)
TXTStateNumber = "s#" // Current state number (ex. 1)
TXTCategory = "ci" // Accessory Category Identifier (ex. 2, 5, 17)
TXTSetupHash = "sh" // Setup hash (ex. Y9w9hQ==)
// TXTFeatureFlags
// - 0001b - Supports Apple Authentication Coprocessor
// - 0010b - Supports Software Authentication
TXTFeatureFlags = "ff" // Pairing Feature flags (ex. 0, 1, 2)
// TXTStatusFlags
// - 0001b - Accessory has not been paired with any controllers
// - 0100b - A problem has been detected on the accessory
TXTStatusFlags = "sf" // Status flags (ex. 0, 1)
StateM1 = 1
StateM2 = 2
StateM3 = 3
StateM4 = 4
StateM5 = 5
StateM6 = 6
MethodPair = 0
MethodPairMFi = 1 // if device has MFI cert
MethodVerifyPair = 2
MethodAddPairing = 3
MethodDeletePairing = 4
MethodListPairings = 5
)
const (
PermissionUser = 0
PermissionAdmin = 1
)
const DeviceAID = 1 // TODO: fix someday
func GenerateKey() []byte {
_, key, _ := ed25519.GenerateKey(nil)
return key
}
func GenerateID(name string) string {
sum := sha512.Sum512([]byte(name))
return fmt.Sprintf(
@@ -28,46 +73,22 @@ func GenerateUUID() string {
return s[:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:]
}
type PairVerifyPayload struct {
Method byte `tlv8:"0,optional"`
Identifier string `tlv8:"1,optional"`
PublicKey []byte `tlv8:"3,optional"`
EncryptedData []byte `tlv8:"5,optional"`
State byte `tlv8:"6,optional"`
Status byte `tlv8:"7,optional"`
Signature []byte `tlv8:"10,optional"`
func Append(items ...any) (b []byte) {
for _, item := range items {
switch v := item.(type) {
case string:
b = append(b, v...)
case []byte:
b = append(b, v[:]...)
default:
panic(v)
}
}
return
}
//func (c *Character) Unmarshal(value any) error {
// switch c.Format {
// case characteristic.FormatTLV8:
// data, err := base64.StdEncoding.DecodeString(c.Value.(string))
// if err != nil {
// return err
// }
// return tlv8.Unmarshal(data, value)
// }
// return nil
//}
//func (c *Character) Marshal(value any) error {
// switch c.Format {
// case characteristic.FormatTLV8:
// data, err := tlv8.Marshal(value)
// if err != nil {
// return err
// }
// c.Value = base64.StdEncoding.EncodeToString(data)
// }
// return nil
//}
func (c *Character) String() string {
data, err := json.Marshal(c)
if err != nil {
return "ERROR"
}
return string(data)
func NewResponseError(req, res any) error {
return fmt.Errorf("hap: wrong response: %#v, on request: %#v", res, req)
}
func UnmarshalEvent(res *http.Response) (char *Character, err error) {