modules/batt_health/sys.c
static void sys_battery_state_read(bhd_state_t *state)
{
	int is_charging;
	int chrg_lvl;
	int percent_delta;
	float cc_delta;
	float capacity_mah;
	float capacity_percent_float;
	int capacity_percent_int;

	sys_bms_param_read(state);

	is_charging = sys_is_battery_charging();
	if (is_charging == 1) {
		if (!state->is_charging) {
			chrg_lvl = sys_battery_charge_level_get();
			ALOGD("Battery start charging @ %d%%!", chrg_lvl);
			state->is_charging = true;
		}
	} else if (is_charging == 0) {
		if (state->is_charging) {
			bool eoc_ok;
			bhd_eoc_params_t eoc_tmp;

			chrg_lvl = sys_battery_charge_level_get();
			ALOGD("Battery stop charging @ %d%%", chrg_lvl);
			/* Read the end of charge parameters */
			eoc_ok = sys_eoc_param_read(&eoc_tmp);
			if ( (eoc_ok == true) &&
				(eoc_tmp.boc_percent >= 0) &&
				(eoc_tmp.boc_percent <= 5) &&
				(sys_is_battery_full() == 1) &&
				(eoc_tmp.eoc_percent == 100) ) {
				ALOGD("Criteria met to calculate new aged capacity!");
				memcpy(&state->eoc, &eoc_tmp, sizeof(eoc_tmp));

				percent_delta = 100 - (100 - state->eoc.eoc_percent) -
					(state->eoc.boc_percent + state->eoc.bms_offset);
				if (percent_delta < 1)
					percent_delta = 1;
				cc_delta = (float) (abs(state->eoc.eoc_cc_uah - state->eoc.boc_cc_uah)
						/ 1000);
				capacity_mah = (cc_delta / percent_delta) * 100;
				capacity_percent_float = (capacity_mah / state->eeprom.capacity)
					* 100;
				capacity_percent_float += 2.5;
				capacity_percent_int = (int) capacity_percent_float;
				if (capacity_percent_int > 120)
					capacity_percent_int = 120;
				else if (capacity_percent_int < 1)
					capacity_percent_int = 1;
				state->aged_capacity = capacity_percent_int;
				state->eoc.aged_timestamp = time(NULL);

				ALOGD("Start %% = %d, start cc = %d, end %% = %d, end cc = %d, offset = %d",
					state->eoc.boc_percent, state->eoc.boc_cc_uah,
					state->eoc.eoc_percent, state->eoc.eoc_cc_uah,
					state->eoc.bms_offset);
				ALOGD("Percent delta = %d, cc delta = %f, capacity mah = %f",
					percent_delta, cc_delta, capacity_mah);
				ALOGD("New aged capacity = %d%%, calculated @ %s",
					state->aged_capacity, ctime(&state->eoc.aged_timestamp));
				sys_bms_aged_capacity_write(state, 1, 0);
				sys_bms_aged_timestamp_write(state, 1, 0);
			}

			state->is_charging = false;
		}
	}

}
/* Read sysfs to determine battery state */
Read sysfs to determine battery state
Read sysfs to determine battery state
static void sys_battery_state_read(bhd_state_t *state)
{
	int is_charging;
	int chrg_lvl;
	int percent_delta;
	float cc_delta;
	float capacity_mah;
	float capacity_percent_float;
	int capacity_percent_int;
	sys_bms_param_read(state);
	is_charging = sys_is_battery_charging();
	if (is_charging == 1) {
		if (!state->is_charging) {
			chrg_lvl = sys_battery_charge_level_get();
			ALOGD("Battery start charging @ %d%%!", chrg_lvl);
			state->is_charging = true;
		}
	} else if (is_charging == 0) {
		if (state->is_charging) {
			bool eoc_ok;
			bhd_eoc_params_t eoc_tmp;
			chrg_lvl = sys_battery_charge_level_get();
			ALOGD("Battery stop charging @ %d%%", chrg_lvl);
			eoc_ok = sys_eoc_param_read(&eoc_tmp);
			if ( (eoc_ok == true) &&
				(eoc_tmp.boc_percent >= 0) &&
				(eoc_tmp.boc_percent <= 5) &&
				(sys_is_battery_full() == 1) &&
				(eoc_tmp.eoc_percent == 100) ) {
				ALOGD("Criteria met to calculate new aged capacity!");
				memcpy(&state->eoc, &eoc_tmp, sizeof(eoc_tmp));
				percent_delta = 100 - (100 - state->eoc.eoc_percent) -
					(state->eoc.boc_percent + state->eoc.bms_offset);
				if (percent_delta < 1)
					percent_delta = 1;
				cc_delta = (float) (abs(state->eoc.eoc_cc_uah - state->eoc.boc_cc_uah)
						/ 1000);
				capacity_mah = (cc_delta / percent_delta) * 100;
				capacity_percent_float = (capacity_mah / state->eeprom.capacity)
					* 100;
				capacity_percent_float += 2.5;
				capacity_percent_int = (int) capacity_percent_float;
				if (capacity_percent_int > 120)
					capacity_percent_int = 120;
				else if (capacity_percent_int < 1)
					capacity_percent_int = 1;
				state->aged_capacity = capacity_percent_int;
				state->eoc.aged_timestamp = time(NULL);
				ALOGD("Start %% = %d, start cc = %d, end %% = %d, end cc = %d, offset = %d",
					state->eoc.boc_percent, state->eoc.boc_cc_uah,
					state->eoc.eoc_percent, state->eoc.eoc_cc_uah,
					state->eoc.bms_offset);
				ALOGD("Percent delta = %d, cc delta = %f, capacity mah = %f",
					percent_delta, cc_delta, capacity_mah);
				ALOGD("New aged capacity = %d%%, calculated @ %s",
					state->aged_capacity, ctime(&state->eoc.aged_timestamp));
				sys_bms_aged_capacity_write(state, 1, 0);
				sys_bms_aged_timestamp_write(state, 1, 0);
			}
			state->is_charging = false;
		}
	}
}
static void sys_battery_state_read ( bhd_state_t * state ) { int is_charging ; int chrg_lvl ; int percent_delta ; float cc_delta ; float capacity_mah ; float capacity_percent_float ; int capacity_percent_int ; sys_bms_param_read ( state ) ; is_charging = sys_is_battery_charging ( ) ; if ( is_charging == 1 ) { if ( ! state -> is_charging ) { chrg_lvl = sys_battery_charge_level_get ( ) ; ALOGD ( " " , chrg_lvl ) ; state -> is_charging = true ; } } else if ( is_charging == 0 ) { if ( state -> is_charging ) { bool eoc_ok ; bhd_eoc_params_t eoc_tmp ; chrg_lvl = sys_battery_charge_level_get ( ) ; ALOGD ( " " , chrg_lvl ) ; eoc_ok = sys_eoc_param_read ( & eoc_tmp ) ; if ( ( eoc_ok == true ) && ( eoc_tmp . boc_percent >= 0 ) && ( eoc_tmp . boc_percent <= 5 ) && ( sys_is_battery_full ( ) == 1 ) && ( eoc_tmp . eoc_percent == 100 ) ) { ALOGD ( " " ) ; memcpy ( & state -> eoc , & eoc_tmp , sizeof ( eoc_tmp ) ) ; percent_delta = 100 - ( 100 - state -> eoc . eoc_percent ) - ( state -> eoc . boc_percent + state -> eoc . bms_offset ) ; if ( percent_delta < 1 ) percent_delta = 1 ; cc_delta = ( float ) ( abs ( state -> eoc . eoc_cc_uah - state -> eoc . boc_cc_uah ) / 1000 ) ; capacity_mah = ( cc_delta / percent_delta ) * 100 ; capacity_percent_float = ( capacity_mah / state -> eeprom . capacity ) * 100 ; capacity_percent_float += 2.5 ; capacity_percent_int = ( int ) capacity_percent_float ; if ( capacity_percent_int > 120 ) capacity_percent_int = 120 ; else if ( capacity_percent_int < 1 ) capacity_percent_int = 1 ; state -> aged_capacity = capacity_percent_int ; state -> eoc . aged_timestamp = time ( NULL ) ; ALOGD ( " " , state -> eoc . boc_percent , state -> eoc . boc_cc_uah , state -> eoc . eoc_percent , state -> eoc . eoc_cc_uah , state -> eoc . bms_offset ) ; ALOGD ( " " , percent_delta , cc_delta , capacity_mah ) ; ALOGD ( " " , state -> aged_capacity , ctime ( & state -> eoc . aged_timestamp ) ) ; sys_bms_aged_capacity_write ( state , 1 , 0 ) ; sys_bms_aged_timestamp_write ( state , 1 , 0 ) ; } state -> is_charging = false ; } } }
Read sysfs to determine battery state
Read sysfs to determine battery state
/* Read the end of charge parameters */