Menu

Broker.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2008-2010 Michael Schulze <mschulze@ivs.cs.uni-magdeburg.de>
00004  * All rights reserved.
00005  *
00006  *    Redistribution and use in source and binary forms, with or without
00007  *    modification, are permitted provided that the following conditions
00008  *    are met:
00009  *
00010  *    * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *
00013  *    * Redistributions in binary form must reproduce the above copyright
00014  *      notice, this list of conditions and the following disclaimer in
00015  *      the documentation and/or other materials provided with the
00016  *      distribution.
00017  *
00018  *    * Neither the name of the copyright holders nor the names of
00019  *      contributors may be used to endorse or promote products derived
00020  *      from this software without specific prior written permission.
00021  *
00022  *
00023  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00024  *    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00025  *    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00026  *    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00027  *    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00028  *    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00029  *    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00030  *    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00031  *    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032  *    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *
00036  * $Id$
00037  *
00038  ******************************************************************************/
00039 
00040 #ifndef __etagBP_Broker_h__
00041 #define __etagBP_Broker_h__
00042 
00043 #include "mw/nl/can/constants.h"
00044 #include "mw/nl/can/canETAGS.h"
00045 #include "mw/common/Subject.h"
00046 #include "util/endianness.h"
00047 
00048 namespace famouso {
00049     namespace mw {
00050         namespace nl {
00051             namespace CAN {
00052                 namespace etagBP {
00053                     using namespace famouso::mw::nl::CAN::detail;
00054 
00069                     template < class CAN_Driver>
00070                     class Broker {
00071 
00072                             Subject etags[constants::etagBP::count];
00073 
00079                             uint16_t bind_subject_to_etag(const Subject &sub) {
00080                                 uint16_t etag = constants::etagBP::count;
00081                                 while (etag > constants::etagBP::reserved) {
00082                                     --etag;
00083                                     if ((etags[etag] == UID()) || (etags[etag] == sub)) {
00084                                         etags[etag] = sub;
00085                                         ::logging::log::emit() << PROGMEMSTRING("Supply etag")
00086                                             << ::logging::log::tab << ::logging::log::tab
00087                                             << PROGMEMSTRING("-- Subject [");
00088                                         for (uint8_t i = 0;i < 8;++i) {
00089                                             uint8_t c = sub.tab()[i];
00090                                             if ((c < 32) || (c > 126)) c = 32;   // only printable characters
00091                                             ::logging::log::emit() << c ;
00092                                         }
00093                                         ::logging::log::emit() << PROGMEMSTRING("] -> ")
00094                                             << ::logging::log::hex << PROGMEMSTRING("etag [")
00095                                             << etag << PROGMEMSTRING("] ")
00096                                             << sub.value() << ::logging::log::endl;
00097                                         return etag;
00098                                     }
00099                                 }
00100                                 ::logging::log::emit()
00101                                     << PROGMEMSTRING("no free etags -- that should never be happen")
00102                                     << ::logging::log::endl;
00103                                 return 0;
00104                             }
00105 
00106                         public:
00107                             typedef typename CAN_Driver::MOB::IDType IDType;
00108 
00119                             uint16_t bind_subject(const Subject &sub, uint16_t tx_node, CAN_Driver& canDriver) {
00120                                 return bind_subject_to_etag(sub);
00121                             }
00122 
00133                             bool handle_subject_bind_request(typename CAN_Driver::MOB &mob, CAN_Driver& canDriver) {
00134                                 IDType *id = &mob.id();
00135                                 Subject sub(mob.data());
00136                                 if (id->etag() == famouso::mw::nl::CAN::detail::ETAGS::GET_ETAG) {
00137                                     uint16_t etag = bind_subject_to_etag(sub);
00138 
00139                                     mob.len(8);
00140                                     mob.data()[0] = id->tx_node();
00141                                     mob.data()[1] = 0x0;
00142                                     mob.data()[2] = etag >> 8;
00143                                     mob.data()[3] = static_cast<uint8_t>(etag & 0xff);
00144                                     mob.data()[4] = sub.tab()[0];
00145                                     mob.data()[5] = sub.tab()[1];
00146                                     mob.data()[6] = sub.tab()[2];
00147                                     mob.data()[7] = sub.tab()[3];
00148                                     id->prio(0xFD);
00149                                     id->etag(famouso::mw::nl::CAN::detail::ETAGS::SUPPLY_ETAG_NEW_BP);
00150                                     id->tx_node(constants::Broker_tx_node);
00151                                     canDriver.transmit(mob);
00152 
00153                                     mob.data()[1] = 0x1;
00154                                     mob.data()[4] = sub.tab()[4];
00155                                     mob.data()[5] = sub.tab()[5];
00156                                     mob.data()[6] = sub.tab()[6];
00157                                     mob.data()[7] = sub.tab()[7];
00158                                     canDriver.transmit(mob);
00159 
00160                                     return true;
00161                                 }
00162                                 return false;
00163                             }
00164                     };
00165 
00166                 }
00167             } /* namespace CAN */
00168         } /* namespace nl */
00169     } /* namespace mw */
00170 } /* namespace famouso */
00171 
00172 #endif
00173