modules/batt_health/nvm.c
bool BHD_NVM_file_load(bhd_state_t *state)
{
	bool is_success = false;
	bool read_success;
	bhd_state_t tmp_state[NVM_NUM_FILES];
	int r;
	int fd;
	char path[100];
	int i;
	int index_to_use = -1;
	unsigned long long int max_counter = 0;
	nvm_file_hdr_t hdr;
	int status;

	/* Attempt to read all battery health files associated with battery ID */
	state->file_write_count = 0;
	for (i = 0; i < NVM_NUM_FILES; i++) {
		read_success = false;

		/* Copy current state into temporary state */
		memcpy(&tmp_state[i], state, sizeof(*state));

		r = snprintf(path, sizeof(path), "%s%s_%d",
			NVM_PATH_ROOT, state->eeprom.uid_str, i);
		if (r < 0)
			ALOGE("NVM Read - Failed to create nvm path name");
		else if (r > (int) (sizeof(path) - 1))
			ALOGE("NVM Read - Failed to create nvm path name, too long");
		else {
			fd = open(path, O_RDONLY);
			if (fd < 0)
				ALOGE("NVM Read - Can not open %s, errno = %d (%s)",
					path, errno, strerror(errno));
			else {
				ALOGD("NVM Read - Opened NVM file %s", path);
				r = read(fd, &hdr, sizeof(hdr));
				if (r != sizeof(hdr))
				     ALOGE("Failed to read header, r = %d, errno = %d (%s)",
				                r, errno, strerror(errno));
				else if (hdr.version == NVM_FILE_HEADER_VERSION_0) {
					close(fd);
					status = remove(path);
					if (status == 0 ) {
						ALOGD("File deletion with version0 success %s",
									path);
					}
					else
						ALOGE("Unable to delete file %s\n",path);
					continue;
				}
				else {
						if(lseek(fd,0L,SEEK_SET) < 0)
							ALOGE("lseek failed \n");
						else
							read_success = nvm_file_read(fd, &tmp_state[i]);
				}
				close(fd);
			}
		}

		/* If the file was read successfully, determine if it is the latest
		   data file based on file write counter */
		if (read_success && (tmp_state[i].file_write_count >= max_counter)) {
			index_to_use = i;
			max_counter = tmp_state[i].file_write_count;
		}
	}

	/* If we found a valid file, set the current state to the temp state
	   associated with the valid file */
	if (index_to_use != -1) {
		ALOGD("NVM Load - Found newest file, index to use = %d", index_to_use);
		is_success = true;
		memcpy(state, &tmp_state[index_to_use], sizeof(*state));

		/* Range check the values that were read */
		nvm_file_bms_data_purify(state);
		state->last_chg_inc_write = state->bms.charge_increase;
		state->file_next_write = (index_to_use + 1) % NVM_NUM_FILES;
		state->file_write_count++;
	}

	return is_success;
}
/* Loads latest battery health file from NVM */
Loads latest battery health file from NVM
Loads latest battery health file from NVM
bool BHD_NVM_file_load(bhd_state_t *state)
{
	bool is_success = false;
	bool read_success;
	bhd_state_t tmp_state[NVM_NUM_FILES];
	int r;
	int fd;
	char path[100];
	int i;
	int index_to_use = -1;
	unsigned long long int max_counter = 0;
	nvm_file_hdr_t hdr;
	int status;
	state->file_write_count = 0;
	for (i = 0; i < NVM_NUM_FILES; i++) {
		read_success = false;
		memcpy(&tmp_state[i], state, sizeof(*state));
		r = snprintf(path, sizeof(path), "%s%s_%d",
			NVM_PATH_ROOT, state->eeprom.uid_str, i);
		if (r < 0)
			ALOGE("NVM Read - Failed to create nvm path name");
		else if (r > (int) (sizeof(path) - 1))
			ALOGE("NVM Read - Failed to create nvm path name, too long");
		else {
			fd = open(path, O_RDONLY);
			if (fd < 0)
				ALOGE("NVM Read - Can not open %s, errno = %d (%s)",
					path, errno, strerror(errno));
			else {
				ALOGD("NVM Read - Opened NVM file %s", path);
				r = read(fd, &hdr, sizeof(hdr));
				if (r != sizeof(hdr))
				     ALOGE("Failed to read header, r = %d, errno = %d (%s)",
				                r, errno, strerror(errno));
				else if (hdr.version == NVM_FILE_HEADER_VERSION_0) {
					close(fd);
					status = remove(path);
					if (status == 0 ) {
						ALOGD("File deletion with version0 success %s",
									path);
					}
					else
						ALOGE("Unable to delete file %s\n",path);
					continue;
				}
				else {
						if(lseek(fd,0L,SEEK_SET) < 0)
							ALOGE("lseek failed \n");
						else
							read_success = nvm_file_read(fd, &tmp_state[i]);
				}
				close(fd);
			}
		}
		if (read_success && (tmp_state[i].file_write_count >= max_counter)) {
			index_to_use = i;
			max_counter = tmp_state[i].file_write_count;
		}
	}
	if (index_to_use != -1) {
		ALOGD("NVM Load - Found newest file, index to use = %d", index_to_use);
		is_success = true;
		memcpy(state, &tmp_state[index_to_use], sizeof(*state));
		nvm_file_bms_data_purify(state);
		state->last_chg_inc_write = state->bms.charge_increase;
		state->file_next_write = (index_to_use + 1) % NVM_NUM_FILES;
		state->file_write_count++;
	}
	return is_success;
}
bool BHD_NVM_file_load ( bhd_state_t * state ) { bool is_success = false ; bool read_success ; bhd_state_t tmp_state [ NVM_NUM_FILES ] ; int r ; int fd ; char path [ 100 ] ; int i ; int index_to_use = -1 ; unsigned long long int max_counter = 0 ; nvm_file_hdr_t hdr ; int status ; state -> file_write_count = 0 ; for ( i = 0 ; i < NVM_NUM_FILES ; i ++ ) { read_success = false ; memcpy ( & tmp_state [ i ] , state , sizeof ( * state ) ) ; r = snprintf ( path , sizeof ( path ) , " " , NVM_PATH_ROOT , state -> eeprom . uid_str , i ) ; if ( r < 0 ) ALOGE ( " " ) ; else if ( r > ( int ) ( sizeof ( path ) - 1 ) ) ALOGE ( " " ) ; else { fd = open ( path , O_RDONLY ) ; if ( fd < 0 ) ALOGE ( " " , path , errno , strerror ( errno ) ) ; else { ALOGD ( " " , path ) ; r = read ( fd , & hdr , sizeof ( hdr ) ) ; if ( r != sizeof ( hdr ) ) ALOGE ( " " , r , errno , strerror ( errno ) ) ; else if ( hdr . version == NVM_FILE_HEADER_VERSION_0 ) { close ( fd ) ; status = remove ( path ) ; if ( status == 0 ) { ALOGD ( " " , path ) ; } else ALOGE ( " \n " , path ) ; continue ; } else { if ( lseek ( fd , 0L , SEEK_SET ) < 0 ) ALOGE ( " \n " ) ; else read_success = nvm_file_read ( fd , & tmp_state [ i ] ) ; } close ( fd ) ; } } if ( read_success && ( tmp_state [ i ] . file_write_count >= max_counter ) ) { index_to_use = i ; max_counter = tmp_state [ i ] . file_write_count ; } } if ( index_to_use != -1 ) { ALOGD ( " " , index_to_use ) ; is_success = true ; memcpy ( state , & tmp_state [ index_to_use ] , sizeof ( * state ) ) ; nvm_file_bms_data_purify ( state ) ; state -> last_chg_inc_write = state -> bms . charge_increase ; state -> file_next_write = ( index_to_use + 1 ) % NVM_NUM_FILES ; state -> file_write_count ++ ; } return is_success ; }
Loads latest battery health file from NVM
Loads latest battery health file from NVM
/* Attempt to read all battery health files associated with battery ID */ /* Copy current state into temporary state */ /* If the file was read successfully, determine if it is the latest
		   data file based on file write counter */ /* If we found a valid file, set the current state to the temp state
	   associated with the valid file */ /* Range check the values that were read */