summaryrefslogtreecommitdiffstats
path: root/libsensors_iio/software/core/mllite/linux/ml_stored_data.c
diff options
context:
space:
mode:
Diffstat (limited to 'libsensors_iio/software/core/mllite/linux/ml_stored_data.c')
-rw-r--r--libsensors_iio/software/core/mllite/linux/ml_stored_data.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/libsensors_iio/software/core/mllite/linux/ml_stored_data.c b/libsensors_iio/software/core/mllite/linux/ml_stored_data.c
index c5cf2e6..24b3217 100644
--- a/libsensors_iio/software/core/mllite/linux/ml_stored_data.c
+++ b/libsensors_iio/software/core/mllite/linux/ml_stored_data.c
@@ -38,26 +38,40 @@
#define STORECAL_LOG MPL_LOGI
#define LOADCAL_LOG MPL_LOGI
-inv_error_t inv_read_cal(unsigned char *cal, size_t len)
+inv_error_t inv_read_cal(unsigned char **calData, size_t *bytesRead)
{
FILE *fp;
- int bytesRead;
inv_error_t result = INV_SUCCESS;
+ size_t fsize;
fp = fopen(MLCAL_FILE,"rb");
if (fp == NULL) {
MPL_LOGE("Cannot open file \"%s\" for read\n", MLCAL_FILE);
return INV_ERROR_FILE_OPEN;
}
- bytesRead = fread(cal, 1, len, fp);
- if (bytesRead != len) {
- MPL_LOGE("bytes read (%d) don't match requested length (%d)\n",
- bytesRead, len);
+
+ // obtain file size
+ fseek (fp, 0 , SEEK_END);
+ fsize = ftell (fp);
+ rewind (fp);
+
+ *calData = (unsigned char *)inv_malloc(fsize);
+ if (*calData==NULL) {
+ MPL_LOGE("Could not allocate buffer of %d bytes - "
+ "aborting\n", fsize);
+ fclose(fp);
+ return INV_ERROR_MEMORY_EXAUSTED;
+ }
+
+ *bytesRead = fread(*calData, 1, fsize, fp);
+ if (*bytesRead != fsize) {
+ MPL_LOGE("bytes read (%d) don't match file size (%d)\n",
+ *bytesRead, fsize);
result = INV_ERROR_FILE_READ;
goto read_cal_end;
}
else {
- MPL_LOGI("Bytes read = %d", bytesRead);
+ MPL_LOGI("Bytes read = %d", *bytesRead);
}
read_cal_end:
@@ -261,31 +275,18 @@ inv_error_t inv_store_cal(unsigned char *calData, size_t length)
*/
inv_error_t inv_load_calibration(void)
{
- unsigned char *calData;
+ unsigned char *calData= NULL;
inv_error_t result = 0;
- size_t length;
-
- inv_get_mpl_state_size(&length);
- if (length <= 0) {
- MPL_LOGE("Could not get file calibration length - "
- "error %d - aborting\n", result);
- return result;
- }
+ size_t bytesRead = 0;
- calData = (unsigned char *)inv_malloc(length);
- if (!calData) {
- MPL_LOGE("Could not allocate buffer of %d bytes - "
- "aborting\n", length);
- return INV_ERROR_MEMORY_EXAUSTED;
- }
-
- result = inv_read_cal(calData, length);
+ result = inv_read_cal(&calData, &bytesRead);
if(result != INV_SUCCESS) {
MPL_LOGE("Could not load cal file - "
"aborting\n");
+ goto free_mem_n_exit;
}
- result = inv_load_mpl_states(calData, length);
+ result = inv_load_mpl_states(calData, bytesRead);
if (result != INV_SUCCESS) {
MPL_LOGE("Could not load the calibration data - "
"error %d - aborting\n", result);
@@ -294,7 +295,7 @@ inv_error_t inv_load_calibration(void)
free_mem_n_exit:
inv_free(calData);
- return INV_SUCCESS;
+ return result;
}
/**