Skip to content

Commit

Permalink
killed a wild copy/paste hell to check for extended CAN IDs
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Hartkopp <[email protected]>
  • Loading branch information
hartkopp committed Dec 3, 2014
1 parent c098ab1 commit 461b0fe
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 195 deletions.
35 changes: 35 additions & 0 deletions socketcand.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,41 @@ int state_changed(char *buf, int current_state)
return (current_state != state);
}

int element_length(char *buf, int element)
{
int len = strlen(buf);
int elem, i, j;

/*
* < elem1 elem2 elem3 >
*
* get the length of the requested element in bytes
*/

for (i=0, j=0, elem=0; i<len; i++) {

if (buf[i] == ' ') {
elem++;

/* step to next non-space */
while (buf[i] == ' ')
i++;

if (i >= len)
return 0;
}

if (elem == element) {

while (j+i < len && buf[j+i] != ' ')
j++;

return j;
}
}
return 0;
}

int main(int argc, char **argv)
{
int i, found;
Expand Down
1 change: 1 addition & 0 deletions socketcand.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ extern struct sockaddr_in saddr;

int receive_command(int socket, char *buf);
int state_changed(char *buf, int current_state);
int element_length(char *buf, int element);
189 changes: 18 additions & 171 deletions state_bcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,32 +174,10 @@ inline void state_bcm() {
PRINT_ERROR("Syntax error in send command\n")
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 1:
start=i;
break;
case 2:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {
/* < send XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 2) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = TX_SEND;
msg.frame.can_id = msg.msg_head.can_id;
Expand Down Expand Up @@ -233,32 +211,10 @@ inline void state_bcm() {
PRINT_ERROR("Syntax error in add command.\n");
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 3:
start=i;
break;
case 4:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {
/* < add sec usec XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 4) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = TX_SETUP;
msg.msg_head.flags |= SETTIMER | STARTTIMER;
Expand Down Expand Up @@ -291,32 +247,10 @@ inline void state_bcm() {
PRINT_ERROR("Syntax error in update send job command\n")
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 1:
start=i;
break;
case 2:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {
/* < update XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 2) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = TX_SETUP;
msg.msg_head.flags = 0;
Expand All @@ -336,32 +270,10 @@ inline void state_bcm() {
PRINT_ERROR("Syntax error in delete job command\n")
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 1:
start=i;
break;
case 2:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {

/* < delete XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 2) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = TX_DELETE;
msg.frame.can_id = msg.msg_head.can_id;
Expand Down Expand Up @@ -395,32 +307,10 @@ inline void state_bcm() {
PRINT_ERROR("syntax error in filter command.\n")
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 3:
start=i;
break;
case 4:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {

/* < filter sec usec XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 4) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = RX_SETUP;
msg.msg_head.flags = SETTIMER;
Expand All @@ -442,32 +332,10 @@ inline void state_bcm() {
PRINT_ERROR("syntax error in subscribe command\n")
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 3:
start=i;
break;
case 4:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {

/* < subscribe sec usec XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 4) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = RX_SETUP;
msg.msg_head.flags = RX_FILTER_ID | SETTIMER;
Expand All @@ -488,31 +356,10 @@ inline void state_bcm() {
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 1:
start=i;
break;
case 2:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {
/* < unsubscribe XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 2) == 8)
msg.msg_head.can_id |= CAN_EFF_FLAG;
}

msg.msg_head.opcode = RX_DELETE;
msg.frame.can_id = msg.msg_head.can_id;

Expand Down
26 changes: 2 additions & 24 deletions state_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,31 +175,9 @@ inline void state_raw() {
return;
}

/* check if this is standard or extended identifier */
int start=0;
int stop=0;
int curr=0;
int i=0;
for(;i<strlen(buf);i++) {
if(buf[i] == ' ') {

switch(curr) {
case 1:
start=i;
break;
case 2:
stop=i;
break;
default:
break;
}
curr++;
}
}

if((stop-start) > 4) {
/* < send XXXXXXXX ... > check for extended identifier */
if(element_length(buf, 2) == 8)
frame.can_id |= CAN_EFF_FLAG;
}

ret = send(raw_socket, &frame, sizeof(struct can_frame), 0);
if(ret==-1) {
Expand Down

0 comments on commit 461b0fe

Please sign in to comment.