First push

This commit is contained in:
uzy lol 2024-12-05 22:36:41 -08:00
parent 5cb3fd2d1e
commit 0319f66d39
5 changed files with 140 additions and 5 deletions

View File

@ -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
@ -14,4 +14,3 @@ all:
clean:
make -C $(KERNELDIR) M=$(PWD) clean

82
Module/tempMonitor.c Normal file
View File

@ -0,0 +1,82 @@
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#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");

View File

@ -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

View File

@ -0,0 +1,29 @@
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#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;
}

25
me.md Normal file
View File

@ -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.