Skip to content

Commit

Permalink
Merge pull request #2526 from particle-iot/feature/system-hardware-info
Browse files Browse the repository at this point in the history
[wiring] System.hardwareInfo()
  • Loading branch information
avtolstoy committed Aug 29, 2022
2 parents 7e099a4 + ac140ab commit 1bf0fc2
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 45 deletions.
18 changes: 18 additions & 0 deletions hal/inc/deviceid_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ int hal_get_device_hw_version(uint32_t* revision, void* reserved);
*/
int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved);

#define HAL_DEVICE_MAX_NCP_IDENTIFIERS (5)

typedef struct hal_device_hw_info {
uint16_t size;
uint16_t version;

uint32_t revision;
uint32_t model;
uint32_t variant;
uint32_t features;
uint16_t ncp[HAL_DEVICE_MAX_NCP_IDENTIFIERS];
} hal_device_hw_info;

/**
* Get the device's hardware information
*/
int hal_get_device_hw_info(hal_device_hw_info* info, void* reserved);

#include "deviceid_hal_compat.h"

#include "deviceid_hal_impl.h"
Expand Down
1 change: 1 addition & 0 deletions hal/inc/hal_dynalib_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ DYNALIB_FN(38, hal_core, hal_get_device_hw_model, int(uint32_t*, uint32_t*, void
DYNALIB_FN(39, hal_core, hal_get_device_serial_number, int(char*, size_t, void*))
DYNALIB_FN(40, hal_core, hal_get_device_secret, int(char*, size_t, void*))
DYNALIB_FN(41, hal_core, hal_set_device_secret, int(char*, size_t, void*))
DYNALIB_FN(42, hal_core, hal_get_device_hw_info, int(hal_device_hw_info*, void*))

DYNALIB_END(hal_core)

Expand Down
23 changes: 17 additions & 6 deletions hal/shared/platform_ncp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@
#include "ota_flash_hal.h"
#include "hal_platform.h"

enum PlatformNCPManufacturer {
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

typedef enum PlatformNCPManufacturer {
PLATFORM_NCP_MANUFACTURER_UNKNOWN,
PLATFORM_NCP_MANUFACTURER_ESPRESSIF = 1,
PLATFORM_NCP_MANUFACTURER_UBLOX = 2,
PLATFORM_NCP_MANUFACTURER_QUECTEL = 3,
PLATFORM_NCP_MANUFACTURER_BROADCOM = 4,
PLATFORM_NCP_MANUFACTURER_REALTEK = 5
};
} PlatformNCPManufacturer;

#define PLATFORM_NCP_IDENTIFIER(mf,t) (t|(mf<<5))
#define PLATFORM_NCP_MANUFACTURER(v) ((v) >> 5)

enum PlatformNCPIdentifier {
PLATFORM_NCP_UNKNOWN = -1,
typedef enum PlatformNCPIdentifier {
PLATFORM_NCP_UNKNOWN = 0xffff,
PLATFORM_NCP_NONE = 0,

// 0x2x
Expand All @@ -61,8 +65,12 @@ enum PlatformNCPIdentifier {
// 0x8x
PLATFORM_NCP_BROADCOM_BCM9WCDUSI09 = PLATFORM_NCP_IDENTIFIER(PLATFORM_NCP_MANUFACTURER_BROADCOM, 1),
PLATFORM_NCP_BROADCOM_BCM9WCDUSI14 = PLATFORM_NCP_IDENTIFIER(PLATFORM_NCP_MANUFACTURER_BROADCOM, 2),
PLATFORM_NCP_REALTEK_RTL872X = PLATFORM_NCP_IDENTIFIER(PLATFORM_NCP_MANUFACTURER_REALTEK, 1)
};
PLATFORM_NCP_REALTEK_RTL872X = PLATFORM_NCP_IDENTIFIER(PLATFORM_NCP_MANUFACTURER_REALTEK, 0)
} PlatformNCPIdentifier;

#if PLATFORM_ID != PLATFORM_GCC
static_assert(sizeof(PlatformNCPIdentifier) == sizeof(uint16_t), "sizeof(Platform_NCPIdentifier) has changed");
#endif

struct PlatformNCPInfo {
PlatformNCPIdentifier identifier;
Expand Down Expand Up @@ -97,3 +105,6 @@ int platform_ncp_fetch_module_info(hal_module_t* module);

#endif

#ifdef __cplusplus
}
#endif // __cplusplus
6 changes: 3 additions & 3 deletions hal/src/b5som/platform_ncp_quectel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ bool isValidNcpId(uint8_t id) {
PlatformNCPIdentifier platform_primary_ncp_identifier() {
// Check the DCT
uint8_t ncpId = 0;
int r = dct_read_app_data_copy(DCT_NCP_ID_OFFSET, &ncpId, 1);
int r = dct_read_app_data_copy(DCT_NCP_ID_OFFSET, &ncpId, sizeof(ncpId));
if (r < 0 || !isValidNcpId(ncpId)) {
// Check the OTP flash
r = hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, NCP_ID_OTP_ADDRESS, &ncpId, 1);
r = hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, NCP_ID_OTP_ADDRESS, &ncpId, sizeof(ncpId));
if (r < 0 || !isValidNcpId(ncpId)) {
ncpId = PlatformNCPIdentifier::PLATFORM_NCP_UNKNOWN;
return PlatformNCPIdentifier::PLATFORM_NCP_UNKNOWN;
}
}
return (PlatformNCPIdentifier)ncpId;
Expand Down
6 changes: 3 additions & 3 deletions hal/src/boron/platform_ncp_boron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ bool isValidNcpId(uint8_t id) {
PlatformNCPIdentifier platform_primary_ncp_identifier() {
// Check the DCT
uint8_t ncpId = 0;
int r = dct_read_app_data_copy(DCT_NCP_ID_OFFSET, &ncpId, 1);
int r = dct_read_app_data_copy(DCT_NCP_ID_OFFSET, &ncpId, sizeof(ncpId));
if (r < 0 || !isValidNcpId(ncpId)) {
// Check the OTP flash
r = hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, NCP_ID_OTP_ADDRESS, &ncpId, 1);
r = hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, NCP_ID_OTP_ADDRESS, &ncpId, sizeof(ncpId));
if (r < 0 || !isValidNcpId(ncpId)) {
ncpId = PlatformNCPIdentifier::PLATFORM_NCP_UNKNOWN;
return PlatformNCPIdentifier::PLATFORM_NCP_UNKNOWN;
}
}
return (PlatformNCPIdentifier)ncpId;
Expand Down
32 changes: 29 additions & 3 deletions hal/src/gcc/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
#include <string.h>
#include <cstdio>
#include <limits>


#include "system_error.h"

using std::string;

Expand All @@ -52,4 +51,31 @@ unsigned hal_get_platform_id()
int HAL_Get_Device_Identifier(const char** name, char* buf, size_t buflen, unsigned index, void* reserved)
{
return -1;
}
}

void hal_save_device_id(uint32_t offset) {
}

int hal_get_device_serial_number(char* str, size_t size, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_secret(char* data, size_t size, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_set_device_secret(char* data, size_t size, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_hw_version(uint32_t* revision, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_hw_info(hal_device_hw_info* info, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}
25 changes: 25 additions & 0 deletions hal/src/nRF52840/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
#ifndef HAL_DEVICE_ID_NO_DCT
#include "dct.h"
#endif /* HAL_DEVICE_ID_NO_DCT */
#include "check.h"

#include "nrf52840.h"

#include <algorithm>

#include "platform_ncp.h"

namespace {

using namespace particle;
Expand Down Expand Up @@ -111,6 +114,28 @@ int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved)
return SYSTEM_ERROR_NONE;
}

int hal_get_device_hw_info(hal_device_hw_info* info, void* reserved) {
CHECK_TRUE(info, SYSTEM_ERROR_INVALID_ARGUMENT);
// HW Data format: | NCP_ID | HW_VERSION | HW Feature Flags |
// | byte 0 | byte 1 | byte 2/3 |
uint8_t hw_data[4] = {};
CHECK(hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, HW_DATA_OTP_ADDRESS, hw_data, sizeof(hw_data)));
CHECK(hal_get_device_hw_model(&info->model, &info->variant, nullptr));
info->revision = hw_data[1];
if (info->revision == 0xff) {
info->revision = 0xffffffff;
}
info->features = ((uint32_t)hw_data[3] << 8) | (uint32_t)hw_data[2];
memset(info->ncp, 0xff, sizeof(info->ncp));
for (int i = 0; i < HAL_PLATFORM_NCP_COUNT; i++) {
PlatformNCPInfo ncpInfo = {};
if (!platform_ncp_get_info(i, &ncpInfo)) {
info->ncp[i] = (uint16_t)ncpInfo.identifier;
}
}
return 0;
}

#ifndef HAL_DEVICE_ID_NO_DCT
int hal_get_device_secret(char* data, size_t size, void* reserved)
{
Expand Down
45 changes: 26 additions & 19 deletions hal/src/rtl872x/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,16 @@ extern "C" {
#include "rtl8721d_efuse.h"
}

#include "platform_ncp.h"

namespace {

using namespace particle;

#define LOGICAL_EFUSE_SIZE 1024
#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 SERIAL_NUMBER_FRONT_PART_SIZE 9
#define HARDWARE_DATA_SIZE 4
#define HARDWARE_MODEL_SIZE 4
#define WIFI_OUID_SIZE 3
#define DEVICE_ID_PREFIX_SIZE 6

const uint8_t DEVICE_ID_PREFIX[] = {0x0a, 0x10, 0xac, 0xed, 0x20, 0x21};

} // Anonymous

int readLogicalEfuse(uint32_t offset, uint8_t* buf, size_t size) {
#if MODULE_FUNCTION != MOD_FUNC_BOOTLOADER
std::unique_ptr<uint8_t[]> efuseData(new uint8_t[LOGICAL_EFUSE_SIZE]);
Expand All @@ -77,8 +64,6 @@ int readLogicalEfuse(uint32_t offset, uint8_t* buf, size_t size) {
return SYSTEM_ERROR_NONE;
};

} // Anonymous

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] = {};
Expand Down Expand Up @@ -165,6 +150,28 @@ int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved)
return SYSTEM_ERROR_NONE;
}

int hal_get_device_hw_info(hal_device_hw_info* info, void* reserved) {
CHECK_TRUE(info, SYSTEM_ERROR_INVALID_ARGUMENT);
// HW Data format: | NCP_ID (LSB) | HW_VERSION | HW Feature Flags |
// | byte 0 | byte 1 | byte 2/3 |
uint8_t hw_data[4] = {};
CHECK(readLogicalEfuse(HARDWARE_DATA_OFFSET, (uint8_t*)hw_data, sizeof(hw_data)));
CHECK(hal_get_device_hw_model(&info->model, &info->variant, nullptr));
info->revision = hw_data[1];
if (info->revision == 0xff) {
info->revision = 0xffffffff;
}
info->features = ((uint32_t)hw_data[3] << 8) | (uint32_t)hw_data[2];
memset(info->ncp, 0xff, sizeof(info->ncp));
for (int i = 0; i < HAL_PLATFORM_NCP_COUNT; i++) {
PlatformNCPInfo ncpInfo = {};
if (!platform_ncp_get_info(i, &ncpInfo)) {
info->ncp[i] = (uint16_t)ncpInfo.identifier;
}
}
return 0;
}

#ifndef HAL_DEVICE_ID_NO_DCT
int hal_get_device_secret(char* data, size_t size, void* reserved) {
// Check if the device secret data is initialized in the DCT
Expand Down
19 changes: 19 additions & 0 deletions hal/src/rtl872x/deviceid_hal_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,29 @@
(type) == HAL_DEVICE_MAC_WIFI_AP || \
(type) == HAL_DEVICE_MAC_ETHERNET)

#define LOGICAL_EFUSE_SIZE 1024
#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 SERIAL_NUMBER_FRONT_PART_SIZE 9
#define HARDWARE_DATA_SIZE 4
#define HARDWARE_MODEL_SIZE 4
#define WIFI_OUID_SIZE 3
#define DEVICE_ID_PREFIX_SIZE 6

#ifdef __cplusplus
extern "C" {
#endif

int readLogicalEfuse(uint32_t offset, uint8_t* buf, size_t size);

/**
* Get the device's BLE MAC address.
*/
Expand Down
28 changes: 28 additions & 0 deletions hal/src/template/deviceid_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "deviceid_hal.h"
#include <stddef.h>
#include "system_error.h"

unsigned hal_get_device_id(uint8_t* dest, unsigned destLen)
{
Expand All @@ -38,3 +39,30 @@ unsigned hal_get_platform_id()
{
return PLATFORM_ID;
}

void hal_save_device_id(uint32_t offset) {
}

int hal_get_device_serial_number(char* str, size_t size, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_secret(char* data, size_t size, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_set_device_secret(char* data, size_t size, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_hw_version(uint32_t* revision, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_hw_model(uint32_t* model, uint32_t* variant, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}

int hal_get_device_hw_info(hal_device_hw_info* info, void* reserved) {
return SYSTEM_ERROR_NOT_SUPPORTED;
}
7 changes: 7 additions & 0 deletions hal/src/tracker/hal_platform_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@
#define HAL_PLATFORM_MAX_CLOUD_CONNECT_TIME (9*60*1000)

#define PRODUCT_SERIES "boron"

#define PLATFORM_TRACKER_MODEL_BARE_SOM_DEFAULT (0xffff)
#define PLATFORM_TRACKER_MODEL_BARE_SOM (0x0000)
#define PLATFORM_TRACKER_MODEL_EVAL (0x0001)
#define PLATFORM_TRACKER_MODEL_TRACKERONE (0x0002)
#define PLATFORM_TRACKER_MODEL_MONITORONE (0x0003)
#define PLATFORM_TRACKER_MODEL_MAX (PLATFORM_TRACKER_MODEL_MONITORONE)
6 changes: 3 additions & 3 deletions hal/src/tracker/platform_ncp_quectel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ const auto NCP_IDX_SECONDARY_ESP32 = 1;
PlatformNCPIdentifier platform_primary_ncp_identifier() {
// Check the DCT
uint8_t ncpId = 0;
int r = dct_read_app_data_copy(DCT_NCP_ID_OFFSET, &ncpId, 1);
int r = dct_read_app_data_copy(DCT_NCP_ID_OFFSET, &ncpId, sizeof(ncpId));
if (r < 0 || !isValidNcpId(ncpId)) {
// Check the OTP flash
r = hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, NCP_ID_OTP_ADDRESS, &ncpId, 1);
r = hal_exflash_read_special(HAL_EXFLASH_SPECIAL_SECTOR_OTP, NCP_ID_OTP_ADDRESS, &ncpId, sizeof(ncpId));
if (r < 0 || !isValidNcpId(ncpId)) {
ncpId = PlatformNCPIdentifier::PLATFORM_NCP_UNKNOWN;
return PlatformNCPIdentifier::PLATFORM_NCP_UNKNOWN;
}
}
return (PlatformNCPIdentifier)ncpId;
Expand Down
2 changes: 1 addition & 1 deletion hal/src/trackerm/hal_platform_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define HAL_PLATFORM_SPI_NUM (2)
#define HAL_PLATFORM_I2C_NUM (2)
#define HAL_PLATFORM_USART_NUM (3)
#define HAL_PLATFORM_NCP_COUNT (1)
#define HAL_PLATFORM_NCP_COUNT (2)
#define HAL_PLATFORM_WIFI (1)
#define HAL_PLATFORM_WIFI_COMPAT (0)
// #define HAL_PLATFORM_WIFI_NCP_SDIO (1)
Expand Down

0 comments on commit 1bf0fc2

Please sign in to comment.