summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common/protocol.c109
-rw-r--r--src/common/protocol_private.h17
-rw-r--r--src/common/utils.c8
-rwxr-xr-xsrc/device/protocol_device.ino38
-rwxr-xr-xsrc/device/protocol_device_private.h37
5 files changed, 132 insertions, 77 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)
{
diff --git a/src/common/protocol_private.h b/src/common/protocol_private.h
index b74c0eb..eecbc15 100644
--- a/src/common/protocol_private.h
+++ b/src/common/protocol_private.h
@@ -37,7 +37,8 @@
* */
#define PACKET_MAX_SIZE (512)
-/** Returns the amount of tlv's int a packet.
+/**
+ * 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.
@@ -73,17 +74,17 @@ size_t get_tlvs( const struct packet_data * const data,
* @return Returns 0 on success. Otherwise: E_PACKET_OVERFLOW.
*
* */
-int push_string(char *str);
+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(char *num);
+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(char *data);
+int push_timestamp(cd_t connection, char *data);
/**
* Pushes a request for daemon to repeat a message identified by a msg_index_t.
@@ -93,7 +94,7 @@ int push_timestamp(char *data);
* @return 0 on success or E_PACKET_OVERFLOW, if not enough space is available
* to push all the data.
*/
-int push_request(char *data);
+int push_request(cd_t connection, char *data);
/**
* Pushes a message to the outgoing packet buffer as a reply. A reply is just
@@ -105,16 +106,16 @@ int push_request(char *data);
* @return On success --- 0 or E_PACKET_OVERFLOW, if not enough buffer is
* available.
*/
-int push_reply(char *data);
+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(char *data);
+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(enum tlv_type type, size_t size);
+int push_tlv_header(cd_t connection, enum tlv_type type, size_t size);
#endif /* PROTOCOL_H_PRIVATE */
diff --git a/src/common/utils.c b/src/common/utils.c
index 956ef1b..0619517 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -6,22 +6,22 @@ int cmp_uuid(uuid_t *first, uuid_t *second)
return memcmp(first, second, sizeof(*first));
}
-fpi1_t add(fpi1_t a, fpi1_t b)
+fpi1_t fpi1_add(fpi1_t a, fpi1_t b)
{
return a + b;
}
-fpi1_t sub(fpi1_t a, fpi1_t b)
+fpi1_t fpi1_sub(fpi1_t a, fpi1_t b)
{
return a - b;
}
-fpi1_t mul(fpi1_t a, fpi1_t b)
+fpi1_t fpi1_mul(fpi1_t a, fpi1_t b)
{
return (fpi1_t)(((long)a * (long)b) / 100);
}
-fpi1_t div(fpi1_t a, fpi1_t b)
+fpi1_t fpi1_div(fpi1_t a, fpi1_t b)
{
return (fpi1_t)(((long)a * 10) / ((long)b * 10));
}
diff --git a/src/device/protocol_device.ino b/src/device/protocol_device.ino
new file mode 100755
index 0000000..778196d
--- /dev/null
+++ b/src/device/protocol_device.ino
@@ -0,0 +1,38 @@
+#include "protocol.h"
+#include "protocol_private.h"
+#include "protocol_device_private.h"
+#include <ESP8266WiFi.h>
+#include <time.h>
+#include <stdlib.h>
+
+static struct connection_t connection;
+
+static int push_bytes(cd_t cd, 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 {
+
+ }
+ return ret;
+}
+
+static int push_tlv_header(cd_t connection, enum tlv_type type, size_t size)
+{
+ return 0;
+}
+
+struct tlv * get_tlv()
+{
+ return NULL;
+}
+
+
+void get_last_data(cd_t connection)
+{
+
+}
diff --git a/src/device/protocol_device_private.h b/src/device/protocol_device_private.h
new file mode 100755
index 0000000..0fb46c7
--- /dev/null
+++ b/src/device/protocol_device_private.h
@@ -0,0 +1,37 @@
+#ifndef PROTOCOL_DEVICE_PRIVATE_H
+#define PROTOCOL_DEVICE_PRIVATE_H
+
+#define MAX_PACKET_SIZE_OUT (64)
+#define MAX_PACKET_SIZE_IN (512)
+#define MAX_CONNECTIONS (1UL)
+
+/**
+ * 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;
+
+ /** Daemon IP adress */
+ IPAddress address;
+
+ /** 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;
+};
+
+#endif /* PROTOCOL_DEVICE_PRIVATE_H */ \ No newline at end of file