forked from etwmc/Personal-HomeKit-HAP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PHKControllerRecord.cpp
99 lines (82 loc) · 2.2 KB
/
PHKControllerRecord.cpp
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//
// PHKKeyRecord.cpp
// Workbench
//
// Created by Wai Man Chan on 9/23/14.
//
//
#include "PHKControllerRecord.h"
#include "Configuration.h"
#include <vector>
#include <strings.h>
#if MCU
#else
#include <fstream>
#endif
using namespace std;
vector<PHKKeyRecord>readIn();
vector<PHKKeyRecord>controllerRecords = readIn();
vector<PHKKeyRecord>readIn() {
ifstream fs;
#if MCU
#else
fs.open(controllerRecordsAddress, std::ifstream::in);
#endif
char buffer[69];
PHKKeyRecord record;
vector<PHKKeyRecord> results;
#if MCU
#else
while (fs.is_open()&&fs.good()&&!fs.eof()) {
fs.get(buffer, 69);
#endif
bcopy(buffer, record.controllerID, 36);
bcopy(&buffer[36], record.publicKey, 32);
results.push_back(record);
}
#if MCU
#else
fs.close();
#endif
return results;
}
void addControllerKey(PHKKeyRecord record) {
if (doControllerKeyExist(record) == false) {
controllerRecords.push_back(record);
#if MCU
#else
ofstream fs;
fs.open(controllerRecordsAddress, std::ofstream::trunc);
#endif
for (vector<PHKKeyRecord>::iterator it = controllerRecords.begin(); it != controllerRecords.end(); it++) {
#if MCU
#else
fs.write(it->controllerID, 36);
fs.write(it->publicKey, 32);
#endif
}
fs.close();
}
}
bool doControllerKeyExist(PHKKeyRecord record) {
for (vector<PHKKeyRecord>::iterator it = controllerRecords.begin(); it != controllerRecords.end(); it++) {
if (bcmp((*it).controllerID, record.controllerID, 32) == 0) return true;
}
return false;
}
void removeControllerKey(PHKKeyRecord record) {
for (vector<PHKKeyRecord>::iterator it = controllerRecords.begin(); it != controllerRecords.end(); it++) {
if (bcmp((*it).controllerID, record.controllerID, 32) == 0) {
controllerRecords.push_back(record);
return;
}
}
}
PHKKeyRecord getControllerKey(char key[32]) {
for (vector<PHKKeyRecord>::iterator it = controllerRecords.begin(); it != controllerRecords.end(); it++) {
if (bcmp(key, it->controllerID, 32) == 0) return *it;
}
PHKKeyRecord emptyRecord;
bzero(emptyRecord.controllerID, 32);
return emptyRecord;
}