Skip to content

Commit

Permalink
Examples: provisioning, add wifi version check and prompt retry for U…
Browse files Browse the repository at this point in the history
…NO R4 WiFi
  • Loading branch information
pennam committed Feb 16, 2024
1 parent 31fa0f8 commit 84fc709
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions examples/utility/Provisioning/Provisioning.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#include <WiFiNINA.h>
#define LATEST_WIFI_FIRMWARE_VERSION WIFI_FIRMWARE_LATEST_VERSION
#endif
#if defined(ARDUINO_UNOR4_WIFI)
#include <WiFiS3.h>
#define LATEST_WIFI_FIRMWARE_VERSION WIFI_FIRMWARE_LATEST_VERSION
#endif

String promptAndReadLine(const char* prompt, const unsigned int timeout = 0);

void setup() {
Serial.begin(9600);
Expand Down Expand Up @@ -51,7 +57,7 @@ void setup() {
}

/* WARNING: This string is parsed from IoTCloud frontend */
String csrConfirm = promptAndReadLine("Would you like to generate a new private key and CSR (y/N): ");
String csrConfirm = promptAndReadLine("Would you like to generate a new private key and CSR (y/N): ", 5000);
csrConfirm.toLowerCase();

if (csrConfirm != "y") {
Expand Down Expand Up @@ -86,6 +92,7 @@ void setup() {

Serial.println("Generated CSR is:");
Serial.println();
/* WARNING: This string is parsed from IoTCloud frontend */
Serial.println(csr);

String issueYear = promptAndReadLine("Please enter the issue year of the certificate (2000 - 2031): ");
Expand All @@ -111,6 +118,7 @@ void setup() {
}

if (!Certificate.begin()) {
/* WARNING: This string is parsed from IoTCloud frontend */
Serial.println("Error starting crypto storage!");
while (1);
}
Expand All @@ -133,12 +141,13 @@ void setup() {
Serial.println("Error building cert!");
while (1);
}

if (!SElementArduinoCloudCertificate::write(secureElement, Certificate, SElementArduinoCloudSlot::CompressedCertificate)) {
Serial.println("Error storing cert!");
while (1);
}

/* WARNING: This string is parsed from IoTCloud frontend */
Serial.println("Compressed cert = ");

const byte* certData = Certificate.bytes();
Expand Down Expand Up @@ -193,18 +202,32 @@ void setup() {
void loop() {
}

String promptAndReadLine(const char* prompt) {
Serial.print(prompt);
String s = readLine();
String promptAndReadLine(const char* prompt, const unsigned int timeout) {
String s = "";
while(1) {
Serial.print(prompt);
s = readLine(timeout);
if (s.length() > 0) {
break;
}
}
Serial.println(s);

return s;
}

String readLine() {
String line;
bool isExpired(const unsigned int start, const unsigned int timeout) {
if (timeout) {
return (millis() - start) > timeout;
} else {
return false;
}
}

while (1) {
String readLine(const unsigned int timeout) {
String line;
const unsigned int start = millis();
while (!isExpired(start, timeout)) {
if (Serial.available()) {
char c = Serial.read();

Expand Down

0 comments on commit 84fc709

Please sign in to comment.