diff --git a/Module/Makefile b/Module/Makefile index adbd8e1..c4a4fa6 100644 --- a/Module/Makefile +++ b/Module/Makefile @@ -3,7 +3,7 @@ KERNELDIR=/lib/modules/`uname -r`/build # This the the Makefile for the device driver (not your demonstration program) #Change the base name here to your module file name - no extension -BASENAME=helloworld +BASENAME=tempMonitor MODULES = $(BASENAME).ko @@ -13,5 +13,4 @@ all: make -C $(KERNELDIR) M=$(PWD) modules clean: - make -C $(KERNELDIR) M=$(PWD) clean - + make -C $(KERNELDIR) M=$(PWD) clean \ No newline at end of file diff --git a/Module/tempMonitor.c b/Module/tempMonitor.c new file mode 100644 index 0000000..bb4ba69 --- /dev/null +++ b/Module/tempMonitor.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include +#include +#include + +#define DEVICE_NAME "temp_monitor" +#define IOCTL_GET_TEMP _IOR('t', 1, int) + +static int temp = 0; +static int major_number; +static struct class* temp_monitor_class = NULL; +static struct device* temp_monitor_device = NULL; + +static int device_open(struct inode *inode, struct file *file) { + return 0; +} + +static int device_release(struct inode *inode, struct file *file) { + return 0; +} + +static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { + switch (cmd) { + case IOCTL_GET_TEMP: + // Simulate reading temperature + temp = 50; // Example temperature + if (copy_to_user((int *)arg, &temp, sizeof(temp))) { + return -EACCES; + } + break; + default: + return -EINVAL; + } + return 0; +} + +static struct file_operations fops = { + .open = device_open, + .release = device_release, + .unlocked_ioctl = device_ioctl, +}; + +static int __init temp_monitor_init(void) { + major_number = register_chrdev(0, DEVICE_NAME, &fops); + if (major_number < 0) { + printk(KERN_ALERT "temp_monitor: Failed to register a major number\n"); + return major_number; + } + temp_monitor_class = class_create(THIS_MODULE, DEVICE_NAME); + if (IS_ERR(temp_monitor_class)) { + unregister_chrdev(major_number, DEVICE_NAME); + printk(KERN_ALERT "temp_monitor: Failed to register device class\n"); + return PTR_ERR(temp_monitor_class); + } + temp_monitor_device = device_create(temp_monitor_class, NULL, MKDEV(major_number, 0), NULL, DEVICE_NAME); + if (IS_ERR(temp_monitor_device)) { + class_destroy(temp_monitor_class); + unregister_chrdev(major_number, DEVICE_NAME); + printk(KERN_ALERT "temp_monitor: Failed to create the device\n"); + return PTR_ERR(temp_monitor_device); + } + printk(KERN_INFO "temp_monitor: Device initialized\n"); + return 0; +} + +static void __exit temp_monitor_exit(void) { + device_destroy(temp_monitor_class, MKDEV(major_number, 0)); + class_unregister(temp_monitor_class); + class_destroy(temp_monitor_class); + unregister_chrdev(major_number, DEVICE_NAME); + printk(KERN_INFO "temp_monitor: Device removed\n"); +} + +module_init(temp_monitor_init); +module_exit(temp_monitor_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Your Name"); +MODULE_DESCRIPTION("A CPU Temperature Monitoring Kernel Module"); \ No newline at end of file diff --git a/Test/Makefile b/Test/Makefile index ad02652..7ed935b 100644 --- a/Test/Makefile +++ b/Test/Makefile @@ -33,8 +33,8 @@ # will delete the executable and any object files in your directory. # -FIRSTNAME= -LASTNAME= +FIRSTNAME=UzairHamed +LASTNAME=Mohammed ROOTNAME=$(LASTNAME)_$(FIRSTNAME)_HW HW=6 diff --git a/Test/Mohammed_UzairHamed_HW6_main.c b/Test/Mohammed_UzairHamed_HW6_main.c new file mode 100644 index 0000000..cffd82f --- /dev/null +++ b/Test/Mohammed_UzairHamed_HW6_main.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include + +#define DEVICE_NAME "/dev/temp_monitor" +#define IOCTL_GET_TEMP _IOR('t', 1, int) + +int main() { + int fd; + int temp; + + fd = open(DEVICE_NAME, O_RDONLY); + if (fd < 0) { + perror("Failed to open the device"); + return -1; + } + + if (ioctl(fd, IOCTL_GET_TEMP, &temp) < 0) { + perror("Failed to get temperature"); + close(fd); + return -1; + } + + printf("Current CPU Temperature: %d°C\n", temp); + + close(fd); + return 0; +} \ No newline at end of file diff --git a/me.md b/me.md new file mode 100644 index 0000000..6da6204 --- /dev/null +++ b/me.md @@ -0,0 +1,25 @@ +# Project Idea + +## Temperature Monitoring Driver + +Interfaces with sensors to monitor system temperature and provides alerts if thresholds are exceeded. + +### Kernel Module + +Functions: + +- `open` +- `release` +- `read` +- `write` +- `ioctl` + +Use `ioctl` to set alert thresholds and configure sensor settings. + +Use `read` to retrieve temperature data, and `write` to configure thresholds. + +Ensure proper unloading and use `printk` for logging messages. + +### User Application + +Open the device, read temperature data, set thresholds using `ioctl`, and display alerts. Also verify that temperature data is correctly read and alerts are triggered. \ No newline at end of file