github.com/20yyq/packet@v0.1.4-0.20231013092308-386a004a3baa/netlink_can.go (about)

     1  // @@
     2  // @ Author       : Eacher
     3  // @ Date         : 2023-09-16 14:21:44
     4  // @ LastEditTime : 2023-09-16 14:50:29
     5  // @ LastEditors  : Eacher
     6  // @ --------------------------------------------------------------------------------<
     7  // @ Description  : 
     8  // @ --------------------------------------------------------------------------------<
     9  // @ FilePath     : /20yyq/packet/netlink_can.go
    10  // @@
    11  package packet
    12  
    13  import (
    14  	"unsafe"
    15  	"golang.org/x/sys/unix"
    16  )
    17  
    18  const (
    19  	SizeofCANBitTiming		= 0x20
    20  	SizeofCANBitTimingConst	= 0x30
    21  	SizeofCANDeviceStats	= 0x18
    22  	SizeofCANCtrlMode		= 0x08
    23  )
    24  
    25  type CANBitTiming unix.CANBitTiming
    26  type CANBitTimingConst unix.CANBitTimingConst
    27  type CANDeviceStats unix.CANDeviceStats
    28  type CANClock unix.CANClock
    29  type CANBusErrorCounters unix.CANBusErrorCounters
    30  type CANCtrlMode unix.CANCtrlMode
    31  
    32  
    33  func NewCANBitTiming(b [SizeofCANBitTiming]byte) *CANBitTiming {
    34  	return (*CANBitTiming)(unsafe.Pointer(&b[0]))
    35  }
    36  
    37  func (bitt CANBitTiming) WireFormat() []byte {
    38  	var b [SizeofCANBitTiming]byte
    39  	*(*uint32)(unsafe.Pointer(&b[0]))	= bitt.Bitrate
    40  	*(*uint32)(unsafe.Pointer(&b[4]))	= bitt.Sample_point
    41  	*(*uint32)(unsafe.Pointer(&b[8]))	= bitt.Tq
    42  	*(*uint32)(unsafe.Pointer(&b[12]))	= bitt.Prop_seg
    43  	*(*uint32)(unsafe.Pointer(&b[16]))	= bitt.Phase_seg1
    44  	*(*uint32)(unsafe.Pointer(&b[20]))	= bitt.Phase_seg2
    45  	*(*uint32)(unsafe.Pointer(&b[24]))	= bitt.Sjw
    46  	*(*uint32)(unsafe.Pointer(&b[28]))	= bitt.Brp
    47  	return b[:]
    48  }
    49  
    50  func NewCANBitTimingConst(b [SizeofCANBitTimingConst]byte) *CANBitTimingConst {
    51  	return (*CANBitTimingConst)(unsafe.Pointer(&b[0]))
    52  }
    53  
    54  func (bitc CANBitTimingConst) WireFormat() []byte {
    55  	var b [SizeofCANBitTimingConst]byte
    56  	*(*[16]uint8)(b[:])					= bitc.Name
    57  	*(*uint32)(unsafe.Pointer(&b[16]))	= bitc.Tseg1_min
    58  	*(*uint32)(unsafe.Pointer(&b[20]))	= bitc.Tseg1_max
    59  	*(*uint32)(unsafe.Pointer(&b[24]))	= bitc.Tseg2_min
    60  	*(*uint32)(unsafe.Pointer(&b[28]))	= bitc.Tseg2_max
    61  	*(*uint32)(unsafe.Pointer(&b[32]))	= bitc.Sjw_max
    62  	*(*uint32)(unsafe.Pointer(&b[36]))	= bitc.Brp_min
    63  	*(*uint32)(unsafe.Pointer(&b[40]))	= bitc.Brp_max
    64  	*(*uint32)(unsafe.Pointer(&b[44]))	= bitc.Brp_inc
    65  	return b[:]
    66  }
    67  
    68  func NewCANDeviceStats(b [SizeofCANDeviceStats]byte) *CANDeviceStats {
    69  	return (*CANDeviceStats)(unsafe.Pointer(&b[0]))
    70  }
    71  
    72  func (devs CANDeviceStats) WireFormat() []byte {
    73  	var b [SizeofCANDeviceStats]byte
    74  	*(*uint32)(unsafe.Pointer(&b[0]))	= devs.Bus_error
    75  	*(*uint32)(unsafe.Pointer(&b[4]))	= devs.Error_warning
    76  	*(*uint32)(unsafe.Pointer(&b[8]))	= devs.Error_passive
    77  	*(*uint32)(unsafe.Pointer(&b[12]))	= devs.Bus_off
    78  	*(*uint32)(unsafe.Pointer(&b[16]))	= devs.Arbitration_lost
    79  	*(*uint32)(unsafe.Pointer(&b[20]))	= devs.Restarts
    80  	return b[:]
    81  }
    82  
    83  func NewCANClock(b [4]byte) *CANClock {
    84  	return (*CANClock)(unsafe.Pointer(&b[0]))
    85  }
    86  
    87  func (clock CANClock) WireFormat() []byte {
    88  	var b [4]byte
    89  	*(*uint32)(unsafe.Pointer(&b[0]))	= clock.Freq
    90  	return b[:]
    91  }
    92  
    93  func NewCANBusErrorCounters(b [4]byte) *CANBusErrorCounters {
    94  	return (*CANBusErrorCounters)(unsafe.Pointer(&b[0]))
    95  }
    96  
    97  func (buse CANBusErrorCounters) WireFormat() []byte {
    98  	var b [4]byte
    99  	*(*uint16)(unsafe.Pointer(&b[0]))	= buse.Txerr
   100  	*(*uint16)(unsafe.Pointer(&b[2]))	= buse.Rxerr
   101  	return b[:]
   102  }
   103  
   104  func NewCANCtrlMode(b [SizeofCANCtrlMode]byte) *CANCtrlMode {
   105  	return (*CANCtrlMode)(unsafe.Pointer(&b[0]))
   106  }
   107  
   108  func (ctrl CANCtrlMode) WireFormat() []byte {
   109  	var b [SizeofCANCtrlMode]byte
   110  	*(*uint32)(unsafe.Pointer(&b[0]))	= ctrl.Mask
   111  	*(*uint32)(unsafe.Pointer(&b[4]))	= ctrl.Flags
   112  	return b[:]
   113  }