<commit_before>
#include <stdint.h>
#include <errno.h>

#include <osmocom/gsm/gsm_utils.h>

#include <osmo-bts/gsm_data.h>
#include <osmo-bts/logging.h>
#include <osmo-bts/measurement.h>

/* TS 05.08, Chapter 8.4.1 */
/* measurement period ends at fn % 104 == ? */
static const uint8_t tchf_meas_rep_fn104[] = {
	[0] =	103,
	[1] =	12,
	[2] =	25,
	[3] =	38,
	[4] =	51,
	[5] =	64,
	[6] =	77,
	[7] =	90,
};
static const uint8_t tchh0_meas_rep_fn104[] = {
	[0] =	103,
	[1] =	103,
	[2] =	25,
	[3] =	25,
	[4] =	51,
	[5] =	51,
	[6] =	77,
	[7] =	77,
};
static const uint8_t tchh1_meas_rep_fn104[] = {
	[0] =	12,
	[1] =	12,
	[2] =	38,
	[3] =	38,
	[4] =	64,
	[5] =	64,
	[6] =	90,
	[7] =	90,
};

/* determine if a measurement period ends at the given frame number */
static int is_meas_complete(enum gsm_phys_chan_config pchan, unsigned int ts,
			    unsigned int subch, uint32_t fn)
{
	unsigned int fn_mod;
	const uint8_t *tbl;
	int rc = 0;

	if (ts >= 8)
		return -EINVAL;
	if (pchan >= _GSM_PCHAN_MAX)
		return -EINVAL;

	switch (pchan) {
	case GSM_PCHAN_TCH_F:
		fn_mod = fn % 104;
		if (tchf_meas_rep_fn104[ts] == fn_mod)
			rc = 1;
		break;
	case GSM_PCHAN_TCH_H:
		fn_mod = fn % 104;
		if (subch == 0)	
			tbl = tchh0_meas_rep_fn104;
		else
			tbl = tchh1_meas_rep_fn104;
		if (tbl[ts] == fn_mod)
			rc = 1;
		break;
	case GSM_PCHAN_SDCCH8_SACCH8C:
		fn_mod = fn % 102;
		if (fn_mod == 11)
			rc = 1;
		break;
	case GSM_PCHAN_CCCH_SDCCH4:
		fn_mod = fn % 102;
		if (fn_mod == 36)
			rc = 1;
		break;
	default:
		rc = 0;
		break;
	}
	return rc;
}

/* receive a L1 uplink measurement from L1 */
int lchan_new_ul_meas(struct gsm_lchan *lchan, struct bts_ul_meas *ulm)
{
	DEBUGP(DMEAS, "%s adding measurement, num_ul_meas=%d\n",
		gsm_lchan_name(lchan), lchan->meas.num_ul_meas);

	if (lchan->meas.num_ul_meas >= ARRAY_SIZE(lchan->meas.uplink)) {
		LOGP(DMEAS, LOGL_NOTICE, "%s no space for uplink measurement\n",
			gsm_lchan_name(lchan));
		return -ENOSPC;
	}

	memcpy(&lchan->meas.uplink[lchan->meas.num_ul_meas++], ulm,
		sizeof(*ulm));

	return 0;
}

/* input: BER in steps of .01%, i.e. percent/100 */
static uint8_t ber10k_to_rxqual(uint32_t ber10k)
{
	/* 05.08 / 8.2.4 */
	if (ber10k < 20)
		return 0;
	if (ber10k < 40)
		return 1;
	if (ber10k < 80)
		return 2;<commit_msg>measurement: Add debug helper when we have a report for an inactive channel<commit_after>
#include <stdint.h>
#include <errno.h>

#include <osmocom/gsm/gsm_utils.h>

#include <osmo-bts/gsm_data.h>
#include <osmo-bts/logging.h>
#include <osmo-bts/measurement.h>

/* TS 05.08, Chapter 8.4.1 */
/* measurement period ends at fn % 104 == ? */
static const uint8_t tchf_meas_rep_fn104[] = {
	[0] =	103,
	[1] =	12,
	[2] =	25,
	[3] =	38,
	[4] =	51,
	[5] =	64,
	[6] =	77,
	[7] =	90,
};
static const uint8_t tchh0_meas_rep_fn104[] = {
	[0] =	103,
	[1] =	103,
	[2] =	25,
	[3] =	25,
	[4] =	51,
	[5] =	51,
	[6] =	77,
	[7] =	77,
};
static const uint8_t tchh1_meas_rep_fn104[] = {
	[0] =	12,
	[1] =	12,
	[2] =	38,
	[3] =	38,
	[4] =	64,
	[5] =	64,
	[6] =	90,
	[7] =	90,
};

/* determine if a measurement period ends at the given frame number */
static int is_meas_complete(enum gsm_phys_chan_config pchan, unsigned int ts,
			    unsigned int subch, uint32_t fn)
{
	unsigned int fn_mod;
	const uint8_t *tbl;
	int rc = 0;

	if (ts >= 8)
		return -EINVAL;
	if (pchan >= _GSM_PCHAN_MAX)
		return -EINVAL;

	switch (pchan) {
	case GSM_PCHAN_TCH_F:
		fn_mod = fn % 104;
		if (tchf_meas_rep_fn104[ts] == fn_mod)
			rc = 1;
		break;
	case GSM_PCHAN_TCH_H:
		fn_mod = fn % 104;
		if (subch == 0)	
			tbl = tchh0_meas_rep_fn104;
		else
			tbl = tchh1_meas_rep_fn104;
		if (tbl[ts] == fn_mod)
			rc = 1;
		break;
	case GSM_PCHAN_SDCCH8_SACCH8C:
		fn_mod = fn % 102;
		if (fn_mod == 11)
			rc = 1;
		break;
	case GSM_PCHAN_CCCH_SDCCH4:
		fn_mod = fn % 102;
		if (fn_mod == 36)
			rc = 1;
		break;
	default:
		rc = 0;
		break;
	}
	return rc;
}

/* receive a L1 uplink measurement from L1 */
int lchan_new_ul_meas(struct gsm_lchan *lchan, struct bts_ul_meas *ulm)
{
	DEBUGP(DMEAS, "%s adding measurement, num_ul_meas=%d\n",
		gsm_lchan_name(lchan), lchan->meas.num_ul_meas);

	if (lchan->state != LCHAN_S_ACTIVE) {
		LOGP(DMEAS, LOGL_NOTICE, "%s measurement during state: %s\n",
			gsm_lchan_name(lchan), gsm_lchans_name(lchan->state));
	}

	if (lchan->meas.num_ul_meas >= ARRAY_SIZE(lchan->meas.uplink)) {
		LOGP(DMEAS, LOGL_NOTICE, "%s no space for uplink measurement\n",
			gsm_lchan_name(lchan));
		return -ENOSPC;
	}

	memcpy(&lchan->meas.uplink[lchan->meas.num_ul_meas++], ulm,
		sizeof(*ulm));

	return 0;
}

/* input: BER in steps of .01%, i.e. percent/100 */
static uint8_t ber10k_to_rxqual(uint32_t ber10k)
{
	/* 05.08 / 8.2.4 */
	if (ber10k < 20)
		return 0;
	if (ber10k < 40)
		return 1;
	if (ber10k < 80)
		return 2;
{'fraction_non_alphanumeric': 0.16041884816753926, 'fraction_numerical': 0.07413612565445026, 'mean_word_length': 3.3696248856358646, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 33, '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': '7427474', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2334, 'n_words': 533}