remote.ino (1499B) download
1#include "include/config.h"
2#include "include/remote.h"
3
4static JSONVar readJSON() {
5 char line[lineBuffer];
6 char buf;
7 int i = 0;
8 for (;;) {
9 while (!usbSerial.available())
10 ;
11 buf = usbSerial.read();
12 if (buf == '\r')
13 continue;
14 if (buf == '\n')
15 break;
16 line[i++] = buf;
17 }
18 line[i++] = '\0';
19
20 return JSON.parse(line);
21}
22
23void serial_remote::begin() {
24 usbSerial.println("{\"command\":\"hello\"}");
25 JSONVar res_json = readJSON();
26 if (res_json["error"] != nullptr) {
27 // :(
28 }
29 write
30}
31
32bool serial_remote::available() {
33 return usbSerial;
34}
35
36void serial_remote::connect(const char* host, int port) {
37 JSONVar body;
38 body["command"] = "connect";
39 body["host"] = host;
40 body["port"] = port;
41
42 usbSerial.println(body);
43}
44
45const char* serial_remote::send(http_packet request, http_packet& response) {
46 JSONVar body;
47 body["command"] = "send";
48 body["method"] = request.method;
49 body["endpoint"] = request.endpoint;
50 body["headers"] = request.headers;
51 body["body"] = request.body;
52 usbSerial.println(body);
53
54 JSONVar res_json = readJSON();
55 response.body = res_json["body"];
56 response.headers = res_json["headers"];
57
58 return res_json["error"];
59}
60
61
62const char* serial_remote::send(http_packet request) {
63 JSONVar body;
64 body["command"] = "send";
65 body["method"] = request.method;
66 body["endpoint"] = request.endpoint;
67 body["headers"] = request.headers;
68 body["body"] = request.body;
69 usbSerial.println(body);
70
71 JSONVar res_json = readJSON();
72
73 return res_json["error"];
74}