Updated driver and application

This commit is contained in:
uzy lol 2024-12-08 17:33:19 -08:00
parent f0e36b9d2e
commit 17fa22c7ae
2 changed files with 117 additions and 34 deletions

View File

@ -9,14 +9,18 @@
#include <linux/stat.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/random.h>
#define DEVICE_NAME "temp_monitor"
#define IOCTL_GET_TEMP _IOR('t', 1, int)
#define TEMP_SENSOR_PATH "/sys/class/thermal/thermal_zone0/temp"
#define BUF_LEN 100
static int major_number;
static struct class* temp_monitor_class = NULL;
static struct device* temp_monitor_device = NULL;
static char temp_unit[BUF_LEN] = "C"; // Default unit is Celsius
static bool simulate_temp = false;
static int device_open(struct inode *inode, struct file *file) {
return 0;
@ -26,33 +30,84 @@ static int device_release(struct inode *inode, struct file *file) {
return 0;
}
static ssize_t device_read(struct file *file, char __user *buffer, size_t len, loff_t *offset) {
int temp = 0;
char temp_str[BUF_LEN];
struct file *f;
char *buf;
loff_t pos = 0;
if (simulate_temp) {
get_random_bytes(&temp, sizeof(temp));
temp = (temp % 40) + 10; // Simulate a temperature between 10 and 50 degrees
} else {
// Check if the temperature file exists
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
if (IS_ERR(f)) {
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
return -EINVAL;
} else {
buf = kmalloc(16, GFP_KERNEL);
if (!buf) {
filp_close(f, NULL);
return -ENOMEM;
}
// Read the temperature
kernel_read(f, buf, 16, &pos);
sscanf(buf, "%d", &temp);
temp /= 1000; // Convert to degrees Celsius
filp_close(f, NULL);
kfree(buf);
}
}
// Convert to desired unit
if (strcmp(temp_unit, "F") == 0) {
temp = temp * 9 / 5 + 32; // Convert to Fahrenheit
} else if (strcmp(temp_unit, "K") == 0) {
temp += 273; // Convert to Kelvin
}
snprintf(temp_str, BUF_LEN, "%d %s\n", temp, temp_unit);
// Copy the result to user-space
if (copy_to_user(buffer, temp_str, strlen(temp_str) + 1)) {
return -EFAULT;
}
return strlen(temp_str) + 1;
}
static ssize_t device_write(struct file *file, const char __user *buffer, size_t len, loff_t *offset) {
char msg[BUF_LEN];
if (len > BUF_LEN - 1) {
return -EINVAL; // Input too long
}
if (copy_from_user(msg, buffer, len)) {
return -EFAULT;
}
msg[len] = '\0'; // Null-terminate the string
if (strcmp(msg, "simulate") == 0) {
simulate_temp = true;
} else {
simulate_temp = false;
strncpy(temp_unit, msg, BUF_LEN - 1);
}
printk(KERN_INFO "temp_monitor: Write operation: %s\n", msg);
return len;
}
static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
switch (cmd) {
case IOCTL_GET_TEMP: {
int temp = 0;
struct file *f;
char *buf;
loff_t pos = 0;
// Check if the temperature file exists
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
if (IS_ERR(f)) {
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
} else {
buf = kmalloc(16, GFP_KERNEL);
if (!buf) {
filp_close(f, NULL);
return -ENOMEM;
}
// Read the temperature
kernel_read(f, buf, 16, &pos);
sscanf(buf, "%d", &temp);
temp /= 1000; // Convert to degrees Celsius
filp_close(f, NULL);
kfree(buf);
}
// Retrieve the temperature data here
if (copy_to_user((int *)arg, &temp, sizeof(temp))) {
return -EACCES;
}
@ -67,6 +122,8 @@ static long device_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
static struct file_operations fops = {
.open = device_open,
.release = device_release,
.read = device_read,
.write = device_write,
.unlocked_ioctl = device_ioctl,
};

View File

@ -2,28 +2,54 @@
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#define IOCTL_GET_TEMP _IOR('t', 1, int)
#define GREEN "\033[1;32m"
#define RESET "\033[0m"
void read_temp(int fd, const char *unit, int is_simulated) {
char buffer[128];
ssize_t bytes_read;
// Writing the desired unit or "simulate" command to the device
ssize_t bytes_written = write(fd, unit, strlen(unit));
if (bytes_written < 0) {
return; // Exit the function if writing fails
}
// Reading the temperature
bytes_read = read(fd, buffer, sizeof(buffer));
if (bytes_read < 0) {
return; // Exit the function if reading fails
} else {
buffer[bytes_read] = '\0'; // Null-terminate the string
if (is_simulated) {
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s (simulated temperature)\n", buffer);
} else {
printf(GREEN "CPU Temperature\n--- -----------\n" RESET "%s\n", buffer);
}
}
}
int main() {
int fd, temp;
int fd;
fd = open("/dev/temp_monitor", O_RDWR);
if (fd < 0) {
perror("Failed to open the device");
return -1;
}
if (ioctl(fd, IOCTL_GET_TEMP, &temp) == -1) {
perror("Failed to get temperature");
close(fd);
return -1;
}
// Requesting the temperature in Celsius
read_temp(fd, "C", 0);
if (temp == 0) {
printf("\033[32mCPU Temperature\n--- -----------\033[0m\n\033[31mCouldn't determine current temperature. Are you in a virtual machine?\033[0m\n");
} else {
printf("\033[32mCPU Temperature\n--- -----------\033[0m\n%d°C\n", temp);
}
// Requesting the temperature in Fahrenheit
read_temp(fd, "F", 0);
// Requesting simulated temperature
read_temp(fd, "simulate", 1);
close(fd);
return 0;