From 3d58a62727346b7ac1a6cb36fed1a06ed72228dd Mon Sep 17 00:00:00 2001 From: save Date: Mon, 7 Apr 2008 14:17:42 +0000 Subject: Moved the complete svn base into the cesar directory. git-svn-id: svn+ssh://pessac/svn/cesar/trunk@1769 017c9cb6-072f-447c-8318-d5b54f68fe89 --- cesar/host/src/sci_msg.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 cesar/host/src/sci_msg.c (limited to 'cesar/host/src/sci_msg.c') 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 + * + * <<>> + * + * }}} */ + +/** + * \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 +#include +#include +#include "host/sci.h" +#ifndef UNIT_TEST +#include "host/syscall.h" +#else /* UNIT_TEST */ +#include +#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; +} + -- cgit v1.2.3