-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathHAPDeviceID.c
85 lines (73 loc) · 2.62 KB
/
HAPDeviceID.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2015-2019 The HomeKit ADK Contributors
//
// Licensed under the Apache License, Version 2.0 (the “License”);
// you may not use this file except in compliance with the License.
// See [CONTRIBUTORS.md] for the list of HomeKit ADK project authors.
#include "HAP+Internal.h"
static const HAPLogObject logObject = { .subsystem = kHAP_LogSubsystem, .category = "DeviceID" };
HAP_RESULT_USE_CHECK
HAPError HAPDeviceIDGet(HAPPlatformKeyValueStoreRef keyValueStore, HAPDeviceID* deviceID) {
HAPPrecondition(keyValueStore);
HAPPrecondition(deviceID);
HAPError err;
// Try to load Device ID.
bool found;
size_t numBytes;
err = HAPPlatformKeyValueStoreGet(
keyValueStore,
kHAPKeyValueStoreDomain_Configuration,
kHAPKeyValueStoreKey_Configuration_DeviceID,
deviceID->bytes,
sizeof deviceID->bytes,
&numBytes,
&found);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
if (!found) {
// Generate new Device ID.
HAPPlatformRandomNumberFill(deviceID->bytes, sizeof deviceID->bytes);
HAPLogBufferInfo(&logObject, deviceID->bytes, sizeof deviceID->bytes, "Generated new Device ID.");
// Store new Device ID.
err = HAPPlatformKeyValueStoreSet(
keyValueStore,
kHAPKeyValueStoreDomain_Configuration,
kHAPKeyValueStoreKey_Configuration_DeviceID,
deviceID->bytes,
sizeof deviceID->bytes);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
} else if (numBytes != sizeof deviceID->bytes) {
HAPLog(&logObject, "Invalid Device ID.");
return kHAPError_Unknown;
}
return kHAPError_None;
}
HAP_RESULT_USE_CHECK
HAPError HAPDeviceIDGetAsString(HAPPlatformKeyValueStoreRef keyValueStore, HAPDeviceIDString* deviceIDString) {
HAPPrecondition(keyValueStore);
HAPPrecondition(deviceIDString);
HAPError err;
HAPDeviceID deviceID;
err = HAPDeviceIDGet(keyValueStore, &deviceID);
if (err) {
HAPAssert(err == kHAPError_Unknown);
return err;
}
HAPAssert(sizeof deviceID.bytes == 6);
err = HAPStringWithFormat(
deviceIDString->stringValue,
sizeof deviceIDString->stringValue,
"%02X:%02X:%02X:%02X:%02X:%02X",
deviceID.bytes[0],
deviceID.bytes[1],
deviceID.bytes[2],
deviceID.bytes[3],
deviceID.bytes[4],
deviceID.bytes[5]);
HAPAssert(!err);
return kHAPError_None;
}