kfsw-services/src/ftp/ftp_state.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | |||
| 4 | #include <zephyr/kernel.h> | ||
| 5 | |||
| 6 | #include <kfsw/services/ftp.h> | ||
| 7 | |||
| 8 | #include "ftp_internal.h" | ||
| 9 | |||
| 10 | /* | ||
| 11 | * Counters and settings that can be changed from the ground. | ||
| 12 | */ | ||
| 13 | |||
| 14 | static atomic_t ftp_transfers; | ||
| 15 | static atomic_t ftp_failures; | ||
| 16 | static atomic_t ftp_bytes; | ||
| 17 | |||
| 18 | static uint32_t ftp_timeout_ms = CONFIG_KFSW_FTP_TIMEOUT_MS; | ||
| 19 | static uint16_t ftp_chunk_size = KFSW_FTP_CHUNK_SIZE; | ||
| 20 | |||
| 21 | 68 | void kfsw_ftp_count_transfer(uint32_t bytes, bool failed) | |
| 22 | { | ||
| 23 | 68 | if (failed) { | |
| 24 | 56 | (void)atomic_inc(&ftp_failures); | |
| 25 | 56 | return; | |
| 26 | } | ||
| 27 | 12 | (void)atomic_inc(&ftp_transfers); | |
| 28 | 12 | (void)atomic_add(&ftp_bytes, (atomic_val_t)bytes); | |
| 29 | } | ||
| 30 | |||
| 31 | 12 | int kfsw_ftp_get_stats(struct kfsw_ftp_stats *stats) | |
| 32 | { | ||
| 33 | 12 | if (stats == NULL) { | |
| 34 | ✗ | return -EINVAL; | |
| 35 | } | ||
| 36 | |||
| 37 | 12 | stats->transfers = (uint32_t)atomic_get(&ftp_transfers); | |
| 38 | 12 | stats->failures = (uint32_t)atomic_get(&ftp_failures); | |
| 39 | 12 | stats->bytes = (uint32_t)atomic_get(&ftp_bytes); | |
| 40 | 12 | stats->busy = kfsw_ftp_server_is_busy(); | |
| 41 | 12 | return 0; | |
| 42 | } | ||
| 43 | |||
| 44 | ✗ | uint32_t kfsw_ftp_get_timeout_ms(void) | |
| 45 | { | ||
| 46 | ✗ | return ftp_timeout_ms; | |
| 47 | } | ||
| 48 | |||
| 49 | 4 | int kfsw_ftp_check_timeout_ms(uint32_t timeout_ms) | |
| 50 | { | ||
| 51 | /* Same range as Kconfig. Checked separately because a change callback | ||
| 52 | * can't refuse a value. | ||
| 53 | */ | ||
| 54 | 4 | if ((timeout_ms < KFSW_FTP_TIMEOUT_MIN_MS) || (timeout_ms > KFSW_FTP_TIMEOUT_MAX_MS)) { | |
| 55 | ✗ | return -ERANGE; | |
| 56 | } | ||
| 57 | 4 | return 0; | |
| 58 | } | ||
| 59 | |||
| 60 | 2 | int kfsw_ftp_set_timeout_ms(uint32_t timeout_ms) | |
| 61 | { | ||
| 62 | 2 | int result = kfsw_ftp_check_timeout_ms(timeout_ms); | |
| 63 | |||
| 64 | 2 | if (result != 0) { | |
| 65 | ✗ | return result; | |
| 66 | } | ||
| 67 | 2 | ftp_timeout_ms = timeout_ms; | |
| 68 | 2 | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | 20 | uint16_t kfsw_ftp_get_chunk_size(void) | |
| 72 | { | ||
| 73 | 20 | return ftp_chunk_size; | |
| 74 | } | ||
| 75 | |||
| 76 | 12 | int kfsw_ftp_check_chunk_size(uint16_t chunk_size) | |
| 77 | { | ||
| 78 | /* The buffer is sized at build time, so a larger chunk is refused here. */ | ||
| 79 | 12 | if ((chunk_size == 0U) || (chunk_size > KFSW_FTP_CHUNK_SIZE)) { | |
| 80 | 4 | return -ERANGE; | |
| 81 | } | ||
| 82 | 8 | return 0; | |
| 83 | } | ||
| 84 | |||
| 85 | 4 | int kfsw_ftp_set_chunk_size(uint16_t chunk_size) | |
| 86 | { | ||
| 87 | 4 | int result = kfsw_ftp_check_chunk_size(chunk_size); | |
| 88 | |||
| 89 | 4 | if (result != 0) { | |
| 90 | ✗ | return result; | |
| 91 | } | ||
| 92 | 4 | ftp_chunk_size = chunk_size; | |
| 93 | 4 | return 0; | |
| 94 | } | ||
| 95 |