⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 af_can.c

📁 canpie 一个can bus的协议栈 - CAN interface for embedded control - CAN interface for PC (without local
💻 C
📖 第 1 页 / 共 2 页
字号:
         
         break;
      
   
      //---------------------------------------------------
      // set size of FIFO
      //
                  
      default:
         slErrCodeT = -EINVAL;
         break;
         
	}
   return(slErrCodeT);
}


//----------------------------------------------------------------------------//
// can_raw_listen()                                                           //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_listen(struct socket *sock, int len)
{
   PK_DBG(eDBG_SOCK, "socket %p", sock);
   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_mmap()                                                             //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_mmap(struct file *file, struct socket *sock, 
                        struct vm_area_struct * vma)
{
   PK_DBG(eDBG_SOCK, "socket %p", sock);
   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_poll()                                                             //
//                                                                            //
//----------------------------------------------------------------------------//
static unsigned int can_raw_poll(struct file *file, struct socket *sock, 
                                 struct poll_table_struct *wait)
{
   PK_DBG(eDBG_SOCK, "socket %p", sock);
   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_recvmsg()                                                          //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_recvmsg(struct kiocb *iocb, struct socket *sock, 
                           struct msghdr *msg, size_t total_len, int flags)
{
   int                  slFrameCopyT;     // number of frames to copy
   int                  slFrameCntT;      // counter for copy loop
   int                  slFrameOffsetT;   // offset in the frame struct
   int                  slCpMsgCntT;      // messages copied from queue
   _U16                 uwMsgNumT;
   struct can_sock *    ptsCanSockT;
   _TsCpCanMsg          tsCanMsgT;        // CANpie frame 
   _TvCpStatus          tvCanStatusT;     // CAN status
      
   
   //----------------------------------------------------------------
   // Inside this function the CAN frames are copied from kernel
   // space into the user space. 
   // First, we calculate the total number of messages to copy,
   // i.e. the supplied buffer size divided by the '_TsCpCanMsg'
   // size.
   //
   slFrameCopyT = total_len / sizeof(_TsCpCanMsg);

   
   //-----------------------------------------------------------------
   // get pointer to can_sock structure 
   //
   ptsCanSockT = sock->sk->sk_protinfo;
   
   //----------------------------------------------------------------
   // set the counter for the queue to zero, it is incremented in
   // the loop
   //
   slCpMsgCntT = 0;

   
   //----------------------------------------------------------------
   // Try to copy the maximum number of possible messages, we quit
   // this loop if the CpCoreMsgRead() function does not return
   // one valid message
   //
   for(slFrameCntT = 0; slFrameCntT < slFrameCopyT; slFrameCntT++)
   {
      //---------------------------------------------------
      // try to get one message, if it fails, we quit the
      // loop
      //
      uwMsgNumT = 1; 
      tvCanStatusT = CpCoreMsgRead(ptsCanSockT->ptsCanPort, &tsCanMsgT, &uwMsgNumT);
      if(uwMsgNumT != 1) break;
      
      //---------------------------------------------------
      // increase the message counter
      //
      slCpMsgCntT++;
      PK_DBG(eDBG_DATA, "Message: ID=%04X DLC=%1d Data=%02X %02X", 
             tsCanMsgT.tuMsgId.ulExt, tsCanMsgT.ubMsgDLC, 
             tsCanMsgT.tuMsgData.aubByte[0],
             tsCanMsgT.tuMsgData.aubByte[1]);
      
      slFrameOffsetT = slFrameCntT * sizeof(_TsCpCanMsg);


      //---------------------------------------------------
      // copy the stuff to the user space
      //
      memcpy_toiovec(msg->msg_iov + slFrameOffsetT,
                     (char*) &tsCanMsgT, 
                     sizeof(_TsCpCanMsg));
      
   }
      
   
   //-----------------------------------------------------------------
   // return the length of the copied data
   //
   return(slCpMsgCntT * sizeof(_TsCpCanMsg));
}


//----------------------------------------------------------------------------//
// can_raw_release()                                                          //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_release(struct socket *sock)
{
	struct sock *     sk;
   struct can_sock * ptsCanSockT;

   sk = sock->sk;

      
   //-----------------------------------------------------------------
   // get pointer to can_sock structure 
   //
   ptsCanSockT = sock->sk->sk_protinfo;
   PK_DBG(eDBG_SOCK, "can_sock=%p ", ptsCanSockT);

   can_queue_delete(ptsCanSockT->ptsCanPort);
   
   PK_DBG(eDBG_SOCK, "sock=%p", sk);
	can_sock_release(sk);
   //ptsCanSockT = (struct can_sock_raw*) ptsSocketV->sk;
   //kfree(ptsCanSockT);

   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_sendmsg()                                                          //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_sendmsg(struct kiocb *iocb, struct socket *sock, 
                           struct msghdr *msg, size_t total_len)
{
   int                  slFrameNumT;      // number of frames to copy
   //int                  slFrameCntT;      // counter for copy loop
   //int                  slMsgOffsetT;
   _U16                 uwMsgCntT;
   struct can_sock *    ptsCanSockT;
   //_TsCpCanMsg          tsCanMsgT;        // CANpie frame 
   char *               pszBufferT;
   _TsCpCanMsg *        ptsFrameT;        // CANpie frame 
      
   
   PK_DBG(eDBG_SOCK, "socket %p", sock);
   
