TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/src/hardware/eeg_sim_msg_parser.cpp
00001 /*
00002     This file is part of the TOBI SignalServer.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file gpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/gpl.html.
00020 
00021     In case of GNU General Public License Usage ,the TOBI SignalServer
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU General Public License
00028     along with the TOBI SignalServer. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: SignalServer@tobi-project.org
00032 */
00033 
00034 #include "hardware/eeg_sim_msg_parser.h"
00035 
00036 #include <stdexcept>
00037 #include <iostream>
00038 
00039 #include <boost/lexical_cast.hpp>
00040 
00041 using std::string;
00042 
00043 namespace tobiss
00044 {
00045 
00046 static const std::string EEGSIM_MSG_STRING("eegsimconfig");
00047 static const std::string EEGSIM_EEG_STRING("eeg");
00048 static const std::string EEGSIM_SINE_STRING("sine");
00049 
00050 static const std::string EEGSIM_MSG_CMD_DELIMITER(":");
00051 static const std::string EEGSIM_MSG_PARAM_DELIMITER("/");
00052 static const std::string EEGSIM_MSG_CHANNEL_DELIMITER(",");
00053 
00054 static const std::string EEGSIM_MSG_GETCONFIG("getconfig");
00055 static const std::string EEGSIM_MSG_CONFIG("config");
00056 static const std::string EEGSIM_MSG_OK("ok");
00057 static const std::string EEGSIM_MSG_ERROR("error");
00058 
00059 const unsigned int EEGSimMsgParser::MESSAGE_VERSION = 1;
00060 
00061 //-----------------------------------------------------------------------------
00062 
00063 EEGSimMsgParser::EEGSimMsgParser(std::string& str_buffer)
00064   : type_(Invalid), str_buffer_(str_buffer)
00065 {
00066   msg_types_map_.insert(std::make_pair( EEGSIM_MSG_GETCONFIG, GetConfig ) );
00067   msg_types_map_.insert(std::make_pair( EEGSIM_MSG_CONFIG, Config ) );
00068 }
00069 
00070 //-----------------------------------------------------------------------------
00071 
00072 EEGSimMsgParser::~EEGSimMsgParser()
00073 {
00074 
00075 }
00076 
00077 //-----------------------------------------------------------------------------
00078 
00079 void EEGSimMsgParser::parseMessage()
00080 {
00081   type_ = Invalid;
00082   checkMessage();
00083   type_ = parseMessageType();
00084 
00085   if(type_ == Config)
00086     parseConfigMessage();
00087   else
00088     return;
00089 }
00090 
00091 //-----------------------------------------------------------------------------
00092 
00093 void EEGSimMsgParser::checkMessage()
00094 {
00095   size_t pos = str_buffer_.find(EEGSIM_MSG_STRING);
00096 
00097   if(pos == std::string::npos || pos !=0)
00098     throw(std::runtime_error("Error -- Message is not an EEG Simulator message!"));
00099 
00100   str_buffer_.erase(0, EEGSIM_MSG_STRING.size());
00101 
00102   std::string version(EEGSIM_MSG_CMD_DELIMITER +
00103                       boost::lexical_cast<string>(EEGSimMsgParser::MESSAGE_VERSION));
00104   pos = str_buffer_.find( version );
00105 
00106   if(pos == std::string::npos || pos !=0)
00107     throw(std::runtime_error("Error -- Message version could not be determined!"));
00108 
00109   str_buffer_.erase(0, version.size());
00110 }
00111 
00112 //-----------------------------------------------------------------------------
00113 
00114 EEGSimMsgParser::MessageType EEGSimMsgParser::parseMessageType()
00115 {
00116   size_t pos = str_buffer_.find(EEGSIM_MSG_CMD_DELIMITER);
00117 
00118   if(pos == std::string::npos || pos !=0)
00119     throw(std::runtime_error("Error -- Message is not a valid EEG Simulator message!"));
00120 
00121   pos = str_buffer_.find(EEGSIM_MSG_CMD_DELIMITER,1);
00122 
00123   std::string type_str = str_buffer_.substr(1,pos-1);
00124 
00125 //  std::cout << "  *** TYPE: " <<  type_str << std::endl;
00126 
00127 //  std::cout << "  *** BUFFER: " <<  str_buffer_ << std::endl;
00128 
00129   if(pos != std::string::npos)
00130     str_buffer_.erase(0,pos+1);
00131   else
00132     str_buffer_.clear();
00133 
00134 //  std::cout << "  *** BUFFER: " <<  str_buffer_ << std::endl;
00135 
00136   std::map<std::string, MessageType>::iterator it(msg_types_map_.find(type_str));
00137 
00138   if(it == msg_types_map_.end())
00139     return(Invalid);
00140 
00141   return( it->second );
00142 }
00143 
00144 //-----------------------------------------------------------------------------
00145 
00146 // EEGSimConfig:1:GetConfig\n
00147 
00148 // EEGSimConfig:1:Sine:chX/freq/amp/phase,chY/freq/amp/phase,chA-B/freq/amp/phase:
00149 //              EEG:chX/scale/offset,chY/scale/offset,chA-B/scale/offset\n
00150 
00151 void EEGSimMsgParser::parseConfigMessage()
00152 {
00153   eeg_config_.clear();
00154   sine_configs_.clear();
00155 
00156   size_t eeg_pos = str_buffer_.find(EEGSIM_EEG_STRING);
00157 
00158   if(eeg_pos != std::string::npos)
00159   {
00160 
00161     size_t end_pos = str_buffer_.find(EEGSIM_MSG_CMD_DELIMITER, eeg_pos + EEGSIM_EEG_STRING.size() +1);
00162 
00163     std::string eeg_cfg = str_buffer_.substr(eeg_pos + EEGSIM_EEG_STRING.size() +1,
00164                                               end_pos -eeg_pos -  EEGSIM_EEG_STRING.size() -1);
00165 
00166     str_buffer_.erase(eeg_pos,end_pos);
00167 
00168     EEGConfig eeg_config;
00169     do
00170     {
00171       size_t par_pos = eeg_cfg.find(EEGSIM_MSG_PARAM_DELIMITER);
00172       boost::uint16_t ch = boost::lexical_cast<boost::uint16_t>( eeg_cfg.substr(0, par_pos) );
00173       eeg_cfg.erase(0,par_pos+1);
00174 
00175       par_pos = eeg_cfg.find(EEGSIM_MSG_PARAM_DELIMITER);
00176       double scale  = boost::lexical_cast<double>(eeg_cfg.substr(0, par_pos));
00177       eeg_cfg.erase(0,par_pos+1);
00178 
00179       par_pos = eeg_cfg.find(EEGSIM_MSG_CHANNEL_DELIMITER);
00180       double offset = boost::lexical_cast<double>(eeg_cfg.substr(0, par_pos));
00181       eeg_cfg.erase(0,par_pos+1);
00182 
00183       if(par_pos == std::string::npos)
00184         eeg_cfg.clear();
00185       //      std::cout << "ch: " << ch  << ", scale: " << scale << ", offset: " << offset << std::endl;
00186 
00187       eeg_config.offset_ = offset;
00188       eeg_config.scaling_ = scale;
00189       eeg_config_.insert( std::make_pair(ch, eeg_config) );
00190 
00191     }
00192     while(eeg_cfg.size() != 0);
00193 
00194   }
00195 
00196   size_t sine_pos = str_buffer_.find(EEGSIM_SINE_STRING);
00197 
00198   if(sine_pos != std::string::npos)
00199   {
00200 
00201     size_t end_pos = str_buffer_.find(EEGSIM_MSG_CMD_DELIMITER, sine_pos + EEGSIM_SINE_STRING.size() +1);
00202 
00203     std::string sine_cfg = str_buffer_.substr(sine_pos + EEGSIM_SINE_STRING.size() +1,
00204                                               end_pos -sine_pos -  EEGSIM_SINE_STRING.size() -1);
00205 
00206     str_buffer_.erase(sine_pos,end_pos);
00207 
00208     SineConfig sine_config;
00209     do
00210     {
00211       size_t par_pos = sine_cfg.find(EEGSIM_MSG_PARAM_DELIMITER);
00212       boost::uint16_t ch = boost::lexical_cast<boost::uint16_t>( sine_cfg.substr(0, par_pos) );
00213       sine_cfg.erase(0,par_pos+1);
00214 
00215       par_pos = sine_cfg.find(EEGSIM_MSG_PARAM_DELIMITER);
00216       double freq  = boost::lexical_cast<double>(sine_cfg.substr(0, par_pos));
00217       sine_cfg.erase(0,par_pos+1);
00218 
00219       par_pos = sine_cfg.find(EEGSIM_MSG_PARAM_DELIMITER);
00220       double ampl  = boost::lexical_cast<double>(sine_cfg.substr(0, par_pos));
00221       sine_cfg.erase(0,par_pos+1);
00222 
00223       par_pos = sine_cfg.find(EEGSIM_MSG_CHANNEL_DELIMITER);
00224       double phase = boost::lexical_cast<double>(sine_cfg.substr(0, par_pos));
00225       sine_cfg.erase(0,par_pos+1);
00226 
00227       if(par_pos == std::string::npos)
00228         sine_cfg.clear();
00229       //      std::cout << "ch: " << ch << ", freq: " << freq << ", ampl: " << ampl << ", phase: " << phase << std::endl;
00230 
00231       sine_config.freq_ = freq;
00232       sine_config.amplitude_ = ampl;
00233       sine_config.phase_ = phase;
00234       sine_configs_.insert( std::make_pair(ch, sine_config) );
00235     }
00236     while(sine_cfg.size() != 0);
00237 
00238   }
00239 
00240 }
00241 
00242 //-----------------------------------------------------------------------------
00243 
00244 std::string EEGSimMsgParser::buildConfigMsgString(std::map<boost::uint16_t, EEGConfig>& eeg,
00245                                                std::multimap<boost::uint16_t, SineConfig>& sine)
00246 {
00247   std::string config_str(EEGSIM_MSG_STRING + EEGSIM_MSG_CMD_DELIMITER +
00248                          boost::lexical_cast<string>(MESSAGE_VERSION) +
00249                          EEGSIM_MSG_CMD_DELIMITER + EEGSIM_MSG_CONFIG +
00250                          EEGSIM_MSG_CMD_DELIMITER);
00251 
00252   config_str += EEGSIM_EEG_STRING+ EEGSIM_MSG_CMD_DELIMITER;
00253 
00254   for(std::map<boost::uint16_t, EEGConfig>::iterator it(eeg.begin());
00255       it != eeg.end(); it++)
00256   {
00257     config_str += boost::lexical_cast<string>(it->first);
00258     config_str += EEGSIM_MSG_PARAM_DELIMITER + boost::lexical_cast<string>(it->second.scaling_);
00259     config_str += EEGSIM_MSG_PARAM_DELIMITER + boost::lexical_cast<string>(it->second.offset_);
00260     config_str += EEGSIM_MSG_CHANNEL_DELIMITER;
00261   }
00262   config_str.erase(--config_str.end(), config_str.end());
00263 
00264   config_str += EEGSIM_MSG_CMD_DELIMITER + EEGSIM_SINE_STRING+ EEGSIM_MSG_CMD_DELIMITER;
00265 
00266   for(std::multimap<boost::uint16_t, SineConfig>::iterator it2(sine.begin());
00267       it2 != sine.end(); it2++)
00268   {
00269     config_str += boost::lexical_cast<string>(it2->first);
00270     config_str += EEGSIM_MSG_PARAM_DELIMITER + boost::lexical_cast<string>(it2->second.freq_);
00271     config_str += EEGSIM_MSG_PARAM_DELIMITER + boost::lexical_cast<string>(it2->second.amplitude_);
00272     config_str += EEGSIM_MSG_PARAM_DELIMITER + boost::lexical_cast<string>(it2->second.phase_);
00273     config_str += EEGSIM_MSG_CHANNEL_DELIMITER;
00274   }
00275   config_str.erase(--config_str.end(), config_str.end());
00276 
00277   return(config_str + "\n");
00278 }
00279 
00280 //-----------------------------------------------------------------------------
00281 
00282 void EEGSimMsgParser::getConfigs(std::map<boost::uint16_t, EEGConfig>& eeg,
00283                 std::multimap<boost::uint16_t, SineConfig>& sine)
00284 {
00285   eeg = eeg_config_;
00286   sine = sine_configs_;
00287 }
00288 
00289 //-----------------------------------------------------------------------------
00290 
00291 std::string EEGSimMsgParser::getOKMsg()
00292 {
00293   return(EEGSIM_MSG_STRING + EEGSIM_MSG_CMD_DELIMITER +
00294          boost::lexical_cast<string>(MESSAGE_VERSION) +
00295          EEGSIM_MSG_CMD_DELIMITER + EEGSIM_MSG_OK);
00296 }
00297 
00298 //-----------------------------------------------------------------------------
00299 
00300 std::string EEGSimMsgParser::getErrorMsg()
00301 {
00302   return(EEGSIM_MSG_STRING + EEGSIM_MSG_CMD_DELIMITER +
00303          boost::lexical_cast<string>(MESSAGE_VERSION) +
00304          EEGSIM_MSG_CMD_DELIMITER + EEGSIM_MSG_ERROR);
00305 }
00306 
00307 //-----------------------------------------------------------------------------
00308 
00309 }  //tobiss
 All Data Structures Files Functions Variables