Add support for using the nRF52 preprogrammed random MAC address

Each nRF52 chip comes preprogrammed with a random static MAC address.
This is the default MAC address that is used if we don't specify one.

Fixes #25
This commit is contained in:
rbaron 2022-01-06 22:06:43 +01:00
parent 742ae103da
commit e16f61509c
2 changed files with 28 additions and 15 deletions

View file

@ -26,11 +26,18 @@
// Prints out BLE debug info, such as the final encoded advertisement packet.
#define PRST_BLE_DEBUG 0
#define PRST_BLE_PROTOCOL_VERSION 1
// We're using a random static MAC address, which has the following constraints:
// 1. Two most significant bits are set to 1;
// 2. The remaining bits should not _all_ be set to 0;
// 2. The remaining bits should not _all_ be set to 1;
// There are two options for configuring the MAC address of b-parasites:
// 1. Comment out the PRST_BLE_MAC_ADDR to use a random static MAC address that
// is preprogrammed in each nRF52 chip.
// 2. Manually specify the MAC address you want below. In this scenario, the
// following constraints must be met to ensure valid random static MAC
// addresses:
// a. Two most significant bits are set to 1;
// b. The remaining bits should not _all_ be set to 0;
// c. The remaining bits should not _all_ be set to 1;
#define PRST_BLE_MAC_ADDR "f0:ca:f0:ca:01:01"
#define PRST_BLE_ADV_NAME "prst"
// Total time spend advertising.
#define PRST_BLE_ADV_TIME_IN_MS 1000

View file

@ -118,16 +118,8 @@ static void init_advertisement_data() {
service_data[0] |= 1;
#endif
// Parses configured MAC address from PRST_BLE_MAC_ADDR.
int mac_bytes[6];
sscanf(PRST_BLE_MAC_ADDR, "%x:%x:%x:%x:%x:%x", &mac_bytes[0], &mac_bytes[1],
&mac_bytes[2], &mac_bytes[3], &mac_bytes[4], &mac_bytes[5]);
// Bytes 10-15 (inclusive) contain the whole MAC address.
for (int i = 0; i < 6; i++) {
gap_addr_.addr[5 - i] = (uint8_t)mac_bytes[i];
service_data[10 + i] = (uint8_t)mac_bytes[i];
}
memcpy(service_data + 10, gap_addr_.addr, 6);
}
void prst_ble_init() {
@ -146,9 +138,23 @@ void prst_ble_init() {
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
init_advertisement_data();
#ifdef PRST_BLE_MAC_ADDR
// Parses configured MAC address from PRST_BLE_MAC_ADDR.
int mac_bytes[6];
sscanf(PRST_BLE_MAC_ADDR, "%x:%x:%x:%x:%x:%x", &mac_bytes[0], &mac_bytes[1],
&mac_bytes[2], &mac_bytes[3], &mac_bytes[4], &mac_bytes[5]);
for (int i = 0; i < 6; i++) {
gap_addr_.addr[5 - i] = (uint8_t)mac_bytes[i];
}
APP_ERROR_CHECK(sd_ble_gap_addr_set(&gap_addr_));
#endif
APP_ERROR_CHECK(sd_ble_gap_addr_get(&gap_addr_));
NRF_LOG_INFO("[ble] MAC address: %02x:%02x:%02x:%02x:%02x:%02x\n",
gap_addr_.addr[5], gap_addr_.addr[4], gap_addr_.addr[3],
gap_addr_.addr[2], gap_addr_.addr[1], gap_addr_.addr[0]);
init_advertisement_data();
}
void prst_ble_update_adv_data(uint16_t batt_millivolts,