|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <unity.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <string.h> |
| 10 | +#include <math.h> |
| 11 | + |
| 12 | +#include <bluetooth/cs_de.h> |
| 13 | + |
| 14 | +#define NUM_CHANNELS (75) |
| 15 | +#define CHANNEL_SPACING_HZ (1e6f) |
| 16 | +#define PI (3.14159265358979f) |
| 17 | +#define SPEED_OF_LIGHT_M_PER_S (299792458.0f) |
| 18 | + |
| 19 | +/* The unity_main is not declared in any header file. It is only defined in the generated test |
| 20 | + * runner because of ncs' unity configuration. It is therefore declared here to avoid a compiler |
| 21 | + * warning. |
| 22 | + */ |
| 23 | +extern int unity_main(void); |
| 24 | + |
| 25 | +/* Generate ideal IQ data for a given distance in meters.*/ |
| 26 | +static void generate_ideal_iq_data(float distance, cs_de_iq_tones_t *iq_tones) |
| 27 | +{ |
| 28 | + float rotation_per_channel = |
| 29 | + 2 * PI * CHANNEL_SPACING_HZ * distance / SPEED_OF_LIGHT_M_PER_S; |
| 30 | + for (int i = 0; i < NUM_CHANNELS; i++) { |
| 31 | + iq_tones->i_local[i] = 100 * cosf(-rotation_per_channel * i); |
| 32 | + iq_tones->q_local[i] = 100 * sinf(-rotation_per_channel * i); |
| 33 | + iq_tones->i_remote[i] = 100 * cosf(-rotation_per_channel * i); |
| 34 | + iq_tones->q_remote[i] = 100 * sinf(-rotation_per_channel * i); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +void test_cs_de_calc_empty_report(void) |
| 39 | +{ |
| 40 | + cs_de_report_t test_report; |
| 41 | + cs_de_quality_t result; |
| 42 | + |
| 43 | + /* Setup: Initialize report with no antenna paths */ |
| 44 | + test_report.n_ap = 0; |
| 45 | + |
| 46 | + /* Execute */ |
| 47 | + result = cs_de_calc(&test_report); |
| 48 | + |
| 49 | + /* Verify: Should return DO_NOT_USE quality */ |
| 50 | + TEST_ASSERT_EQUAL(CS_DE_QUALITY_DO_NOT_USE, result); |
| 51 | +} |
| 52 | + |
| 53 | +void test_cs_de_calc_bad_tone_quality(void) |
| 54 | +{ |
| 55 | + cs_de_report_t test_report; |
| 56 | + cs_de_quality_t result; |
| 57 | + /* Setup: Initialize report with 1 antenna path but bad tone quality */ |
| 58 | + test_report.n_ap = 1; |
| 59 | + test_report.tone_quality[0] = CS_DE_TONE_QUALITY_BAD; |
| 60 | + |
| 61 | + /* Execute */ |
| 62 | + result = cs_de_calc(&test_report); |
| 63 | + |
| 64 | + /* Verify: Should return DO_NOT_USE quality due to bad tone quality */ |
| 65 | + TEST_ASSERT_EQUAL(CS_DE_QUALITY_DO_NOT_USE, result); |
| 66 | +} |
| 67 | + |
| 68 | +void test_cs_de_calc_with_ideal_iq_data(void) |
| 69 | +{ |
| 70 | + for (uint8_t n_ap = 1; n_ap <= CONFIG_BT_RAS_MAX_ANTENNA_PATHS; n_ap++) { |
| 71 | + for (float distance = 0.0f; distance < 74.5f; distance += 0.1f) { |
| 72 | + cs_de_report_t test_report; |
| 73 | + cs_de_quality_t result; |
| 74 | + |
| 75 | + test_report.n_ap = n_ap; |
| 76 | + |
| 77 | + for (uint8_t ap = 0; ap < n_ap; ap++) { |
| 78 | + test_report.tone_quality[ap] = CS_DE_TONE_QUALITY_OK; |
| 79 | + generate_ideal_iq_data(distance, &test_report.iq_tones[ap]); |
| 80 | + } |
| 81 | + |
| 82 | + result = cs_de_calc(&test_report); |
| 83 | + TEST_ASSERT_TRUE(result == CS_DE_QUALITY_OK); |
| 84 | + |
| 85 | + for (uint8_t ap = 0; ap < n_ap; ap++) { |
| 86 | + /* Verify that the estimated distance is within 1 cm of the distance |
| 87 | + * used to generate the ideal IQ data. |
| 88 | + */ |
| 89 | + TEST_ASSERT_FLOAT_WITHIN(0.01f, |
| 90 | + distance, |
| 91 | + test_report.distance_estimates[ap].ifft); |
| 92 | + TEST_ASSERT_FLOAT_WITHIN(0.01f, |
| 93 | + distance, |
| 94 | + test_report.distance_estimates[ap].phase_slope); |
| 95 | + TEST_ASSERT_EQUAL_FLOAT(test_report.distance_estimates[ap].ifft, |
| 96 | + test_report.distance_estimates[ap].best); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/* Main test entry point */ |
| 103 | +int main(void) |
| 104 | +{ |
| 105 | + (void)unity_main(); |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
0 commit comments