summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Ramūnas Mažeikis <ramunasnezinomas@gmail.com> 2019-05-22 20:33:08 +0300
committerGravatar Ramūnas Mažeikis <ramunasnezinomas@gmail.com> 2019-05-22 20:33:08 +0300
commit28b26ca86cf18947d6d9543ad753ef112ff4da89 (patch)
tree00a9205e36efe91e4229ad1c17750aa6aaf0fa68
parentf810b277752fc42f3a0e951c7501670a596eb387 (diff)
downloadusurpation-28b26ca86cf18947d6d9543ad753ef112ff4da89.tar.gz
usurpation-28b26ca86cf18947d6d9543ad753ef112ff4da89.tar.bz2
usurpation-28b26ca86cf18947d6d9543ad753ef112ff4da89.zip
Protocol: interface redesign.
Every function is a stub now. Reimplementaion coming up. Signed-off-by: Ramūnas Mažeikis <ramunasnezinomas@gmail.com>
-rw-r--r--include/protocol.h45
-rw-r--r--src/common/protocol.c102
-rw-r--r--src/common/protocol_private.h122
-rw-r--r--src/device/protocol_device.c121
-rw-r--r--src/device/protocol_device_private.h59
5 files changed, 56 insertions, 393 deletions
diff --git a/include/protocol.h b/include/protocol.h
index 8d5c535..d8f395d 100644
--- a/include/protocol.h
+++ b/include/protocol.h
@@ -23,9 +23,9 @@
#include <errno.h>
-#define E_TLV_OVERFLOW (1 << 0)
-#define E_UNKNOWN_TYPE (1 << 1)
-#define E_IVALID_DESCRIPTOR (1 << 2)
+#define E_TLV_OVERFLOW (1 << 0)
+#define E_UNKNOWN_TYPE (1 << 1)
+#define E_IVALID_DESCRIPTOR (1 << 2)
/**
* Regular packets contain tlv's defined by tlv_type.
@@ -41,11 +41,6 @@ enum packet_type {
};
/**
- * Connection descriptor.
- */
-typedef unsigned int cd_t;
-
-/**
* Message sequence number since beggining of sesssion.
*
* Mainly used for identifying lost messages.
@@ -86,10 +81,13 @@ enum tlv_type {
/**
* Packet data itself is a special type of tlv. A packet is either regular,
* hearbeat or discovery.
+ *
+ * May be used to send data.
*/
-struct packet_data {
+struct tlv_packet {
enum packet_type type;
size_t packet_size;
+ size_t offset;
char *data; /* Bytes representing tlv's */
};
@@ -102,33 +100,18 @@ struct tlv {
void *data;
};
-/**
- * Reads last packets received, parses and stores them to be later retreived
- * via get_tlv.
- * */
-int get_last_data(cd_t connection);
-
-/** Returns tlv's parsed by get_last_data. Returned tlv is only valid until
- * next call to get_tlv.
- * */
-struct tlv * get_tlv(cd_t cd);
+struct tlv_parser {
+ char *data;
+ size_t offset;
+ size_t size;
+};
-/**
- * Any modifications made to the pending outgoing packet are nullified.
- * */
-int clear_data(cd_t connection);
+int get_tlv(struct tlv_parser *parser, struct tlv *ret);
/**
* Appends data to the next packet to be sent. Type of data is determined by
* enum tlv_type.
* */
-int push_data(cd_t connection, const char *data, enum tlv_type);
-
-/**
- * Sends packet towards the other end.
- * */
-int flush_data(cd_t connection);
-
-cd_t protocol_init(void);
+int push_tlv(struct tlv_packet *packet, const char *data, enum tlv_type);
#endif /* PROTOCOL_H_INCLUDED */
diff --git a/src/common/protocol.c b/src/common/protocol.c
index 54d51f6..58a70f6 100644
--- a/src/common/protocol.c
+++ b/src/common/protocol.c
@@ -33,121 +33,85 @@
#define READ_AS(type, from) (*((type*)(from)))
-int push_data(cd_t connection, const char *data, enum tlv_type type)
+int push_data(enum tlv_type type)
{
- int ret = E_UNKNOWN_TYPE;
+ int ret = 0;
switch (type) {
case TEXT:
- ret = push_string(connection, data);
break;
case FPI1:
- ret = push_fpi1(connection, data);
break;
case TIMESTAMP:
- ret = push_timestamp(connection, data);
break;
case REQUEST:
- ret = push_request(connection, data);
break;
case REPLY:
- ret = push_reply(connection, data);
break;
case UUID:
- ret = push_uuid(connection, data);
break;
+ default:
+ ret = E_UNKNOWN_TYPE;
}
return ret;
}
-static int push_string(cd_t connection, const char *str)
+static int push_string(const char *str)
{
- int ret = 0;
- size_t size = strlen(str);
-
- ret |= push_tlv_header(connection, TEXT, size);
- ret |= push_bytes(connection, str, size);
- return ret;
+ return 0;
}
-static int push_fpi1(cd_t connection, const char *num)
+static int push_fpi1(const char *num)
{
- return push_tlv_header(connection, FPI1, sizeof(fpi1_t))
- | push_bytes(connection, num, sizeof(fpi1_t));
+ return 0;
}
-static int push_timestamp(cd_t connection, const char *data)
+static int push_timestamp(const char *data)
{
- return push_tlv_header(connection, TIMESTAMP, sizeof(time_t))
- | push_bytes(connection, data, sizeof(time_t));
+ return 0;
}
-static int push_request(cd_t connection, const char *data)
+static int push_request(const char *data)
{
- return push_tlv_header(connection, REQUEST, sizeof(msg_idx_t))
- | push_bytes(connection, data, sizeof(msg_idx_t));
+ return 0;
}
-static int push_reply(cd_t connection, const char *data)
+static int push_reply(const char *data)
{
- 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;
+ return 0;
}
-static int push_uuid(cd_t connection, const char *data)
+static int push_uuid(const char *data)
{
- return push_tlv_header(connection, UUID, sizeof(uuid_t))
- | push_bytes(connection, data, sizeof(uuid_t));
+ return 0;
}
size_t tlv_count(const struct packet_data * const packet)
{
- size_t cursor = 0;
- size_t length = 0;
- size_t ret = 0;
-
- while (cursor < packet->packet_size) {
- cursor += sizeof(enum tlv_type);
- length = READ_AS(enum tlv_type, packet->data + cursor);
- cursor += sizeof(size_t) + length;
- ret++;
- }
- if (cursor != packet->packet_size) {
- errno = E_TLV_OVERFLOW;
- }
- return ret;
+ return 0;
}
-size_t get_tlvs( const struct packet_data * const data,
- struct tlv *buf,
- size_t buf_size)
+size_t parse_tlv(const char *data, size_t cursor, struct tlv *t)
{
- size_t tlvs_read = 0;
- size_t cursor = 0;
+ return 0;
+}
- while (cursor < data->packet_size && tlvs_read <= buf_size) {
- cursor += parse_tlv(data->data, cursor, buf + tlvs_read);
- tlvs_read++;
- }
- if (cursor > data->packet_size) {
- errno = E_TLV_OVERFLOW;
- }
-
- return tlvs_read;
+int clear_data()
+{
+ return 0;
}
-size_t parse_tlv(const char *data, size_t cursor, struct tlv *t)
+static int push_bytes(const char *data, size_t size)
{
- const char *begin = data + cursor;
+ return 0;
+}
- t->type = READ_AS(enum tlv_type, data + cursor);
- cursor += sizeof(enum tlv_type);
- t->length += READ_AS(enum tlv_type ,data + cursor);
- data += sizeof(size_t);
- t->data = data + cursor;
+static int push_tlv_header(enum tlv_type type, size_t size)
+{
+ return 0;
+}
- return data + cursor - begin + 1UL;
+int get_tlv(struct tlv_parser *parser, struct tlv *ret)
+{
+ return 0;
}
diff --git a/src/common/protocol_private.h b/src/common/protocol_private.h
index d50a314..5228afd 100644
--- a/src/common/protocol_private.h
+++ b/src/common/protocol_private.h
@@ -1,115 +1,11 @@
-/*
- * Usurpataion --- server-client protocol private interface.
- *
- * Copyright (C) 2019 Ramūnas Mažeikis
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; version 2.1
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
+#ifndef PROTOCOL_PRIVATE_H
+#define PROTOCOL_PRIVATE_H
-#ifndef PROTOCOL_H_PRIVATE
-#define PROTOCOL_H_PRIVATE
+static int push_string(const char *str);
+static int push_fpi1(const char *num);
+static int push_timestamp(const char *data);
+static int push_request(const char *data);
+static int push_reply(const char *data);
+static int push_uuid(const char *data);
-#include "utils.h"
-
-/**
- * Returns the amount of tlv's int a packet.
- *
- * If a tlv reports length that goes beyond the end of a packet, errno is set
- * to TLV_OVERFLOW. To check this, set errno to 0 first.
- *
- * @param packet data parsed from a packet. Function cannot use raw packets.
- *
- * @return Amount of tlv's in a packet.
- * */
-size_t tlv_count(const struct packet_data * const packet_data);
-
-/**
- * Pushes bytes to outgoing packet and adjusts the offset accordingly.
- *
- * Returns E_PACKET_OVERFLOW, if no more bytes can fit into the packet.
- */
-
-int push_bytes(cd_t cd, const char *data, size_t size);
-
-/**
- * Parses tlv's from packet data and writes them to a buffer of given size.
- *
- * Returns how many tlv's were actually parsed.
- *
- * To check for errors, set errno to 0 and check after calling.
- *
- * Not yet implemented.
- *
- * @param data Data from network packet
- * @param buf Buffer to store parsed tlv's
- * @param buf_size Size of buffer used to store tlv's
- *
- * @return Number of tlv's actually parsed. Greter than or equal to buffer
- * size, if an error occurs.
- * */
-size_t get_tlvs( const struct packet_data * const data,
- const struct tlv *buf,
- size_t buf_size);
-
-/** Takes a null-terminated string and appends it to the next outgoing packet.
- *
- * @return Returns 0 on success. Otherwise: E_PACKET_OVERFLOW.
- *
- * */
-int push_string(cd_t connection, char *str);
-
-/** Reinterprets char * as fpi1_t * and appends it to the outgoing packet as a
- * tlv.
- */
-int push_fpi1(cd_t connection, char *num);
-
-/** Reinterprets char * as time_t * and appends it to the outgoing packet as a
- * tlv.
- */
-int push_timestamp(cd_t connection, char *data);
-
-/**
- * Pushes a request for daemon to repeat a message identified by a msg_index_t.
- *
- * @param data Pointer to a msg_index_t.
- *
- * @return 0 on success or E_PACKET_OVERFLOW, if not enough space is available
- * to push all the data.
- */
-int push_request(cd_t connection, char *data);
-
-/**
- * Pushes a message to the outgoing packet buffer as a reply. A reply is just
- * a null terminated string.
- *
- * @param data msg_idx_t representing sequence number since beggining of
- * connection and a null-terminated string.
- *
- * @return On success --- 0 or E_PACKET_OVERFLOW, if not enough buffer is
- * available.
- */
-int push_reply(cd_t connection, char *data);
-
-/** Reinterprets char * as uuid_t * and appends it to the outgoing packet as a
- * tlv.
- */
-int push_uuid(cd_t connection, char *data);
-
-/* Appends tlv_type and size of data to a packet effectively creating a tlv
- * header.
- */
-int push_tlv_header(cd_t connection, enum tlv_type type, size_t size);
-
-#endif /* PROTOCOL_H_PRIVATE */
+#endif /* PROTOCOL_PRIVATE_H */ \ No newline at end of file
diff --git a/src/device/protocol_device.c b/src/device/protocol_device.c
deleted file mode 100644
index d5ec62d..0000000
--- a/src/device/protocol_device.c
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * Usurpation – wearable device udp packet handling
- *
- * Copyright (C) 2019 Ramūnas Mažeikis
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; version 2.1
- * of the License.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
- */
-
-#include <time.h>
-#include <stdlib.h>
-#include <string.h>
-#include "net.h"
-#include "protocol.h"
-#include "protocol_private.h"
-#include "protocol_device_private.h"
-#include "settings.h"
-
-#define READ_AS(type, from) (*((type*)(from)))
-
-static struct connection_t connection;
-
-cd_t protocol_init(void)
-{
- connection.is_live = 1;
- connection.nd = net_init(setting_port());
- return 0;
-}
-
-int clear_data(cd_t cd)
-{
- int ret = 0;
- if (cd >= MAX_CONNECTIONS) {
- ret = E_IVALID_DESCRIPTOR;
- } else {
- connection.inp_crs = 0;
- }
- return ret;
-}
-
-static int push_bytes(cd_t cd, const char *data, size_t size)
-{
- int ret = 0;
-
- if (cd >= MAX_CONNECTIONS) {
- ret = E_IVALID_DESCRIPTOR;
- } else if (connection.outp_crs + size >= sizeof(connection.outp_buf)) {
- ret = E_PACKET_OVERFLOW;
- } else {
- memcpy(connection.outp_buf, data, size);
- connection.outp_crs += size;
- }
- return ret;
-}
-
-static int push_tlv_header(cd_t cd, enum tlv_type type, size_t size)
-{
- int ret = 0;
-
- if (cd >= MAX_CONNECTIONS) {
- errno = E_IVALID_DESCRIPTOR;
- ret = NULL;
- } else if (connection.outp_crs >= MAX_PACKET_SIZE_IN) {
- errno = E_TLV_OVERFLOW;
- ret = NULL;
- } else {
- READ_AS(enum tlv_type, connection.outp_buf + connection.outp_crs)
- = type;
- connection.outp_crs += sizeof(enum tlv_type);
- READ_AS(size_t, connection.outp_buf + connection.outp_crs) = size;
- connection.outp_crs += sizeof(size_t);
- }
- return ret;
-}
-
-struct tlv * get_tlv(cd_t cd)
-{
- struct tlv *ret = &connection.next_tlv;
- size_t offset = connection.inp_crs;
- char *data = connection.inp_buf;
-
- if (cd >= MAX_CONNECTIONS) {
- errno = E_IVALID_DESCRIPTOR;
- ret = NULL;
- } else if (offset >= MAX_PACKET_SIZE_IN) {
- errno = E_TLV_OVERFLOW;
- ret = NULL;
- } else {
- ret->type = READ_AS(enum tlv_type, data + offset);
- offset += sizeof(enum tlv_type);
- ret->length = READ_AS(size_t, data + offset);
- offset += sizeof(size_t);
- ret->data = data + offset;
- connection.inp_crs = offset;
- }
- return ret;
-}
-
-int flush_data(cd_t cd)
-{
- return 0;
-}
-
-int get_last_data(cd_t cd)
-{
- if (cd >= MAX_CONNECTIONS) {
- return E_IVALID_DESCRIPTOR;
- }
- net_getlastdata(connection.nd, connection.inp_buf);
-}
diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h
deleted file mode 100644
index 6754ecf..0000000
--- a/src/device/protocol_device_private.h
+++ /dev/null
@@ -1,59 +0,0 @@
-#ifndef PROTOCOL_DEVICE_PRIVATE_H
-#define PROTOCOL_DEVICE_PRIVATE_H
-
-#include "net.h"
-
-#define MAX_PACKET_SIZE_OUT (64)
-
-/* Maximum size of packet that can fit into the packet buffer. UDP can carry
- * bigger packets but memory is hard to come by and more won't be needed
- * anyway.
- * */
-#define MAX_PACKET_SIZE_IN 512
-/**
- * Device only gets one connection. Because daemon.
- */
-#define MAX_CONNECTIONS (1UL)
-
-/**
- * Error which says that writing any more to packet buffer will overflow
- * outgoing buffer.
- */
-#define E_PACKET_OVERFLOW (1 << 0)
-
-/**
- * Device's view of connection to daemon.
- */
-struct connection_t {
- /**
- * Initially 0, non-zero when connection is open, 0 when connection
- * is closed again.
- */
- int is_live;
-
- /** Network descriptor that maps to daemon */
- int nd;
-
- /** Time last packet was reveived */
- time_t last_packet_sec;
-
- /** Incoming packet buffer */
- char inp_buf[MAX_PACKET_SIZE_IN];
-
- /** Incoming packet cursor */
- size_t inp_crs;
-
- /** Outgoing packet buffer */
- char outp_buf[MAX_PACKET_SIZE_OUT];
-
- /** Outgoing packet cursor */
- size_t outp_crs;
-
- /**
- * Next tlv to be returned via get_tlv. NULL initially and after last
- * element.
- * */
- struct tlv next_tlv;
-};
-
-#endif /* PROTOCOL_DEVICE_PRIVATE_H */ \ No newline at end of file