Пример #1
0
void sysfs_power_meter::measure()
{
	bool got_rate = false;
	bool got_capacity = false;

	rate = 0.0;
	capacity = 0.0;
	this->set_discharging(false);

	if (!is_present())
		return;
	/** do not jump over. we may have discharging battery */
	if (read_sysfs_string("/sys/class/power_supply/%s/status", name) == "Discharging")
		this->set_discharging(true);

	got_rate = set_rate_from_power();
	got_capacity = set_capacity_from_energy();

	if (!got_rate || !got_capacity) {
		double voltage = get_voltage();
		if (voltage < 0.0)
			return;
		if (!got_rate)
			set_rate_from_current(voltage);
		if (!got_capacity)
			set_capacity_from_charge(voltage);
	}
}
Пример #2
0
int read_acpi_info_sysfs(int battery)
{
	DIR *sysfs;
	struct dirent *propety;
	char *name;

	sysfs = opendir(batteries[battery]);
	if (sysfs == 0)
	{
	#ifdef DEBUG
		  printf("DBG:Can't open %s!\n", batteries[battery]);
	#endif
	    return 0;
	}
	/* malloc.. might explain the random battery level values on 2.6.24 
	systems (energe_full is called charge_full so the value isn't initialised
	and some random data from the heap is displayed..) 
	if (!acpiinfo) acpiinfo=(ACPIinfo *)malloc(sizeof(ACPIinfo)); 
	*/
	if (!acpiinfo) acpiinfo=(ACPIinfo *)calloc(1, sizeof(ACPIinfo));
	
	while ((propety = readdir(sysfs)))
	{
		name = propety->d_name;
		if (!strncmp(".", name, 1) || !strncmp("..", name, 2)) continue;
		/* at least on my system this is called charge_full */
		if ((strcmp(name,"energy_full") == 0) || (strcmp(name,"charge_full") == 0))
		{
			sprintf(buf,"%s/%s",batteries[battery], name);
			acpiinfo->last_full_capacity = read_sysfs_int(buf);
		}
		if ((strcmp(name,"energy_full_design") == 0) || (strcmp(name,"charge_full_design") == 0))
		{
			sprintf(buf,"%s/%s",batteries[battery], name);
			acpiinfo->design_capacity = read_sysfs_int(buf);
		}
		if (strcmp(name,"technology") == 0)
		{
			char *tmp;
			sprintf(buf,"%s/%s",batteries[battery], name);
			tmp = read_sysfs_string(buf);
			if (tmp != NULL)
			{
				if (strcmp(tmp,"Li-ion") == 0)
					acpiinfo->battery_technology = 1;
				else
					acpiinfo->battery_technology = 0;
			}
		}
		if (strcmp(name,"present") == 0)
		{
			sprintf(buf,"%s/%s",batteries[battery], name);
			acpiinfo->present = read_sysfs_int(buf);
		}
	}
	closedir(sysfs);
	return acpiinfo->present;
}
Пример #3
0
cpufreq_tunable::cpufreq_tunable(void) : tunable("", 0.3, _("Good"), _("Bad"), _("Unknown"))
{
	string str;
	sprintf(desc, _("Using 'ondemand' cpufreq governor"));

	str = read_sysfs_string("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
	strcpy(original, str.c_str());
	if (strlen(original) < 1)
		strcpy(original, "ondemand");
}
Пример #4
0
int i2c_tunable::good_bad(void)
{
	string content;

	content = read_sysfs_string(i2c_path);

	if (strcmp(content.c_str(), "auto") == 0)
		return TUNE_GOOD;

	return TUNE_BAD;
}
Пример #5
0
usb_tunable::usb_tunable(const char *path, const char *name) : tunable("", 0.9, _("Good"), _("Bad"), _("Unknown"))
{
	ifstream file;
	char filename[4096];
	char vendor[2048];
	char product[2048];
	string str1, str2;
	sprintf(usb_path, "%s/power/control", path);

	vendor[0] = 0;
	product[0] = 0;

	str1 = read_sysfs_string("%s/idVendor", path);
	str2 = read_sysfs_string("%s/idProduct", path);

	sprintf(desc, _("Autosuspend for unknown USB device %s (%s:%s)"), name, str1.c_str(), str2.c_str());

	sprintf(filename, "%s/manufacturer", path);
	file.open(filename, ios::in);
	if (file) {
		file.getline(vendor, 2047);
		if (strstr(vendor, "Linux "))
			vendor[0] = 0;
		file.close();
	};	
	sprintf(filename, "%s/product", path);
	file.open(filename, ios::in);
	if (file) {
		file.getline(product, 2040);
		file.close();
	};	
	if (strlen(vendor) && strlen(product))
		sprintf(desc, _("Autosuspend for USB device %s [%s]"), product, vendor);
	else if (strlen(product))
		sprintf(desc, _("Autosuspend for USB device %s [%s]"), product, name);
	else if (strlen(vendor))
		sprintf(desc, _("Autosuspend for USB device %s [%s]"), vendor, name);

	sprintf(toggle_good, "echo 'auto' > '%s';", usb_path);
	sprintf(toggle_bad, "echo 'on' > '%s';", usb_path);
}
Пример #6
0
int usb_tunable::good_bad(void)
{
	ifstream file;
	string content;

	content = read_sysfs_string(usb_path);

	if (strcmp(content.c_str(), "auto") == 0)
		return TUNE_GOOD;

	return TUNE_BAD;
}
Пример #7
0
void sysfs_power_meters_callback(const char *d_name)
{
	std::string type = read_sysfs_string("/sys/class/power_supply/%s/type", d_name);

	if (type != "Battery" && type != "UPS")
		return;

	class sysfs_power_meter *meter;
	meter = new(std::nothrow) class sysfs_power_meter(d_name);
	if (meter)
		power_meters.push_back(meter);
}
Пример #8
0
static int get_trace_type(const char *eventname)
{
	string str;
	int this_trace;

	str = read_sysfs_string("/sys/kernel/debug/tracing/events/%s/id",
					eventname);
	if (str.length() < 1)
		return -1;

	this_trace = strtoull(str.c_str(), NULL, 10);
	return this_trace;
}
static int read_sysfs_number(const char *sysfs_name, const char *sysfs_node)
{
    char buf[16];
    int value;

    if (read_sysfs_string(sysfs_name, sysfs_node, buf, sizeof(buf)) < 0)
        return -1;

    if (sscanf(buf, "%d", &value) != 1)
        return -1;

    return value;
}
Пример #10
0
static int dump_one_trigger(const char *dev_dir_name)
{
    char name[IIO_MAX_NAME_LENGTH];
    int dev_idx;
    int retval;

    retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger),
                    "%i", &dev_idx);
    if (retval != 1)
        return -EINVAL;
    retval = read_sysfs_string("name", dev_dir_name, name);
    if (retval)
        return retval;

    printf("Trigger %03d: %s\n", dev_idx, name);
    return 0;
}
Пример #11
0
static int dump_one_device(const char *dev_dir_name)
{
    char name[IIO_MAX_NAME_LENGTH];
    int dev_idx;
    int retval;

    retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device),
                    "%i", &dev_idx);
    if (retval != 1)
        return -EINVAL;
    retval = read_sysfs_string("name", dev_dir_name, name);
    if (retval)
        return retval;

    printf("Device %03d: %s\n", dev_idx, name);

    if (verblevel >= VERBLEVEL_SENSORS)
        return dump_channels(dev_dir_name);
    return 0;
}
Пример #12
0
int read_acpi_state_sysfs(int battery)
{
	DIR *sysfs;
	struct dirent *propety;
	char *name;

	sysfs = opendir(batteries[battery]);
	if (sysfs == 0)
	{
	#ifdef DEBUG
	  printf("DBG:Can't open %s!\n", batteries[battery]);
	#endif
	return 0;
	}
  
	/* again it might be better to use calloc 
	if (!acpistate) acpistate=(ACPIstate *)malloc(sizeof(ACPIstate));
	*/
	if (!acpistate) acpistate=(ACPIstate *)calloc(1, sizeof(ACPIstate));

	while ((propety = readdir(sysfs)))
	{
		name = propety->d_name;
		if (!strncmp(".", name, 1) || !strncmp("..", name, 2)) continue;
		
		if (strcmp(name,"status") == 0)
		{
			char *tmp;
			sprintf(buf,"%s/%s",batteries[battery], name);
			tmp = read_sysfs_string(buf);
			if ( tmp != NULL )
			{
				if (strcmp(tmp,"Charging") == 0)
					acpistate->state = CHARGING;
				else if (strcmp(tmp,"Discharging") == 0)
					acpistate->state = DISCHARGING;
				else if (strcmp(tmp,"Full") == 0)
					acpistate->state = POWER;
				else
					acpistate->state = UNKNOW;
			}
		}
		/* on my system this is called charge_now */
		if ((strcmp(name,"energy_now") == 0) || (strcmp(name,"charge_now") == 0))
		{
			sprintf(buf,"%s/%s",batteries[battery], name);
			acpistate->rcapacity = read_sysfs_int(buf);
			acpistate->percentage = (((float) acpistate->rcapacity)/acpiinfo->last_full_capacity) * 100;
		}
		if ((strcmp(name,"current_now") == 0) || (strcmp(name,"power_now") == 0))
		{
			sprintf(buf,"%s/%s",batteries[battery], name);
			acpistate->prate = read_sysfs_int(buf);
			if ( acpistate->prate < 0 )
				acpistate->prate = 0;
			if ( acpistate->prate > 0 )
				acpistate->rtime = (((float) acpistate->rcapacity) / acpistate->prate) * 60;
		}
		if (strcmp(name,"voltage_now") == 0)
		{
			sprintf(buf,"%s/%s",batteries[battery], name);
			acpistate->pvoltage = read_sysfs_int(buf);
		}
	}
	closedir(sysfs);
	return acpiinfo->present;
}