Skip to content
Snippets Groups Projects
Verified Commit dc98526c authored by Štěpán Henek's avatar Štěpán Henek :bear:
Browse files

sendbeacon: runforever or end after a beacon is successfully recieved

note the 0x15 is read from the serial console when beacon succeeds
parent 2f492428
No related branches found
No related tags found
No related merge requests found
CFLAGS += -pthread
LDLAGS += -pthread
sendbeacon: sendbeacon.c
......@@ -22,15 +22,22 @@
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define READ_BUFFER_SIZE 100
#define VERIFY_LEN 20
#define VERIFY_CHR 0x15
#define error_message(args...) fprintf(stderr, args)
static pthread_barrier_t barrier;
int set_interface_attribs(int fd, int speed, int parity)
{
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0) {
error_message("error %d from tcgetattr", errno);
error_message("error %d from tcgetattr\n", errno);
return -1;
}
......@@ -49,31 +56,92 @@ int set_interface_attribs(int fd, int speed, int parity)
tty.c_cflag &= ~CRTSCTS;
if(tcsetattr(fd, TCSANOW, &tty) != 0) {
error_message("error %d from tcsetattr", errno);
error_message("error %d from tcsetattr\n", errno);
return -1;
}
return 0;
}
void set_blocking(int fd, int block)
void set_blocking(int fd, char block, char timeout)
{
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fd, &tty) != 0) {
error_message("error %d from tggetattr", errno);
error_message("error %d from tggetattr\n", errno);
return;
}
tty.c_cc[VMIN] = block ? 1 : 0;
tty.c_cc[VTIME] = 5;
tty.c_cc[VMIN] = block;
tty.c_cc[VTIME] = timeout;
if(tcsetattr(fd, TCSANOW, &tty) != 0)
error_message("error %d setting term attributes", errno);
error_message("error %d setting term attributes\n", errno);
}
void print_usage()
{
printf("usage: sendbeacon <serial_device> \n");
printf("usage: sendbeacon <serial_device>\n");
}
void * write_handler(void *ptr)
{
char *path = (char *)ptr;
while (1) {
int fd = open(path, O_WRONLY | O_NOCTTY | O_SYNC);
if(fd < 0) {
error_message("error %d opening %s: %s\n", errno, path, strerror(errno));
return NULL;
}
set_interface_attribs(fd, B115200, 0);
set_blocking(fd, 0, 5);
// write for some time
char buf [8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xbb};
int i;
for (i = 0; i < 10000; i++) {
write(fd, buf, 8);
}
close(fd);
pthread_barrier_wait(&barrier);
}
}
void * read_handler(void *ptr)
{
char *path = (char *)ptr;
char expected[VERIFY_LEN];
memset(expected, VERIFY_CHR, VERIFY_LEN);
while (1) {
int fd = open(path, O_RDONLY | O_NOCTTY | O_SYNC);
if(fd < 0) {
error_message("error %d opening %s: %s\n", errno, path, strerror(errno));
return NULL;
}
set_interface_attribs(fd, B115200, 0);
set_blocking(fd, VERIFY_LEN, 5);
char buffer[READ_BUFFER_SIZE];
int cnt = read(fd, buffer, sizeof buffer);
if (cnt) {
int i;
for (i = 0; i < cnt; ++i) {
printf("%02X", (unsigned char)buffer[i]);
}
printf("\n");
if (cnt == VERIFY_LEN) {
if (0 == memcmp(expected, buffer, VERIFY_LEN)) {
exit(0);
}
}
}
close(fd);
pthread_barrier_wait(&barrier);
}
}
int main(int argc, char **argv)
......@@ -90,23 +158,32 @@ int main(int argc, char **argv)
char *portname = argv[1];
int fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if(fd < 0) {
error_message("error %d opening %s: %s", errno, portname, strerror(errno));
// init barrier
int res = pthread_barrier_init(&barrier, NULL, 2);
if (res != 0) {
error_message("pthread_barrier_init failed!\n");
return 1;
}
set_interface_attribs(fd, B115200, 0);
set_blocking(fd, 0);
// create worker threads
pthread_t write_thread;
res = pthread_create(&write_thread, NULL, write_handler, (void *) portname);
if (res != 0) {
error_message("failed to create write thread!\n");
return 1;
}
printf("Sending beacon in loop. You have 5-10 seconds to power up Omnia.\n");
pthread_t read_thread;
res = pthread_create(&read_thread, NULL, read_handler, (void *) portname);
if (res != 0) {
error_message("failed to create read thread!\n");
return 1;
}
char buf [8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0xbb};
int i;
for(i=0; i<10000; i++)
write(fd, buf, 8);
close(fd);
pthread_join(write_thread, NULL);
pthread_join(read_thread, NULL);
return 0;
// this should not be reached if no error is triggered
return 1;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment