kfsw-services/src/hk/hk_beacon.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | #include <csp/csp.h> | ||
| 6 | #include <kfsw/comms/csp.h> | ||
| 7 | |||
| 8 | #include <kfsw/services/hk.h> | ||
| 9 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HK | ||
| 10 | #include <kfsw/services/log.h> | ||
| 11 | |||
| 12 | #include "hk_internal.h" | ||
| 13 | |||
| 14 | #if CONFIG_KFSW_HK_BEACON | ||
| 15 | |||
| 16 | /* One unacknowledged sample per interval, with a CSP buffer reserve. */ | ||
| 17 | struct beacon_state { | ||
| 18 | uint16_t node; | ||
| 19 | uint32_t interval_ms; | ||
| 20 | int64_t next_uptime_ms; | ||
| 21 | }; | ||
| 22 | |||
| 23 | static struct beacon_state beacons[CONFIG_KFSW_HK_REPORTS]; | ||
| 24 | static uint32_t sent_count; | ||
| 25 | static uint32_t skipped_count; | ||
| 26 | |||
| 27 | 728 | static int set_beacon_impl(uint8_t report, uint16_t node, uint32_t interval_ms) | |
| 28 | { | ||
| 29 | 728 | if (report >= ARRAY_SIZE(beacons)) { | |
| 30 | 4 | return -EINVAL; | |
| 31 | } | ||
| 32 | 724 | if (interval_ms != 0U && interval_ms < CONFIG_KFSW_HK_BEACON_FLOOR_MS) { | |
| 33 | 4 | return -ERANGE; | |
| 34 | } | ||
| 35 | 720 | if (interval_ms != 0U && (node == 0U || node >= KFSW_CSP_BROADCAST_ADDRESS)) { | |
| 36 | 12 | return -EINVAL; | |
| 37 | } | ||
| 38 | 708 | kfsw_hk_lock(); | |
| 39 | 708 | beacons[report].node = node; | |
| 40 | 708 | beacons[report].interval_ms = interval_ms; | |
| 41 | 708 | beacons[report].next_uptime_ms = 0; | |
| 42 | 708 | kfsw_hk_unlock(); | |
| 43 | 708 | kfsw_hk_wake(); | |
| 44 | 708 | return 0; | |
| 45 | } | ||
| 46 | |||
| 47 | 536 | int kfsw_hk_set_beacon(uint8_t report, uint16_t node, uint32_t interval_ms) | |
| 48 | { | ||
| 49 | 536 | int result = kfsw_hk_config_begin(); | |
| 50 | |||
| 51 | return result != 0 ? result | ||
| 52 | 536 | : kfsw_hk_config_end(set_beacon_impl(report, node, interval_ms)); | |
| 53 | } | ||
| 54 | |||
| 55 | 192 | void kfsw_hk_beacon_restore(uint8_t report, uint16_t node, uint32_t interval_ms) | |
| 56 | { | ||
| 57 | 192 | (void)set_beacon_impl(report, node, interval_ms); | |
| 58 | 192 | } | |
| 59 | |||
| 60 | 1536 | int kfsw_hk_get_beacon(uint8_t report, uint16_t *node, uint32_t *interval_ms) | |
| 61 | { | ||
| 62 | 1536 | if ((report >= ARRAY_SIZE(beacons)) || (node == NULL) || (interval_ms == NULL)) { | |
| 63 | 4 | return -EINVAL; | |
| 64 | } | ||
| 65 | 1532 | kfsw_hk_lock(); | |
| 66 | 1532 | *node = beacons[report].node; | |
| 67 | 1532 | *interval_ms = beacons[report].interval_ms; | |
| 68 | 1532 | kfsw_hk_unlock(); | |
| 69 | 1532 | return 0; | |
| 70 | } | ||
| 71 | |||
| 72 | 88 | void kfsw_hk_beacon_stats(uint32_t *sent, uint32_t *skipped) | |
| 73 | { | ||
| 74 | 88 | kfsw_hk_lock(); | |
| 75 | 88 | if (sent != NULL) { | |
| 76 | 88 | *sent = sent_count; | |
| 77 | } | ||
| 78 | 88 | if (skipped != NULL) { | |
| 79 | 88 | *skipped = skipped_count; | |
| 80 | } | ||
| 81 | 88 | kfsw_hk_unlock(); | |
| 82 | 88 | } | |
| 83 | |||
| 84 | 60 | void kfsw_hk_beacon_tick(uint8_t report, int64_t now) | |
| 85 | { | ||
| 86 | struct kfsw_hk_sample sample; | ||
| 87 | csp_packet_t *packet; | ||
| 88 | uint16_t node; | ||
| 89 | |||
| 90 | 60 | if (report >= ARRAY_SIZE(beacons)) { | |
| 91 | 24 | return; | |
| 92 | } | ||
| 93 | 60 | kfsw_hk_lock(); | |
| 94 | 60 | struct beacon_state *beacon = &beacons[report]; | |
| 95 | |||
| 96 | 60 | if (beacon->interval_ms == 0U || now < beacon->next_uptime_ms) { | |
| 97 | 12 | kfsw_hk_unlock(); | |
| 98 | 12 | return; | |
| 99 | } | ||
| 100 | 48 | node = beacon->node; | |
| 101 | 48 | beacon->next_uptime_ms = kfsw_hk_next_due(beacon->next_uptime_ms, beacon->interval_ms, now); | |
| 102 | 48 | kfsw_hk_unlock(); | |
| 103 | |||
| 104 | 48 | if (kfsw_hk_get(report, 0U, &sample) != 0) { | |
| 105 | 4 | return; | |
| 106 | } | ||
| 107 | |||
| 108 | /* Skip the beacon when too few buffers are free, and count it. */ | ||
| 109 | 44 | if (csp_buffer_remaining() <= CONFIG_KFSW_HK_BEACON_BUFFER_RESERVE) { | |
| 110 | 8 | kfsw_hk_lock(); | |
| 111 | 8 | skipped_count++; | |
| 112 | 8 | kfsw_hk_unlock(); | |
| 113 | 8 | return; | |
| 114 | } | ||
| 115 | |||
| 116 | 36 | packet = csp_buffer_get(sample.length); | |
| 117 | 36 | if (packet == NULL) { | |
| 118 | ✗ | kfsw_hk_lock(); | |
| 119 | ✗ | skipped_count++; | |
| 120 | ✗ | kfsw_hk_unlock(); | |
| 121 | ✗ | return; | |
| 122 | } | ||
| 123 | 36 | memcpy(packet->data, sample.data, sample.length); | |
| 124 | 36 | packet->length = sample.length; | |
| 125 | |||
| 126 | /* Use a separate destination port so a beacon cannot trigger a request. */ | ||
| 127 | 36 | csp_sendto(CSP_PRIO_LOW, node, CONFIG_KFSW_HK_BEACON_PORT, CONFIG_KFSW_HK_CSP_PORT, | |
| 128 | (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION), packet); | ||
| 129 | 36 | kfsw_hk_lock(); | |
| 130 | 36 | sent_count++; | |
| 131 | 36 | kfsw_hk_unlock(); | |
| 132 | } | ||
| 133 | |||
| 134 | /* Caller holds the HK state lock. */ | ||
| 135 | ✗ | int64_t kfsw_hk_beacon_wait(uint8_t report, int64_t now) | |
| 136 | { | ||
| 137 | ✗ | return beacons[report].interval_ms == 0U ? 200 | |
| 138 | ✗ | : MAX(0, beacons[report].next_uptime_ms - now); | |
| 139 | } | ||
| 140 | |||
| 141 | #endif /* CONFIG_KFSW_HK_BEACON */ | ||
| 142 |