Skip to content

Commit

Permalink
tests: lib: lte_lc_api: Add coverage for edrx
Browse files Browse the repository at this point in the history
Adding test coverage for edrx related tests.
Also cleaning up dead code, i.e., conditions that cannot be met
have been changed from if-statements to asserts.

Signed-off-by: Tommi Rantanen <tommi.rantanen@nordicsemi.no>
  • Loading branch information
trantanen authored and tmon-nordic committed Aug 19, 2024
1 parent 3914dcb commit 29d06ab
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 39 deletions.
13 changes: 13 additions & 0 deletions lib/lte_link_control/lte_lc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,19 @@ int lte_lc_edrx_req(bool enable)
int err = 0;
int actt[] = {AT_CEDRXS_ACTT_WB, AT_CEDRXS_ACTT_NB};

LOG_DBG("enable=%d, "
"requested_edrx_value_ltem=%s, edrx_value_ltem=%s, "
"requested_ptw_value_ltem=%s, ptw_value_ltem=%s, ",
enable,
requested_edrx_value_ltem, edrx_value_ltem,
requested_ptw_value_ltem, ptw_value_ltem);
LOG_DBG("enable=%d, "
"requested_edrx_value_nbiot=%s, edrx_value_nbiot=%s, "
"requested_ptw_value_nbiot=%s, ptw_value_nbiot=%s",
enable,
requested_edrx_value_nbiot, edrx_value_nbiot,
requested_ptw_value_nbiot, ptw_value_nbiot);

requested_edrx_enable = enable;

