Multithreaded-software/external/soem/oshw/rtk/nicdrv.c
int ecx_inframe(ecx_portt *port, int idx, int stacknumber)
{
   uint16  l;
   int     rval;
   uint8   idxf;
   ec_etherheadert *ehp;
   ec_comt *ecp;
   ec_stackT *stack;
   ec_bufT *rxbuf;

   if (!stacknumber)
   {
      stack = &(port->stack);
   }
   else
   {
      stack = &(port->redport->stack);
   }
   rval = EC_NOFRAME;
   rxbuf = &(*stack->rxbuf)[idx];
   /* check if requested index is already in buffer ? */
   if ((idx < EC_MAXBUF) && (   (*stack->rxbufstat)[idx] == EC_BUF_RCVD))
   {
      l = (*rxbuf)[0] + ((uint16)((*rxbuf)[1] & 0x0f) << 8);
      /* return WKC */
      rval = ((*rxbuf)[l] + ((uint16)(*rxbuf)[l + 1] << 8));
      /* mark as completed */
      (*stack->rxbufstat)[idx] = EC_BUF_COMPLETE;
   }
   else
   {
      mtx_lock (port->rx_mutex);
      /* non blocking call to retrieve frame from socket */
      if (ecx_recvpkt(port, stacknumber))
      {
         rval = EC_OTHERFRAME;
         ehp =(ec_etherheadert*)(stack->tempbuf);
         /* check if it is an EtherCAT frame */
         if (ehp->etype == oshw_htons(ETH_P_ECAT))
         {
            ecp =(ec_comt*)(&(*stack->tempbuf)[ETH_HEADERSIZE]);
            l = etohs(ecp->elength) & 0x0fff;
            idxf = ecp->index;
            /* found index equals reqested index ? */
            if (idxf == idx)
            {
               /* yes, put it in the buffer array (strip ethernet header) */
               memcpy(rxbuf, &(*stack->tempbuf)[ETH_HEADERSIZE], (*stack->txbuflength)[idx] - ETH_HEADERSIZE);
               /* return WKC */
               rval = ((*rxbuf)[l] + ((uint16)((*rxbuf)[l + 1]) << 8));
               /* mark as completed */
               (*stack->rxbufstat)[idx] = EC_BUF_COMPLETE;
               /* store MAC source word 1 for redundant routing info */
               (*stack->rxsa)[idx] = oshw_ntohs(ehp->sa1);
            }
            else
            {
               /* check if index exist and someone is waiting for it */
               if (idxf < EC_MAXBUF && (*stack->rxbufstat)[idxf] == EC_BUF_TX)
               {
                  rxbuf = &(*stack->rxbuf)[idxf];
                  /* put it in the buffer array (strip ethernet header) */
                  memcpy(rxbuf, &(*stack->tempbuf)[ETH_HEADERSIZE], (*stack->txbuflength)[idxf] - ETH_HEADERSIZE);
                  /* mark as received */
                  (*stack->rxbufstat)[idxf] = EC_BUF_RCVD;
                  (*stack->rxsa)[idxf] = oshw_ntohs(ehp->sa1);
               }
               else
               {
                  /* strange things happend */
               }
            }
         }
      }
      mtx_unlock (port->rx_mutex);

   }

   /* WKC if mathing frame found */
   return rval;
}
/** Non blocking receive frame function. Uses RX buffer and index to combine
 * read frame with transmitted frame. To compensate for received frames that
 * are out-of-order all frames are stored in their respective indexed buffer.
 * If a frame was placed in the buffer previously, the function retreives it
 * from that buffer index without calling ec_recvpkt. If the requested index
 * is not already in the buffer it calls ec_recvpkt to fetch it. There are
 * three options now, 1 no frame read, so exit. 2 frame read but other
 * than requested index, store in buffer and exit. 3 frame read with matching
 * index, store in buffer, set completed flag in buffer status and exit.
 *
 * @param[in] port        = port context struct
 * @param[in] idx         = requested index of frame
 * @param[in] stacknumber = 0=primary 1=secondary stack
 * @return Workcounter if a frame is found with corresponding index, otherwise
 * EC_NOFRAME or EC_OTHERFRAME.
 */
Non blocking receive frame function. Uses RX buffer and index to combine
read frame with transmitted frame. To compensate for received frames that
are out-of-order all frames are stored in their respective indexed buffer.
If a frame was placed in the buffer previously, the function retreives it
from that buffer index without calling ec_recvpkt. If the requested index
is not already in the buffer it calls ec_recvpkt to fetch it. There are
three options now, 1 no frame read, so exit. 2 frame read but other
than requested index, store in buffer and exit. 3 frame read with matching
index, store in buffer, set completed flag in buffer status and exit.


