Skip to content

Commit

Permalink
Merge pull request #2779 from particle-iot/sc-128198/p2-photon2-names
Browse files Browse the repository at this point in the history
Display Photon 2 instead of P2, dont allow extended advertising data on rtl platforms
  • Loading branch information
scott-brust authored Jun 5, 2024
2 parents 8281cdf + 80d4dab commit 917ed35
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 31 deletions.
1 change: 1 addition & 0 deletions bootloader/src/rtl872x/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/littlef
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/shared/,filesystem.cpp)
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,usbd_device.cpp)
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,usbd_driver.cpp)
CPPSRC += $(call target_files,$(BOOTLOADER_MODULE_PATH)/../hal/src/rtl872x/,device_code.cpp)

LDFLAGS += -T$(BOOTLOADER_SRC_PATH)/linker.ld
LINKER_DEPS += $(BOOTLOADER_SRC_PATH)/linker.ld
2 changes: 2 additions & 0 deletions hal/inc/device_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ int get_device_name(char* buf, size_t size);

int get_device_setup_code(char* code, size_t size);

int get_device_usb_name(char* buf, size_t size);

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions hal/src/nRF52840/ble_hal_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
#define BLE_MAX_ADV_DATA_LEN_EXT BLE_GAP_SCAN_BUFFER_EXTENDED_MAX_SUPPORTED
#define BLE_MAX_ADV_DATA_LEN_EXT_CONNECTABLE BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_CONNECTABLE_MAX_SUPPORTED

#define BLE_MAX_SUPPORTED_ADV_DATA_LEN BLE_MAX_ADV_DATA_LEN_EXT

/* Connection Parameters limits */
#define BLE_CONN_PARAMS_SLAVE_LATENCY_ERR 5
#define BLE_CONN_PARAMS_TIMEOUT_ERR 100
Expand Down
4 changes: 4 additions & 0 deletions hal/src/nRF52840/device_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,7 @@ int get_device_name(char* buf, size_t size) {
}
return nameSize;
}

int get_device_usb_name(char* buf, size_t size) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}
2 changes: 2 additions & 0 deletions hal/src/rtl872x/ble_hal_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
/* Maximum length of advertising and scan response data */
#define BLE_MAX_ADV_DATA_LEN 31

#define BLE_MAX_SUPPORTED_ADV_DATA_LEN BLE_MAX_ADV_DATA_LEN

/* Maximum length of the buffer to store scan report data */
#define BLE_MAX_SCAN_REPORT_BUF_LEN 255 /* Must support extended length for CODED_PHY scanning */

Expand Down
28 changes: 26 additions & 2 deletions hal/src/rtl872x/device_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ bool fetch_or_generate_setup_ssid(device_code_t* code) {
return true;
}

static const char * get_product_series_name(void) {
#if PLATFORM_ID == PLATFORM_P2
uint32_t model, variant = 0;
hal_get_device_hw_model(&model, &variant, nullptr);
if (variant == PLATFORM_P2_PHOTON_2) {
return PRODUCT_SERIES_PHOTON2;
}
#endif
return PRODUCT_SERIES;
}

int get_device_name(char* buf, size_t size) {
char dctName[DEVICE_NAME_DCT_SIZE] = {};
int ret = dct_read_app_data_copy(DEVICE_NAME_DCT_OFFSET, dctName, DEVICE_NAME_DCT_SIZE);
Expand All @@ -101,8 +112,8 @@ int get_device_name(char* buf, size_t size) {
return ret;
}
// Get platform name
const char* const platform = PRODUCT_SERIES;
nameSize = sizeof(PRODUCT_SERIES) - 1; // Exclude term. null
const char* const platform = get_product_series_name();
nameSize = strlen(platform); // Exclude term. null
if (nameSize + SETUP_CODE_SIZE + 1 > DEVICE_NAME_MAX_SIZE) { // Reserve 1 character for '-'
nameSize = DEVICE_NAME_MAX_SIZE - SETUP_CODE_SIZE - 1;
}
Expand All @@ -122,3 +133,16 @@ int get_device_name(char* buf, size_t size) {
}
return nameSize;
}

