diff --git a/software/linux/.gitignore b/software/linux/.gitignore new file mode 100644 index 0000000..ba077a4 --- /dev/null +++ b/software/linux/.gitignore @@ -0,0 +1 @@ +bin diff --git a/software/linux/Makefile b/software/linux/Makefile index 200be47..2785700 100644 --- a/software/linux/Makefile +++ b/software/linux/Makefile @@ -5,8 +5,8 @@ OBJDIR = bin CC = clang -CFLAGS = -I ../sss7core/ -Os -Wall -Wstrict-prototypes -fPIC -LDFLAGS = -Wl,--gc-sections +CFLAGS = -I ../sss7core/ -Os -Wall -Wstrict-prototypes -fPIC +LDFLAGS = -pthread -Wl,--gc-sections all: start $(OBJDIR)/libsss7.so $(OBJDIR)/test @echo ":: Done !" diff --git a/software/linux/bin/libsss7.so b/software/linux/bin/libsss7.so deleted file mode 100755 index a314a4b..0000000 Binary files a/software/linux/bin/libsss7.so and /dev/null differ diff --git a/software/linux/bin/sss7core/sss7.o b/software/linux/bin/sss7core/sss7.o deleted file mode 100644 index b7b5f5f..0000000 Binary files a/software/linux/bin/sss7core/sss7.o and /dev/null differ diff --git a/software/linux/bin/test b/software/linux/bin/test deleted file mode 100755 index a2cfd93..0000000 Binary files a/software/linux/bin/test and /dev/null differ diff --git a/software/linux/bin/test.o b/software/linux/bin/test.o deleted file mode 100644 index 15f97f5..0000000 Binary files a/software/linux/bin/test.o and /dev/null differ diff --git a/software/linux/libsss7.c b/software/linux/libsss7.c index ea0fd6a..27252e9 100644 --- a/software/linux/libsss7.c +++ b/software/linux/libsss7.c @@ -6,15 +6,20 @@ #include #include #include +#include + +#include "sss7.h" + uint8_t uart_rx_byte; -uint8_t uart_tx_byte, uart_has_tx_byte; +_Atomic uint8_t uart_tx_byte, uart_has_tx_byte, uart_tx_done; + +pthread_t event_thread; +pthread_mutex_t state_mutex, rx_buffer_mutex; int serial_fd; -int libsss7_start(char *serialport) { - sss7_init(); - +int uart_init(char *serialport) { serial_fd = open(serialport, O_RDWR | O_NOCTTY | O_NDELAY); if (serial_fd == -1) { perror("Error: Unable to open serialport"); @@ -45,18 +50,30 @@ int libsss7_start(char *serialport) { options.c_cc[VMIN] = 0; tcsetattr(serial_fd, TCSAFLUSH, &options); - int res = 0; - while(1) { - res = read(serial_fd, &uart_rx_byte, 1); - if(res == 1) { - sss7_process_rx(); - } + return 0; +} - if(uart_has_tx_byte) { - uart_has_tx_byte = 0; - write(serial_fd, &uart_has_tx_byte, 1); - sss7_process_tx(); - } +void *eventloop(void *arg); + +int libsss7_start(char *serialport) { + int res = 0; + + sss7_init(); + res = uart_init(serialport); + if(res) { + return -1; + } + + uart_has_tx_byte = 0; + uart_tx_done = 0; + + pthread_mutex_init(&state_mutex, NULL); + pthread_mutex_init(&rx_buffer_mutex, NULL); + + res = pthread_create(&event_thread, NULL, eventloop, NULL); + if(res) { + printf("Could not create eventloop thread\n"); + return -1; } return 0; @@ -67,7 +84,9 @@ int libsss7_can_send(void) { } void libsss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) { - + pthread_mutex_lock(&state_mutex); + sss7_send(msg); + pthread_mutex_unlock(&state_mutex); } int libsss7_send_failed(void) { @@ -79,10 +98,14 @@ int libsss7_has_received(void) { } void libsss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) { - return sss7_get_received(msg); + pthread_mutex_lock(&rx_buffer_mutex); + sss7_get_received(msg); + pthread_mutex_unlock(&rx_buffer_mutex); } void libsss7_stop() { + pthread_join(event_thread, NULL); + close(serial_fd); } @@ -94,3 +117,33 @@ void uart_put_byte(uint8_t byte) { uart_has_tx_byte = 1; uart_tx_byte = byte; } + +void *eventloop(void *arg) { + int res = 0; + while(1) { + if(uart_has_tx_byte) { + write(serial_fd, &uart_tx_byte, 1); + printf("Send %x\n", uart_tx_byte); + uart_has_tx_byte = 0; + uart_tx_done = 1; + } + + res = read(serial_fd, &uart_rx_byte, 1); + if(res == 1) { + pthread_mutex_lock(&state_mutex); + pthread_mutex_lock(&rx_buffer_mutex); + sss7_process_rx(); + pthread_mutex_unlock(&rx_buffer_mutex); + pthread_mutex_unlock(&state_mutex); + } + + if(uart_tx_done) { + uart_tx_done = 0; + pthread_mutex_lock(&state_mutex); + sss7_process_tx(); + pthread_mutex_unlock(&state_mutex); + } + } + + return NULL; +} diff --git a/software/linux/libsss7.h b/software/linux/libsss7.h index e26f3ac..e2564b6 100644 --- a/software/linux/libsss7.h +++ b/software/linux/libsss7.h @@ -3,18 +3,17 @@ #include "sss7.h" - int libsss7_start(char *serialport); int libsss7_can_send(void); -void libsss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]); +void libsss7_send(uint8_t *msg); int libsss7_send_failed(void); int libsss7_has_received(void); -void libsss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]); +void libsss7_get_received(uint8_t *msg); void libsss7_stop(); diff --git a/software/linux/test.c b/software/linux/test.c index 7374678..a0c8185 100644 --- a/software/linux/test.c +++ b/software/linux/test.c @@ -1,10 +1,41 @@ #include #include +#include +#include #include "libsss7.h" int main(int argc, char const *argv[]) { - libsss7_start("/tmp/ttyVA"); + libsss7_start("/dev/ttyUSB0"); + + uint8_t msg[SSS7_PAYLOAD_SIZE]; + memset(msg, 0, SSS7_PAYLOAD_SIZE); + msg[0] = 'H'; + msg[1] = 'e'; + msg[2] = 'l'; + msg[3] = 'l'; + msg[4] = 'o'; + msg[5] = ' '; + msg[6] = 'W'; + msg[7] = 'o'; + msg[8] = 'r'; + msg[9] = 'l'; + msg[10] = 'd'; + + + while(1) { + + while(!libsss7_can_send()); + libsss7_send(msg); + while(!libsss7_can_send()); + if(libsss7_send_failed()) { + printf("Send failed\n"); + } + sleep(2); + } + + + libsss7_send(msg); libsss7_stop(); return 0; diff --git a/software/sss7core/sss7.c b/software/sss7core/sss7.c index 5906a7a..4dc58ca 100644 --- a/software/sss7core/sss7.c +++ b/software/sss7core/sss7.c @@ -2,7 +2,7 @@ #include -volatile enum sss7State sss7_state; +sss7_shared_modfier enum sss7State sss7_state; uint8_t sss7_rx_buffer[SSS7_RX_BUFFER_SIZE][SSS7_PAYLOAD_SIZE]; uint8_t sss7_rx_buffer_write; @@ -12,11 +12,11 @@ uint8_t sss7_rx_pos; uint8_t sss7_tx_buffer[SSS7_PAYLOAD_SIZE]; uint8_t sss7_tx_pos; uint8_t sss7_tx_crc; -volatile uint8_t sss7_tx_failed; +sss7_shared_modfier uint8_t sss7_tx_failed; uint8_t sss7_tx_last_byte; uint8_t sss7_tx_last_ack; -volatile uint8_t sss7_timeout_counter; +sss7_shared_modfier uint8_t sss7_timeout_counter; void sss7_init(void) { diff --git a/software/sss7core/sss7.h b/software/sss7core/sss7.h index e8e0d20..db18204 100644 --- a/software/sss7core/sss7.h +++ b/software/sss7core/sss7.h @@ -5,9 +5,14 @@ extern "C" { #endif - #include +#ifndef sss7_shared_modfier +#define sss7_shared_modfier volatile +#endif + + + enum sss7State { SSS7_IDLE, SSS7_TX_HEADER, @@ -27,11 +32,12 @@ const static uint16_t sss7_timeout_increment = 1; #define SSS7_RX_BUFFER_SIZE 2 -extern volatile enum sss7State sss7_state; -extern volatile uint8_t sss7_tx_failed; +extern sss7_shared_modfier enum sss7State sss7_state; +extern sss7_shared_modfier uint8_t sss7_tx_failed; extern uint8_t sss7_rx_buffer_write; extern uint8_t sss7_rx_buffer_read; + void sss7_process_rx(void); void sss7_process_tx(void); void sss7_process_ticks(uint16_t ticks);