Skip to content
Snippets Groups Projects
Commit 79e9c99b authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Internal changes in the base CComm class to improve the efficiency of the data handling.

parent bd7b17db
No related branches found
No related tags found
No related merge requests found
...@@ -33,9 +33,9 @@ ELSE(FTDI_INCLUDE_DIR AND FTDI_LIBRARY) ...@@ -33,9 +33,9 @@ ELSE(FTDI_INCLUDE_DIR AND FTDI_LIBRARY)
ENDIF(FTDI_INCLUDE_DIR AND FTDI_LIBRARY) ENDIF(FTDI_INCLUDE_DIR AND FTDI_LIBRARY)
# edit the following line to add all the source code files of the library # edit the following line to add all the source code files of the library
SET(sources ./comm.cpp ./commexceptions.cpp ./serial/rs232.cpp ./serial/rs232exceptions.cpp ./sockets/socket.cpp ./sockets/socketclient.cpp ./sockets/socketserver.cpp ./sockets/socketexceptions.cpp ./can/can.cpp) SET(sources ./comm.cpp ./cqueue.cpp ./commexceptions.cpp ./serial/rs232.cpp ./serial/rs232exceptions.cpp ./sockets/socket.cpp ./sockets/socketclient.cpp ./sockets/socketserver.cpp ./sockets/socketexceptions.cpp ./can/can.cpp)
# edit the following line to add all the header files of the library # edit the following line to add all the header files of the library
SET(headers ./comm.h ./commexceptions.h ./serial/rs232.h ./serial/rs232exceptions.h ./sockets/socket.h ./sockets/socketclient.h ./sockets/socketserver.h ./sockets/socketexceptions.h ./can/can.h) SET(headers ./comm.h ./cqueue.h ./commexceptions.h ./serial/rs232.h ./serial/rs232exceptions.h ./sockets/socket.h ./sockets/socketclient.h ./sockets/socketserver.h ./sockets/socketexceptions.h ./can/can.h)
IF(BUILD_FTDI) IF(BUILD_FTDI)
SET(sources ${sources} ./usb_ftdi/ftdiserver.cpp ./usb_ftdi/ftdimodule.cpp ./usb_ftdi/ftdiexceptions.cpp) SET(sources ${sources} ./usb_ftdi/ftdiserver.cpp ./usb_ftdi/ftdimodule.cpp ./usb_ftdi/ftdiexceptions.cpp)
......
...@@ -105,8 +105,7 @@ void CComm::config(void *config) ...@@ -105,8 +105,7 @@ void CComm::config(void *config)
int CComm::read(unsigned char *data,int len) int CComm::read(unsigned char *data,int len)
{ {
int num_available=0; int num_read=0;
int i=0;
if(data==NULL) if(data==NULL)
{ {
...@@ -118,17 +117,12 @@ int CComm::read(unsigned char *data,int len) ...@@ -118,17 +117,12 @@ int CComm::read(unsigned char *data,int len)
if(this->state==configured || this->state==sending) if(this->state==configured || this->state==sending)
{ {
this->access_comm.enter(); this->access_comm.enter();
num_available=this->receive_queue.size(); num_read=this->receive_queue.read(data,len);
if(len>num_available) if(len>num_read)
{ {
len=num_available; len=num_read;
this->event_server->reset_event(this->rx_event_id); this->event_server->reset_event(this->rx_event_id);
} }
for(i=0;i<len;i++)
{
data[i]=*this->receive_queue.begin();
this->receive_queue.pop_front();
}
this->access_comm.exit(); this->access_comm.exit();
return len; return len;
} }
...@@ -186,7 +180,7 @@ unsigned int CComm::get_num_data(void) ...@@ -186,7 +180,7 @@ unsigned int CComm::get_num_data(void)
if(this->state==configured || this->state==sending) if(this->state==configured || this->state==sending)
{ {
this->access_comm.enter(); this->access_comm.enter();
num=this->receive_queue.size(); num=this->receive_queue.get_num_data();
this->access_comm.exit(); this->access_comm.exit();
} }
...@@ -230,7 +224,7 @@ void CComm::close(void) ...@@ -230,7 +224,7 @@ void CComm::close(void)
throw; throw;
} }
/* flush the data queues */ /* flush the data queues */
this->receive_queue.erase(this->receive_queue.begin(),this->receive_queue.end()); this->receive_queue.flush();
/* change the current state */ /* change the current state */
this->state=created; this->state=created;
this->access_comm.exit(); this->access_comm.exit();
...@@ -271,7 +265,7 @@ void *CComm::comm_thread(void *param) ...@@ -271,7 +265,7 @@ void *CComm::comm_thread(void *param)
void CComm::on_receive(void) void CComm::on_receive(void)
{ {
unsigned char *data=NULL; unsigned char *data=NULL;
int num=0,num_read=0,i=0; int num=0,num_read=0;
if((num=this->hard_get_num_data())==-1) if((num=this->hard_get_num_data())==-1)
{ {
...@@ -295,8 +289,7 @@ void CComm::on_receive(void) ...@@ -295,8 +289,7 @@ void CComm::on_receive(void)
} }
else else
{ {
for(i=0;i<num;i++) this->receive_queue.write(data,num);
this->receive_queue.push_back(data[i]);
if(!this->event_server->event_is_set(this->rx_event_id)) if(!this->event_server->event_is_set(this->rx_event_id))
{ {
this->event_server->set_event(this->rx_event_id); this->event_server->set_event(this->rx_event_id);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "eventserver.h" #include "eventserver.h"
#include "threadserver.h" #include "threadserver.h"
#include "mutex.h" #include "mutex.h"
#include "cqueue.h"
typedef enum {created,configured,opened,sending} comm_states; typedef enum {created,configured,opened,sending} comm_states;
...@@ -135,7 +136,7 @@ class CComm ...@@ -135,7 +136,7 @@ class CComm
* function get_num_data() returns the number of bytes in this queue. By default, * function get_num_data() returns the number of bytes in this queue. By default,
* this queue is empty. * this queue is empty.
*/ */
std::list<unsigned char> receive_queue; CQueue receive_queue;
/** /**
* \brief current state of the communication device * \brief current state of the communication device
* *
......
#include "cqueue.h"
CQueue::CQueue()
{
this->size=QUEUE_DEFAULT_SIZE;
this->data=new unsigned char[this->size];
this->num_data=0;
this->read_pointer=0;
this->write_pointer=0;
}
CQueue::CQueue(int size)
{
this->size=size;
this->data=new unsigned char[this->size];
this->num_data=0;
this->read_pointer=0;
this->write_pointer=0;
}
void CQueue::resize(int new_size)
{
}
int CQueue::read(unsigned char *data,int len)
{
int num_data_to_end=0;
this->queue_access.enter();
if(len>this->num_data)
len=this->num_data;
num_data_to_end=this->size-this->read_pointer;
if(num_data_to_end<len)
{
memcpy(data,&this->data[read_pointer],num_data_to_end);
memcpy(&data[num_data_to_end],this->data,len-num_data_to_end);
this->read_pointer=len-num_data_to_end;
this->num_data-=len;
}
else
{
memcpy(data,&this->data[this->read_pointer],len);
this->read_pointer+=len;
this->num_data-=len;
}
this->queue_access.exit();
return len;
}
int CQueue::write(unsigned char *data,int len)
{
int num_data_to_end=0;
this->queue_access.enter();
if(len>(this->size-this->num_data))
len=this->size-this->num_data;
num_data_to_end=this->size-this->write_pointer;
if(num_data_to_end<len)
{
memcpy(&this->data[this->write_pointer],data,num_data_to_end);
memcpy(this->data,&data[num_data_to_end],len-num_data_to_end);
this->write_pointer=len-num_data_to_end;
this->num_data+=len;
}
else
{
memcpy(&this->data[this->write_pointer],data,len);
this->write_pointer+=len;
this->num_data+=len;
}
this->queue_access.exit();
return len;
}
int CQueue::get_num_data(void)
{
return this->num_data;
}
bool CQueue::is_empty(void)
{
if(this->num_data==0)
return true;
else
return false;
}
bool CQueue::is_full(void)
{
if(this->num_data==this->size)
return true;
else
return false;
}
void CQueue::flush(void)
{
this->queue_access.enter();
this->read_pointer=0;
this->write_pointer=0;
this->num_data=0;
this->queue_access.exit();
}
CQueue::~CQueue()
{
if(this->data!=NULL)
{
delete[] this->data;
this->data=NULL;
}
this->num_data=0;
this->read_pointer=0;
this->write_pointer=0;
}
#ifndef _CQUEUE_H
#define _CQUEUE_H
#include <string.h>
#include "mutex.h"
#define QUEUE_DEFAULT_SIZE 1024
class CQueue
{
private:
int size;
int num_data;
int read_pointer;
int write_pointer;
unsigned char *data;
CMutex queue_access;
public:
CQueue();
CQueue(int size);
void resize(int new_size);
int read(unsigned char *data,int len);
int write(unsigned char *data,int len);
int get_num_data(void);
bool is_empty(void);
bool is_full(void);
void flush(void);
~CQueue();
};
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment