<commit_before>/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/types.h>
#include <sys/util.h>

#include "util/mem.h"
#include "util/memq.h"
#include "util/mayfly.h"

#include "hal/ticker.h"

#include "ticker/ticker.h"

#include "pdu.h"

#include "lll.h"
#include "lll_vendor.h"
#include "lll_scan.h"
#include "lll_scan_aux.h"

#include "ull_scan_types.h"

#include "ull_internal.h"
#include "ull_scan_internal.h"

#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER)
#define LOG_MODULE_NAME bt_ctlr_ull_scan_aux
#include "common/log.h"
#include <soc.h>
#include "hal/debug.h"

static int init_reset(void);
static inline struct ll_scan_aux_set *aux_acquire(void);
static inline void aux_release(struct ll_scan_aux_set *aux);
static inline uint8_t aux_handle_get(struct ll_scan_aux_set *aux);
static void flush(struct ll_scan_aux_set *aux, struct node_rx_hdr *rx);
static void ticker_cb(uint32_t ticks_at_expire, uint32_t remainder,
		      uint16_t lazy, void *param);
static void ticker_op_cb(uint32_t status, void *param);
static void ticker_op_aux_failure(void *param);

static struct ll_scan_aux_set ll_scan_aux_pool[CONFIG_BT_CTLR_SCAN_AUX_SET];
static void *scan_aux_free;

int ull_scan_aux_init(void)
{
	int err;

	err = init_reset();
	if (err) {
		return err;
	}

	return 0;
}

int ull_scan_aux_reset(void)
{
	int err;

	err = init_reset();
	if (err) {
		return err;
	}

	return 0;
}

