Testing...

This commit is contained in:
uzy lol 2024-12-08 17:57:36 -08:00
parent 17fa22c7ae
commit 7e87cabb3c
2 changed files with 32 additions and 23 deletions

View File

@ -36,30 +36,28 @@ static ssize_t device_read(struct file *file, char __user *buffer, size_t len, l
struct file *f;
char *buf;
loff_t pos = 0;
bool is_simulated = false;
if (simulate_temp) {
// 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");
is_simulated = true;
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
buf = kmalloc(16, GFP_KERNEL);
if (!buf) {
filp_close(f, NULL);
kfree(buf);
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
@ -69,7 +67,7 @@ static ssize_t device_read(struct file *file, char __user *buffer, size_t len, l
temp += 273; // Convert to Kelvin
}
snprintf(temp_str, BUF_LEN, "%d %s\n", temp, temp_unit);
snprintf(temp_str, BUF_LEN, "%d %s%s\n", temp, temp_unit, is_simulated ? " (simulated temperature)" : "");
// Copy the result to user-space
if (copy_to_user(buffer, temp_str, strlen(temp_str) + 1)) {

View File

@ -35,6 +35,7 @@ void read_temp(int fd, const char *unit, int is_simulated) {
int main() {
int fd;
char choice;
fd = open("/dev/temp_monitor", O_RDWR);
if (fd < 0) {
@ -42,11 +43,21 @@ int main() {
return -1;
}
// Requesting the temperature in Celsius
read_temp(fd, "C", 0);
// Ask the user for the preferred temperature unit
printf("Choose temperature unit (C/F): ");
scanf(" %c", &choice);
// Requesting the temperature in Fahrenheit
read_temp(fd, "F", 0);
if (choice == 'C' || choice == 'c') {
// Requesting the temperature in Celsius
read_temp(fd, "C", 0);
} else if (choice == 'F' || choice == 'f') {
// Requesting the temperature in Fahrenheit
read_temp(fd, "F", 0);
} else {
printf("Invalid choice. Please choose 'C' or 'F'.\n");
close(fd);
return -1;
}
// Requesting simulated temperature
read_temp(fd, "simulate", 1);