Init basesystem source codes.
[staging/basesystem.git] / vehicleservice / positioning / client / src / POS_sensor_API / Sensor_API.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        :Sensor_API.cpp
19  *    System name        :GPF
20  *    Subsystem name    :Sensor I/F library
21  *    Program name    :SensorI/F API
22  ******************************************************************************/
23
24 #include <stdio.h>
25 #include <vehicle_service/POS_sensor_API.h>
26 #include <vehicle_service/POS_define.h>
27 #include <vehicle_service/POS_sensor_API.h>
28 #include <vehicle_service/positioning_base_library.h>
29 #include "POS_sensor_private.h"
30 #include "Sensor_Common_API.h"
31 #include "Sensor_API_private.h"
32 #include "Sensor_Common_API.h"
33 #include "Vehicle_API_Dummy.h"
34 #include "Vehicle_API_private.h"
35 #include "Naviinfo_API.h"
36 #include "POS_private.h"
37
38 /*************************************************/
39 /*           Global variable                      */
40 /*************************************************/
41 /********************************************************************************
42  * TAG      :TG_GPS_REQ_RESET
43  * ABSTRACT :GPS reset request
44  * NOTE     :I/F information between host applications(Reset mode)
45  ********************************************************************************/
46 /**
47  * @brief    POS_RegisterListenerPkgSensData Return code list
48  */
49 static const SENSOR_RET_PKG g_ret_list_reg_lis_pkg_sens_data[SENSOR_PUBLIC_DID_NUM] = {
50     /* GRADE2   GRADE1     DID (Key)     */
51     {TRUE,      TRUE,     POS_DID_SPEED_PULSE        },
52     {FALSE,     TRUE,     POS_DID_SPEED_KMPH         },
53     {TRUE,      TRUE,     POS_DID_GYRO_X             },
54     {TRUE,      TRUE,     POS_DID_GYRO_Y             },
55     {TRUE,      TRUE,     POS_DID_GYRO_Z             },
56     {TRUE,      TRUE,     POS_DID_GSNS_X             },
57     {TRUE,      TRUE,     POS_DID_GSNS_Y             },
58     {TRUE,      TRUE,     POS_DID_GSNS_Z             },
59     {FALSE,     TRUE,     POS_DID_GPS_ANTENNA        },
60     {TRUE,      TRUE,     POS_DID_SNS_COUNTER        },
61     {FALSE,     TRUE,     POS_DID_SPEED_PULSE_FST    },
62     {FALSE,     TRUE,     POS_DID_GYRO_X_FST         },
63     {FALSE,     TRUE,     POS_DID_GYRO_Y_FST         },
64     {FALSE,     TRUE,     POS_DID_GYRO_Z_FST         },
65     {FALSE,     TRUE,     POS_DID_GPS__CWORD82__NMEA },
66     {FALSE,     TRUE,     POS_DID_GPS__CWORD82___CWORD44_GP4   },
67     {FALSE,     TRUE,     POS_DID_GPS__CWORD82__FULLBINARY },
68     {FALSE,     TRUE,     POS_DID_GPS_NMEA           },
69     {TRUE,      TRUE,     POS_DID_REV                },
70     {FALSE,     TRUE,     POS_DID_REV_FST            },
71     {TRUE,      TRUE,     POS_DID_GYRO_TEMP          },
72     {FALSE,     TRUE,     POS_DID_GYRO_TEMP_FST      },
73     {FALSE,     TRUE,     POS_DID_GSNS_X_FST         },
74     {FALSE,     TRUE,     POS_DID_GSNS_Y_FST         },
75     {FALSE,     TRUE,     POS_DID_GSNS_Z_FST         },
76     {TRUE,      TRUE,     POS_DID_PULSE_TIME         },
77     {FALSE,     TRUE,     POS_DID_GPS_CLOCK_DRIFT    },
78     {FALSE,     TRUE,     POS_DID_GPS_CLOCK_FREQ     },
79     {FALSE,     TRUE,     VEHICLE_DID_GPS_TIME       }, /* For local use */
80     {FALSE,     TRUE,     VEHICLE_DID_GPS_TIME_RAW   }, /* For local use */
81     {FALSE,     TRUE,     VEHICLE_DID_GPS_WKNROLLOVER}  /* For local use */
82 };
83
84 /**
85  * @brief    POS_RegisterListenerSensData Return code list
86  */
87 static const SENSOR_RET_PKG g_ret_list_reg_lis_sens_data[SENSOR_PUBLIC_DID_NUM] = {
88     /* GRADE2   GRADE1     DID (Key)     */
89     {TRUE,      TRUE,      POS_DID_SPEED_PULSE        },
90     {FALSE,     TRUE,      POS_DID_SPEED_KMPH         },
91     {TRUE,      TRUE,      POS_DID_GYRO_X             },
92     {TRUE,      TRUE,      POS_DID_GYRO_Y             },
93     {TRUE,      TRUE,      POS_DID_GYRO_Z             },
94     {TRUE,      TRUE,      POS_DID_GSNS_X             },
95     {TRUE,      TRUE,      POS_DID_GSNS_Y             },
96     {TRUE,      TRUE,      POS_DID_GSNS_Z             },
97     {TRUE,      TRUE,      POS_DID_GPS_ANTENNA        },
98     {FALSE,     TRUE,      POS_DID_SNS_COUNTER        },
99     {TRUE,      TRUE,      POS_DID_SPEED_PULSE_FST    },
100     {TRUE,      TRUE,      POS_DID_GYRO_X_FST         },
101     {TRUE,      TRUE,      POS_DID_GYRO_Y_FST         },
102     {TRUE,      TRUE,      POS_DID_GYRO_Z_FST         },
103     {TRUE,      TRUE,      POS_DID_GPS__CWORD82__NMEA },
104     {FALSE,     TRUE,      POS_DID_GPS__CWORD82___CWORD44_GP4   },
105     {TRUE,      TRUE,      POS_DID_GPS__CWORD82__FULLBINARY },
106     {FALSE,     TRUE,      POS_DID_GPS_NMEA           },
107     {FALSE,     TRUE,      POS_DID_REV                },
108     {TRUE,      TRUE,      POS_DID_REV_FST            },
109     {TRUE,      TRUE,      POS_DID_GYRO_TEMP          },
110     {TRUE,      TRUE,      POS_DID_GYRO_TEMP_FST      },
111     {TRUE,      TRUE,      POS_DID_GSNS_X_FST         },
112     {TRUE,      TRUE,      POS_DID_GSNS_Y_FST         },
113     {TRUE,      TRUE,      POS_DID_GSNS_Z_FST         },
114     {FALSE,     TRUE,      POS_DID_PULSE_TIME         },
115     {FALSE,     TRUE,      POS_DID_GPS_CLOCK_DRIFT    },
116     {FALSE,     TRUE,      POS_DID_GPS_CLOCK_FREQ     },
117     {FALSE,     TRUE,      VEHICLE_DID_GPS_TIME           }, /* For local use */
118     {FALSE,     TRUE,      VEHICLE_DID_GPS_TIME_RAW       }, /* For local use */
119     {FALSE,     TRUE,      VEHICLE_DID_GPS_WKNROLLOVER    }  /* For local use */
120 };
121
122 /**
123  * @brief    POS_GetSensData Return code list
124  */
125 static const SENSOR_RET_PKG g_ret_list_get_sens_data[SENSOR_PUBLIC_DID_NUM] = {
126     /* GRADE2   GRADE1     DID (Key)     */
127     {TRUE,      TRUE,      POS_DID_SPEED_PULSE        },
128     {FALSE,     TRUE,      POS_DID_SPEED_KMPH         },
129     {TRUE,      TRUE,      POS_DID_GYRO_X             },
130     {TRUE,      TRUE,      POS_DID_GYRO_Y             },
131     {TRUE,      TRUE,      POS_DID_GYRO_Z             },
132     {TRUE,      TRUE,      POS_DID_GSNS_X             },
133     {TRUE,      TRUE,      POS_DID_GSNS_Y             },
134     {TRUE,      TRUE,      POS_DID_GSNS_Z             },
135     {TRUE,      TRUE,      POS_DID_GPS_ANTENNA        },
136     {FALSE,     TRUE,      POS_DID_SNS_COUNTER        },
137     {FALSE,     TRUE,      POS_DID_SPEED_PULSE_FST    },
138     {FALSE,     TRUE,      POS_DID_GYRO_X_FST         },
139     {FALSE,     TRUE,      POS_DID_GYRO_Y_FST         },
140     {FALSE,     TRUE,      POS_DID_GYRO_Z_FST         },
141     {TRUE,      TRUE,      POS_DID_GPS__CWORD82__NMEA       },
142     {FALSE,     TRUE,      POS_DID_GPS__CWORD82___CWORD44_GP4   },
143     {TRUE,      TRUE,      POS_DID_GPS__CWORD82__FULLBINARY },
144     {FALSE,     TRUE,      POS_DID_GPS_NMEA           },
145     {FALSE,     TRUE,      POS_DID_REV                },
146     {FALSE,     TRUE,      POS_DID_REV_FST            },
147     {TRUE,      TRUE,      POS_DID_GYRO_TEMP          },
148     {FALSE,     TRUE,      POS_DID_GYRO_TEMP_FST      },
149     {FALSE,     TRUE,      POS_DID_GSNS_X_FST         },
150     {FALSE,     TRUE,      POS_DID_GSNS_Y_FST         },
151     {FALSE,     TRUE,      POS_DID_GSNS_Z_FST         },
152     {FALSE,     TRUE,      POS_DID_PULSE_TIME         },
153     {FALSE,     TRUE,      POS_DID_GPS_CLOCK_DRIFT    },
154     {FALSE,     TRUE,      POS_DID_GPS_CLOCK_FREQ     },
155     {FALSE,     TRUE,      VEHICLE_DID_GPS_TIME           }, /* For local use */
156     {FALSE,     TRUE,      VEHICLE_DID_GPS_TIME_RAW       }, /* For local use */
157     {FALSE,     TRUE,      VEHICLE_DID_GPS_WKNROLLOVER    }  /* For local use */
158 };
159
160 /**
161  * @brief
162  *   DID-decision functions for Sensor API
163  *
164  *    Determines whether or not the DID specified by the public API corresponds to the DID.
165  *
166  * @param[in]     did    Data ID
167  * @param[in]    mode    Operation mode 1:For Package Delivery Registration API<br>
168  *                                 2:For Sensor Data Delivery Registration API<br>
169  *                                 3:For Sensor Data Acquisition API
170  *
171  * @return    TRUE    Be supported
172  *             FALSE    Not supported
173  */
174 BOOL SensorJudgeDid(DID did, uint8_t mode) {
175     BOOL                    ret = FALSE;
176     UNIT_TYPE               type;
177     const SENSOR_RET_PKG    *pkg_list = NULL;
178     int32_t i;
179
180     /* Set Return pakage list */
181     switch (mode) {
182         case MODE_REGISTER_LISTENER_PKG_SENS_DATA: /* POS_RegisterListenerPkgSensData */
183         {
184             pkg_list = g_ret_list_reg_lis_pkg_sens_data;
185             break;
186         }
187         case MODE_REGISTER_LISTENER_SENSDATA: /* POS_RegisterListenerSensData */
188         {
189             pkg_list = g_ret_list_reg_lis_sens_data;
190             break;
191         }
192         case MODE_GET_SENSDATA: /* POS_GetSensData */
193         {
194             pkg_list = g_ret_list_get_sens_data;
195             break;
196         }
197         default:
198             /* Error log */
199             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR [mode = %d]", mode);
200             break;
201     }
202
203     if (pkg_list != NULL) {
204         /* Search Return code list for DID */
205         for (i = 0; i < SENSOR_PUBLIC_DID_NUM; i++) {
206             if (did == pkg_list[i].did) {
207                 break;
208             }
209         }
210
211         if (i != SENSOR_PUBLIC_DID_NUM) {
212             /* Get Unit type */
213             type = GetEnvSupportInfo();
214             switch (type) {
215                 case UNIT_TYPE_GRADE1:
216                 {
217                     ret = pkg_list[i].GRADE1_ret;
218                     break;
219                 }
220                 case UNIT_TYPE_GRADE2:
221                 {
222                     /*
223                      *  Note.
224                      *  This feature branches processing depending on the unit type.
225                      */
226                     ret = pkg_list[i].GRADE2_ret;
227                     break;
228                 }
229                 default:
230                     /* Error log */
231                     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
232                                   "GetEnvSupportInfo ERROR [type = %d]", type);
233                     break;
234             }
235         } else {
236             /* Error log */
237             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR [did = %d]", did);
238         }
239     }
240
241     return ret;
242 }
243
244 /**
245  * @brief
246  *    Vehicle Sensor Information Extended Package Delivery Registration
247  *  Sensor information is delivered for the first time.,Extension Packaging and Registering for Delivery.
248  *
249  * @param[in]  hApp                    Application handle
250  * @param[in]  notifyName            Destination thread name
251  * @param[in]  ucPkgNum                Number of package data(1 to 16)
252  * @param[in]  *pulDid                Pointer to an array of data IDs for vehicle information
253  * @param[in]  ucCtrlFlg            Delivery control<br>
254  *                Delivery registration: SENSOR_DELIVERY_REGIST<br>
255  *                Delivery stop: SENSOR_DELIVERY_STOP        (Note: Not mounted)<br>
256  *                Resume delivery: SENSOR_DELIVERY_RESTART    (Note: Not mounted)
257  * @param[in]  ucDeliveryTiming        Delivery timing<br>
258  *                Updating    : SENSOR_DELIVERY_TIMING_UPDATE<br>
259  *                Changing    : SENSOR_DELIVERY_TIMING_CHANGE
260  *
261  * @return    SENSOR_RET_NORMAL                Successful registration<br>
262  *            SENSOR_RET_ERROR_CREATE_EVENT    Event generation failure<br>
263  *            SENSOR_RET_ERROR_PARAM            Parameter error<br>
264  *            SENSOR_RET_ERROR_DID            Unregistered ID<br>
265  *            SENSOR_RET_ERROR_BUFFULL        FULL of delivery registers<br>
266  *            SENSOR_RET_ERROR_NOSUPPORT        Unsupported environment<br>
267  *            SENSOR_RET_ERROR_INNER            Internal abnormality
268  *
269  */
270 SENSOR_RET_API POS_RegisterListenerPkgSensData(HANDLE hApp,
271                                                PCSTR notifyName,
272                                                uint8_t ucPkgNum,
273                                                DID *pulDid, uint8_t ucCtrlFlg, uint8_t ucDeliveryTiming) {
274     SENSOR_RET_API                   ret;            /* Return value */
275     BOOL                             ret_b;
276     RET_API                          ret_api;      /* System API return value */
277     EventID                          event_id;     /* Event ID */
278     int32_t                          event_val;    /* Event value */
279     SENSOR_MSG_DELIVERY_ENTRY_DAT    data;         /* Message data */
280     int32_t                          i;            /* Generic counters */
281     PNO                              ch_pno;       /* Converted internal PNO    */
282     UNIT_TYPE                        type;         /* Supported HW Configuration Type    */
283
284     /* Internal debug log output */
285     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "+");
286
287     ret = SENSOR_RET_NORMAL;
288     /* Check Handle */
289     if (hApp == NULL) {
290         /* NULL terminates with an abnormal parameter */
291         ret = SENSOR_RET_ERROR_PARAM;
292     }
293
294     if (ret == SENSOR_RET_NORMAL) {
295         /* Positioning Base API initialization */
296         _pb_Setup_CWORD64_API(hApp);
297
298         /* Supported HW Configuration Check */
299         type = GetEnvSupportInfo();
300         if (UNIT_TYPE_GRADE1 == type) {
301             /* GRADE1 */
302             ret = SENSOR_RET_NORMAL;
303         } else if (UNIT_TYPE_GRADE2 == type) {
304           /*
305            *  Note.
306            *  This feature branches processing depending on the unit type.
307            */
308             ret = SENSOR_RET_ERROR_NOSUPPORT;
309         } else {
310             /* Environment error */
311             ret = SENSOR_RET_ERROR_NOSUPPORT;
312         }
313     }
314
315     if (ret == SENSOR_RET_NORMAL) {
316         /* Check Delivery Control Designation */
317         if (SENSOR_DELIVERY_REGIST != ucCtrlFlg) {
318             /* Parameters other than delivery registration terminated abnormally. */
319             ret = SENSOR_RET_ERROR_PARAM;
320         } else if ((ucDeliveryTiming != SENSOR_DELIVERY_TIMING_CHANGE) &&
321                    (ucDeliveryTiming != SENSOR_DELIVERY_TIMING_UPDATE)) {
322             /* Check delivery timing */
323             /* Change delivery timing,Terminate as a parameter error except update */
324             ret = SENSOR_RET_ERROR_PARAM;
325         } else if (notifyName == NULL) {
326             /* Check Thread Name */
327             /* NULL terminates with an abnormal parameter */
328             ret = SENSOR_RET_ERROR_PARAM;
329         } else if ((0 == ucPkgNum) || (SENSOR_PKG_DELIVERY_MAX < ucPkgNum)) {
330             /* Check Package Data Count */
331             /* Out-of-range is terminated as a parameter error. */
332             ret = SENSOR_RET_ERROR_PARAM;
333         } else if (pulDid == NULL) {
334             /* Check Data ID */
335             /* NULL terminates with an abnormal parameter */
336             ret = SENSOR_RET_ERROR_PARAM;
337         } else {
338             /* Check if data ID is acceptable */
339             for (i = 0; i < ucPkgNum; i++) {
340                 /* Judge DID */
341                 ret_b = SENSOR_DID_JUDGE_REGLIS_PKG(pulDid[i]);
342                 if (ret_b == FALSE) {
343                     /* An unacceptable ID terminates with an abnormal parameter. */
344                     ret = SENSOR_RET_ERROR_PARAM;
345                     break;
346                 } else {
347                     ret = SENSOR_RET_NORMAL;
348                 }
349             }
350         }
351     }
352
353     if (ret == SENSOR_RET_NORMAL) {
354         /* Resource acquisition */
355         if (VehicleGetResource() == TRUE) {
356
357         /* Initialization */
358         event_id = 0;
359         event_val = 0;
360         memset(reinterpret_cast<void *>(&data), 0, sizeof(SENSOR_MSG_DELIVERY_ENTRY_DAT));
361
362         /* Get PNO from Thread Name */
363         ch_pno = _pb_CnvName2Pno(notifyName);
364
365         /* Event Generation */
366         event_id = PosCreateEvent(ch_pno);
367
368         if (0 != event_id) {
369             /* Successful event generation */
370
371             /*--------------------------------------------------------------*
372              *    Send Vehicle Sensor Information Delivery Registration Message                    *
373              *--------------------------------------------------------------*/
374             /* Create Message Data */
375             data.pno             = ch_pno;
376             data.pkg_num         = ucPkgNum;
377             data.delivery_timing = ucDeliveryTiming;
378             data.ctrl_flg        = ucCtrlFlg;
379             data.event_id        = event_id;
380             for (i = 0; i < ucPkgNum; i++) {
381                 data.did[i] = pulDid[i];
382             }
383
384             /* Messaging */
385             ret_api = PosSndMsg(ch_pno,
386                                 PNO_VEHICLE_SENSOR,
387                                 CID_SENSORIF_PKG_DELIVERY_ENTRY_EXT,
388                                 (uint16_t)sizeof(SENSOR_MSG_DELIVERY_ENTRY_DAT),
389                                 (const void *)&data);
390
391             if (RET_NORMAL == ret_api) {
392                 /* Message transmission processing is successful */
393                 /* Wait for completion event from vehicle sensor thread */
394                 ret_api = _pb_WaitEvent(event_id,
395                                         SAPI_EVWAIT_VAL,
396                                         SENSOR_RET_ERROR_MIN,
397                                         SENSOR_RET_NORMAL,
398                                         &event_val,
399                                         POS_API_TIME_OUT_MS);
400                 if (RET_NORMAL != ret_api) {
401                     /* Return an internal error */
402                     ret = SENSOR_RET_ERROR_INNER;
403                 } else {
404                     /* Return from Event Wait */
405                     /* Set event value (processing result) as return value */
406                     ret = (SENSOR_RET_API)event_val;
407                 }
408             } else {
409                 /* Message transmission processing failed */
410                 /* Return an internal error */
411                 ret = SENSOR_RET_ERROR_INNER;
412             }
413
414             /* Event deletion */
415             ret_api = PosDeleteEvent(event_id);
416         } else {
417             /* Event generation failure */
418             ret = SENSOR_RET_ERROR_CREATE_EVENT;
419         }
420         } else {
421             /* When resource shortage occurs, the system terminates with an insufficient resource error. */
422             ret = SENSOR_RET_ERROR_RESOURCE;
423         }
424         /* Resource release */
425         VehicleReleaseResource();
426     }
427
428     /* Internal debug log output */
429     FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "- [ret = %d]", ret);
430
431     return ret;
432 }
433
434 /*******************************************************************************
435  * MODULE    : PosCreateEvent
436  * ABSTRACT  : Event creation process
437  * FUNCTION  : Generate an event
438  * ARGUMENT  : pno        : Thread ID
439  * NOTE      :
440  * RETURN    : Non-zero        : Event ID
441  *           : Zero            : Event generation failure
442  ******************************************************************************/
443 EventID PosCreateEvent(PNO pno) {
444     EventID    event_id;          /* Event ID */
445     char       event_name[32];    /* Event name character string buffer */
446     RET_API    ret_api;           /* System API return value */
447
448     /* Initialization of event name character string buffer */
449     (void)memset(reinterpret_cast<void *>(event_name), 0, sizeof(event_name));
450
451     /* Event name creation */
452     snprintf(event_name, sizeof(event_name), "SENSOR_%X", pno);    /* Ignore->MISRA-C++:2008 Rule 5-2-12 */
453
454     /* Event Generation */
455     event_id = _pb_CreateEvent(FALSE , 0, event_name);    /* Ignore->MISRA-C++:2008 Rule 5-2-12 */
456
457     if (0 != event_id) {
458         /* For successful event generation */
459
460         /* Initialize the event */
461         ret_api = _pb_SetEvent(event_id, SAPI_EVSET_ABSOLUTE, SENSOR_EVENT_VAL_INIT);
462         if (RET_NORMAL != ret_api) {
463             /* Event initialization failed */
464
465             /* Delete Event and Return Event Generation Failed */
466             ret_api = PosDeleteEvent(event_id);
467             event_id = 0;
468         }
469     }
470
471     return event_id;
472 }
473
474 /*******************************************************************************
475  * MODULE    : PosDeleteEvent
476  * ABSTRACT  : Event deletion processing
477  * FUNCTION  : Delete events
478  * ARGUMENT  : event_id    : Event ID of the event to delete
479  * NOTE      :
480  * RETURN    : RET_NORMAL    : Normal completion
481  *           : RET_EV_NONE    : Specified event does not exist
482  ******************************************************************************/
483 RET_API PosDeleteEvent(EventID event_id) {
484     return(_pb_DeleteEvent(event_id));
485 }
486
487 /*******************************************************************************
488  * MODULE    : SensorLinkShareData
489  * ABSTRACT  : Link to shared memory
490  * FUNCTION  : Link to shared memory
491  * ARGUMENT  : **share_top    : Storage destination of shared memory top address
492  *           : *share_size    : Storage destination of shared memory area size
493  *           : *offset    : Offset storage destination to free shared memory area
494  * NOTE      :
495  * RETURN    : RET_NORMAL    : Normal completion
496  *           : RET_ERROR    : There is no shared memory area.
497  ******************************************************************************/
498 RET_API SensorLinkShareData(void **share_top, uint32_t *share_size, uint16_t *offset) {  // LCOV_EXCL_START 8:dead code
499     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
500     RET_API         ret_api;    /* System API return value */
501     SemID           sem_id;     /* Semaphore ID */
502     SENSOR_SHARE    *share_top_tmp;
503     int32_t         i;
504
505     /* Initialization */
506     ret_api = RET_ERROR;
507
508     /* Create Semaphore */
509     sem_id = _pb_CreateSemaphore(const_cast<char *>(SENSOR_SEMAPHO_NAME));
510     if (0 != sem_id) {
511         /* Semaphore Lock */
512         ret_api = _pb_SemLock(sem_id);
513         if (RET_NORMAL == ret_api) {
514             /* Link to shared memory */
515             ret_api = _pb_LinkShareData(const_cast<char *>(SENSOR_SHARE_NAME), share_top, share_size);
516             if (RET_NORMAL == ret_api) {
517                 /* By searching the free shared memory area,Offset is calculated if there is free space. */
518                 share_top_tmp = reinterpret_cast<SENSOR_SHARE *>(*share_top);
519
520                 /* Because the first block of the shared memory area is the control area,Loop from i = 1 */
521                 for (i = 1; i < SENSOR_SHARE_BLOCK_NUM; i++) {
522                     if (SENSOR_SHARE_UNLOCK == share_top_tmp->mng.lock_info[i]) {
523                         break;
524                     }
525                 }
526                 if (i < SENSOR_SHARE_BLOCK_NUM) {
527                     /* Empty space */
528                     /* Lock the block */
529                     share_top_tmp->mng.lock_info[i] = SENSOR_SHARE_LOCK;
530
531                     /* Calculate the offset to the block */
532                     *offset = static_cast<uint16_t>(i * SENSOR_SHARE_BLOCK_SIZE);
533
534                     /* Normal completion */
535                     ret_api = RET_NORMAL;
536                 } else {
537                     /* No free space */
538                     ret_api = RET_ERROR;
539                 }
540             } else {
541                 /* Failed link to shared memory */
542                 ret_api = RET_ERROR;
543             }
544             /* Semaphore unlock */
545             _pb_SemUnlock(sem_id);
546         } else {
547             /* Semaphore lock failed */
548             ret_api = RET_ERROR;
549         }
550     } else {
551         /* Semaphore creation failed */
552         ret_api = RET_ERROR;
553     }
554
555     return ret_api;
556 }
557 // LCOV_EXCL_STOP
558
559 /*******************************************************************************
560  * MODULE    : SensorUnLinkShareData
561  * ABSTRACT  : Unlinking shared memory
562  * FUNCTION  : Unlink shared memory
563  * ARGUMENT  : *share_top    : Start address of shared memory
564  *           : offset        : Offset to shared memory free area
565  * NOTE      :
566  * RETURN    : RET_NORMAL    : Normal completion
567  *           : RET_ERROR    : There is no shared memory area./semaphore error
568  ******************************************************************************/
569 RET_API SensorUnLinkShareData(SENSOR_SHARE *share_top, uint16_t offset) {  // LCOV_EXCL_START 8:dead code
570     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
571     RET_API    ret_api;    /* System API return value */
572     SemID      sem_id;     /* Semaphore ID */
573     int32_t    i;
574
575     /* Initialization */
576     ret_api = RET_ERROR;
577
578     /* Create Semaphore */
579     sem_id = _pb_CreateSemaphore(const_cast<char *>(SENSOR_SEMAPHO_NAME));
580     if (0 != sem_id) {
581         /* Semaphore Lock */
582         ret_api = _pb_SemLock(sem_id);
583         if (RET_NORMAL == ret_api) {
584             /* Unlock the block */
585             i = static_cast<int32>(offset) / SENSOR_SHARE_BLOCK_SIZE;
586             share_top->mng.lock_info[i] = SENSOR_SHARE_UNLOCK;
587
588             /* Semaphore unlock */
589             _pb_SemUnlock(sem_id);
590
591             /* Normal completion */
592             ret_api = RET_NORMAL;
593         } else {
594             /* Semaphore lock failed */
595             ret_api = RET_ERROR;
596         }
597     } else {
598         /* Semaphore creation failed */
599         ret_api = RET_ERROR;
600     }
601
602     return ret_api;
603 }
604 // LCOV_EXCL_STOP
605
606 /*******************************************************************************
607  * MODULE    : SensorSetShareData
608  * ABSTRACT  : Write processing to shared memory
609  * FUNCTION  : Write shared memory
610  * ARGUMENT  : *share_top    : Start address of shared memory
611  *           : offset        : Offsets to shared memory write destination
612  *           : *data_src    :
613              : size_src :
614  * NOTE      :
615  * RETURN    : void
616  ******************************************************************************/
617 void SensorSetShareData(void *share_top, uint16_t offset, const void *data_src, uint16_t size_src) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
618     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
619     SENSOR_SHARE_BLOCK_DAT *share_dat;
620
621     /* Calculate Shared Memory Write Address */
622     share_dat = reinterpret_cast<SENSOR_SHARE_BLOCK_DAT *>(reinterpret_cast<uint8_t *>(share_top) + offset);
623     /* #QAC confirmation Rule11.4 Use structure for member reference(Cast according to shared memory link IF) */
624
625     /* Clear Shared Memory */
626     memset(reinterpret_cast<void *>(share_dat), 0, sizeof(SENSOR_SHARE_BLOCK_DAT));
627
628     /* Set write size to shared memory */
629     share_dat->size = size_src;
630
631     /* Set specified data in shared memory */
632     memcpy(reinterpret_cast<void *>(&share_dat->data), data_src, (size_t)size_src);
633 }
634 // LCOV_EXCL_STOP
635
636 /*******************************************************************************
637  * MODULE    : PosSndMsg
638  * ABSTRACT  : Message transmission processing
639  * FUNCTION  : Send a message to the specified PNO
640  * ARGUMENT  : pno_src            : Source PNO
641  *           : pno_dest        : Destination PNO
642  *           : cid            : Command ID
643  *           : msg_len            : Message data body length
644  *           : *msg_len        : Pointer to message data
645  * NOTE      :
646  * RETURN    : RET_NORMAL        : Normal completion
647  *           : RET_ERRNOTRDY    : Destination process is not wakeup
648  *           : RET_ERRMSGFULL    : Message queue overflows
649  *           : RET_ERRPARAM        : Buffer size error
650  ******************************************************************************/
651 RET_API PosSndMsg(PNO pno_src, PNO pno_dest, CID cid, uint16_t msg_len, const void *msg_data) {
652     SENSOR_INTERNAL_MSG_BUF   msg_buf;        /* message buffer */
653     T_APIMSG_MSGBUF_HEADER    *msg_hdr;       /* Pointer to the message header */
654     RET_API                   ret_api;        /* Return value */
655     PCSTR                     thread_name;    /* Destination thread name                */
656
657     /* Internal debug log output */
658     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "+");
659
660     /* _CWORD71_ processing speed(Memset modification) */
661     /* Initializing the header of the message buffer */
662     memset(reinterpret_cast<void *>(&msg_buf.hdr), 0, sizeof(T_APIMSG_MSGBUF_HEADER));
663
664     /* Get pointer to send buffer */
665     msg_hdr = reinterpret_cast<T_APIMSG_MSGBUF_HEADER *>(reinterpret_cast<void *>(&msg_buf));
666
667     /*--------------------------------------------------------------*
668      *    Create message headers                                    *
669      *--------------------------------------------------------------*/
670     msg_hdr->hdr.sndpno         = pno_src;    /* Source PNO */
671     msg_hdr->hdr.cid            = cid;        /* Command ID */
672     msg_hdr->hdr.msgbodysize    = msg_len;    /* Message data body length */
673
674     /*--------------------------------------------------------------*
675      *    Create message data                                    *
676      *--------------------------------------------------------------*/
677     if ((0 != msg_data) && (0 != msg_len)) {    /* Ignore->MISRA-C++:2008 Rule 5-0-5 */
678         /* Set the message data */
679         memcpy(reinterpret_cast<void *>(msg_buf.data), msg_data, (size_t)msg_len);
680     }
681     /*--------------------------------------------------------------*
682      * Send messages                                            *
683      *--------------------------------------------------------------*/
684     /* Get Thread Name from PNO */
685     if (pno_dest <= SYS_PNO_MAX) {
686         thread_name = POS_THREAD_NAME;
687     } else {
688         thread_name = _pb_CnvPno2Name(pno_dest);
689     }
690
691     if ((pno_dest <= SYS_PNO_MAX) && (pno_src <= SYS_PNO_MAX)) {
692         /* Internal debug log output */
693         FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "[LOG pno_dest = 0x%x]", pno_dest);
694
695         /* Internal Process Transmission and Reception Messages */
696         ret_api = _pb_SndMsg(pno_dest,
697                              (uint16_t)(sizeof(T_APIMSG_MSGBUF_HEADER) + msg_len),/* Ignore->MISRA-C++:2008 Rule 5-0-5 */
698                              reinterpret_cast<void *>(&msg_buf), 0);
699     } else {
700         /* Internal debug log output */
701         FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, 
702                       "[LOG thread_name = %s, cid = 0x%x]", thread_name, cid);
703
704         /* External Process Transmission and Reception Messages */
705         ret_api = _pb_SndMsg_Ext(thread_name,
706                                  cid,
707                                  (uint16_t)(msg_len),    /* Ignore->MISRA-C++:2008 Rule 5-0-5 */
708                                  reinterpret_cast<void *>(&(msg_buf.data)), 0);
709     }
710     /* If RET_ERROR,Register a dialog if called from a Vehicle related thread */ /* Task_30332 */
711     if (ret_api == RET_ERROR) {
712         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "[ERROR]");
713     }
714
715     /* Internal debug log output */
716     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "- [ret = %d]", ret_api);
717
718     return ret_api;
719 }
720
721 /**
722  * @brief
723  *  Sensor information acquisition
724  *
725  *  Obtain sensor information
726  *
727  * @param[in] hApp          HANDLE   - Application handle
728  * @param[in] did         DID      - Data ID for vehicle information
729  * @param[in] dest_data     void*    - Pointer representing the storage destination of vehicle sensor information
730  * @param[in] dest_size    uint16_t - Storage destination size of vehicle sensor information(byte)
731  *
732  * @return  0 or more                        Stored data size(Include illegal)<br>
733  *          POS_RET_ERROR_CREATE_EVENT   Event generation failure<br>
734  *          POS_RET_ERROR_OUTOF_MEMORY   Shared memory allocation failed<br>
735  *          POS_RET_ERROR_SIZE           Storage destination size error<br>
736  *          POS_RET_ERROR_DID            Unregistered ID<br>
737  *          POS_RET_ERROR_NOSUPPORT      Unsupported environment
738  *
739  */
740 POS_RET_API POS_GetSensData(HANDLE hApp, DID did, void *dest_data, uint16_t dest_size)
741 {
742     POS_RET_API     ret;                               /* Return value */
743     UNIT_TYPE       type = UNIT_TYPE_NONE;            /* Supported HW Configuration Type    */
744     BOOL            ret_b;
745
746     /** NULL checking */
747     if ((hApp == NULL) || (dest_data == NULL)) {
748         /** Parameter error */
749         ret = POS_RET_ERROR_PARAM;
750     } else {
751         /* Positioning Base API initialization */
752         _pb_Setup_CWORD64_API(hApp);
753
754         /* Supported HW Configuration Check */
755         type = GetEnvSupportInfo();
756         if (UNIT_TYPE_GRADE1 == type) {
757             /* GRADE1 */
758             ret = POS_RET_NORMAL;
759         } else if (UNIT_TYPE_GRADE2 == type) {
760           /*
761            *  Note.
762            *  This feature branches processing depending on the unit type.
763            */
764             ret = POS_RET_ERROR_NOSUPPORT;
765         } else {
766             /* Environment error */
767             ret = POS_RET_ERROR_NOSUPPORT;
768         }
769     }
770
771     if (ret == NAVIINFO_RET_NORMAL) {
772         /* Judge DID*/
773         ret_b = SENSOR_DID_JUDGE_GET(did);
774         if (ret_b == FALSE) {
775             /* An unacceptable ID is regarded as a parameter error. */
776             ret = POS_RET_ERROR_PARAM;
777         } else {
778             /* Data acquisition process */
779             ret = PosGetProc(did, dest_data, dest_size);
780         }
781     }
782
783     return ret;
784 }