Wiselib
wiselib.testing/util/wisebed_node_api/remote_uart_message.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef CONNECTOR_REMOTE_UART_MESSAGE_H
00020 #define CONNECTOR_REMOTE_UART_MESSAGE_H
00021 
00022 #include "util/serialization/simple_types.h"
00023 //#include <fstream>
00024 //#include <sstream>
00025 //#include <iostream>
00026 
00027 namespace wiselib
00028 {
00029 
00030    template<typename OsModel_P,
00031             typename Radio_P = typename OsModel_P::Radio>
00032    class RemoteUartInMessage
00033    {
00034    public:
00035       typedef OsModel_P OsModel;
00036       typedef Radio_P Radio;
00037       typedef typename Radio::block_data_t block_data_t;
00038       typedef typename Radio::size_t size_t;
00039       typedef typename Radio::node_id_t node_id_t;
00040       RemoteUartInMessage()
00041       {
00042           set_payload_length(0);
00043       }
00044       // --------------------------------------------------------------------
00045       inline uint8_t command_type()
00046       { return read<OsModel, block_data_t, uint8_t>( buffer ); }
00047       // --------------------------------------------------------------------
00048       inline void set_command_type( uint8_t type )
00049       { write<OsModel, block_data_t, uint8_t>( buffer, type ); }
00050       // --------------------------------------------------------------------
00051       inline uint8_t sequence_number()
00052       { return read<OsModel, block_data_t, uint8_t>( buffer + SEQUENCE_NUMBER_POS ); }
00053       // --------------------------------------------------------------------
00054       inline void set_sequence_number( uint8_t number )
00055       { write<OsModel, block_data_t, uint8_t>( buffer + SEQUENCE_NUMBER_POS, number ); }
00056       // --------------------------------------------------------------------
00057       inline uint8_t payload_length()
00058       { return read<OsModel, block_data_t, uint8_t>( buffer + PAYLOAD_SIZE_POS ); }
00059       // --------------------------------------------------------------------
00060       inline uint64_t destination()
00061       { return read<OsModel, block_data_t, uint64_t>( buffer + DESTINATION_POS ); }
00062       // --------------------------------------------------------------------
00063       inline void set_destination( uint64_t dst )
00064       { write<OsModel, block_data_t, uint64_t>( buffer + DESTINATION_POS, dst ); }
00065       // --------------------------------------------------------------------
00066       inline uint64_t source()
00067       { return read<OsModel, block_data_t, uint64_t>( buffer + SOURCE_POS ); }
00068       // --------------------------------------------------------------------
00069       inline void set_source( uint64_t src )
00070       { write<OsModel, block_data_t, uint64_t>( buffer + SOURCE_POS, src ); }
00071       // --------------------------------------------------------------------
00072       inline block_data_t* payload()
00073       { return buffer + PAYLOAD_POS; }
00074       // --------------------------------------------------------------------
00075       inline void set_payload( size_t len, block_data_t *buf )
00076       {
00077          set_payload_length( len );
00078          memcpy( buffer + PAYLOAD_POS, buf, len);
00079       }
00080       // --------------------------------------------------------------------
00081       inline size_t buffer_size()
00082       { return PAYLOAD_POS + payload_length(); }
00083       // --------------------------------------------------------------------
00084 
00085       bool append(block_data_t *other, size_t start, size_t length) {
00086 //          std::cout<<"append params: " << (int)payload_length() <<" "<< length <<" " <<start << " "<<Radio_P::MAX_MESSAGE_LENGTH<<" " << PAYLOAD_POS<<std::endl;
00087             if ((payload_length() + length) <= (Radio_P::MAX_MESSAGE_LENGTH-PAYLOAD_POS)) {
00088 //                std::cout<< "writing byte " << (int)other[start] << std::endl;
00089 
00090                 memcpy( buffer + PAYLOAD_POS + payload_length(), &other[start], length);
00091 
00092 //                std::cout<< "written byte " << (int)(buffer[PAYLOAD_POS + payload_length()]) << std::endl;
00093 //                std::cout<<"append params: " << (int)payload_length() <<" "<< length<<" " <<start << " "<<Radio_P::MAX_MESSAGE_LENGTH<<" " << PAYLOAD_POS<<std::endl;
00094                 //mybuffcpy(&(buffer[PAYLOAD_POS + payload_length()]), &(other[start]), length);
00095 
00096                 set_payload_length(payload_length() + length);
00097 
00098 //                std::cout<<"append params: " << (int)payload_length() <<" "<< length<<" " <<start << " "<<Radio_P::MAX_MESSAGE_LENGTH<<" " << PAYLOAD_POS<<std::endl;
00099                 return true;
00100             }
00101             return false;
00102         }
00103 
00104       enum data_in_positions
00105       {
00106          COMMAND_TYPE     = 0,
00107          SEQUENCE_NUMBER_POS  = 1,         
00108          DESTINATION_POS  = 2,
00109          SOURCE_POS       = 10,
00110          PAYLOAD_SIZE_POS = 18,
00111          PAYLOAD_POS      = 19
00112       };
00113       
00114    private:
00115        inline void set_payload_length( uint8_t len )
00116        { write<OsModel, block_data_t, uint8_t>( buffer + PAYLOAD_SIZE_POS, len ); }
00117 
00118 //        void *mybuffcpy(block_data_t *target1, const block_data_t *source1, size_t no) {
00119 //        char *tempt = target1;
00120 //        for (size_t n = 0; n < no;) {
00121 //            *target1 = *source1;
00122 //            target1++;
00123 //            source1++;
00124 //            n++;
00125 //        }
00126 //        return tempt;
00127 //    }
00128       // --------------------------------------------------------------------
00129       block_data_t buffer[Radio_P::MAX_MESSAGE_LENGTH];
00130    };
00131 
00132 }
00133 
00134 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines