Testing...
This commit is contained in:
parent
17fa22c7ae
commit
7e87cabb3c
@ -36,16 +36,15 @@ static ssize_t device_read(struct file *file, char __user *buffer, size_t len, l
|
|||||||
struct file *f;
|
struct file *f;
|
||||||
char *buf;
|
char *buf;
|
||||||
loff_t pos = 0;
|
loff_t pos = 0;
|
||||||
|
bool is_simulated = false;
|
||||||
|
|
||||||
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
|
// Check if the temperature file exists
|
||||||
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
|
f = filp_open(TEMP_SENSOR_PATH, O_RDONLY, 0);
|
||||||
if (IS_ERR(f)) {
|
if (IS_ERR(f)) {
|
||||||
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
|
printk(KERN_INFO "temp_monitor: Temperature file not found\n");
|
||||||
return -EINVAL;
|
is_simulated = true;
|
||||||
|
get_random_bytes(&temp, sizeof(temp));
|
||||||
|
temp = (temp % 40) + 10; // Simulate a temperature between 10 and 50 degrees
|
||||||
} else {
|
} else {
|
||||||
buf = kmalloc(16, GFP_KERNEL);
|
buf = kmalloc(16, GFP_KERNEL);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
@ -60,7 +59,6 @@ static ssize_t device_read(struct file *file, char __user *buffer, size_t len, l
|
|||||||
filp_close(f, NULL);
|
filp_close(f, NULL);
|
||||||
kfree(buf);
|
kfree(buf);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Convert to desired unit
|
// Convert to desired unit
|
||||||
if (strcmp(temp_unit, "F") == 0) {
|
if (strcmp(temp_unit, "F") == 0) {
|
||||||
@ -69,7 +67,7 @@ static ssize_t device_read(struct file *file, char __user *buffer, size_t len, l
|
|||||||
temp += 273; // Convert to Kelvin
|
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
|
// Copy the result to user-space
|
||||||
if (copy_to_user(buffer, temp_str, strlen(temp_str) + 1)) {
|
if (copy_to_user(buffer, temp_str, strlen(temp_str) + 1)) {
|
||||||
|
|||||||
@ -35,6 +35,7 @@ void read_temp(int fd, const char *unit, int is_simulated) {
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int fd;
|
int fd;
|
||||||
|
char choice;
|
||||||
|
|
||||||
fd = open("/dev/temp_monitor", O_RDWR);
|
fd = open("/dev/temp_monitor", O_RDWR);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
@ -42,11 +43,21 @@ int main() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ask the user for the preferred temperature unit
|
||||||
|
printf("Choose temperature unit (C/F): ");
|
||||||
|
scanf(" %c", &choice);
|
||||||
|
|
||||||
|
if (choice == 'C' || choice == 'c') {
|
||||||
// Requesting the temperature in Celsius
|
// Requesting the temperature in Celsius
|
||||||
read_temp(fd, "C", 0);
|
read_temp(fd, "C", 0);
|
||||||
|
} else if (choice == 'F' || choice == 'f') {
|
||||||
// Requesting the temperature in Fahrenheit
|
// Requesting the temperature in Fahrenheit
|
||||||
read_temp(fd, "F", 0);
|
read_temp(fd, "F", 0);
|
||||||
|
} else {
|
||||||
|
printf("Invalid choice. Please choose 'C' or 'F'.\n");
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Requesting simulated temperature
|
// Requesting simulated temperature
|
||||||
read_temp(fd, "simulate", 1);
|
read_temp(fd, "simulate", 1);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user