commit 24e635da88749ddc99b6ad4fda53122b11de35cd Author: serxoz Date: Wed Jul 12 12:20:53 2023 +0200 primeiro commit - detecta eventos de conexión diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..32829df --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# build results +target + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..459f42a --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +CC ?= cc +INSTALL_PATH ?= /usr + +all: build + +build: target/mpfm target/mpfm.service + +target/mpfm: mpfm.c + mkdir -p target + $(CC) -std=gnu99 -O2 mpfm.c -I/usr/include/libusb-1.0 -lusb-1.0 -o target/mpfm + +target/mpfm.service: mpfm.service + mkdir -p target + sed -E 's|__INSTALL_PATH__|$(INSTALL_PATH)|g' mpfm.service > target/mpfm.service + +install: build mpfm.rules + mkdir -p $(INSTALL_PATH)/{bin,lib/systemd/system} + install target/mpfm $(INSTALL_PATH)/bin/mpfm + install target/mpfm.service $(INSTALL_PATH)/lib/systemd/system/mpfm.service + +uninstall: + rm -f $(INSTALL_PATH)/lib/systemd/system/mpfm.service + rm -f $(INSTALL_PATH)/bin/mpfm + +clean: + rm -rf target + +.PHONY: all build clean install uninstall diff --git a/README.md b/README.md new file mode 100644 index 0000000..766ec7f --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Music Player FM + +A idea é non cambiar o aparello de radio do coche e poder escoitar música desde +unha unidade USB. Para esto vou usar unha raspberry para emitir a música que +leva o pendrive a través de radio FM. + +Este proxecto estará «vixilando» a existencia de un pendrive conectado á raspberry +e en canto sexa posible, montarao e reproducirá os arquivos de música. Levándoos +ás ondas de radio gracias a [este outro proxecto](https://github.com/markondej/fm_transmitter). diff --git a/mpfm.c b/mpfm.c new file mode 100644 index 0000000..3a0faeb --- /dev/null +++ b/mpfm.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +static int count = 0; + +int hotplug_callback(struct libusb_context *ctx, struct libusb_device *dev, + libusb_hotplug_event event, void *user_data) { + static libusb_device_handle *dev_handle = NULL; + struct libusb_device_descriptor desc; + int rc; + printf("Entering callback\n"); // DEBUG + (void)libusb_get_device_descriptor(dev, &desc); + + if (LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED == event) { + rc = libusb_open(dev, &dev_handle); + if (LIBUSB_SUCCESS != rc) { + printf("Could not open USB device\n"); + } + } else if (LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT == event) { + if (dev_handle) { + libusb_close(dev_handle); + dev_handle = NULL; + } + } else { + printf("Unhandled event %d\n", event); + } + count++; + + return 0; +} + +int main (void) { + libusb_hotplug_callback_handle callback_handle; + int rc; + + libusb_init(NULL); + + rc = libusb_hotplug_register_callback( + NULL, + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, + 0, + LIBUSB_HOTPLUG_MATCH_ANY, + LIBUSB_HOTPLUG_MATCH_ANY, + LIBUSB_HOTPLUG_MATCH_ANY, + hotplug_callback, + NULL, + &callback_handle); + if (LIBUSB_SUCCESS != rc) { + printf("Error creating a hotplug callback\n"); + libusb_exit(NULL); + return EXIT_FAILURE; + } + + while (count < 2) { + libusb_handle_events_completed(NULL, NULL); + nanosleep(&(struct timespec){0, 10000000UL}, NULL); + } + + libusb_hotplug_deregister_callback(NULL, callback_handle); + libusb_exit(NULL); + + return 0; +} diff --git a/mpfm.service b/mpfm.service new file mode 100644 index 0000000..6138f69 --- /dev/null +++ b/mpfm.service @@ -0,0 +1,6 @@ +[Unit] +Description=Music Player FM + +[Service] +ExecStart=__INSTALL_PATH__/bin/mpfm +