Non blocking receive frame function . Uses RX buffer and index to combine read frame with transmitted frame . To compensate for received frames that are out - of - order all frames are stored in their respective indexed buffer . If a frame was placed in the buffer previously the function retreives it from that buffer index without calling ec_recvpkt . If the requested index is not already in the buffer it calls ec_recvpkt to fetch it . There are three options now 1 no frame read so exit . 2 frame read but other than requested index store in buffer and exit . 3 frame read with matching index store in buffer set completed flag in buffer status and exit .
int ecx_inframe(ecx_portt *port, int idx, int stacknumber)
{
   uint16  l;
   int     rval;
   uint8   idxf;
   ec_etherheadert *ehp;
   ec_comt *ecp;
   ec_stackT *stack;
   ec_bufT *rxbuf;
   if (!stacknumber)
   {
      stack = &(port->stack);
   }
   else
   {
      stack = &(port->redport->stack);
   }
   rval = EC_NOFRAME;
   rxbuf = &(*stack->rxbuf)[idx];
   if ((idx < EC_MAXBUF) && (   (*stack->rxbufstat)[idx] == EC_BUF_RCVD))
   {
      l = (*rxbuf)[0] + ((uint16)((*rxbuf)[1] & 0x0f) << 8);
      rval = ((*rxbuf)[l] + ((uint16)(*rxbuf)[l + 1] << 8));
      (*stack->rxbufstat)[idx] = EC_BUF_COMPLETE;
   }
   else
   {
      mtx_lock (port->rx_mutex);
      if (ecx_recvpkt(port, stacknumber))
      {
         rval = EC_OTHERFRAME;
         ehp =(ec_etherheadert*)(stack->tempbuf);
         if (ehp->etype == oshw_htons(ETH_P_ECAT))
         {
            ecp =(ec_comt*)(&(*stack->tempbuf)[ETH_HEADERSIZE]);
            l = etohs(ecp->elength) & 0x0fff;
            idxf = ecp->index;
            if (idxf == idx)
            {
               memcpy(rxbuf, &(*stack->tempbuf)[ETH_HEADERSIZE], (*stack->txbuflength)[idx] - ETH_HEADERSIZE);
               rval = ((*rxbuf)[l] + ((uint16)((*rxbuf)[l + 1]) << 8));
               (*stack->rxbufstat)[idx] = EC_BUF_COMPLETE;
               (*stack->rxsa)[idx] = oshw_ntohs(ehp->sa1);
            }
            else
            {
               if (idxf < EC_MAXBUF && (*stack->rxbufstat)[idxf] == EC_BUF_TX)
               {
                  rxbuf = &(*stack->rxbuf)[idxf];
                  memcpy(rxbuf, &(*stack->tempbuf)[ETH_HEADERSIZE], (*stack->txbuflength)[idxf] - ETH_HEADERSIZE);
                  (*stack->rxbufstat)[idxf] = EC_BUF_RCVD;
                  (*stack->rxsa)[idxf] = oshw_ntohs(ehp->sa1);
               }
               else
               {
               }
            }
         }
      }
      mtx_unlock (port->rx_mutex);
   }
   return rval;
}
int ecx_inframe ( ecx_portt * port , int idx , int stacknumber ) { uint16 l ; int rval ; uint8 idxf ; ec_etherheadert * ehp ; ec_comt * ecp ; ec_stackT * stack ; ec_bufT * rxbuf ; if ( ! stacknumber ) { stack = & ( port -> stack ) ; } else { stack = & ( port -> redport -> stack ) ; } rval = EC_NOFRAME ; rxbuf = & ( * stack -> rxbuf ) [ idx ] ; if ( ( idx < EC_MAXBUF ) && ( ( * stack -> rxbufstat ) [ idx ] == EC_BUF_RCVD ) ) { l = ( * rxbuf ) [ 0 ] + ( ( uint16 ) ( ( * rxbuf ) [ 1 ] & 0x0f ) << 8 ) ; rval = ( ( * rxbuf ) [ l ] + ( ( uint16 ) ( * rxbuf ) [ l + 1 ] << 8 ) ) ; ( * stack -> rxbufstat ) [ idx ] = EC_BUF_COMPLETE ; } else { mtx_lock ( port -> rx_mutex ) ; if ( ecx_recvpkt ( port , stacknumber ) ) { rval = EC_OTHERFRAME ; ehp = ( ec_etherheadert * ) ( stack -> tempbuf ) ; if ( ehp -> etype == oshw_htons ( ETH_P_ECAT ) ) { ecp = ( ec_comt * ) ( & ( * stack -> tempbuf ) [ ETH_HEADERSIZE ] ) ; l = etohs ( ecp -> elength ) & 0x0fff ; idxf = ecp -> index ; if ( idxf == idx ) { memcpy ( rxbuf , & ( * stack -> tempbuf ) [ ETH_HEADERSIZE ] , ( * stack -> txbuflength ) [ idx ] - ETH_HEADERSIZE ) ; rval = ( ( * rxbuf ) [ l ] + ( ( uint16 ) ( ( * rxbuf ) [ l + 1 ] ) << 8 ) ) ; ( * stack -> rxbufstat ) [ idx ] = EC_BUF_COMPLETE ; ( * stack -> rxsa ) [ idx ] = oshw_ntohs ( ehp -> sa1 ) ; } else { if ( idxf < EC_MAXBUF && ( * stack -> rxbufstat ) [ idxf ] == EC_BUF_TX ) { rxbuf = & ( * stack -> rxbuf ) [ idxf ] ; memcpy ( rxbuf , & ( * stack -> tempbuf ) [ ETH_HEADERSIZE ] , ( * stack -> txbuflength ) [ idxf ] - ETH_HEADERSIZE ) ; ( * stack -> rxbufstat ) [ idxf ] = EC_BUF_RCVD ; ( * stack -> rxsa ) [ idxf ] = oshw_ntohs ( ehp -> sa1 ) ; } else { } } } } mtx_unlock ( port -> rx_mutex ) ; } return rval ; }
Non blocking receive frame function.
Non blocking receive frame function .
/* check if requested index is already in buffer ? */ /* return WKC */ /* mark as completed */ /* non blocking call to retrieve frame from socket */ /* check if it is an EtherCAT frame */ /* found index equals reqested index ? */ /* yes, put it in the buffer array (strip ethernet header) */ /* return WKC */ /* mark as completed */ /* store MAC source word 1 for redundant routing info */ /* check if index exist and someone is waiting for it */ /* put it in the buffer array (strip ethernet header) */ /* mark as received */ /* strange things happend */ /* WKC if mathing frame found */