./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
14 / 25
56.0%
Branches:
10 / 20
50.0%
Functions:
4 / 5
80.0%

k-fsw/app/src/parameters/system_table.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/sys/util.h>
7
8 #include <kfsw/services/parameter.h>
9
10 #include "tables.h"
11
12 #if CONFIG_KFSW_HEALTH
13 #define KFSW_APP_REPORT_MS_DEFAULT (CONFIG_KFSW_APP_HEALTH_DEADLINE_MS / 4U)
14 /* The report period must be below half the health deadline, or one late cycle
15 * would look like a stopped thread and reset the board.
16 */
17 #define KFSW_APP_REPORT_MS_MAX (CONFIG_KFSW_APP_HEALTH_DEADLINE_MS / 2U)
18 #else
19 #define KFSW_APP_REPORT_MS_DEFAULT 1000U
20 #define KFSW_APP_REPORT_MS_MAX UINT16_MAX
21 #endif
22
23 /* Read once at start-up, before any service runs. Takes effect at the next boot. */
24 static uint16_t system_boot_delay_ms;
25 static uint16_t system_app_report_ms = KFSW_APP_REPORT_MS_DEFAULT;
26
27 /* 0000 by default. */
28 static char system_reboot_pin[KFSW_SYSTEM_REBOOT_PIN_SIZE] = "0000";
29
30 /* Reject PIN characters the console cannot send. */
31 2 static int validate_reboot_pin(const char *text)
32 {
33 2 size_t length = strlen(text);
34
35
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if ((length == 0U) || (length >= KFSW_SYSTEM_REBOOT_PIN_SIZE)) {
36 ✗ return -EINVAL;
37 }
38
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (size_t index = 0U; index < length; index++) {
39
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if ((text[index] < '0') || (text[index] > '9')) {
40 ✗ return -EINVAL;
41 }
42 }
43 2 return 0;
44 }
45
46 ✗ bool kfsw_system_reboot_pin_matches(const char *pin)
47 {
48 size_t index;
49 ✗ uint8_t difference = 0U;
50
51
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (pin == NULL) {
52 ✗ return false;
53 }
54
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (strlen(pin) != strlen(system_reboot_pin)) {
55 ✗ return false;
56 }
57 /* Compare every byte so the time taken doesn't depend on the match. */
58
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ for (index = 0U; index < strlen(system_reboot_pin); index++) {
59 ✗ difference |= (uint8_t)(pin[index] ^ system_reboot_pin[index]);
60 }
61 ✗ return difference == 0U;
62 }
63
64 8 static int validate_app_report_ms(const union kfsw_param_scalar *value)
65 {
66
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 4 times.
8 if ((value->u16 == 0U) || (value->u16 > KFSW_APP_REPORT_MS_MAX)) {
67 4 return -ERANGE;
68 }
69 4 return 0;
70 }
71
72 static const struct kfsw_param_definition system_param_definitions[] = {
73 {
74 .offset = 0x10U,
75 .type = KFSW_PARAM_STRING,
76 .capacity = KFSW_SYSTEM_REBOOT_PIN_SIZE,
77 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT,
78 .name = "reboot_pin",
79 .description = "Digits a remote reboot must quote, 1 to 8 of them",
80 .value = system_reboot_pin,
81 .default_text = "0000",
82 .validate_text = validate_reboot_pin,
83 },
84 {
85 .offset = 0x00U,
86 .type = KFSW_PARAM_U16,
87 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT,
88 .name = "boot_delay_ms",
89 .unit = "ms",
90 .description = "Delay before services start on the next boot",
91 .value = &system_boot_delay_ms,
92 .default_value = {.u16 = 0U},
93 },
94 {
95 .offset = 0x02U,
96 .type = KFSW_PARAM_U16,
97 /* Live without a callback: the application reads it every cycle. */
98 .flags = KFSW_PARAM_FLAG_CONFIGURATION | KFSW_PARAM_FLAG_PERSISTENT |
99 KFSW_PARAM_FLAG_LIVE,
100 .name = "app_report_ms",
101 .unit = "ms",
102 .description = "How often the application thread reports it is running",
103 .value = &system_app_report_ms,
104 .default_value = {.u16 = KFSW_APP_REPORT_MS_DEFAULT},
105 .validate = validate_app_report_ms,
106 },
107 };
108
109 KFSW_PARAM_TABLE_FITS_THE_LINK(system_param_definitions);
110
111 const struct kfsw_param_definition_set kfsw_system_param_definitions = {
112 .table = KFSW_PARAM_TABLE_SYSTEM,
113 .name = KFSW_PARAM_TABLE_SYSTEM_NAME,
114 .description = "Boot delay, report period, reboot pin",
115 .definitions = system_param_definitions,
116 .count = ARRAY_SIZE(system_param_definitions),
117 };
118
119 2 uint16_t kfsw_system_boot_delay_ms(void)
120 {
121 2 return system_boot_delay_ms;
122 }
123
124 8 uint16_t kfsw_system_app_report_ms(void)
125 {
126 8 return system_app_report_ms;
127 }
128