13#include <yarp/os/Time.h>
14#include <yarp/os/Network.h>
15#include <yarp/conf/environment.h>
16#include <yarp/os/NetType.h>
19#include "EOropframe_hid.h"
21#include "EoProtocol.h"
22#include "EoManagement.h"
23#include "EoProtocolMN.h"
26using namespace yarp::os;
27using namespace yarp::os::impl;
42 isInRunningMode =
false;
44 verifiedBoardPresence =
false;
45 askedBoardVersion =
false;
46 verifiedBoardTransceiver =
false;
48 cleanedBoardBehaviour =
false;
50 boardMNprotocolversion.major = boardMNprotocolversion.minor;
53 txconfig.cycletime = defcycletime;
54 txconfig.txratedivider = defTXrateOfRegularROPs;
55 txconfig.maxtimeRX = defmaxtimeRX;
56 txconfig.maxtimeDO = defmaxtimeDO;
57 txconfig.maxtimeTX = defmaxtimeTX;
58 txconfig.logging.flags = (0x0001 << eomn_appl_log_asynchro_exectime_overflow);
59 txconfig.logging.period10ms = 0;
61 memset(verifiedEPprotocol, 0,
sizeof(verifiedEPprotocol));
63 usedNumberOfRegularROPs = 0;
65 for(
int i = 0; i<16; i++)
67 c_string_handler[i] = NULL;
70 std::string tmp = yarp::conf::environment::get_string(
"ETH_VERBOSEWHENOK");
73 verbosewhenok = (bool)(yarp::conf::numeric::from_string(tmp, 0U));
77 verbosewhenok =
false;
82 regularsAreSet =
false;
91 for(
int i=0; i<16; i++)
93 if (c_string_handler[i] != NULL)
95 delete c_string_handler[i];
96 c_string_handler[i] = NULL;
101bool EthResource::lock(
bool on)
156 if(
false == transceiver.
init2(
this, cfgtotal, localIPv4, remIP))
158 yError() <<
"EthResource::open2() cannot init transceiver w/ HostTransceiver::init2() for BOARD" << properties.
boardnameString <<
"IP" << properties.
ipv4addrString;
165 monitorpresence.
config(mpConfig);
166 monitorpresence.
tick();
186 const void * udp = transceiver.
getUDP(sizeofpacket, numofrops);
205 monitorpresence.
tick();
212 if(
false == regularsAreSet)
217 return monitorpresence.
check();
242bool EthResource::isRunning(
void)
244 return(isInRunningMode);
249bool EthResource::verifyBoardTransceiver()
253 if(verifiedBoardTransceiver)
259#define DONT_ASK_COMM_STATUS
261#if defined(DONT_ASK_COMM_STATUS)
263 const eoprot_version_t * pc104versionMN = eoprot_version_of_endpoint_get(eoprot_endpoint_management);
264 const eoprot_version_t * brdversionMN = &boardMNprotocolversion;
273 const eoprot_version_t * pc104versionMN = eoprot_version_of_endpoint_get(eoprot_endpoint_management);
274 const double timeout = 0.100;
276 eOprotID32_t id32 = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_comm, 0, eoprot_tag_mn_comm_status);
277 eOmn_comm_status_t brdstatus = {0};
280 bool rr = nvman.
ask(ipv4addr, id32, &brdstatus, timeout);
288 eoprot_version_t * brdversionMN = (eoprot_version_t*)&brdstatus.managementprotocolversion;
292 if(pc104versionMN->major != brdversionMN->major)
294 yError() <<
"EthResource::verifyBoardTransceiver() detected different mn protocol major versions: local =" << pc104versionMN->major <<
", remote =" << brdversionMN->major <<
": cannot proceed any further";
299 if(pc104versionMN->minor != brdversionMN->minor)
301 yError() <<
"EthResource::verifyBoardTransceiver() detected different mn protocol minor versions: local =" << pc104versionMN->minor <<
", remote =" << brdversionMN->minor <<
": cannot proceed any further.";
312 verifiedBoardTransceiver =
true;
319bool EthResource::setTimingOfRunningCycle()
332 eOprotID32_t id32 = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_appl, 0, eoprot_tag_mn_appl_config);
336 if(
false == nvman.
setcheck(properties.
ipv4addr, id32, &txconfig, 5, 0.010, 2.0))
338 yWarning() <<
"EthResource::setTimingOfRunningCycle() for BOARD" <<
getProperties().
boardnameString <<
"with IP" <<
getProperties().
ipv4addrString <<
"could not configure: cycletime =" << txconfig.cycletime <<
"usec, RX DO TX = (" << txconfig.maxtimeRX << txconfig.maxtimeDO << txconfig.maxtimeTX <<
") usec and TX rate =" << txconfig.txratedivider <<
" every cycle";
345 yDebug() <<
"EthResource::setTimingOfRunningCycle() for BOARD" <<
getProperties().
boardnameString <<
"with IP" <<
getProperties().
ipv4addrString <<
"has succesfully set: cycletime =" << txconfig.cycletime <<
"usec, RX DO TX = (" << txconfig.maxtimeRX << txconfig.maxtimeDO << txconfig.maxtimeTX <<
") usec and TX rate =" << txconfig.txratedivider <<
" every cycle";
356bool EthResource::cleanBoardBehaviour(
void)
358 if(cleanedBoardBehaviour)
370 regularsAreSet =
false;
378 cleanedBoardBehaviour =
true;
384bool EthResource::testMultipleASK()
392 eOprotID32_t id32_commstatus = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_comm, 0, eoprot_tag_mn_comm_status);
393 eOmn_comm_status_t value_commstatus = {0};
395 eOprotID32_t id32_applconfig = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_appl, 0, eoprot_tag_mn_appl_config);
396 eOmn_appl_config_t value_applconfig = {0};
398 eOprotID32_t id32_applconfig_txratedivider = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_appl, 0, eoprot_tag_mn_appl_config_txratedivider);
399 uint8_t txratedivider = 0;
401 std::vector<eOprotID32_t> id32s;
402 std::vector<void*> values;
404 id32s.push_back(id32_commstatus);
405 values.push_back(&value_commstatus);
407 id32s.push_back(id32_applconfig);
408 values.push_back(&value_applconfig);
410 id32s.push_back(id32_applconfig_txratedivider);
411 values.push_back(&txratedivider);
413 id32s.push_back(id32_applconfig_txratedivider);
414 values.push_back(&txratedivider);
416 id32s.push_back(id32_applconfig_txratedivider);
417 values.push_back(&txratedivider);
419 id32s.push_back(id32_applconfig_txratedivider);
420 values.push_back(&txratedivider);
422 id32s.push_back(id32_applconfig_txratedivider);
423 values.push_back(&txratedivider);
425 id32s.push_back(id32_applconfig_txratedivider);
426 values.push_back(&txratedivider);
428 id32s.push_back(id32_applconfig_txratedivider);
429 values.push_back(&txratedivider);
431 id32s.push_back(id32_applconfig_txratedivider);
432 values.push_back(&txratedivider);
436 double tprev = SystemClock::nowSystem();
437 double tcurr = SystemClock::nowSystem();
439 double delta = tcurr - tprev;
441 yDebug() <<
"before";
442 yDebug() <<
"value_commstatus.managementprotocolversion.major = " << value_commstatus.managementprotocolversion.major <<
"value_commstatus.managementprotocolversion.minor = " << value_commstatus.managementprotocolversion.minor;
443 yDebug() <<
"value_applconfig.cycletime = " << value_applconfig.cycletime <<
"value_applconfig.txratedivider" << value_applconfig.txratedivider <<
"etc";
444 yDebug() <<
"txratedivider = " << txratedivider;
446 tprev = SystemClock::nowSystem();
449 delta = SystemClock::nowSystem() - tprev;
451 yDebug() <<
"parallel mode: after" << delta <<
"seconds";
452 yDebug() <<
"value_commstatus.managementprotocolversion.major = " << value_commstatus.managementprotocolversion.major <<
"value_commstatus.managementprotocolversion.minor = " << value_commstatus.managementprotocolversion.minor;
453 yDebug() <<
"value_applconfig.cycletime = " << value_applconfig.cycletime <<
"value_applconfig.txratedivider" << value_applconfig.txratedivider <<
"etc";
454 yDebug() <<
"txratedivider = " << txratedivider;
456 memset(&value_commstatus, 0,
sizeof(value_commstatus));
457 memset(&value_applconfig, 0,
sizeof(value_applconfig));
461 tprev = SystemClock::nowSystem();
462 nvman.
ask(&transceiver, id32_commstatus, &value_commstatus, 3.0);
463 nvman.
ask(&transceiver, id32_applconfig, &value_applconfig, 3.0);
464 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
465 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
466 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
467 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
468 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
469 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
470 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
471 nvman.
ask(&transceiver, id32_applconfig_txratedivider, &txratedivider, 3.0);
472 delta = SystemClock::nowSystem() - tprev;
474 yDebug() <<
"serial mode: after" << delta <<
"seconds";
475 yDebug() <<
"value_commstatus.managementprotocolversion.major = " << value_commstatus.managementprotocolversion.major <<
"value_commstatus.managementprotocolversion.minor = " << value_commstatus.managementprotocolversion.minor;
476 yDebug() <<
"value_applconfig.cycletime = " << value_applconfig.cycletime <<
"value_applconfig.txratedivider" << value_applconfig.txratedivider <<
"etc";
477 yDebug() <<
"txratedivider = " << txratedivider;
489 if((uint8_t)ep >= eoprot_endpoints_numberof)
491 yError() <<
"EthResource::verifyEPprotocol() called with wrong ep = " << ep <<
": cannot proceed any further";
495 if(
true == verifiedEPprotocol[ep])
500 if(
false == verifyBoard())
506 if(
false == askBoardVersion())
521 const double timeout = 0.100;
523 eOprotID32_t id2send = eo_prot_ID32dummy;
524 eOprotID32_t id2wait = eo_prot_ID32dummy;
525 eOmn_command_t command = {0};
529 id2send = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_comm, 0, eoprot_tag_mn_comm_cmmnds_command_queryarray);
530 memset(&command, 0,
sizeof(command));
531 command.cmd.opc = eomn_opc_query_array_EPdes;
532 command.cmd.queryarray.opcpar.opc = eomn_opc_query_array_EPdes;
533 command.cmd.queryarray.opcpar.endpoint = eoprot_endpoint_all;
534 command.cmd.queryarray.opcpar.setnumber = 0;
535 command.cmd.queryarray.opcpar.setsize = 0;
538 id2wait = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_comm, 0, eoprot_tag_mn_comm_cmmnds_command_replyarray);
542 if(
false == nvman.
command(properties.
ipv4addr, id2send, &command, id2wait, &command, timeout))
549 eOmn_cmd_replyarray_t* cmdreplyarray = (eOmn_cmd_replyarray_t*)&command.cmd.replyarray;
550 EOarray* array = (EOarray*)cmdreplyarray->array;
553 uint8_t sizeofarray = eo_array_Size(array);
556 for(
int i=0; i<sizeofarray; i++)
558 eoprot_endpoint_descriptor_t *epd = (eoprot_endpoint_descriptor_t*)eo_array_At(array, i);
560 if(epd->endpoint == eoprot_endpoint_management)
562 const eoprot_version_t * pc104versionMN = eoprot_version_of_endpoint_get(eoprot_endpoint_management);
563 if(pc104versionMN->major != epd->version.major)
565 yError() <<
"EthResource::verifyEPprotocol() for ep =" << eoprot_EP2string(epd->endpoint) <<
"detected: pc104.version.major =" << pc104versionMN->major <<
"and board.version.major =" << epd->version.major;
570 if(pc104versionMN->minor != epd->version.minor)
572 yError() <<
"EthResource::verifyEPprotocol() for ep =" << eoprot_EP2string(epd->endpoint) <<
"detected: pc104.version.minor =" << pc104versionMN->minor <<
"and board.version.minor =" << epd->version.minor;
578 if(epd->endpoint == ep)
580 const eoprot_version_t * pc104versionEP = eoprot_version_of_endpoint_get(ep);
581 if(pc104versionEP->major != epd->version.major)
583 yError() <<
"EthResource::verifyEPprotocol() for ep =" << eoprot_EP2string(epd->endpoint) <<
"detected: pc104.version.major =" << pc104versionEP->major <<
"and board.version.major =" << epd->version.major;
588 if(pc104versionEP->minor != epd->version.minor)
590 yError() <<
"EthResource::verifyEPprotocol() for ep =" << eoprot_EP2string(epd->endpoint) <<
"detected: pc104.version.minor =" << pc104versionEP->minor <<
"and board.version.minor =" << epd->version.minor;
598 verifiedEPprotocol[ep] =
true;
606bool EthResource::verifyBoard(
void)
608 if((
true == verifyBoardPresence()) &&
609 (
true == verifyBoardTransceiver()) &&
610 (
true == cleanBoardBehaviour()) &&
611 (
true == setTimingOfRunningCycle()) )
620bool EthResource::verifyBoardPresence(
void)
622 if(verifiedBoardPresence)
627 const double timeout = 1.00;
628 const int retries = 20;
630 double start_time = yarp::os::Time::now();
633 verifiedBoardPresence = nvman.
ping(properties.
ipv4addr, boardMNprotocolversion, timeout, retries);
635 double end_time = yarp::os::Time::now();
637 if(
true == verifiedBoardPresence)
639 verifiedBoardPresence =
true;
650 return(verifiedBoardPresence);
654bool EthResource::askBoardVersion(
void)
657 if(askedBoardVersion)
662 const double timeout = 0.500;
664 eOprotID32_t id32 = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_appl, 0, eoprot_tag_mn_appl_status);
665 eOmn_appl_status_t applstatus = {0};
669 askedBoardVersion = nvman.
ask(properties.
ipv4addr, id32, &applstatus, timeout);
671 if(
false == askedBoardVersion)
684 properties.
firmwaredate.year = applstatus.buildate.year;
685 properties.
firmwaredate.month = applstatus.buildate.month;
687 properties.
firmwaredate.hour = applstatus.buildate.hour;
690 char versstr[32] = {0};
692 char datestr[32] = {0};
693 eo_common_date_to_string(properties.
firmwaredate, datestr,
sizeof(datestr));
698 if(eobool_true == eoboards_is_eth((eObrd_type_t)applstatus.boardtype))
700 detectedBoardType = (eObrd_ethtype_t) applstatus.boardtype;
704 detectedBoardType = eobrd_ethtype_unknown;
707 if(detectedBoardType != properties.
boardtype)
709 yWarning() <<
"EthResource::askBoardVersion(): detected wrong board. expecting" << properties.
boardtypeString <<
"and detected" << eoboards_type2string2(eoboards_ethtype2type(detectedBoardType), eobool_true);
716 return(askedBoardVersion);
722 bool replied =
false;
724 double start_time = yarp::os::Time::now();
728 for(
unsigned int numOfattempts=0; numOfattempts<(retries+1); numOfattempts++)
730 if(
true == nvman.
ask(&transceiver, id32, value, timeout))
744 double end_time = yarp::os::Time::now();
759 double start_time = yarp::os::Time::now();
761 bool replied = nvman.
ask(&transceiver, id32s, values, timeout);
763 double end_time = yarp::os::Time::now();
777 return nvman.
set(properties.
ipv4addr, id32, value);
783 return nvman.
setcheck(properties.
ipv4addr, id32, value, retries, waitbeforecheck, timeout);
789 char canfullmessage[128];
791 static const char * sourcestrings[] =
798 int source = EOMN_INFO_PROPERTIES_FLAGS_get_source(infobasic->properties.flags);
799 const char * str_source = (source > eomn_info_source_can2) ? (sourcestrings[3]) : (sourcestrings[source]);
800 uint16_t address = EOMN_INFO_PROPERTIES_FLAGS_get_address(infobasic->properties.flags);
801 uint8_t *p64 = (uint8_t*)&(infobasic->properties.par64);
803 int msg_id = (p64[1]&0xF0) >> 4;
805 uint32_t sec = infobasic->timestamp / 1000000;
806 uint32_t msec = (infobasic->timestamp % 1000000) / 1000;
807 uint32_t usec = infobasic->timestamp % 1000;
814 snprintf(canfullmessage,
sizeof(canfullmessage),
"Error while parsing the message: CAN address detected is out of allowed range");
815 snprintf(str,
sizeof(str),
"from BOARD %s (%s), src %s, adr %d, time %ds %dm %du: CAN PRINT MESSAGE[id %d] -> %s",
831 if (c_string_handler[address] == NULL)
835 can_msg.
setCanData(infobasic->properties.par64);
836 can_msg.
setId(msg_id);
837 can_msg.
setSize(infobasic->properties.par16);
838 int ret = c_string_handler[address]->
add_string(&can_msg);
843 char* themsg = c_string_handler[address]->
get_string(ret);
844 memcpy(canfullmessage, themsg,
sizeof(canfullmessage));
845 canfullmessage[63] = 0;
848 snprintf(str,
sizeof(str),
"from BOARD %s (%s), src %s, adr %d, time %ds %dm %du: CAN PRINT MESSAGE[id %d] -> %s",
866bool EthResource::serviceCommand(eOmn_serv_operation_t operation, eOmn_serv_category_t category,
const eOmn_serv_parameter_t* param,
double timeout,
int times)
868 eOprotID32_t id2send = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_service, 0, eoprot_tag_mn_service_cmmnds_command);
869 eOprotID32_t id2wait = eoprot_ID_get(eoprot_endpoint_management, eoprot_entity_mn_service, 0, eoprot_tag_mn_service_status_commandresult);
871 eOmn_service_cmmnds_command_t command = {0};
872 eOmn_service_command_result_t result = {0};
874 command.operation = operation;
875 command.category = category;
878 memcpy(&command.parameter, param,
sizeof(eOmn_serv_parameter_t));
882 memset(&command.parameter, 0,
sizeof(eOmn_serv_parameter_t));
883 if((eomn_serv_operation_regsig_load == operation) || ((eomn_serv_operation_regsig_clear == operation)))
885 eo_array_New(eOmn_serv_capacity_arrayof_id32, 4, &command.parameter.arrayofid32);
889 command.parameter.configuration.type = eomn_serv_NONE;
896 bool replied =
false;
897 for(
int i=0; i<times; i++)
899 if(
true == nvman.
command(properties.
ipv4addr, id2send, &command, id2wait, &result, timeout))
914 return(result.latestcommandisok);
920 return(serviceCommand(eomn_serv_operation_verifyactivate, category, param, timeout, 3));
926 eOmn_serv_parameter_t param = {0};
927 EOarray *array = eo_array_New(eOmn_serv_capacity_arrayof_id32, 4, ¶m.arrayofid32);
928 for(
int i=0; i<id32vector.size(); i++)
930 eOprotID32_t id32 = id32vector.at(i);
931 eo_array_PushBack(array, &id32);
934 regularsAreSet = serviceCommand(eomn_serv_operation_regsig_load, category, ¶m, timeout, 3);
936 return regularsAreSet;
943 bool ret = serviceCommand(eomn_serv_operation_start, category, NULL, timeout, 3);
947 isInRunningMode =
true;
956 bool ret = serviceCommand(eomn_serv_operation_stop, category, NULL, timeout, 3);
958 if(ret && (category == eomn_serv_category_all))
960 regularsAreSet =
false;
972 return transceiver.
read(id32,
data);
978 return transceiver.
write(id32, value, overrideROprotection);
void setSize(uint8_t size)
void setCanData(uint64_t data)
char * get_string(int buffer_num)
int add_string(CanFrame *can_packet)
Process a string can packet.
void clear_string(int buffer_num)
Resets the string buffer.
void config(const Config &cfg)
bool getRemoteValues(const std::vector< eOprotID32_t > &id32s, const std::vector< void * > &values, const double timeout=0.500)
const void * getUDPtransmit(eOipv4addressing_t &destination, size_t &sizeofpacket, uint16_t &numofrops)
bool processRXpacket(const void *data, size_t size)
HostTransceiver * getTransceiver()
bool getLocalValue(const eOprotID32_t id32, void *value)
bool serviceSetRegulars(eOmn_serv_category_t category, vector< eOprotID32_t > &id32vector, double timeout=0.500)
bool open2(eOipv4addr_t remIP, yarp::os::Searchable &cfgtotal)
bool serviceStop(eOmn_serv_category_t category, double timeout=0.500)
bool verifyEPprotocol(eOprot_endpoint_t ep)
bool getRemoteValue(const eOprotID32_t id32, void *value, const double timeout=0.100, const unsigned int retries=0)
bool setLocalValue(eOprotID32_t id32, const void *value, bool overrideROprotection=false)
bool setRemoteValue(const eOprotID32_t id32, void *value)
bool setcheckRemoteValue(const eOprotID32_t id32, void *value, const unsigned int retries=10, const double waitbeforecheck=0.001, const double timeout=0.050)
bool serviceVerifyActivate(eOmn_serv_category_t category, const eOmn_serv_parameter_t *param, double timeout=0.500)
const Properties & getProperties()
bool serviceStart(eOmn_serv_category_t category, double timeout=0.500)
bool CANPrintHandler(eOmn_info_basic_t *infobasic)
static TheEthManager * instance()
const eOipv4addressing_t & getLocalIPV4addressing(void)
static theNVmanager & getInstance()
bool setcheck(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value, const unsigned int retries=10, double waitbeforecheck=0.001, double timeout=0.5)
bool ask(const eOprotIP_t ipv4, const eOprotID32_t id32, void *value, const double timeout=0.5)
bool command(const eOprotIP_t ipv4, const eOprotID32_t id32cmd, const void *cmd, const eOprotID32_t id32rep, void *rep, double timeout=0.5)
bool ping(const eOprotIP_t ipv4, eoprot_version_t &mnprotversion, const double timeout=0.5, const unsigned int retries=20)
bool set(const eOprotIP_t ipv4, const eOprotID32_t id32, const void *value)
bool write(const eOprotID32_t id32, const void *data, bool forcewriteOfReadOnly)
bool init2(AbstractEthResource *owner, yarp::os::Searchable &cfgtotal, eOipv4addressing_t &localIPaddressing, eOipv4addr_t remoteIP, uint16_t rxpktsize=maxSizeOfRXpacket)
const void * getUDP(size_t &size, uint16_t &numofrops)
bool parseUDP(const void *data, const uint16_t size)
bool read(const eOprotID32_t id32, void *data)
void feat_PrintInfo(char *string)
void feat_PrintError(char *string)
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)
bool print(const pc104Data &pc104data)
string ipv4addressingString
eOipv4addressing_t ipv4addressing
eObrd_ethtype_t boardtype
eOversion_t firmwareversion
double periodmissingreport
double monitorpresence_periodofmissingreport
bool monitorpresence_enabled
double monitorpresence_timeout
boardProperties properties
eOipv4addressing_t ipv4addressing
std::string ipv4addressingstring
eOmn_appl_config_t txconfig