§ Coverage
1/*2 * sc_ratelimit.c -- implementation. Blocks are tagged with the low-level3 * requirement they satisfy; see requirements/sc_ratelimit.md.4 */5#include "sc_ratelimit.h"6#include "sc_sat.h"785sc_status_t sc_ratelimit_config_valid(const sc_ratelimit_config_t *cfg)9{10 sc_status_t status;11125 if (cfg == NULL)condition outcomes covered 2/213 {141 status = SC_ERR_NULL; /* LLR-RL-11 */15 }164 else if ((cfg->max_step_up < 0) || (cfg->max_step_down < 0))condition outcomes covered 4/417 {182 status = SC_ERR_PARAM; /* LLR-RL-12 */19 }202 else if (cfg->out_min > cfg->out_max)condition outcomes covered 2/221 {221 status = SC_ERR_PARAM; /* LLR-RL-13 */23 }24 else25 {261 status = SC_OK; /* LLR-RL-14 */27 }28295 return status;30}313214sc_status_t sc_ratelimit_init(sc_ratelimit_state_t *state, int32_t initial)33{34 sc_status_t status;353614 if (state == NULL)condition outcomes covered 2/237 {381 status = SC_ERR_NULL; /* LLR-RL-15 */39 }40 else41 {4213 state->output = initial; /* LLR-RL-16 */4313 status = SC_OK;44 }454614 return status;47}4849131int32_t sc_ratelimit_update(const sc_ratelimit_config_t *cfg,50 sc_ratelimit_state_t *state,51 int32_t target)52{53 int32_t result;5455131 if (state == NULL)condition outcomes covered 2/256 {571 result = 0; /* LLR-RL-6 */58 }59130 else if (cfg == NULL)condition outcomes covered 2/260 {611 result = state->output; /* LLR-RL-6 */62 }63 else64 {65129 int32_t delta = sc_sat_sub_i32(target, state->output); /* LLR-RL-1 */66 int32_t next;6768129 if (delta > cfg->max_step_up)condition outcomes covered 2/269 {7014 delta = cfg->max_step_up; /* LLR-RL-2 */71 }72115 else if (delta < -cfg->max_step_down)condition outcomes covered 2/273 {749 delta = -cfg->max_step_down; /* LLR-RL-3 */75 }76 else77 {78 /* LLR-RL-4: change is already within the slew limits */79 }8081129 next = sc_sat_add_i32(state->output, delta); /* LLR-RL-5 */8283129 if (next < cfg->out_min)condition outcomes covered 2/284 {855 next = cfg->out_min; /* LLR-RL-7 */86 }87124 else if (next > cfg->out_max)condition outcomes covered 2/288 {896 next = cfg->out_max; /* LLR-RL-8 */90 }91 else92 {93 /* LLR-RL-17: already within the clamp range */94 }9596129 state->output = next;97129 result = state->output; /* LLR-RL-9 */98 }99100131 return result;101}1021037int32_t sc_ratelimit_output(const sc_ratelimit_state_t *state)104{105 int32_t result;1061077 if (state == NULL)condition outcomes covered 2/2108 {1091 result = 0; /* LLR-RL-18 */110 }111 else112 {1136 result = state->output; /* LLR-RL-19 */114 }1151167 return result;117}