MCPcopy Create free account
hub / github.com/PolySync/oscc / init_can_socket

Function init_can_socket

api/src/oscc.c:779–852  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

777
778
779int init_can_socket( const char *can_channel,
780 struct timeval *tv )
781{
782 if( can_channel == NULL )
783 {
784 return UNINITIALIZED_SOCKET;
785 }
786
787 int valid = UNINITIALIZED_SOCKET;
788 int sock = UNINITIALIZED_SOCKET;
789 struct ifreq ifr;
790 memset( &ifr, 0, sizeof( ifr ) );
791
792 sock = socket( PF_CAN, SOCK_RAW, CAN_RAW );
793
794 if ( sock < 0 )
795 {
796 perror( "Opening CAN socket failed:" );
797 }
798 else
799 {
800 strncpy( ifr.ifr_name, can_channel, IFNAMSIZ );
801
802 valid = ioctl( sock, SIOCGIFINDEX, &ifr );
803
804 if ( valid < 0 )
805 {
806 perror( "Finding CAN index failed:" );
807 }
808 }
809
810 //If a timeout has been specified set one here since it should be set before
811 //the bind call
812 if( valid >= 0 && tv != NULL )
813 {
814 valid = setsockopt( sock,
815 SOL_SOCKET,
816 SO_RCVTIMEO,
817 tv,
818 sizeof( struct timeval ) );
819
820 if ( valid < 0 )
821 {
822 perror( "Setting timeout failed:" );
823 }
824 }
825
826 if ( valid >= 0 )
827 {
828 struct sockaddr_can can_address;
829
830 memset( &can_address, 0, sizeof( can_address ) );
831 can_address.can_family = AF_CAN;
832 can_address.can_ifindex = ifr.ifr_ifindex;
833
834 valid = bind( sock,
835 (struct sockaddr *) &can_address,
836 sizeof( can_address ) );

Callers 3

init_oscc_canFunction · 0.85
init_vehicle_canFunction · 0.85
can_detectionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected