forked from etwmc/Personal-HomeKit-HAP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PHKControllerRecord.cpp
executable file
·116 lines (93 loc) · 2.58 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// PHKKeyRecord.cpp
// Workbench
//
// Created by Wai Man Chan on 9/23/14.
//
//
#include <vector>
#include <strings.h>
#if MCU
#else
#include <fstream>
#endif
using namespace std;
#include "PHKControllerRecord.h"
#include "Configuration.h"
void ControllerRecord::loadController() {
ifstream fs;
#if MCU
#else
fs.open(this->storeFilePath.c_str(), std::ifstream::in);
#endif
char buffer[70] = {0};
PHKKeyRecord record;
vector<PHKKeyRecord> results;
#if MCU
#else
bool isEmpty = fs.peek() == EOF;
while (!isEmpty&&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
this->controllerRecords = results;
}
void ControllerRecord::resetController() {
unlink(this->storeFilePath.c_str() );
this->controllerRecords.clear();
}
bool ControllerRecord::hasController() {
return this->controllerRecords.size() > 0;
}
void ControllerRecord::storeFiles()
{
#if MCU
#else
ofstream fs;
fs.open(this->storeFilePath.c_str(), std::ofstream::trunc);
#endif
for (vector<PHKKeyRecord>::iterator it = this->controllerRecords.begin(); it != this->controllerRecords.end(); it++) {
#if MCU
#else
fs.write(it->controllerID, 36);
fs.write(it->publicKey, 32);
#endif
}
fs.close();
}
void ControllerRecord::addControllerKey(PHKKeyRecord record) {
if (doControllerKeyExist(record) == false) {
this->controllerRecords.push_back(record);
storeFiles();
}
}
bool ControllerRecord::doControllerKeyExist(PHKKeyRecord record) {
for (vector<PHKKeyRecord>::iterator it = this->controllerRecords.begin(); it != this->controllerRecords.end(); it++) {
if (bcmp((*it).controllerID, record.controllerID, 32) == 0) return true;
}
return false;
}
void ControllerRecord::removeControllerKey(PHKKeyRecord record) {
for (vector<PHKKeyRecord>::iterator it = this->controllerRecords.begin(); it != this->controllerRecords.end(); it++) {
if (bcmp((*it).controllerID, record.controllerID, 32) == 0) {
this->controllerRecords.erase(it);
storeFiles();
return;
}
}
}
PHKKeyRecord ControllerRecord::getControllerKey(char key[32]) {
for (vector<PHKKeyRecord>::iterator it = this->controllerRecords.begin(); it != this->controllerRecords.end(); it++) {
if (bcmp(key, it->controllerID, 32) == 0) return *it;
}
PHKKeyRecord emptyRecord;
bzero(emptyRecord.controllerID, 32);
return emptyRecord;
}