Skip to content

Commit f7ed4b9

Browse files
committed
CpuUid: complete refactoring to meet review requirements
Hopefully all review items were addressed.
1 parent 649ce41 commit f7ed4b9

File tree

7 files changed

+184
-105
lines changed

7 files changed

+184
-105
lines changed

drivers/CpuUid.cpp

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,50 @@
2222

2323
namespace mbed {
2424

25+
uint8_t CpuUid::_uidbuf[MBED_CPU_UID_SIZE] = {0};
26+
uint8_t* CpuUid::_uidptr = NULL;
27+
char CpuUid::_strbuf[CPU_UID_STRING_BUFFER_SIZE] = {'\0'};
28+
char* CpuUid::_strptr = NULL;
29+
#ifndef MBED_CPU_UID_STR_SIZE_MAX
2530
const char CpuUid::_hexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
31+
#endif
2632

27-
CpuUid::CpuUid() : _data(NULL)
33+
CpuUid::CpuUid()
2834
{
29-
_size = cpu_uid_get_length();
30-
#ifdef MBED_ASSERT
31-
MBED_ASSERT(_size > 0);
32-
#endif
33-
if (0 < _size) {
34-
_data = new uint8_t[_size];
35-
cpu_uid_get_uid(_data);
36-
}
35+
populate_uid_buf();
3736
}
3837

3938
CpuUid::~CpuUid()
4039
{
41-
if (_data) {
42-
delete _data;
43-
}
4440
}
4541

46-
CpuUid::operator std::string()
42+
void CpuUid::populate_uid_buf()
4743
{
48-
std::string str;
49-
50-
for (int i = 0; i < _size; ++i) {
51-
str += _hexChars[_data[i] >> 4];
52-
str += _hexChars[_data[i] & 0x0F];
44+
if (_uidptr == NULL) {
45+
cpu_uid_get_uid(_uidbuf);
46+
_uidptr = _uidbuf;
5347
}
54-
55-
return str;
5648
}
5749

58-
CpuUid::operator cpu_uid_array_t()
50+
void CpuUid::populate_str_buf()
5951
{
60-
cpu_uid_array_t array;
61-
62-
for (int i = 0; i < _size; ++i) {
63-
array.push_back(_data[i]);
64-
}
65-
66-
return array;
67-
}
68-
69-
uint8_t CpuUid::operator[](int x)
70-
{
71-
if (x >= 0 && x < _size) {
72-
return _data[x];
52+
if (_strptr == NULL) {
53+
#ifdef MBED_CPU_UID_STR_SIZE_MAX
54+
cpu_uid_get_str(_strbuf);
55+
_strptr = _strbuf;
56+
#else
57+
int pos = 0;
58+
populate_uid_buf();
59+
for (int i = 0; i < MBED_CPU_UID_SIZE; ++i) {
60+
_strbuf[pos++] = _hexChars[_uidptr[i] >> 4];
61+
_strbuf[pos++] = _hexChars[_uidptr[i] & 0x0F];
62+
}
63+
_strbuf[pos] = '\0';
64+
_strptr = _strbuf;
65+
#endif
7366
}
74-
75-
return 0x00;
7667
}
77-
68+
7869
} // namespace mbed
7970

8071
#endif

drivers/CpuUid.h

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
#if defined(DEVICE_CPUUID) || defined(DOXYGEN_ONLY)
2222

23+
#ifdef MBED_CPU_UID_STR_SIZE_MAX
24+
#define CPU_UID_STRING_BUFFER_SIZE MBED_CPU_UID_STR_SIZE_MAX
25+
#else
26+
#define CPU_UID_STRING_BUFFER_SIZE (MBED_CPU_UID_SIZE * 2 + 1)
27+
#endif
28+
2329
#include <string>
2430
#include <vector>
2531

@@ -33,10 +39,6 @@ namespace mbed {
3339
*/
3440
class CpuUid {
3541
public:
36-
/** CPU UID array typedef
37-
*/
38-
typedef std::vector<uint8_t> cpu_uid_array_t;
39-
4042
/** CpuUid constructor
4143
*/
4244
CpuUid();
@@ -51,7 +53,7 @@ class CpuUid {
5153
*/
5254
int size()
5355
{
54-
return _size;
56+
return MBED_CPU_UID_SIZE;
5557
}
5658

5759
/** Get CPU UID data pointer
@@ -60,28 +62,41 @@ class CpuUid {
6062
*/
6163
const uint8_t *data()
6264
{
63-
return _data;
65+
populate_uid_buf();
66+
return _uidptr;
6467
}
6568

66-
/** Overload operator for std::string
69+
/** Get CPU UID vendor string
6770
*
68-
* @return string object containing the CPU UID in uppercase hex letters in ascii format
69-
*/
70-
operator std::string();
71-
72-
/** Overload operator for cpu_uid_array_t
71+
* @return Pointer to zero terminated uid vendor string
72+
*
73+
* @note
74+
* If vendor did not define MBED_CPU_UID_STR_SIZE_MAX, CpuUid driver will
75+
* assume the HAL interface function cpu_uid_get_str() is not implemented
76+
* on the target, and instead the driver will construct just an ASCII
77+
* string out of the uid byte buffer contents.
7378
*
74-
* @return cpu_uid_array_t object containing the CPU UID
7579
*/
76-
operator cpu_uid_array_t();
80+
const char *c_str()
81+
{
82+
populate_str_buf();
83+
return _strptr;
84+
}
7785

7886
/** Overload operator for byte pointer
7987
*
8088
* @return Pointer to uid data buffer
8189
*/
8290
operator const uint8_t*()
8391
{
84-
return _data;
92+
populate_uid_buf();
93+
return _uidptr;
94+
}
95+
96+
operator const char*()
97+
{
98+
populate_str_buf();
99+
return _strptr;
85100
}
86101

87102
/** Overload operator for array subscript
@@ -90,13 +105,27 @@ class CpuUid {
90105
*
91106
* @return Byte located at index x
92107
*/
93-
uint8_t operator[](int x);
108+
uint8_t operator[](int x)
109+
{
110+
if (x >= 0 && x < MBED_CPU_UID_SIZE) {
111+
populate_uid_buf();
112+
return _uidptr[x];
113+
}
114+
115+
return 0x00;
116+
}
94117

95118
private:
96-
uint8_t *_data;
97-
int _size;
119+
void populate_uid_buf();
120+
void populate_str_buf();
98121

122+
static uint8_t _uidbuf[MBED_CPU_UID_SIZE];
123+
static uint8_t* _uidptr;
124+
static char _strbuf[CPU_UID_STRING_BUFFER_SIZE];
125+
static char* _strptr;
126+
#ifndef MBED_CPU_UID_STR_SIZE_MAX
99127
static const char _hexChars[16];
128+
#endif
100129
};
101130

102131
} // namespace mbed

hal/cpu_uid_api.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
#if DEVICE_CPUUID
2525

26+
#ifndef MBED_CPU_UID_SIZE
27+
#error "CPU UID Vendor implementation must define macro MBED_CPU_UID_SIZE with the uid size in bytes!"
28+
#endif
29+
30+
#ifndef MBED_CPU_UID_STR_SIZE_MAX
31+
#warning "CPU UID max vendor string length not defined! cpu_uid_get_str() HAL interface is disabled!"
32+
#endif
33+
2634
#ifdef __cplusplus
2735
extern "C" {
2836
#endif
@@ -32,25 +40,26 @@ extern "C" {
3240
* @{
3341
*/
3442

35-
36-
/** Get length of CPU UID in bytes
37-
*
38-
* @return Number of uid bytes
39-
*
40-
* @note
41-
* Shall be used by the driver to get the needed size of the byte buffer.
42-
* Target implementation must return a value higher than 0 or the driver will assert.
43-
*
44-
*/
45-
int cpu_uid_get_length(void);
46-
4743
/** Get CPU UID data bytes
4844
*
49-
* @param uid Byte buffer for uid. Must at least be of size obtained by call to cpu_uid_get_length()
45+
* @param uid Byte buffer for uid. Must be of size MBED_CPU_UID_SIZE
5046
*
5147
*/
5248
void cpu_uid_get_uid(uint8_t *uid);
5349

50+
#ifdef MBED_CPU_UID_STR_SIZE_MAX
51+
/** Get UID vendor string
52+
*
53+
* @param str Character buffer for vendor specific UID string. Must be of size MBED_CPU_UID_STR_SIZE_MAX
54+
*
55+
* @note
56+
* Implementing this function on target side can be used to provide a vendor specific
57+
* string describing the contents of the UID.
58+
* The string length including terminating zero character must not exceed MBED_CPU_UID_STR_SIZE_MAX bytes!
59+
*
60+
*/
61+
void cpu_uid_get_str(char *str);
62+
#endif
5463

5564
/**@}*/
5665

targets/TARGET_NORDIC/TARGET_NRF5/cpu_uid_api.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,15 @@
4343
#include "nrf.h"
4444
#include "cpu_uid_api.h"
4545

46-
#define UID_LENGTH 8
4746
#define UID_WORDS 2
4847

49-
int cpu_uid_get_length(void)
50-
{
51-
return UID_LENGTH;
52-
}
53-
5448
void cpu_uid_get_uid(uint8_t *uid)
5549
{
5650
int pos = 0;
5751

58-
for (int i = (UID_WORDS-1); i >= 0; --i)
59-
{
60-
for (int j = 3; j >= 0; --j)
61-
{
62-
uid[pos] = (uint8_t)((NRF_FICR->DEVICEID[i] >> (j*8)) & 0xFF);
63-
++pos;
52+
for (int i = (UID_WORDS-1); i >= 0; --i) {
53+
for (int j = 3; j >= 0; --j) {
54+
uid[pos++] = (uint8_t)((NRF_FICR->DEVICEID[i] >> (j*8)) & 0xFF);
6455
}
6556
}
6657
}

0 commit comments

Comments
 (0)