kfsw-services/src/remexec/remexec_protocol.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <zephyr/sys/byteorder.h> | ||
| 8 | #include <zephyr/sys/util.h> | ||
| 9 | |||
| 10 | #include "remexec_internal.h" | ||
| 11 | |||
| 12 | /* Layout: version, opcode, be16 length, text. */ | ||
| 13 | |||
| 14 | ✗ | static bool known_opcode(uint8_t opcode) | |
| 15 | { | ||
| 16 | ✗ | return (opcode == (uint8_t)KFSW_REMEXEC_OP_LIST) || | |
| 17 | (opcode == (uint8_t)KFSW_REMEXEC_OP_RUN); | ||
| 18 | } | ||
| 19 | |||
| 20 | ✗ | int kfsw_remexec_encode_request(uint8_t *buffer, size_t capacity, enum kfsw_remexec_opcode opcode, | |
| 21 | const char *text, size_t *size) | ||
| 22 | { | ||
| 23 | ✗ | size_t length = (text != NULL) ? strnlen(text, KFSW_REMEXEC_COMMAND_MAX + 1U) : 0U; | |
| 24 | |||
| 25 | ✗ | if ((buffer == NULL) || (size == NULL) || !known_opcode((uint8_t)opcode)) { | |
| 26 | ✗ | return -EINVAL; | |
| 27 | } | ||
| 28 | ✗ | if (length > KFSW_REMEXEC_COMMAND_MAX) { | |
| 29 | ✗ | return -ENAMETOOLONG; | |
| 30 | } | ||
| 31 | ✗ | if (capacity < (KFSW_REMEXEC_REQUEST_HEADER_SIZE + length)) { | |
| 32 | ✗ | return -ENOSPC; | |
| 33 | } | ||
| 34 | ✗ | buffer[0] = KFSW_REMEXEC_PROTOCOL_VERSION; | |
| 35 | ✗ | buffer[1] = (uint8_t)opcode; | |
| 36 | ✗ | sys_put_be16((uint16_t)length, &buffer[2]); | |
| 37 | ✗ | if (length != 0U) { | |
| 38 | ✗ | memcpy(&buffer[KFSW_REMEXEC_REQUEST_HEADER_SIZE], text, length); | |
| 39 | } | ||
| 40 | ✗ | *size = KFSW_REMEXEC_REQUEST_HEADER_SIZE + length; | |
| 41 | ✗ | return 0; | |
| 42 | } | ||
| 43 | |||
| 44 | ✗ | int kfsw_remexec_decode_request(const uint8_t *data, size_t size, enum kfsw_remexec_opcode *opcode, | |
| 45 | char *text, size_t text_capacity) | ||
| 46 | { | ||
| 47 | uint16_t length; | ||
| 48 | |||
| 49 | ✗ | if ((data == NULL) || (opcode == NULL) || (text == NULL) || (text_capacity == 0U)) { | |
| 50 | ✗ | return -EINVAL; | |
| 51 | } | ||
| 52 | ✗ | if (size < KFSW_REMEXEC_REQUEST_HEADER_SIZE) { | |
| 53 | ✗ | return -EBADMSG; | |
| 54 | } | ||
| 55 | ✗ | if ((data[0] != KFSW_REMEXEC_PROTOCOL_VERSION) || !known_opcode(data[1])) { | |
| 56 | ✗ | return -EBADMSG; | |
| 57 | } | ||
| 58 | ✗ | length = sys_get_be16(&data[2]); | |
| 59 | ✗ | if ((size_t)length != (size - KFSW_REMEXEC_REQUEST_HEADER_SIZE)) { | |
| 60 | ✗ | return -EBADMSG; | |
| 61 | } | ||
| 62 | ✗ | if ((size_t)length >= text_capacity) { | |
| 63 | ✗ | return -ENAMETOOLONG; | |
| 64 | } | ||
| 65 | ✗ | *opcode = (enum kfsw_remexec_opcode)data[1]; | |
| 66 | ✗ | memcpy(text, &data[KFSW_REMEXEC_REQUEST_HEADER_SIZE], length); | |
| 67 | ✗ | text[length] = '\0'; | |
| 68 | ✗ | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | int kfsw_remexec_encode_reply(uint8_t *buffer, size_t capacity, enum kfsw_remexec_opcode opcode, | |
| 72 | const struct kfsw_remexec_reply *reply, size_t *size) | ||
| 73 | { | ||
| 74 | size_t length; | ||
| 75 | |||
| 76 | ✗ | if ((buffer == NULL) || (reply == NULL) || (size == NULL) || | |
| 77 | ✗ | !known_opcode((uint8_t)opcode)) { | |
| 78 | ✗ | return -EINVAL; | |
| 79 | } | ||
| 80 | ✗ | length = MIN((size_t)reply->size, KFSW_REMEXEC_OUTPUT_MAX); | |
| 81 | ✗ | if (capacity < (KFSW_REMEXEC_REPLY_HEADER_SIZE + length)) { | |
| 82 | ✗ | return -ENOSPC; | |
| 83 | } | ||
| 84 | ✗ | buffer[0] = KFSW_REMEXEC_PROTOCOL_VERSION; | |
| 85 | ✗ | buffer[1] = (uint8_t)opcode; | |
| 86 | ✗ | buffer[2] = (uint8_t)reply->status; | |
| 87 | ✗ | buffer[3] = reply->truncated ? KFSW_REMEXEC_FLAG_TRUNCATED : 0U; | |
| 88 | ✗ | sys_put_be32((uint32_t)reply->command_result, &buffer[4]); | |
| 89 | ✗ | sys_put_be32(reply->produced, &buffer[8]); | |
| 90 | ✗ | sys_put_be32(reply->dropped, &buffer[12]); | |
| 91 | ✗ | sys_put_be16((uint16_t)length, &buffer[16]); | |
| 92 | ✗ | if (length != 0U) { | |
| 93 | ✗ | memcpy(&buffer[KFSW_REMEXEC_REPLY_HEADER_SIZE], reply->text, length); | |
| 94 | } | ||
| 95 | ✗ | *size = KFSW_REMEXEC_REPLY_HEADER_SIZE + length; | |
| 96 | ✗ | return 0; | |
| 97 | } | ||
| 98 | |||
| 99 | ✗ | int kfsw_remexec_decode_reply(const uint8_t *data, size_t size, enum kfsw_remexec_opcode opcode, | |
| 100 | struct kfsw_remexec_reply *reply) | ||
| 101 | { | ||
| 102 | uint16_t length; | ||
| 103 | |||
| 104 | ✗ | if ((data == NULL) || (reply == NULL)) { | |
| 105 | ✗ | return -EINVAL; | |
| 106 | } | ||
| 107 | ✗ | if (size < KFSW_REMEXEC_REPLY_HEADER_SIZE) { | |
| 108 | ✗ | return -EBADMSG; | |
| 109 | } | ||
| 110 | ✗ | if ((data[0] != KFSW_REMEXEC_PROTOCOL_VERSION) || (data[1] != (uint8_t)opcode) || | |
| 111 | ✗ | (data[2] > (uint8_t)KFSW_REMEXEC_UNAVAILABLE)) { | |
| 112 | ✗ | return -EBADMSG; | |
| 113 | } | ||
| 114 | ✗ | length = sys_get_be16(&data[16]); | |
| 115 | ✗ | if (((size_t)length != (size - KFSW_REMEXEC_REPLY_HEADER_SIZE)) || | |
| 116 | ((size_t)length > KFSW_REMEXEC_OUTPUT_MAX)) { | ||
| 117 | ✗ | return -EBADMSG; | |
| 118 | } | ||
| 119 | ✗ | reply->status = (enum kfsw_remexec_status)data[2]; | |
| 120 | ✗ | reply->truncated = (data[3] & KFSW_REMEXEC_FLAG_TRUNCATED) != 0U; | |
| 121 | ✗ | reply->command_result = (int32_t)sys_get_be32(&data[4]); | |
| 122 | ✗ | reply->produced = sys_get_be32(&data[8]); | |
| 123 | ✗ | reply->dropped = sys_get_be32(&data[12]); | |
| 124 | ✗ | reply->size = length; | |
| 125 | ✗ | memcpy(reply->text, &data[KFSW_REMEXEC_REPLY_HEADER_SIZE], length); | |
| 126 | ✗ | reply->text[length] = '\0'; | |
| 127 | ✗ | return 0; | |
| 128 | } | ||
| 129 |