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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #include <sys/types.h>
00069 #include <sys/ioctl.h>
00070 #include <net/if.h>
00071 #include <sys/socket.h>
00072 #include <unistd.h>
00073 #include <sys/time.h>
00074 #include <arpa/inet.h>
00075 #include <stdio.h>
00076 #include <fcntl.h>
00077 #include <string.h>
00078 #include <netpacket/packet.h>
00079 #include <pthread.h>
00080 #include <stdlib.h>
00081
00082 #include "ethercattype.h"
00083 #include "nicdrv.h"
00084
00085
00086 enum
00087 {
00088
00089 ECT_RED_NONE,
00090
00091 ECT_RED_DOUBLE
00092 };
00093
00094
00095 typedef struct
00096 {
00097
00098 int *sock;
00099
00100 ec_bufT (*txbuf)[EC_MAXBUF];
00101
00102 int (*txbuflength)[EC_MAXBUF];
00103
00104 ec_bufT *tempbuf;
00105
00106 ec_bufT (*rxbuf)[EC_MAXBUF];
00107
00108 int (*rxbufstat)[EC_MAXBUF];
00109
00110 int (*rxsa)[EC_MAXBUF];
00111 } ec_stackT;
00112
00113
00114 ec_bufT ec_rxbuf[EC_MAXBUF];
00115
00116 int ec_rxbufstat[EC_MAXBUF];
00117
00118 static int ec_rxsa[EC_MAXBUF];
00119
00120 static ec_bufT ec_tempinbuf;
00121
00122 static int ec_tempinbufs;
00123
00124
00125 static ec_bufT ec_rxbuf2[EC_MAXBUF];
00126
00127 static int ec_rxbufstat2[EC_MAXBUF];
00128
00129 static int ec_rxsa2[EC_MAXBUF];
00130
00131 static ec_bufT ec_tempinbuf2;
00132
00133
00134 ec_bufT ec_txbuf[EC_MAXBUF];
00135
00136 int ec_txbuflength[EC_MAXBUF];
00137
00138 ec_bufT ec_txbuf2;
00139
00140 int ec_txbuflength2;
00141
00142
00143 int sockhandle = -1;
00144
00145 int sockhandle2 = -1;
00146
00147
00148 static ec_stackT ec_stack[2]=
00149 {{&sockhandle, &ec_txbuf, &ec_txbuflength, &ec_tempinbuf, &ec_rxbuf, &ec_rxbufstat, &ec_rxsa},
00150 {&sockhandle2, &ec_txbuf, &ec_txbuflength, &ec_tempinbuf2, &ec_rxbuf2, &ec_rxbufstat2, &ec_rxsa2}};
00151
00152
00153 static uint8 ec_lastidx;
00154
00155 int ec_incnt;
00156
00157 int ec_errcnt;
00158
00159 int ec_redstate;
00160
00161
00162 int hlp_txtime;
00163
00164 int hlp_rxtime;
00165
00166
00167
00168
00169
00170
00171 const uint16 priMAC[3] = { 0x0101, 0x0101, 0x0101 };
00172
00173 const uint16 secMAC[3] = { 0x0404, 0x0404, 0x0404 };
00174
00175
00176 #define RX_PRIM priMAC[1]
00177
00178 #define RX_SEC secMAC[1]
00179
00180 pthread_mutex_t ec_getindex_mutex = PTHREAD_MUTEX_INITIALIZER;
00181 pthread_mutex_t ec_tx_mutex = PTHREAD_MUTEX_INITIALIZER;
00182 pthread_mutex_t ec_rx_mutex = PTHREAD_MUTEX_INITIALIZER;
00183
00184
00185
00186
00187
00188
00189 int ec_setupnic(const char * ifname, int secondary)
00190 {
00191 int i;
00192 int r, rval, ifindex, fl;
00193 struct timeval timeout;
00194 struct ifreq ifr;
00195 struct sockaddr_ll sll;
00196 int *psock;
00197
00198 rval = 0;
00199 if (secondary)
00200 {
00201
00202 psock = &sockhandle2;
00203 ec_redstate = ECT_RED_DOUBLE;
00204 }
00205 else
00206 {
00207 psock = &sockhandle;
00208 ec_redstate = ECT_RED_NONE;
00209 }
00210
00211 *psock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ECAT));
00212 timeout.tv_sec = 0;
00213 timeout.tv_usec = 1;
00214
00215 r = setsockopt(*psock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
00216 r = setsockopt(*psock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
00217 i = 1;
00218 r = setsockopt(*psock, SOL_SOCKET, SO_DONTROUTE, &i, sizeof(i));
00219
00220 strcpy(ifr.ifr_name, ifname);
00221 r = ioctl(*psock, SIOCGIFINDEX, &ifr);
00222 ifindex = ifr.ifr_ifindex;
00223 strcpy(ifr.ifr_name, ifname);
00224 ifr.ifr_flags = 0;
00225
00226 r = ioctl(*psock, SIOCGIFFLAGS, &ifr);
00227
00228 ifr.ifr_flags = ifr.ifr_flags || IFF_PROMISC || IFF_BROADCAST;
00229 r = ioctl(*psock, SIOCGIFFLAGS, &ifr);
00230
00231 sll.sll_family = AF_PACKET;
00232 sll.sll_ifindex = ifindex;
00233 sll.sll_protocol = htons(ETH_P_ECAT);
00234 r = bind(*psock, (struct sockaddr *)&sll, sizeof(sll));
00235
00236 fl = fcntl(*psock, F_GETFL, 0);
00237
00238
00239
00240 for (i = 0; i < EC_MAXBUF; i++)
00241 {
00242 ec_setupheader(&ec_txbuf[i]);
00243 ec_rxbufstat[i] = EC_BUF_EMPTY;
00244 }
00245 ec_setupheader(&ec_txbuf2);
00246 ec_errcnt = ec_incnt = 0;
00247 if (r == 0) rval = 1;
00248
00249 return rval;
00250 }
00251
00252
00253
00254
00255 int ec_closenic(void)
00256 {
00257 if (sockhandle >= 0) close(sockhandle);
00258 if (sockhandle2 >= 0) close(sockhandle2);
00259
00260 return 0;
00261 }
00262
00263
00264
00265
00266
00267
00268 void ec_setupheader(void *p)
00269 {
00270 ec_etherheadert *bp;
00271 bp = p;
00272 bp->da0 = htons(0xffff);
00273 bp->da1 = htons(0xffff);
00274 bp->da2 = htons(0xffff);
00275 bp->sa0 = htons(priMAC[0]);
00276 bp->sa1 = htons(priMAC[1]);
00277 bp->sa2 = htons(priMAC[2]);
00278 bp->etype = htons(ETH_P_ECAT);
00279 }
00280
00281
00282
00283
00284 uint8 ec_getindex(void)
00285 {
00286 uint8 idx;
00287 int cnt;
00288
00289 pthread_mutex_lock( &ec_getindex_mutex );
00290
00291 idx = ec_lastidx + 1;
00292
00293 if (idx >= EC_MAXBUF)
00294 {
00295 idx = 0;
00296 }
00297 cnt = 0;
00298
00299 while ((ec_rxbufstat[idx] != EC_BUF_EMPTY) && (cnt < EC_MAXBUF))
00300 {
00301 idx++;
00302 cnt++;
00303 if (idx >= EC_MAXBUF)
00304 {
00305 idx = 0;
00306 }
00307 }
00308 ec_rxbufstat[idx] = EC_BUF_ALLOC;
00309 ec_rxbufstat2[idx] = EC_BUF_ALLOC;
00310 ec_lastidx = idx;
00311
00312 pthread_mutex_unlock( &ec_getindex_mutex );
00313
00314 return idx;
00315 }
00316
00317
00318
00319
00320
00321 void ec_setbufstat(uint8 idx, int bufstat)
00322 {
00323 ec_rxbufstat[idx] = bufstat;
00324 ec_rxbufstat2[idx] = bufstat;
00325 }
00326
00327
00328
00329
00330
00331
00332 int ec_outframe(uint8 idx, int stacknumber)
00333 {
00334 int lp, rval;
00335 ec_stackT *stack;
00336
00337 stack = &ec_stack[stacknumber];
00338 lp = (*stack->txbuflength)[idx];
00339 rval = send(*stack->sock, (*stack->txbuf)[idx], lp, 0);
00340 (*stack->rxbufstat)[idx] = EC_BUF_TX;
00341
00342 return rval;
00343 }
00344
00345
00346
00347
00348
00349 int ec_outframe_red(uint8 idx)
00350 {
00351 ec_comt *datagramP;
00352 ec_etherheadert *ehp;
00353 int rval;
00354
00355 ehp = (ec_etherheadert *)&ec_txbuf[idx];
00356
00357 ehp->sa1 = htons(priMAC[1]);
00358
00359 rval = ec_outframe(idx, 0);
00360 if (ec_redstate != ECT_RED_NONE)
00361 {
00362 pthread_mutex_lock( &ec_tx_mutex );
00363 ehp = (ec_etherheadert *)&ec_txbuf2;
00364
00365 datagramP = (ec_comt*)&ec_txbuf2[ETH_HEADERSIZE];
00366
00367 datagramP->index = idx;
00368
00369 ehp->sa1 = htons(secMAC[1]);
00370
00371 send(sockhandle2, &ec_txbuf2, ec_txbuflength2 , 0);
00372 pthread_mutex_unlock( &ec_tx_mutex );
00373 ec_rxbufstat2[idx] = EC_BUF_TX;
00374 }
00375
00376 return rval;
00377 }
00378
00379
00380
00381
00382
00383 static int ec_recvpkt(int stacknumber)
00384 {
00385 int lp, bytesrx;
00386 ec_stackT *stack;
00387
00388 stack = &ec_stack[stacknumber];
00389 lp = sizeof(ec_tempinbuf);
00390 bytesrx = recv(*stack->sock, (*stack->tempbuf), lp, 0);
00391 ec_tempinbufs = bytesrx;
00392
00393 return (bytesrx > 0);
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411 int ec_inframe(uint8 idx, int stacknumber)
00412 {
00413 uint16 l;
00414 int rval;
00415 uint8 idxf;
00416 ec_etherheadert *ehp;
00417 ec_comt *ecp;
00418 ec_stackT *stack;
00419 ec_bufT *rxbuf;
00420
00421 stack = &ec_stack[stacknumber];
00422 rval = EC_NOFRAME;
00423 rxbuf = &(*stack->rxbuf)[idx];
00424
00425 if ((idx < EC_MAXBUF) && ( (*stack->rxbufstat)[idx] == EC_BUF_RCVD))
00426 {
00427 l = (*rxbuf)[0] + ((uint16)((*rxbuf)[1] & 0x0f) << 8);
00428
00429 rval = ((*rxbuf)[l] + ((uint16)(*rxbuf)[l + 1] << 8));
00430
00431 (*stack->rxbufstat)[idx] = EC_BUF_COMPLETE;
00432 }
00433 else
00434 {
00435 pthread_mutex_lock( &ec_rx_mutex );
00436
00437 if (ec_recvpkt( stacknumber))
00438 {
00439 rval = EC_OTHERFRAME;
00440 ehp =(ec_etherheadert*)(stack->tempbuf);
00441
00442 if (ehp->etype == htons(ETH_P_ECAT))
00443 {
00444 ec_incnt++;
00445 ecp =(ec_comt*)(&(*stack->tempbuf)[ETH_HEADERSIZE]);
00446 l = etohs(ecp->elength) & 0x0fff;
00447 idxf = ecp->index;
00448
00449 if (idxf == idx)
00450 {
00451
00452 memcpy(rxbuf, &(*stack->tempbuf)[ETH_HEADERSIZE], (*stack->txbuflength)[idx] - ETH_HEADERSIZE);
00453
00454 rval = ((*rxbuf)[l] + ((uint16)((*rxbuf)[l + 1]) << 8));
00455
00456 (*stack->rxbufstat)[idx] = EC_BUF_COMPLETE;
00457
00458 (*stack->rxsa)[idx] = ntohs(ehp->sa1);
00459 }
00460 else
00461 {
00462 int size = (*stack->txbuflength)[idxf] - ETH_HEADERSIZE;
00463
00464 if (idxf < EC_MAXBUF && size > 0)
00465 {
00466 rxbuf = &(*stack->rxbuf)[idxf];
00467
00468 memcpy(rxbuf, &(*stack->tempbuf)[ETH_HEADERSIZE], (*stack->txbuflength)[idxf] - ETH_HEADERSIZE);
00469
00470 (*stack->rxbufstat)[idxf] = EC_BUF_RCVD;
00471 (*stack->rxsa)[idxf] = ntohs(ehp->sa1);
00472 }
00473 else
00474 {
00475
00476 ec_errcnt++;
00477 printf("There is a other instance of a EtherCAT master running!\n");
00478 exit(0);
00479 }
00480 }
00481 }
00482 }
00483 pthread_mutex_unlock( &ec_rx_mutex );
00484
00485 }
00486
00487
00488 return rval;
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502 static int ec_waitinframe_red(uint8 idx, struct timeval tvs)
00503 {
00504 struct timeval tv1,tv2;
00505 int wkc = EC_NOFRAME;
00506 int wkc2 = EC_NOFRAME;
00507 int primrx, secrx;
00508
00509
00510 if (ec_redstate == ECT_RED_NONE)
00511 wkc2 = 0;
00512 do
00513 {
00514
00515 if (wkc <= EC_NOFRAME)
00516 wkc = ec_inframe(idx, 0);
00517
00518 if (ec_redstate != ECT_RED_NONE)
00519 {
00520
00521 if (wkc2 <= EC_NOFRAME)
00522 wkc2 = ec_inframe(idx, 1);
00523 }
00524 gettimeofday(&tv1, 0);
00525
00526 } while (((wkc <= EC_NOFRAME) || (wkc2 <= EC_NOFRAME)) && (timercmp(&tv1, &tvs, <)));
00527
00528 if (ec_redstate != ECT_RED_NONE)
00529 {
00530
00531 primrx = 0;
00532 if (wkc > EC_NOFRAME) primrx = ec_rxsa[idx];
00533
00534 secrx = 0;
00535 if (wkc2 > EC_NOFRAME) secrx = ec_rxsa2[idx];
00536
00537
00538
00539 if ( ((primrx == RX_SEC) && (secrx == RX_PRIM)) )
00540 {
00541
00542 memcpy(&ec_rxbuf[idx], &ec_rxbuf2[idx], ec_txbuflength[idx] - ETH_HEADERSIZE);
00543 wkc = wkc2;
00544 }
00545
00546
00547 if ( ((primrx == 0) && (secrx == RX_SEC)) ||
00548 ((primrx == RX_PRIM) && (secrx == RX_SEC)) )
00549 {
00550
00551
00552
00553 if ( (primrx == RX_PRIM) && (secrx == RX_SEC) )
00554 {
00555
00556 memcpy(&ec_txbuf[idx][ETH_HEADERSIZE], &ec_rxbuf[idx], ec_txbuflength[idx] - ETH_HEADERSIZE);
00557 }
00558 gettimeofday(&tv1, 0);
00559 tv2.tv_sec = 0;
00560 tv2.tv_usec = EC_TIMEOUTRET;
00561 timeradd(&tv1, &tv2, &tvs);
00562
00563 ec_outframe(idx,1);
00564 do
00565 {
00566
00567 wkc2 = ec_inframe(idx, 1);
00568 gettimeofday(&tv1, 0);
00569 } while ((wkc2 <= EC_NOFRAME) && (timercmp(&tv1, &tvs, <)));
00570 if (wkc2 > EC_NOFRAME)
00571 {
00572
00573 memcpy(&ec_rxbuf[idx], &ec_rxbuf2[idx], ec_txbuflength[idx] - ETH_HEADERSIZE);
00574 wkc = wkc2;
00575 }
00576 }
00577 }
00578
00579
00580 return wkc;
00581 }
00582
00583
00584
00585
00586
00587
00588
00589 int ec_waitinframe(uint8 idx, int timeout)
00590 {
00591 int wkc;
00592 struct timeval tv1, tv2, tve;
00593
00594 gettimeofday(&tv1, 0);
00595 tv2.tv_sec = 0;
00596 tv2.tv_usec = timeout;
00597 timeradd(&tv1, &tv2, &tve);
00598 wkc = ec_waitinframe_red(idx, tve);
00599
00600 if (wkc <= EC_NOFRAME)
00601 {
00602 ec_setbufstat(idx, EC_BUF_EMPTY);
00603 }
00604
00605 return wkc;
00606 }
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619 int ec_srconfirm(uint8 idx, int timeout)
00620 {
00621 int wkc = EC_NOFRAME;
00622 struct timeval tv1, tv2, tv3, tve, tvs, tvh;
00623
00624 gettimeofday(&tv1, 0);
00625 tv2.tv_sec = 0;
00626 tv2.tv_usec = timeout;
00627 timeradd(&tv1, &tv2, &tve);
00628 do
00629 {
00630
00631 ec_outframe_red(idx);
00632 gettimeofday(&tv2, 0);
00633 timersub(&tv2, &tv1, &tvh);
00634 hlp_txtime += (int)tvh.tv_usec;
00635 tv1.tv_sec = 0;
00636 if (timeout < EC_TIMEOUTRET)
00637 {
00638 tv1.tv_usec = timeout;
00639 }
00640 else
00641 {
00642
00643 tv1.tv_usec = EC_TIMEOUTRET;
00644 }
00645 timeradd(&tv2, &tv1, &tvs);
00646
00647 wkc = ec_waitinframe_red(idx, tvs);
00648 gettimeofday(&tv3, 0);
00649 timersub(&tv3, &tv2, &tvh);
00650 hlp_rxtime += (int)tvh.tv_usec;
00651
00652 } while ((wkc <= EC_NOFRAME) && (timercmp(&tv3, &tve, <)));
00653
00654 if (wkc <= EC_NOFRAME)
00655 {
00656 ec_setbufstat(idx, EC_BUF_EMPTY);
00657 }
00658
00659 return wkc;
00660 }