if (!enable) {
Expand Down
51 changes: 14 additions & 37 deletions lib/lte_link_control/lte_lc_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ static void get_ptw_multiplier(enum lte_lc_lte_mode lte_mode, float *ptw_multipl
if (lte_mode == LTE_LC_LTE_MODE_NBIOT) {
*ptw_multiplier = 2.56;
} else {
__ASSERT_NO_MSG(lte_mode == LTE_LC_LTE_MODE_LTEM);
*ptw_multiplier = 1.28;
}
}

static int get_edrx_value(enum lte_lc_lte_mode lte_mode, uint8_t idx, float *edrx_value)
static void get_edrx_value(enum lte_lc_lte_mode lte_mode, uint8_t idx, float *edrx_value)
{
uint16_t multiplier = 0;

Expand All @@ -186,25 +187,17 @@ static int get_edrx_value(enum lte_lc_lte_mode lte_mode, uint8_t idx, float *edr
};

__ASSERT_NO_MSG(edrx_value != NULL);
/* idx is parsed from 4 character bit field string so it cannot be more than 15 */
__ASSERT_NO_MSG(idx < ARRAY_SIZE(edrx_lookup_ltem));

if (idx > ARRAY_SIZE(edrx_lookup_ltem) - 1) {
return -EINVAL;
}

switch (lte_mode) {
case LTE_LC_LTE_MODE_LTEM:
if (lte_mode == LTE_LC_LTE_MODE_LTEM) {
multiplier = edrx_lookup_ltem[idx];
break;
case LTE_LC_LTE_MODE_NBIOT:
} else {
__ASSERT_NO_MSG(lte_mode == LTE_LC_LTE_MODE_NBIOT);
multiplier = edrx_lookup_nbiot[idx];
break;
default:
return -ENOTCONN;
}

*edrx_value = multiplier == 0 ? 5.12 : multiplier * 10.24;

return 0;
}

/* Counts the frequency of a character in a null-terminated string. */
Expand Down Expand Up @@ -327,11 +320,7 @@ int parse_edrx(const char *at_response, struct lte_lc_edrx_cfg *cfg, char *edrx_
*/
get_ptw_multiplier(cfg->mode, &ptw_multiplier);

err = get_edrx_value(cfg->mode, idx, &cfg->edrx);
if (err) {
LOG_ERR("Failed to get eDRX value, error; %d", err);
goto clean_exit;
}
get_edrx_value(cfg->mode, idx, &cfg->edrx);

len = sizeof(tmp_buf);

Expand All @@ -342,18 +331,15 @@ int parse_edrx(const char *at_response, struct lte_lc_edrx_cfg *cfg, char *edrx_
goto clean_exit;
}

__ASSERT_NO_MSG(ptw_str != NULL);
strcpy(ptw_str, tmp_buf);

/* Value can be a maximum of 15, as there are 16 entries in the table
* for paging time window (both for LTE-M and NB1).
* We can use assert as only 4 bits can be received and if there would be more,
* the previous at_parser_string_get would fail.
*/
idx = strtoul(tmp_buf, NULL, 2);
if (idx > 15) {
LOG_ERR("Invalid PTW lookup index: %d", idx);
err = -EINVAL;
goto clean_exit;
}
__ASSERT_NO_MSG(idx <= 15);

/* The Paging Time Window is different for LTE-M and NB-IoT:
* - LTE-M: (idx + 1) * 1.28 s
Expand Down Expand Up @@ -538,10 +524,7 @@ int parse_psm(const char *active_time_str, const char *tau_ext_str,
memcpy(unit_str, tau_ext_str, unit_str_len);

lut_idx = strtoul(unit_str, NULL, 2);
if (lut_idx > (ARRAY_SIZE(t3412_ext_lookup) - 1)) {
LOG_ERR("Unable to parse periodic TAU string (T3412 extended)");
return -EINVAL;
}
__ASSERT_NO_MSG(lut_idx < ARRAY_SIZE(t3412_ext_lookup));

timer_unit = t3412_ext_lookup[lut_idx];
timer_value = strtoul(tau_ext_str + unit_str_len, NULL, 2);
Expand All @@ -554,10 +537,7 @@ int parse_psm(const char *active_time_str, const char *tau_ext_str,
memcpy(unit_str, tau_legacy_str, unit_str_len);

lut_idx = strtoul(unit_str, NULL, 2);
if (lut_idx > (ARRAY_SIZE(t3412_lookup) - 1)) {
LOG_ERR("Unable to parse periodic TAU string (T3412)");
return -EINVAL;
}
__ASSERT_NO_MSG(lut_idx < ARRAY_SIZE(t3412_lookup));

timer_unit = t3412_lookup[lut_idx];
if (timer_unit == 0) {
Expand All @@ -575,10 +555,7 @@ int parse_psm(const char *active_time_str, const char *tau_ext_str,
memcpy(unit_str, active_time_str, unit_str_len);

lut_idx = strtoul(unit_str, NULL, 2);
if (lut_idx > (ARRAY_SIZE(t3324_lookup) - 1)) {
LOG_ERR("Unable to parse active time string");
return -EINVAL;
}
__ASSERT_NO_MSG(lut_idx < ARRAY_SIZE(t3324_lookup));

timer_unit = t3324_lookup[lut_idx];
timer_value = strtoul(active_time_str + unit_str_len, NULL, 2);
Expand Down
71 changes: 69 additions & 2 deletions tests/lib/lte_lc_api/src/lte_lc_api_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1305,7 +1305,7 @@ void test_lte_lc_edrx_get_ltem2_success(void)
{
int ret;
struct lte_lc_edrx_cfg edrx_cfg;
static const char cedrxrdp_resp[] = "+CEDRXRDP: 4,\"1000\",\"0010\",\"1110\"";
static const char cedrxrdp_resp[] = "+CEDRXRDP: 4,\"1000\",\"0000\",\"1110\"";

__cmock_nrf_modem_at_cmd_ExpectAndReturn(NULL, 0, "AT+CEDRXRDP", 0);
__cmock_nrf_modem_at_cmd_IgnoreArg_buf();
Expand All @@ -1316,7 +1316,7 @@ void test_lte_lc_edrx_get_ltem2_success(void)
ret = lte_lc_edrx_get(&edrx_cfg);
TEST_ASSERT_EQUAL(EXIT_SUCCESS, ret);
TEST_ASSERT_EQUAL(LTE_LC_LTE_MODE_LTEM, edrx_cfg.mode);
TEST_ASSERT_EQUAL_FLOAT(20.48, edrx_cfg.edrx);
TEST_ASSERT_EQUAL_FLOAT(5.12, edrx_cfg.edrx);
TEST_ASSERT_EQUAL_FLOAT(19.2, edrx_cfg.ptw);
}

Expand Down Expand Up @@ -1374,6 +1374,73 @@ void test_lte_lc_edrx_get_invalid_mode_fail(void)
TEST_ASSERT_EQUAL(-EBADMSG, ret);
}

void test_lte_lc_edrx_get_invalid_nw_edrx_value_fail(void)
{
int ret;
struct lte_lc_edrx_cfg edrx_cfg;
static const char cedrxrdp_resp[] = "+CEDRXRDP: 4,\"1000\",0101,\"1011\"";

__cmock_nrf_modem_at_cmd_ExpectAndReturn(NULL, 0, "AT+CEDRXRDP", 0);
__cmock_nrf_modem_at_cmd_IgnoreArg_buf();
__cmock_nrf_modem_at_cmd_IgnoreArg_len();
__cmock_nrf_modem_at_cmd_ReturnArrayThruPtr_buf(
(char *)cedrxrdp_resp, sizeof(cedrxrdp_resp));

ret = lte_lc_edrx_get(&edrx_cfg);
TEST_ASSERT_EQUAL(-EBADMSG, ret);
}

void test_lte_lc_edrx_get_invalid_nw_edrx_value_empty(void)
{
int ret;
struct lte_lc_edrx_cfg edrx_cfg;
static const char cedrxrdp_resp[] = "+CEDRXRDP: 4,\"1000\",\"\",\"1011\"";

__cmock_nrf_modem_at_cmd_ExpectAndReturn(NULL, 0, "AT+CEDRXRDP", 0);
__cmock_nrf_modem_at_cmd_IgnoreArg_buf();
__cmock_nrf_modem_at_cmd_IgnoreArg_len();
__cmock_nrf_modem_at_cmd_ReturnArrayThruPtr_buf(
(char *)cedrxrdp_resp, sizeof(cedrxrdp_resp));

ret = lte_lc_edrx_get(&edrx_cfg);
TEST_ASSERT_EQUAL(0, ret);
TEST_ASSERT_EQUAL(LTE_LC_LTE_MODE_NONE, edrx_cfg.mode);
TEST_ASSERT_EQUAL_FLOAT(0, edrx_cfg.edrx);
TEST_ASSERT_EQUAL_FLOAT(0, edrx_cfg.ptw);
}

void test_lte_lc_edrx_get_missing_nw_edrx_value(void)
{
int ret;
struct lte_lc_edrx_cfg edrx_cfg;
static const char cedrxrdp_resp[] = "+CEDRXRDP: 4,\"1000\"";

__cmock_nrf_modem_at_cmd_ExpectAndReturn(NULL, 0, "AT+CEDRXRDP", 0);
__cmock_nrf_modem_at_cmd_IgnoreArg_buf();
__cmock_nrf_modem_at_cmd_IgnoreArg_len();
__cmock_nrf_modem_at_cmd_ReturnArrayThruPtr_buf(
(char *)cedrxrdp_resp, sizeof(cedrxrdp_resp));

ret = lte_lc_edrx_get(&edrx_cfg);
TEST_ASSERT_EQUAL(-EBADMSG, ret);
}

void test_lte_lc_edrx_get_missing_ptw_value_fail(void)
{
int ret;
struct lte_lc_edrx_cfg edrx_cfg;
static const char cedrxrdp_resp[] = "+CEDRXRDP: 4,\"1000\",\"0101\"";

__cmock_nrf_modem_at_cmd_ExpectAndReturn(NULL, 0, "AT+CEDRXRDP", 0);
__cmock_nrf_modem_at_cmd_IgnoreArg_buf();
__cmock_nrf_modem_at_cmd_IgnoreArg_len();
__cmock_nrf_modem_at_cmd_ReturnArrayThruPtr_buf(
(char *)cedrxrdp_resp, sizeof(cedrxrdp_resp));

ret = lte_lc_edrx_get(&edrx_cfg);
TEST_ASSERT_EQUAL(-EBADMSG, ret);
}

void test_lte_lc_edrx_get_null_fail(void)
{
int ret;
Expand Down

0 comments on commit 29d06ab

Please sign in to comment.