modules/batt_health/nvm.c
bool BHD_NVM_file_save(bhd_state_t *state)
{
	bool is_success = false;
	int r;
	int fd;
	char path[100];
	const char tmp_path[] = NVM_PATH_ROOT"tmp";

	if (state->is_factory_mode) {
		ALOGE("NVM Write - In factory mode, forbid saving to NVM");
		return is_success;
	}

	r = snprintf(path, sizeof(path), "%s%s_%d",
		NVM_PATH_ROOT, state->eeprom.uid_str, state->file_next_write);
	if (r < 0)
		ALOGE("NVM Write - Failed to create nvm path name");
	else if (r > (int) (sizeof(path) - 1))
		ALOGE("NVM Write - Failed to create nvm path name, too long");
	else {
		/* Write data to temp file first */
		fd = open(tmp_path, O_WRONLY | O_CREAT,
			(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
		if (fd <0)
			ALOGE("NVM Write - Can not open %s, errno = %d (%s)",
				path, errno, strerror(errno));
		else {
			ALOGD("NVM Write - Opened NVM file %s", path);
			is_success = nvm_file_write(fd, state);
			close(fd);
			if (is_success) {
				/* If wrote to temp file successfully, move
				   to final location */
				r = rename(tmp_path, path);
				if (r) {
					is_success = false;
					ALOGE("Rename %s to %s failed, errno = %d (%s)",
						tmp_path, path, errno, strerror(errno));
				} else {
					nvm_file_fsync(path);
					state->file_next_write = (state->file_next_write + 1) %
						NVM_NUM_FILES;
					state->file_write_count++;
					state->last_chg_inc_write = state->bms.charge_increase;
					ALOGD("NVM Write - Successfully updated NVM file");
				}
			}
		}
	}

	return is_success;
}
/* Save battery health state to a file */
Save battery health state to a file
Save battery health state to a file
bool BHD_NVM_file_save(bhd_state_t *state)
{
	bool is_success = false;
	int r;
	int fd;
	char path[100];
	const char tmp_path[] = NVM_PATH_ROOT"tmp";
	if (state->is_factory_mode) {
		ALOGE("NVM Write - In factory mode, forbid saving to NVM");
		return is_success;
	}
	r = snprintf(path, sizeof(path), "%s%s_%d",
		NVM_PATH_ROOT, state->eeprom.uid_str, state->file_next_write);
	if (r < 0)
		ALOGE("NVM Write - Failed to create nvm path name");
	else if (r > (int) (sizeof(path) - 1))
		ALOGE("NVM Write - Failed to create nvm path name, too long");
	else {
		fd = open(tmp_path, O_WRONLY | O_CREAT,
			(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
		if (fd <0)
			ALOGE("NVM Write - Can not open %s, errno = %d (%s)",
				path, errno, strerror(errno));
		else {
			ALOGD("NVM Write - Opened NVM file %s", path);
			is_success = nvm_file_write(fd, state);
			close(fd);
			if (is_success) {
				r = rename(tmp_path, path);
				if (r) {
					is_success = false;
					ALOGE("Rename %s to %s failed, errno = %d (%s)",
						tmp_path, path, errno, strerror(errno));
				} else {
					nvm_file_fsync(path);
					state->file_next_write = (state->file_next_write + 1) %
						NVM_NUM_FILES;
					state->file_write_count++;
					state->last_chg_inc_write = state->bms.charge_increase;
					ALOGD("NVM Write - Successfully updated NVM file");
				}
			}
		}
	}
	return is_success;
}
bool BHD_NVM_file_save ( bhd_state_t * state ) { bool is_success = false ; int r ; int fd ; char path [ 100 ] ; const char tmp_path [ ] = NVM_PATH_ROOT " " ; if ( state -> is_factory_mode ) { ALOGE ( " " ) ; return is_success ; } r = snprintf ( path , sizeof ( path ) , " " , NVM_PATH_ROOT , state -> eeprom . uid_str , state -> file_next_write ) ; if ( r < 0 ) ALOGE ( " " ) ; else if ( r > ( int ) ( sizeof ( path ) - 1 ) ) ALOGE ( " " ) ; else { fd = open ( tmp_path , O_WRONLY | O_CREAT , ( S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) ) ; if ( fd < 0 ) ALOGE ( " " , path , errno , strerror ( errno ) ) ; else { ALOGD ( " " , path ) ; is_success = nvm_file_write ( fd , state ) ; close ( fd ) ; if ( is_success ) { r = rename ( tmp_path , path ) ; if ( r ) { is_success = false ; ALOGE ( " " , tmp_path , path , errno , strerror ( errno ) ) ; } else { nvm_file_fsync ( path ) ; state -> file_next_write = ( state -> file_next_write + 1 ) % NVM_NUM_FILES ; state -> file_write_count ++ ; state -> last_chg_inc_write = state -> bms . charge_increase ; ALOGD ( " " ) ; } } } } return is_success ; }
Save battery health state to a file
Save battery health state to a file
/* Write data to temp file first */ /* If wrote to temp file successfully, move
				   to final location */