Menu

SerialNL.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  *                    2010 Thomas Kiebel <kiebel@ivs.cs.uni-magdeburg.de>
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  *
00014  *    * Redistributions in binary form must reproduce the above copyright
00015  *      notice, this list of conditions and the following disclaimer in
00016  *      the documentation and/or other materials provided with the
00017  *      distribution.
00018  *
00019  *    * Neither the name of the copyright holders nor the names of
00020  *      contributors may be used to endorse or promote products derived
00021  *      from this software without specific prior written permission.
00022  *
00023  *
00024  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00025  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00026  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00027  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  *
00037  * $Id$
00038  *
00039  ******************************************************************************/
00040 #ifndef __SERIAL_NL_h__
00041 #define __SERIAL_NL_h__
00042 
00043 #include <stdio.h>
00044 
00045 #include "mw/afp/Config.h"
00046 #include "mw/api/EventChannel.h"
00047 #include "mw/common/NodeID.h"
00048 #include "mw/common/Subject.h"
00049 #include "mw/el/EventLayerCallBack.h"
00050 #include "mw/nl/DistinctNL.h"
00051 #include "mw/nl/Packet.h"
00052 
00053 #include "debug.h"
00054 
00055 namespace famouso {
00056     namespace mw {
00057         namespace nl {
00065             template< class DRV >
00066             class SerialNL : public DistinctNL {
00067                 public:
00068                     typedef SerialNL<DRV> type;
00069                     typedef famouso::mw::Subject SNN;
00070                     typedef Packet<SNN> Packet_t;
00072                     typedef afp::Disable AFP_Config;
00073                     typedef typename DRV::mob_t mob_t;
00074 
00076                     struct info {
00077                         enum {
00078                             payload  = DRV::cfgInfo::payload - sizeof( SNN )
00080                         };
00081                     };
00082                 protected:
00083                     DRV driver;
00084                     mob_t rmob;
00085                 public:
00086                     SerialNL()  {}
00087                     ~SerialNL() {}
00088 
00096                     void init() {
00097                         TRACE_FUNCTION;
00098 
00099                         driver.onReceive.template bind<type, &type::rx_interrupt>(this);
00100                         driver.init();
00101                     }
00102 
00108                     void bind(const Subject &s, SNN &snn) {
00109                         TRACE_FUNCTION;
00110                         snn = s;
00111                     }
00112 
00117                     void deliver(const Packet_t& p) {
00118                         TRACE_FUNCTION;
00119 
00120                         mob_t smob;
00121                         SNN *subject = reinterpret_cast<SNN*>( smob.payload );
00122                         *subject     = p.snn;
00123 
00124                         smob.size = 0;
00125                         while ( smob.size != p.data_length ) {
00126                             smob.payload[sizeof( SNN ) + smob.size] = p.data[smob.size];
00127                             ++smob.size;
00128                         }
00129                         smob.size += sizeof( SNN );
00130                         driver.send( smob );
00131                     }
00132 
00139                     void fetch(Packet_t& p) {
00140                         TRACE_FUNCTION;
00141 
00142                         p.snn         = *reinterpret_cast<SNN*>( rmob.payload );
00143                         p.data        = rmob.payload + sizeof( SNN );
00144                         p.data_length = rmob.size - sizeof( SNN );
00145                         p.fragment    = 0;
00146                     }
00147 
00151                     void rx_interrupt() {
00152                         TRACE_FUNCTION;
00153 
00154                         driver.recv(rmob);
00155                         if (rmob.size != 0)
00156                             famouso::mw::el::IncommingEventFromNL(this);
00157                     }
00158 
00164                     void tx_interrupt() {
00165                         TRACE_FUNCTION;
00166                     }
00167 
00172                     SNN lastPacketSNN() {
00173                         return *reinterpret_cast<SNN*>( rmob.payload );
00174                     }
00175             };
00176         } // namespace nl
00177     } // namespace mw
00178 } //namespace famouso
00179 
00180 #endif /*__SERIAL_NL_h__*/