From 15b1b4d0fb491ab0bcde821b37ec13876d48a56c Mon Sep 17 00:00:00 2001
From: "leon.bohnwagner" <leon.bohnwagner@informatik.hs-fulda.de>
Date: Wed, 12 Feb 2025 18:59:26 +0100
Subject: [PATCH] feat: add hrtimer to kernel module

---
 kernel/kmod.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/kernel/kmod.c b/kernel/kmod.c
index cb13bab..e29d238 100644
--- a/kernel/kmod.c
+++ b/kernel/kmod.c
@@ -4,17 +4,23 @@
 #include <linux/cdev.h>
 #include <linux/vmalloc.h>
 #include <linux/uaccess.h>
+#include <linux/hrtimer.h>
 #include "../common/include/message.h"
 
 #define DEVICE_NAME "amogus"
 #define CLASS_NAME "amogus_class"
 
+#define SLOT_INTERVAL_NS 250000
+
 static int major_number;
 static struct class* class = NULL;
 static struct cdev mycdev;
 
 static message_t* message;
 
+static struct hrtimer timer;
+static ktime_t interval;
+
 static int device_open(struct inode *inode, struct file *file)
 {
     return 0;
@@ -48,6 +54,11 @@ static const struct file_operations fops = {
     .release = device_release
 };
 
+static enum hrtimer_restart timer_callback(struct hrtimer *timer) {
+    hrtimer_forward_now(timer, interval);
+    return HRTIMER_RESTART;
+}
+
 static int __init lkm_init(void)
 {
     dev_t dev;
@@ -93,6 +104,12 @@ static int __init lkm_init(void)
     }
 
     printk(KERN_INFO "lkm: device created successfully\n");
+
+    interval = ktime_set(0, SLOT_INTERVAL_NS);
+    hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
+    timer.function = timer_callback;
+    hrtimer_start(&timer, interval, HRTIMER_MODE_REL);
+
     return 0;
 }
 
@@ -104,6 +121,8 @@ static void __exit lkm_exit(void)
     class_destroy(class);
     unregister_chrdev_region(MKDEV(major_number, 0), 1);
     printk(KERN_INFO "lkm: device removed successfully\n");
+
+    hrtimer_cancel(&timer);
 }
 
 module_init(lkm_init);
-- 
GitLab