-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakey.cpp
More file actions
62 lines (51 loc) · 1.59 KB
/
makey.cpp
File metadata and controls
62 lines (51 loc) · 1.59 KB
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
#include <Arduino.h>
#include <Ethernet.h>
#include "makey.h"
char webBuffer[256];
int webBytes = 0;
Makey::Makey(const char* url, int port, EthernetClient *client) {
this->url = url;
this->port = port;
this->client = client;
}
MakeyResponse Makey::request(const char* path, const char** headers, int headerCount, const char* body, long timeout) {
int retValue = client->connect(url, port);
client->print("GET ");
client->print(path);
client->println(" HTTP/1.1");
for(int i=0;i<headerCount;i++) {
client->println(headers[i]);
}
client->println();
client->println(body);
MakeyResponse res;
webBytes = 0;
long startMillis = millis();
while (client->connected()) {
int len = client->available();
if (len > 0) {
if (webBytes + len > 255) len = 255 - webBytes;
client->read((uint8_t*)&(webBuffer[webBytes]), len);
webBytes = webBytes + len;
}
if(millis() > startMillis + timeout) {
client->stop();
res.statusCode = 0;
return res;
}
}
client->stop();
webBuffer[webBytes] = 0;
String response = String(webBuffer);
res.rawBody = response;
res.body = response.substring(response.indexOf("\r\n\r\n") + 4);
res.statusCode = (int)response.substring(response.indexOf(" ") + 1, response.indexOf(" ") + 4).toInt();
return res;
}
void Makey::rotateKey() {
MakeyResponse res = request("/hubot/randombytes", {}, 0, "", 20000);
while(res.statusCode != 200) {
res = request("/hubot/randombytes", {}, 0, "", 20000);
}
memcpy(key, res.body.c_str(), 16);
}