IEEE_802_15_4_NL.h
Go to the documentation of this file.00001 /******************************************************************************* 00002 * 00003 * Copyright (c) 2008-2010 00004 * Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de> 00005 * Thomas Kiebel <kiebel@ivs.cs.uni-magdeburg.de> 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * 00015 * * Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in 00017 * the documentation and/or other materials provided with the 00018 * distribution. 00019 * 00020 * * Neither the name of the copyright holders nor the names of 00021 * contributors may be used to endorse or promote products derived 00022 * from this software without specific prior written permission. 00023 * 00024 * 00025 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00026 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00027 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00028 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00029 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00030 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00031 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00032 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00033 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00034 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00035 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 * 00037 * 00038 * $Id$ 00039 * 00040 ******************************************************************************/ 00041 00042 #ifndef __IEEE_802_15_4_NL_h__ 00043 #define __IEEE_802_15_4_NL_h__ 00044 00045 #include <stdio.h> 00046 00047 #include "mw/api/EventChannel.h" 00048 #include "mw/common/NodeID.h" 00049 #include "mw/common/Subject.h" 00050 #include "mw/el/EventLayerCallBack.h" 00051 #include "mw/nl/DistinctNL.h" 00052 #include "mw/nl/Packet.h" 00053 #include "mw/afp/Config.h" 00054 00055 #include "debug.h" 00056 00057 namespace famouso { 00058 namespace mw { 00059 namespace nl { 00060 00074 template< class driver_t > 00075 class IEEE_802_15_4_NL : public DistinctNL { 00076 driver_t driver; 00077 public: 00078 typedef IEEE_802_15_4_NL type; 00079 typedef famouso::mw::Subject SNN; 00080 typedef Packet<SNN> Packet_t; 00081 typedef typename driver_t::mob_t mob_t; 00082 00084 struct info { 00085 enum { 00086 payload = 127 - sizeof( SNN ) 00087 }; 00088 }; 00089 00091 typedef afp::Disable AFP_Config; 00092 00093 protected: 00094 struct message { 00095 uint8_t size; 00096 uint8_t data[info::payload]; 00097 } mbuffer; 00098 00099 public: 00100 00101 IEEE_802_15_4_NL() {} 00102 ~IEEE_802_15_4_NL() {} 00109 void init() { 00110 TRACE_FUNCTION; 00111 driver.init(); 00112 driver.rx_Interrupt.template bind<type, &type::rx_interrupt>(this); 00113 } 00114 00120 void bind(const Subject &s, SNN &snn) { 00121 TRACE_FUNCTION; 00122 snn = s; 00123 } 00124 00130 void deliver(const Packet_t& p) { 00131 TRACE_FUNCTION; 00132 struct message buffer; 00133 00134 SNN *subject = reinterpret_cast<SNN*>( buffer.data );//TODO Problem mit kleinem subject 00135 *subject = p.snn; 00136 00137 buffer.size = 0; 00138 while ( buffer.size != p.data_length ) { 00139 buffer.data[sizeof( SNN ) + buffer.size] = p.data[buffer.size]; 00140 ++buffer.size; 00141 } 00142 buffer.size += sizeof( SNN ); 00143 driver.send(buffer.data, buffer.size); 00144 } 00145 00152 void fetch(Packet_t& p) { 00153 TRACE_FUNCTION; 00154 00155 p.snn = *reinterpret_cast<SNN*>( mbuffer.data ); 00156 p.data = mbuffer.data + sizeof( famouso::mw::Subject ); 00157 p.data_length = mbuffer.size - sizeof( famouso::mw::Subject ); 00158 } 00159 00164 void rx_interrupt(const mob_t& msg) { 00165 TRACE_FUNCTION; 00166 mbuffer.size = 0; 00167 while ( mbuffer.size != msg.length) { 00168 mbuffer.data[mbuffer.size] = msg.data[mbuffer.size]; 00169 mbuffer.size++; 00170 } 00171 famouso::mw::el::IncommingEventFromNL(this); 00172 } 00173 00179 void tx_interrupt() { 00180 TRACE_FUNCTION; 00181 } 00182 00187 SNN lastPacketSNN() { 00188 return *reinterpret_cast<SNN*>( mbuffer.data ); 00189 } 00190 }; 00191 } // namespace nl 00192 } // namespace mw 00193 } //namespace famouso 00194 00195 #endif /*__IEEE_802_15_4_NL_h__*/