Skip to content

Commit

Permalink
nrf_802154: rev 563da20eb4a58ad9a8abd3f512d672f4555307db
Browse files Browse the repository at this point in the history
This commit updates revision of the nrf_802154 component to commit
563da20eb4a58ad9a8abd3f512d672f4555307db of the sdk-nrf-802154
repository.

Signed-off-by: Artur Hadasz <[email protected]>
  • Loading branch information
ahasztag authored and rlubos committed Feb 8, 2022
1 parent a85ab18 commit 92748b2
Show file tree
Hide file tree
Showing 16 changed files with 409 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ static uint8_t ie_header_terminate(const uint8_t * p_ie_data,
static bool encryption_prepare(const nrf_802154_frame_parser_data_t * p_ack_data)
{
#if NRF_802154_ENCRYPTION_ENABLED
nrf_802154_encrypt_ack_reset();

if (nrf_802154_frame_parser_security_enabled_bit_is_set(p_ack_data) == false)
{
return true;
Expand Down
114 changes: 86 additions & 28 deletions nrf_802154/driver/src/mac_features/nrf_802154_csma_ca.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,24 @@

#if NRF_802154_CSMA_CA_ENABLED

/**
* @brief States of the CSMA-CA procedure.
*/
typedef enum
{
PROCEDURE_STOPPED,
PROCEDURE_RUNNING,
PROCEDURE_ABORTING
} procedure_state_t;

static uint8_t m_nb; ///< The number of times the CSMA-CA algorithm was required to back off while attempting the current transmission.
static uint8_t m_be; ///< Backoff exponent, which is related to how many backoff periods a device shall wait before attempting to assess a channel.

static uint8_t * mp_data; ///< Pointer to a buffer containing PHR and PSDU of the frame being transmitted.
static nrf_802154_transmitted_frame_props_t m_data_props; ///< Structure containing detailed properties of data in buffer.
static bool m_is_running; ///< Indicates if CSMA-CA procedure is running.
static procedure_state_t m_proc_state; ///< The current state of the CSMA-CA procedure.

static volatile bool m_nested_timeslot_cancel; ///< Variable protecting against cancelling the timeslot nested inside nrf_802154_rsch_delayed_timeslot_request
/**
* @brief Perform appropriate actions for busy channel conditions.
*
Expand All @@ -86,23 +97,31 @@ static bool channel_busy(void);
*/
static bool procedure_is_running(void)
{
return m_is_running;
return PROCEDURE_RUNNING == m_proc_state;
}

/**
* @brief Stop CSMA-CA procedure.
* @brief Set state of CSMA-CA procedure.
*
* @param[in] state The new state of the procedure
*/
static void procedure_stop(void)
static void set_procedure_state(procedure_state_t state)
{
if (procedure_is_running())
{
nrf_802154_log_function_enter(NRF_802154_LOG_VERBOSITY_HIGH);

nrf_802154_rsch_delayed_timeslot_cancel(NRF_802154_RESERVED_CSMACA_ID);
m_is_running = false;
nrf_802154_log_function_enter(NRF_802154_LOG_VERBOSITY_HIGH);
assert(PROCEDURE_ABORTING != state || PROCEDURE_RUNNING == m_proc_state);
assert(PROCEDURE_ABORTING != m_proc_state || PROCEDURE_STOPPED == state);

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_HIGH);
if (m_proc_state != state)
{
if (procedure_is_running() && !m_nested_timeslot_cancel)
{
// Stopping/aborting CSMA-CA
nrf_802154_rsch_delayed_timeslot_cancel(NRF_802154_RESERVED_CSMACA_ID);
}
m_proc_state = state;
}

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_HIGH);
}

static void priority_leverage(void)
Expand All @@ -127,6 +146,21 @@ static void priority_leverage(void)
nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_HIGH);
}

/**
* @brief Notify MAC layer that CSMA-CA failed
*
* @param[in] error The error that caused the failure
*/
static void notify_failed(nrf_802154_tx_error_t error)
{
// core rejected attempt, use my current frame_props
nrf_802154_transmit_done_metadata_t metadata = {};

metadata.frame_props = m_data_props;

nrf_802154_notify_transmit_failed(mp_data, error, &metadata);
}

/**
* @brief Notify MAC layer that channel is busy if tx request failed and there are no retries left.
*
Expand All @@ -142,12 +176,7 @@ static void notify_busy_channel(bool result)
// the comparison uses `greater or equal` instead of `greater than`.
if (!result && (m_nb >= nrf_802154_pib_csmaca_max_backoffs_get()))
{
// core rejected attempt, use my current frame_props
nrf_802154_transmit_done_metadata_t metadata = {};

metadata.frame_props = m_data_props;

nrf_802154_notify_transmit_failed(mp_data, NRF_802154_TX_ERROR_BUSY_CHANNEL, &metadata);
notify_failed(NRF_802154_TX_ERROR_BUSY_CHANNEL);
}

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_HIGH);
Expand Down Expand Up @@ -288,11 +317,17 @@ static void random_backoff_start(void)
nrf_802154_rsch_delayed_timeslot_cancel(NRF_802154_RESERVED_CSMACA_ID);
}

m_nested_timeslot_cancel = true;
// Delayed timeslot with these parameters should always be scheduled
if (!nrf_802154_rsch_delayed_timeslot_request(&backoff_ts_param))
{
assert(false);
}
m_nested_timeslot_cancel = false;
if (!procedure_is_running())
{
nrf_802154_rsch_delayed_timeslot_cancel(NRF_802154_RESERVED_CSMACA_ID);
}

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_HIGH);
}
Expand All @@ -314,7 +349,7 @@ static bool channel_busy(void)

if (m_nb > nrf_802154_pib_csmaca_max_backoffs_get())
{
procedure_stop();
set_procedure_state(PROCEDURE_STOPPED);
}
else
{
Expand All @@ -328,7 +363,7 @@ static bool channel_busy(void)
return result;
}

void nrf_802154_csma_ca_start(uint8_t * p_data,
bool nrf_802154_csma_ca_start(uint8_t * p_data,
const nrf_802154_transmit_csma_ca_metadata_t * p_metadata)
{
nrf_802154_log_function_enter(NRF_802154_LOG_VERBOSITY_LOW);
Expand All @@ -345,11 +380,13 @@ void nrf_802154_csma_ca_start(uint8_t * p_d
m_data_props = p_metadata->frame_props;
m_nb = 0;
m_be = nrf_802154_pib_csmaca_min_be_get();
m_is_running = true;
set_procedure_state(PROCEDURE_RUNNING);

random_backoff_start();

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_LOW);

return true;
}

bool nrf_802154_csma_ca_abort(nrf_802154_term_t term_lvl, req_originator_t req_orig)
Expand All @@ -366,8 +403,11 @@ bool nrf_802154_csma_ca_abort(nrf_802154_term_t term_lvl, req_originator_t req_o

if (term_lvl >= NRF_802154_TERM_802154)
{
// Stop CSMA-CA if termination level is high enough.
procedure_stop();
// Abort CSMA-CA if termination level is high enough.
if (procedure_is_running())
{
set_procedure_state(PROCEDURE_ABORTING);
}
}
else
{
Expand All @@ -388,17 +428,35 @@ bool nrf_802154_csma_ca_tx_failed_hook(uint8_t * p_frame, nrf_802154_tx_error_t

switch (error)
{
/* Below errors mean a failure occurred during the frame processing and the frame cannot be
* transmitted unless a higher layer takes appropriate actions, hence the CSMA-CA procedure
* shall be stopped.
*/
// Below errors mean a failure occurred during the frame processing and the frame cannot be
// transmitted unless a higher layer takes appropriate actions, hence the CSMA-CA procedure
// shall be stopped.

case NRF_802154_TX_ERROR_KEY_ID_INVALID:
/* Fallthrough. */
case NRF_802154_TX_ERROR_FRAME_COUNTER_ERROR:
procedure_stop();
set_procedure_state(PROCEDURE_STOPPED);
result = true;
break;

case NRF_802154_TX_ERROR_ABORTED:
if (PROCEDURE_ABORTING == m_proc_state)
{
// If CSMA-CA was aborted when another (for example a delayed TX) operation was holding
// the mp_tx_data in nrf_802154_core.c then the higher layer would only be notified
// about the failure of this other operation, not CSMA-CA, so we should notify here
// Otherwise core will notify
set_procedure_state(PROCEDURE_STOPPED);

if (p_frame != mp_data)
{
notify_failed(NRF_802154_TX_ERROR_ABORTED);
}

break;
}

