diff options
author | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2011-09-26 18:06:29 +0200 |
---|---|---|
committer | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2011-09-26 18:06:29 +0200 |
commit | fb45ecc3f3fba699e2cfe7892a96b783c2128fc9 (patch) | |
tree | a89ac27424561179d3b563e504cd507d1da8fce9 | |
parent | bd41c67478a0ecb745c20510eab6228398ea59ea (diff) | |
download | DLT-daemon-fb45ecc3f3fba699e2cfe7892a96b783c2128fc9.tar.gz |
Shared memory now used until client connects.
-rw-r--r-- | include/dlt/dlt_shm.h | 2 | ||||
-rwxr-xr-x | src/daemon/dlt-daemon.c | 23 | ||||
-rw-r--r-- | src/shared/dlt_shm.c | 125 |
3 files changed, 143 insertions, 7 deletions
diff --git a/include/dlt/dlt_shm.h b/include/dlt/dlt_shm.h index aca92c6..5b7d335 100644 --- a/include/dlt/dlt_shm.h +++ b/include/dlt/dlt_shm.h @@ -86,6 +86,8 @@ extern int dlt_shm_init_server(DltShm *buf,int key,int size); extern int dlt_shm_push(DltShm *buf,const unsigned char *data1,unsigned int size1,const unsigned char *data2,unsigned int size2,const unsigned char *data3,unsigned int size3); extern int dlt_shm_pull(DltShm *buf,unsigned char *data, int size); +extern int dlt_shm_copy(DltShm *buf,unsigned char *data, int size); +extern int dlt_shm_remove(DltShm *buf); extern void dlt_shm_info(DltShm *buf); extern void dlt_shm_status(DltShm *buf); diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index 90f2732..9cb8922 100755 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -479,8 +479,6 @@ int main(int argc, char* argv[]) return -1; } - //return(0); - /* Initialize logging facility */ dlt_log_init(daemon_local.flags.dflag); @@ -1771,7 +1769,7 @@ int dlt_daemon_process_user_message_log(DltDaemon *daemon, DltDaemonLocal *daemo } //dlt_shm_status(&(daemon_local->dlt_shm)); - while ( (size = dlt_shm_pull(&(daemon_local->dlt_shm),rcv_buffer,10000)) > 0) + while ( (size = dlt_shm_copy(&(daemon_local->dlt_shm),rcv_buffer,10000)) > 0) { if (dlt_message_read(&(daemon_local->msg),rcv_buffer,size,0,verbose)==0) { @@ -1784,6 +1782,7 @@ int dlt_daemon_process_user_message_log(DltDaemon *daemon, DltDaemonLocal *daemo if (dlt_message_set_extraparameters(&(daemon_local->msg),0)==-1) { dlt_log(LOG_ERR,"Can't set message extra parameters in process user message log\n"); + dlt_shm_remove(&(daemon_local->dlt_shm)); return -1; } @@ -1797,6 +1796,7 @@ int dlt_daemon_process_user_message_log(DltDaemon *daemon, DltDaemonLocal *daemo if (dlt_set_storageheader(daemon_local->msg.storageheader,daemon_local->msg.headerextra.ecu)==-1) { dlt_log(LOG_ERR,"Can't set storage header in process user message log\n"); + dlt_shm_remove(&(daemon_local->dlt_shm)); return -1; } } @@ -1805,11 +1805,12 @@ int dlt_daemon_process_user_message_log(DltDaemon *daemon, DltDaemonLocal *daemo if (dlt_set_storageheader(daemon_local->msg.storageheader,daemon->ecuid)==-1) { dlt_log(LOG_ERR,"Can't set storage header in process user message log\n"); + dlt_shm_remove(&(daemon_local->dlt_shm)); return -1; } } - /* if no filter set or filter is matching display message */ + /* display message */ if (daemon_local->flags.xflag) { if (dlt_message_print_hex(&(daemon_local->msg),text,DLT_DAEMON_TEXTSIZE,verbose)==-1) @@ -1903,16 +1904,24 @@ int dlt_daemon_process_user_message_log(DltDaemon *daemon, DltDaemonLocal *daemo /* Message was not sent to client, so store it in client ringbuffer */ if (sent==0) { - if (dlt_ringbuffer_put3(&(daemon->client_ringbuffer), + /* dlt message was not sent, keep in buffer */ + break; + + /*if (dlt_ringbuffer_put3(&(daemon->client_ringbuffer), daemon_local->msg.headerbuffer+sizeof(DltStorageHeader),daemon_local->msg.headersize-sizeof(DltStorageHeader), daemon_local->msg.databuffer,daemon_local->msg.datasize, 0, 0 )<0) { dlt_log(LOG_ERR,"Storage of message in history buffer failed! Message discarded.\n"); - } + }*/ } - + else + { + /* dlt message was sent, remove from buffer */ + dlt_shm_remove(&(daemon_local->dlt_shm)); + } + /* keep not read data in buffer */ /*bytes_to_be_removed = daemon_local->msg.headersize+daemon_local->msg.datasize-sizeof(DltStorageHeader)+sizeof(DltUserHeader); if (daemon_local->msg.found_serialheader) diff --git a/src/shared/dlt_shm.c b/src/shared/dlt_shm.c index 709a389..853dc2d 100644 --- a/src/shared/dlt_shm.c +++ b/src/shared/dlt_shm.c @@ -368,6 +368,131 @@ int dlt_shm_pull(DltShm *buf,unsigned char *data, int max_size) return size; // OK } +int dlt_shm_copy(DltShm *buf,unsigned char *data, int max_size) +{ + int write, read, count, size; + unsigned char status; + + if(!buf->mem) { + // shm not initialised + printf("SHM not initialised\n"); + return -1; /* ERROR */ + } + + // get current write pointer + DLT_SHM_SEM_GET(buf->semid); + write = ((int*)(buf->shm))[0]; + read = ((int*)(buf->shm))[1]; + count = ((int*)(buf->shm))[2]; + DLT_SHM_SEM_FREE(buf->semid); + + // check if data is in there + if(count<=0) { + //printf("SHM is empty\n"); + return -1; // ERROR + } + + // check if end of buffer is reached and read status and size + if((read+sizeof(unsigned char)+sizeof(int)) <= buf->size) { + status = *((unsigned char*)(buf->mem+read)); + size = *((int*)(buf->mem+read+sizeof(unsigned char))); + if(status == 0) { + // data fits not end of shm + read = 0; + status = *((unsigned char*)(buf->mem+read)); + size = *((int*)(buf->mem+read+sizeof(unsigned char))); + } + } + else { + read = 0; + status = *((unsigned char*)(buf->mem+read)); + size = *((int*)(buf->mem+read+sizeof(unsigned char))); + } + + // check status + if(status != 2 ) { + //printf("Buffer is not fully written\n"); + return -1; // ERROR + } + + // plausibility check of buffer size + if( (read+size) > buf->size) { + printf("Buffers size bigger than shm buffer\n"); + return -1; // ERROR + } + + // check max read size + if(size > max_size) { + printf("Buffer is bigger than max size\n"); + return -1; // ERROR + } + + // copy data + memcpy(data,buf->mem+read+sizeof(unsigned char)+sizeof(int),size); + + return size; // OK +} + +int dlt_shm_remove(DltShm *buf) +{ + int write, read, count, size; + unsigned char status; + + if(!buf->mem) { + // shm not initialised + printf("SHM not initialised\n"); + return -1; /* ERROR */ + } + + // get current write pointer + DLT_SHM_SEM_GET(buf->semid); + write = ((int*)(buf->shm))[0]; + read = ((int*)(buf->shm))[1]; + count = ((int*)(buf->shm))[2]; + DLT_SHM_SEM_FREE(buf->semid); + + // check if data is in there + if(count<=0) { + //printf("SHM is empty\n"); + return -1; // ERROR + } + + // check if end of buffer is reached and read status and size + if((read+sizeof(unsigned char)+sizeof(int)) <= buf->size) { + status = *((unsigned char*)(buf->mem+read)); + size = *((int*)(buf->mem+read+sizeof(unsigned char))); + if(status == 0) { + // data fits not end of shm + read = 0; + status = *((unsigned char*)(buf->mem+read)); + size = *((int*)(buf->mem+read+sizeof(unsigned char))); + } + } + else { + read = 0; + status = *((unsigned char*)(buf->mem+read)); + size = *((int*)(buf->mem+read+sizeof(unsigned char))); + } + + // check status + if(status != 2 ) { + //printf("Buffer is not fully written\n"); + return -1; // ERROR + } + + // plausibility check of buffer size + if( (read+size) > buf->size) { + printf("Buffers size bigger than shm buffer\n"); + return -1; // ERROR + } + + // update buffer pointers + ((int*)(buf->shm))[1] = read+sizeof(unsigned char)+sizeof(int)+size; // set new read pointer + ((int*)(buf->shm))[2] -= 1; // decrease counter + + return size; // OK +} + int dlt_shm_free_server(DltShm *buf) { if(!buf->shm) { |