Skip to content

Commit

Permalink
Add udev listener for GPS
Browse files Browse the repository at this point in the history
  • Loading branch information
gdyuldin committed Oct 1, 2024
1 parent b63b537 commit 6dcf494
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ target_link_libraries(${PROJECT_NAME} PRIVATE png)
target_link_libraries(${PROJECT_NAME} PRIVATE gps)
target_link_libraries(${PROJECT_NAME} PRIVATE sndfile)
target_link_libraries(${PROJECT_NAME} PRIVATE RHVoice RHVoice_core RHVoice_audio)
target_link_libraries(${PROJECT_NAME} PRIVATE udev)
1 change: 1 addition & 0 deletions src/dialog.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "main_screen.h"
#include "keyboard.h"
#include "events.h"
#include "waterfall.h"

static lv_obj_t *obj;
static dialog_t *current_dialog = NULL;
Expand Down
45 changes: 44 additions & 1 deletion src/gps.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
#include <errno.h>
#include <pthread.h>
#include <math.h>
#include <libudev.h>

static struct gps_data_t gpsdata;
static uint64_t prev_time = 0;
static gps_status_t status=GPS_STATUS_WAITING;

static struct udev *udev;
static struct udev_device *dev;
static struct udev_monitor *mon;
static int fd;


static bool connect() {
if (gps_open("localhost", "2947", &gpsdata) == -1) {
Expand Down Expand Up @@ -59,19 +65,56 @@ static void disconnect() {

}

static void wait_new_device() {
while (1) {
fd_set fds;
struct timeval tv;
int ret;

FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 0;
tv.tv_usec = 0;

ret = select(fd+1, &fds, NULL, NULL, &tv);
if (ret > 0 && FD_ISSET(fd, &fds)) {
dev = udev_monitor_receive_device(mon);
if (dev && (strcmp(udev_device_get_action(dev), "add") == 0)) {
/* free dev */
udev_device_unref(dev);
return;
}
}
/* 500 milliseconds */
usleep(500*1000);
}
}

static void * gps_thread(void *arg) {
while (true) {
status = GPS_STATUS_WAITING;
if (connect()) {
data_receive();
status = GPS_STATUS_RESTARTING;
disconnect();
} else {
status = GPS_STATUS_WAITING;
wait_new_device();
}
usleep(10000000);
}
}

void gps_init() {
/* create udev object */
udev = udev_new();
if (!udev) {
LV_LOG_ERROR("Cannot create udev context.");
}
mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", NULL);
udev_monitor_enable_receiving(mon);
fd = udev_monitor_get_fd(mon);

pthread_t thread;

pthread_create(&thread, NULL, gps_thread, NULL);
Expand Down

0 comments on commit 6dcf494

Please sign in to comment.