Skip to content

Commit 0a33273

Browse files
committed
[rtl872x] Generalize API to fetch MAC address
1 parent 5b81888 commit 0a33273

File tree

6 files changed

+87
-32
lines changed

6 files changed

+87
-32
lines changed

hal/inc/deviceid_hal.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,10 @@ int hal_get_device_hw_version(uint32_t* revision, void* reserved);
9696
*/
9797
int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved);
9898

99-
/**
100-
* Get the device's BLE MAC address.
101-
*/
102-
int hal_get_ble_mac_address(uint8_t* dest, size_t destLen, void* reserved);
103-
10499
#include "deviceid_hal_compat.h"
105100

101+
#include "deviceid_hal_impl.h"
102+
106103
#ifdef __cplusplus
107104
}
108105
#endif

hal/src/nRF52840/deviceid_hal_impl.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2022 Particle Industries, Inc. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation, either
7+
* version 3 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
20+
// placeholder

hal/src/rtl872x/ble_hal.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,14 @@ bool addressEqual(const hal_ble_addr_t& srcAddr, const hal_ble_addr_t& destAddr)
160160

161161
hal_ble_addr_t chipDefaultPublicAddress() {
162162
hal_ble_addr_t localAddr = {};
163-
hal_get_ble_mac_address(localAddr.addr, BLE_SIG_ADDR_LEN, nullptr);
164-
localAddr.addr_type = BLE_SIG_ADDR_TYPE_PUBLIC;
163+
uint8_t mac[BLE_SIG_ADDR_LEN] = {};
164+
if (hal_get_mac_address(HAL_DEVICE_MAC_BLE, mac, BLE_SIG_ADDR_LEN, nullptr) == BLE_SIG_ADDR_LEN) {
165+
// As per BLE spec, we store BLE data in little-endian
166+
for (uint8_t i = 0, j = BLE_SIG_ADDR_LEN - 1; i < BLE_SIG_ADDR_LEN; i++, j--) {
167+
localAddr.addr[i] = mac[j];
168+
}
169+
localAddr.addr_type = BLE_SIG_ADDR_TYPE_PUBLIC;
170+
}
165171
return localAddr;
166172
}
167173

hal/src/rtl872x/deviceid_hal.cpp

+12-21
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ extern "C" {
3434
#include "rtl8721d_efuse.h"
3535
}
3636

