Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning / server / src / Sensor / VehicleSens_SharedMemory.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_SharedMemory.cpp
19  *  System name    :PastModel002
20  *  Subsystem name  :Vehicle sensor process
21  *  Program name  :Vehicle sensor shared memory management
22  ******************************************************************************/
23
24 #include <vehicle_service/positioning_base_library.h>
25 #include "VehicleSens_SharedMemory.h"
26 #include "Sensor_API.h"
27 #include "VehicleSens_DataMaster.h"
28 #include "Sensor_API_private.h"
29 #include "SensorLocation_API.h"
30 #include "SensorLocation_API_private.h"
31
32 /********************************************************************************
33  *      prototype declalation                          *
34  ********************************************************************************/
35 static void VehicleSensLinkSharedMemory(char *shared_memory_name, void **p_share_addr);
36 static RET_API VehicleSensWriteDataGpsInterruptSignal(DID ul_did);
37 static RET_API VehicleSensWriteDataGyroConnectStatus(DID ul_did);
38 static RET_API VehicleSensWriteDataLocalTime(void);
39 static RET_API VehicleSensWriteDataLonLat(void);
40
41 /********************************************************************************
42  *      Definition                              *
43  ********************************************************************************/
44
45 /*******************************************************************************
46 * MODULE    : VehicleSensInitSharedMemory
47 * ABSTRACT  : Shared Memory Initialization
48 * FUNCTION  : Initialize shared memory
49 * ARGUMENT  : None
50 * NOTE      :
51 * RETURN    : RET_NORMAL  :Normal
52 *           : RET_ERROR  :Abnormality
53 ******************************************************************************/
54 RET_API  VehicleSensInitSharedMemory(void) {  // LCOV_EXCL_START 8 : dead code
55     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
56     RET_API  l_ret;  /* Return of the functions */
57
58     /* All shared memory initialization */
59     l_ret = VehicleSensWriteDataGpsInterruptSignal(VEHICLE_DID_MAIN_GPS_INTERRUPT_SIGNAL);
60     if (l_ret == RET_NORMAL) {
61         l_ret = VehicleSensWriteDataGpsInterruptSignal(VEHICLE_DID_SYS_GPS_INTERRUPT_SIGNAL);
62     }
63     if (l_ret == RET_NORMAL) {
64         l_ret = VehicleSensWriteDataGyroConnectStatus(VEHICLE_DID_GYRO_CONNECT_STATUS);
65     }
66
67     /* Initializes the effective ephemeris count when the shared memory is shut down. */
68     if (l_ret == RET_NORMAL) {
69         l_ret = VehicleSensWriteDataValidEphemerisNum(0);  /* Initialized with effective ephemeris number 0 */
70     }
71
72     /* Writes the value read from the non-volatile memory to the shared memory. */
73     /* This process is executed only at startup.,After that, the shared memory will not be overwritten. */
74     if (l_ret == RET_NORMAL) {
75         l_ret = VehicleSensWriteDataLocalTime();
76     }
77
78     if (l_ret == RET_NORMAL) {
79         l_ret = VehicleSensWriteDataLonLat();
80     }
81
82     return l_ret;
83 }
84
85 /*******************************************************************************
86 * MODULE    : VehicleSensLinkSharedMemory
87 * ABSTRACT  : Shared memory link
88 * FUNCTION  : Link to shared memory
89 * ARGUMENT  :
90 * NOTE      :
91 * RETURN    : None
92 ******************************************************************************/
93 static void VehicleSensLinkSharedMemory(char *shared_memory_name, void **p_share_addr) {
94     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
95     RET_API  ret_api;
96     void  *pv_share_mem;  /* Store Shared Memory Address */
97     u_int32  ul_share_mem_size;  /* Size of the linked shared memory */
98
99     /* Link to the handle storage area */
100     ret_api = _pb_LinkShareData(shared_memory_name, &pv_share_mem, &ul_share_mem_size);
101
102     if (ret_api == RET_NORMAL) {
103         /* If the link is successful */
104         *p_share_addr = pv_share_mem;  /* Set the address */
105     } else {
106         /* If the link fails */
107         *p_share_addr = NULL;
108     }
109 }
110
111 /*******************************************************************************
112 * MODULE    : VehicleSensWriteDataGpsInterruptSignal
113 * ABSTRACT  : Writing of data
114 * FUNCTION  : Writing Data to Shared Memory
115 * ARGUMENT  : DID : Data ID
116 * NOTE      :
117 * RETURN    : RET_NORMAL  :Normal
118 *           : RET_ERROR  :Abnormality
119 ******************************************************************************/
120 static RET_API VehicleSensWriteDataGpsInterruptSignal(DID ul_did) {
121     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
122     static GPS_INTERRUPT  *gpsInterruptSharedAddr = NULL;  /* Store Shared Memory Address */
123     static SemID      sem_id = 0;            /* ID of shared memory exclusive semaphore */
124
125     VEHICLESENS_DATA_MASTER_GPS_INTERRUPT_SIGNAL  pst_data;
126
127     RET_API l_ret = RET_NORMAL;  /* Return of the functions */
128     RET_API l_ret_api;       /* Return of the functions */
129
130
131     /* Get Semaphore ID */
132     if (sem_id == 0) {
133         sem_id = _pb_CreateSemaphore(const_cast<char *>(GPS_INTERRUPT_SIGNAL_SEMAPHO_NAME));
134     }
135
136     if (sem_id != 0) {
137         /* Semaphore ID successfully acquired */
138         l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */
139
140         if (l_ret_api == RET_NORMAL) {
141             /* Semaphore lock successful */
142
143             /* When the shared memory is not linked */
144             if (gpsInterruptSharedAddr == NULL) {
145                 /* Link to shared memory */
146                 /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
147                 VehicleSensLinkSharedMemory(const_cast<char *>(GPS_INTERRUPT_SIGNAL_SHARE_NAME),
148                                               reinterpret_cast<void **>(&gpsInterruptSharedAddr));
149             }
150
151             if (gpsInterruptSharedAddr != NULL) {
152                 /* The link to shared memory is successful. */
153                 switch (ul_did) {
154                     case VEHICLE_DID_MAIN_GPS_INTERRUPT_SIGNAL:
155                     {
156                         /* Get data from data master */
157                         VehicleSensGetDataMasterMainGpsInterruptSignal(ul_did, 0, &pst_data);
158
159                         /* Writing Data to Shared Memory */
160                         gpsInterruptSharedAddr->_CWORD102__interrupt = pst_data.uc_data;
161                         break;
162                     }
163                     case VEHICLE_DID_SYS_GPS_INTERRUPT_SIGNAL:
164                     {
165                         /* Get data from data master */
166                         VehicleSensGetDataMasterSysGpsInterruptSignal(ul_did, 0, &pst_data);
167
168                         /* Writing Data to Shared Memory */
169                         gpsInterruptSharedAddr->_CWORD56__interrupt = pst_data.uc_data;
170                         break;
171                     }
172                     default:
173                         l_ret = RET_ERROR;
174                         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "DID is unknown. \r\n");
175                         break;
176                 }
177
178             } else {
179                 l_ret = RET_ERROR;
180                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "gpsInterruptSharedAddr == NULL \r\n");
181             }
182
183             /* Semaphore unlock */
184             (void)_pb_SemUnlock(sem_id);
185         } else {
186             l_ret = RET_ERROR;
187             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
188         }
189     } else {
190         l_ret = RET_ERROR;
191         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
192     }
193
194     return l_ret;
195 }
196
197 /*******************************************************************************
198 * MODULE    : VehicleSensWriteDataGyroConnectStatus
199 * ABSTRACT  : Writing of data
200 * FUNCTION  : Writing Data to Shared Memory
201 * ARGUMENT  : DID : Data ID
202 * NOTE      :
203 * RETURN    : RET_NORMAL  :Normal
204 *           : RET_ERROR  :Abnormality
205 ******************************************************************************/
206 static RET_API VehicleSensWriteDataGyroConnectStatus(DID ul_did) {
207     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
208     static u_int8    *gyroConnectSharedAddr = NULL;  /* Store Shared Memory Address */
209     static SemID    sem_id = 0;            /* ID of shared memory exclusive semaphore */
210
211     VEHICLESENS_DATA_MASTER_GYRO_CONNECT_STATUS    pst_data;
212
213     RET_API l_ret = RET_NORMAL;  /* Return of the functions */
214     RET_API l_ret_api;       /* Return of the functions */
215
216     /* Get Semaphore ID */
217     if (sem_id == 0) {
218         sem_id = _pb_CreateSemaphore(const_cast<char *>(GYRO_CONNECT_STATUS_SEMAPHO_NAME));
219     }
220
221     if (sem_id != 0) {
222         /* Semaphore ID successfully acquired */
223         l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */
224
225         if (l_ret_api == RET_NORMAL) {
226             /* Semaphore lock successful */
227
228             /* When the shared memory is not linked */
229             if (gyroConnectSharedAddr == NULL) {
230                 /* Link to shared memory */
231                 /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
232                 VehicleSensLinkSharedMemory(const_cast<char *>(GYRO_CONNECT_STATUS_SHARE_NAME),
233                                               reinterpret_cast<void **>(&gyroConnectSharedAddr));
234             }
235
236             if (gyroConnectSharedAddr != NULL) {
237                 /* The link to shared memory is successful. */
238
239                 /* Get data from data master */
240                 VehicleSensGetDataMasterGyroConnectStatus(ul_did, 0, &pst_data);
241
242                 /* Writing Data to Shared Memory */
243                 *gyroConnectSharedAddr = pst_data.uc_data;
244
245             } else {
246                 l_ret = RET_ERROR;
247                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
248                     "gyroConnectSharedAddr == NULL \r\n");
249             }
250
251             /* Semaphore unlock */
252             (void)_pb_SemUnlock(sem_id);
253         } else {
254             l_ret = RET_ERROR;
255             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
256         }
257     } else {
258         l_ret = RET_ERROR;
259         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
260     }
261
262     return l_ret;
263 }
264
265 /*******************************************************************************
266 * MODULE    : VehicleSensWriteDataValidEphemerisNum
267 * ABSTRACT  : Write effective ephemeris number at shutdown
268 * FUNCTION  : Write effective ephemeris number at shutdown to shared memory
269 * ARGUMENT  : u_int8 valid_ephemer_isnum : Number of effective ephemeris written to shared memory during shutdown
270 * NOTE      :
271 * RETURN    : RET_NORMAL  :Normal
272 *           : RET_ERROR  :Abnormality
273 ******************************************************************************/
274 RET_API VehicleSensWriteDataValidEphemerisNum(u_int8 valid_ephemer_isnum) {
275     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
276     static u_int8  *shared_addr = NULL;  /* Store Shared Memory Address */
277     static SemID  sem_id = 0;      /* ID of shared memory exclusive semaphore */
278
279     RET_API l_ret;    /* Return of the functions */
280     RET_API l_ret_api;   /* Return of the functions */
281
282     /* Get Semaphore ID */
283     if (sem_id == 0) {
284         sem_id = _pb_CreateSemaphore(const_cast<char *>(EPHEMERIS_NUM_SEMAPHO_NAME));
285     }
286
287     if (sem_id != 0) {
288         /* Semaphore ID successfully acquired */
289         l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */
290
291         if (l_ret_api == RET_NORMAL) {
292             /* Semaphore lock successful */
293
294             /* When the shared memory is not linked */
295             if (shared_addr == NULL) {
296                 /* Link to shared memory */
297                 /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
298                 VehicleSensLinkSharedMemory(const_cast<char *>(EPHEMERIS_NUM_SHARE_NAME),
299                                              reinterpret_cast<void **>(&shared_addr));
300             }
301
302             if (shared_addr != NULL) {
303                 *shared_addr = valid_ephemer_isnum;
304                 l_ret = RET_NORMAL;
305
306             } else {
307                 l_ret = RET_ERROR;
308                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "shared_addr == NULL \r\n");
309             }
310
311             /* Semaphore unlock */
312             l_ret_api = _pb_SemUnlock(sem_id);
313             if (l_ret_api != RET_NORMAL) {
314                 /* Semaphore unlock failure */
315                 l_ret = RET_ERROR;
316                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemUnlock failed");
317             }
318         } else {
319             /* Semaphore ID acquisition failure */
320             l_ret = RET_ERROR;
321             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
322         }
323     } else {
324         l_ret = RET_ERROR;
325         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
326     }
327
328     return l_ret;
329 }
330
331 /*******************************************************************************
332 * MODULE    : VehicleSensWriteDataLocalTime
333 * ABSTRACT  : Writing Local Time at Shutdown
334 * FUNCTION  : Write local time on shutdown to shared memory
335 * ARGUMENT  : None
336 * NOTE      :
337 * RETURN    : RET_NORMAL  :Acquisition normal
338 *           : RET_ERROR  :Acquisition anomaly
339 ******************************************************************************/
340 static RET_API VehicleSensWriteDataLocalTime(void) {
341     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
342     static LOCALTIME  *shared_addr = NULL;  /* Store Shared Memory Address */
343     static SemID    sem_id = 0;      /* ID of shared memory exclusive semaphore */
344
345     RET_API l_ret;    /* Return of the functions */
346     RET_API l_ret_api;   /* Return of the functions */
347
348     LOCALTIME LocalTime;
349
350     /* Get Semaphore ID */
351     if (sem_id == 0) {
352         sem_id = _pb_CreateSemaphore(const_cast<char *>(LOCALTIME_SEMAPHO_NAME));
353     }
354
355     if (sem_id != 0) {
356         /* Semaphore ID successfully acquired */
357         l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */
358
359         if (l_ret_api == RET_NORMAL) {
360             /* Semaphore lock successful */
361
362             /* When the shared memory is not linked */
363             if (shared_addr == NULL) {
364                 /* Link to shared memory */
365                 /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
366                 VehicleSensLinkSharedMemory(const_cast<char *>(LOCALTIME_SHARE_NAME),
367                                               reinterpret_cast<void **>(&shared_addr));
368             }
369
370             if (shared_addr != NULL) {
371                 /* The link to shared memory is successful. */
372
373                 /* Acquires data from the non-volatile memory and writes it to the shared memory. */
374                 l_ret_api = VehicleSensReadNVLocalTime(&LocalTime);
375                 if (l_ret_api == RET_NORMAL) {
376                     *shared_addr = LocalTime;
377                     l_ret = RET_NORMAL;
378                 } else {
379                     /* When data acquisition from non-volatile memory fails,Set an invalid value */
380                     (*shared_addr).status  = CLOCK_INVALID;
381                     (*shared_addr).year  = 0xFFFFU; /* invalid */
382                     (*shared_addr).month  = 255U; /* invalid */
383                     (*shared_addr).day    = 255U; /* invalid */
384                     (*shared_addr).hour  = 255U; /* invalid */
385                     (*shared_addr).min    = 255U; /* invalid */
386                     (*shared_addr).sec    = 255U; /* invalid */
387                     l_ret = RET_NORMAL;
388                 }
389
390             } else {
391                 l_ret = RET_ERROR;
392                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "shared_addr == NULL \r\n");
393             }
394
395             /* Semaphore unlock */
396             l_ret_api = _pb_SemUnlock(sem_id);
397             if (l_ret_api != RET_NORMAL) {
398                 /* Semaphore unlock failure */
399                 l_ret = RET_ERROR;
400                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemUnlock failed");
401             }
402         } else {
403             /* Semaphore ID acquisition failure */
404             l_ret = RET_ERROR;
405             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
406         }
407     } else {
408         l_ret = RET_ERROR;
409         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
410     }
411
412     return l_ret;
413 }
414
415 /*******************************************************************************
416 * MODULE    : VehicleSensWriteDataLonLat
417 * ABSTRACT  : Write position at shutdown
418 * FUNCTION  : Write shutdown position to shared memory
419 * ARGUMENT  : None
420 * NOTE      :
421 * RETURN    : RET_NORMAL  :Successful acquisition
422 *           : RET_ERROR  :Failed to acquire
423 ******************************************************************************/
424 static RET_API VehicleSensWriteDataLonLat(void) {
425     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
426     static LONLAT  *shared_addr = NULL;  /* Store Shared Memory Address */
427     static SemID  sem_id = 0;      /* ID of shared memory exclusive semaphore */
428
429     RET_API l_ret;    /* Return of the functions */
430     RET_API l_ret_api;   /* Return of the functions */
431
432     LONLAT lonlat;    /* Position */
433
434     /* Get Semaphore ID */
435     if (sem_id == 0) {
436         sem_id = _pb_CreateSemaphore(const_cast<char *>(LONLAT_SEMAPHO_NAME));
437     }
438
439     if (sem_id != 0) {
440         /* Semaphore ID successfully acquired */
441         l_ret_api = _pb_SemLock(sem_id);  /* Semaphore Lock */
442
443         if (l_ret_api == RET_NORMAL) {
444             /* Semaphore lock successful */
445
446             /* When the shared memory is not linked */
447             if (shared_addr == NULL) {
448                 /* Link to shared memory */
449                 /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
450                 VehicleSensLinkSharedMemory(const_cast<char *>(LONLAT_SHARE_NAME),
451                                               reinterpret_cast<void **>(&shared_addr));
452             }
453
454             if (shared_addr != NULL) {
455                 /* The link to shared memory is successful. */
456
457                 /* Acquires data from the non-volatile memory and writes it to the shared memory. */
458                 l_ret_api = VehicleSensReadNVLonLat(&lonlat);
459                 if (l_ret_api == RET_NORMAL) {
460                     *shared_addr = lonlat;
461                     l_ret = RET_NORMAL;
462                 } else {
463                     /* When data acquisition from non-volatile memory fails */
464                     (*shared_addr).latitude  = SENSORLOCATION_LATITUDE_INIT_VALUE;
465                     (*shared_addr).longitude  = SENSORLOCATION_LONGITUDE_INIT_VALUE;
466                     l_ret = RET_NORMAL;
467                 }
468
469             } else {
470                 l_ret = RET_ERROR;
471                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "shared_addr == NULL \r\n");
472             }
473
474             /* Semaphore unlock */
475             l_ret_api = _pb_SemUnlock(sem_id);
476             if (l_ret_api != RET_NORMAL) {
477                 /* Semaphore unlock failure */
478                 l_ret = RET_ERROR;
479                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemUnlock failed");
480             }
481         } else {
482             /* Semaphore ID acquisition failure */
483             l_ret = RET_ERROR;
484             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
485         }
486     } else {
487         l_ret = RET_ERROR;
488         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "sem_id == 0");
489     }
490
491     return l_ret;
492 }
493
494 /*******************************************************************************
495 * MODULE    : VehicleSensWriteSharedMemory
496 * ABSTRACT  : Write Shared Memory
497 * FUNCTION  : Write Shared Memory
498 * ARGUMENT  : DID : Data ID
499 * RETURN    : None
500 * NOTE      :
501 ******************************************************************************/
502 void VehicleSensWriteSharedMemory(DID ul_did) {
503     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
504     switch (ul_did) {
505         case VEHICLE_DID_MAIN_GPS_INTERRUPT_SIGNAL:
506         case VEHICLE_DID_SYS_GPS_INTERRUPT_SIGNAL:
507         {
508             (void)VehicleSensWriteDataGpsInterruptSignal(ul_did);
509             break;
510         }
511         case VEHICLE_DID_GYRO_CONNECT_STATUS:
512         {
513             (void)VehicleSensWriteDataGyroConnectStatus(ul_did);
514             break;
515         }
516         default:
517             FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "DID is unknown. \r\n");
518             break;
519     }
520 }
521 // LCOV_EXCL_STOP