./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
70 / 80
87.5%
Branches:
50 / 64
78.1%
Functions:
2 / 3
66.7%

kfsw-services/src/command/command_retry.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3
4 #include <zephyr/random/random.h>
5 #include <zephyr/sys/util.h>
6
7 #include "command_retry.h"
8
9 ✗ int kfsw_command_retry_random(uint64_t *value)
10 {
11 #if CONFIG_CSPRNG_ENABLED
12 ✗ int result = sys_csrand_get(value, sizeof(*value));
13
14
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != 0) {
15 ✗ *value = 0U;
16 ✗ return result;
17 }
18
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return (*value == 0U) ? -EIO : 0;
19 #else
20 *value = 0U;
21 return -ENOTSUP;
22 #endif
23 }
24
25 22 static bool matches(const struct kfsw_command_ticket *entry,
26 const struct kfsw_command_message *request)
27 {
28
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 return (entry->command == request->command_id) && (entry->request == request->request_id) &&
29
4/6
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
62 (entry->size == request->payload_size) && (entry->count == request->arg_count) &&
30
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 (memcmp(entry->payload, request->payload, entry->size) == 0);
31 }
32
33 56 void kfsw_command_retry_dispatch(struct kfsw_command_retry_cache *cache, uint16_t source,
34 const struct kfsw_command_message *request, uint64_t new_ticket,
35 int64_t now, uint64_t *ticket, struct kfsw_command_result *result)
36 {
37 56 struct kfsw_command_ticket *entry = NULL;
38 56 struct kfsw_command_ticket *free_entry = NULL;
39 struct kfsw_command_arg args[KFSW_COMMAND_MAX_ARGS];
40 char text[KFSW_COMMAND_MAX_ARGS][KFSW_COMMAND_MAX_TEXT_SIZE + 1U];
41 56 struct kfsw_command_source origin = {.node = source, .via_csp = true};
42 56 bool collision = false;
43 int count;
44
45 56 memset(result, 0, sizeof(*result));
46 56 result->status = KFSW_COMMAND_UNAVAILABLE;
47 56 *ticket = request->token;
48
3/6
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
56 if ((request->version != 2U) || (request->token == 0U) || (request->status != 0U) ||
49
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 (request->payload_size > KFSW_COMMAND_MAX_PAYLOAD_SIZE) ||
50
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 ((request->opcode != KFSW_COMMAND_OP_PREPARE) &&
51
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 (request->opcode != KFSW_COMMAND_OP_EXECUTE))) {
52 ✗ result->status = KFSW_COMMAND_INVALID_ARGUMENT;
53 42 return;
54 }
55
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 56 times.
168 for (size_t i = 0; i < ARRAY_SIZE(cache->entries); i++) {
56 112 struct kfsw_command_ticket *candidate = &cache->entries[i];
57
58
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 68 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 40 times.
112 if ((candidate->ticket == 0U) || (now >= candidate->expires)) {
59
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 if (free_entry == NULL) {
60 48 free_entry = candidate;
61 }
62 72 continue;
63 }
64 40 collision |= candidate->ticket == new_ticket;
65
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 14 times.
40 if ((candidate->source == source) &&
66
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 22 times.
26 (((request->opcode == KFSW_COMMAND_OP_PREPARE) &&
67
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 (candidate->nonce == request->token)) ||
68
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
24 ((request->opcode == KFSW_COMMAND_OP_EXECUTE) &&
69
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 2 times.
22 (candidate->ticket == request->token)))) {
70 22 entry = candidate;
71 }
72 }
73
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 34 times.
56 if (entry != NULL) {
74
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 18 times.
22 if (!matches(entry, request)) {
75 4 result->status = KFSW_COMMAND_INVALID_ARGUMENT;
76 4 return;
77 }
78 18 *ticket = entry->ticket;
79
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 16 times.
18 if (request->opcode == KFSW_COMMAND_OP_PREPARE) {
80 2 result->status = KFSW_COMMAND_OK;
81 2 return;
82 }
83
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
16 if (entry->complete) {
84 2 *result = entry->result;
85 2 return;
86 }
87
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 8 times.
34 } else if (request->opcode == KFSW_COMMAND_OP_PREPARE) {
88
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 24 times.
26 if (free_entry == NULL) {
89 2 result->status = KFSW_COMMAND_BUSY;
90 2 return;
91 }
92
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 20 times.
24 if ((new_ticket == 0U) || collision) {
93 4 return;
94 }
95 20 count = kfsw_command_decode_args(request, args, ARRAY_SIZE(args), text);
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (count < 0) {
97 ✗ result->status = KFSW_COMMAND_INVALID_ARGUMENT;
98 ✗ return;
99 }
100 20 entry = free_entry;
101 20 memset(entry, 0, sizeof(*entry));
102 20 entry->nonce = request->token;
103 20 entry->ticket = new_ticket;
104 20 entry->expires = now + CONFIG_KFSW_COMMAND_RETRY_WINDOW_MS;
105 20 entry->source = source;
106 20 entry->command = request->command_id;
107 20 entry->request = request->request_id;
108 20 entry->size = request->payload_size;
109 20 entry->count = request->arg_count;
110 20 memcpy(entry->payload, request->payload, entry->size);
111 20 *ticket = entry->ticket;
112 20 result->status = KFSW_COMMAND_OK;
113 20 return;
114 } else {
115 8 strcpy(result->detail, "ticket expired or unknown; outcome may be unknown");
116 8 return;
117 }
118 14 count = kfsw_command_decode_args(request, args, ARRAY_SIZE(args), text);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (count < 0) {
120 ✗ result->status = KFSW_COMMAND_INVALID_ARGUMENT;
121 } else {
122 14 (void)kfsw_command_invoke_id(request->command_id, args, (size_t)count, &origin,
123 result);
124 }
125 14 entry->result = *result;
126 14 entry->complete = true;
127 }
128