TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/include/hardware/hw_thread.h
Go to the documentation of this file.
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 
00039 #ifndef HWThread_H
00040 #define HWThread_H
00041 
00042 #define SLAVE       0
00043 #define MASTER      1
00044 #define APERIODIC   2
00045 
00046 #include <map>
00047 #include <string>
00048 
00049 #include <boost/thread.hpp>
00050 #include <boost/cstdint.hpp>
00051 
00052 #include "ticpp/ticpp.h"
00053 
00054 #include "tia/defines.h"
00055 #include "sampleblock/sample_block.h"
00056 
00057 namespace tobiss
00058 {
00059 
00060 //-----------------------------------------------------------------------------
00061 
00080 class HWThread
00081 {
00082   public:
00083 
00087     virtual ~HWThread()
00088     {    }
00089 
00094     std::string getType()  { return(type_); }
00095 
00100     bool isMaster()   { return(mode_ == MASTER); }
00101 
00106     bool isSlave()   { return(mode_ == SLAVE); }
00107 
00112     bool isAperiodic()   { return(mode_ == APERIODIC); }
00113 
00118     bool hasHomogenousSignalType()  { return(homogenous_signal_type_); }
00119 
00124     boost::uint16_t getNrOfBlocks()    { return(blocks_); }
00129     double getSamplingRate()  { return(fs_); }
00134     boost::uint16_t getNrOfChannels()  { return(nr_ch_); }
00139     std::vector<boost::uint32_t> getChannelTypes()  { return(channel_types_); }
00144     std::map<boost::uint16_t, std::pair<std::string, boost::uint32_t> > getChannelInfoMap()  { return(channel_info_); }
00149     bool samplesAvailable();
00155     void checkMandatoryHardwareTags(ticpp::Iterator<ticpp::Element> hw);
00156 
00164     virtual SampleBlock<double> getSyncData() = 0;
00178     virtual SampleBlock<double> getAsyncData() = 0;
00182     virtual void run() = 0;
00183 
00187     virtual void stop() = 0;
00188 
00189   protected:
00190 
00198     HWThread(boost::uint32_t sampling_rate, boost::uint16_t channels, boost::uint16_t blocks)
00199     : nr_ch_(channels),fs_(sampling_rate), samples_available_(0), blocks_(blocks),
00200     mode_(SLAVE), running_(0)
00201     {    }
00202 
00208     HWThread()
00209     : nr_ch_(0),fs_(0), samples_available_(0), blocks_(1), mode_(SLAVE), running_(0)
00210     {    }
00211 
00217     virtual void setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father) = 0;
00223     virtual void setChannelSettings(ticpp::Iterator<ticpp::Element>const &father) = 0;
00224 
00225 
00231     void setSamplingRate(ticpp::Iterator<ticpp::Element>const &elem);
00237     void setDeviceChannels(ticpp::Iterator<ticpp::Element>const &elem);
00243     void setBlocks(ticpp::Iterator<ticpp::Element>const &elem);
00252     void setChannelSelection(ticpp::Iterator<ticpp::Element>const &elem);
00256     void setChannelTypes();
00257 
00262     bool isNumber(const std::string& s)
00263     {
00264       for (unsigned int i = 0; i < s.length(); i++)
00265         if (!isdigit(s[i]))
00266           return false;
00267       return true;
00268     }
00273     void setType(std::string s)
00274     {
00275       type_ = s;
00276     }
00277 
00282     boost::uint16_t getChannelPosition(boost::uint16_t ch_nr)
00283     {
00284       unsigned int n = 0;
00285       for(std::map<boost::uint16_t, std::pair<std::string, boost::uint32_t> >::iterator it(channel_info_.begin());
00286           it != channel_info_.end(); it++)
00287       {
00288         if(it->first == ch_nr)
00289           break;
00290         n++;
00291       }
00292       if(n > channel_info_.size())
00293         throw(std::runtime_error("HWThread::getChannelPosition -- Channel not found in channel_info!"));
00294 
00295       return(n);
00296     }
00297 
00298 //-----------------------------------------------
00299 
00300   protected:
00301     boost::uint16_t nr_ch_;   
00302     double fs_;      
00303     bool samples_available_;
00304     boost::uint16_t blocks_;   
00305     boost::uint8_t mode_;       
00306     bool running_;      
00307 
00308     boost::shared_mutex rw_;     
00309 
00310     std::map<boost::uint16_t, std::pair<std::string, boost::uint32_t> > channel_info_;  
00311     bool homogenous_signal_type_;  
00312     std::vector<boost::uint32_t> channel_types_;   
00313 
00325     SampleBlock<double> data_;
00326 
00327     std::string   type_;
00328 
00329 
00330     //-----------------------------------------------
00331     // Constant variables & methods:
00332 
00333     static const std::string hardware_version_;
00334     static const std::string hardware_serial_;
00335 
00336     static const std::string hw_mode_;
00337     static const std::string hw_devset_;        
00338     static const std::string hw_fs_;            
00339 
00340     static const std::string hw_channels_;      
00341     static const std::string hw_ch_nr_;         
00342     static const std::string hw_ch_names_;      
00343     static const std::string hw_ch_type_;       
00344 
00345     static const std::string hw_blocksize_;     
00346 
00347     static const std::string hw_chset_;       
00348     static const std::string hw_chset_sel_;           
00349     static const std::string hw_chset_ch_;      
00350     static const std::string hw_chset_nr_;     
00351     static const std::string hw_chset_name_;   
00352 
00353 
00354     //---------------------------------------------------------------------------------------
00355 
00356     bool equalsOnOrOff(const std::string& s);
00357     bool equalsYesOrNo(const std::string& s);
00358     bool equalsMaster(const std::string& s);
00359     bool equalsSlave(const std::string& s);
00360     bool equalsAperiodic(const std::string& s);
00361 //    uint32_t getSignalFlag(const std::string& s);
00362 //    std::string getSignalName(const boost::uint32_t& flag);
00363 
00364     void parseDeviceChannels(ticpp::Iterator<ticpp::Element>const &elem, boost::uint16_t& nr_ch,
00365                                  std::string& naming, std::string& type);
00366 
00367     void parseChannelSelection(ticpp::Iterator<ticpp::Element>const &elem, boost::uint16_t& ch,
00368                                    std::string& name, std::string& type);
00369 
00370     void checkMandatoryHardwareTagsXML(ticpp::Iterator<ticpp::Element> hw);
00371 
00372 
00373 };
00374 
00375 } // Namespace tobiss
00376 
00377 #endif // HWThread_H
00378 
00379 //-----------------------------------------------------------------------------
 All Data Structures Files Functions Variables