kfsw-modules/gpredict/src/gpredict_profiles.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #include <zephyr/kernel.h> | ||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | |||
| 9 | #include <kfsw/services/log.h> | ||
| 10 | |||
| 11 | #include "gpredict_internal.h" | ||
| 12 | |||
| 13 | /* | ||
| 14 | * A slot holds a profile when its name is not empty. Define refuses an empty | ||
| 15 | * name, so no second flag is needed to say the slot is in use. | ||
| 16 | * | ||
| 17 | * The lock covers the slots and the selection. A bearing is judged with it | ||
| 18 | * held, so a select lands strictly before or after that bearing. Judging takes | ||
| 19 | * the tracker lock inside this one, and the tracker never calls in here, so the | ||
| 20 | * order is always profile lock then tracker lock and cannot invert. | ||
| 21 | */ | ||
| 22 | static struct kfsw_gpredict_profile profiles[CONFIG_KFSW_GPREDICT_PROFILES]; | ||
| 23 | static int selected = -ENOENT; | ||
| 24 | static K_MUTEX_DEFINE(profile_lock); | ||
| 25 | |||
| 26 | 154 | static bool slot_defined(uint8_t index) | |
| 27 | { | ||
| 28 | 154 | return (index < ARRAY_SIZE(profiles)) && (profiles[index].name[0] != '\0'); | |
| 29 | } | ||
| 30 | |||
| 31 | /* A profile narrows the rotator's travel and never widens it. */ | ||
| 32 | 62 | static bool travel_fits(const struct kfsw_gpredict_profile *profile) | |
| 33 | { | ||
| 34 | 122 | return (profile->azimuth_min_mdeg <= profile->azimuth_max_mdeg) && | |
| 35 | 60 | (profile->elevation_min_mdeg <= profile->elevation_max_mdeg) && | |
| 36 | 58 | (profile->azimuth_min_mdeg >= GPREDICT_AZIMUTH_MIN_MDEG) && | |
| 37 | 56 | (profile->azimuth_max_mdeg <= GPREDICT_AZIMUTH_MAX_MDEG) && | |
| 38 | 174 | (profile->elevation_min_mdeg >= GPREDICT_ELEVATION_MIN_MDEG) && | |
| 39 | 52 | (profile->elevation_max_mdeg <= GPREDICT_ELEVATION_MAX_MDEG); | |
| 40 | } | ||
| 41 | |||
| 42 | 66 | static bool profile_valid(const struct kfsw_gpredict_profile *profile) | |
| 43 | { | ||
| 44 | /* No terminator within the name is too long; one at the first byte is empty. */ | ||
| 45 | 66 | const char *terminator = memchr(profile->name, '\0', sizeof(profile->name)); | |
| 46 | |||
| 47 | 66 | if ((terminator == NULL) || (terminator == profile->name)) { | |
| 48 | 4 | return false; | |
| 49 | } | ||
| 50 | 62 | if (!travel_fits(profile)) { | |
| 51 | 12 | return false; | |
| 52 | } | ||
| 53 | 50 | if (profile->policy > KFSW_GPREDICT_FREQUENCY_IGNORE) { | |
| 54 | 2 | return false; | |
| 55 | } | ||
| 56 | /* HOLD with nothing to hold would tune the radio to zero every cycle. */ | ||
| 57 | 48 | return (profile->policy != KFSW_GPREDICT_FREQUENCY_HOLD) || (profile->frequency_hz != 0U); | |
| 58 | } | ||
| 59 | |||
| 60 | 16 | static bool within_profile(const struct kfsw_gpredict_profile *profile, | |
| 61 | const struct kfsw_gpredict_bearing *bearing) | ||
| 62 | { | ||
| 63 | 30 | return (bearing->azimuth_mdeg >= profile->azimuth_min_mdeg) && | |
| 64 | 14 | (bearing->azimuth_mdeg <= profile->azimuth_max_mdeg) && | |
| 65 | 40 | (bearing->elevation_mdeg >= profile->elevation_min_mdeg) && | |
| 66 | 10 | (bearing->elevation_mdeg <= profile->elevation_max_mdeg); | |
| 67 | } | ||
| 68 | |||
| 69 | 70 | int kfsw_gpredict_profile_define(uint8_t index, const struct kfsw_gpredict_profile *profile) | |
| 70 | { | ||
| 71 | 70 | bool deselected = false; | |
| 72 | |||
| 73 | 70 | if (profile == NULL) { | |
| 74 | 2 | return -EINVAL; | |
| 75 | } | ||
| 76 | 68 | if (index >= ARRAY_SIZE(profiles)) { | |
| 77 | 2 | return -ENOSPC; | |
| 78 | } | ||
| 79 | 66 | if (!profile_valid(profile)) { | |
| 80 | 20 | kfsw_log_warning("Pass tracking refused profile %u", index); | |
| 81 | 20 | return -EINVAL; | |
| 82 | } | ||
| 83 | |||
| 84 | 46 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 85 | 46 | profiles[index] = *profile; | |
| 86 | 46 | if ((selected == (int)index) && !profile->enabled) { | |
| 87 | /* A disabled selection would judge against rules nobody can select. */ | ||
| 88 | 2 | selected = -ENOENT; | |
| 89 | 2 | deselected = true; | |
| 90 | } | ||
| 91 | 46 | k_mutex_unlock(&profile_lock); | |
| 92 | |||
| 93 | 46 | kfsw_log_info("Pass tracking profile %u is %s, catalogue %u", index, profile->name, | |
| 94 | profile->catalogue); | ||
| 95 | 46 | if (deselected) { | |
| 96 | 2 | kfsw_log_warning("Pass tracking profile %u disabled; none selected", index); | |
| 97 | } | ||
| 98 | 46 | return 0; | |
| 99 | } | ||
| 100 | |||
| 101 | 38 | int kfsw_gpredict_profile_select(uint8_t index) | |
| 102 | { | ||
| 103 | 38 | char name[KFSW_GPREDICT_PROFILE_NAME_MAX] = ""; | |
| 104 | 38 | int result = 0; | |
| 105 | |||
| 106 | 38 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 107 | 38 | if (!slot_defined(index)) { | |
| 108 | 6 | result = -ENOENT; | |
| 109 | 32 | } else if (!profiles[index].enabled) { | |
| 110 | 4 | result = -EPERM; | |
| 111 | } else { | ||
| 112 | 28 | selected = (int)index; | |
| 113 | 28 | (void)memcpy(name, profiles[index].name, sizeof(name)); | |
| 114 | } | ||
| 115 | 38 | k_mutex_unlock(&profile_lock); | |
| 116 | |||
| 117 | 38 | if (result != 0) { | |
| 118 | 10 | kfsw_log_warning("Pass tracking refused to select profile %u: %d", index, result); | |
| 119 | } else { | ||
| 120 | 28 | kfsw_log_info("Pass tracking selected profile %u, %s", index, name); | |
| 121 | } | ||
| 122 | 38 | return result; | |
| 123 | } | ||
| 124 | |||
| 125 | 22 | int kfsw_gpredict_profile_enable(uint8_t index, bool enabled) | |
| 126 | { | ||
| 127 | 22 | int result = 0; | |
| 128 | |||
| 129 | 22 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 130 | 22 | if (!slot_defined(index)) { | |
| 131 | 6 | result = -ENOENT; | |
| 132 | 16 | } else if (!enabled && (selected == (int)index)) { | |
| 133 | 4 | result = -EBUSY; | |
| 134 | } else { | ||
| 135 | 12 | profiles[index].enabled = enabled; | |
| 136 | } | ||
| 137 | 22 | k_mutex_unlock(&profile_lock); | |
| 138 | |||
| 139 | 22 | if (result != 0) { | |
| 140 | 10 | kfsw_log_warning("Pass tracking refused to set profile %u enabled=%u: %d", index, | |
| 141 | enabled ? 1U : 0U, result); | ||
| 142 | } else { | ||
| 143 | 12 | kfsw_log_info("Pass tracking profile %u enabled=%u", index, enabled ? 1U : 0U); | |
| 144 | } | ||
| 145 | 22 | return result; | |
| 146 | } | ||
| 147 | |||
| 148 | 96 | int kfsw_gpredict_profile_get(uint8_t index, struct kfsw_gpredict_profile *profile) | |
| 149 | { | ||
| 150 | 96 | int result = 0; | |
| 151 | |||
| 152 | 96 | if (profile == NULL) { | |
| 153 | 2 | return -EINVAL; | |
| 154 | } | ||
| 155 | |||
| 156 | 94 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 157 | 94 | if (!slot_defined(index)) { | |
| 158 | 58 | result = -ENOENT; | |
| 159 | } else { | ||
| 160 | 36 | *profile = profiles[index]; | |
| 161 | } | ||
| 162 | 94 | k_mutex_unlock(&profile_lock); | |
| 163 | 94 | return result; | |
| 164 | } | ||
| 165 | |||
| 166 | 50 | int kfsw_gpredict_profile_selected(void) | |
| 167 | { | ||
| 168 | int index; | ||
| 169 | |||
| 170 | 50 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 171 | 50 | index = selected; | |
| 172 | 50 | k_mutex_unlock(&profile_lock); | |
| 173 | 50 | return index; | |
| 174 | } | ||
| 175 | |||
| 176 | 22 | int kfsw_gpredict_profile_bearing(const struct kfsw_gpredict_bearing *bearing) | |
| 177 | { | ||
| 178 | int result; | ||
| 179 | |||
| 180 | 22 | if (bearing == NULL) { | |
| 181 | 2 | return -EINVAL; | |
| 182 | } | ||
| 183 | |||
| 184 | 20 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 185 | 20 | if (selected < 0) { | |
| 186 | 4 | result = -ENOENT; | |
| 187 | 16 | } else if (!within_profile(&profiles[selected], bearing)) { | |
| 188 | /* Counted with the tracker's own refusals, so a profile that refuses | ||
| 189 | * a whole pass shows on gp_refusals rather than as a quiet antenna. | ||
| 190 | */ | ||
| 191 | 8 | gpredict_refuse_bearing(bearing, -EINVAL); | |
| 192 | 8 | result = -EINVAL; | |
| 193 | } else { | ||
| 194 | 8 | result = kfsw_gpredict_bearing(bearing); | |
| 195 | } | ||
| 196 | 20 | k_mutex_unlock(&profile_lock); | |
| 197 | 20 | return result; | |
| 198 | } | ||
| 199 | |||
| 200 | 12 | int kfsw_gpredict_profile_frequency(uint64_t offered, uint64_t *send) | |
| 201 | { | ||
| 202 | 12 | int result = 0; | |
| 203 | |||
| 204 | 12 | if (send == NULL) { | |
| 205 | 2 | return -EINVAL; | |
| 206 | } | ||
| 207 | /* Anything but a decision forwards nothing, whatever the caller checks. */ | ||
| 208 | 10 | *send = 0U; | |
| 209 | 10 | if (offered == 0U) { | |
| 210 | 2 | return -EINVAL; | |
| 211 | } | ||
| 212 | |||
| 213 | 8 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 214 | 8 | if (selected < 0) { | |
| 215 | 2 | result = -ENOENT; | |
| 216 | 6 | } else if (profiles[selected].policy == KFSW_GPREDICT_FREQUENCY_FOLLOW) { | |
| 217 | 2 | *send = offered; | |
| 218 | 4 | } else if (profiles[selected].policy == KFSW_GPREDICT_FREQUENCY_HOLD) { | |
| 219 | 2 | *send = profiles[selected].frequency_hz; | |
| 220 | } | ||
| 221 | 8 | k_mutex_unlock(&profile_lock); | |
| 222 | 8 | return result; | |
| 223 | } | ||
| 224 | |||
| 225 | #if CONFIG_ZTEST | ||
| 226 | 88 | void gpredict_test_profiles_reset(void) | |
| 227 | { | ||
| 228 | 88 | (void)k_mutex_lock(&profile_lock, K_FOREVER); | |
| 229 | 88 | (void)memset(profiles, 0, sizeof(profiles)); | |
| 230 | 88 | selected = -ENOENT; | |
| 231 | 88 | k_mutex_unlock(&profile_lock); | |
| 232 | 88 | } | |
| 233 | #endif | ||
| 234 |