Init basesystem source codes.
[staging/basesystem.git] / vehicleservice / positioning / server / src / Sensor / VehicleUtility.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            VehicleUtility.cpp
19 @detail            Common processing function of Vehicle
20 *****************************************************************************/
21
22 #include "VehicleUtility.h"
23 #include <vehicle_service/positioning_base_library.h>
24
25 #include "gps_hal.h"
26 #include "positioning_common.h"
27
28
29 /*---------------------------------------------------------------------------------*
30  * Grobal Value                                                                    *
31  *---------------------------------------------------------------------------------*/
32 /** Timer management table */
33 static VEHICLEUTILITY_TIM_MNG   g_st_tim_mng;
34
35 /** Timer setting information table */
36 static const VEHICLEUTILITY_TIM_INFO g_tim_info[TIM_NUM] = {
37     /* GSP-related */
38     {TIMVAL_GPS_STARTUP,        PNO_NAVI_GPS_MAIN},   /* Start confirmation monitoring timer                */
39     {TIMVAL_GPS_RCVCYCLDAT,     PNO_NAVI_GPS_MAIN},   /* Periodic reception data monitoring timer           */
40     {TIMVAL_GPS_RCVACK,         PNO_NAVI_GPS_MAIN},   /* ACK reception monitoring timer                */
41     {TIMVAL_GPS_NAVIFST,        PNO_NAVI_GPS_MAIN},   /* Initial Navigation Monitoring Timer                */
42     {TIMVAL_GPS_NAVICYCLE,      PNO_NAVI_GPS_MAIN},   /* Navi monitoring timer                   */
43     {TIMVAL_GPS_NAVIDISRPT,     PNO_NAVI_GPS_MAIN},   /* Navigation Monitoring Disruption Log Output Timer         */
44     {TIMVAL_GPS_DIAGCLKGUARD,   PNO_NAVI_GPS_MAIN},   /* Diag provision time guard monitoring timer       */
45     {TIMVAL_GPS_NMEADATAGUARD,  PNO_NAVI_GPS_MAIN},   /* NMEA data-providing guard monitoring timer     */
46     {TIMVAL_GPS_RECOVERY,       PNO_NAVI_GPS_MAIN},   /* GPS recovery timer                    */
47     {TIMVAL_GPS_RECEIVERERR,    PNO_NAVI_GPS_MAIN},   /* GPS receiver anomaly detection timer          */
48     /* Sensor Related Extensions */
49     {TIMVAL_SNS_RCVFSTDAT,      PNO_VEHICLE_SENSOR},  /* Initial cyclic sensor data reception monitoring timer  */
50     {TIMVAL_SNS_RCVCYCLDAT,     PNO_VEHICLE_SENSOR},  /* Cyclic sensor data reception monitoring timer      */
51     {TIMVAL_SNS_RCVDISRPT,      PNO_VEHICLE_SENSOR},  /* Cyclic sensor data interruption log output timer   */
52 };
53
54 /*---------------------------------------------------------------------------------*
55  * Prototype                                                                       *
56  *---------------------------------------------------------------------------------*/
57 static uint16_t VehicleUtilityTimeMakSeqNo(VEHICLEUTILITY_TIM_KIND tim_kind);
58
59 /***************************************************************************
60 @brief            send message function for Vehicle domain.
61 @outline        send message function with put error diag function if error occurred.
62 @type            Completion return type
63 @param[in]        PNO pno               :    PNO
64 @param[in]        u_int16 size          :    size of message data
65 @param[in]        void* msgbuf          :    message data
66 @param[in]        u_int16 mode          :    mode
67 @threshold
68 @return            RET_API
69 @retval            RET_NORMAL           :    Normal end
70 @retval            RET_ERROR            :    Abnormal end
71 *****************************************************************************/
72 RET_API VehicleUtilitySndMsg(PNO pno, u_int16 size, void *msgbuf, u_int16 mode) {
73     RET_API        ret_api;
74     if (msgbuf != NULL) {  // LCOV_EXCL_BR_LINE 6: msgbuf cannot be null
75         ret_api = _pb_SndMsg(pno, size, msgbuf, mode);
76         /* RET_ERROR: Execute _pb_Exit() after dialog registration */
77         if (ret_api == RET_ERROR) {
78             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SndMsg ERROR [ret_api:%d]", ret_api);
79         }
80     } else {
81         ret_api = RET_ERRPARAM;
82     }
83
84     return ret_api;
85 }
86
87 /***************************************************************************
88 @brief            send message function for Vehicle domain.
89 @outline        receive message function with put error diag function if error occurred.
90 @type            Completion return type
91 @param[in]        PNO pno               :    PNO
92 @param[in]        u_int16 size          :    size of message data
93 @param[in]        void* msgbuf          :    message data
94 @param[in]        u_int16 mode          :    mode
95 @threshold
96 @return            RET_API
97 @retval            RET_NORMAL           :    Normal end
98 @retval            RET_ERROR            :    Abnormal end
99 *****************************************************************************/
100 RET_API VehicleUtilityRcvMsg(PNO pno, u_int16 size, void **msgbuf, u_int16 mode)
101 {
102     RET_API        ret_api;
103
104     if (msgbuf != NULL) {  // LCOV_EXCL_BR_LINE 6: msgbuf cannot be null
105         ret_api = _pb_RcvMsg(pno, size, msgbuf, mode);
106         /* For RET_ERROR Sys_Exit() after dialog registration */
107         if (ret_api == RET_ERROR) {
108             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_RcvMsg ERROR [ret_api:%d]", ret_api);
109         }
110     } else {
111         ret_api = RET_ERRPARAM;
112     }
113
114     return ret_api;
115 }
116
117 /***************************************************************************
118 @brief            Diagcode submit for Vehicle domain.
119 @outline        submit the DiagCode for Vehicle domain.
120 @type            Completion return type
121 @param[in]        u_int32 err_id        :    Diag Code
122 @param[in]        u_int16 positioning_code     :    Positioning Code
123 @param[in]        void* msgbuf          :    message data
124 @return            void
125 *****************************************************************************/
126 void VehicleUtilityDiagCodePut(u_int32 err_id, u_int16 positioning_code) {  // LCOV_EXCL_START 8 : dead code
127     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
128 }
129 // LCOV_EXCL_STOP
130
131 /**
132  * @brief
133  *   Timer function initialization processing
134  *
135  */
136 void VehicleUtilityInitTimer(void) {
137     VEHICLEUTILITY_TIM_MNG *pst_tim_mng;
138     u_int32    i;
139
140     pst_tim_mng = &g_st_tim_mng;
141
142     /* Initialize timer management table */
143     memset(pst_tim_mng, 0x00, sizeof(VEHICLEUTILITY_TIM_MNG));
144
145     for (i = 0; i < TIM_NUM; i++) {
146         pst_tim_mng->sts[i].flag = TIMER_OFF;
147         pst_tim_mng->sts[i].cnt = 0;
148     }
149 }
150
151 /**
152  * @brief
153  *   Timer start processing<br>
154  *
155  *   Starts a timer of the specified type<br>
156  *   1.Increment total number of timer start<br>
157  *   2.Timer Sequence Number Creation<br>
158  *   3.Get timeout value<br>
159  *   4.Timer start<br>
160  *
161  * @param[in]  Tim_kind Timer type
162  *
163  * @return TRUE  Normal completion<br>
164  *         FALSE abend<br>
165  */
166 BOOL VehicleUtilitySetTimer(VEHICLEUTILITY_TIM_KIND tim_kind) {
167     VEHICLEUTILITY_TIM_MNG *pst_tim_mng;
168     const uint32_t *p_time_val;
169     const PNO   *p_pno;
170     RET_API    api_ret;                /* Timer API return value */
171     u_int16    seq_no;
172     BOOL    ret = TRUE;
173
174     pst_tim_mng = &g_st_tim_mng;
175
176     p_time_val = &(g_tim_info[tim_kind].timer_val); /* Timer set value */
177     p_pno = &(g_tim_info[tim_kind].pno); /* Notify party PNO */
178
179     if (pst_tim_mng->sts[tim_kind].flag == TIMER_ON) {
180         /*-----------------------------------------------------------------------*/
181         /* When the same timer has already started                                    */
182         /* Terminate without starting the timer because the timer is set multiple times.            */
183         /*-----------------------------------------------------------------------*/
184         ret = FALSE;
185     } else {
186         /*-----------------------------------------------------------------------*/
187         /* Count up the timer counter of the corresponding timer by 1.                     */
188         /*-----------------------------------------------------------------------*/
189         if (pst_tim_mng->sts[tim_kind].cnt >= TIM_CNTMAX) {
190             /*-----------------------------------------------------------------------*/
191             /* When the count reaches the maximum number,Count again from 1                 */
192             /*-----------------------------------------------------------------------*/
193             pst_tim_mng->sts[tim_kind].cnt = TIM_CNTMIN;
194         } else {
195             /*-----------------------------------------------------------------------*/
196             /* If the count has not reached the maximum,Count up              */
197             /*-----------------------------------------------------------------------*/
198             pst_tim_mng->sts[tim_kind].cnt++;
199         }
200
201         /*-----------------------------------------------------------------------*/
202         /* Creating Timer Sequence Numbers                                            */
203         /*-----------------------------------------------------------------------*/
204         seq_no = VehicleUtilityTimeMakSeqNo(tim_kind);
205
206         /*-----------------------------------------------------------------------*/
207         /* Start the timer                                                      */
208         /*-----------------------------------------------------------------------*/
209         api_ret = _pb_ReqTimerStart(*p_pno, seq_no, TIMER_TYPE_USN, (u_int32)*p_time_val);
210         if (api_ret != RET_NORMAL) {
211             ret = FALSE;
212             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
213                 "_pb_ReqTimerStart ERROR!! [api_ret=%d]", api_ret);
214         } else {
215             /*-----------------------------------------------------------------------*/
216             /* Successful timer start                                            */
217             /* Set the start/stop flag of the corresponding timer to start (MCSUB_ON) */
218             /*-----------------------------------------------------------------------*/
219             pst_tim_mng->sts[tim_kind].flag = TIMER_ON;
220         }
221     }
222
223     return ret;
224 }
225
226 /**
227  * @brief
228  *   Timer stop processing<br>
229  *
230  *   Stops a timer of the specified type<br>
231  *   1.Get the sequence number of the specified type<br>
232  *   2.Timer stop<br>
233  *
234  * @param[in]  Tim_kind Timer type
235  * @param[in]  pno       Process number
236  *
237  * @return TRUE  Normal completion<br>
238  *         FALSE abend<br>
239  */
240 BOOL VehicleUtilityStopTimer(VEHICLEUTILITY_TIM_KIND tim_kind) {
241     VEHICLEUTILITY_TIM_MNG *pst_tim_mng;
242     const PNO   *p_pno;
243     BOOL    ret = TRUE;
244     RET_API    api_ret;
245     u_int16    seq_no;
246
247     pst_tim_mng = &g_st_tim_mng;
248
249     p_pno = &(g_tim_info[tim_kind].pno); /* Notify party PNO */
250
251     /* Check timer start/stop flag    */
252     if (pst_tim_mng->sts[tim_kind].flag == TIMER_OFF) {
253         /*  If it is already stopped, do nothing.    */
254         ret = FALSE;
255     } else {
256         /*-----------------------------------------------------------------------*/
257         /* Creating Timer Sequence Numbers                                            */
258         /*-----------------------------------------------------------------------*/
259         seq_no = VehicleUtilityTimeMakSeqNo(tim_kind);
260
261         /*-----------------------------------------------------------------------*/
262         /* Set the corresponding timer to stop                                          */
263         /*-----------------------------------------------------------------------*/
264         api_ret = _pb_TimerStop(*p_pno, seq_no, TIMER_TYPE_USN);
265         if (api_ret != RET_NORMAL) {
266             ret = FALSE;
267             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_TimerStop ERROR!! [api_ret=%d]", api_ret);
268         }
269
270         /*-----------------------------------------------------------------------*/
271         /* Set the start/stop flag of the corresponding timer to stop (MCSUB_OFF) */
272         /* Set the ID of the corresponding timer to invalid (DEV_TED_INVALID)    */
273         /*-----------------------------------------------------------------------*/
274         pst_tim_mng->sts[tim_kind].flag = TIMER_OFF;
275     }
276     return ret;
277 }
278
279 /**
280  * @brief
281  *   Timer Sequence Number Determination<br>
282  *
283  *   Determine whether the timer sequence number corresponds to the one being managed
284  *
285  * @param[in]  seqno  Timer Sequence Number
286  *
287  * @return TRUE  Normal completion(No problem)<br>
288  *         FALSE abend(Unusual number)<br>
289  */
290 BOOL VehicleUtilityTimeJdgKnd(uint16_t seqno) {  // LCOV_EXCL_START 8 : dead code
291     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
292     VEHICLEUTILITY_TIM_MNG *pst_tim_mng;
293     BOOL ret;
294     u_int8 timekind;
295     u_int8 count;
296
297     pst_tim_mng = &g_st_tim_mng;
298
299     timekind = (u_int8)((seqno & 0xff00) >> 8);
300     count = (u_int8)(seqno & 0x00ff);
301
302     /* Timer type is unexpected        */
303     if (timekind >= TIM_NUM) {
304         ret = FALSE;
305     } else {
306         if ((pst_tim_mng->sts[timekind].cnt == count) &&
307                 (pst_tim_mng->sts[timekind].flag == TIMER_ON)) {
308             /* The counter matches and the counter start/stop flag is "Start".        */
309             ret = TRUE;
310         } else {
311             /* Not applicable due to differences                                    */
312             ret = FALSE;
313         }
314     }
315
316     return ret;
317 }
318 // LCOV_EXCL_STOP
319
320 /**
321  * @brief
322  *   Timer Sequence Number Creation<br>
323  *
324  *   Creating a Sequence Number for a Timer<br>
325  *   1. The upper 1 byte is the timer type.,So that the lower 1 times is the total number of timer starts
326  *     Create a sequence number.
327  *
328  * @param[in]  Tim_kind Timer type
329  *
330  * @return Timer Sequence Number
331  */
332 static uint16_t VehicleUtilityTimeMakSeqNo(VEHICLEUTILITY_TIM_KIND tim_kind) {
333     VEHICLEUTILITY_TIM_MNG *pst_tim_mng;
334     u_int16    seq_no;                    /* Timer Sequence Number */
335
336     pst_tim_mng = &g_st_tim_mng;
337
338     /*------------------------------------------------------------------------*/
339     /* Definition of Sequence Number                                                   */
340     /* |-------------------  Sequence number(2Byte)  -----------------------|  */
341     /* 15                             8 7                                  0  */
342     /* +-------------------------------+-----------------------------------+  */
343     /* |      Timer type(1Byte)        |   Counter(1Byte)(0x01 ? 0xFF)   |  */
344     /* +-------------------------------+-----------------------------------+  */
345     /* For the timer type,0x00 ? (Number of timers-1)                                     */
346     /* For counters,0x01 ? 0xFF(Do not use 0x00.)                             */
347     /* (Counters are counted up each time a timer is started.                  */
348     /*  Count up when counter is 0xFF,                          */
349     /*  Be counted up from the 0x01)                                         */
350     /*------------------------------------------------------------------------*/
351     seq_no = static_cast<u_int16>(((u_int16)tim_kind << 8) | (pst_tim_mng->sts[tim_kind].cnt));
352
353     return seq_no;
354 }
355
356 /**
357  * @brief
358  *   External pin status request
359  */
360 void LineSensDrvExtTermStsReq(void) {
361     T_APIMSG_MSGBUF_HEADER  st_snd_msg;
362     RET_API                 lret;
363
364     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "+");
365
366     memset(&st_snd_msg, 0x00, sizeof(st_snd_msg));
367
368     st_snd_msg.hdr.sndpno      = PNO_LINE_SENS_DRV;
369     st_snd_msg.hdr.respno      = 0x0000;
370     st_snd_msg.hdr.cid         = CID_EXTTERM_REQ;
371     st_snd_msg.hdr.msgbodysize = 0x00; /* No data */
372     st_snd_msg.hdr.rid         = 0x00;
373
374     /* Messaging */
375     lret = _pb_SndMsg(PNO_LINE_SENS_DRV, sizeof(T_APIMSG_MSGBUF_HEADER), &st_snd_msg, 0);
376     if (lret != RET_NORMAL) {
377         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SndMsg ERROR!! lret=%d", lret);
378         lret = RET_ERROR;
379     }
380
381     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "-");
382
383     return;
384 }
385
386
387 /**
388  * @brief
389  *    Backup data read request send processing<BR>
390  *
391  * @return  RET_NORMAL    Normal completion
392  * @return  RET_ERROR     ABENDs
393  */
394 RET_API DEVGpsSndBackupDataLoadReq(void) {
395     RET_API lret;
396     T_APIMSG_MSGBUF_HEADER st_snd_msg;
397
398     /** Create GPS Data Notification Message */
399     (void)memset(&st_snd_msg, 0x00, sizeof(st_snd_msg)); /* QAC 3200 */
400     /** Message header */
401     st_snd_msg.hdr.sndpno = 0x0000;
402     st_snd_msg.hdr.respno = 0x0000;
403     st_snd_msg.hdr.cid    = CID_GPS_BACKUPDATA_LOAD;
404     st_snd_msg.hdr.msgbodysize = 0x00;
405     st_snd_msg.hdr.rid    = 0x00;
406
407     /* Messaging */
408     lret = _pb_SndMsg(PNO_NAVI_GPS_MAIN, sizeof(st_snd_msg), &st_snd_msg, 0);
409     if (lret != RET_NORMAL) {
410         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SndMsg ERROR!! [lret=%d]", lret);
411         lret = RET_ERROR;
412     }
413
414     return(lret);
415 }
416
417 /**
418  * @brief
419  *  Acquisition of GPS-format error count information (dump)
420  *
421  * @param[out]     p_buf    Dump information
422  */
423 void DEVGpsGetDebugGpsFormatFailCnt(void* p_buf) {  // LCOV_EXCL_START 8 : dead code
424     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
425     if (p_buf != NULL) {
426         snprintf(reinterpret_cast<char *>(p_buf), \
427             512, "GPS Format Fail Count is not supported.\n");  // NOLINT(readability/nolint)
428     }
429     return;
430 }
431 // LCOV_EXCL_STOP
432
433 /**
434  * @brief
435  *   Initial sensor data reception flag acquisition
436  */
437 u_int8 LineSensDrvGetSysRecvFlag(void) {  // LCOV_EXCL_START 7: uesd only in FrameworkunifiedOnDebugDump
438     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
439     return 0;
440 }
441 // LCOV_EXCL_STOP
442
443 /**
444  * @brief
445  *   Get GPS Rollover Standard Week Number
446  *
447  * @return  GPS rollover base week number
448  */
449 uint16_t DEVGpsGetWknRollover(void) {  // LCOV_EXCL_START 7: uesd only in FrameworkunifiedOnDebugDump
450     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
451     return 0;
452 }
453 // LCOV_EXCL_STOP
454
455 /* end of file */