| 1 | /* |
|---|
| 2 | access.c - Part of libsensors, a Linux library for reading sensor data. |
|---|
| 3 | Copyright (c) 1998 Frodo Looijaard <frodol@dds.nl> |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include <stddef.h> |
|---|
| 21 | #include <string.h> |
|---|
| 22 | #include "access.h" |
|---|
| 23 | #include "sensors.h" |
|---|
| 24 | #include "data.h" |
|---|
| 25 | #include "error.h" |
|---|
| 26 | #include "proc.h" |
|---|
| 27 | |
|---|
| 28 | /* Compare two chips name descriptions, to see whether they could match. |
|---|
| 29 | Return 0 if it does not match, return 1 if it does match. */ |
|---|
| 30 | int sensors_match_chip(sensors_chip_name chip1, sensors_chip_name chip2) |
|---|
| 31 | { |
|---|
| 32 | if ((chip1.prefix != SENSORS_CHIP_NAME_PREFIX_ANY) && |
|---|
| 33 | (chip2.prefix != SENSORS_CHIP_NAME_PREFIX_ANY) && |
|---|
| 34 | strcmp(chip1.prefix,chip2.prefix)) |
|---|
| 35 | return 0; |
|---|
| 36 | if ((chip1.bus != SENSORS_CHIP_NAME_BUS_ANY) && |
|---|
| 37 | (chip2.bus != SENSORS_CHIP_NAME_BUS_ANY) && |
|---|
| 38 | (chip1.bus != chip2.bus)) { |
|---|
| 39 | if ((chip1.bus == SENSORS_CHIP_NAME_BUS_ISA) || |
|---|
| 40 | (chip2.bus == SENSORS_CHIP_NAME_BUS_ISA)) |
|---|
| 41 | return 0; |
|---|
| 42 | if ((chip1.bus != SENSORS_CHIP_NAME_BUS_ANY_I2C) && |
|---|
| 43 | (chip2.bus != SENSORS_CHIP_NAME_BUS_ANY_I2C)) |
|---|
| 44 | return 0; |
|---|
| 45 | } |
|---|
| 46 | if ((chip1.addr != chip2.addr) && |
|---|
| 47 | (chip1.addr != SENSORS_CHIP_NAME_ADDR_ANY) && |
|---|
| 48 | (chip2.addr != SENSORS_CHIP_NAME_ADDR_ANY)) |
|---|
| 49 | return 0; |
|---|
| 50 | return 1; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /* Returns, one by one, a pointer to all sensor_chip structs of the |
|---|
| 54 | config file which match with the given chip name. Last should be |
|---|
| 55 | the value returned by the last call, or NULL if this is the first |
|---|
| 56 | call. Returns NULL if no more matches are found. Do not modify |
|---|
| 57 | the struct the return value points to! |
|---|
| 58 | Note that this visits the list of chips from last to first. Usually, |
|---|
| 59 | you want the match that was latest in the config file. */ |
|---|
| 60 | sensors_chip *sensors_for_all_config_chips(sensors_chip_name chip_name, |
|---|
| 61 | sensors_chip *last) |
|---|
| 62 | { |
|---|
| 63 | int nr,i; |
|---|
| 64 | sensors_chip_name_list chips; |
|---|
| 65 | |
|---|
| 66 | for (nr = last?(last-sensors_config_chips)-1:sensors_config_chips_count-1; |
|---|
| 67 | nr >= 0; nr--) { |
|---|
| 68 | chips = sensors_config_chips[nr].chips; |
|---|
| 69 | for (i =0; i < chips.fits_count; i++) { |
|---|
| 70 | if (sensors_match_chip(chips.fits[i],chip_name)) |
|---|
| 71 | return sensors_config_chips+nr; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | return NULL; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /* Look up a resource in the intern chip list, and return a pointer to it. |
|---|
| 78 | Do not modify the struct the return value points to! Returns NULL if |
|---|
| 79 | not found.*/ |
|---|
| 80 | sensors_chip_feature *sensors_lookup_feature_nr(const char *prefix, int feature) |
|---|
| 81 | { |
|---|
| 82 | int i,j; |
|---|
| 83 | sensors_chip_feature *features; |
|---|
| 84 | for (i = 0; sensors_chip_features_list[i].prefix; i++) |
|---|
| 85 | if (!strcmp(sensors_chip_features_list[i].prefix,prefix)) { |
|---|
| 86 | features = sensors_chip_features_list[i].feature; |
|---|
| 87 | for (j=0; features[j].name; j++) |
|---|
| 88 | if (features[j].number == feature) |
|---|
| 89 | return features + j; |
|---|
| 90 | } |
|---|
| 91 | return NULL; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | /* Look up a resource in the intern chip list, and return a pointer to it. |
|---|
| 95 | Do not modify the struct the return value points to! Returns NULL if |
|---|
| 96 | not found.*/ |
|---|
| 97 | sensors_chip_feature *sensors_lookup_feature_name(const char *prefix, |
|---|
| 98 | const char *feature) |
|---|
| 99 | { |
|---|
| 100 | int i,j; |
|---|
| 101 | sensors_chip_feature *features; |
|---|
| 102 | for (i = 0; sensors_chip_features_list[i].prefix; i++) |
|---|
| 103 | if (!strcmp(sensors_chip_features_list[i].prefix,prefix)) { |
|---|
| 104 | features = sensors_chip_features_list[i].feature; |
|---|
| 105 | for (j=0; features[j].name; j++) |
|---|
| 106 | if (!strcmp(features[j].name,feature)) |
|---|
| 107 | return features + j; |
|---|
| 108 | } |
|---|
| 109 | return NULL; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | /* Check whether the chip name is an 'absolute' name, which can only match |
|---|
| 114 | one chip, or whether it has wildcards. Returns 0 if it is absolute, 1 |
|---|
| 115 | if there are wildcards. */ |
|---|
| 116 | int sensors_chip_name_has_wildcards(sensors_chip_name chip) |
|---|
| 117 | { |
|---|
| 118 | if ((chip.prefix == SENSORS_CHIP_NAME_PREFIX_ANY) || |
|---|
| 119 | (chip.bus == SENSORS_CHIP_NAME_BUS_ANY) || |
|---|
| 120 | (chip.bus == SENSORS_CHIP_NAME_BUS_ANY_I2C) || |
|---|
| 121 | (chip.bus == SENSORS_CHIP_NAME_ADDR_ANY)) |
|---|
| 122 | return 1; |
|---|
| 123 | else |
|---|
| 124 | return 0; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /* Look up the label which belongs to this chip. Note that chip should not |
|---|
| 128 | contain wildcard values! *result is newly allocated (free it yourself). |
|---|
| 129 | This function will return 0 on success, and <0 on failure. */ |
|---|
| 130 | int sensors_get_label(sensors_chip_name name, int feature, char **result) |
|---|
| 131 | { |
|---|
| 132 | sensors_chip *chip; |
|---|
| 133 | sensors_chip_feature *featureptr; |
|---|
| 134 | int i; |
|---|
| 135 | |
|---|
| 136 | if (sensors_chip_name_has_wildcards(name)) |
|---|
| 137 | return -SENSORS_ERR_WILDCARDS; |
|---|
| 138 | if (! (featureptr = sensors_lookup_feature_nr(name.prefix,feature))) |
|---|
| 139 | return -SENSORS_ERR_NO_ENTRY; |
|---|
| 140 | for (chip = NULL; (chip = sensors_for_all_config_chips(name,chip));) |
|---|
| 141 | for (i = 0; i < chip->labels_count; i++) |
|---|
| 142 | if (!strcmp(featureptr->name, chip->labels[i].name)) { |
|---|
| 143 | if (! (*result = strdup(chip->labels[i].value))) |
|---|
| 144 | sensors_fatal_error("sensors_get_label","Allocating label text"); |
|---|
| 145 | return 0; |
|---|
| 146 | } |
|---|
| 147 | if (! (*result = strdup(featureptr->name))) |
|---|
| 148 | sensors_fatal_error("sensors_get_label","Allocating label text"); |
|---|
| 149 | return 0; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /* Read the value of a feature of a certain chip. Note that chip should not |
|---|
| 153 | contain wildcard values! This function will return 0 on success, and <0 |
|---|
| 154 | on failure. */ |
|---|
| 155 | int sensors_get_feature(sensors_chip_name name, int feature, double *result) |
|---|
| 156 | { |
|---|
| 157 | sensors_chip_feature *featureptr; |
|---|
| 158 | sensors_chip *chip; |
|---|
| 159 | sensors_expr *expr = NULL; |
|---|
| 160 | double val; |
|---|
| 161 | int res,i; |
|---|
| 162 | |
|---|
| 163 | if (sensors_chip_name_has_wildcards(name)) |
|---|
| 164 | return -SENSORS_ERR_WILDCARDS; |
|---|
| 165 | if (! (featureptr = sensors_lookup_feature_nr(name.prefix,feature))) |
|---|
| 166 | return -SENSORS_ERR_NO_ENTRY; |
|---|
| 167 | if (! (featureptr->mode && SENSORS_R)) |
|---|
| 168 | return -SENSORS_ERR_ACCESS; |
|---|
| 169 | for (chip = NULL; !expr && (chip = sensors_for_all_config_chips(name,chip));) |
|---|
| 170 | for (i = 0; !expr && (i < chip->computes_count); i++) |
|---|
| 171 | if (!strcmp(featureptr->name,chip->computes->name)) |
|---|
| 172 | expr = chip->computes->from_proc; |
|---|
| 173 | if (sensors_read_proc(name,feature,&val)) |
|---|
| 174 | return -SENSORS_ERR_PROC; |
|---|
| 175 | if (! expr) |
|---|
| 176 | *result = val; |
|---|
| 177 | else if ((res = sensors_eval_expr(expr,val,result))) |
|---|
| 178 | return res; |
|---|
| 179 | return 0; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /* Set the value of a feature of a certain chip. Note that chip should not |
|---|
| 183 | contain wildcard values! This function will return 0 on success, and <0 |
|---|
| 184 | on failure. */ |
|---|
| 185 | int sensors_set_feature(sensors_chip_name name, int feature, double value) |
|---|
| 186 | { |
|---|
| 187 | sensors_chip_feature *featureptr; |
|---|
| 188 | sensors_chip *chip; |
|---|
| 189 | sensors_expr *expr = NULL; |
|---|
| 190 | int i,res; |
|---|
| 191 | |
|---|
| 192 | if (sensors_chip_name_has_wildcards(name)) |
|---|
| 193 | return -SENSORS_ERR_WILDCARDS; |
|---|
| 194 | if (! (featureptr = sensors_lookup_feature_nr(name.prefix,feature))) |
|---|
| 195 | return -SENSORS_ERR_NO_ENTRY; |
|---|
| 196 | if (! (featureptr->mode && SENSORS_W)) |
|---|
| 197 | return -SENSORS_ERR_ACCESS; |
|---|
| 198 | for (chip = NULL; !expr && (chip = sensors_for_all_config_chips(name,chip));) |
|---|
| 199 | for (i = 0; !expr && (i < chip->computes_count); i++) |
|---|
| 200 | if (!strcmp(featureptr->name,chip->computes->name)) |
|---|
| 201 | expr = chip->computes->to_proc; |
|---|
| 202 | if (expr) |
|---|
| 203 | if ((res = sensors_eval_expr(expr,value,&value))) |
|---|
| 204 | return res; |
|---|
| 205 | if (sensors_write_proc(name,feature,value)) |
|---|
| 206 | return -SENSORS_ERR_PROC; |
|---|
| 207 | return 0; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | const sensors_chip_name *sensors_get_detected_chips (int *nr) |
|---|
| 211 | { |
|---|
| 212 | const sensors_chip_name *res; |
|---|
| 213 | res = *nr >= sensors_proc_chips_count?NULL:&sensors_proc_chips[*nr].name; |
|---|
| 214 | (*nr)++; |
|---|
| 215 | return res; |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | const char *sensors_get_adapter_name(int bus_nr) |
|---|
| 219 | { |
|---|
| 220 | int i; |
|---|
| 221 | if (bus_nr == SENSORS_CHIP_NAME_BUS_ISA) |
|---|
| 222 | return "ISA adapter"; |
|---|
| 223 | for (i=0; i < sensors_proc_bus_count; i++) |
|---|
| 224 | if (sensors_proc_bus[i].number == bus_nr) |
|---|
| 225 | return sensors_proc_bus[i].adapter; |
|---|
| 226 | return NULL; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | const char *sensors_get_algorithm_name(int bus_nr) |
|---|
| 230 | { |
|---|
| 231 | int i; |
|---|
| 232 | if (bus_nr == SENSORS_CHIP_NAME_BUS_ISA) |
|---|
| 233 | return "ISA algorithm"; |
|---|
| 234 | for (i=0; i < sensors_proc_bus_count; i++) |
|---|
| 235 | if (sensors_proc_bus[i].number == bus_nr) |
|---|
| 236 | return sensors_proc_bus[i].algorithm; |
|---|
| 237 | return NULL; |
|---|
| 238 | } |
|---|