From e133f0a222daf9d28ac08a4a7417e2da57fd25ff Mon Sep 17 00:00:00 2001 From: PA4WDH Date: Sun, 28 Jul 2024 16:21:26 +0200 Subject: Move matching/validating code to cputemp_match_and_validate --- cputemp.c | 111 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 47 insertions(+), 64 deletions(-) diff --git a/cputemp.c b/cputemp.c index 0960662..4276e54 100644 --- a/cputemp.c +++ b/cputemp.c @@ -9,16 +9,51 @@ extern struct s_config config; +// Check if the sensor matches our search criteria and validate the readout +void cputemp_match_and_validate(char *name_file,char *input_file,char *sensor_name,struct s_sensor *sensor) +{ + int datalen; + long int testdata; + char buf[255]; + +// Read the name to see if it matches what we're looking for + datalen=sysfs_read_str(name_file,buf,sizeof(buf)); + + if (datalen<0) + { + DEBUG2_CPUTEMP("Failed to read name\n"); + return; + } + + DEBUG1_CPUTEMP("Name is: %s\n",buf); + if (strncasecmp(buf,sensor_name,255)==0) + { + DEBUG2_CPUTEMP("Name machtes our search criteria\n"); + DEBUG1_CPUTEMP("Input filename: %s\n",input_file); + +// Validate the sensor by reading it + testdata=sysfs_read_long_int(input_file); + DEBUG2_CPUTEMP("Input value: %ld\n",testdata); + if ((testdata>=VALID_TEMP_MIN) && (testdata<=VALID_TEMP_MAX)) + { + DEBUG1_CPUTEMP("Input data is valid, marking sensor as valid\n"); + sensor->valid=1; + strncpy(sensor->name,buf,sizeof(buf)); + strncpy(sensor->filename,input_file,sizeof(sensor->filename)); + } else { + DEBUG1_CPUTEMP("Input data is invalid, ignoring sensor\n"); + } + } +} + // Read a single hwmon directory and search for the requested sensor name void cputemp_read_hwmon(char *hwmon,char *sensor_name,struct s_sensor *sensor) { DIR *hwmon_dir; struct dirent *hwmon_dirent; int count; - char buf[255]; - int datalen; - char sysfs_file[255]; - long int testdata; + char name_file[255]; + char input_file[255]; int offset; DEBUG1_CPUTEMP("Searching for sensors in %s\n",hwmon); @@ -52,36 +87,11 @@ void cputemp_read_hwmon(char *hwmon,char *sensor_name,struct s_sensor *sensor) if (strncmp(hwmon_dirent->d_name+offset,"label",5)==0) { DEBUG2_CPUTEMP("Found label %s\n",hwmon_dirent->d_name); - sprintf(sysfs_file,"%s/%s",hwmon,hwmon_dirent->d_name); - -// Read the label to see if it matches what we're looking for - datalen=sysfs_read_str(sysfs_file,buf,sizeof(buf)); - if (datalen<0) - { - DEBUG2_CPUTEMP("Failed to read label\n"); - } else { - DEBUG1_CPUTEMP("Label is: %s\n",buf); - if (strncasecmp(buf,sensor_name,255)==0) - { - DEBUG2_CPUTEMP("Label machtes our search criteria\n"); - offset=strlen(sysfs_file)-5; - strncpy(sysfs_file+offset,"input",5); - DEBUG1_CPUTEMP("Input filename: %s\n",sysfs_file); - -// Validate the sensor by reading it - testdata=sysfs_read_long_int(sysfs_file); - DEBUG2_CPUTEMP("Input value: %ld\n",testdata); - if ((testdata>=VALID_TEMP_MIN) && (testdata<=VALID_TEMP_MAX)) - { - DEBUG1_CPUTEMP("Input data is valid, marking sensor as valid\n"); - sensor->valid=1; - strncpy(sensor->name,buf,sizeof(buf)); - strncpy(sensor->filename,sysfs_file,sizeof(sysfs_file)); - } else { - DEBUG1_CPUTEMP("Input data is invalid, ignoring sensor\n"); - } - } - } + sprintf(name_file,"%s/%s",hwmon,hwmon_dirent->d_name); + sprintf(input_file,"%s/%s",hwmon,hwmon_dirent->d_name); + offset=strlen(input_file)-5; + strncpy(input_file+offset,"input",5); + cputemp_match_and_validate(name_file,input_file,sensor_name,sensor); } } hwmon_dirent=readdir(hwmon_dir); @@ -92,36 +102,9 @@ void cputemp_read_hwmon(char *hwmon,char *sensor_name,struct s_sensor *sensor) if (sensor->valid==0) { DEBUG2_CPUTEMP("Checking driver name\n"); - sprintf(sysfs_file,"%s/name",hwmon); - -// Read the name to see if it matches what we're looking for - datalen=sysfs_read_str(sysfs_file,buf,sizeof(buf)); - - if (datalen<0) - { - DEBUG2_CPUTEMP("Failed to driver name\n"); - } else { - DEBUG1_CPUTEMP("Name is: %s\n",buf); - if (strncasecmp(buf,sensor_name,255)==0) { - DEBUG2_CPUTEMP("Label machtes our search criteria\n"); - - sprintf(sysfs_file,"%s/temp1_input",hwmon); - DEBUG1_CPUTEMP("Input filename: %s\n",sysfs_file); - -// Validate the sensor by reading it - testdata=sysfs_read_long_int(sysfs_file); - DEBUG2_CPUTEMP("Input value: %ld\n",testdata); - if ((testdata>=VALID_TEMP_MIN) && (testdata<=VALID_TEMP_MAX)) - { - DEBUG1_CPUTEMP("Input data is valid, marking sensor as valid\n"); - sensor->valid=1; - strncpy(sensor->name,buf,sizeof(buf)); - strncpy(sensor->filename,sysfs_file,sizeof(sysfs_file)); - } else { - DEBUG1_CPUTEMP("Input data is invalid, ignoring sensor\n"); - } - } - } + sprintf(name_file,"%s/name",hwmon); + sprintf(input_file,"%s/temp1_input",hwmon); + cputemp_match_and_validate(name_file,input_file,sensor_name,sensor); } } -- cgit v1.2.3