Skip to content

Commit 31d6a28

Browse files
committed
Added driver class using the CPU UID HAL API
1 parent 2d658a2 commit 31d6a28

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

drivers/CpuUid.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <cstdio>
18+
#include "hal/cpu_uid_api.h"
19+
#include "drivers/CpuUid.h"
20+
21+
#if DEVICE_CPUUID
22+
23+
namespace mbed {
24+
25+
CpuUid::CpuUid() : _data(NULL)
26+
{
27+
_size = cpu_uid_get_length();
28+
if (0 < _size)
29+
{
30+
_data = new uint8_t[_size];
31+
cpu_uid_get_uid(_data);
32+
}
33+
}
34+
35+
CpuUid::~CpuUid()
36+
{
37+
if (_data)
38+
{
39+
delete _data;
40+
}
41+
}
42+
43+
CpuUid::operator std::string()
44+
{
45+
std::string str;
46+
char buf[3];
47+
48+
for (int i = 0; i < _size; ++i)
49+
{
50+
snprintf(buf, 3, "%.2X", _data[i]);
51+
str += buf;
52+
}
53+
54+
return str;
55+
}
56+
57+
CpuUid::operator CpuUidArray()
58+
{
59+
CpuUidArray array;
60+
61+
for (int i = 0; i < _size; ++i)
62+
{
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+
{
73+
return _data[x];
74+
}
75+
76+
return 0x00;
77+
}
78+
79+
} // namespace mbed
80+
81+
#endif

drivers/CpuUid.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_CPUUID_H
17+
#define MBED_CPUUID_H
18+
19+
#include <string>
20+
#include <vector>
21+
#include "platform/platform.h"
22+
23+
#if defined(DEVICE_CPUUID) || defined(DOXYGEN_ONLY)
24+
25+
26+
namespace mbed {
27+
/** \addtogroup drivers */
28+
29+
/** CPU UID reader class
30+
*
31+
* @note Synchronization level: Interrupt safe
32+
* @ingroup drivers
33+
*/
34+
class CpuUid {
35+
36+
public:
37+
/** CPU UID array typedef
38+
*/
39+
typedef std::vector<uint8_t> CpuUidArray;
40+
41+
/** CpuUid constructor
42+
*/
43+
CpuUid();
44+
45+
/** CpuUid destructor
46+
*/
47+
virtual ~CpuUid();
48+
49+
/** Get size of CPU UID in bytes
50+
*
51+
* @return Size of device's CPU UID in bytes
52+
*/
53+
int size() { return _size; }
54+
55+
/** Get CPU UID data pointer
56+
*
57+
* @return Pointer to uid data buffer
58+
*/
59+
const uint8_t* data() { return _data; }
60+
61+
/** Overload operator for std::string
62+
*
63+
* @return string object containing the CPU UID in uppercase hex letters in ascii format
64+
*/
65+
operator std::string();
66+
67+
/** Overload operator for CpuUidArray
68+
*
69+
* @return CpuUidArray object containing the CPU UID
70+
*/
71+
operator CpuUidArray();
72+
73+
/** Overload operator for byte pointer
74+
*
75+
* @return Pointer to uid data buffer
76+
*/
77+
operator const uint8_t*() { return _data; }
78+
79+
/** Overload operator for array subscript
80+
*
81+
* @param x CPU UID Byte index
82+
*
83+
* @return Byte located at index x
84+
*/
85+
uint8_t operator[](int x);
86+
87+
private:
88+
uint8_t * _data;
89+
int _size;
90+
};
91+
92+
} // namespace mbed
93+
94+
#endif
95+
96+
#endif

mbed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#include "drivers/RawSerial.h"
7171
#include "drivers/UARTSerial.h"
7272
#include "drivers/FlashIAP.h"
73+
#include "drivers/CpuUid.h"
7374

7475
// mbed Internal components
7576
#include "drivers/Timer.h"

0 commit comments

Comments
 (0)