1/******************************************************************************
2 * $Id: AKFS_FileIO.c 580 2012-03-29 09:56:21Z yamada.rj $
3 ******************************************************************************
4 *
5 * Copyright (C) 2012 Asahi Kasei Microdevices Corporation, Japan
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19#include "AKFS_FileIO.h"
20
21/*** Constant definition ******************************************************/
22#ifdef AKFS_PRECISION_DOUBLE
23#define AKFS_SCANF_FORMAT	"%63s = %lf"
24#else
25#define AKFS_SCANF_FORMAT	"%63s = %f"
26#endif
27#define AKFS_PRINTF_FORMAT	"%s = %f\n"
28#define LOAD_BUF_SIZE	64
29
30/*!
31 Load parameters from file which is specified with #path.  This function reads
32  data from a beginning of the file line by line, and check parameter name
33  sequentially. In otherword, this function depends on the order of eache
34  parameter described in the file.
35 @return If function fails, the return value is #AKM_FAIL. When function fails,
36  the output is undefined. Therefore, parameters which are possibly overwritten
37  by this function should be initialized again. If function succeeds, the
38  return value is #AKM_SUCCESS.
39 @param[out] prms A pointer to #AK8975PRMS structure. Loaded parameter is
40  stored to the member of this structure.
41 @param[in] path A path to the setting file.
42 */
43int16 AKFS_LoadParameters(AK8975PRMS * prms, const char* path)
44{
45	int16 ret;
46	char buf[LOAD_BUF_SIZE];
47	FILE *fp = NULL;
48
49	/* Open setting file for read. */
50	if ((fp = fopen(path, "r")) == NULL) {
51		AKMERROR_STR("fopen");
52		return AKM_FAIL;
53	}
54
55	ret = 1;
56
57	/* Load data to HO */
58	if (fscanf(fp, AKFS_SCANF_FORMAT, buf, &prms->mfv_ho.u.x) != 2) {
59		ret = 0;
60	} else {
61		if (strncmp(buf, "HO.x", sizeof(buf)) != 0) {
62			ret = 0;
63		}
64	}
65	if (fscanf(fp, AKFS_SCANF_FORMAT, buf, &prms->mfv_ho.u.y) != 2) {
66		ret = 0;
67	} else {
68		if (strncmp(buf, "HO.y", sizeof(buf)) != 0) {
69			ret = 0;
70		}
71	}
72	if (fscanf(fp, AKFS_SCANF_FORMAT, buf, &prms->mfv_ho.u.z) != 2) {
73		ret = 0;
74	} else {
75		if (strncmp(buf, "HO.z", sizeof(buf)) != 0) {
76			ret = 0;
77		}
78	}
79
80	if (fclose(fp) != 0) {
81		AKMERROR_STR("fclose");
82		ret = 0;
83	}
84
85	if (ret == 0) {
86		AKMERROR;
87		return AKM_FAIL;
88	}
89
90	return AKM_SUCCESS;
91}
92
93/*!
94 Save parameters to file which is specified with #path. This function saves
95  variables when the offsets of magnetic sensor estimated successfully.
96 @return If function fails, the return value is #AKM_FAIL. When function fails,
97  the parameter file may collapsed. Therefore, the parameters file should be
98  discarded. If function succeeds, the return value is #AKM_SUCCESS.
99 @param[out] prms A pointer to #AK8975PRMS structure. Member variables are
100  saved to the parameter file.
101 @param[in] path A path to the setting file.
102 */
103int16 AKFS_SaveParameters(AK8975PRMS *prms, const char* path)
104{
105	int16 ret = 1;
106	FILE *fp;
107
108	/*Open setting file for write. */
109	if ((fp = fopen(path, "w")) == NULL) {
110		AKMERROR_STR("fopen");
111		return AKM_FAIL;
112	}
113
114	/* Save data to HO */
115	if (fprintf(fp, AKFS_PRINTF_FORMAT, "HO.x", prms->mfv_ho.u.x) < 0) { ret = 0; }
116	if (fprintf(fp, AKFS_PRINTF_FORMAT, "HO.y", prms->mfv_ho.u.y) < 0) { ret = 0; }
117	if (fprintf(fp, AKFS_PRINTF_FORMAT, "HO.z", prms->mfv_ho.u.z) < 0) { ret = 0; }
118
119	if (fclose(fp) != 0) {
120		AKMERROR_STR("fclose");
121		ret = 0;
122	}
123
124	if (ret == 0) {
125		AKMERROR;
126		return AKM_FAIL;
127	}
128
129	return AKM_SUCCESS;
130}
131
132