00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2011, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote prducts derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 00035 #ifndef ROS_NODEOUTPUT_H_ 00036 #define ROS_NODEOUTPUT_H_ 00037 00038 #include "msg.h" 00039 00040 namespace ros { 00041 00042 /* 00043 * This class is responsible for controlling the node ouput. 00044 * It it is the object that is passed to Publishers and services 00045 */ 00046 class NodeOutput_{ 00047 public: 00048 virtual int publish(int id, Msg* msg)=0; 00049 }; 00050 00051 template<class Hardware, int OUTSIZE =512> 00052 class NodeOutput : public NodeOutput_{ 00053 00054 private: 00055 Hardware* hardware_; 00056 bool configured_; 00057 unsigned char message_out[OUTSIZE]; 00058 00059 public: 00060 NodeOutput(Hardware* h){ 00061 hardware_ = h; 00062 configured_ = false; 00063 } 00064 00065 NodeOutput(){}; 00066 00067 void setHardware(Hardware* h){ 00068 hardware_ = h; 00069 configured_=false; 00070 } 00071 00072 void setConfigured(bool b){ 00073 configured_ =b; 00074 } 00075 bool configured(){return configured_;}; 00076 00077 virtual int publish(int id, Msg * msg){ 00078 if(!configured_) return 0; 00079 00080 /* serialize message */ 00081 int l = msg->serialize(message_out+6); 00082 00083 /* setup the header */ 00084 message_out[0] = 0xff; 00085 message_out[1] = 0xff; 00086 message_out[2] = (unsigned char) id&255; 00087 message_out[3] = (unsigned char) id>>8; 00088 message_out[4] = (unsigned char) l&255; 00089 message_out[5] = ((unsigned char) l>>8); 00090 00091 /* calculate checksum */ 00092 int chk = 0; 00093 for(int i =2; i<l+6; i++) 00094 chk += message_out[i]; 00095 l += 6; 00096 message_out[l++] = 255 - (chk%256); 00097 00098 hardware_->write(message_out, l); 00099 return l; 00100 } 00101 }; 00102 00103 } 00104 00105 #endif