void ull_scan_aux_setup(memq_link_t *link, struct node_rx_hdr *rx, uint8_t phy)
{
	struct ext_adv_aux_ptr *aux_ptr;
	struct pdu_adv_com_ext_adv *p;
	struct ll_scan_aux_set *aux;
	uint32_t ticks_slot_overhead;
	uint32_t window_widening_us;
	struct lll_scan_aux *lll;
	struct node_rx_ftr *ftr;
	uint32_t ticks_slot_offset;
	struct ext_adv_hdr *h;
	uint32_t ready_delay_us;
	struct pdu_adv *pdu;
	uint32_t aux_offset_us;
	uint32_t ticker_status;
	uint8_t aux_handle;
	uint8_t *ptr;

	ftr = &rx->rx_ftr;
	lll = ftr->param;
	aux = lll->hdr.parent;
	if (((uint8_t *)aux < (uint8_t *)ll_scan_aux_pool) ||
	    ((uint8_t *)aux > ((uint8_t *)ll_scan_aux_pool +
			    (sizeof(struct ll_scan_aux_set) *
			     (CONFIG_BT_CTLR_SCAN_AUX_SET - 1))))) {
		aux = NULL;
	}

	rx->link = link;
	ftr->extra = NULL;

	pdu = (void *)((struct node_rx_pdu *)rx)->pdu;
	p = (void *)&pdu->adv_ext_ind;
	if (!p->ext_hdr_len) {
		goto ull_scan_aux_rx_flush;
	}

	h = (void *)p->ext_hdr_adi_adv_data;
	if (!h->aux_ptr) {
		goto ull_scan_aux_rx_flush;
	}

	ptr = (uint8_t *)h + sizeof(*h);

	if (h->adv_addr) {
		ptr += BDADDR_SIZE;
	}

	if (h->tgt_addr) {
		ptr += BDADDR_SIZE;
	}

	if (h->adi) {
		struct ext_adv_adi *adi;

		adi = (void *)ptr;
		ptr += sizeof(*adi);
	}

	aux_ptr = (void *)ptr;
	if (!aux_ptr->offs) {
		goto ull_scan_aux_rx_flush;
	}

	if (!aux) {
		aux = aux_acquire();
		if (!aux) {
			goto ull_scan_aux_rx_flush;
		}

		aux->rx_last = NULL;
		lll = &aux->lll;

		ull_hdr_init(&aux->ull);
		lll_hdr_init(lll, aux);
	} else {
		LL_ASSERT(0);
	}

	/* Enqueue the rx in aux context */
	if (aux->rx_last) {
		ftr = &aux->rx_last->rx_ftr;
		ftr->extra = rx;
	} else {
		aux->rx_head = rx;
	}
	aux->rx_last = rx;

	lll->chan = aux_ptr->chan_idx;
	lll->phy = BIT(aux_ptr->phy);

	aux_offset_us = ftr->radio_end_us - PKT_AC_US(pdu->len, 0, phy);
	if (aux_ptr->offs_units) {
		lll->window_size_us = 300U;
	} else {
		lll->window_size_us = 30U;
	}
	aux_offset_us += (uint32_t)aux_ptr->offs * lll->window_size_us;

	if (aux_ptr->ca) {
		window_widening_us = aux_offset_us / 2000U;
	} else {
		window_widening_us = aux_offset_us / 20000U;
	}

	lll->window_size_us += (EVENT_TICKER_RES_MARGIN_US +
				(EVENT_JITTER_US << 1) +
				window_widening_us);

	ready_delay_us = lll_radio_rx_ready_delay_get(lll->phy, 1);

	aux_offset_us -= EVENT_OVERHEAD_START_US;
	aux_offset_us -= EVENT_JITTER_US;
	aux_offset_us -= ready_delay_us;
	aux_offset_us -= window_widening_us;<commit_msg>Bluetooth: controller: split: Fix auxiliary channel scan window width<commit_after>/*
 * Copyright (c) 2020 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/types.h>
#include <sys/util.h>

#include "util/mem.h"
#include "util/memq.h"
#include "util/mayfly.h"

#include "hal/ticker.h"

#include "ticker/ticker.h"

#include "pdu.h"

#include "lll.h"
#include "lll_vendor.h"
#include "lll_scan.h"
#include "lll_scan_aux.h"

#include "ull_scan_types.h"

#include "ull_internal.h"
#include "ull_scan_internal.h"

#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_HCI_DRIVER)
#define LOG_MODULE_NAME bt_ctlr_ull_scan_aux
#include "common/log.h"
#include <soc.h>
#include "hal/debug.h"

static int init_reset(void);
static inline struct ll_scan_aux_set *aux_acquire(void);
static inline void aux_release(struct ll_scan_aux_set *aux);
static inline uint8_t aux_handle_get(struct ll_scan_aux_set *aux);
static void flush(struct ll_scan_aux_set *aux, struct node_rx_hdr *rx);
static void ticker_cb(uint32_t ticks_at_expire, uint32_t remainder,
		      uint16_t lazy, void *param);
static void ticker_op_cb(uint32_t status, void *param);
static void ticker_op_aux_failure(void *param);

static struct ll_scan_aux_set ll_scan_aux_pool[CONFIG_BT_CTLR_SCAN_AUX_SET];
static void *scan_aux_free;

int ull_scan_aux_init(void)
{
	int err;

	err = init_reset();
	if (err) {
		return err;
	}

	return 0;
}

int ull_scan_aux_reset(void)
{
	int err;

	err = init_reset();
	if (err) {
		return err;
	}

	return 0;
}

void ull_scan_aux_setup(memq_link_t *link, struct node_rx_hdr *rx, uint8_t phy)
{
	struct ext_adv_aux_ptr *aux_ptr;
	struct pdu_adv_com_ext_adv *p;
	struct ll_scan_aux_set *aux;
	uint32_t ticks_slot_overhead;
	uint32_t window_widening_us;
	struct lll_scan_aux *lll;
	struct node_rx_ftr *ftr;
	uint32_t ticks_slot_offset;
	struct ext_adv_hdr *h;
	uint32_t ready_delay_us;
	struct pdu_adv *pdu;
	uint32_t aux_offset_us;
	uint32_t ticker_status;
	uint8_t aux_handle;
	uint8_t *ptr;

	ftr = &rx->rx_ftr;
	lll = ftr->param;
	aux = lll->hdr.parent;
	if (((uint8_t *)aux < (uint8_t *)ll_scan_aux_pool) ||
	    ((uint8_t *)aux > ((uint8_t *)ll_scan_aux_pool +
			    (sizeof(struct ll_scan_aux_set) *
			     (CONFIG_BT_CTLR_SCAN_AUX_SET - 1))))) {
		aux = NULL;
	}

	rx->link = link;
	ftr->extra = NULL;

	pdu = (void *)((struct node_rx_pdu *)rx)->pdu;
	p = (void *)&pdu->adv_ext_ind;
	if (!p->ext_hdr_len) {
		goto ull_scan_aux_rx_flush;
	}

	h = (void *)p->ext_hdr_adi_adv_data;
	if (!h->aux_ptr) {
		goto ull_scan_aux_rx_flush;
	}

	ptr = (uint8_t *)h + sizeof(*h);

	if (h->adv_addr) {
		ptr += BDADDR_SIZE;
	}

	if (h->tgt_addr) {
		ptr += BDADDR_SIZE;
	}

	if (h->adi) {
		struct ext_adv_adi *adi;

		adi = (void *)ptr;
		ptr += sizeof(*adi);
	}

	aux_ptr = (void *)ptr;
	if (!aux_ptr->offs) {
		goto ull_scan_aux_rx_flush;
	}

	if (!aux) {
		aux = aux_acquire();
		if (!aux) {
			goto ull_scan_aux_rx_flush;
		}

		aux->rx_last = NULL;
		lll = &aux->lll;

		ull_hdr_init(&aux->ull);
		lll_hdr_init(lll, aux);
	} else {
		LL_ASSERT(0);
	}

	/* Enqueue the rx in aux context */
	if (aux->rx_last) {
		ftr = &aux->rx_last->rx_ftr;
		ftr->extra = rx;
	} else {
		aux->rx_head = rx;
	}
	aux->rx_last = rx;

	lll->chan = aux_ptr->chan_idx;
	lll->phy = BIT(aux_ptr->phy);

	aux_offset_us = ftr->radio_end_us - PKT_AC_US(pdu->len, 0, phy);
	if (aux_ptr->offs_units) {
		lll->window_size_us = 300U;
	} else {
		lll->window_size_us = 30U;
	}
	aux_offset_us += (uint32_t)aux_ptr->offs * lll->window_size_us;

	if (aux_ptr->ca) {
		window_widening_us = aux_offset_us / 2000U;
	} else {
		window_widening_us = aux_offset_us / 20000U;
	}

	lll->window_size_us += (EVENT_TICKER_RES_MARGIN_US +
				((EVENT_JITTER_US + window_widening_us) << 1));

	ready_delay_us = lll_radio_rx_ready_delay_get(lll->phy, 1);

	aux_offset_us -= EVENT_OVERHEAD_START_US;
	aux_offset_us -= EVENT_JITTER_US;
	aux_offset_us -= ready_delay_us;
	aux_offset_us -= window_widening_us;
{'fraction_non_alphanumeric': 0.13528220703619337, 'fraction_numerical': 0.014679827891672994, 'mean_word_length': 4.538192011212334, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4222855', 'n_tokens_mistral': 3707, 'n_tokens_neox': 3497, 'n_words': 659}