Client.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_Client_h__
00041 #define __etagBP_Client_h__
00042
00043 #include "mw/nl/can/constants.h"
00044 #include "mw/nl/can/canETAGS.h"
00045 #include "mw/common/Subject.h"
00046
00047 #include "mw/nl/can/etagBP/PreventBlockingOfMiddlewareCoreInBlockingProtocol.h"
00048 #include "mw/nl/can/etagBP/InterruptEnabler.h"
00049 #include "config/type_traits/contains_type.h"
00050 CONTAINS_TYPE_(asio_tag);
00051 #include "config/type_traits/if_select_type.h"
00052
00053 namespace famouso {
00054 namespace mw {
00055 namespace nl {
00056 namespace CAN {
00057 namespace etagBP {
00058
00079 template < class CAN_Driver >
00080 class Client {
00081 struct BindSubjectInfo {
00082 typename CAN_Driver::MOB mob;
00083 uint16_t etag;
00084 uint8_t round;
00085 BindSubjectInfo () : etag(0), round(0){}
00086 };
00087
00088 BindSubjectInfo * volatile _bsi;
00089 public:
00090
00091 typedef typename CAN_Driver::MOB::IDType IDType;
00092
00093 Client(): _bsi(0) {}
00105 uint16_t bind_subject(const Subject &sub, uint16_t tx_node, CAN_Driver& canDriver) {
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 typename if_select_type <
00117 contains_type_asio_tag<CAN_Driver>::value,
00118 PreventBlockingOfMiddlewareCoreInBlockingProtocol,
00119 InterruptEnabler
00120 >::type unblocker;
00121
00122 BindSubjectInfo bsi;
00123 IDType *id = reinterpret_cast<IDType*>(&(bsi.mob.id()));
00124 bsi.mob.len(8);
00125 bsi.mob.extended();
00126 for (uint8_t i = 0;i < bsi.mob.len();++i)
00127 bsi.mob.data()[i] = sub.tab()[i];
00128
00129 _bsi=&bsi;
00130
00131 id->prio(0xFD);
00132 id->etag(famouso::mw::nl::CAN::detail::ETAGS::GET_ETAG);
00133 id->tx_node(tx_node);
00134 canDriver.transmit(bsi.mob);
00135
00136 while (_bsi)
00137 unblocker.process();
00138
00139 return bsi.etag;
00140
00141 }
00142
00152 bool handle_subject_bind_request(typename CAN_Driver::MOB &mob, CAN_Driver& canDriver) {
00153 IDType *id = &mob.id();
00154 if (id->etag() == famouso::mw::nl::CAN::detail::ETAGS::SUPPLY_ETAG_NEW_BP) {
00155 if (_bsi){
00156
00157 uint8_t index=mob.data()[1]<<2;
00158
00159 if ((_bsi->round<<2) == index) {
00160 typename CAN_Driver::MOB &request_mob=const_cast<typename CAN_Driver::MOB &>(_bsi->mob);
00161
00162
00163 for (uint8_t count=0;count<4;++count)
00164 if (request_mob.data()[index+count] != mob.data()[4+count]) {
00165 _bsi->round=0;
00166 return true;
00167 }
00168
00169 _bsi->round++;
00170 }
00171
00172
00173
00174 if (_bsi->round == 2 ) {
00175
00176 _bsi->etag = ((mob.data()[2] & 0x3f) << 8) + mob.data()[3];
00177
00178 _bsi=NULL;
00179 }
00180 }
00181 return true;
00182 } else {
00183 return false;
00184 }
00185 }
00186 };
00187
00188 }
00189 }
00190 }
00191 }
00192 }
00193
00194 #endif
00195