Broker.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
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;
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 }
00168 }
00169 }
00170 }
00171
00172 #endif
00173