// else fallthrough
default:
if (p_frame == mp_data)
{
Expand All @@ -417,7 +475,7 @@ bool nrf_802154_csma_ca_tx_started_hook(uint8_t * p_frame)
{
nrf_802154_log_function_enter(NRF_802154_LOG_VERBOSITY_LOW);

procedure_stop();
set_procedure_state(PROCEDURE_STOPPED);

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_LOW);
}
Expand Down
4 changes: 3 additions & 1 deletion nrf_802154/driver/src/mac_features/nrf_802154_csma_ca.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@
* to be transmitted.
* @param[in] p_metadata Pointer to metadata structure. Contains detailed properties of data
* to transmit.
*
* @retval true The function always returns true for compatibility reasons
*/
void nrf_802154_csma_ca_start(uint8_t * p_data,
bool nrf_802154_csma_ca_start(uint8_t * p_data,
const nrf_802154_transmit_csma_ca_metadata_t * p_metadata);

/**
Expand Down
5 changes: 2 additions & 3 deletions nrf_802154/driver/src/nrf_802154.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#include "timer/nrf_802154_timer_sched.h"

#include "mac_features/nrf_802154_ack_timeout.h"
#include "mac_features/nrf_802154_csma_ca.h"
#include "mac_features/nrf_802154_delayed_trx.h"
#include "mac_features/nrf_802154_ie_writer.h"
#include "mac_features/nrf_802154_security_pib.h"
Expand Down Expand Up @@ -867,7 +866,7 @@ bool nrf_802154_transmit_csma_ca_raw(uint8_t
result = are_frame_properties_valid(&p_metadata->frame_props);
if (result)
{
nrf_802154_csma_ca_start(p_data, p_metadata);
nrf_802154_request_csma_ca_start(p_data, p_metadata);
}

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_LOW);
Expand Down Expand Up @@ -898,7 +897,7 @@ bool nrf_802154_transmit_csma_ca(const uint8_t *
if (result)
{
tx_buffer_fill(p_data, length);
nrf_802154_csma_ca_start(m_tx_buffer, p_metadata);
nrf_802154_request_csma_ca_start(m_tx_buffer, p_metadata);
}

nrf_802154_log_function_exit(NRF_802154_LOG_VERBOSITY_LOW);
Expand Down
7 changes: 5 additions & 2 deletions nrf_802154/driver/src/nrf_802154_encrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,6 @@ bool nrf_802154_encrypt_ack_prepare(const nrf_802154_frame_parser_data_t * p_ack
nrf_802154_aes_ccm_data_t aes_ccm_data;
bool success = false;

nrf_802154_aes_ccm_transform_reset();

if (!nrf_802154_frame_parser_security_enabled_bit_is_set(p_ack_data))
{
success = true;
Expand All @@ -320,6 +318,11 @@ bool nrf_802154_encrypt_ack_prepare(const nrf_802154_frame_parser_data_t * p_ack
return success;
}

void nrf_802154_encrypt_ack_reset(void)
{
nrf_802154_aes_ccm_transform_reset();
}

bool nrf_802154_encrypt_tx_setup(
uint8_t * p_frame,
nrf_802154_transmit_params_t * p_params,
Expand Down
5 changes: 5 additions & 0 deletions nrf_802154/driver/src/nrf_802154_encrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ extern "C" {
*/
bool nrf_802154_encrypt_ack_prepare(const nrf_802154_frame_parser_data_t * p_ack_data);

/**
* @brief Resets encryption of ACK
*/
void nrf_802154_encrypt_ack_reset(void);

/**
* @brief Transmission setup hook for the encryption module.
*
Expand Down
11 changes: 11 additions & 0 deletions nrf_802154/driver/src/nrf_802154_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,17 @@ bool nrf_802154_request_receive_at_cancel(uint32_t id);

#endif // NRF_802154_DELAYED_TRX_ENABLED

/**
* @brief Requests CSMA-CA procedure for the transmission of a given frame.
*
* @param[in] p_data Pointer to a buffer the contains PHR and PSDU of the frame that is
* to be transmitted.
* @param[in] p_metadata Pointer to metadata structure. Contains detailed properties of data
* to transmit.
*/
bool nrf_802154_request_csma_ca_start(uint8_t * p_data,
const nrf_802154_transmit_csma_ca_metadata_t * p_metadata);

/**
*@}
**/
Expand Down
7 changes: 7 additions & 0 deletions nrf_802154/driver/src/nrf_802154_request_direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include "nrf_802154_core.h"
#include "mac_features/nrf_802154_delayed_trx.h"
#include "mac_features/nrf_802154_csma_ca.h"
#include "hal/nrf_radio.h"

#define REQUEST_FUNCTION_PARMS(func_core, ...) \
Expand Down Expand Up @@ -188,3 +189,9 @@ bool nrf_802154_request_receive_at_cancel(uint32_t id)
}

#endif

bool nrf_802154_request_csma_ca_start(uint8_t * p_data,
const nrf_802154_transmit_csma_ca_metadata_t * p_metadata)
{
REQUEST_FUNCTION(nrf_802154_csma_ca_start, p_data, p_metadata);
}
Loading

0 comments on commit 92748b2

Please sign in to comment.