37-
3837
namespace {
3938

4039
using namespace particle;
@@ -50,20 +49,12 @@ using namespace particle;
5049
#define HARDWARE_DATA_OFFSET 0x178
5150
#define HARDWARE_MODEL_OFFSET 0x17B
5251

53-
#define WIFI_MAC_SIZE 6
54-
#define BLE_MAC_SIZE 6
5552
#define SERIAL_NUMBER_FRONT_PART_SIZE 9
5653
#define HARDWARE_DATA_SIZE 4
5754
#define HARDWARE_MODEL_SIZE 4
5855
#define WIFI_OUID_SIZE 3
5956
#define DEVICE_ID_PREFIX_SIZE 6
6057

61-
// Offsets from base WiFi MAC address for reserved interfaces
62-
#define WIFI_STA_MAC_DELTA 0
63-
#define WIFI_AP_MAC_DELTA 1
64-
#define BLE_MAC_DELTA 2
65-
#define ETH_MAC_DELTA 3
66-
6758
const uint8_t DEVICE_ID_PREFIX[] = {0x0a, 0x10, 0xac, 0xed, 0x20, 0x21};
6859

6960
int readLogicalEfuse(uint32_t offset, uint8_t* buf, size_t size) {
@@ -107,20 +98,20 @@ int HAL_Get_Device_Identifier(const char** name, char* buf, size_t buflen, unsig
10798
return SYSTEM_ERROR_NOT_SUPPORTED;
10899
}
109100

110-
int hal_get_ble_mac_address(uint8_t* dest, size_t destLen, void* reserved) {
111-
CHECK_TRUE(dest && destLen >= BLE_MAC_SIZE, SYSTEM_ERROR_INVALID_ARGUMENT);
112-
uint8_t mac[BLE_MAC_SIZE] = {};
113-
destLen = BLE_MAC_SIZE;
114-
CHECK(readLogicalEfuse(BLE_MAC_OFFSET, mac, BLE_MAC_SIZE));
115-
// As per BLE spec, we store BLE data in little-endian
116-
for (uint8_t i = 0, j = BLE_MAC_SIZE - 1; i < BLE_MAC_SIZE; i++, j--) {
117-
dest[i] = mac[j];
101+
int hal_get_mac_address(uint8_t type, uint8_t* dest, size_t destLen, void* reserved) {
102+
CHECK_TRUE(dest && destLen >= MAC_ADDR_SIZE, SYSTEM_ERROR_INVALID_ARGUMENT);
103+
CHECK_TRUE(isSupportedMacType(type), SYSTEM_ERROR_INVALID_ARGUMENT);
104+
uint8_t mac[MAC_ADDR_SIZE] = {};
105+
if (type == HAL_DEVICE_MAC_BLE) {
106+
CHECK(readLogicalEfuse(BLE_MAC_OFFSET, mac, MAC_ADDR_SIZE));
107+
} else {
108+
CHECK(readLogicalEfuse(WIFI_MAC_OFFSET, mac, MAC_ADDR_SIZE));
109+
// Derive the final MAC address
110+
mac[5] += type;
118111
}
119-
return BLE_MAC_SIZE;
112+
return MAC_ADDR_SIZE;
120113
}
121114

122-
123-
124115
int hal_get_device_serial_number(char* str, size_t size, void* reserved) {
125116
char fullSerialNumber[HAL_DEVICE_SERIAL_NUMBER_SIZE] = {};
126117

@@ -133,7 +124,7 @@ int hal_get_device_serial_number(char* str, size_t size, void* reserved) {
133124
CHECK(readLogicalEfuse(SERIAL_NUMBER_OFFSET, (uint8_t*)fullSerialNumber, SERIAL_NUMBER_FRONT_PART_SIZE));
134125

135126
// generate hex string from non-OUI MAC bytes
136-
uint8_t wifiMacRandomBytes[WIFI_MAC_SIZE - WIFI_OUID_SIZE] = {};
127+
uint8_t wifiMacRandomBytes[MAC_ADDR_SIZE - WIFI_OUID_SIZE] = {};
137128
CHECK(readLogicalEfuse(WIFI_MAC_OFFSET + WIFI_OUID_SIZE, wifiMacRandomBytes, sizeof(wifiMacRandomBytes)));
138129
bytes2hexbuf(wifiMacRandomBytes, sizeof(wifiMacRandomBytes), &fullSerialNumber[SERIAL_NUMBER_FRONT_PART_SIZE]);
139130

hal/src/rtl872x/deviceid_hal_impl.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2022 Particle Industries, Inc. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation, either
7+
* version 3 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
20+
#define MAC_ADDR_SIZE 6
21+
22+
#define HAL_DEVICE_MAC_WIFI_STA 0
23+
#define HAL_DEVICE_MAC_BLE 1
24+
#define HAL_DEVICE_MAC_WIFI_AP 2
25+
#define HAL_DEVICE_MAC_ETHERNET 3
26+
27+
#define isSupportedMacType(type) ((type) == HAL_DEVICE_MAC_WIFI_STA || \
28+
(type) == HAL_DEVICE_MAC_BLE || \
29+
(type) == HAL_DEVICE_MAC_WIFI_AP || \
30+
(type) == HAL_DEVICE_MAC_ETHERNET)
31+
32+
#ifdef __cplusplus
33+
extern "C" {
34+
#endif
35+
36+
/**
37+
* Get the device's BLE MAC address.
38+
*/
39+
int hal_get_mac_address(uint8_t type, uint8_t* dest, size_t destLen, void* reserved);
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif

hal/src/trackerm/network/network.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,8 @@ int if_init_platform(void*) {
154154
reserve_netif_index();
155155

156156
/* en2 - Ethernet FeatherWing (optional) */
157-
uint8_t deviceId[HAL_DEVICE_ID_SIZE] = {};
158-
hal_get_device_id(deviceId, sizeof(deviceId));
159-
uint8_t* mac = deviceId + HAL_DEVICE_ID_SIZE - 6;
160-
mac[5] += 3;
157+
uint8_t mac[MAC_ADDR_SIZE] = {};
158+
CHECK(hal_get_mac_address(HAL_DEVICE_MAC_ETHERNET, mac, MAC_ADDR_SIZE, nullptr));
161159

162160
if (HAL_Feature_Get(FEATURE_ETHERNET_DETECTION)) {
163161
en2 = new WizNetif(HAL_SPI_INTERFACE1, D5, D3, D4, mac);

0 commit comments

Comments
 (0)