Skip to content

Commit f439f27

Browse files
committed
Refactor 'readDeviceId' to have a boolean return value like 'reconstructCertificate' which allows for a similiar check/control flow in 'ArduinoIoTCloud::begin'
1 parent 74128b4 commit f439f27

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/ArduinoIoTCloudTCP.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) {
7676
#ifdef BOARD_HAS_ECCX08
7777
if (!ECCX08.begin()) { Debug.print(DBG_ERROR, "Cryptography processor failure. Make sure you have a compatible board."); return 0; }
7878

79-
_device_id = CryptoUtil::readDeviceId(ECCX08, ECCX08Slot::DeviceId);
80-
if(_device_id.length() == 0) { Debug.print(DBG_ERROR, "Cryptography processor read failure."); return 0; }
79+
if (!CryptoUtil::readDeviceId(ECCX08, _device_id, ECCX08Slot::DeviceId)) { Debug.print(DBG_ERROR, "Cryptography processor read failure."); return 0; }
8180

8281
if (!CryptoUtil::reconstructCertificate(ECCX08Cert, _device_id, ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier)) { Debug.print(DBG_ERROR, "Cryptography certificate reconstruction failure."); return 0; }
8382

src/utility/crypto/CryptoUtil.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727
PUBLIC MEMBER FUNCTIONS
2828
******************************************************************************/
2929

30-
String CryptoUtil::readDeviceId(ECCX08Class & eccx08, ECCX08Slot const slot)
30+
bool CryptoUtil::readDeviceId(ECCX08Class & eccx08, String & device_id, ECCX08Slot const device_id_slot)
3131
{
3232
byte device_id_bytes[72] = {0};
3333

34-
if (eccx08.readSlot(static_cast<int>(slot), device_id_bytes, sizeof(device_id_bytes))) {
35-
return String(reinterpret_cast<char *>(device_id_bytes));
36-
} else {
37-
return String("");
34+
if (eccx08.readSlot(static_cast<int>(device_id_slot), device_id_bytes, sizeof(device_id_bytes))) {
35+
device_id = String(reinterpret_cast<char *>(device_id_bytes));
36+
return true;
37+
}
38+
else
39+
{
40+
return false;
3841
}
3942
}
4043

src/utility/crypto/CryptoUtil.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ class CryptoUtil
5050
{
5151
public:
5252

53-
static String readDeviceId(ECCX08Class & eccx08, ECCX08Slot const slot);
54-
static bool reconstructCertificate(ECCX08CertClass & cert, String const & device_id, ECCX08Slot const key, ECCX08Slot const compressed_certificate, ECCX08Slot const serial_number_and_authority_key);
53+
static bool readDeviceId(ECCX08Class & eccx08, String & device_id, ECCX08Slot const device_id_slot);
54+
static bool reconstructCertificate(ECCX08CertClass & cert, String const & device_id, ECCX08Slot const key, ECCX08Slot const compressed_certificate, ECCX08Slot const serial_number_and_authority_key);
5555

5656

5757
private:

0 commit comments

Comments
 (0)