-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unique ID to connection parameter. (#84)
* Minor C++ refactor. Use static_cast where possible. Migrate to class function binding rather than static. Default to parent class constructor using initialisers. * Add unique ID to connection parameter. When connecting, append a subset of the ESP32 'unique' identifier, which is derived from the device MAC address.
- Loading branch information
Showing
13 changed files
with
134 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <Esp.h> | ||
|
||
#include "Device.h" | ||
#include "FurbleTypes.h" | ||
|
||
namespace Furble { | ||
|
||
Device::uuid128_t Device::g_Uuid; | ||
char Device::g_StringID[DEVICE_ID_STR_MAX]; | ||
|
||
/** | ||
* Generate a 32-bit PRNG. | ||
*/ | ||
static uint32_t xorshift(uint32_t x) { | ||
/* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */ | ||
x ^= x << 13; | ||
x ^= x << 17; | ||
x ^= x << 5; | ||
return x; | ||
} | ||
|
||
void Device::init(void) { | ||
uint32_t chip_id = (uint32_t)ESP.getEfuseMac(); | ||
for (size_t i = 0; i < UUID128_AS_32_LEN; i++) { | ||
chip_id = xorshift(chip_id); | ||
g_Uuid.uint32[i] = chip_id; | ||
} | ||
|
||
// truncate ID to 5 hex characters (arbitrary, just make it 'nice' to read) | ||
snprintf(g_StringID, DEVICE_ID_STR_MAX, "%s-%05x", FURBLE_STR, g_Uuid.uint32[0] & 0xFFFFF); | ||
} | ||
|
||
void Device::getUUID128(uuid128_t *uuid) { | ||
*uuid = g_Uuid; | ||
} | ||
|
||
const char *Device::getStringID(void) { | ||
return g_StringID; | ||
} | ||
|
||
} // namespace Furble |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef DEVICE_H | ||
#define DEVICE_H | ||
|
||
#include <cstdint> | ||
|
||
#define DEVICE_ID_STR_MAX (16) | ||
#define UUID128_LEN (16) | ||
#define UUID128_AS_32_LEN (UUID128_LEN / sizeof(uint32_t)) | ||
|
||
namespace Furble { | ||
|
||
class Device { | ||
public: | ||
/** | ||
* UUID type. | ||
*/ | ||
typedef struct _uuid128_t { | ||
union { | ||
uint32_t uint32[UUID128_AS_32_LEN]; | ||
uint8_t uint8[UUID128_LEN]; | ||
}; | ||
} uuid128_t; | ||
|
||
/** | ||
* Initialise the device. | ||
*/ | ||
static void init(void); | ||
|
||
/** | ||
* Return a device consistent 128-bit UUID. | ||
*/ | ||
static void getUUID128(uuid128_t *uuid); | ||
|
||
/** | ||
* Return pseudo-unique identifier string of this device. | ||
*/ | ||
static const char *getStringID(void); | ||
|
||
private: | ||
static uuid128_t g_Uuid; | ||
static char g_StringID[DEVICE_ID_STR_MAX]; | ||
}; | ||
} // namespace Furble | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.