kfsw-services/src/ftp/ftp_link_csp.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stddef.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #include <zephyr/sys/util.h> | ||
| 7 | |||
| 8 | #include <csp/csp.h> | ||
| 9 | #include <csp/csp_buffer.h> | ||
| 10 | #include <csp/csp_crc32.h> | ||
| 11 | #include <csp/csp_id.h> | ||
| 12 | |||
| 13 | #include <kfsw/comms/csp.h> | ||
| 14 | |||
| 15 | #include "ftp_link.h" | ||
| 16 | |||
| 17 | /* | ||
| 18 | * CSP backend of the file transfer link, and the only file in the service that | ||
| 19 | * includes libcsp. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #define KFSW_FTP_LINK_OVERHEAD (CSP_RDP_HEADER_SIZE + sizeof(csp_crc32_t)) | ||
| 23 | |||
| 24 | /* | ||
| 25 | * The usable payload is the buffer minus the RDP header and the CRC32. | ||
| 26 | */ | ||
| 27 | BUILD_ASSERT(KFSW_FTP_PROTOCOL_HEADER_SIZE + KFSW_FTP_CHUNK_SIZE + KFSW_FTP_LINK_OVERHEAD <= | ||
| 28 | CSP_BUFFER_SIZE, | ||
| 29 | "One FTP message plus the RDP header and CRC32 must fit in one CSP packet"); | ||
| 30 | |||
| 31 | 4 | uint16_t kfsw_ftp_link_max_node(void) | |
| 32 | { | ||
| 33 | 4 | return (uint16_t)((1UL << csp_id_get_host_bits()) - 1UL); | |
| 34 | } | ||
| 35 | |||
| 36 | 140 | uint16_t kfsw_ftp_link_local_node(void) | |
| 37 | { | ||
| 38 | struct kfsw_csp_info csp_info; | ||
| 39 | |||
| 40 | 140 | kfsw_csp_get_info(&csp_info); | |
| 41 | 140 | return csp_info.address; | |
| 42 | } | ||
| 43 | |||
| 44 | 8 | bool kfsw_ftp_link_is_ready(void) | |
| 45 | { | ||
| 46 | struct kfsw_csp_info csp_info; | ||
| 47 | |||
| 48 | 8 | kfsw_csp_get_info(&csp_info); | |
| 49 | 8 | return csp_info.initialized && csp_info.router_running; | |
| 50 | } | ||
| 51 | |||
| 52 | ✗ | int kfsw_ftp_link_connect(struct kfsw_ftp_link *link, uint16_t node) | |
| 53 | { | ||
| 54 | ✗ | if (link == NULL) { | |
| 55 | ✗ | return -EINVAL; | |
| 56 | } | ||
| 57 | ✗ | link->peer = node; | |
| 58 | ✗ | link->connection = csp_connect( | |
| 59 | CSP_PRIO_NORM, node, CONFIG_KFSW_FTP_CSP_PORT, kfsw_ftp_get_timeout_ms(), | ||
| 60 | CSP_O_RDP | (CSP_O_CRC32 | KFSW_CSP_AUTHENTICATED_CONNECTION)); | ||
| 61 | ✗ | return (link->connection != NULL) ? 0 : -ECONNREFUSED; | |
| 62 | } | ||
| 63 | |||
| 64 | ✗ | int kfsw_ftp_link_send(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *message) | |
| 65 | { | ||
| 66 | csp_packet_t *packet; | ||
| 67 | size_t encoded_size; | ||
| 68 | int result; | ||
| 69 | |||
| 70 | ✗ | if ((link == NULL) || (link->connection == NULL) || (message == NULL)) { | |
| 71 | ✗ | return -EINVAL; | |
| 72 | } | ||
| 73 | ✗ | packet = csp_buffer_get(KFSW_FTP_PROTOCOL_HEADER_SIZE + message->path_size + | |
| 74 | ✗ | message->data_size); | |
| 75 | ✗ | if (packet == NULL) { | |
| 76 | ✗ | return -ENOMEM; | |
| 77 | } | ||
| 78 | ✗ | result = kfsw_ftp_protocol_encode(packet->data, CSP_BUFFER_SIZE, message, &encoded_size); | |
| 79 | ✗ | if (result != 0) { | |
| 80 | ✗ | csp_buffer_free(packet); | |
| 81 | ✗ | return result; | |
| 82 | } | ||
| 83 | ✗ | packet->length = encoded_size; | |
| 84 | /* csp_send() frees the packet, even when sending fails. */ | ||
| 85 | ✗ | csp_send(link->connection, packet); | |
| 86 | ✗ | return 0; | |
| 87 | } | ||
| 88 | |||
| 89 | ✗ | int kfsw_ftp_link_receive(struct kfsw_ftp_link *link, struct kfsw_ftp_link_frame *frame) | |
| 90 | { | ||
| 91 | csp_packet_t *packet; | ||
| 92 | int result; | ||
| 93 | |||
| 94 | ✗ | if ((link == NULL) || (link->connection == NULL) || (frame == NULL)) { | |
| 95 | ✗ | return -EINVAL; | |
| 96 | } | ||
| 97 | ✗ | frame->buffer = NULL; | |
| 98 | ✗ | packet = csp_read(link->connection, kfsw_ftp_get_timeout_ms()); | |
| 99 | ✗ | if (packet == NULL) { | |
| 100 | ✗ | return -ETIMEDOUT; | |
| 101 | } | ||
| 102 | ✗ | result = kfsw_ftp_protocol_decode(packet->data, packet->length, &frame->message); | |
| 103 | ✗ | if (result != 0) { | |
| 104 | ✗ | csp_buffer_free(packet); | |
| 105 | ✗ | return result; | |
| 106 | } | ||
| 107 | ✗ | frame->buffer = packet; | |
| 108 | ✗ | return 0; | |
| 109 | } | ||
| 110 | |||
| 111 | ✗ | void kfsw_ftp_link_release(struct kfsw_ftp_link_frame *frame) | |
| 112 | { | ||
| 113 | ✗ | if ((frame == NULL) || (frame->buffer == NULL)) { | |
| 114 | ✗ | return; | |
| 115 | } | ||
| 116 | ✗ | csp_buffer_free(frame->buffer); | |
| 117 | ✗ | frame->buffer = NULL; | |
| 118 | /* The decoded pointers borrowed that buffer; do not leave them dangling. */ | ||
| 119 | ✗ | frame->message.path = NULL; | |
| 120 | ✗ | frame->message.data = NULL; | |
| 121 | } | ||
| 122 | |||
| 123 | 4 | void kfsw_ftp_link_close(struct kfsw_ftp_link *link) | |
| 124 | { | ||
| 125 | 4 | if ((link == NULL) || (link->connection == NULL)) { | |
| 126 | 4 | return; | |
| 127 | } | ||
| 128 | ✗ | (void)csp_close(link->connection); | |
| 129 | ✗ | link->connection = NULL; | |
| 130 | } | ||
| 131 | |||
| 132 | ✗ | bool kfsw_ftp_link_is_open(const struct kfsw_ftp_link *link) | |
| 133 | { | ||
| 134 | ✗ | return (link != NULL) && (link->connection != NULL); | |
| 135 | } | ||
| 136 | |||
| 137 | /* | ||
| 138 | * One listening endpoint is enough: the service accepts one request at a time | ||
| 139 | * and rejects overlapping connections as busy. | ||
| 140 | */ | ||
| 141 | static csp_socket_t link_server_socket; | ||
| 142 | |||
| 143 | 4 | int kfsw_ftp_link_listen(struct kfsw_ftp_listener *listener) | |
| 144 | { | ||
| 145 | int result; | ||
| 146 | |||
| 147 | 4 | if (listener == NULL) { | |
| 148 | ✗ | return -EINVAL; | |
| 149 | } | ||
| 150 | 4 | memset(&link_server_socket, 0, sizeof(link_server_socket)); | |
| 151 | 4 | link_server_socket.opts = CSP_SO_RDPREQ | CSP_SO_CRC32REQ | KFSW_CSP_AUTHENTICATED_SOCKET; | |
| 152 | 4 | result = csp_listen(&link_server_socket, 1U); | |
| 153 | 4 | if (result != CSP_ERR_NONE) { | |
| 154 | ✗ | (void)csp_socket_close(&link_server_socket); | |
| 155 | ✗ | return -EIO; | |
| 156 | } | ||
| 157 | 4 | result = csp_bind(&link_server_socket, CONFIG_KFSW_FTP_CSP_PORT); | |
| 158 | 4 | if (result != CSP_ERR_NONE) { | |
| 159 | ✗ | (void)csp_socket_close(&link_server_socket); | |
| 160 | ✗ | return -EADDRINUSE; | |
| 161 | } | ||
| 162 | 4 | listener->socket = &link_server_socket; | |
| 163 | 4 | return 0; | |
| 164 | } | ||
| 165 | |||
| 166 | ✗ | int kfsw_ftp_link_accept(struct kfsw_ftp_listener *listener, struct kfsw_ftp_link *link, | |
| 167 | uint32_t timeout_ms) | ||
| 168 | { | ||
| 169 | ✗ | if ((listener == NULL) || (listener->socket == NULL) || (link == NULL)) { | |
| 170 | ✗ | return -EINVAL; | |
| 171 | } | ||
| 172 | ✗ | link->connection = csp_accept(listener->socket, timeout_ms); | |
| 173 | ✗ | if (link->connection != NULL) { | |
| 174 | ✗ | link->peer = (uint16_t)csp_conn_src(link->connection); | |
| 175 | } | ||
| 176 | ✗ | return (link->connection != NULL) ? 0 : -EAGAIN; | |
| 177 | } | ||
| 178 | |||
| 179 | ✗ | void kfsw_ftp_link_listener_close(struct kfsw_ftp_listener *listener) | |
| 180 | { | ||
| 181 | ✗ | if ((listener == NULL) || (listener->socket == NULL)) { | |
| 182 | ✗ | return; | |
| 183 | } | ||
| 184 | ✗ | (void)csp_socket_close(listener->socket); | |
| 185 | /* csp_socket_close() leaves the queue pointer set; clear it before reuse. */ | ||
| 186 | ✗ | link_server_socket.rx_queue = NULL; | |
| 187 | ✗ | listener->socket = NULL; | |
| 188 | } | ||
| 189 |