Counter reader

This commit is contained in:
Inex Code 2023-04-28 09:38:13 +03:00
parent 0e7e0d9bcc
commit 8c4c31b686
4 changed files with 56 additions and 5 deletions

View File

@ -10,7 +10,7 @@
#include <linux/kthread.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Inex Code");
MODULE_DESCRIPTION("A counter device");
MODULE_DESCRIPTION("A counter_reader device");
MODULE_VERSION("0.0.3");
#define DEVICE_NAME "inex_counter"
@ -53,7 +53,7 @@ static int counter_thread(void *data) {
}
static int __init counter_init(void) {
printk(KERN_INFO "Counter: Initializing the counter device.\n");
printk(KERN_INFO "Counter: Initializing the counter_reader device.\n");
Major = register_chrdev(0, DEVICE_NAME, &counter_fops);
if (Major < 0) {
printk(KERN_ALERT "Registering char device failed with %d\n", Major);
@ -93,7 +93,7 @@ static int device_release(struct inode *inode, struct file *file) {
}
static ssize_t device_read(struct file *filp, char __user *buffer, size_t length, loff_t *offset) {
// Output the counter value to the user.
// Output the counter_reader value to the user.
int bytes_read = 0;
char buf[32];
int len = sprintf(buf, "%d\n", counter);
@ -107,13 +107,13 @@ static ssize_t device_read(struct file *filp, char __user *buffer, size_t length
return -EINVAL;
}
bytes_read = len;
printk("Counter: Read %d bytes, counter = %d", bytes_read, counter);
printk("Counter: Read %d bytes, counter_reader = %d", bytes_read, counter);
is_read = 1;
return bytes_read;
}
static ssize_t device_write(struct file *filp, const char __user *buffer, size_t length, loff_t *offset) {
// Input the counter value from the user.
// Input the counter_reader value from the user.
char buf[32];
int len = length;
if (len > sizeof(buf)) {

View File

@ -0,0 +1,16 @@
"""
This file is used to read the counter_reader value from the /dev/inex_counter device.
The value is then send as a notification.
"""
from subprocess import run
def __main__():
"""
This function is called when the script is executed.
"""
# Read the counter_reader value from the device.
counter_value = run(["cat", "/dev/inex_counter"], capture_output=True).stdout.decode("utf-8")
# Send the counter_reader value as a notification.
run(["notify-send", "Counter value", counter_value])

View File

@ -0,0 +1,14 @@
[Unit]
# A short human readable title of the unit
Description=Counter Reader
[Service]
# Configures the process start-up type for this service unit, one of:
# simple - The process defined in ExecStart= will stay in the foreground while the unit is activated.
# forking - The process defined in ExecStart= will fork a background process and exit right away.
# oneshot - The process will exit right away, use with RemainAfterExit= to ensure the serice is marked as active.
# Consult the documentantion for types (https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=) other options.
Type=oneshot
# Command with arguments to invoke when the unit is activated.
# This runs a python script that reads the counter
ExecStart=/usr/bin/python3 /home/inex/dev/Linux/counter_reader/counter_reader.py

View File

@ -0,0 +1,21 @@
[Unit]
# A short human readable title of the unit
Description=Counter Reader launcher
# A list of units whose activations will occur before this unit starts.
After=network.target
[Timer]
# The syntax for timers is described in (https://www.freedesktop.org/software/systemd/man/systemd.time.html#)
# A shorthand version is that you can specify timers by specifying values using (us, ms, s, m, h, days, weeks, months, years)
# e.g., 2 weeks 1 hours or 1 year 6 months or 5 m 30 s
# Defines a timer relative to the moment the timer unit itself is activated.
OnActiveSec=30s
# In addition to the above, OnCalendar takes a cron like syntax or values such as (minutely, hourly, daily, monthly, weekly,...)
# Defines realtime (i.e. wallclock) timers with calendar event expressions
OnCalendar=daily
[Install]
# A list of units who when activated will try and activate this unit
WantedBy=multi-user.target