Commit bf2329de authored by Franksen, Benjamin's avatar Franksen, Benjamin
Browse files

scan/ellsi: print socket in debug and error messages

parent bfec074d
......@@ -211,7 +211,7 @@ static ssize_t ellsi_write(void *pvt, const struct can_frame *frame) {
msg.payload.len |= CAN_TELEGRAM_LEN_RTR;
memcpy(msg.payload.data, frame->data, CAN_MAX_DLC);
r = write(state->sock, &msg, sizeof(msg));
debug("ellsi_write: sent %d\n", r);
debug("ellsi_write(%d): sent %d bytes\n", state->sock, r);
return r;
}
......@@ -232,7 +232,7 @@ static ssize_t ellsi_read(void *pvt, struct can_frame *frame) {
return -1;
}
if (state->msg.header.magic != htonl(ELLSI_MAGIC)) {
fprintf(stderr, "ellsi_read: bad magic\n");
fprintf(stderr, "ellsi_read(%d): bad magic\n", state->sock);
return -1;
}
switch (ntohl(state->msg.header.command)) {
......@@ -248,7 +248,8 @@ static ssize_t ellsi_read(void *pvt, struct can_frame *frame) {
nbytes = write(state->sock, &state->msg.header, sizeof(state->msg.header));
if (nbytes < sizeof(state->msg.header)) {
errbuf_t buf;
fprintf(stderr, "ellsi_read: reply to heartbeat failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_read(%d): reply to heartbeat failed (%s)\n",
state->sock, ERRNO_STR(buf));
}
break;
#if 0
......@@ -266,7 +267,7 @@ static ssize_t ellsi_read(void *pvt, struct can_frame *frame) {
state->unread--;
this = state->next++;
if (this->len & CAN_TELEGRAM_LEN_TXDONE) {
fprintf(stderr, "ellsi_read: ignored confirmation (TXDONE)\n");
fprintf(stderr, "ellsi_read(%d): ignored confirmation (TXDONE)\n", state->sock);
return ellsi_read(pvt, frame);
}
frame->can_id = ntohl(this->id) & CAN_SFF_MASK;
......@@ -300,11 +301,12 @@ static int ellsi_check(int sock) {
if (r < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
errbuf_t buf;
fprintf(stderr, "ellsi_check: recv failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_check(%d): recv failed (%s)\n", sock, ERRNO_STR(buf));
}
return r;
}
debug("ellsi_check: got back magic %#x cmd %#x subcmd %#x payloadLen %d\n",
debug("ellsi_check(%d): got back magic %#x cmd %#x subcmd %#x payloadLen %d\n",
sock,
ntohl(ack.header.magic),
ntohl(ack.header.command),
ntohl(ack.header.subcommand),
......@@ -318,7 +320,8 @@ static int ellsi_check(int sock) {
&& ntohl(ack.header.command) == ELLSI_CMD_CTRL
&& ntohl(ack.header.subcommand) == ELLSI_IOCTL_GET_LAST_STATE)
{
debug("ellsi_check: last cmd %#x subcmd %#x state %#x\n",
debug("ellsi_check(%d): last cmd %#x subcmd %#x state %#x\n",
sock,
ntohl(ack.payload.lastCommand),
ntohl(ack.payload.lastSubcommand),
ntohl(ack.payload.lastState));
......@@ -338,12 +341,12 @@ static int ellsi_register(ellsi_state_t *state) {
msg.header.magic = htonl(ELLSI_MAGIC);
msg.header.command = htonl(ELLSI_CMD_REGISTER);
msg.header.subcommand = htonl(ELLSI_SUBCMD_AUTOACK);
debug("ellsi_register: send magic %#x, cmd %#x subcmd %#x\n",
ELLSI_MAGIC, ELLSI_CMD_REGISTER, ELLSI_SUBCMD_AUTOACK);
debug("ellsi_register(%d): send magic %#x, cmd %#x subcmd %#x\n",
state->sock, ELLSI_MAGIC, ELLSI_CMD_REGISTER, ELLSI_SUBCMD_AUTOACK);
r = write(state->sock, &msg, sizeof(msg));
if (r < 0) {
errbuf_t buf;
fprintf(stderr, "ellsi_register: write failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_register(%d): write failed (%s)\n", state->sock, ERRNO_STR(buf));
return r;
}
return ellsi_check(state->sock);
......@@ -370,12 +373,12 @@ static int ellsi_set_baudrate(ellsi_state_t *state, int baudrate) {
fprintf(stderr, "ignoring invalid baudrate %d\n", baudrate);
}
}
debug("ellsi_set_baudrate: send magic %#x cmd %#x subcmd %#x baudrate_code %#x\n",
ELLSI_MAGIC, ELLSI_CMD_CTRL, ELLSI_IOCTL_CAN_SET_BAUDRATE, code);
debug("ellsi_set_baudrate(%d): send magic %#x cmd %#x subcmd %#x baudrate_code %#x\n",
state->sock, ELLSI_MAGIC, ELLSI_CMD_CTRL, ELLSI_IOCTL_CAN_SET_BAUDRATE, code);
r = write(state->sock, &msg, sizeof(msg));
if (r < 0) {
errbuf_t buf;
fprintf(stderr, "ellsi_set_baudrate: write failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_set_baudrate(%d): write failed (%s)\n", state->sock, ERRNO_STR(buf));
return r;
}
msg.header.command = htonl(ELLSI_CMD_CTRL);
......@@ -384,7 +387,7 @@ static int ellsi_set_baudrate(ellsi_state_t *state, int baudrate) {
r = write(state->sock, &msg, sizeof(msg));
if (r < 0) {
errbuf_t buf;
fprintf(stderr, "ellsi_set_baudrate: write failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_set_baudrate(%d): write failed (%s)\n", state->sock, ERRNO_STR(buf));
return r;
}
return ellsi_check(state->sock);
......@@ -404,12 +407,12 @@ static int ellsi_add_can_ids(ellsi_state_t *state) {
msg.header.payloadLen = htonl(sizeof(msg.payload));
msg.payload.rangeStart = 0;
msg.payload.rangeEnd = htonl(CAN_SFF_MASK);
debug("ellsi_add_can_ids: send magic %#x, cmd %#x subcmd %#x\n",
ELLSI_MAGIC, ELLSI_CMD_CTRL, ELLSI_IOCTL_CAN_ID_ADD);
debug("ellsi_add_can_ids(%d): send magic %#x, cmd %#x subcmd %#x\n",
state->sock, ELLSI_MAGIC, ELLSI_CMD_CTRL, ELLSI_IOCTL_CAN_ID_ADD);
r = write(state->sock, &msg, sizeof(msg));
if (r < 0) {
errbuf_t buf;
fprintf(stderr, "ellsi_add_can_ids: write failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_add_can_ids(%d): write failed (%s)\n", state->sock, ERRNO_STR(buf));
return r;
}
msg.header.command = htonl(ELLSI_CMD_CTRL);
......@@ -418,7 +421,7 @@ static int ellsi_add_can_ids(ellsi_state_t *state) {
r = write(state->sock, &msg, sizeof(msg));
if (r < 0) {
errbuf_t buf;
fprintf(stderr, "ellsi_add_can_ids: write failed (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_add_can_ids(%d): write failed (%s)\n", state->sock, ERRNO_STR(buf));
return r;
}
return ellsi_check(state->sock);
......@@ -431,13 +434,14 @@ static int ellsi_connect(void *pvt) {
/* connect socket */
if (connect(state->sock, (struct sockaddr *)&state->addr, sizeof(struct sockaddr_in)) < 0) {
fprintf(stderr, "ellsi_connect: error in socket connect (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsi_connect(%d): error in socket connect (%s)\n",
state->sock, ERRNO_STR(buf));
return -1;
}
/* set recv timout to 5 seconds so we can detect missing heartbeat (every 2.5 seconds) */
if (setsockopt(state->sock, SOL_SOCKET, SO_RCVTIMEO, &tmo, sizeof(tmo)) < 0) {
fprintf(stderr, "ellsi_connect: error setting socket option SO_RCVTIMEO (%s)\n",
ERRNO_STR(buf));
fprintf(stderr, "ellsi_connect(%d): error setting socket option SO_RCVTIMEO (%s)\n",
state->sock, ERRNO_STR(buf));
return -1;
}
/* initialize protocol */
......@@ -467,20 +471,22 @@ int ellsiConfigPort(int can_port, const char *ip_addr, int ip_port, int baudrate
ellsi_state_t *state = new(ellsi_state_t);
if (!state) {
fprintf(stderr, "ellsiConfigPort: out of memory\n");
fprintf(stderr, "ellsiConfigPort(%d): out of memory\n", state->sock);
return -1;
}
/* success, create socket */
state->sock = socket(AF_INET, SOCK_DGRAM, 0);
if (state->sock < 0) {
fprintf(stderr, "ellsiConfigPort: error creating socket (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsiConfigPort(%d): error creating socket (%s)\n",
state->sock, ERRNO_STR(buf));
free(state);
return -1;
}
/* success, prepare addr with parameters ip_addr and ip_port */
state->addr.sin_family = AF_INET;
if (aToIPAddr(ip_addr, ip_port, &state->addr)<0) {
fprintf(stderr, "ellsiConfigPort: error converting ip address (%s)\n", ERRNO_STR(buf));
fprintf(stderr, "ellsiConfigPort(%d): error converting ip address (%s)\n",
state->sock, ERRNO_STR(buf));
free(state);
return -1;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment