More cleanup and comments
This commit is contained in:
parent
c22565f694
commit
c43eb50661
|
@ -11,31 +11,44 @@
|
||||||
|
|
||||||
#include "sss7.h"
|
#include "sss7.h"
|
||||||
|
|
||||||
|
// A state kept a across eventloop iterations to emulate tx complete interrupts
|
||||||
enum UartTxState {
|
enum UartTxState {
|
||||||
TX_IDLE,
|
TX_IDLE, // Idle state
|
||||||
TX_HAS_BYTE,
|
TX_HAS_BYTE, // There is a byte to be send in the next iteration
|
||||||
TX_COMPLETE
|
TX_COMPLETE // A byte has been sent in the current iteration
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// The last byte that has been received, read by uart_get_byte
|
||||||
uint8_t uart_rx_byte;
|
uint8_t uart_rx_byte;
|
||||||
|
|
||||||
|
// The byte to send in the next eventloop iteration
|
||||||
_Atomic uint8_t uart_tx_byte;
|
_Atomic uint8_t uart_tx_byte;
|
||||||
|
// TX state variable
|
||||||
_Atomic enum UartTxState uart_tx_state;
|
_Atomic enum UartTxState uart_tx_state;
|
||||||
|
|
||||||
|
// Flag to stop the event loop thread
|
||||||
_Atomic int stop_event_thread;
|
_Atomic int stop_event_thread;
|
||||||
|
// Handle to the event loop thread
|
||||||
pthread_t event_thread;
|
pthread_t event_thread;
|
||||||
pthread_mutex_t state_mutex, rx_buffer_mutex;
|
|
||||||
|
|
||||||
|
// Mutex that protects the general sss7 state from concurrent modification
|
||||||
|
pthread_mutex_t state_mutex;
|
||||||
|
// Mutex that protects the rx buffer from concurrent modification
|
||||||
|
pthread_mutex_t rx_buffer_mutex;
|
||||||
|
|
||||||
|
// File handle for the serial port
|
||||||
int serial_fd;
|
int serial_fd;
|
||||||
|
|
||||||
int get_milliseconds(void) {
|
|
||||||
|
// Generate a millisecond timestamp from gettimeofday
|
||||||
|
unsigned long get_milliseconds(void) {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
gettimeofday(&tv,NULL);
|
gettimeofday(&tv,NULL);
|
||||||
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open the serial port and configure it
|
||||||
int uart_init(char *serialport) {
|
int serial_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) {
|
||||||
perror("Error: Unable to open serialport");
|
perror("Error: Unable to open serialport");
|
||||||
|
@ -45,6 +58,7 @@ int uart_init(char *serialport) {
|
||||||
//Switch to blocking mode
|
//Switch to blocking mode
|
||||||
fcntl(serial_fd, F_SETFL, 0);
|
fcntl(serial_fd, F_SETFL, 0);
|
||||||
|
|
||||||
|
// Set baudrates for input and output
|
||||||
struct termios options;
|
struct termios options;
|
||||||
cfsetispeed(&options, B9600);
|
cfsetispeed(&options, B9600);
|
||||||
cfsetospeed(&options, B9600);
|
cfsetospeed(&options, B9600);
|
||||||
|
@ -70,20 +84,27 @@ int uart_init(char *serialport) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Event loop thread, design to emulate the uart interrupts on AVR8
|
||||||
void *eventloop(void *arg) {
|
void *eventloop(void *arg) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
int timestamp = get_milliseconds();
|
unsigned long timestamp = get_milliseconds();
|
||||||
|
|
||||||
while(!stop_event_thread) {
|
while(!stop_event_thread) {
|
||||||
|
|
||||||
|
// Send a byte, if there is one
|
||||||
if(uart_tx_state == TX_HAS_BYTE) {
|
if(uart_tx_state == TX_HAS_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);
|
||||||
uart_tx_state = TX_COMPLETE;
|
uart_tx_state = TX_COMPLETE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read back the byte, that was just send.
|
||||||
|
// Or read a byte send by somebody else.
|
||||||
res = read(serial_fd, &uart_rx_byte, 1);
|
res = read(serial_fd, &uart_rx_byte, 1);
|
||||||
if(res == 1) {
|
if(res == 1) {
|
||||||
printf("Read %x\n", uart_tx_byte);
|
printf("Read %x\n", uart_tx_byte);
|
||||||
|
// Lock mutexes and call rx handler
|
||||||
pthread_mutex_lock(&state_mutex);
|
pthread_mutex_lock(&state_mutex);
|
||||||
pthread_mutex_lock(&rx_buffer_mutex);
|
pthread_mutex_lock(&rx_buffer_mutex);
|
||||||
sss7_process_rx();
|
sss7_process_rx();
|
||||||
|
@ -91,6 +112,7 @@ void *eventloop(void *arg) {
|
||||||
pthread_mutex_unlock(&state_mutex);
|
pthread_mutex_unlock(&state_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Call the tx handler if there has been a byte sent in this iteration
|
||||||
if(uart_tx_state == TX_COMPLETE) {
|
if(uart_tx_state == TX_COMPLETE) {
|
||||||
// Keep this before sss7_process_tx
|
// Keep this before sss7_process_tx
|
||||||
// uart_put_byte might need to overwrite it
|
// uart_put_byte might need to overwrite it
|
||||||
|
@ -101,6 +123,7 @@ void *eventloop(void *arg) {
|
||||||
pthread_mutex_unlock(&state_mutex);
|
pthread_mutex_unlock(&state_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lock muxte and call the timeout handler
|
||||||
pthread_mutex_lock(&state_mutex);
|
pthread_mutex_lock(&state_mutex);
|
||||||
int now = get_milliseconds();
|
int now = get_milliseconds();
|
||||||
sss7_process_ticks(now - timestamp);
|
sss7_process_ticks(now - timestamp);
|
||||||
|
@ -111,11 +134,14 @@ void *eventloop(void *arg) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Setup function for the serialport and the eventloop thread
|
||||||
|
// Return 0 on succes -1 on failure.
|
||||||
int libsss7_start(char *serialport) {
|
int libsss7_start(char *serialport) {
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
sss7_init();
|
sss7_init();
|
||||||
res = uart_init(serialport);
|
res = serial_init(serialport);
|
||||||
if(res) {
|
if(res) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -135,6 +161,8 @@ int libsss7_start(char *serialport) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Teardown function for serialport and event loop thread
|
||||||
void libsss7_stop() {
|
void libsss7_stop() {
|
||||||
stop_event_thread = 1;
|
stop_event_thread = 1;
|
||||||
pthread_join(event_thread, NULL);
|
pthread_join(event_thread, NULL);
|
||||||
|
@ -142,35 +170,41 @@ void libsss7_stop() {
|
||||||
close(serial_fd);
|
close(serial_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper for sss7_can_send
|
||||||
int libsss7_can_send(void) {
|
int libsss7_can_send(void) {
|
||||||
return sss7_can_send();
|
return sss7_can_send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper for sss7_send, also locks the state_mutex
|
||||||
void libsss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
void libsss7_send(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
pthread_mutex_lock(&state_mutex);
|
pthread_mutex_lock(&state_mutex);
|
||||||
sss7_send(msg);
|
sss7_send(msg);
|
||||||
pthread_mutex_unlock(&state_mutex);
|
pthread_mutex_unlock(&state_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper for sss7_send_failed
|
||||||
int libsss7_send_failed(void) {
|
int libsss7_send_failed(void) {
|
||||||
return sss7_send_failed();
|
return sss7_send_failed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper for sss7_has_received
|
||||||
int libsss7_has_received(void) {
|
int libsss7_has_received(void) {
|
||||||
return sss7_has_received();
|
return sss7_has_received();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wrapper for sss7_get_received, takes care of locking rx buffer mutex
|
||||||
void libsss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
void libsss7_get_received(uint8_t msg[SSS7_PAYLOAD_SIZE]) {
|
||||||
pthread_mutex_lock(&rx_buffer_mutex);
|
pthread_mutex_lock(&rx_buffer_mutex);
|
||||||
sss7_get_received(msg);
|
sss7_get_received(msg);
|
||||||
pthread_mutex_unlock(&rx_buffer_mutex);
|
pthread_mutex_unlock(&rx_buffer_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Platform dependent function required by the sss7core
|
||||||
uint8_t uart_get_byte(void) {
|
uint8_t uart_get_byte(void) {
|
||||||
return uart_rx_byte;
|
return uart_rx_byte;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Platform dependent function required by the sss7core
|
||||||
void uart_put_byte(uint8_t byte) {
|
void uart_put_byte(uint8_t byte) {
|
||||||
uart_tx_state = TX_HAS_BYTE;
|
uart_tx_state = TX_HAS_BYTE;
|
||||||
uart_tx_byte = byte;
|
uart_tx_byte = byte;
|
||||||
|
|
|
@ -3,18 +3,27 @@
|
||||||
|
|
||||||
#include "sss7.h"
|
#include "sss7.h"
|
||||||
|
|
||||||
|
// See libsss7.c for more detailed descriptions
|
||||||
|
|
||||||
|
// Setup function
|
||||||
int libsss7_start(char *serialport);
|
int libsss7_start(char *serialport);
|
||||||
|
|
||||||
|
// Checks if a frame can be sent
|
||||||
int libsss7_can_send(void);
|
int libsss7_can_send(void);
|
||||||
|
|
||||||
|
// Send a frame with the payload msg
|
||||||
void libsss7_send(uint8_t *msg);
|
void libsss7_send(uint8_t *msg);
|
||||||
|
|
||||||
|
// Check if the last transmission failed
|
||||||
int libsss7_send_failed(void);
|
int libsss7_send_failed(void);
|
||||||
|
|
||||||
|
// Check if there are received payloads in the rx buffer
|
||||||
int libsss7_has_received(void);
|
int libsss7_has_received(void);
|
||||||
|
|
||||||
|
// Get a payload from the rx buffer
|
||||||
void libsss7_get_received(uint8_t *msg);
|
void libsss7_get_received(uint8_t *msg);
|
||||||
|
|
||||||
|
// Teardown function
|
||||||
void libsss7_stop();
|
void libsss7_stop();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue