mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-08 22:21:35 +08:00
wip: squashed patch
This commit is contained in:
@@ -60,3 +60,5 @@ endif()
|
||||
|
||||
add_subdirectory(px4_work_queue)
|
||||
add_subdirectory(work_queue)
|
||||
|
||||
px4_add_unit_gtest(SRC board_identity_test.cpp LINKLIBS px4_platform)
|
||||
|
||||
@@ -40,6 +40,12 @@
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* For special cases, specific boards may need to override the UUID, instead of using the generic
|
||||
* PX4 GUID (gettable via 'board_get_px4_guid_formated' function). In that case we define the cascaded
|
||||
* UUID function getters to incorporate the overridden UUID into the GUID.
|
||||
*/
|
||||
#if defined(BOARD_OVERRIDE_UUID) || defined(BOARD_OVERRIDE_MFGUID) || defined(BOARD_OVERRIDE_PX4_GUID)
|
||||
static const uint16_t soc_arch_id = PX4_SOC_ARCH_ID;
|
||||
static const char board_uuid[17] = BOARD_OVERRIDE_UUID;
|
||||
@@ -72,11 +78,11 @@ int board_get_uuid32_formated(char *format_buffer, int size,
|
||||
int offset = 0;
|
||||
int sep_size = seperator ? strlen(seperator) : 0;
|
||||
|
||||
for (unsigned i = 0; i < PX4_CPU_UUID_WORD32_LENGTH; i++) {
|
||||
for (unsigned i = 0; (offset < size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH); i++) {
|
||||
offset += snprintf(&format_buffer[offset], size - offset, format, uuid[i]);
|
||||
|
||||
if (sep_size && i < PX4_CPU_UUID_WORD32_LENGTH - 1) {
|
||||
strcat(&format_buffer[offset], seperator);
|
||||
if (sep_size && (offset < size - sep_size - 1) && (i < PX4_CPU_UUID_WORD32_LENGTH - 1)) {
|
||||
strncat(&format_buffer[offset], seperator, size - offset);
|
||||
offset += sep_size;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2022 PX4 Development Team. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @file board_identity_test.cpp
|
||||
* @author Junwoo Hwang <junwoo091400@gmail.com>
|
||||
*
|
||||
* Unit test for checking if board identification string retrieval function works correctly,
|
||||
* and without any unexpected buffer overflows
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Test `board_get_uuid32_formated` function for buffer overflows & correct formatting
|
||||
*
|
||||
* This is a deprecated function that gets used on exceptional cases where "BOARD_OVERRIDE_UUID" is defined.
|
||||
* Which is the case in SITL target as well as few others. In that case, the generic function `board_get_px4_guid_formated`
|
||||
* calls this function to fill out the buffer.
|
||||
*
|
||||
* It should write 16 bytes of UUID for SITL (PX4_CPU_UUID_BYTE_LENGTH depends on target!) in specified format, and if the
|
||||
* buffer has size less than that, the buffer shouldn't overflow, respecting the size argument.
|
||||
*
|
||||
* For example, with the format "02x", which writes each byte in 2-character hexadecimal value, the 16 bytes of UUID
|
||||
* will be written across '33' bytes of buffer, as each UUID byte will use 2 bytes, and NULL terminator will use 1 byte.
|
||||
*
|
||||
* If the buffer overflow occurs, the GCC compiler level will terminate the test with '*** stack smashing detected ***'
|
||||
* message, which should cancel the test
|
||||
*
|
||||
* TODO: Make the test fail properly, instead of getting canceled in stack smashing case
|
||||
*/
|
||||
TEST(BoardIdentityTest, UUID32BufferOverflow)
|
||||
{
|
||||
// Get groundtruth UUID value. Without separator, should require 33 bytes
|
||||
char groundtruth_uuid_no_sep[50];
|
||||
board_get_uuid32_formated(groundtruth_uuid_no_sep, sizeof(groundtruth_uuid_no_sep), "%02x", NULL); // No separator
|
||||
EXPECT_EQ(32, strlen(groundtruth_uuid_no_sep));
|
||||
// Get groundtruth UUID value. With separator, should be 36 bytes
|
||||
char groundtruth_uuid_with_sep[50];
|
||||
board_get_uuid32_formated(groundtruth_uuid_with_sep, sizeof(groundtruth_uuid_with_sep), "%02x", "-"); // With separator
|
||||
EXPECT_EQ(35, strlen(groundtruth_uuid_with_sep));
|
||||
|
||||
// 12 bytes : Shouldn't be enough, but shouldn't also cause buffer overflow
|
||||
// We check : 1. If the function causes buffer overflow | 2. If the buffer gets filled with correct data
|
||||
// Note: Since for `char buf[N]`, the maimum string length is N-1, we give N-1 for `strncmp` function.
|
||||
char buf_12[12];
|
||||
board_get_uuid32_formated(buf_12, sizeof(buf_12), "%02x", NULL); // No separator
|
||||
EXPECT_EQ(0, strncmp(groundtruth_uuid_no_sep, buf_12, sizeof(buf_12) - 1));
|
||||
board_get_uuid32_formated(buf_12, sizeof(buf_12), "%02x", "-"); // With separator
|
||||
EXPECT_EQ(0, strncmp(groundtruth_uuid_with_sep, buf_12, sizeof(buf_12) - 1));
|
||||
|
||||
// 33 bytes: Should be enough for UUID with no separator, but not enough for UUID with separator
|
||||
char buf_33[33];
|
||||
board_get_uuid32_formated(buf_33, sizeof(buf_33), "%02x", NULL); // No separator
|
||||
EXPECT_EQ(0, strncmp(groundtruth_uuid_no_sep, buf_33, sizeof(buf_33) - 1));
|
||||
board_get_uuid32_formated(buf_33, sizeof(buf_33), "%02x", "-"); // With separator
|
||||
EXPECT_EQ(0, strncmp(groundtruth_uuid_with_sep, buf_33, sizeof(buf_33) - 1));
|
||||
|
||||
// 40 bytes: Should be enough for UUID for all cases
|
||||
char buf_40[40];
|
||||
board_get_uuid32_formated(buf_40, sizeof(buf_40), "%02x", NULL); // No separator
|
||||
EXPECT_EQ(0, strncmp(groundtruth_uuid_no_sep, buf_40, sizeof(buf_40) - 1));
|
||||
board_get_uuid32_formated(buf_40, sizeof(buf_40), "%02x", "-"); // With separator
|
||||
EXPECT_EQ(0, strncmp(groundtruth_uuid_with_sep, buf_40, sizeof(buf_40) - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test `board_get_mfguid_formated` function for buffer overflows & correct formatting
|
||||
*
|
||||
* The size that should be written is PX4_CPU_MFGUID_FORMAT_SIZE, which is usually 33 (like UUID)
|
||||
*/
|
||||
TEST(BoardIdentityTest, MFGUIDBufferOverflow)
|
||||
{
|
||||
// Get groundtruth MFGUID value
|
||||
char groundtruth_mfg_uid[50];
|
||||
board_get_mfguid_formated(groundtruth_mfg_uid, sizeof(groundtruth_mfg_uid));
|
||||
|
||||
// Expect buffer of size PX4_CPU_MFGUID_FORMAT_SIZE, which means string length will be -1 of that.
|
||||
EXPECT_EQ(PX4_CPU_MFGUID_FORMAT_SIZE - 1, strlen(groundtruth_mfg_uid));
|
||||
|
||||
// 12 bytes : Shouldn't be enough, but shouldn't also cause buffer overflow
|
||||
// We check : 1. If the function causes buffer overflow | 2. If the buffer gets filled with correct data
|
||||
// Note: Since for `char buf[N]`, the maimum string length is N-1, we give N-1 for `strncmp` function.
|
||||
char buf_12[12];
|
||||
board_get_mfguid_formated(buf_12, sizeof(buf_12));
|
||||
EXPECT_EQ(0, strncmp(groundtruth_mfg_uid, buf_12, sizeof(buf_12) - 1));
|
||||
|
||||
// 33 bytes: Should be just enough for MFGUID
|
||||
char buf_33[33];
|
||||
board_get_mfguid_formated(buf_33, sizeof(buf_33));
|
||||
EXPECT_EQ(0, strncmp(groundtruth_mfg_uid, buf_33, sizeof(buf_33) - 1));
|
||||
|
||||
// 40 bytes: Should be enough for MFGUID
|
||||
char buf_40[40];
|
||||
board_get_mfguid_formated(buf_40, sizeof(buf_40));
|
||||
EXPECT_EQ(0, strncmp(groundtruth_mfg_uid, buf_40, sizeof(buf_40) - 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Test `board_get_px4_guid_formated` function for buffer overflows & correct formatting
|
||||
*
|
||||
* The size that needs to be written is PX4_GUID_FORMAT_SIZE, which includes the NULL
|
||||
* termination character into account, which is usually 33.
|
||||
*/
|
||||
TEST(BoardIdentityTest, PX4GUIDBufferOverflow)
|
||||
{
|
||||
// Get groundtruth PX4 GUID value with enough buffer (minimum 32 + 1)
|
||||
char groundtruth_px4_guid[50];
|
||||
board_get_px4_guid_formated(groundtruth_px4_guid, sizeof(groundtruth_px4_guid));
|
||||
|
||||
// Expect buffer of size PX4_GUID_FORMAT_SIZE, which means string length will be -1 of that.
|
||||
EXPECT_EQ(PX4_GUID_FORMAT_SIZE - 1, strlen(groundtruth_px4_guid));
|
||||
|
||||
// 12 bytes : Shouldn't be enough, but shouldn't also cause buffer overflow
|
||||
// We check : 1. If the function causes buffer overflow | 2. If the buffer gets filled with correct data
|
||||
// Note: Since for `char buf[N]`, the maimum string length is N-1, we give N-1 for `strncmp` function.
|
||||
char buf_12[12];
|
||||
board_get_px4_guid_formated(buf_12, sizeof(buf_12));
|
||||
EXPECT_EQ(0, strncmp(groundtruth_px4_guid, buf_12, sizeof(buf_12) - 1));
|
||||
|
||||
// 33 bytes: Should be just enough for PX4 GUID
|
||||
char buf_33[33];
|
||||
board_get_px4_guid_formated(buf_33, sizeof(buf_33));
|
||||
EXPECT_EQ(0, strncmp(groundtruth_px4_guid, buf_33, sizeof(buf_33) - 1));
|
||||
|
||||
// 40 bytes: Should be enough for PX4 GUID
|
||||
char buf_40[40];
|
||||
board_get_px4_guid_formated(buf_40, sizeof(buf_40));
|
||||
EXPECT_EQ(0, strncmp(groundtruth_px4_guid, buf_40, sizeof(buf_40) - 1));
|
||||
}
|
||||
@@ -82,6 +82,10 @@
|
||||
#define BOARD_NUM_SPI_CFG_HW_VERSIONS 1
|
||||
#endif
|
||||
|
||||
#ifndef BOARD_MTD_NUM_EEPROM
|
||||
#define BOARD_MTD_NUM_EEPROM 1
|
||||
#endif
|
||||
|
||||
/* ADC defining tools
|
||||
* We want to normalize the V5 Sensing to V = (adc_dn) * ADC_V5_V_FULL_SCALE/(2 ^ ADC_BITS) * ADC_V5_SCALE)
|
||||
*/
|
||||
@@ -258,9 +262,12 @@
|
||||
|
||||
#if defined(BOARD_HAS_HW_VERSIONING)
|
||||
# define BOARD_HAS_VERSIONING 1
|
||||
# define HW_VER_REV(v,r) ((uint32_t)((v) & 0xff) << 8) | ((uint32_t)(r) & 0xff)
|
||||
# define HW_VER_REV(v,r) ((uint32_t)((v) & 0xffff) << 16) | ((uint32_t)(r) & 0xffff)
|
||||
#endif
|
||||
|
||||
#define HW_INFO_REV_DIGITS 3
|
||||
#define HW_INFO_VER_DIGITS 3
|
||||
|
||||
/* Default LED logical to color mapping */
|
||||
|
||||
#if defined(BOARD_OVERLOAD_LED)
|
||||
@@ -950,7 +957,7 @@ int board_get_mfguid_formated(char *format_buffer, int size); // DEPRICATED use
|
||||
int board_get_px4_guid(px4_guid_t guid);
|
||||
|
||||
/************************************************************************************
|
||||
* Name: board_get_mfguid_formated
|
||||
* Name: board_get_px4_guid_formated
|
||||
*
|
||||
* Description:
|
||||
* All boards either provide a way to retrieve a formatted string of the
|
||||
|
||||
@@ -70,6 +70,9 @@ static inline constexpr bool PX4_ISFINITE(double x) { return __builtin_isfinite(
|
||||
#define PX4_O_MODE_666 0666
|
||||
#define PX4_O_MODE_600 0600
|
||||
|
||||
#define DIRENT_REGULAR_FILE DTYPE_FILE
|
||||
#define DIRENT_DIRECTORY DTYPE_DIRECTORY
|
||||
|
||||
#elif defined(__PX4_POSIX)
|
||||
/****************************************************************************
|
||||
* POSIX Specific defines
|
||||
@@ -91,6 +94,9 @@ static inline constexpr bool PX4_ISFINITE(double x) { return __builtin_isfinite(
|
||||
#define USEC_PER_TICK (1000000/PX4_TICKS_PER_SEC)
|
||||
#define USEC2TICK(x) (((x)+(USEC_PER_TICK/2))/USEC_PER_TICK)
|
||||
|
||||
#define DIRENT_REGULAR_FILE DT_REG
|
||||
#define DIRENT_DIRECTORY DT_DIR
|
||||
|
||||
__BEGIN_DECLS
|
||||
extern long PX4_TICKS_PER_SEC;
|
||||
__END_DECLS
|
||||
|
||||
@@ -37,12 +37,13 @@ typedef enum {
|
||||
MTD_PARAMETERS = 1,
|
||||
MTD_WAYPOINTS = 2,
|
||||
MTD_CALDATA = 3,
|
||||
MTD_MFT = 4,
|
||||
MTD_ID = 5,
|
||||
MTD_NET = 6,
|
||||
MTD_MFT_VER = 4,
|
||||
MTD_MFT_REV = 5,
|
||||
MTD_ID = 6,
|
||||
MTD_NET = 7
|
||||
} px4_mtd_types_t;
|
||||
#define PX4_MFT_MTD_TYPES {MTD_PARAMETERS, MTD_WAYPOINTS, MTD_CALDATA, MTD_MFT, MTD_ID, MTD_NET}
|
||||
#define PX4_MFT_MTD_STR_TYPES {"MTD_PARAMETERS", "MTD_WAYPOINTS", "MTD_CALDATA", "MTD_MFT", "MTD_ID", "MTD_NET"}
|
||||
#define PX4_MFT_MTD_TYPES {MTD_PARAMETERS, MTD_WAYPOINTS, MTD_CALDATA, MTD_MFT_VER, MTD_MFT_REV, MTD_ID, MTD_NET}
|
||||
#define PX4_MFT_MTD_STR_TYPES {"MTD_PARAMETERS", "MTD_WAYPOINTS", "MTD_CALDATA", "MTD_MFT_VER", "MTD_MFT_REV", "MTD_ID", "MTD_NET"}
|
||||
|
||||
typedef struct {
|
||||
const px4_mtd_types_t type;
|
||||
@@ -100,6 +101,6 @@ __EXPORT int px4_mtd_config(const px4_mtd_manifest_t *mft_mtd);
|
||||
* 0 (get !=null) item by type's value is returned at get;
|
||||
*
|
||||
************************************************************************************/
|
||||
__EXPORT int px4_mtd_query(const char *type, const char *val, const char **get = nullptr);
|
||||
__EXPORT int px4_mtd_query(const char *type, const char *val, const char **get);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
@@ -65,7 +65,7 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
const uint32_t nmft;
|
||||
const px4_mft_entry_s *mfts;
|
||||
const px4_mft_entry_s *mfts[];
|
||||
} px4_mft_s;
|
||||
|
||||
#include "px4_platform_common/mtd_manifest.h"
|
||||
@@ -88,6 +88,19 @@ __BEGIN_DECLS
|
||||
|
||||
__EXPORT const px4_mft_s *board_get_manifest(void);
|
||||
|
||||
/************************************************************************************
|
||||
* Name: board_get_base_eeprom_mtd_manifest
|
||||
*
|
||||
* Description:
|
||||
* A board will provide this function to return the mtd with eeprom manifest
|
||||
*
|
||||
* Returned Value:
|
||||
* pointer to mtd manifest
|
||||
*
|
||||
************************************************************************************/
|
||||
|
||||
__EXPORT const px4_mtd_manifest_t *board_get_base_eeprom_mtd_manifest(void);
|
||||
|
||||
/************************************************************************************
|
||||
* Name: px4_mft_configure
|
||||
*
|
||||
|
||||
@@ -35,6 +35,8 @@
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define MAX_MTD_INSTANCES 5u
|
||||
|
||||
// The data needed to interface with mtd device's
|
||||
|
||||
typedef struct {
|
||||
@@ -60,7 +62,7 @@ typedef struct {
|
||||
* This can be Null if there are no mtd instances.
|
||||
*
|
||||
*/
|
||||
__EXPORT mtd_instance_s *px4_mtd_get_instances(unsigned int *count);
|
||||
__EXPORT mtd_instance_s **px4_mtd_get_instances(unsigned int *count);
|
||||
|
||||
/*
|
||||
Get device complete geometry or a device
|
||||
@@ -75,7 +77,9 @@ __EXPORT int px4_mtd_get_geometry(const mtd_instance_s *instance, unsigned long
|
||||
*/
|
||||
__EXPORT ssize_t px4_mtd_get_partition_size(const mtd_instance_s *instance, const char *partname);
|
||||
|
||||
FAR struct mtd_dev_s *px4_at24c_initialize(FAR struct i2c_master_s *dev,
|
||||
uint8_t address);
|
||||
int px4_at24c_initialize(FAR struct i2c_master_s *dev,
|
||||
uint8_t address, FAR struct mtd_dev_s **mtd_dev);
|
||||
|
||||
void px4_at24c_deinitialize(void);
|
||||
|
||||
__END_DECLS
|
||||
|
||||
@@ -65,7 +65,7 @@ static constexpr wq_config_t I2C3{"wq:I2C3", 2336, -11};
|
||||
static constexpr wq_config_t I2C4{"wq:I2C4", 2336, -12};
|
||||
|
||||
// PX4 att/pos controllers, highest priority after sensors.
|
||||
static constexpr wq_config_t nav_and_controllers{"wq:nav_and_controllers", 2240, -13};
|
||||
static constexpr wq_config_t nav_and_controllers{"wq:nav_and_controllers", 2280, -13};
|
||||
|
||||
static constexpr wq_config_t INS0{"wq:INS0", 6000, -14};
|
||||
static constexpr wq_config_t INS1{"wq:INS1", 6000, -15};
|
||||
|
||||
@@ -47,7 +47,7 @@ void px4_set_spi_buses_from_hw_version()
|
||||
#if defined(BOARD_HAS_SIMPLE_HW_VERSIONING)
|
||||
int hw_version_revision = board_get_hw_version();
|
||||
#else
|
||||
int hw_version_revision = (board_get_hw_version() << 8) | board_get_hw_revision();
|
||||
int hw_version_revision = HW_VER_REV(board_get_hw_version(), board_get_hw_revision());
|
||||
#endif
|
||||
|
||||
|
||||
@@ -66,12 +66,12 @@ void px4_set_spi_buses_from_hw_version()
|
||||
}
|
||||
}
|
||||
|
||||
const px4_spi_bus_t *px4_spi_buses{};
|
||||
const px4_spi_bus_t *px4_spi_buses{nullptr};
|
||||
#endif
|
||||
|
||||
int px4_find_spi_bus(uint32_t devid)
|
||||
{
|
||||
for (int i = 0; i < SPI_BUS_MAX_BUS_ITEMS; ++i) {
|
||||
for (int i = 0; px4_spi_buses != nullptr && i < SPI_BUS_MAX_BUS_ITEMS; ++i) {
|
||||
const px4_spi_bus_t &bus_data = px4_spi_buses[i];
|
||||
|
||||
if (bus_data.bus == -1) {
|
||||
|
||||
Reference in New Issue
Block a user