Added threads and mutexes for eventloop
This commit is contained in:
parent
0cf403a7de
commit
20aff761e6
|
@ -0,0 +1 @@
|
|||
bin
|
|
@ -6,7 +6,7 @@ OBJDIR = bin
|
|||
CC = clang
|
||||
|
||||
CFLAGS = -I ../sss7core/ -Os -Wall -Wstrict-prototypes -fPIC
|
||||
LDFLAGS = -Wl,--gc-sections
|
||||
LDFLAGS = -pthread -Wl,--gc-sections
|
||||
|
||||
all: start $(OBJDIR)/libsss7.so $(OBJDIR)/test
|
||||
@echo ":: Done !"
|
||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -6,15 +6,20 @@
|
|||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <termios.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -1,10 +1,41 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <string.h>
|
||||
|
||||
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) {
|
||||
|
|
|
@ -5,9 +5,14 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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);
|
||||
|
|
Loading…
Reference in New Issue