Implemented timeouts for linux port

This commit is contained in:
Sebastian 2016-12-01 00:16:42 +01:00
parent 7c13c4c5ca
commit 4e4601d12f
3 changed files with 20 additions and 3 deletions

View File

@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <termios.h> #include <termios.h>
#include <pthread.h> #include <pthread.h>
#include <sys/time.h>
#include "sss7.h" #include "sss7.h"
@ -19,6 +20,13 @@ pthread_mutex_t state_mutex, rx_buffer_mutex;
int serial_fd; int serial_fd;
int get_milliseconds(void) {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
int uart_init(char *serialport) { int uart_init(char *serialport) {
serial_fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); serial_fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_fd == -1) { if (serial_fd == -1) {
@ -46,7 +54,7 @@ int uart_init(char *serialport) {
// No output processing // No output processing
options.c_oflag = 0; options.c_oflag = 0;
options.c_cc[VTIME] = 20; options.c_cc[VTIME] = 1;
options.c_cc[VMIN] = 0; options.c_cc[VMIN] = 0;
tcsetattr(serial_fd, TCSAFLUSH, &options); tcsetattr(serial_fd, TCSAFLUSH, &options);
@ -120,7 +128,9 @@ void uart_put_byte(uint8_t byte) {
void *eventloop(void *arg) { void *eventloop(void *arg) {
int res = 0; int res = 0;
int timestamp = get_milliseconds();
while(1) { while(1) {
printf("Loop\n");
if(uart_has_tx_byte) { if(uart_has_tx_byte) {
write(serial_fd, &uart_tx_byte, 1); write(serial_fd, &uart_tx_byte, 1);
printf("Send %x\n", uart_tx_byte); printf("Send %x\n", uart_tx_byte);
@ -143,6 +153,13 @@ void *eventloop(void *arg) {
sss7_process_tx(); sss7_process_tx();
pthread_mutex_unlock(&state_mutex); pthread_mutex_unlock(&state_mutex);
} }
pthread_mutex_lock(&state_mutex);
int now = get_milliseconds();
printf("Ticks: %d\n", now - timestamp);
sss7_process_ticks(now - timestamp);
timestamp = now;
pthread_mutex_unlock(&state_mutex);
} }
return NULL; return NULL;

View File

@ -31,7 +31,7 @@ int main(int argc, char const *argv[]) {
if(libsss7_send_failed()) { if(libsss7_send_failed()) {
printf("Send failed\n"); printf("Send failed\n");
} }
sleep(2); sleep(5);
} }

View File

@ -27,7 +27,7 @@ enum sss7State {
}; };
const static uint8_t SSS7_HEADER[] = {0xAA, 0xFE}; const static uint8_t SSS7_HEADER[] = {0xAA, 0xFE};
const static uint16_t sss7_timeout = 50; const static uint16_t sss7_timeout = 150;
const static uint16_t sss7_timeout_increment = 1; const static uint16_t sss7_timeout_increment = 1;
#define SSS7_PAYLOAD_SIZE 16 #define SSS7_PAYLOAD_SIZE 16