Skip to content

Commit f5a3259

Browse files
committed
Better key distribution topic extraction
1 parent 7edb87f commit f5a3259

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

Firmware/RTK_Surveyor/menuPP.ino

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,17 @@ bool pointperfectProvisionDevice()
398398

399399
strcpy(settings.pointPerfectClientID, (const char *)((*jsonZtp)["clientId"]));
400400
strcpy(settings.pointPerfectBrokerHost, (const char *)((*jsonZtp)["brokerHost"]));
401-
strcpy(settings.pointPerfectLBandTopic, (const char *)((*jsonZtp)["subscriptions"][0]["path"]));
401+
402+
// Note: from the ZTP documentation:
403+
// ["subscriptions"][0] will contain the key distribution topic
404+
// But, assuming the key distribution topic is always ["subscriptions"][0] is potentially brittle
405+
// It is safer to check the "description" contains "key distribution topic"
406+
int subscription =
407+
findZtpJSONEntry("subscriptions", "description", "key distribution topic", jsonZtp);
408+
if (subscription >= 0)
409+
strncpy(settings.pointPerfectLBandTopic,
410+
(const char *)((*jsonZtp)["subscriptions"][subscription]["path"]),
411+
sizeof(settings.pointPerfectLBandTopic));
402412

403413
strcpy(settings.pointPerfectCurrentKey,
404414
(const char *)((*jsonZtp)["dynamickeys"]["current"]["value"]));
@@ -451,6 +461,26 @@ bool pointperfectProvisionDevice()
451461
#endif // COMPILE_WIFI
452462
}
453463

464+
// Find thing3 in (*jsonZtp)[thing1][n][thing2]. Return n on success. Return -1 on error / not found.
465+
int findZtpJSONEntry(const char *thing1, const char *thing2, const char *thing3, DynamicJsonDocument *jsonZtp)
466+
{
467+
if (!jsonZtp)
468+
return (-1);
469+
470+
int i = (*jsonZtp)[thing1].size();
471+
472+
if (i == 0)
473+
return (-1);
474+
475+
for (int j = 0; j < i; j++)
476+
if (strstr((const char *)(*jsonZtp)[thing1][j][thing2], thing3) != nullptr)
477+
{
478+
return j;
479+
}
480+
481+
return (-1);
482+
}
483+
454484
// Check certificate and privatekey for valid formatting
455485
// Return false if improperly formatted
456486
bool checkCertificates()
@@ -618,8 +648,6 @@ bool pointperfectUpdateKeys()
618648
// Successful connection
619649
systemPrintln("MQTT connected");
620650

621-
// Originally the provisioning process reported the '/pp/key/Lb' channel which fails to respond with
622-
// keys. Looks like they fixed it to /pp/ubx/0236/Lb.
623651
mqttClient.subscribe(settings.pointPerfectLBandTopic);
624652
}
625653
else

Firmware/RTK_Surveyor/settings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ typedef struct
961961
bool autoKeyRenewal = true; // Attempt to get keys if we get under 28 days from the expiration date
962962
char pointPerfectClientID[50] = "";
963963
char pointPerfectBrokerHost[50] = ""; // pp.services.u-blox.com
964-
char pointPerfectLBandTopic[20] = ""; // /pp/key/Lb
964+
char pointPerfectLBandTopic[20] = ""; // /pp/ubx/0236/Lb
965965

966966
char pointPerfectCurrentKey[33] = ""; // 32 hexadecimal digits = 128 bits = 16 Bytes
967967
uint64_t pointPerfectCurrentKeyDuration = 0;

0 commit comments

Comments
 (0)