diff options
author | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2011-10-18 15:21:39 +0200 |
---|---|---|
committer | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2011-10-18 15:21:39 +0200 |
commit | bd907a3a892ab13d8d2f781a0536d46b4cf3b12b (patch) | |
tree | b41ccfe188ce92b92ecdf42085ed36c728afd6f5 /src | |
parent | 30606fb6cfb984baf5782793effd70d07ce68195 (diff) | |
download | DLT-daemon-bd907a3a892ab13d8d2f781a0536d46b4cf3b12b.tar.gz |
[Reverted] Removed Ringbuffer implementation in Library.
Diffstat (limited to 'src')
-rw-r--r-- | src/examples/dlt-example-filetransfer.c | 4 | ||||
-rwxr-xr-x | src/lib/dlt_user.c | 81 | ||||
-rwxr-xr-x | src/lib/dlt_user_cfg.h | 3 | ||||
-rwxr-xr-x | src/shared/dlt_common.c | 463 | ||||
-rwxr-xr-x | src/tests/dlt-test-internal.c | 119 |
5 files changed, 661 insertions, 9 deletions
diff --git a/src/examples/dlt-example-filetransfer.c b/src/examples/dlt-example-filetransfer.c index 4c6b12b..41e926f 100644 --- a/src/examples/dlt-example-filetransfer.c +++ b/src/examples/dlt-example-filetransfer.c @@ -1,5 +1,5 @@ -#include "dlt/dlt_filetransfer.h" /*Needed for transferring files with the dlt protocol*/ -#include "dlt/dlt.h" /*Needed for dlt logging*/ +#include <dlt_filetransfer.h> /*Needed for transferring files with the dlt protocol*/ +#include <dlt.h> /*Needed for dlt logging*/ #define MAXSTRLEN 1024 diff --git a/src/lib/dlt_user.c b/src/lib/dlt_user.c index 459a5d1..a031de3 100755 --- a/src/lib/dlt_user.c +++ b/src/lib/dlt_user.c @@ -320,6 +320,12 @@ int dlt_init_common(void) dlt_user.dlt_ll_ts_max_num_entries = 0; dlt_user.dlt_ll_ts_num_entries = 0; + if (dlt_ringbuffer_init(&(dlt_user.rbuf), DLT_USER_RINGBUFFER_SIZE)==-1) + { + dlt_user_initialised = 0; + return -1; + } + signal(SIGPIPE,SIG_IGN); /* ignore pipe signals */ atexit(dlt_user_atexit_handler); @@ -377,6 +383,9 @@ int dlt_free(void) /* Ignore return value */ dlt_receiver_free(&(dlt_user.receiver)); + /* Ignore return value */ + dlt_ringbuffer_free(&(dlt_user.rbuf)); + if (dlt_user.dlt_ll_ts) { for (i=0;i<dlt_user.dlt_ll_ts_max_num_entries;i++) @@ -876,8 +885,16 @@ int dlt_forward_msg(void *msgdata,size_t size) /* store message in ringbuffer, if an error has occured */ if (ret!=DLT_RETURN_OK) { - /* message could not be sent */ - /* in old implementation messages was saved in ringbuffer */ + DLT_SEM_LOCK(); + + if (dlt_ringbuffer_put3(&(dlt_user.rbuf), + &(userheader), sizeof(DltUserHeader), + msgdata, size, 0, 0)==-1) + { + dlt_log(LOG_ERR,"Storing message to history buffer failed! Message discarded.\n"); + } + + DLT_SEM_FREE(); } switch (ret) @@ -2185,8 +2202,17 @@ int dlt_user_log_send_log(DltContextData *log, int mtype) /* store message in ringbuffer, if an error has occured */ if (ret!=DLT_RETURN_OK) { - /* in old implementation message was stored in ringbuffer - * if it was not able to be sent. */ + DLT_SEM_LOCK(); + + if (dlt_ringbuffer_put3(&(dlt_user.rbuf), + &(userheader), sizeof(DltUserHeader), + msg.headerbuffer+sizeof(DltStorageHeader), msg.headersize-sizeof(DltStorageHeader), + log->buffer, log->size)==-1) + { + dlt_log(LOG_ERR,"Storing message to history buffer failed! Message discarded.\n"); + } + + DLT_SEM_FREE(); } switch (ret) @@ -2727,10 +2753,14 @@ int dlt_user_log_check_user_message(void) void dlt_user_log_reattach_to_daemon(void) { - int num, reregistered=0; + int num, count, reregistered=0; + + uint8_t buf[DLT_USER_RINGBUFFER_SIZE]; + size_t size; DltContext handle; DltContextData log_new; + DltReturnValue ret; if (dlt_user.dlt_log_handle<0) { @@ -2783,8 +2813,45 @@ void dlt_user_log_reattach_to_daemon(void) if (reregistered==1) { - /* In old implementation Send content of ringbuffer */ - } + /* Send content of ringbuffer */ + DLT_SEM_LOCK(); + count = dlt_user.rbuf.count; + DLT_SEM_FREE(); + + for (num=0;num<count;num++) + { + + DLT_SEM_LOCK(); + dlt_ringbuffer_get(&(dlt_user.rbuf),buf,&size); + DLT_SEM_FREE(); + + if (size>0) + { + dlt_shm_push(&dlt_user.dlt_shm,buf+sizeof(DltUserHeader),size-sizeof(DltUserHeader),0,0,0,0); + + /* log to FIFO */ + ret = dlt_user_log_out3(dlt_user.dlt_log_handle, buf,sizeof(DltUserHeader),0,0,0,0); + + /* in case of error, push message back to ringbuffer */ + if (ret!=DLT_RETURN_OK) + { + DLT_SEM_LOCK(); + if (dlt_ringbuffer_put(&(dlt_user.rbuf), buf, size)==-1) + { + dlt_log(LOG_ERR,"Error pushing back message to history buffer. Message discarded.\n"); + } + DLT_SEM_FREE(); + + /* In case of: data could not be written, set overflow flag */ + if (ret==DLT_RETURN_PIPE_FULL) + { + dlt_user.overflow = 1; + } + } + } + + } + } } } } diff --git a/src/lib/dlt_user_cfg.h b/src/lib/dlt_user_cfg.h index 419fbc2..5b1e71e 100755 --- a/src/lib/dlt_user_cfg.h +++ b/src/lib/dlt_user_cfg.h @@ -87,6 +87,9 @@ /* Size of receive buffer */
#define DLT_USER_RCVBUF_MAX_SIZE 10024
+/* Size of ring buffer */
+#define DLT_USER_RINGBUFFER_SIZE 10024
+
/* Temporary buffer length */
#define DLT_USER_BUFFER_LENGTH 255
diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index d24527b..4ac7dbe 100755 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -2231,6 +2231,469 @@ int dlt_check_storageheader(DltStorageHeader *storageheader) (storageheader->pattern[3] == 1)); } +int dlt_ringbuffer_init(DltRingBuffer *dltbuf, uint32_t size) +{ + + if (dltbuf==0) + { + return -1; + } + + if (size<=sizeof(uint32_t)) + { + return -1; + } + + dltbuf->buffer=(char*)malloc(size); + if (dltbuf->buffer==0) + { + return -1; + } + + dltbuf->size=size; + + dltbuf->pos_write=0; + dltbuf->pos_read=0; + + dltbuf->count=0; + + return 0; +} + +int dlt_ringbuffer_free(DltRingBuffer *dltbuf) +{ + + if (dltbuf==0) + { + return -1; + } + + if (dltbuf->buffer) + { + free(dltbuf->buffer); + } + + dltbuf->buffer=0; + + dltbuf->size=0; + + dltbuf->pos_write=0; + dltbuf->pos_read=0; + + dltbuf->count=0; + + return 0; +} + +int dlt_ringbuffer_put(DltRingBuffer *dltbuf, void *data, uint32_t size) +{ + uint32_t sui, part1, part2; + + if (dltbuf==0) + { + return -1; + } + + if (dltbuf->buffer==0) + { + return -1; + } + + if (data==0) + { + return -1; + } + + sui = sizeof(uint32_t); + + if ((size+sui)>dltbuf->size) + { + return -1; + } + + dlt_ringbuffer_checkandfreespace(dltbuf, (size+sui)); + + if (dltbuf->pos_write >= dltbuf->size) + { + dltbuf->pos_write = 0; + } + + /* Not enough space for one uint available before end of linear buffer */ + /* Start at begin of linear buffer */ + if ((dltbuf->size - dltbuf->pos_write) < sui) + { + dltbuf->pos_write = 0; + } + + /* Write length of following data to buffer */ + memcpy(&(dltbuf->buffer[dltbuf->pos_write]), &size, sui); + dltbuf->pos_write+=sui; + + if (dltbuf->pos_write >= dltbuf->size) + { + dltbuf->pos_write = 0; + } + + if ((dltbuf->size - dltbuf->pos_write) < size) + { + /* Not enough space til end of linear buffer, */ + /* split up write call */ + part1 = dltbuf->size - dltbuf->pos_write; + part2 = size - part1; + + memcpy(dltbuf->buffer + dltbuf->pos_write, data, part1); + memcpy(dltbuf->buffer, ((char*)data) + part1, part2); + dltbuf->pos_write = part2; + + } + else + { + /* Enough space til end of linear buffer */ + memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data, size); + dltbuf->pos_write+=size; + } + + dltbuf->count++; + + return 0; +} + + +int dlt_ringbuffer_put3(DltRingBuffer *dltbuf, void *data1, uint32_t size1, void *data2, uint32_t size2, void *data3, uint32_t size3) +{ + uint32_t sui, part1, part2; + uint32_t total_size; + + if (dltbuf==0) + { + return -1; + } + + if (dltbuf->buffer==0) + { + return -1; + } + + sui = sizeof(uint32_t); + + total_size = size1+size2+size3; + + if ((total_size+sui)>dltbuf->size) + { + return -1; + } + + dlt_ringbuffer_checkandfreespace(dltbuf, (total_size+sui)); + + if (dltbuf->pos_write >= dltbuf->size) + { + dltbuf->pos_write = 0; + } + + /* Not enough space for one uint available before end of linear buffer */ + /* Start at begin of linear buffer */ + if ((dltbuf->size - dltbuf->pos_write) < sui) + { + dltbuf->pos_write = 0; + } + + /* Write length of following data to buffer */ + memcpy(&(dltbuf->buffer[dltbuf->pos_write]), &total_size, sui); + dltbuf->pos_write+=sui; + + if (dltbuf->pos_write >= dltbuf->size) + { + dltbuf->pos_write = 0; + } + + /* First chunk of data (data1, size1) */ + if ((dltbuf->size - dltbuf->pos_write) < size1) + { + /* Not enough space til end of linear buffer, */ + /* split up write call */ + part1 = dltbuf->size - dltbuf->pos_write; + part2 = size1 - part1; + + memcpy(dltbuf->buffer + dltbuf->pos_write, data1, part1); + memcpy(dltbuf->buffer, ((char*)data1) + part1, part2); + dltbuf->pos_write = part2; + + } + else + { + /* Enough space til end of linear buffer */ + memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data1, size1); + dltbuf->pos_write+=size1; + } + + if (dltbuf->pos_write >= dltbuf->size) + { + dltbuf->pos_write = 0; + } + + /* Second chunk of data (data2, size2) */ + if ((dltbuf->size - dltbuf->pos_write) < size2) + { + /* Not enough space til end of linear buffer, */ + /* split up write call */ + part1 = dltbuf->size - dltbuf->pos_write; + part2 = size2 - part1; + + memcpy(dltbuf->buffer + dltbuf->pos_write, data2, part1); + memcpy(dltbuf->buffer, ((char*)data2) + part1, part2); + dltbuf->pos_write = part2; + + } + else + { + /* Enough space til end of linear buffer */ + memcpy(&(dltbuf->buffer[dltbuf->pos_write]), data2, size2); + dltbuf->pos_write+=size2; + } + + if (dltbuf->pos_write >= dltbuf->size) + { + dltbuf->pos_write = 0; + } + + /* Third chunk of data (data3, size3) */ + if ((dltbuf->size - dltbuf->pos_write) < size3) + { + /* Not enough space til end of linear buffer, */ + /* split up write call */ + part1 = dltbuf->size - dltbuf->pos_write; + part2 = size3 - part1; + + memcpy(dltbuf->buffer + dltbuf->pos_write, data3, part1); + memcpy(dltbuf->buffer, ((char*)data3) + part1, part2); + dltbuf->pos_write = part2; + + } + else + { + /* Enough space til end of linear buffer */ + memcpy(dltbuf->buffer + dltbuf->pos_write, data3, size3); + dltbuf->pos_write+=size3; + } + + dltbuf->count++; + + return 0; +} + +int dlt_ringbuffer_get(DltRingBuffer *dltbuf, void *data, size_t *size) +{ + uint32_t tmpsize=0; + uint32_t sui; + + uint32_t part1, part2; + + if (dltbuf==0) + { + return -1; + } + + if (dltbuf->buffer==0) + { + return -1; + } + + if (dltbuf->count==0) + { + return -1; + } + + sui = sizeof(uint32_t); + + if (dltbuf->pos_read >= dltbuf->size) + { + dltbuf->pos_read = 0; + } + + if ((dltbuf->size - dltbuf->pos_read) < sui) + { + dltbuf->pos_read = 0; + } + + /* printf("Reading at offset: %d\n", dltbuf->pos_read); */ + + memcpy(&tmpsize,&(dltbuf->buffer[dltbuf->pos_read]), sui); + dltbuf->pos_read += sui; + + if (dltbuf->pos_read >= dltbuf->size) + { + dltbuf->pos_read = 0; + } + + if ((tmpsize>0) && ((tmpsize+sizeof(uint32_t))<=dltbuf->size)) + { + if ((dltbuf->size - dltbuf->pos_read) < tmpsize) + { + /* Not enough space til end of linear buffer, */ + /* split up read call */ + part1 = dltbuf->size - dltbuf->pos_read; + part2 = tmpsize - part1; + + memcpy(data, dltbuf->buffer + dltbuf->pos_read, part1); + memcpy(((char*)data)+part1, dltbuf->buffer, part2); + dltbuf->pos_read = part2; + } + else + { + /* Enough space til end of linear buffer */ + /* no split up read call */ + memcpy(data, &(dltbuf->buffer[dltbuf->pos_read]), tmpsize); + dltbuf->pos_read+=tmpsize; + } + *size = tmpsize; + } + else + { + data=0; + *size=0; + } + + dltbuf->count--; + + return 0; +} + +int dlt_ringbuffer_get_skip(DltRingBuffer *dltbuf) +{ + uint32_t tmpsize=0; + uint32_t sui; + + uint32_t part1, part2; + + if (dltbuf==0) + { + return -1; + } + + if (dltbuf->buffer==0) + { + return -1; + } + + if (dltbuf->count==0) + { + return -1; + } + + sui = sizeof(uint32_t); + + if (dltbuf->pos_read >= dltbuf->size) + { + dltbuf->pos_read = 0; + } + + if ((dltbuf->size - dltbuf->pos_read) < sui) + { + dltbuf->pos_read = 0; + } + + memcpy(&tmpsize,&(dltbuf->buffer[dltbuf->pos_read]), sui); + dltbuf->pos_read += sui; + + if (dltbuf->pos_read >= dltbuf->size) + { + dltbuf->pos_read = 0; + } + + if ((tmpsize>0) && ((tmpsize+sui)<=dltbuf->size)) + { + if ((dltbuf->size - dltbuf->pos_read) < tmpsize) + { + /* Not enough space til end of linear buffer */ + part1 = dltbuf->size - dltbuf->pos_read; + part2 = tmpsize - part1; + + dltbuf->pos_read = part2; + } + else + { + /* Enough space til end of linear buffer */ + dltbuf->pos_read+=tmpsize; + } + } + + dltbuf->count--; + + return 0; +} + +int dlt_ringbuffer_freespacewrite(DltRingBuffer *dltbuf, uint32_t *freespace) +{ + if ((dltbuf==0) || (freespace==0)) + { + return -1; + } + + *freespace=0; + + /* Space til pos_read */ + if (dltbuf->pos_read > dltbuf->pos_write) + { + *freespace=(dltbuf->pos_read - dltbuf->pos_write); + return 0; + } + else if (dltbuf->pos_read < dltbuf->pos_write) + { + *freespace=(dltbuf->size - dltbuf->pos_write + dltbuf->pos_read ); + return 0; + } + else + { + if (dltbuf->count) + { + return 0; + } + else + { + *freespace=dltbuf->size; + return 0; + } + } + return 0; +} + +int dlt_ringbuffer_checkandfreespace(DltRingBuffer *dltbuf, uint32_t reqspace) +{ + uint32_t space_left; + + if (dltbuf==0) + { + return -1; + } + + if (dlt_ringbuffer_freespacewrite(dltbuf,&space_left) == -1) + { + return -1; + } + + /* printf("Now reading at: %d, space_left = %d, req = %d, r=%d, w=%d, count=%d \n", + dltbuf->pos_read,space_left, reqspace, dltbuf->pos_read, dltbuf->pos_write, dltbuf->count); */ + + while (space_left<reqspace) + { + /* Overwrite, correct read position */ + + /* Read and skip one element */ + dlt_ringbuffer_get_skip(dltbuf); + + /* Space until pos_read */ + if (dlt_ringbuffer_freespacewrite(dltbuf,&space_left) == -1) + { + return -1; + } + + /* printf("Overwrite: Now reading at: %d, space_left = %d, req = %d, r=%d, w=%d, count=%d \n", + dltbuf->pos_read,space_left, reqspace, dltbuf->pos_read, dltbuf->pos_write, dltbuf->count); */ + } + + return 0; +} + #if !defined (__WIN32__) int dlt_setup_serial(int fd, speed_t speed) diff --git a/src/tests/dlt-test-internal.c b/src/tests/dlt-test-internal.c index 02142b9..00209f1 100755 --- a/src/tests/dlt-test-internal.c +++ b/src/tests/dlt-test-internal.c @@ -179,6 +179,11 @@ int main(int argc, char* argv[]) return -1; } + if (test[0]) + { + internal1(); + } + printf("\n"); printf("%d tests passed\n",tests_passed); printf("%d tests failed\n",tests_failed); @@ -186,3 +191,117 @@ int main(int argc, char* argv[]) return 0; } +void internal1(void) +{ + int index,result_index; + unsigned int c; + unsigned int size; + + char buf[1024],result[1024]; + + DltRingBuffer mybuf; + + printf("Test1i: Ringbuffer, writing and reading \n"); + + for (size=8;size<=30;size++) + { + + dlt_ringbuffer_init(&mybuf, size); + + memset(result,0,1024); + + if (vflag) + { + printf("\nRingbuffer Size = %d \n\n",size); + } + + /* Write several times to ringbuffer */ + for (index=0; index<6; index++) + { + memset(buf,0,1024); + + sprintf(buf,"%d",index); + dlt_ringbuffer_put(&mybuf,buf,strlen(buf)); + + if (vflag) + { + printf("W[%d], Bytes = %d, Hex: ", index, strlen(buf)); + dlt_print_hex((uint8_t *)buf, strlen(buf)); + printf("\n"); + } + } + + if (vflag) + { + printf("\nCount=%d, Max. by buffer size %d = %d\n",mybuf.count, size, (int)(size/(strlen(buf)+sizeof(unsigned int)))); + } + + /* Check value of mybuf.count, counting the elements in ringbuffer */ + if (mybuf.count!=(int)(size/(strlen(buf)+sizeof(unsigned int)))) + { + tests_failed++; + printf("Test1i FAILED\n"); + + break; + } + + result_index = 0; + + /* Read several times from ringbuffer */ + for (index=0; index<6; index++) + { + memset(buf,0,1024); + + if (dlt_ringbuffer_get(&mybuf,buf,&c)!=-1) + { + if (vflag) + { + printf("R[%d], Bytes = %d, Hex: ", index, c); + dlt_print_hex((uint8_t *)buf, c); + printf("\n"); + } + + if (c==1) + { + result[result_index] = buf[0]; + } + result_index++; + } + } + + /* Check value of mybuf.count, counting the elements in ringbuffer, must be 0 now */ + if (mybuf.count!=0) + { + tests_failed++; + printf("Test1i FAILED\n"); + + dlt_ringbuffer_free(&mybuf); + return; + } + + /* Check the read elements */ + for (index=0; index<result_index; index++) + { + sprintf(buf,"%d",((6-result_index)+index)); + if (result[index]!=buf[0]) + { + tests_failed++; + printf("Test1i FAILED\n"); + + dlt_ringbuffer_free(&mybuf); + return; + } + } + + if (vflag) + { + printf("\n"); + } + + dlt_ringbuffer_free(&mybuf); + } + + tests_passed++; + printf("Test1i PASSED\n"); +} + |