Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTL872x: Implement APIs to set/get BLE address #2508

Merged
merged 5 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hal/inc/deviceid_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved);

#include "deviceid_hal_compat.h"

#include "deviceid_hal_impl.h"

#ifdef __cplusplus
}
#endif
Expand Down
20 changes: 20 additions & 0 deletions hal/src/gcc/deviceid_hal_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2022 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

// placeholder
20 changes: 20 additions & 0 deletions hal/src/nRF52840/deviceid_hal_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2022 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

// placeholder
20 changes: 20 additions & 0 deletions hal/src/newhal/deviceid_hal_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2022 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

// placeholder
85 changes: 74 additions & 11 deletions hal/src/rtl872x/ble_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,17 @@
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#undef LOG_COMPILE_TIME_LEVEL
#define LOG_COMPILE_TIME_LEVEL LOG_LEVEL_TRACE
#include "logging.h"
LOG_SOURCE_CATEGORY("hal.ble");

#include "ble_hal.h"

#if HAL_PLATFORM_BLE

#ifdef __cplusplus
extern "C" {
#endif
#include "rtl8721d.h"
#ifdef __cplusplus
} // extern "C"
#endif

extern "C" {
#include "rtl8721d_efuse.h"
#include "rtk_coex.h"
}

Expand All @@ -43,6 +37,7 @@ extern "C" {
#include <gap_adv.h>
#include <gap_scan.h>
#include <gap_bond_le.h>
#include <gap_le.h>
#include <gap_conn_le.h>
#include <profile_server.h>
#include <gatt_builtin_services.h>
Expand All @@ -57,7 +52,6 @@ extern "C" {
#include <wifi/wifi_conf.h>
#include "rtk_coex.h"
#include "ftl_int.h"
#include "rtl8721d.h"
#include "bt_intf.h"

//FIXME
Expand Down Expand Up @@ -164,6 +158,19 @@ bool addressEqual(const hal_ble_addr_t& srcAddr, const hal_ble_addr_t& destAddr)
return (srcAddr.addr_type == destAddr.addr_type && !memcmp(srcAddr.addr, destAddr.addr, BLE_SIG_ADDR_LEN));
}

hal_ble_addr_t chipDefaultPublicAddress() {
hal_ble_addr_t localAddr = {};
uint8_t mac[BLE_SIG_ADDR_LEN] = {};
if (hal_get_mac_address(HAL_DEVICE_MAC_BLE, mac, BLE_SIG_ADDR_LEN, nullptr) == BLE_SIG_ADDR_LEN) {
// As per BLE spec, we store BLE data in little-endian
for (uint8_t i = 0, j = BLE_SIG_ADDR_LEN - 1; i < BLE_SIG_ADDR_LEN; i++, j--) {
localAddr.addr[i] = mac[j];
}
localAddr.addr_type = BLE_SIG_ADDR_TYPE_PUBLIC;
}
return localAddr;
}

} //anonymous namespace


Expand Down Expand Up @@ -329,6 +336,8 @@ class BleGap {
int setAppearance(uint16_t appearance) const;
int setDeviceName(const char* deviceName, size_t len);
int getDeviceName(char* deviceName, size_t len) const;
int setDeviceAddress(const hal_ble_addr_t* address);
int getDeviceAddress(hal_ble_addr_t* address) const;

int setAdvertisingParameters(const hal_ble_adv_params_t* params);
int getAdvertisingParameters(hal_ble_adv_params_t* params) const;
Expand Down Expand Up @@ -391,6 +400,7 @@ class BleGap {
: initialized_(false),
btStackStarted_(false),
state_{},
addr_{},
advParams_{},
advTimeoutTimer_(nullptr),
isScanning_(false),
Expand Down Expand Up @@ -483,6 +493,7 @@ class BleGap {
bool initialized_;
bool btStackStarted_;
volatile RtlGapDevState state_; /**< This should be atomically r/w as the struct is <= uint32_t */
hal_ble_addr_t addr_;
hal_ble_adv_params_t advParams_;
os_timer_t advTimeoutTimer_; /**< Timer for advertising timeout. */
volatile bool isScanning_; /**< If it is scanning or not. */
Expand Down Expand Up @@ -793,6 +804,14 @@ int BleGap::init() {
CHECK(setDeviceName(devName_, devNameLen_));
CHECK(setAdvertisingParameters(&advParams_));
CHECK(setScanParams(&scanParams_));

constexpr uint8_t zeros[BLE_SIG_ADDR_LEN] = {0,0,0,0,0,0};
if (!memcmp(addr_.addr, zeros, BLE_SIG_ADDR_LEN)) {
CHECK(setDeviceAddress(nullptr));
} else {
CHECK(setDeviceAddress(&addr_));
}

/* register gap message callback */
le_register_app_cb(gapEventCallback);

Expand Down Expand Up @@ -910,6 +929,48 @@ int BleGap::getDeviceName(char* deviceName, size_t len) const {
return SYSTEM_ERROR_NONE;
}

int BleGap::setDeviceAddress(const hal_ble_addr_t* address) {
CHECK_FALSE(isAdvertising(), SYSTEM_ERROR_INVALID_STATE);
CHECK_FALSE(scanning(), SYSTEM_ERROR_INVALID_STATE);
// RTL872x doesn't support changing the the public address.
// But to be compatible with existing BLE platforms, it should accept nulllptr.
if (!address) {
addr_ = chipDefaultPublicAddress();
return SYSTEM_ERROR_NONE;
}
if (address->addr_type != BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
if ((address->addr[5] & 0xC0) != 0xC0) {
// For random static address, the two most significant bits of the address shall be equal to 1.
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
uint8_t addr[BLE_SIG_ADDR_LEN] = {};
memcpy(addr, address->addr, BLE_SIG_ADDR_LEN);
CHECK_RTL(le_cfg_local_identity_address(addr, GAP_IDENT_ADDR_RAND));
CHECK_RTL(le_set_gap_param(GAP_PARAM_RANDOM_ADDR, BLE_SIG_ADDR_LEN, addr));
addr_ = *address;

uint8_t advLocalAddrType = GAP_LOCAL_ADDR_LE_PUBLIC;
if (address->addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
advLocalAddrType = GAP_LOCAL_ADDR_LE_RANDOM;
}
CHECK_RTL(le_adv_set_param(GAP_PARAM_ADV_LOCAL_ADDR_TYPE, sizeof(advLocalAddrType), &advLocalAddrType));
uint8_t scanLocalAddrType = GAP_LOCAL_ADDR_LE_PUBLIC;
if (address->addr_type == BLE_SIG_ADDR_TYPE_RANDOM_STATIC) {
scanLocalAddrType = GAP_LOCAL_ADDR_LE_RANDOM;
}
CHECK_RTL(le_scan_set_param(GAP_PARAM_SCAN_LOCAL_ADDR_TYPE, sizeof(uint8_t), &scanLocalAddrType));

return SYSTEM_ERROR_NONE;
}

int BleGap::getDeviceAddress(hal_ble_addr_t* address) const {
CHECK_TRUE(address, SYSTEM_ERROR_INVALID_ARGUMENT);
*address = addr_;
return SYSTEM_ERROR_NONE;
}

int BleGap::setAdvertisingParameters(const hal_ble_adv_params_t* params) {
hal_ble_adv_params_t tempParams = {};
tempParams.version = BLE_API_VERSION;
Expand Down Expand Up @@ -2057,13 +2118,15 @@ int hal_ble_set_callback_on_periph_link_events(hal_ble_on_link_evt_cb_t callback
int hal_ble_gap_set_device_address(const hal_ble_addr_t* address, void* reserved) {
BleLock lk;
LOG_DEBUG(TRACE, "hal_ble_gap_set_device_address().");
return SYSTEM_ERROR_NOT_SUPPORTED;
CHECK_TRUE(BleGap::getInstance().initialized(), SYSTEM_ERROR_INVALID_STATE);
return BleGap::getInstance().setDeviceAddress(address);
}

int hal_ble_gap_get_device_address(hal_ble_addr_t* address, void* reserved) {
BleLock lk;
LOG_DEBUG(TRACE, "hal_ble_gap_get_device_address().");
return SYSTEM_ERROR_NOT_SUPPORTED;
CHECK_TRUE(BleGap::getInstance().initialized(), SYSTEM_ERROR_INVALID_STATE);
return BleGap::getInstance().getDeviceAddress(address);
}

int hal_ble_gap_set_device_name(const char* device_name, size_t len, void* reserved) {
Expand Down
42 changes: 24 additions & 18 deletions hal/src/rtl872x/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ extern "C" {
#include "rtl8721d_efuse.h"
}


namespace {

using namespace particle;
Expand All @@ -43,13 +42,13 @@ using namespace particle;
#define EFUSE_SUCCESS 1
#define EFUSE_FAILURE 0

#define BLE_MAC_OFFSET 0x190
#define WIFI_MAC_OFFSET 0x11A
#define MOBILE_SECRET_OFFSET 0x160
#define SERIAL_NUMBER_OFFSET 0x16F
#define HARDWARE_DATA_OFFSET 0x178
#define HARDWARE_MODEL_OFFSET 0x17B

#define WIFI_MAC_SIZE 6
#define SERIAL_NUMBER_FRONT_PART_SIZE 9
#define HARDWARE_DATA_SIZE 4
#define HARDWARE_MODEL_SIZE 4
Expand Down Expand Up @@ -80,8 +79,7 @@ int readLogicalEfuse(uint32_t offset, uint8_t* buf, size_t size) {

} // Anonymous

unsigned hal_get_device_id(uint8_t* dest, unsigned destLen)
{
unsigned hal_get_device_id(uint8_t* dest, unsigned destLen) {
// Device ID is composed of prefix and MAC address
uint8_t id[HAL_DEVICE_ID_SIZE] = {};
memcpy(id, DEVICE_ID_PREFIX, DEVICE_ID_PREFIX_SIZE);
Expand All @@ -92,18 +90,29 @@ unsigned hal_get_device_id(uint8_t* dest, unsigned destLen)
return HAL_DEVICE_ID_SIZE;
}

unsigned hal_get_platform_id()
{
unsigned hal_get_platform_id() {
return PLATFORM_ID;
}

int HAL_Get_Device_Identifier(const char** name, char* buf, size_t buflen, unsigned index, void* reserved)
{
return -1;
int HAL_Get_Device_Identifier(const char** name, char* buf, size_t buflen, unsigned index, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_mac_address(uint8_t type, uint8_t* dest, size_t destLen, void* reserved) {
CHECK_TRUE(dest && destLen >= HAL_DEVICE_MAC_ADDR_SIZE, SYSTEM_ERROR_INVALID_ARGUMENT);
CHECK_TRUE(isSupportedMacType(type), SYSTEM_ERROR_INVALID_ARGUMENT);
uint8_t mac[HAL_DEVICE_MAC_ADDR_SIZE] = {};
if (type == HAL_DEVICE_MAC_BLE) {
CHECK(readLogicalEfuse(BLE_MAC_OFFSET, mac, HAL_DEVICE_MAC_ADDR_SIZE));
} else {
CHECK(readLogicalEfuse(WIFI_MAC_OFFSET, mac, HAL_DEVICE_MAC_ADDR_SIZE));
// Derive the final MAC address
mac[5] += type;
}
return HAL_DEVICE_MAC_ADDR_SIZE;
}

int hal_get_device_serial_number(char* str, size_t size, void* reserved)
{
int hal_get_device_serial_number(char* str, size_t size, void* reserved) {
char fullSerialNumber[HAL_DEVICE_SERIAL_NUMBER_SIZE] = {};

// Serial Number (15 chars) is comprised of:
Expand All @@ -115,7 +124,7 @@ int hal_get_device_serial_number(char* str, size_t size, void* reserved)
CHECK(readLogicalEfuse(SERIAL_NUMBER_OFFSET, (uint8_t*)fullSerialNumber, SERIAL_NUMBER_FRONT_PART_SIZE));

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

Expand All @@ -133,8 +142,7 @@ int hal_get_device_serial_number(char* str, size_t size, void* reserved)
return HAL_DEVICE_SERIAL_NUMBER_SIZE;
}

int hal_get_device_hw_version(uint32_t* revision, void* reserved)
{
int hal_get_device_hw_version(uint32_t* revision, void* reserved) {
// HW Data format: | NCP_ID (LSB) | HW_VERSION | HW Feature Flags |
// | byte 0 | byte 1 | byte 2/3 |
uint8_t hw_data[4] = {};
Expand All @@ -146,8 +154,7 @@ int hal_get_device_hw_version(uint32_t* revision, void* reserved)
return SYSTEM_ERROR_NONE;
}

int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved)
{
int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved) {
// HW Model format: | Model Number LSB | Model Number MSB | Model Variant LSB | Model Variant MSB |
// | byte 0 | byte 1 | byte 2 | byte 3 |
uint8_t hw_model[4] = {};
Expand All @@ -159,8 +166,7 @@ int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved)
}

#ifndef HAL_DEVICE_ID_NO_DCT
int hal_get_device_secret(char* data, size_t size, void* reserved)
{
int hal_get_device_secret(char* data, size_t size, void* reserved) {
// Check if the device secret data is initialized in the DCT
char secret[HAL_DEVICE_SECRET_SIZE] = {};
static_assert(sizeof(secret) == DCT_DEVICE_SECRET_SIZE, "");
Expand Down
43 changes: 43 additions & 0 deletions hal/src/rtl872x/deviceid_hal_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2022 Particle Industries, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#define HAL_DEVICE_MAC_ADDR_SIZE 6

#define HAL_DEVICE_MAC_WIFI_STA 0
#define HAL_DEVICE_MAC_BLE 1
#define HAL_DEVICE_MAC_WIFI_AP 2
#define HAL_DEVICE_MAC_ETHERNET 3

#define isSupportedMacType(type) ((type) == HAL_DEVICE_MAC_WIFI_STA || \
(type) == HAL_DEVICE_MAC_BLE || \
(type) == HAL_DEVICE_MAC_WIFI_AP || \
(type) == HAL_DEVICE_MAC_ETHERNET)

#ifdef __cplusplus
extern "C" {
#endif

/**
* Get the device's BLE MAC address.
*/
int hal_get_mac_address(uint8_t type, uint8_t* dest, size_t destLen, void* reserved);

#ifdef __cplusplus
}
#endif
Loading