Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning / server / src / Sensor / VehicleSens_FromAccess.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*******************************************************************************
18  * File name        :VehicleSens_FromAccess.cpp
19  *    System name        :PastModel002
20  *    Subsystem name    :Vehicle sensor process
21  *    Program name    :Functions for accessing vehicle sensor FROM
22  ******************************************************************************/
23
24 #include <vehicle_service/positioning_base_library.h>
25 #include "VehicleSens_FromAccess.h"
26 #include "ClockDataMng.h"
27
28 /********************************************************************************
29  *            Global variable                                                        *
30  ********************************************************************************/
31
32 /********************************************************************************
33  *            prototype declalation                                                  *
34  ********************************************************************************/
35 static RET_API VehicleSensReadNV(NV_DATA_VEHICLESENS *p_nv_data);
36
37 /********************************************************************************
38  *            Definition                                                            *
39  ********************************************************************************/
40 #define VEHICLESENS_FROM_ACCESS_DEBUG 0
41
42 /*******************************************************************************
43 * MODULE    : VehicleSensFromAccessInitialize
44 * ABSTRACT  : Initializing process
45 * FUNCTION  :
46 * ARGUMENT  : None
47 * NOTE      :
48 * RETURN    : None
49 ******************************************************************************/
50 void VehicleSensFromAccessInitialize(void) {
51     /* The result of tag registration for non-volatile memory access is stored in g_nv_access_available, so no confirmation is required. */
52     (void)VehicleSensRegistNvTag();
53
54 }
55
56 /*******************************************************************************
57 * MODULE    : VehicleSensRegistNvTag
58 * ABSTRACT  : Tag registration process
59 * FUNCTION  : Registering Tag Name to Identify Data Storage Destination
60 * ARGUMENT  : None
61 * NOTE      :
62 * RETURN    : RET_NORMAL    :Successful registration
63 *           : RET_ERROR    :Registration failure
64 ******************************************************************************/
65 RET_API VehicleSensRegistNvTag(void) {
66     RET_API            lret = RET_NORMAL;
67     return lret;
68 }
69
70 /*******************************************************************************
71 * MODULE    : VehicleSensReadNV
72 * ABSTRACT  : Get local time Lonlat
73 * FUNCTION  : Reading local time Lonlat from non-volatile memory
74 * ARGUMENT  : NV_DATA_VEHICLESENS * p_nv_data : Pointer to data acquired from non-volatile memory
75 * NOTE      : None
76 * RETURN    : RET_NORMAL    :Read success
77 *           : RET_ERROR    :Failed to read
78 ******************************************************************************/
79 static RET_API VehicleSensReadNV(NV_DATA_VEHICLESENS * p_nv_data) {    // LCOV_EXCL_START 8 : dead code
80     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
81     RET_API                lret = RET_NORMAL;
82     FILE                *fp;
83     NV_DATA_VEHICLESENS    nv_data_tmp;
84     BOOL                ret_read    = FALSE;
85     BOOL                ret_read2    = FALSE;
86
87     u_int32                loop;
88     u_int8                CK_A = 0;
89     u_int8                CK_B = 0;
90     u_int8                *ptr;
91
92     fp = fopen(NV_FILE_VEHICLESENS_TEMP, "rb");
93
94     /* Read File1 */
95     if (fp != NULL) {
96         if ((fread(reinterpret_cast<void *>(p_nv_data), sizeof(u_int8), sizeof(NV_DATA_VEHICLESENS), fp)) == \
97             sizeof(NV_DATA_VEHICLESENS)) {
98             /* Checksum processing */
99             ptr = reinterpret_cast<u_int8 *>(p_nv_data);    /* #QAC confirmation Rule11.4 1Byte accesses for checksums */
100             CK_A = 0;
101             CK_B = 0;
102
103             /* The 2Byte portion from the end of the checksum data is excluded because the checksum storage area. */
104             for (loop = 0; loop < (sizeof(NV_DATA_VEHICLESENS) - 2); loop++) {
105                 CK_A = static_cast<u_int8>(CK_A + ptr[loop]);
106                 CK_B = static_cast<u_int8>(CK_B + CK_A);
107             }
108
109
110             if ((p_nv_data->cka == CK_A) && (p_nv_data->ckb == CK_B)) {
111                 ret_read = TRUE;
112
113 #if VEHICLESENS_FROM_ACCESS_DEBUG
114                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "NV read, status, year, month, day, hour, min, sec, lat, lon, "\
115                     "timediff, update_counter, cka, ckb ");
116                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "NV read, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d ",
117                                  p_nv_data->localtime.status,
118                                  p_nv_data->localtime.year,
119                                  p_nv_data->localtime.month,
120                                  p_nv_data->localtime.day,
121                                  p_nv_data->localtime.hour,
122                                  p_nv_data->localtime.min,
123                                  p_nv_data->localtime.sec,
124                                  p_nv_data->lonlat.latitude,
125                                  p_nv_data->lonlat.longitude,
126                                  p_nv_data->timediff,
127                                  p_nv_data->update_counter,
128                                  p_nv_data->cka,
129                                  p_nv_data->ckb);
130 #endif /* VEHICLESENS_FROM_ACCESS_DEBUG */
131             } else {
132                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Checksum1 failed.");
133                 ret_read = FALSE;
134             }
135         } else {
136             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "fread1 failed.");
137         }
138         fclose(fp);
139     } else {
140         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "fopen1 failed.");
141     }
142
143     /* Read File2 */
144     fp = fopen(NV_FILE_VEHICLESENS2_TEMP, "rb");
145
146     if (fp != NULL) {
147         if ((fread(reinterpret_cast<void *>(&nv_data_tmp), sizeof(u_int8), sizeof(NV_DATA_VEHICLESENS), fp)) == \
148             sizeof(NV_DATA_VEHICLESENS)) {
149             /* Checksum processing */
150             ptr = reinterpret_cast<u_int8 *>(&nv_data_tmp);    /* #QAC confirmation Rule11.4 1Byte accesses for checksums */
151             CK_A = 0;
152             CK_B = 0;
153
154             /* The 2Byte portion from the end of the checksum data is excluded because the checksum storage area. */
155             for (loop = 0; loop < (sizeof(NV_DATA_VEHICLESENS) - 2); loop++) {
156                 CK_A = static_cast<u_int8>(CK_A + ptr[loop]);
157                 CK_B = static_cast<u_int8>(CK_B + CK_A);
158             }
159
160
161             if ((nv_data_tmp.cka == CK_A) && (nv_data_tmp.ckb == CK_B)) {
162                 ret_read2 = TRUE;
163
164 #if VEHICLESENS_FROM_ACCESS_DEBUG
165                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "NV read2, status, year, month, day, hour, min, sec, lat, lon, "\
166                     "timediff, update_counter, cka, ckb ");
167                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "NV read2, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d ",
168                                  nv_data_tmp.localtime.status,
169                                  nv_data_tmp.localtime.year,
170                                  nv_data_tmp.localtime.month,
171                                  nv_data_tmp.localtime.day,
172                                  nv_data_tmp.localtime.hour,
173                                  nv_data_tmp.localtime.min,
174                                  nv_data_tmp.localtime.sec,
175                                  nv_data_tmp.lonlat.latitude,
176                                  nv_data_tmp.lonlat.longitude,
177                                  nv_data_tmp.timediff,
178                                  nv_data_tmp.update_counter,
179                                  nv_data_tmp.cka,
180                                  nv_data_tmp.ckb);
181 #endif /* VEHICLESENS_FROM_ACCESS_DEBUG */
182             } else {
183                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Checksum2 failed.");
184                 ret_read2 = FALSE;
185             }
186         } else {
187             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "fread2 failed.");
188         }
189         fclose(fp);
190     } else {
191         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "fopen2 failed.");
192     }
193
194
195     /* Check the read results of File 1 and File 2. */
196     if ((ret_read == TRUE) && (ret_read2 == TRUE)) {
197         /* Whether File 1 or File 2 is the most recent file,Check with the update count counter */
198         if (p_nv_data->update_counter < nv_data_tmp.update_counter) {
199             (void)memcpy(reinterpret_cast<void *>(p_nv_data), reinterpret_cast<void *>(&nv_data_tmp), \
200                 sizeof(NV_DATA_VEHICLESENS));
201         }
202         lret = RET_NORMAL;
203     } else if (ret_read == TRUE) {
204         /* Use file 1 */
205         lret = RET_NORMAL;
206     } else if (ret_read2 == TRUE) {
207         /* Use file 2 */
208         (void)memcpy(reinterpret_cast<void *>(p_nv_data), reinterpret_cast<void *>(&nv_data_tmp), \
209             sizeof(NV_DATA_VEHICLESENS));
210         lret = RET_NORMAL;
211     } else {
212         /* Read failed for both file 1 and file 2 */
213         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "fread failed.");
214         lret = RET_ERROR;
215     }
216
217     return lret;
218 }
219
220 /*******************************************************************************
221 * MODULE    : VehicleSensReadNVLocalTime
222 * ABSTRACT  : Local time acquisition at shutdown
223 * FUNCTION  : Read local time at shutdown from non-volatile memory
224 * ARGUMENT  : LOCALTIME * local_time : Local time at shutdown
225 * NOTE      : None
226 * RETURN    : RET_NORMAL    :Successful acquisition
227 *           : RET_ERROR    :Failed to acquire
228 ******************************************************************************/
229 RET_API VehicleSensReadNVLocalTime(LOCALTIME * local_time) {
230     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
231     RET_API                lret;
232     NV_DATA_VEHICLESENS    nv_data;
233
234     lret = VehicleSensReadNV(&nv_data);
235
236     if (lret == RET_NORMAL) {
237         (void)memcpy(reinterpret_cast<void *>(local_time), (const void *)(&nv_data.localtime), sizeof(LOCALTIME));
238     }
239
240     return lret;
241 }
242
243 /*******************************************************************************
244 * MODULE    : VehicleSensReadNVLonLat
245 * ABSTRACT  : Obtain position at shutdown
246 * FUNCTION  : Read the shutdown position from the non-volatile memory
247 * ARGUMENT  : LONLAT * lonlat : Shutdown position
248 * NOTE      : None
249 * RETURN    : RET_NORMAL    :Successful acquisition
250 *           : RET_ERROR    :Failed to acquire
251 ******************************************************************************/
252 RET_API VehicleSensReadNVLonLat(LONLAT * lonlat) {
253     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
254     RET_API                lret;
255     NV_DATA_VEHICLESENS    nv_data;
256
257     lret = VehicleSensReadNV(&nv_data);
258
259     if (lret == RET_NORMAL) {
260         (void)memcpy(reinterpret_cast<void *>(lonlat), (const void *)(&nv_data.lonlat), sizeof(LONLAT));
261     }
262
263     return lret;
264 }
265
266 /*******************************************************************************
267 * MODULE    : VehicleSensReadNVTimeDiff
268 * ABSTRACT  : Shutdown time difference acquisition
269 * FUNCTION  : Reading the shutdown time difference from the non-volatile memory
270 * ARGUMENT  : int32 * time_diff : Shutdown position
271 * NOTE      : None
272 * RETURN    : RET_NORMAL    :Successful acquisition
273 *           : RET_ERROR    :Failed to acquire
274 ******************************************************************************/
275 RET_API VehicleSensReadNVTimeDiff(int32 * time_diff) {
276     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
277     RET_API                lret;
278     NV_DATA_VEHICLESENS    nv_data;
279
280     lret = VehicleSensReadNV(&nv_data);
281
282     if (lret == RET_NORMAL) {
283         *time_diff = nv_data.timediff;
284     }
285
286     return lret;
287 }
288
289 /*******************************************************************************
290 * MODULE    : VehicleSensStoreLonlat
291 * ABSTRACT  : Store location data in non-volatile memory
292 * FUNCTION  :
293 * ARGUMENT  : LONLAT * plonlat : Position data
294 * NOTE      :
295 * RETURN    : None
296 ******************************************************************************/
297 void VehicleSensStoreLonlat(LONLAT * plonlat) {
298     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
299
300     return;
301 }
302
303 /*******************************************************************************
304 * MODULE    : VehicleSensWriteNVLocaltime
305 * ABSTRACT  : Local Time Write
306 * FUNCTION  : Write local time
307 * ARGUMENT  : LOCALTIME * local_time : Local time
308 * NOTE      :
309 * RETURN    : RET_NORMAL    :Successful write
310 *           : RET_ERROR    :Writing failure
311 ******************************************************************************/
312 RET_API VehicleSensWriteNVLocaltime(LOCALTIME * local_time, int32 * time_diff) {
313     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
314     RET_API lret = RET_NORMAL;
315
316     return lret;
317 }
318
319 // LCOV_EXCL_STOP