   //---------------------------------------------------------------
   // data is transfered via the structure msghdr (linux/socket.h)
   //
   if(msg->msg_flags & ~MSG_DONTWAIT)
   {
      return(-EINVAL);
   }
   /*
   if (sk->sk_state != TCP_ESTABLISHED)
      return -ENOTCONN;
   */

   //----------------------------------------------------------------
   // calculate the number of messages to copy
   //
   slFrameNumT = total_len / sizeof(_TsCpCanMsg);
   PK_DBG(eDBG_DATA, "copy %d bytes", total_len);
   
   pszBufferT = kmalloc(total_len, GFP_ATOMIC);
   
   //-----------------------------------------------------------------
   // get pointer to can_sock structure 
   //
   ptsCanSockT = sock->sk->sk_protinfo;
   
   //----------------------------------------------------------------
   // copy data into CANpie message structure and call the 
   // CANpie alculate the number of messages to copy
   //
   PK_DBG(eDBG_DATA, "copy %d CAN frames", slFrameNumT);
   /*
   for(slFrameCntT = 0; slFrameCntT < slFrameNumT; slFrameCntT++)
   {
      slMsgOffsetT = slFrameCntT * sizeof(_TsCpCanMsg);
      PK_DBG(eDBG_DATA, "offset = %d", slMsgOffsetT);
      memcpy_fromiovec( (char*) &tsCanMsgT, 
                        msg->msg_iov ,//+ slMsgOffsetT, 
                        sizeof(_TsCpCanMsg));
       msg->msg_iov =  msg->msg_iov + sizeof(_TsCpCanMsg);
      uwMsgCntT = 1;
      
      PK_DBG(eDBG_DATA, "Message: ID=%04X DLC=%1d Data=%02X %02X", 
             tsCanMsgT.tuMsgId.ulExt, tsCanMsgT.ubMsgDLC, 
             tsCanMsgT.tuMsgData.aubByte[0],
             tsCanMsgT.tuMsgData.aubByte[1]);
      CpCoreMsgWrite(ptsCanSockT->ptsCanPort, &tsCanMsgT, &uwMsgCntT);
   }
    */ 
   memcpy_fromiovec( pszBufferT, msg->msg_iov , total_len);
   ptsFrameT = (_TsCpCanMsg *) pszBufferT;
   uwMsgCntT = slFrameNumT;
   PK_DBG(eDBG_DATA, "Message: ID=%04X DLC=%1d Data=%02X %02X", 
             ptsFrameT->tuMsgId.ulExt, ptsFrameT->ubMsgDLC, 
             ptsFrameT->tuMsgData.aubByte[0],
             ptsFrameT->tuMsgData.aubByte[1]);
   
   CpCoreMsgWrite(ptsCanSockT->ptsCanPort, ptsFrameT, &uwMsgCntT);
   kfree(pszBufferT);
   //-----------------------------------------------------------------
   // return the length of the copied data
   //
   return(total_len);
}


//----------------------------------------------------------------------------//
// can_raw_sendpage()                                                         //
//                                                                            //
//----------------------------------------------------------------------------//
static ssize_t can_raw_sendpage(struct socket *sock, struct page *page, 
                                int offset, size_t size, int flags)
{
   PK_DBG(eDBG_SOCK, "socket %p", sock);
   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_setsockopt()                                                       //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_setsockopt(struct socket *sock, int level, 
                              int optname, char __user *optval, int optlen)
{
   PK_DBG(eDBG_SOCK, "Level=%d  Option=%d", level, optname);
   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_shutdown()                                                         //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_shutdown(struct socket *sock, int flags)
{
   PK_DBG(eDBG_SOCK, "socket %p", sock);
   return(0);
}


//----------------------------------------------------------------------------//
// can_raw_socketpair()                                                       //
//                                                                            //
//----------------------------------------------------------------------------//
static int can_raw_socketpair(struct socket *sock1, struct socket *sock2)
{
   return(0);
}



/*----------------------------------------------------------------------------*\
** Static structures                                                          **
** proto_ops / net_proto_family                                               **
\*----------------------------------------------------------------------------*/

static struct proto_ops can_raw_ops = {
      
  .family 	= AF_CAN,
   .owner      = THIS_MODULE,
   .release    = can_raw_release,
   .bind       = can_raw_bind,
   .connect    = can_raw_connect,
   .socketpair = can_raw_socketpair,
   .accept     = can_raw_accept,
   .getname    = can_raw_getname,
   .poll       = can_raw_poll,
   .ioctl      = can_raw_ioctl,
   .listen     = can_raw_listen,
   .shutdown   = can_raw_shutdown,
   .setsockopt = can_raw_setsockopt,
   .getsockopt = can_raw_getsockopt,
   .sendmsg    = can_raw_sendmsg,
   .recvmsg    = can_raw_recvmsg,
   .mmap       = can_raw_mmap,
   .sendpage   = can_raw_sendpage,
};

static struct net_proto_family can_family_ops =
{
	AF_CAN,
	can_create
};


//----------------------------------------------------------------------------//
// can_socket_init()                                                          //
// init CAN protocol layer                                                    //
//----------------------------------------------------------------------------//
int can_socket_init(void)
{
   
   sock_register(&can_family_ops);

	return 0;
   
}


//----------------------------------------------------------------------------//
// can_socket_cleanup()                                                       //
// remove CAN protocol layer                                                  //
//----------------------------------------------------------------------------//
void can_socket_cleanup(void)
{

   sock_unregister(AF_CAN);

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -