Skip to content

Commit 5865761

Browse files
authored
Merge pull request #3 from tectrolabs/add_device_validation_feature
Add device validation feature
2 parents 68e444d + cd74abf commit 5865761

File tree

4 files changed

+72
-28
lines changed

4 files changed

+72
-28
lines changed

mcrng/MicroRngSPI.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2014-2020 TectroLabs LLC, https://tectrolabs.com
2+
* Copyright (C) 2014-2022 TectroLabs LLC, https://tectrolabs.com
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the "Software"),
@@ -23,8 +23,8 @@
2323
/**
2424
* @file MicroRngSPI.cpp
2525
* @author Andrian Belinski
26-
* @date 04/21/2020
27-
* @version 1.0
26+
* @date 06/07/2022
27+
* @version 1.1
2828
*
2929
* @brief communicates with MicroRNG device through SPI interface on Raspberry PI 3+ or other Linux-based single-board computers.
3030
*
@@ -220,6 +220,41 @@ bool MicroRngSPI::executeCommand(char cmd, uint8_t *rx)
220220
return exhangeByte(cmd, rx);
221221
}
222222

223+
/**
224+
* Check to see if the MicroRNG device is actually responding to requests.
225+
*
226+
* @return true when validated successfully
227+
*/
228+
bool MicroRngSPI::validateDevice()
229+
{
230+
if (!isConnected())
231+
{
232+
return false;
233+
}
234+
235+
uint8_t beginTransactionID;
236+
for (int i = 1; i <= 257; ++i)
237+
{
238+
uint8_t transactionID;
239+
if (!executeCommand('t', &transactionID))
240+
{
241+
return false;
242+
}
243+
244+
if (i == 1)
245+
{
246+
beginTransactionID = transactionID;
247+
}
248+
249+
if ( i != 1 && transactionID != ++beginTransactionID)
250+
{
251+
sprintf(lastError, "MicroRNG device not found");
252+
return false;
253+
}
254+
}
255+
return true;
256+
}
257+
223258
/**
224259
* Retrieve MicroRNG internal status.
225260
*

mcrng/MicroRngSPI.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2014-2020 TectroLabs LLC, https://tectrolabs.com
2+
* Copyright (C) 2014-2022 TectroLabs LLC, https://tectrolabs.com
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the "Software"),
@@ -23,8 +23,8 @@
2323
/**
2424
* @file MicroRngSPI.h
2525
* @author Andrian Belinski
26-
* @date 04/21/2020
27-
* @version 1.0
26+
* @date 06/07/2022
27+
* @version 1.1
2828
*
2929
* @brief communicates with MicroRNG device through SPI interface on Raspberry PI 3+ or other Linux-based single-board computers.
3030
*
@@ -53,6 +53,7 @@ class MicroRngSPI
5353
virtual ~MicroRngSPI();
5454
bool isConnected();
5555
bool connect(const char *devicePath);
56+
bool validateDevice();
5657
bool disconnect();
5758
bool executeCommand(char cmd, uint8_t *rx);
5859
const char* getLastErrMsg();

mcrng/mcrng.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2014-2020 TectroLabs LLC, https://tectrolabs.com
2+
* Copyright (C) 2014-2022 TectroLabs LLC, https://tectrolabs.com
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the "Software"),
@@ -23,8 +23,8 @@
2323
/**
2424
* @file mcrng.cpp
2525
* @author Andrian Belinski
26-
* @date 04/21/2020
27-
* @version 1.0
26+
* @date 06/-7/2022
27+
* @version 1.1
2828
*
2929
* @brief downloads random bytes from MicroRNG device through SPI interface on Raspberry PI 3+ or other Linux-based single-board computers.
3030
*
@@ -220,15 +220,20 @@ int handleDownloadRequest()
220220

221221
uint8_t receiveByteBuffer[MCR_BUFF_FILE_SIZE_BYTES];
222222

223-
bool status = spi.connect(devicePath);
224-
if (!status)
223+
if (!spi.connect(devicePath))
225224
{
226225
fprintf(stderr, " Cannot open SPI device %s, error: %s ... \n", devicePath, spi.getLastErrMsg());
227226
return -1;
228227
}
229228

230229
spi.setMaxClockFrequency(maxSpiMasterClock);
231230

231+
if (!spi.validateDevice())
232+
{
233+
fprintf(stderr, " Cannot access device, error: %s ... \n", spi.getLastErrMsg());
234+
return -1;
235+
}
236+
232237
if (filePathName == NULL)
233238
{
234239
fprintf(stderr, "No file name defined.\n");
@@ -253,8 +258,7 @@ int handleDownloadRequest()
253258
while (numGenBytes == -1)
254259
{
255260
// Infinite loop for downloading unlimited random bytes
256-
status = spi.retrieveRandomBytes(MCR_BUFF_FILE_SIZE_BYTES, receiveByteBuffer);
257-
if (!status)
261+
if (!spi.retrieveRandomBytes(MCR_BUFF_FILE_SIZE_BYTES, receiveByteBuffer))
258262
{
259263
fprintf(stderr,
260264
"Failed to receive %d bytes for unlimited download, error: %s. \n",
@@ -274,8 +278,7 @@ int handleDownloadRequest()
274278
int64_t chunkNum;
275279
for (chunkNum = 0; chunkNum < numCompleteChunks; chunkNum++)
276280
{
277-
status = spi.retrieveRandomBytes(MCR_BUFF_FILE_SIZE_BYTES, receiveByteBuffer);
278-
if (!status)
281+
if (!spi.retrieveRandomBytes(MCR_BUFF_FILE_SIZE_BYTES, receiveByteBuffer))
279282
{
280283
fprintf(stderr, "Failed to receive %d bytes, error: %s. \n",
281284
MCR_BUFF_FILE_SIZE_BYTES, spi.getLastErrMsg());
@@ -287,8 +290,7 @@ int handleDownloadRequest()
287290
if (chunkRemaindBytes > 0)
288291
{
289292
//Process incomplete chunk
290-
status = spi.retrieveRandomBytes(chunkRemaindBytes, receiveByteBuffer);
291-
if (!status)
293+
if (!spi.retrieveRandomBytes(chunkRemaindBytes, receiveByteBuffer))
292294
{
293295
fprintf(stderr, "Failed to receive %d bytes for last chunk, error: code %s. ",
294296
chunkRemaindBytes,spi.getLastErrMsg());

mcrng/sample.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (C) 2014-2020 TectroLabs LLC, https://tectrolabs.com
2+
* Copyright (C) 2014-2022 TectroLabs LLC, https://tectrolabs.com
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining
55
* a copy of this software and associated documentation files (the "Software"),
@@ -23,8 +23,8 @@
2323
/**
2424
* @file sample.cpp
2525
* @author Andrian Belinski
26-
* @date 04/21/2020
27-
* @version 1.0
26+
* @date 06/07/2022
27+
* @version 1.1
2828
*
2929
* @brief a sample C program that demonstrates how to retrieve random bytes from MicroRNG device through SPI interface on Raspberry PI 3+ or other Linux-based single-board computers.
3030
*
@@ -49,23 +49,29 @@ int main(int argc, char **argv) {
4949
printf("--------------------------------------------------------------------------\n");
5050
printf("--- Sample C program for retrieving random bytes from MicroRNG device ----\n");
5151
printf("--- Use with RPI 3+ or other Linux-based single-board computers ---\n");
52-
printf("--------------------------------------------------------------------------\n");
52+
printf("--------------------------------------------------------------------------\n");
5353

54-
setbuf(stdout, NULL);
54+
setbuf(stdout, NULL);
5555

56-
if (argc < 2)
57-
{
58-
printf("Usage: sample <spi device>\n");
59-
printf("Example: sample /dev/spidev0.0\n");
60-
return -1;
61-
}
56+
if (argc < 2)
57+
{
58+
printf("Usage: sample <spi device>\n");
59+
printf("Example: sample /dev/spidev0.0\n");
60+
return -1;
61+
}
6262

6363
char *devicePath = argv[1];
6464

6565
if (!spi.connect(devicePath)) {
6666
printf("%s\n", spi.getLastErrMsg());
6767
return -1;
6868
}
69+
70+
if (!spi.validateDevice()) {
71+
printf("%s\n", spi.getLastErrMsg());
72+
return -1;
73+
}
74+
6975
printf("\nMicroRNG device open successfully, SPI clock frequency: %8ld Hz\n\n", (long)spi.getMaxClockFrequency());
7076

7177
// Retrieve random bytes from device

0 commit comments

Comments
 (0)