summaryrefslogtreecommitdiff
path: root/cesar/host/src/sci_msg.c
diff options
context:
space:
mode:
authorsave2008-04-07 14:17:42 +0000
committersave2008-04-07 14:17:42 +0000
commit3d58a62727346b7ac1a6cb36fed1a06ed72228dd (patch)
treed7788c3cf9f76426aef0286d0202e2097f0fa0eb /cesar/host/src/sci_msg.c
parent095dca4b0a8d4924093bab424f71f588fdd84613 (diff)
Moved the complete svn base into the cesar directory.
git-svn-id: svn+ssh://pessac/svn/cesar/trunk@1769 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/host/src/sci_msg.c')
-rw-r--r--cesar/host/src/sci_msg.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/cesar/host/src/sci_msg.c b/cesar/host/src/sci_msg.c
new file mode 100644
index 0000000000..f685c41eee
--- /dev/null
+++ b/cesar/host/src/sci_msg.c
@@ -0,0 +1,165 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2007 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+
+/**
+ * \file sci_msg.c
+ * \brief The sci message management functions
+ * \ingroup host
+ *
+ * This file provide sci message management functions
+ *
+ * \todo
+ */
+
+#include "common/std.h"
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include "host/sci.h"
+#ifndef UNIT_TEST
+#include "host/syscall.h"
+#else /* UNIT_TEST */
+#include <unistd.h>
+#endif /* UNIT_TEST */
+
+/**
+ * create a new sci message.
+ * \param size max size of message
+ * \return the new sci msg, NULL if failed, with errno
+ * - EINVAL is size < sizeof(sci_msg_hdr_t) or > SCI_MSG_MAX_SIZE
+ */
+sci_msg_t *sci_msg_new(int max_size)
+{
+ /* maybe will not be implemented */
+ return NULL;
+}
+
+/**
+ * sci message destruction with memory freeing.
+ * \param msg msg context to destroy
+ */
+void sci_msg_free(sci_msg_t *msg)
+{
+ /* maybe will not be implemented */
+ return;
+}
+
+/**
+ * sci message init if a static msg structure is used (instead of dynamic).
+ * \param msg pointer to msg context to initialize
+ * \param buffer pointer to a data buffer with allocated space
+ * \param max_size buffer max size
+ * \return 0 if ok, -1 if failed with errno=
+ * - EINVAL if msg of buffer is NULL, or if size is < sizeof(sci_msg_hdr_t) or >= SCI_MSG_MAX_SIZE
+ */
+int sci_msg_init(sci_msg_t *msg, unsigned char *buffer, int max_size)
+{
+ DBG_ASSERT(msg);
+ DBG_ASSERT(buffer);
+ DBG_ASSERT(max_size >= (int)sizeof(sci_msg_hdr_t));
+ DBG_ASSERT(max_size <= SCI_MSG_MAX_SIZE);
+ if((msg == NULL)
+ || (buffer == NULL)
+ || (max_size < (int)sizeof(sci_msg_hdr_t))
+ || (max_size > SCI_MSG_MAX_SIZE))
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* init structure */
+ /* structure is managed through push/pop calls;
+ * 'data_begin' points to the beginning accessible data, and is decremented while new data are pushed
+ * 'length' give current size of stored data
+ */
+ memset(msg, '\0', sizeof(sci_msg_t));
+ msg->data = buffer;
+ msg->max_size = max_size;
+ msg->data_begin = msg->data + max_size;
+ msg->data_end = msg->data_begin;
+ return 0;
+}
+
+/**
+ * sci message adding of new data with pointers update.
+ * \param msg msg where to add data
+ * \param length length of data to add
+ * \return length if ok, -1 if failed with errno=
+ * - EINVAL if msg is NULL
+ * - ENOSPC if length is bigger than free space into buffer
+ */
+int sci_msg_push(sci_msg_t *msg, int length)
+{
+ DBG_ASSERT(msg);
+ DBG_ASSERT(length >= 0);
+ if((msg == NULL)
+ || (length < 0))
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if(length > msg->max_size - msg->length)
+ {
+ errno = ENOSPC;
+ return -1;
+ }
+
+ msg->data_begin -= length;
+ msg->length += length;
+
+ return length;
+}
+
+/**
+ * sci message removing of data with pointers update.
+ * \param msg msg where to remove data
+ * \param length length of data to remove
+ * \return number of bytes stored, -1 if failed with errno=
+ * - EINVAL if msg or data is NULL
+ */
+int sci_msg_pop(sci_msg_t *msg, int length)
+{
+ int final_len;
+
+ DBG_ASSERT(msg);
+ DBG_ASSERT(length >= 0);
+ if((msg == NULL)
+ || (length < 0))
+ {
+ errno = EINVAL;
+ return -1;
+ }
+
+ if(length >= msg->length)
+ final_len = msg->length;
+ else
+ final_len = length;
+
+ msg->length -= final_len;
+ msg->data_begin += final_len;
+
+ return final_len;
+}
+
+void sci_msg_dump(sci_msg_t *msg, int fd, char *buffer, int size)
+{
+ if((msg == NULL)
+ || (fd < 0)
+ || (size <= 0))
+ return;
+ buffer[size - 1] = '\0';
+ snprintf(buffer, size - 1, "sci_msg(%p): sci=%p, hdr=%p ", msg, msg->sci_hdr, msg->hdr.generic);
+ write(fd, buffer, strlen(buffer));
+ snprintf(buffer, size - 1, "data=%p, begin=%p, end=%p, length=%d, max_size=%d\n",
+ msg->data, msg->data_begin, msg->data_end,
+ msg->length, msg->max_size);
+ write(fd, buffer, strlen(buffer));
+ return;
+}
+