/*Write thread. Reads messages from UDP socket and sends them to the J1708 bus.*/ void * WriteThread(void* args){ int gpio; int len; int len2; char msg_buf[256]; char read_buf[256]; int client_size = sizeof(other_addr); int sent; struct timespec sleepspec; sleepspec.tv_sec = 0; sleepspec.tv_nsec = BIT_TIME*10; useconds_t sleeptime; // gpio = open_gpio("/sys/class/gpio/gpio60/value"); while(1){ len = recvfrom(read_socket,msg_buf,256,0,(struct sockaddr *) &other_addr, &client_size); sleeptime = BIT_TIME_MICROS*12*len; sent = 0; // printf("%s\n","Sending to ecm serial"); // ppj1708(len,msg_buf); while(!sent){ wait_for_quiet(gpio,6,&buslock); //fprintf(stderr,"sending a message!\n"); write(fd,msg_buf,len); //Wait for the UART to finish sending the message onto the bus. usleep(sleeptime); /* Read the message back. If we don't do this, it will be read by the read thread. * This also helps collision detection. */ len2 = read(fd,&read_buf,len); // printf("%s","ECM cleared from message buffer: "); // ppj1708(len2,read_buf); /* Quick and dirty collision detection. * Compare data read back from the bus with the data that we tried to send. * If they're different, we know that there was a collision. Retry until success. */ if(!memcmp(msg_buf,read_buf,len)){ sent = 1; }else{ pthread_mutex_unlock(&buslock); } } pthread_mutex_unlock(&buslock); nanosleep(&sleepspec,NULL); } }
void * WriteThread(void* args){ int gpio; int len; int len2; char msg_buf[256]; char read_buf[256]; int client_size = sizeof(other_addr); int sent; struct timespec sleepspec; sleepspec.tv_sec = 0; sleepspec.tv_nsec = BIT_TIME*10; useconds_t sleeptime; // gpio = open_gpio("/sys/class/gpio/gpio60/value"); while(1){ len = recvfrom(read_socket,msg_buf,256,0,(struct sockaddr *) &other_addr, &client_size); sleeptime = BIT_TIME_MICROS*12*len; sent = 0; // printf("%s\n","Sending to ecm serial"); // ppj1708(len,msg_buf); while(!sent){ wait_for_quiet(gpio,6,&buslock); //fprintf(stderr,"sending a message!\n"); write(fd,msg_buf,len); usleep(sleeptime); len2 = read(fd,&read_buf,len); printf("%s","ECM cleared from message buffer: "); ppj1708(len2,read_buf); if(!memcmp(msg_buf,read_buf,len)){ sent = 1; }else{ pthread_mutex_unlock(&buslock); } } pthread_mutex_unlock(&buslock); nanosleep(&sleepspec,NULL); } }