int get_device_usb_name(char* buf, size_t size) {
#if PLATFORM_ID == PLATFORM_P2
uint32_t model, variant = 0;
hal_get_device_hw_model(&model, &variant, nullptr);
if (variant == PLATFORM_P2_PHOTON_2) {
snprintf(buf, size, "%s", HAL_PLATFORM_PHOTON2_USB_PRODUCT_STRING);
return 0;
}
#endif
snprintf(buf, size, "%s", HAL_PLATFORM_USB_PRODUCT_STRING);
return 0;
}
10 changes: 8 additions & 2 deletions hal/src/rtl872x/usbd_cdc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
#include <algorithm>
#include <mutex>
#include "service_debug.h"
#include "device_code.h"

using namespace particle::usbd;

namespace {

const char DEFAULT_NAME[] = HAL_PLATFORM_USB_PRODUCT_STRING " " "USB Serial";
const char DEFAULT_NAME_SUFFIX[] = "USB Serial";
const uint8_t DUMMY_IN_EP = 0x8a;

} // anonymous
Expand Down Expand Up @@ -378,7 +379,12 @@ int CdcClassDriver::getString(unsigned id, uint16_t langId, uint8_t* buf, size_t
if (name_) {
return dev_->getUnicodeString(name_, strlen(name_), buf, length);
}
return dev_->getUnicodeString(DEFAULT_NAME, sizeof(DEFAULT_NAME) - 1, buf, length);

char usbName[64] = {};
char strBuffer[256] = {};
get_device_usb_name(usbName, sizeof(usbName));
snprintf(strBuffer, sizeof(strBuffer), "%s %s", usbName, DEFAULT_NAME_SUFFIX);
return dev_->getUnicodeString(strBuffer, strlen(strBuffer), buf, length);
}
return SYSTEM_ERROR_NOT_FOUND;
}
Expand Down
9 changes: 7 additions & 2 deletions hal/src/rtl872x/usbd_control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include "usbd_control.h"
#include <cstring>
#include <algorithm>
#include <cstdio>
#include "usbd_wcid.h"
#include "appender.h"
#include "check.h"
#include "device_code.h"

using namespace particle::usbd;

