From 3f739baab456c2cc977f11090381d1f61eb023c4 Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 15 Oct 2020 12:48:30 +0200 Subject: Split Executable to reduce root-priviledged Code Execution --- .gitignore | 1 - README.md | 32 ++++++++++++++++++++++++++++---- barcode_scanner_daemon.py | 42 ++++++++++++++++++++++++++++++++++++++++++ barcode_transmit_daemon.py | 38 ++++++++++++++++++++++++++++++++++++++ config.json | 6 +++++- main.py | 29 ----------------------------- 6 files changed, 113 insertions(+), 35 deletions(-) create mode 100755 barcode_scanner_daemon.py create mode 100644 barcode_transmit_daemon.py delete mode 100755 main.py diff --git a/.gitignore b/.gitignore index 0f4de50..bee8a64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -first_test.py __pycache__ diff --git a/README.md b/README.md index c1f49b8..96b137f 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,33 @@ A service that makes barcode scanners available on the network for IoT use. +## Usage + +1. Adapt config.json file if needed. +2. Run ```sudo python barcode_scanner_daemon.py``` +3. Run ```python barcode_transmit_daemon.py``` +4. Listen to MQTT output (e.g. with ```mosquitto_sub -h localhost -t "xengineering.eu/iot-barcode-scanner"```) + + ## Milestones -- MVP: Forward keyboard/scanner input to MQTT topic -- Read MQTT connection details from config file -- Disable scanner as regular input source and bind it only to the service -- Package it for Arch Linux +- [x] MVP: Forward keyboard/scanner input to MQTT topic +- [x] Use config file +- [x] Disable scanner as regular input source and bind it only to the service +- [x] Split into two executables to reduce root-priviledged code +- [ ] Implement systemd services +- [ ] Write Makefile for easy installation +- [ ] Implement auto discovery of barcode scanners +- [ ] Package it for Arch Linux + + +## Dependencies + +- ```python-paho-mqtt``` +- ```python-evdev``` + + +## Links + +- [python-evdev documentation](https://python-evdev.readthedocs.io/en/latest/index.html) +- [Please answer this guy!](https://stackoverflow.com/questions/49850238/redirect-usb-hid-barcode-output-to-dev-tty-device) diff --git a/barcode_scanner_daemon.py b/barcode_scanner_daemon.py new file mode 100755 index 0000000..38f0514 --- /dev/null +++ b/barcode_scanner_daemon.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + + +"""Executable to read the Key Events from the Barcode Scanner (root Priviledges necessary)""" + + +import evdev +import os + +import iot_barcode_scanner.config as config + + +def main(): + + try: + # read config + cfg = config.get_config() + fifo_path = cfg["scanner"]["fifo_path"] + event_device_path = cfg["scanner"]["evdev_path"] + + # prepare fifo + os.makedirs(os.path.dirname(fifo_path), exist_ok=True) + os.mkfifo(fifo_path) + + barcode_scanner = evdev.InputDevice(event_device_path) + with barcode_scanner.grab_context(): + for event in barcode_scanner.read_loop(): + if event.type == evdev.ecodes.EV_KEY: + eventdata = evdev.categorize(event) + if eventdata.keystate: + with open(fifo_path, "a") as fifo: + fifo.append(eventdata.keycode) + + except KeyboardInterrupt: + pass + + finally: + os.remove(fifo_path) + + +if __name__ == "__main__": + main() diff --git a/barcode_transmit_daemon.py b/barcode_transmit_daemon.py new file mode 100644 index 0000000..4edb72d --- /dev/null +++ b/barcode_transmit_daemon.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + + +"""Executable to transmit the Barcode Data to MQTT without root Priviledges""" + + +import iot_barcode_scanner.config as config +from iot_barcode_scanner.mqtt import MqttService + + +def main(): + try: + + # read config + cfg = config.get_config() + fifo_path = cfg["scanner"]["fifo_path"] + topic = cfg["mqtt"]["topic"] + + # setup mqtt + mqtt_service = MqttService(cfg) + mqtt_service.run() + + while True: + with open(fifo_path, "r") as fifo: + text = fifo.read() + mqtt_service.client.publish( + topic, + payload=text, + qos=0, + retain=False + ) + + except KeyboardInterrupt: + pass + + +if __name__ == "__main__": + main() diff --git a/config.json b/config.json index 3f046a3..223dbcf 100644 --- a/config.json +++ b/config.json @@ -2,6 +2,10 @@ "mqtt":{ "broker":"127.0.0.1", "port":1883, - "topic":"eu/xengineering/iot-barcode-scanner" + "topic":"xengineering.eu/iot-barcode-scanner" + }, + "scanner":{ + "evdev_path":"/dev/input/event4", + "fifo_path":"/tmp/xengineering.eu/iot-barcode-scanner/scanner_fifo" } } diff --git a/main.py b/main.py deleted file mode 100755 index b50962a..0000000 --- a/main.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/python3 - - -"""Main executable for the Package / Service""" - - -import iot_barcode_scanner as ibs - - -def main(): - try: - config = ibs.get_config() - mqtt_service = ibs.MqttService(config) - mqtt_service.run() - print("Starting Service") - while True: - text = input() - mqtt_service.client.publish( - config["mqtt"]["topic"], - payload=text, - qos=0, - retain=False - ) - except KeyboardInterrupt: - print("Service stopped") - - -if __name__ == "__main__": - main() -- cgit v1.2.3-70-g09d2