Skip to content

Commit

Permalink
Merge pull request #20 from driftregion/19
Browse files Browse the repository at this point in the history
initialize struct ifreq ifr #19
  • Loading branch information
driftregion authored Nov 1, 2023
2 parents ba10d9c + a0da50f commit 72887ee
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions iso14229.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,34 +220,37 @@ static ssize_t tp_send(UDSTpHandle_t *hdl, const void *buf, size_t count, UDSTpA
#if UDS_TP == UDS_TP_ISOTP_SOCKET
static int LinuxSockBind(const char *if_name, uint32_t rxid, uint32_t txid) {
int fd = 0;
if ((fd = socket(AF_CAN, SOCK_DGRAM | SOCK_NONBLOCK, CAN_ISOTP)) < 0) {
perror("Socket");
return -1;
}

struct ifreq ifr = {0};
struct sockaddr_can addr = {0};
struct can_isotp_fc_options fcopts = {
.bs = 0x10,
.stmin = 3,
.wftmax = 0,
};

if ((fd = socket(AF_CAN, SOCK_DGRAM | SOCK_NONBLOCK, CAN_ISOTP)) < 0) {
fprintf(stderr, "socket: %s", strerror(errno));
return -1;
}

if (setsockopt(fd, SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, &fcopts, sizeof(fcopts)) < 0) {
perror("setsockopt");
return -1;
}

struct ifreq ifr;
strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name));
ioctl(fd, SIOCGIFINDEX, &ifr);
strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
fprintf(stderr, "ioctl: %s %s\n", strerror(errno), if_name);
return -1;
}

struct sockaddr_can addr;
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_addr.tp.rx_id = rxid;
addr.can_addr.tp.tx_id = txid;
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
printf("Bind: %s %s\n", strerror(errno), if_name);
fprintf(stderr, "bind: %s %s\n", strerror(errno), if_name);
return -1;
}
printf("opened ISO-TP link fd: %d, rxid: %03x, txid: %03x\n", fd, rxid, txid);
Expand Down

0 comments on commit 72887ee

Please sign in to comment.