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 __ccp_Client_h__
00041 #define __ccp_Client_h__
00042
00043 #include "mw/nl/can/constants.h"
00044 #include "mw/nl/can/canETAGS.h"
00045 #include "mw/common/UID.h"
00046 #include "debug.h"
00047
00048
00049 namespace famouso {
00050 namespace mw {
00051 namespace nl {
00052 namespace CAN {
00053 namespace ccp {
00054 using namespace famouso::mw::nl::CAN::detail;
00055
00056 enum {
00057 CCP_RUNNING = 0,
00058 CCP_COMPLETE,
00059 CCP_WAIT
00060 };
00061
00062
00063
00064
00065
00066
00067
00068 template < class CAN_Driver >
00069 class Client {
00070 void debug(char* s) {
00071 }
00072
00073 uint8_t compareUID(const uint8_t *msg, const uint8_t* uid_str, uint8_t stage) {
00074 register int8_t count;
00075
00076 for (count = constants::ccp::ccp_stages;count >= stage;--count) {
00077 register uint8_t uidPart = (constants::ccp::ccp_stages - count) >> 1;
00078 register uint8_t uidNibble = (count % 2) ? (uid_str[uidPart] >> 4) : (uid_str[uidPart] & 0xf);
00079 register uint8_t msgNibble = (count % 2) ? (msg[uidPart] >> 4) : (msg[uidPart] & 0xf);
00080 if (uidNibble != msgNibble) {
00081 return 0;
00082 }
00083 }
00084 return 1;
00085 }
00086
00087 public:
00088
00089 typedef typename CAN_Driver::MOB::IDType IDType;
00090
00091 uint8_t ccp_configure_tx_node(const UID &uid, CAN_Driver& canDriver) {
00092 const uint8_t *uid_str = uid.tab();
00093 typename CAN_Driver::MOB msg;
00094 msg.extended();
00095
00096 IDType *id = &msg.id();
00097 uint8_t ccp_status;
00098 uint8_t ccp_stage;
00099 uint8_t tx_node;
00100 int8_t stage;
00101
00102 uint8_t zeros[8] = {0};
00103
00104 ccp_status = CCP_RUNNING;
00105 ccp_stage = 0;
00106
00107
00108 stage = constants::ccp::ccp_stages;
00109 do {
00110
00111 register uint8_t uidPart = (constants::ccp::ccp_stages - stage) >> 1;
00112 register uint8_t uidNibble = (stage % 2) ? (uid_str[uidPart] >> 4) :
00113 (uid_str[uidPart] & 0xf);
00114
00115
00117 id->prio(0xFD);
00118 id->ccp_stage(stage);
00119 id->ccp_nibble(uidNibble);
00120
00121 id->etag(famouso::mw::nl::CAN::detail::ETAGS::CCP_RSI);
00122
00123 msg.len(0);
00124
00125
00126 canDriver.transmit(msg);
00127
00128 ccp_status = CCP_WAIT;
00129
00130 do {
00131
00132 do {
00133 canDriver.receive_blocking(&msg);
00134
00135 } while (id->etag() != famouso::mw::nl::CAN::detail::ETAGS::CCP_SSI);
00136
00137
00138 tx_node = id->tx_node();
00139
00140 ccp_status = CCP_RUNNING;
00141
00142
00143 if (tx_node == constants::Broker_tx_node) {
00144
00145
00146
00147 if (compareUID(msg.data(), zeros, 0)) {
00148 stage = constants::ccp::ccp_stages;
00149 } else {
00150 if ((stage == (msg.data()[7] & 0xf)) && compareUID(msg.data(), uid_str, stage)) {
00151
00152 --stage;
00153 } else {
00154 ccp_status = CCP_WAIT;
00155 }
00156 }
00157
00158 } else {
00159
00160
00161
00162
00163
00164
00165
00166 if (compareUID(msg.data(), uid_str, 0)) {
00167 ::logging::log::emit< ::logging::Trace>()
00168 << PROGMEMSTRING("It is my knot ID")
00169 << ::logging::log::endl;
00170 ccp_stage = CCP_COMPLETE;
00171 } else {
00172
00173
00174
00175 ::logging::log::emit< ::logging::Trace>()
00176 << PROGMEMSTRING("It is not my knot ID")
00177 << ::logging::log::endl;
00178 stage = constants::ccp::ccp_stages;
00179 }
00180 }
00181 } while ((ccp_status != CCP_RUNNING));
00182 } while ((ccp_stage != CCP_COMPLETE));
00183
00184 return tx_node;
00185 }
00186
00187 bool handle_ccp_configure_request(typename CAN_Driver::MOB&, CAN_Driver&) {
00188 return false;
00189 };
00190 };
00191 }
00192 }
00193 }
00194 }
00195 }
00196
00197
00198 #endif