12 #include <yarp/os/Time.h>
13 #include <yarp/os/Bottle.h>
15 #include "EoUpdaterProtocol.h"
17 using namespace yarp::os;
53 for (i = 0x700; i < 0x7FF; i++) iCanBus->canIdAdd (i);
54 for (i = 0x200; i < 0x2FF; i++) iCanBus->canIdAdd (i);
56 iCanBus->canSetBaudRate(0);
58 yarp::os::Time::delay(2.0);
82 maxreadmsg = canpackets.size();
84 if (howMany>maxreadmsg)
87 unsigned int how_many_messages=0;
91 double start=Time::now();
99 ret=iCanBus->canRead(canRXbuffer, maxreadmsg, &how_many_messages,
false);
101 if (
read+how_many_messages>maxreadmsg)
103 how_many_messages=(maxreadmsg-
read);
106 for(
unsigned int k=0;k<how_many_messages;k++)
109 canpackets[
read].setLen(canRXbuffer[k].getLen());
110 canpackets[
read].setId(canRXbuffer[k].getId());
111 memcpy(canpackets[
read].getData(), canRXbuffer[k].getData(), canRXbuffer[k].getLen());
124 if ( (now-start)>TIMEOUT)
142 if(
n > canpackets.size())
144 n = canpackets.size();
153 for(
int i=0; i<
n; i++)
157 canTXbuffer[i].setId(canpackets[i].getId());
158 canTXbuffer[i].setLen(canpackets[i].getLen());
159 void * src = canpackets[i].getData();
160 void * dst = canTXbuffer[i].getData();
161 memcpy(dst, src, canpackets[i].getLen());
166 bool ret = iCanBus->canWrite(canTXbuffer,
n, &sent);
176 yarp::dev::CanBuffer cDriver2::createCanBuffer(
int m)
178 return iFactory->createBuffer(m);
182 void cDriver2::destroyCanBuffer(yarp::dev::CanBuffer &buff)
184 iFactory->destroyBuffer(buff);
204 #undef USE_LEGACY_0X20_MESSAGE
208 ACE_UINT32 local=(ACE_UINT32)config.find(
"local").asInt32();
214 if (!mSocket->create(3334,local))
216 yError(
"invalid address\n");
220 mBoardAddr=(ACE_UINT32)config.find(
"remote").asInt32();
225 timestart = yarp::os::Time::now();
239 yarp::os::Time::delay(2.0);
243 #if defined(USE_LEGACY_0X20_MESSAGE)
248 static unsigned char cmd_cangtw_start[8] = {0x20, 0, 0, 0, 0, 0, 0, 0};
249 if(_verbose) yDebug() <<
"byte is ...." << cmd_cangtw_start[0];
251 mSocket->sendTo(cmd_cangtw_start, 1, 3333, mBoardAddr);
258 yarp::os::Time::delay(1.5);
262 if(_verbose) yWarning(
"sending request to start can gateway mode");
264 const uint16_t t_can_stabilisation = 900;
265 const uint8_t b_send_ff_after_can_stabilisation = 0;
266 const uint16_t t_wait_for_can_reply = 0;
267 const uint8_t b_clear_can_buffers_on_start_gtw = 0;
268 const uint8_t b_send_ack = 1;
282 eOuprot_cmd_CANGATEWAY_t command = {0};
283 command.opc = uprot_OPC_LEGACY_CANGATEWAY;
284 command.sendcanbroadcast = b_send_ff_after_can_stabilisation;
285 command.time4canstable = t_can_stabilisation;
286 command.time2waitcanreply = t_wait_for_can_reply;
287 command.rxcanbufferclear = b_clear_can_buffers_on_start_gtw;
288 command.ackrequired = b_send_ack;
291 mSocket->sendTo(&command,
sizeof(eOuprot_cmd_CANGATEWAY_t), 3333, mBoardAddr);
297 const double waitingTime = 1.5;
302 eOuprot_cmdREPLY_t cmdreply = {0};
304 if(_verbose) yDebug(
"waiting for an ack/nak from board");
308 int mswait = 1000.0f * waitingTime;
309 int nrec = mSocket->receiveFrom(&cmdreply,
sizeof(cmdreply), address, port, mswait);
313 if(_verbose) yWarning(
"remote board did not sent any ack to command 0x20. it may have a version of eUpdater older than 6.6");
316 else if(
sizeof(cmdreply) == nrec)
318 if((uprot_OPC_CANGATEWAY == cmdreply.opc) && (uprot_RES_OK == cmdreply.res))
320 if(_verbose) yDebug(
"REMOTE BOARD is in CAN gateway now");
323 else if((uprot_OPC_CANGATEWAY == cmdreply.opc) && (uprot_RES_OK != cmdreply.res))
325 if(uprot_RES_ERR_TRYAGAIN == cmdreply.res)
327 if(_verbose) yWarning(
"REMOTE BOARD tells that it cannot go to CAN gateway mode, BUT: The eApplication has jumped to eUpdater. Try connect again.");
332 if(_verbose) yWarning(
"REMOTE BOARD tells that it cannot go to CAN gateway mode.");
338 uint8_t *bb = (uint8_t*) &cmdreply;
339 if(_verbose) yWarning(
"REMOTE BOARD sends an unknown reply[%d] = {%x, %x, %x, %x}.", nrec, bb[0], bb[1], bb[2], bb[3]);
345 uint8_t *bb = (uint8_t*) &cmdreply;
346 if(_verbose) yWarning(
"REMOTE BOARD sends an unknown reply[%d] = {%x, %x, %x, %x}.", nrec, bb[0], bb[1], bb[2], bb[3]);
353 yarp::os::Time::delay(waitingTime);
384 double tstart=yarp::os::Time::now();
390 int nrec=mSocket->receiveFrom(&canPkt,
sizeof(
CanPkt_t),address,port,1);
397 if (address==mBoardAddr && port==3334)
401 for (
int f=0;
f<nframes; ++
f)
410 canpackets[nread].setCanBus(canbus);
412 canpackets[nread].setLen(canPkt.
frames[
f].
len);
416 if (++nread>=howMany)
return nread;
422 if (yarp::os::Time::now()-tstart>TIMEOUT)
break;
432 static ACE_UINT32 prognumber = 0;
435 for (
int i=0; i<
n; ++i)
445 canPkt.
frames[0].
len = canpackets[i].getLen();
446 memcpy(canPkt.
frames[0].
data, canpackets[i].getData(), canpackets[i].getLen());
455 mSocket->sendTo(&canPkt,
sizeof(
CanPkt_t), 3334, mBoardAddr);
456 yarp::os::Time::delay(0.001);
459 mSocket->sendTo(&canPkt,
sizeof(
CanPkt_t), 3334, mBoardAddr);
460 yarp::os::Time::delay(0.001);
464 mSocket->sendTo(&canPkt,
sizeof(
CanPkt_t), 3334, mBoardAddr);
465 yarp::os::Time::delay(0.001);
482 #if defined(DRIVER_KEEP_LEGACY_IDRIVER)
507 for (i = 0x700; i < 0x7FF; i++) iCanBus->canIdAdd (i);
508 for (i = 0x200; i < 0x2FF; i++) iCanBus->canIdAdd (i);
510 iCanBus->canSetBaudRate(0);
512 yarp::os::Time::delay(2.0);
532 unsigned int how_many_messages=0;
536 double start=Time::now();
544 ret=iCanBus->canRead(tmpBuff,
MAX_READ_MSG, &how_many_messages,
false);
551 for(
unsigned int k=0;k<how_many_messages;k++)
553 messages[
read]=tmpBuff[k];
565 if ( (now-start)>TIMEOUT)
572 destroyBuffer(tmpBuff);
584 bool ret = iCanBus->canWrite(message, messages, &sent);
595 return iFactory->createBuffer(m);
601 iFactory->destroyBuffer(buff);
611 ACE_UINT32 local=(ACE_UINT32)config.find(
"local").asInt32();
613 if (!mSocket.create(3334,local))
615 yError(
"invalid address\n");
619 mBoardAddr=(ACE_UINT32)config.find(
"remote").asInt32();
621 mCanBusId=config.check(
"canid")?config.find(
"canid").asInt32():0;
624 timestart = yarp::os::Time::now();
633 yarp::os::Time::delay(1.75);
642 unsigned char CMD_CANGTW_START=0x20;
644 mSocket.sendTo(&CMD_CANGTW_START,1,3333,mBoardAddr);
652 yarp::os::Time::delay(0.25);
661 static char CMD_CANGTW_STOP = 0x21;
663 mSocket.sendTo(&CMD_CANGTW_STOP,1,3334,mBoardAddr);
676 double tstart=yarp::os::Time::now();
682 int nrec=mSocket.receiveFrom(&canPkt,
sizeof(
CanPkt_t),address,port,1);
686 if (address==mBoardAddr && port==3334)
690 for (
int f=0;
f<nframes; ++
f)
702 if (++nread>=howMany)
return nread;
708 if (yarp::os::Time::now()-tstart>TIMEOUT)
break;
717 static ACE_UINT32 prognumber = 0;
720 for (
int i=0; i<
n; ++i)
730 canPkt.
frames[0].
len=message[i].getLen();
731 memcpy(canPkt.
frames[0].
data,message[i].getData(),message[i].getLen());
737 mSocket.sendTo(&canPkt,
sizeof(
CanPkt_t),3334,mBoardAddr);
744 yarp::os::Time::delay(0.001);
752 return yarp::dev::ImplementCanBufferFactory<EthCanMessage,ECMSG>::createBuffer(m);
757 yarp::dev::ImplementCanBufferFactory<EthCanMessage,ECMSG>::destroyBuffer(buff);
int receive_message(vector< CanPacket > &canpackets, int howMany=MAX_READ_MSG, double TIMEOUT=1)
int init(yarp::os::Searchable &config, bool verbose=true)
int send_message(vector< CanPacket > &canpackets, int n)
int receive_message(yarp::dev::CanBuffer &messages, int howMany=MAX_READ_MSG, double TIMEOUT=1)
int send_message(yarp::dev::CanBuffer &message, int n)
int init(yarp::os::Searchable &config)
void destroyBuffer(yarp::dev::CanBuffer &buff)
yarp::dev::CanBuffer createBuffer(int m)
int init(yarp::os::Searchable &config, bool verbose=true)
int receive_message(vector< CanPacket > &canpackets, int howMany=MAX_READ_MSG, double TIMEOUT=1)
int send_message(vector< CanPacket > &canpackets, int n)
int init(yarp::os::Searchable &config)
void destroyBuffer(yarp::dev::CanBuffer &buff)
int receive_message(yarp::dev::CanBuffer &messages, int howMany=MAX_READ_MSG, double TIMEOUT=1.0)
int send_message(yarp::dev::CanBuffer &message, int n)
yarp::dev::CanBuffer createBuffer(int m)
bool read(yarp::os::Searchable &cfgtotal, pc104Data &pc104data)