Skip to content

Commit 83a7b7b

Browse files
Restructure HQ connection handling
This commit: - Renames a few functions - Removes the host and port arguments from the wifi.config bitlash commands. HQ host and port are no longer provisioned, but hardcoded in the sketch. - Adds suport for TLS connections (disabled still, though). - Introduces an HqHandler class which now contains just the connection info for the HQ but will later get more HQ-related code.
1 parent f800918 commit 83a7b7b

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

HqHandler.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef LIB_PINOCCIO_HQHANDLER_H_
2+
#define LIB_PINOCCIO_HQHANDLER_H_
3+
4+
#include <stddef.h>
5+
#include <stdint.h>
6+
7+
/**
8+
* This class handles direct connections to the HQ server (e.g., through
9+
* wifi).
10+
*/
11+
class HqHandler {
12+
public:
13+
14+
// TODO: Move more code into here.
15+
16+
/////////////////////////////////////////
17+
// These are defined in HQInfo.cpp
18+
/////////////////////////////////////////
19+
20+
/** Hostname of the hq server */
21+
static const char host[];
22+
/** Port of the hq server */
23+
static const uint16_t port;
24+
/** The CA certificate for the hq server. */
25+
static const uint8_t cacert[];
26+
/** The length of cacert. Is 0 when TLS should not be used. */
27+
static const size_t cacert_len;
28+
};
29+
30+
#endif // LIB_PINOCCIO_HQHANDLER_H_

HqInfo.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "HqHandler.h"
2+
3+
//#define USE_TLS
4+
5+
6+
const char HqHandler::host[] = "api.pinocc.io";
7+
#ifndef USE_TLS
8+
// 22757 for TLS, 22756 for plain
9+
const uint16_t HqHandler::port = 22756;
10+
const uint8_t HqHandler::cacert[] = {};
11+
const size_t HqHandler::cacert_len = 0;
12+
#else
13+
// 22757 for TLS, 22756 for plain
14+
const uint16_t HqHandler::port = 22757;
15+
16+
// CA certificate that signed the server certificate.
17+
// - Using the server certificate here doesn't work, only the CA that
18+
// signed it is checked (except self-signed certificates where the
19+
// CA and server certificates are the same, though this was not
20+
// tested).
21+
// - No checks of the server certificate (like hostname) are done,
22+
// other than to confirm that it was indeed signed by the right CA.
23+
// This means that if you use a server certificate signed by a
24+
// commercial CA, _any_ other certificate signed by the same CA
25+
// will also pass the check, which is probably not what you want...
26+
// - This should be a certificate in (binary) DER format. To convert
27+
// it to something that can be pasted below, you can use the
28+
// `xxd -i` command, which should be available on Linux and MacOS X.
29+
//
30+
const uint8_t HqHandler::cacert[] = {
31+
//TODO
32+
};
33+
const size_t HqHandler::cacert_len = sizeof(cacert);
34+
#endif

ScoutHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void PinoccioScoutHandler::setup() {
1818

1919
Serial.print("Wi-Fi backpack connecting...");
2020
Scout.wifi.setup();
21-
Scout.wifi.autoConnect();
21+
Scout.wifi.autoConnectHq();
2222
Serial.println("Done");
2323
RgbLed.blinkGreen();
2424

Shell.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ static numvar wifiList(void) {
622622
}
623623

624624
static numvar wifiConfig(void) {
625-
if (!Scout.wifi.autoConfig((const char *)getstringarg(1), (const char *)getstringarg(2), (const char *)getstringarg(3), getarg(4))) {
625+
if (!Scout.wifi.wifiConfig((const char *)getstringarg(1), (const char *)getstringarg(2))) {
626626
Serial.println("Error: saving Scout.wifi.configuration data failed");
627627
}
628628
}

utility/WiFiBackpack.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#include <SPI.h>
33
#include <utility/WiFiBackpack.h>
44
#include "../ScoutHandler.h"
5+
#include "../HqHandler.h"
6+
7+
#define CA_CERTNAME_HQ "hq-ca"
58

69
static void print_line(const uint8_t *buf, uint16_t len, void *data) {
710
static_cast<Print*>(data)->write(buf, len);
@@ -23,7 +26,14 @@ bool WiFiBackpack::setup() {
2326
SPI.begin();
2427
SPI.setClockDivider(SPI_CLOCK_DIV8);
2528

26-
return gs.begin(7);
29+
30+
if (!gs.begin(7))
31+
return false;
32+
33+
#ifdef HQ_USE_TLS
34+
if (HqHandler::cacert_len)
35+
gs.addCert(CA_CERTNAME_HQ, /* to_flash */ false, HqHandler::cacert, HqHandler::cacert_len);
36+
#endif
2737
}
2838

2939
void WiFiBackpack::loop() {
@@ -32,31 +42,33 @@ void WiFiBackpack::loop() {
3242
if (!client.connected()) {
3343
hqConnected = false;
3444
} else if (!hqConnected) {
35-
leadHQConnect();
36-
hqConnected = true;
45+
if (HqHandler::cacert_len == 0 || client.enableTls(CA_CERTNAME_HQ)) {
46+
leadHQConnect();
47+
hqConnected = true;
48+
}
3749
}
3850
// TODO: Don't call leadHQConnect directly
3951
// TODO: There is a race condition here: If a disconnect and connect
4052
// happen quickly before we can notice the disconnect, leadHqConnect
4153
// will not be called for the new connection.
4254
}
4355

44-
bool WiFiBackpack::autoConfig(const char *ssid, const char *passphrase, const String& host, uint16_t port) {
56+
bool WiFiBackpack::wifiConfig(const char *ssid, const char *passphrase) {
4557
bool ok = true;
4658
ok = ok && gs.setSecurity(GSModule::GS_SECURITY_AUTO);
4759
ok = ok && gs.setWpaPassphrase(passphrase);
4860
ok = ok && gs.setAutoAssociate(ssid);
49-
ok = ok && gs.setAutoConnectClient(host.c_str(), port);
5061
// Remember these settings through a reboot
5162
ok = ok && gs.saveProfile(0);
5263
ok = ok && gs.setDefaultProfile(0);
5364
return ok;
5465
}
5566

56-
bool WiFiBackpack::autoConnect() {
67+
bool WiFiBackpack::autoConnectHq() {
5768
// Try to disable the NCM in case it's already running
5869
gs.setNcm(false);
59-
return gs.setNcm(/* enable */ true, /* associate_only */ false, /* remember */ false);
70+
return gs.setAutoConnectClient(HqHandler::host, HqHandler::port) &&
71+
gs.setNcm(/* enable */ true, /* associate_only */ false, /* remember */ false);
6072
}
6173

6274
void WiFiBackpack::printAPs(Print& p) {

utility/WiFiBackpack.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class WiFiBackpack : public Backpack {
1515
bool init();
1616
void loop();
1717

18-
bool autoConfig(const char *ssid, const char *passphrase, const String &host, uint16_t port);
19-
bool autoConnect();
18+
bool wifiConfig(const char *ssid, const char *passphrase);
19+
bool autoConnectHq();
2020

2121
void printAPs(Print& p);
2222
void printProfiles(Print& p);

0 commit comments

Comments
 (0)