diff options
author | 2019-05-21 17:41:21 +0300 | |
---|---|---|
committer | 2019-05-21 17:41:21 +0300 | |
commit | cabb90c1240015ee5cd17d91573588527bcc2482 (patch) | |
tree | 0e3b07fa887ca25a12e32f5c126407d149483a21 /src/common/protocol.c | |
parent | bb70fccb66f55e9cc2b9ed0bf366479828f41346 (diff) | |
download | usurpation-cabb90c1240015ee5cd17d91573588527bcc2482.tar.gz usurpation-cabb90c1240015ee5cd17d91573588527bcc2482.tar.bz2 usurpation-cabb90c1240015ee5cd17d91573588527bcc2482.zip |
Protocol: changes to interface and some re-implementation.
Most functions exposed in protocol.h take a connection descriptor
(cd_t) as first argument. This allows for multiple connections. Device
gets only one connection which means that cd_t is effectively 0 all
the time.
Additionaly, any function that actually does anything with a
connection descriptor instead of just passing it to another function
must be implemented separately in device and daemon.
Signed-off-by: Ramūnas Mažeikis <ramunasnezinomas@gmail.com>
Diffstat (limited to 'src/common/protocol.c')
-rw-r--r-- | src/common/protocol.c | 109 |
1 files changed, 44 insertions, 65 deletions
diff --git a/src/common/protocol.c b/src/common/protocol.c index 2243dad..3d63771 100644 --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -18,6 +18,12 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +/** + * Common parts of protocol implementation. Handling of anything that actually + * deals with connection descriptor has to be implemented by device and daemon + * separately. + */ + #include "protocol.h" #include "protocol_private.h" #include "net.h" @@ -37,7 +43,11 @@ * indicating that going back is not possible. * */ static int msg_cursor = 2; -/* Two consecutive null's indicate that going back is not possible. + +/** + * Message buffer. + * + * Two consecutive null's indicate that going back is not possible. * */ static char msg_buf[MSG_BUF_SIZE] = {0}; @@ -49,7 +59,7 @@ static size_t packet_cursor = 0; * */ static char packet_buf[PACKET_MAX_SIZE] = {0}; -static int push_bytes(const void * const data, const size_t size) +static int push_bytes(cd_t connection, const void * const data, const size_t size) { if (packet_cursor + size >= PACKET_MAX_SIZE) { return E_PACKET_OVERFLOW; @@ -59,109 +69,78 @@ static int push_bytes(const void * const data, const size_t size) return 0; } -void clear_data(void) +void clear_data(cd_t connection) { packet_cursor = 0; } -int push_data(const char *data, enum tlv_type type) +int push_data(cd_t connection, const char *data, enum tlv_type type) { int ret = E_UNKNOWN_TYPE; switch (type) { case TEXT: - ret = push_string(data); + ret = push_string(connection, data); break; case FPI1: - ret = push_fpi1(data); + ret = push_fpi1(connection, data); break; case TIMESTAMP: - ret = push_timestamp(data); + ret = push_timestamp(connection, data); break; case REQUEST: - ret = push_request(data); + ret = push_request(connection, data); break; case REPLY: - ret = push_reply(data); + ret = push_reply(connection, data); break; case UUID: - ret = push_uuid(data); + ret = push_uuid(connection, data); break; } return ret; } -static int push_string(char *str) +static int push_string(cd_t connection, char *str) { + int ret = 0; size_t size = strlen(str); - push_tlv_header(TEXT, size); - push_bytes(str, size); - return 0; -} - -static int push_fpi1(char *num) -{ - return push_tlv_header(FPI1, sizeof(fpi1_t)) - | push_bytes(num, sizeof(fpi1_t)); -} - -static int push_timestamp(char *data) -{ - return push_tlv_header(TIMESTAMP, sizeof(time_t)) - | push_bytes(data, sizeof(time_t)); -} - -static int push_request(char *data) -{ - return push_tlv_header(REQUEST, sizeof(msg_idx_t)) - | push_bytes(data, sizeof(msg_idx_t)); + ret |= push_tlv_header(connection, TEXT, size); + ret |= push_bytes(connection, str, size); + return ret; } -static int push_reply(char *data) +static int push_fpi1(cd_t connection, char *num) { - int ret = 0; - size_t msglen = strlen(data + sizeof(msg_idx_t)); - ret |= push_tlv_header(REPLY, msglen + sizeof(msg_idx_t)); - ret |= push_bytes(data, msglen); - return ret; + return push_tlv_header(connection, FPI1, sizeof(fpi1_t)) + | push_bytes(connection, num, sizeof(fpi1_t)); } -static int push_uuid(char *data) +static int push_timestamp(cd_t connection, char *data) { - return push_tlv_header(UUID, sizeof(uuid_t)) - | push_bytes(data, sizeof(uuid_t)); + return push_tlv_header(connection, TIMESTAMP, sizeof(time_t)) + | push_bytes(connection, data, sizeof(time_t)); } -static int push_tlv_header(enum tlv_type type, size_t size) +static int push_request(cd_t connection, char *data) { - if (size + sizeof(type) + packet_cursor >= PACKET_MAX_SIZE) { - return E_PACKET_OVERFLOW; - } - READ_AS(packet_buf, enum tlv_type) = type; - packet_cursor += sizeof(type); - READ_AS(packet_buf, size_t) = size; - packet_cursor += sizeof(size); - return 0; + return push_tlv_header(connection, REQUEST, sizeof(msg_idx_t)) + | push_bytes(connection, data, sizeof(msg_idx_t)); } -void get_last_data() +static int push_reply(cd_t connection, char *data) { - msg_cursor = 2; - msg_buf[0] = '\0'; - msg_buf[1] = '\0'; - tlv_cursor = 0; - get_tlvs(packet_buf, tlv_buf, TLV_BUF_SIZE); + int ret = 0; + size_t msglen = strlen(data + sizeof(msg_idx_t)); + ret |= push_tlv_header(connection, REPLY, msglen + sizeof(msg_idx_t)); + ret |= push_bytes(connection, data, msglen); + return ret; } -struct tlv * get_tlv(void) +static int push_uuid(cd_t connection, char *data) { - struct tlv *ret; - if (tlv_buf + tlv_cursor != NULL) { - struct tlv *ret = &tlv_buf[tlv_cursor++]; - } else { - ret = NULL; - } - return ret; + return push_tlv_header(connection, UUID, sizeof(uuid_t)) + | push_bytes(connection, data, sizeof(uuid_t)); } size_t tlv_count(const struct packet_data * const packet) @@ -182,7 +161,7 @@ size_t tlv_count(const struct packet_data * const packet) return ret; } -size_t get_tlvs( const struct packet_data * const data, +size_t get_tlvs( const struct packet_data * const data, const struct tlv *buf, size_t buf_size) { |