Skip to content

Commit

Permalink
Allow for auto repairing on a clean reject. Clean URLs. Bit more spac…
Browse files Browse the repository at this point in the history
…e for logo and text. Tweak descriptive text.
  • Loading branch information
dirkx committed Oct 30, 2021
1 parent b12e698 commit 1864be7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 47 deletions.
5 changes: 2 additions & 3 deletions display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void updateDisplay()
showLogo();
tft.loadFont(AA_FONT_LARGE);
tft.setTextColor(TFT_YELLOW, TFT_BLACK);
tft.drawString("get prices...", tft.width() / 2, tft.height() / 2 - 10);
tft.drawString("pricelist...", tft.width() / 2, tft.height() / 2 - 10);
break;
case WAIT_FOR_REGISTER_SWIPE:
showLogo();
Expand Down Expand Up @@ -215,7 +215,6 @@ void updateDisplay()
memset(tag, 0, sizeof(tag));
break;
case DID_OK:
Serial.println("DID_OK");
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.loadFont(AA_FONT_SMALL);
tft.drawString("paying..", tft.width() / 2, tft.height() / 2 - 52);
Expand Down Expand Up @@ -291,7 +290,7 @@ void displayForceShowError(char * str) {
void setTFTPower(bool onoff) {
Serial.println(onoff ? "Powering display on" : "Powering display off");
#ifdef TFT_BL
digitalWrite(TFT_BL, onoff ? TFT_BACKLIGHT_ON : !TFT_BACKLIGHT_ON);
digitalWrite(TFT_BL, onoff ? TFT_BACKLIGHT_ON : (!TFT_BACKLIGHT_ON));
#endif
#ifdef ST7735_DISPON
tft.writecommand(onoff ? ST7735_DISPON : ST7735_DISPOFF);
Expand Down
22 changes: 6 additions & 16 deletions global.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,10 @@
#define WIFI_PASSWD "MyWifiPassword"
#endif

#ifndef PAYMENT_TERMINAL_BEARER
// Must match line 245 in makerspaceleiden/settings.py of https://github.com/MakerSpaceLeiden/makerspaceleiden-crm
#define PAYMENT_TERMINAL_BEARER "not-so-very-secret-127.0.0.1"
#ifndef PAY_URL
#define PAY_URL "https://test.makerspaceleiden.nl:4443/crm/pettycash/api"
#endif

#ifndef PAYMENT_URL
#define PAYMENT_URL "https://test.makerspaceleiden.nl/test-server-crm/api/v1/pay"
#endif

#ifndef SDU_URL
#define SDU_URL "https://test.makerspaceleiden.nl/test-server-crm/api/v1/pay"
#endif

#ifndef SDU_URL
#define SDU_URL "https://test.makerspaceleiden.nl/test-server-crm/api/v1/pay"
#endif


#define HTTP_TIMEOUT (5000)

// Jump back to the default after this many milliseconds, provided
Expand All @@ -71,6 +57,10 @@

#define AMOUNT_NO_OK_NEEDED (5.0)

#ifndef NTP_POOL
#define NTP_POOL "nl.pool.ntp.org"
#endif

typedef enum { BOOT = 0, WAITING_FOR_NTP, FETCH_CA, REGISTER, WAIT_FOR_REGISTER_SWIPE, REGISTER_PRICELIST, ENTER_AMOUNT, OK_OR_CANCEL, DID_CANCEL, DID_OK, PAID, SCREENSAVER, FIRMWARE_UPDATE, FIRMWARE_FAIL, REGISTER_FAIL, OEPSIE } state_t;

// keep all ugly globals in one place for now.
Expand Down
25 changes: 15 additions & 10 deletions makerspaceleiden-payment-node.ino
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ bool loopRFID() {
// will reboot the unit early mornings.
//
void updateTimeAndRebootAtMidnight(bool force) {
static char lst[16] = { ' ', ' ' };
time_t now = time(nullptr);
char * p = ctime(&now);

// we keep a space in front; to wipe any (longer) strings). As
// the font is not monospaced.
//
Expand All @@ -137,10 +141,6 @@ void updateTimeAndRebootAtMidnight(bool force) {
Serial.printf("Heap: %d Kb\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024);
}

static char lst[16] = { ' ', ' ' };
time_t now = time(nullptr);
char * p = ctime(&now);

p += 11;
p[5] = 0; // or use 8 to also show seconds.

Expand Down Expand Up @@ -171,7 +171,7 @@ void settupButtons()
{
btn1.setPressedHandler([](Button2 & b) {
if (md == SCREENSAVER) {
update = true; md = ENTER_AMOUNT;
update = true;
return;
};
int l = amount;
Expand All @@ -189,7 +189,7 @@ void settupButtons()

btn2.setPressedHandler([](Button2 & b) {
if (md == SCREENSAVER) {
update = true; md = ENTER_AMOUNT;
update = true;
return;
};
int l = amount;
Expand Down Expand Up @@ -293,7 +293,7 @@ void setup()
// try to get some reliable time; to stop my cert
// checking code complaining.
//
configTzTime(cestTimezone, "nl.pool.ntp.org");
configTzTime(cestTimezone, NTP_POOL);

md = WAITING_FOR_NTP;
updateDisplay_progressText("Waiting for NTP");
Expand Down Expand Up @@ -333,9 +333,17 @@ void loop()
if (fetchPricelist())
md = ENTER_AMOUNT;
return;
case SCREENSAVER:
if (update) {
Serial.println("Wakeup");
setTFTPower(true);
md = ENTER_AMOUNT;
};
break;
case ENTER_AMOUNT:
if (millis() - lastchange > SCREENSAVER_TIMEOUT) {
md = SCREENSAVER;
Serial.println("Enabling screensaver (from enter)");
setTFTPower(false);
return;
};
Expand All @@ -355,9 +363,6 @@ void loop()
};

if (md != laststate || last_amount != amount) {
if (laststate == SCREENSAVER && md != SCREENSAVER) {
setTFTPower(true);
};
laststate = md;
lastchange = millis();
last_amount = amount;
Expand Down
2 changes: 1 addition & 1 deletion pins_ttgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
#define RFID_MISO 02
#define RFID_RESET 21

#define TFT_BL 14
// #define TFT_BL 14

#define TFT_ROTATION 1
44 changes: 27 additions & 17 deletions rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ const char * KS_KEY_CLIENT_CRT = "ccap";
const char * KS_KEY_CLIENT_KEY = "ckap";
const char * KS_KEY_SERVER_KEY = "ssk";

const unsigned short KS_VERSION = 0x100;
const char * stationname = NULL;

#define URL_NONE "https://makerspaceleiden.nl:4443/crm/pettycash/api/none"
const unsigned short KS_VERSION = 0x100;

#define URL "https://makerspaceleiden.nl:4443/crm/pettycash"
#define REGISTER_PATH "/api/v2/register"
#define NONE_PATH "/none"
#define REGISTER_PATH "/v2/register"
#define PAY_PATH "/v2/pay"

static unsigned char hex_digit(unsigned char c) {
return "0123456789ABCDEF"[c & 0x0F];
Expand Down Expand Up @@ -165,13 +166,13 @@ bool fetchCA() {
Serial.println("HTTPClient creation failed.");
goto exit;
}
Serial.printf("Heap: %d Kb\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL)/1024);
Serial.printf("Heap: %d Kb\n", heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024);

// Sadly required - due to a limitation in the current SSL stack we must
// provide the root CA. but we do not know it (yet). So learn it first.
//
client->setInsecure();
if (!https->begin(*client, URL_NONE )) {
if (!https->begin(*client, PAY_URL NONE_PATH )) {
Serial.println("Failed to begin https - fetchCA");
goto exit;
};
Expand All @@ -193,10 +194,10 @@ bool fetchCA() {

updateDisplay_progressText("CA Cert fetched");
if (paired)
md = REGISTER_PRICELIST;
md = REGISTER_PRICELIST;
else
md = REGISTER;

ok = true;
exit:
https->end();
Expand Down Expand Up @@ -229,7 +230,7 @@ bool registerDevice() {
client->setPrivateKey(client_key_as_pem);

if (md == REGISTER) {
snprintf((char *) buff, sizeof(buff), URL REGISTER_PATH "?name=%s",
snprintf((char *) buff, sizeof(buff), PAY_URL REGISTER_PATH "?name=%s",
_argencode((char *) tmp, sizeof(tmp), terminalName));

if (!https.begin(*client, (const char*)buff)) {
Expand Down Expand Up @@ -264,7 +265,7 @@ bool registerDevice() {
mbedtls_sha256_context sha_ctx;
mbedtls_sha256_init(&sha_ctx);
mbedtls_sha256_starts_ret(&sha_ctx, 0);

// we happen to know that the first two can safely be treated as strings.
//
mbedtls_sha256_update_ret(&sha_ctx, (unsigned char*) nonce, strlen(nonce));
Expand All @@ -275,7 +276,7 @@ bool registerDevice() {
sha256toHEX(sha256, (char*)tmp);
mbedtls_sha256_free(&sha_ctx);

snprintf((char *) buff, sizeof(buff), URL REGISTER_PATH "?response=%s", (char *)tmp);
snprintf((char *) buff, sizeof(buff), PAY_URL REGISTER_PATH "?response=%s", (char *)tmp);

if (!https.begin(*client, (char *)buff )) {
Serial.println("Failed to begin https");
Expand Down Expand Up @@ -430,22 +431,21 @@ JSONVar rest(const char *url, int * statusCode) {
return res;
}


int payByREST(char *tag, char * amount, char *lbl) {
char buff[512];
char desc[128];
char tmp[128];

snprintf(desc, sizeof(desc), "%s. Payment at terminal %s", lbl, terminalName);
snprintf(desc, sizeof(desc), "%s. Paid at %s", lbl, stationname);

// avoid logging the tag for privacy/security-by-obscurity reasons.
//
snprintf(buff, sizeof(buff), PAYMENT_URL "?node=%s&src=%s&amount=%s&description=%s",
snprintf(buff, sizeof(buff), PAY_URL PAY_PATH "?node=%s&src=%s&amount=%s&description=%s",
terminalName, "XX-XX-XX-XXX", amount, _argencode(tmp, sizeof(tmp), desc));
Serial.print("URL : ");
Serial.println(buff);

snprintf(buff, sizeof(buff), PAYMENT_URL "?node=%s&src=%s&amount=%s&description=%s",
snprintf(buff, sizeof(buff), PAY_URL PAY_PATH "?node=%s&src=%s&amount=%s&description=%s",
terminalName, tag, amount, _argencode(tmp, sizeof(tmp), desc));

int httpCode = 0;
Expand All @@ -472,18 +472,28 @@ bool fetchPricelist() {

updateDisplay_progressText("fetching prices");

JSONVar res = rest(URL REGISTER_PATH, &httpCode);
JSONVar res = rest(PAY_URL REGISTER_PATH, &httpCode);
if (httpCode == 400) {
// propably too soon/fast
updateDisplay_progressText("re-pairing wit existing key");
md = REGISTER;
};
if (httpCode != 200) {
Serial.println("SKU price list fetch failed.");
return false;
}

const char * nme = res["name"]; // need to strdup them if we weant to use them elsewhere.
const char * nme = res["name"];
const char * desc = res["description"];
if (!nme || !strlen(nme)) {
Serial.println("Bogus SKU or not yet assiged a station");
updateDisplay_progressText("no station assigned in CRM");
return false;
}
if (desc && strlen(desc))
stationname = strdup(desc);
else
stationname = terminalName;

len = res["pricelist"].length();
if (len < 0 || len > 256) {
Expand Down

0 comments on commit 1864be7

Please sign in to comment.