+static double get_value(int id, int type)
+{
+ int att;
+
+ if (type & SENSOR_TYPE_TEMP) {
+ if (type & SENSOR_TYPE_AMBIENT)
+ att = NV_CTRL_AMBIENT_TEMPERATURE;
+ else
+ att = NV_CTRL_GPU_CORE_TEMPERATURE;
+
+ return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
+ } else if (type & SENSOR_TYPE_FAN) {
+ if (type & SENSOR_TYPE_RPM)
+ return get_att(NV_CTRL_TARGET_TYPE_COOLER,
+ id,
+ NV_CTRL_THERMAL_COOLER_SPEED);
+ else /* SENSOR_TYPE_PERCENT */
+ return get_att(NV_CTRL_TARGET_TYPE_COOLER,
+ id,
+ NV_CTRL_THERMAL_COOLER_LEVEL);
+ } else { /* SENSOR_TYPE_PERCENT */
+ return get_usage(id, type);
+ }
+}
+
+static void update(struct psensor *sensor)
+{
+ double v;
+
+ v = get_value(sensor->nvidia_id, sensor->type);
+
+ if (v == UNKNOWN_DBL_VALUE)
+ log_err(_("%s: Failed to retrieve measure of type %x "
+ "for NVIDIA GPU %d"),
+ PROVIDER_NAME,
+ sensor->type,
+ sensor->nvidia_id);
+ psensor_set_current_value(sensor, v);
+}
+
+static int check_sensor(int id, int type)
+{
+ return get_value(id, type) != UNKNOWN_DBL_VALUE;
+}
+
+static char *i2str(int i)
+{
+ char *str;
+ size_t n;
+
+ /* second +1 to avoid issue about the conversion of a double
+ * to a lower int */
+ n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
+
+ str = malloc(n);
+ snprintf(str, n, "%d", i);
+
+ return str;
+}
+
+static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
+{
+ char *pname, *name, *strnid, *sid;
+ const char *stype;
+ int type;
+ size_t n;
+ struct psensor *s;
+
+ type = SENSOR_TYPE_NVCTRL | subtype;
+
+ if (!check_sensor(id, type))
+ return NULL;
+
+ pname = get_product_name(id, type);
+ strnid = i2str(id);
+ stype = get_nvidia_type_str(type);
+
+ n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
+ name = malloc(n);
+ sprintf(name, "%s %s %s", pname, strnid, stype);
+
+ sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
+ sprintf(sid, "%s %s", PROVIDER_NAME, name);
+
+ s = psensor_create(sid, name, pname, type, value_len);
+ s->nvidia_id = id;
+
+ free(strnid);
+
+ return s;
+}