diff options
author | xengineering <mail2xengineering@protonmail.com> | 2020-10-15 12:48:30 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2020-10-15 12:48:30 +0200 |
commit | 3f739baab456c2cc977f11090381d1f61eb023c4 (patch) | |
tree | a35293cd7a0164601213bdfbd10774a1e998ebc1 | |
parent | 449b490f233371fe28e9089a05982cd5634285d2 (diff) | |
download | iot-barcode-scanner-3f739baab456c2cc977f11090381d1f61eb023c4.tar iot-barcode-scanner-3f739baab456c2cc977f11090381d1f61eb023c4.tar.zst iot-barcode-scanner-3f739baab456c2cc977f11090381d1f61eb023c4.zip |
Split Executable to reduce root-priviledged Code Execution
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | README.md | 32 | ||||
-rwxr-xr-x | barcode_scanner_daemon.py | 42 | ||||
-rw-r--r-- | barcode_transmit_daemon.py | 38 | ||||
-rw-r--r-- | config.json | 6 | ||||
-rwxr-xr-x | main.py | 29 |
6 files changed, 113 insertions, 35 deletions
@@ -1,2 +1 @@ -first_test.py __pycache__ @@ -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() |