Expand Down Expand Up @@ -217,8 +219,11 @@ int ControlInterfaceClassDriver::getConfigurationDescriptor(uint8_t* buf, size_t

int ControlInterfaceClassDriver::getString(unsigned id, uint16_t langId, uint8_t* buf, size_t length) {
if (id == stringBase_) {
const char str[] = HAL_PLATFORM_USB_PRODUCT_STRING " " "Control Interface";
return dev_->getUnicodeString(str, strlen(str), buf, length);
char usbName[64] = {};
char strBuffer[256] = {};
get_device_usb_name(usbName, sizeof(usbName));
snprintf(strBuffer, sizeof(strBuffer), "%s %s", usbName, "Control Interface");
return dev_->getUnicodeString(strBuffer, strlen(strBuffer), buf, length);
}
return SYSTEM_ERROR_NOT_FOUND;
}
Expand Down
9 changes: 7 additions & 2 deletions hal/src/rtl872x/usbd_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "usbd_wcid.h"
#include <algorithm>
#include "appender.h"
#include "device_code.h"

using namespace particle::usbd;

Expand Down Expand Up @@ -341,14 +342,18 @@ int Device::getString(unsigned id, uint16_t langId, uint8_t* buf, size_t len) {
return getUnicodeString(HAL_PLATFORM_USB_MANUFACTURER_STRING, sizeof(HAL_PLATFORM_USB_MANUFACTURER_STRING) - 1, buf, len);
}
case STRING_IDX_PRODUCT: {
return getUnicodeString(HAL_PLATFORM_USB_PRODUCT_STRING, sizeof(HAL_PLATFORM_USB_PRODUCT_STRING) - 1, buf, len);
char usbName[64] = {};
get_device_usb_name(usbName, sizeof(usbName));
return getUnicodeString(usbName, strlen(usbName), buf, len);
}
case STRING_IDX_SERIAL: {
char deviceid[HAL_DEVICE_ID_SIZE * 2] = {};
return getUnicodeString(device_id_as_string(deviceid), sizeof(deviceid), buf, len);
}
case STRING_IDX_CONFIG: {
return getUnicodeString(HAL_PLATFORM_USB_CONFIGURATION_STRING, sizeof(HAL_PLATFORM_USB_CONFIGURATION_STRING) - 1, buf, len);
char usbName[64] = {};
get_device_usb_name(usbName, sizeof(usbName));
return getUnicodeString(usbName, strlen(usbName), buf, len);
}
case STRING_IDX_MSFT: {
return getRawString((const char*)MSFT_STR_DESC, sizeof(MSFT_STR_DESC), buf, len);
Expand Down
3 changes: 3 additions & 0 deletions hal/src/tron/hal_platform_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@

#if defined(MODULE_FUNCTION) && MODULE_FUNCTION != 2 // MOD_FUNC_BOOTLOADER
#define HAL_PLATFORM_USB_PRODUCT_STRING "P2"
#define HAL_PLATFORM_PHOTON2_USB_PRODUCT_STRING "Photon 2"
#else
#define HAL_PLATFORM_USB_PRODUCT_STRING "P2 DFU Mode"
#define HAL_PLATFORM_PHOTON2_USB_PRODUCT_STRING "Photon 2 DFU Mode"
#endif // defined(MODULE_FUNCTION) && MODULE_FUNCTION != 2 // MOD_FUNC_BOOTLOADER

#define PRODUCT_SERIES "P2"
#define PRODUCT_SERIES_PHOTON2 "Photon 2"

#define PLATFORM_P2_MODEL_BARE_SOM_DEFAULT (0xffff)
#define PLATFORM_P2_PHOTON_2 (0x0001)
Expand Down
8 changes: 4 additions & 4 deletions system/src/ble_provisioning_mode_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ int BleProvisioningModeHandler::cacheUserConfigurations() {
LOG_DEBUG(TRACE, "Cache user's BLE configurations.");
CHECK_FALSE(exited_, SYSTEM_ERROR_INVALID_STATE);

Vector<uint8_t> tempAdvData(BLE_MAX_ADV_DATA_LEN);
Vector<uint8_t> tempSrData(BLE_MAX_ADV_DATA_LEN);
Vector<uint8_t> tempAdvData(BLE_MAX_SUPPORTED_ADV_DATA_LEN);
Vector<uint8_t> tempSrData(BLE_MAX_SUPPORTED_ADV_DATA_LEN);

// Advertising data set by user application
size_t len = CHECK(hal_ble_gap_get_advertising_data(tempAdvData.data(), BLE_MAX_ADV_DATA_LEN, nullptr));
size_t len = CHECK(hal_ble_gap_get_advertising_data(tempAdvData.data(), BLE_MAX_SUPPORTED_ADV_DATA_LEN, nullptr));
tempAdvData.resize(len);

// Scan response data set by user application
len = CHECK(hal_ble_gap_get_scan_response_data(tempSrData.data(), BLE_MAX_ADV_DATA_LEN, nullptr));
len = CHECK(hal_ble_gap_get_scan_response_data(tempSrData.data(), BLE_MAX_SUPPORTED_ADV_DATA_LEN, nullptr));
tempSrData.resize(len);

// Advertising and connection parameters set by user application
Expand Down
6 changes: 4 additions & 2 deletions system/src/system_ble_prov.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ using namespace particle::system;
int system_ble_prov_mode(bool enabled, void* reserved) {
SYSTEM_THREAD_CONTEXT_SYNC(system_ble_prov_mode(enabled, reserved));
if (!HAL_Feature_Get(FEATURE_DISABLE_LISTENING_MODE)) {
LOG(ERROR, "Listening mode is not disabled. Cannot use prov mode");
return SYSTEM_ERROR_NOT_ALLOWED;
if (enabled) {
LOG(ERROR, "Provisioning mode not enabled, listening mode is not disabled");
}
return SYSTEM_ERROR_NOT_ALLOWED;
}
if (enabled) {
CHECK_FALSE(BleProvisioningModeHandler::instance()->getProvModeStatus(), SYSTEM_ERROR_NONE);
Expand Down
4 changes: 2 additions & 2 deletions user/applications/tinker/src/burnin_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ bool BurninTest::testBleScan() {
Log.info("Found %d beacons", count);

for (int ii = 0; ii < count; ii++) {
uint8_t buf[BLE_MAX_ADV_DATA_LEN];
size_t len = scanResults[ii].advertisingData().get(BleAdvertisingDataType::MANUFACTURER_SPECIFIC_DATA, buf, BLE_MAX_ADV_DATA_LEN);
uint8_t buf[BLE_MAX_SUPPORTED_ADV_DATA_LEN];
size_t len = scanResults[ii].advertisingData().get(BleAdvertisingDataType::MANUFACTURER_SPECIFIC_DATA, buf, sizeof(buf));
Log.info("Beacon %d: rssi %d Advertising Data Len %u", ii, scanResults[ii].rssi(), len);
if (len > 0) {
Log.print("Advertising Data: ");
Expand Down
7 changes: 1 addition & 6 deletions user/applications/tinker/src/fqc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,8 @@ bool FqcTest::ioTest(JSONValue req) {
uint16_t pinB = 0;

// Pick which set of pins to use based on hardware variant
uint32_t model, variant, efuseReadAttempts = 0;
uint32_t model, variant = 0;
int result = hal_get_device_hw_model(&model, &variant, nullptr);
while (result != SYSTEM_ERROR_NONE && efuseReadAttempts < 5) {
Log.warn("Failed to read logical efuse: %d attempt: %lu", result, efuseReadAttempts);
result = hal_get_device_hw_model(&model, &variant, nullptr);
efuseReadAttempts++;
}

if (result != SYSTEM_ERROR_NONE) {
Log.error("Could not read logical efuse");
Expand Down
20 changes: 11 additions & 9 deletions wiring/src/spark_wiring_ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ size_t BleAdvertisingData::set(const uint8_t* buf, size_t len) {
return selfData_.size();
}
selfData_.clear();
len = std::min(len, (size_t)BLE_MAX_ADV_DATA_LEN_EXT);
len = std::min(len, (size_t)BLE_MAX_SUPPORTED_ADV_DATA_LEN);
CHECK_TRUE(selfData_.append(buf, len), 0);
return selfData_.size();
}
Expand All @@ -587,7 +587,7 @@ size_t BleAdvertisingData::set(const iBeacon& beacon) {
return selfData_.size();
}

CHECK_TRUE(selfData_.reserve(BLE_MAX_ADV_DATA_LEN), 0);
CHECK_TRUE(selfData_.reserve(BLE_MAX_SUPPORTED_ADV_DATA_LEN), 0);
selfData_.append(0x02);
selfData_.append(BLE_SIG_AD_TYPE_FLAGS);
selfData_.append(BLE_SIG_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Expand Down Expand Up @@ -625,7 +625,7 @@ size_t BleAdvertisingData::append(BleAdvertisingDataType type, const uint8_t* bu
size_t adsLen = locate(selfData_.data(), selfData_.size(), type, &offset);
if (!force && adsLen > 0) {
// Update the existing AD structure.
if ((selfData_.size() - adsLen + len + 2) <= BLE_MAX_ADV_DATA_LEN_EXT) {
if ((selfData_.size() - adsLen + len + 2) <= BLE_MAX_SUPPORTED_ADV_DATA_LEN) {
// Firstly, remove the existing AD structure.
selfData_.removeAt(offset, adsLen);
// Secondly, Update the AD structure.
Expand All @@ -638,12 +638,14 @@ size_t BleAdvertisingData::append(BleAdvertisingDataType type, const uint8_t* bu
selfData_.insert(offset + 2, buf, len);
}
}
else if ((selfData_.size() + len + 2) <= BLE_MAX_ADV_DATA_LEN_EXT) {
else if ((selfData_.size() + len + 2) <= BLE_MAX_SUPPORTED_ADV_DATA_LEN) {
// Append the AD structure at the and of advertising data.
CHECK_TRUE(selfData_.reserve(selfData_.size() + len + 2), selfData_.size());
selfData_.append(len + 1);
selfData_.append(static_cast<uint8_t>(type));
selfData_.append(buf, len);
} else {
return SYSTEM_ERROR_TOO_LARGE;
}
return selfData_.size();
}
Expand All @@ -665,7 +667,7 @@ size_t BleAdvertisingData::appendAppearance(ble_sig_appearance_t appearance) {
}

size_t BleAdvertisingData::resize(size_t size) {
selfData_.resize(std::min(size, (size_t)BLE_MAX_ADV_DATA_LEN_EXT));
selfData_.resize(std::min(size, (size_t)BLE_MAX_SUPPORTED_ADV_DATA_LEN));
return selfData_.size();
}

Expand Down Expand Up @@ -726,7 +728,7 @@ size_t BleAdvertisingData::deviceName(char* buf, size_t len) const {

String BleAdvertisingData::deviceName() const {
String name;
char buf[BLE_MAX_ADV_DATA_LEN];
char buf[BLE_MAX_SUPPORTED_ADV_DATA_LEN];
size_t len = deviceName(buf, sizeof(buf));
if (len > 0) {
for (size_t i = 0; i < len; i++) {
Expand Down Expand Up @@ -2264,7 +2266,7 @@ ssize_t BleLocalDevice::getScanResponseData(BleAdvertisingData* scanResponse) co
return SYSTEM_ERROR_INVALID_ARGUMENT;
}
scanResponse->clear();
CHECK_TRUE(scanResponse->resize(BLE_MAX_ADV_DATA_LEN), SYSTEM_ERROR_NO_MEMORY);
CHECK_TRUE(scanResponse->resize(BLE_MAX_SUPPORTED_ADV_DATA_LEN), SYSTEM_ERROR_NO_MEMORY);
size_t len = CHECK(hal_ble_gap_get_scan_response_data(scanResponse->data(), scanResponse->length(), nullptr));
scanResponse->resize(len);
return len;
Expand Down Expand Up @@ -2518,8 +2520,8 @@ class BleScanDelegator {
size_t filterCustomDatalen;
const uint8_t* filterCustomData = filter_.customData(&filterCustomDatalen);
if (filterCustomData != nullptr && filterCustomDatalen > 0) {
size_t srLen = result.scanResponse().get(BleAdvertisingDataType::MANUFACTURER_SPECIFIC_DATA, nullptr, BLE_MAX_ADV_DATA_LEN);
size_t advLen = result.advertisingData().get(BleAdvertisingDataType::MANUFACTURER_SPECIFIC_DATA, nullptr, BLE_MAX_ADV_DATA_LEN_EXT);
size_t srLen = result.scanResponse().get(BleAdvertisingDataType::MANUFACTURER_SPECIFIC_DATA, nullptr, BLE_MAX_SUPPORTED_ADV_DATA_LEN);
size_t advLen = result.advertisingData().get(BleAdvertisingDataType::MANUFACTURER_SPECIFIC_DATA, nullptr, BLE_MAX_SUPPORTED_ADV_DATA_LEN);
if (srLen != filterCustomDatalen && advLen != filterCustomDatalen) {
LOG_DEBUG(TRACE, "Custom data mismatched.");
return false;
Expand Down

0 comments on commit 917ed35

Please sign in to comment.