connectionDevice.cpp

00001 /*
00002   デバイス接続のインターフェース
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "connectionDevice.h"
00008 
00009 
00010 ConnectionDevice::ConnectionDevice(int buffer_size)
00011   : device_name(""), baudrate_value(0),
00012     recv_buffer(new RingBuffer<char>(buffer_size)) {
00013 }
00014 
00015 
00016 ConnectionDevice::~ConnectionDevice(void) {
00017   delete recv_buffer;
00018 }
00019 
00020 
00021 int ConnectionDevice::connect(const char* device, long baudrate) {
00022   device_name = device;
00023   baudrate_value = baudrate;
00024 
00025   if (isConnected()) {
00026     disconnect();
00027   }
00028   int ret_value = raw_connect(device, baudrate);
00029   if (ret_value < 0) {
00030     return ret_value;
00031   }
00032   ret_value = raw_setBaudrate(baudrate);
00033   flush();
00034 
00035   return ret_value;
00036 }
00037 
00038 
00039 void ConnectionDevice::disconnect(void) {
00040   if (isConnected()) {
00041     raw_disconnect();
00042   }
00043 }
00044 
00045 
00046 int ConnectionDevice::setBaudrate(long baudrate) {
00047   if (isConnected()) {
00048     return raw_setBaudrate(baudrate);
00049   }
00050   return NotConnected;
00051 }
00052 
00053 
00054 void ConnectionDevice::flush(void) {
00055   if (isConnected()) {
00056     raw_flush();
00057   }
00058   recv_buffer->clear();
00059 }
00060 
00061 
00062 void ConnectionDevice::check(int size, long timeout) {
00063   if (!isConnected()) {
00064     return;
00065   }
00066 
00067   int free_size = recv_buffer->free_size();
00068   int require_size = (size < 0) ? free_size : size;
00069   if (require_size > BufferSize) {
00070     require_size = BufferSize;
00071   }
00072   int filled = recv_buffer->size();
00073   int maxlen = require_size - filled;
00074   if (maxlen <= 0) {
00075     maxlen = (free_size < BufferSize) ? free_size : BufferSize;
00076     timeout = 0;
00077   }
00078   raw_check(maxlen, timeout);
00079 }
00080 
00081 
00082 int ConnectionDevice::send(const char* data, int len) {
00083   check(0);
00084   return (!isConnected()) ? NotConnected : raw_send(data, len);
00085 }
00086 
00087 
00088 int ConnectionDevice::recv(char* data, int maxlen, long timeout) {
00089   check(maxlen, timeout);
00090   if (!isConnected()) {
00091     return NotConnected;
00092   }
00093 
00094   int len = (static_cast<int>(recv_buffer->size()) >= maxlen)
00095     ? maxlen : static_cast<int>(recv_buffer->size());
00096   if (len == 0) {
00097     return 0;
00098   }
00099   recv_buffer->get(static_cast<char*>(data), len);
00100 
00101   return len;
00102 }
00103 
00104 
00105 int ConnectionDevice::copy(char *data, int maxlen) {
00106   return recv_buffer->copy(data, maxlen);
00107 }
00108 
00109 
00110 int ConnectionDevice::size(void) {
00111   check(0);
00112   return recv_buffer->size();
00113 }
00114 
00115 
00116 int ConnectionDevice::capacity(void) {
00117   check(0);
00118   return recv_buffer->free_size();
00119 }
00120 
00121 
00122 const char* ConnectionDevice::getDevice(void) {
00123   return device_name.c_str();
00124 }
00125 
00126 
00127 long ConnectionDevice::getBaudrate(void) {
00128   return baudrate_value;
00129 }
00130 

Generated on Mon Apr 13 22:52:05 2009 by  doxygen 1.5.7.1