fix(802.15.4) added some error checks to make the code more robust

This commit is contained in:
zwx
2026-04-22 02:35:02 +00:00
committed by Zhang Wen Xu
parent 0bed60db48
commit 7adeb08b02
15 changed files with 299 additions and 159 deletions
@@ -26,19 +26,20 @@ extern "C" {
typedef struct {
uint8_t short_addr[CONFIG_IEEE802154_PENDING_TABLE_SIZE][IEEE802154_FRAME_SHORT_ADDR_SIZE]; /*!< Short address table */
uint8_t ext_addr[CONFIG_IEEE802154_PENDING_TABLE_SIZE][IEEE802154_FRAME_EXT_ADDR_SIZE]; /*!< Extend address table */
uint8_t short_addr_mask[IEEE802154_PENDING_TABLE_MASK_SIZE]; /*!< The mask which the index of short address table is used */
uint8_t ext_addr_mask[IEEE802154_PENDING_TABLE_MASK_SIZE]; /*!< The mask which the index of extended address table is used */
uint8_t short_addr_mask[IEEE802154_PENDING_TABLE_MASK_SIZE]; /*!< Bit mask to track which indices of the short address table are used */
uint8_t ext_addr_mask[IEEE802154_PENDING_TABLE_MASK_SIZE]; /*!< Bit mask to track which indices of the extended address table are used */
} ieee802154_pending_table_t;
/**
* @brief Add an address to the pending table.
*
* @param[in] addr The pointer to the address needs to be added.
* @param[in] addr The pointer to the address needs to be added. Must not be NULL.
* @param[in] is_short The type of address, true for short address, false for extended.
*
* @return
* - ESP_OK on success.
* - ESP_FAIL on failure due to the table is full.
* - ESP_ERR_INVALID_ARG if addr is NULL.
*
*/
esp_err_t ieee802154_add_pending_addr(esp_ieee802154_multipan_index_t inf_index, const uint8_t *addr, bool is_short);
@@ -46,12 +47,13 @@ esp_err_t ieee802154_add_pending_addr(esp_ieee802154_multipan_index_t inf_index,
/**
* @brief Remove an address in pending table.
*
* @param[in] addr The pointer to the address needs to be cleared.
* @param[in] addr The pointer to the address needs to be cleared. Must not be NULL.
* @param[in] is_short The type of address, true for short address, false for extended.
*
* @return
* - ESP_OK on success.
* - ESP_FAIL on failure if the given address is not present in the pending table.
* - ESP_ERR_INVALID_ARG if addr is NULL.
*
*/
esp_err_t ieee802154_clear_pending_addr(esp_ieee802154_multipan_index_t inf_index, const uint8_t *addr, bool is_short);
@@ -67,10 +69,10 @@ void ieee802154_reset_pending_table(esp_ieee802154_multipan_index_t inf_index, b
/**
* @brief Check whether the pending bit should be set or not in the ack frame.
*
* @param[in] frame The pointer to the received frame.
* @param[in] frame The pointer to the received frame. Must not be NULL.
*
* @return
* - True The pending bit should be set, otherwise False.
* - True if the pending bit should be set, otherwise false.
*
*/
bool ieee802154_ack_config_pending_bit(const uint8_t *frame, const esp_ieee802154_frame_info_t *frame_info);
@@ -15,9 +15,9 @@ extern "C" {
#endif
#define IEEE802154_FRAME_MAX_LEN 127
#define IEEE802154_FRAME_MIN_LEN 3
#define IEEE802154_FRAME_INVALID_OFFSET 0xff
#define IEEE802154_FRAME_INVALID_ADDR_MODE 0xff
#define IEEE802154_FRAME_INVALID_VALUE 0xff
#define IEEE802154_FRAME_TYPE_OFFSET 1
#define IEEE802154_FRAME_TYPE_MASK 0x07
@@ -111,7 +111,7 @@ static inline bool ieee802154_is_supported_frame_type(uint8_t frame_type)
/**
* @brief Get the frame type.
*
* @param[in] frame The pointer to the frame.
* @param[in] frame The pointer to the frame. Must not be NULL.
*
* @return
* - The type of the frame.
@@ -122,7 +122,7 @@ uint8_t ieee802154_frame_get_type(const uint8_t *frame);
/**
* @brief Get the frame version.
*
* @param[in] frame The pointer to the frame.
* @param[in] frame The pointer to the frame. Must not be NULL.
*
* @return
* - The version of the frame.
@@ -133,7 +133,7 @@ uint8_t ieee802154_frame_get_version(const uint8_t *frame);
/**
* @brief Is the frame ack required.
*
* @param[in] frame The pointer to the frame.
* @param[in] frame The pointer to the frame. Must not be NULL.
*
* @return
* - True if the frame is ack required, otherwise false.
@@ -154,13 +154,14 @@ bool ieee802154_frame_is_security_enabled(const uint8_t *frame);
/**
* @brief Get the destination address of the frame.
*
* @param[in] frame The pointer to the frame.
* @param[out] addr The pointer to the address.
* @param[in] frame The pointer to the frame. Must not be NULL.
* @param[out] addr The pointer to the address. Must not be NULL.
*
* @return
* - IEEE802154_FRAME_DST_MODE_NONE if destination address mode is none.
* - IEEE802154_FRAME_DST_MODE_SHORT if destination address mode is short.
* - IEEE802154_FRAME_DST_MODE_EXT if destination address mode is extended.
* - IEEE802154_FRAME_INVALID_VALUE if frame is NULL, addr is NULL, or frame type is invalid.
*
*/
uint8_t ieee802154_frame_get_dst_addr(const uint8_t *frame, uint8_t *addr);
@@ -168,49 +169,49 @@ uint8_t ieee802154_frame_get_dst_addr(const uint8_t *frame, uint8_t *addr);
/**
* @brief Get the source address of the frame.
*
* @param[in] frame The pointer to the frame.
* @param[out] addr The pointer to the address.
* @param[in] frame The pointer to the frame. Must not be NULL.
* @param[out] addr The pointer to the address. Must not be NULL.
*
* @return
* - IEEE802154_FRAME_SRC_MODE_NONE if source address mode is none.
* - IEEE802154_FRAME_SRC_MODE_SHORT if source address mode is short.
* - IEEE802154_FRAME_SRC_MODE_EXT if source address mode is extended.
*
* - IEEE802154_FRAME_INVALID_VALUE if frame is NULL, addr is NULL, or frame type is invalid.
*/
uint8_t ieee802154_frame_get_src_addr(const uint8_t *frame, uint8_t *addr);
/**
* @brief Get the offset of the private payload.
*
* @param[in] frame The pointer to the frame.
* @param[in] frame The pointer to the frame. Must not be NULL.
*
* @return
* - The offset of the private payload
*
* - IEEE802154_FRAME_INVALID_VALUE if frame is NULL, or frame type is invalid.
*/
uint8_t ieee802154_frame_get_security_payload_offset(uint8_t *frame);
/**
* @brief Get the destination PAN ID of the frame.
*
* @param[in] frame The pointer to the frame.
* @param[out] panid The pointer to the destination PAN ID.
* @param[in] frame The pointer to the frame. Must not be NULL.
* @param[out] panid The pointer to the destination PAN ID. Must not be NULL.
*
* @return
* - ESP_OK if destination PAN ID is present, otherwise ESP_FAIL.
*
* - ESP_FAIL if frame is NULL, panid is NULL, or frame type is invalid.
*/
esp_err_t ieee802154_frame_get_dest_panid(const uint8_t *frame, uint8_t *panid);
/**
* @brief Get the source PAN ID of the frame.
*
* @param[in] frame The pointer to the frame.
* @param[out] panid The pointer to the source PAN ID.
* @param[in] frame The pointer to the frame. Must not be NULL.
* @param[out] panid The pointer to the source PAN ID. Must not be NULL.
*
* @return
* - ESP_OK if source PAN ID is present, otherwise ESP_FAIL.
*
* - ESP_FAIL if frame is NULL, panid is NULL, or frame type is invalid.
*/
esp_err_t ieee802154_frame_get_src_panid(const uint8_t *frame, uint8_t *panid);
@@ -161,7 +161,7 @@ void ieee802154_pib_set_cca_threshold(int8_t cca_threshold);
* @return
* - The CCA threshold has been set in the PIB.
*/
int8_t ieee802154_pib_get_cca_threshold(void);
int8_t ieee802154_pib_get_cca_threshold(void);
/**
* @brief Set the CCA mode to the PIB.
@@ -262,16 +262,16 @@ ieee802154_ll_pending_mode_t ieee802154_pib_get_pending_mode(esp_ieee802154_mult
/**
* @brief Configure the radio mode when the radio is going to enter idle to the PIB.
*
* @param[in] enable True for continuing to receive when the radio is going to enter ilde, otherwise false.
* @param[in] enable True for continuing to receive when the radio is going to enter idle, otherwise false.
*
*/
void ieee802154_pib_set_rx_when_idle(bool enable);
/**
* @brief Get the radio mode when the radio is going to enter ilde to the PIB.
* @brief Get the radio mode when the radio is going to enter idle to the PIB.
*
* @return
* - True for continuing to receive when the radio is going to enter ilde, otherwise false.
* - True for continuing to receive when the radio is going to enter idle, otherwise false.
*
*/
bool ieee802154_pib_get_rx_when_idle(void);
@@ -23,6 +23,12 @@ extern "C" {
#define IEEE802154_OQPSK_2P4G_CHANNEL_MIN 11
#define IEEE802154_OQPSK_2P4G_CHANNEL_MAX 26
#define IEEE802154_RETURN_ON_FALSE_SILENTLY(a, ret) do { \
if (unlikely(!(a))) { \
return ret; \
} \
} while(0)
static inline bool ieee802154_is_valid_channel(uint8_t channel)
{
return ((channel <= IEEE802154_OQPSK_2P4G_CHANNEL_MAX) && (channel >= IEEE802154_OQPSK_2P4G_CHANNEL_MIN));