common_library: gettid is multiple declaration in cl_error
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning / client / include / POS_private.h
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
19  *  POS_private.h
20  * @brief
21  *  Inner functions of Positoning API
22  *
23  */
24 #ifndef POSITIONING_CLIENT_INCLUDE_POS_PRIVATE_H_
25 #define POSITIONING_CLIENT_INCLUDE_POS_PRIVATE_H_
26
27 /*---------------------------------------------------------------------------------*
28  * Include Files                                                                   *
29  *---------------------------------------------------------------------------------*/
30 #include <vehicle_service/positioning_base_library.h>
31 #include <vehicle_service/POS_define.h>
32 #include "Vehicle_API_Dummy.h"
33 #include "Vehicle_API_private.h"
34 #include "Naviinfo_API.h"
35 #include "POS_common_private.h"
36 #include <asm/unistd.h>
37 #include <other_service/VP_GetEnv.h>
38 #include <other_service/env_vehicleparameterlibrary.h>
39 #include "CommonDefine.h"
40 /*---------------------------------------------------------------------------------*
41  * Function Prototype                                                              *
42  *---------------------------------------------------------------------------------*/
43 /* Shared Library Value Area Check Function */
44 inline UNIT_TYPE GetEnvSupportInfo(void);
45 inline BOOL ChkUnitType(UNIT_TYPE type);
46 inline RET_API MunMapDeviceIo(HANDLE dev, uint32_t map_size);
47 inline RET_API MilliSecSleep(uint32_t mill_time);
48 inline POS_RET_API PosChkParam8(int8_t data, int8_t min, int8_t max, const char* fn, int32_t line);
49 inline POS_RET_API PosChkParam16(int16_t data, int16_t min, int16_t max, const char* fn, int32_t line);
50 inline POS_RET_API PosChkParam32(int32_t data, int32_t min, int32_t max, const char* fn, int32_t line);
51 inline POS_RET_API PosChkParamU8(uint8_t data, uint8_t min, uint8_t max, const char* fn, int32_t line);
52 inline POS_RET_API PosChkParamU16(uint16_t data, uint16_t min, uint16_t max, const char* fn, int32_t line);
53 inline POS_RET_API PosChkParamU32(uint32_t data, uint32_t min, uint32_t max, const char* fn, int32_t line);
54
55 /* Shared Intra-Library Core Functions */
56 inline POS_RET_API    PosSetProc(DID did, void *p_data, uint16_t size, uint8_t is_event);
57 inline POS_RET_API    PosGetProc(DID did, void *p_data, uint16_t dest_size);
58 inline SENSOR_RET_API PosRegisterListenerProc(PCSTR notify_name,
59                                               DID did, u_int8 ctrl_flg, u_int8 delivery_timing);
60
61 /* General Functions in Shared Libraries */
62 inline EventID VehicleCreateEvent(PNO pno);
63 inline RET_API VehicleDeleteEvent(EventID event_id);
64 inline RET_API VehicleLinkShareData(void **share_top, uint32_t *share_size, uint16_t *offset);
65 inline RET_API VehicleUnLinkShareData(VEHICLE_SHARE *share_top, uint16_t offset);
66 inline RET_API VehicleSndMsg(PNO pno_src, PNO pno_dest, CID cid, uint16_t msg_len, const void *msg_data);
67 inline BOOL VehicleGetResource(void);
68 inline void VehicleReleaseResource(void);
69 inline uint32_t GetTid(void);
70 /*---------------------------------------------------------------------------------*
71  * Definition                                                                      *
72  *---------------------------------------------------------------------------------*/
73 #define POS_CHKPARAM8(data, min, max)   PosChkParam8(data, min, max, __func__, __LINE__)
74 #define POS_CHKPARAM16(data, min, max)  PosChkParam16(data, min, max, __func__, __LINE__)
75 #define POS_CHKPARAM32(data, min, max)  PosChkParam32(data, min, max, __func__, __LINE__)
76 #define POS_CHKPARAMU8(data, min, max)  PosChkParamU8(data, min, max, __func__, __LINE__)
77 #define POS_CHKPARAMU16(data, min, max) PosChkParamU16(data, min, max, __func__, __LINE__)
78 #define POS_CHKPARAMU32(data, min, max) PosChkParamU32(data, min, max, __func__, __LINE__)
79 #define POS_API_TIME_OUT_MS  5000 /* Timeout period(ms) */
80
81 /*---------------------------------------------------------------------------------*
82  * Inline Functions                                                                *
83  *---------------------------------------------------------------------------------*/
84 UNIT_TYPE GetEnvSupportInfo(void) {
85     UNIT_TYPE  ret_type = UNIT_TYPE_GRADE1;
86
87     char       env_area[VP_MAX_LENGTH];
88     char       env_grade[VP_MAX_LENGTH];
89     char*      p_env_grade    = env_grade;
90     char*      p_env_area     = env_area;
91
92     /*
93      *  Note.
94      *  This feature branches processing depending on the area and grade type.
95      */
96
97     VP_GetEnv(VP_VEHICLEPARAMETERLIBRARY_AREA, p_env_area);
98
99     if (0 == strcmp(p_env_area, "AREA1")) {
100         ret_type = UNIT_TYPE_GRADE2;
101     } else if (0 == strcmp(p_env_area, "AREA2")) {
102         memset(&env_grade, 0x00, sizeof(env_grade));
103         VP_GetEnv(VP_VEHICLEPARAMETERLIBRARY_GRADE, p_env_grade);
104         if (0 == strcmp(p_env_grade, "_CWORD95_") ||
105             0 == strcmp(p_env_grade, "_CWORD101_") ||
106             0 == strcmp(p_env_grade, "_CWORD61_")) {
107             ret_type = UNIT_TYPE_GRADE2;
108         }
109     } else {
110         // NOP
111     }
112
113     return ret_type;
114 }
115
116 BOOL ChkUnitType(UNIT_TYPE type) {
117     UNIT_TYPE type_temp;
118     BOOL ret;
119
120     type_temp = GetEnvSupportInfo();
121     if ((type_temp & type) != 0) {
122         ret = TRUE;
123     } else {
124         ret = FALSE;
125     }
126
127     return ret;
128 }
129
130 RET_API MunMapDeviceIo(HANDLE dev, uint32_t map_size) {
131     return RET_NORMAL;
132 }
133
134 RET_API MilliSecSleep(uint32_t mill_time) {
135     switch (mill_time) {
136         case 0:
137         {
138             /* Discard the time slice */
139             sched_yield();
140             break;
141         }
142         case INFINITE:
143         {
144             /* Abort processing indefinitely */
145             while (1) {
146                 sleep(INFINITE);
147             }
148         }
149         default:
150             /* Sleep for Specified Time */
151             usleep(mill_time * 1000);
152             break;
153     }
154
155     return RET_NORMAL;
156 }
157
158 /**
159  * @brief
160  *  Data Valid Value Determination(int8)
161  *
162  * @param[in] date  int8_t       Object data
163  * @param[in] min   int8_t       Target Data Valid Value Range(Minimum value)
164  * @param[in] max   int8_t       Target Data Valid Value Range(Maximum value)
165  * @param[in] fn    const char*  Pointer to the function name
166  * @param[in] line  int32_t      Number of lines
167  *
168  * @return  POS_RET_NORMAL       Within the valid range<br>
169  *          POS_RET_ERROR        Out of scope<br>
170  *          POS_RET_ERROR_PARAM  Argument error
171  */
172 inline POS_RET_API PosChkParam8(int8_t data, int8_t min, int8_t max, const char* fn, int32_t line) {
173     POS_RET_API ret = POS_RET_NORMAL;
174
175     if (fn == NULL) {
176         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Argument ERROR!! fn=%p", fn);
177         ret = POS_RET_ERROR_PARAM;
178     } else {
179         if ((data < min) || (max < data)) {
180             ret = POS_RET_ERROR;
181             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, 
182                           "%s/%d/Out of range!! data=%d[%d, %d]",
183                           fn, line, data, min, max);
184         }
185     }
186
187     return ret;
188 }
189
190 /**
191  * @brief
192  *  Data Valid Value Determination(int16)
193  *
194  * @param[in] date  int16_t      Object data
195  * @param[in] min   int16_t      Target Data Valid Value Range(Minimum value)
196  * @param[in] max   int16_t      Target Data Valid Value Range(Maximum value)
197  * @param[in] fn    const char*  Pointer to the function name
198  * @param[in] line  int32_t      Number of lines
199  *
200  * @return  POS_RET_NORMAL       Within the valid range<br>
201  *          POS_RET_ERROR        Out of scope<br>
202  *          POS_RET_ERROR_PARAM  Argument error
203  */
204 inline POS_RET_API PosChkParam16(int16_t data, int16_t min, int16_t max, const char* fn, int32_t line) {
205     POS_RET_API ret = POS_RET_NORMAL;
206
207     if (fn == NULL) {
208         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Argument ERROR!! fn=%p", fn);
209         ret = POS_RET_ERROR_PARAM;
210     } else {
211         if ((data < min) || (max < data)) {
212             ret = POS_RET_ERROR;
213             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, 
214                           "%s/%d/Out of range!! data=%d[%d, %d]",
215                           fn, line, data, min, max);
216         }
217     }
218
219     return ret;
220 }
221
222 /**
223  * @brief
224  *  Data Valid Value Determination(int32)
225  *
226  * @param[in] date  int32_t      Object data
227  * @param[in] min   int32_t      Target Data Valid Value Range(Minimum value)
228  * @param[in] max   int32_t      Target Data Valid Value Range(Maximum value)
229  * @param[in] fn    const char*  Pointer to the function name
230  * @param[in] line  int32_t      Number of lines
231  *
232  * @return  POS_RET_NORMAL       Within the valid range<br>
233  *          POS_RET_ERROR        Out of scope<br>
234  *          POS_RET_ERROR_PARAM  Argument error
235  */
236 inline POS_RET_API PosChkParam32(int32_t data, int32_t min, int32_t max, const char* fn, int32_t line) {
237     POS_RET_API ret = POS_RET_NORMAL;
238
239     if (fn == NULL) {
240         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Argument ERROR!! fn=%p", fn);
241         ret = POS_RET_ERROR_PARAM;
242     } else {
243         if ((data < min) || (max < data)) {
244             ret = POS_RET_ERROR;
245             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, 
246                           "%s/%d/Out of range!! data=%d[%d, %d]",
247                           fn, line, data, min, max);
248         }
249     }
250
251     return ret;
252 }
253
254 /**
255  * @brief
256  *  Data Valid Value Determination(unsigned int8)
257  *
258  * @param[in] date  uint8_t      Object data
259  * @param[in] min   uint8_t      Target Data Valid Value Range(Minimum value)
260  * @param[in] max   uint8_t      Target Data Valid Value Range(Maximum value)
261  * @param[in] fn    const char*  Pointer to the function name
262  * @param[in] line  int32_t      Number of lines
263  *
264  * @return  POS_RET_NORMAL       Within the valid range<br>
265  *          POS_RET_ERROR        Out of scope<br>
266  *          POS_RET_ERROR_PARAM  Argument error
267  */
268 inline POS_RET_API PosChkParamU8(uint8_t data, uint8_t min, uint8_t max, const char* fn, int32_t line) {
269     POS_RET_API ret = POS_RET_NORMAL;
270
271     if (fn == NULL) {
272         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Argument ERROR!! fn=%p", fn);
273         ret = POS_RET_ERROR_PARAM;
274     } else {
275         if ((data < min) || (max < data)) {
276             ret = POS_RET_ERROR;
277             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, 
278                           "%s/%d/Out of range!! data=%u[%u, %u]",
279                           fn, line, data, min, max);
280         }
281     }
282
283     return ret;
284 }
285
286 /**
287  * @brief
288  *  Data Valid Value Determination(unsigned int16)
289  *
290  * @param[in] date  uint16_t     Object data
291  * @param[in] min   uint16_t     Target Data Valid Value Range(Minimum value)
292  * @param[in] max   uint16_t     Target Data Valid Value Range(Maximum value)
293  * @param[in] fn    const char*  Pointer to the function name
294  * @param[in] line  int32_t      Number of lines
295  *
296  * @return  POS_RET_NORMAL       Within the valid range<br>
297  *          POS_RET_ERROR        Out of scope<br>
298  *          POS_RET_ERROR_PARAM  Argument error
299  */
300 inline POS_RET_API PosChkParamU16(uint16_t data, uint16_t min, uint16_t max, const char* fn, int32_t line) {
301     POS_RET_API ret = POS_RET_NORMAL;
302
303     if (fn == NULL) {
304         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Argument ERROR!! fn=%p", fn);
305         ret = POS_RET_ERROR_PARAM;
306     } else {
307         if ((data < min) || (max < data)) {
308             ret = POS_RET_ERROR;
309             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, 
310                           "%s/%d/Out of range!! data=%u[%u, %u]",
311                           fn, line, data, min, max);
312         }
313     }
314
315     return ret;
316 }
317
318 /**
319  * @brief
320  *  Data Valid Value Determination(unsigned int32)
321  *
322  * @param[in] date  uint32_t     Object data
323  * @param[in] min   uint32_t     Target Data Valid Value Range(Minimum value)
324  * @param[in] max   uint32_t     Target Data Valid Value Range(Maximum value)
325  * @param[in] fn    const char*  Pointer to the function name
326  * @param[in] line  int32_t      Number of lines
327  *
328  * @return  POS_RET_NORMAL       Within the valid range<br>
329  *          POS_RET_ERROR        Out of scope<br>
330  *          POS_RET_ERROR_PARAM  Argument error
331  */
332 inline POS_RET_API PosChkParamU32(uint32_t data, uint32_t min, uint32_t max, const char* fn, int32_t line) {
333     POS_RET_API ret = POS_RET_NORMAL;
334
335     if (fn == NULL) {
336         FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "Argument ERROR!! fn=%p", fn);
337         ret = POS_RET_ERROR_PARAM;
338     } else {
339         if ((data < min) || (max < data)) {
340             ret = POS_RET_ERROR;
341             FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, 
342                           "%s/%d/Out of range!! data=%u[%u, %u]",
343                           fn, line, data, min, max);
344         }
345     }
346
347     return ret;
348 }
349
350 /**
351  * @brief
352  *  Data setting process(Internal processing)
353  *
354  *  Set the specified information
355  *
356  * @param[in] did      DID         - Data ID
357  * @param[in] p_data    void*       - Configuration data
358  * @param[in] size     uint16_t    - Setting data size
359  * @param[in] is_event  uint8_t     - Event wait status(TRUE/FALSE)
360  *
361  * @return  POS_RET_NORMAL           Normal completion(Include illegal)<br>
362  *          POS_RET_ERROR_PARAM      Parameter error<br>
363  *          POS_RET_ERROR_INNER      Internal error
364  */
365 inline POS_RET_API PosSetProc(DID did, void *p_data, uint16_t size, uint8_t is_event) {
366     POS_RET_API    ret = POS_RET_NORMAL;    /* Return value of this function */
367     RET_API      ret_api = RET_NORMAL;    /* API return value */
368     POS_MSGINFO    snd_msg;          /* Message */
369     EventID      event_id;          /* Event ID */
370     int32_t      event_val;          /* Event value */
371     PNO        pno;            /* Calling thread PNO */
372     uint32_t    pid;            /* Process ID */
373     uint32_t    tid;            /* Thread ID */
374     char      name[128];
375
376     /* Data size check */
377     if (POS_MSG_INFO_DSIZE < size) {
378         ret = POS_RET_ERROR_PARAM;
379     } else {
380         /* Resource acquisition */
381         if (VehicleGetResource() == TRUE) {
382             /* Message data */
383             /* _CWORD71_ processing speed(Memset modification) */
384             /* Initialization of areas that do not contain values */
385             snd_msg.pno = 0;
386             snd_msg.rcv_flag = 0;
387             snd_msg.reserve = 0;
388
389             snd_msg.did  = did;
390             snd_msg.size  = size;
391             memcpy(snd_msg.data, p_data, size);
392
393             if (TRUE == is_event) {
394                 /* After requesting data setting,Wait for the setting completion(Event Wait) */
395
396                 /* Event Generation */
397                 pid = static_cast<uint32_t>(getpid());
398                 tid = GetTid();
399
400                 snprintf(name, sizeof(name), "PS_p%u_t%u", pid, tid);
401                 pno = _pb_CnvName2Pno(name);
402                 event_id = VehicleCreateEvent(pno);
403
404                 /* Set the source Pno of message data */
405                 snd_msg.pno = pno;
406
407                 if (0 != event_id) {
408                     /* Successful event generation */
409
410                     /* Send NAVI Sensor Data Setting to Vehicle Sensor */
411                     ret_api = VehicleSndMsg(pno,
412                                             PNO_VEHICLE_SENSOR,
413                                             CID_POSIF_SET_DATA,
414
415                                             (uint16_t)sizeof(POS_MSGINFO) - POS_MSG_INFO_DSIZE + snd_msg.size, /* variable length */
416                                             (const void *)&snd_msg);
417
418                     if (RET_NORMAL == ret_api) {
419                         /* If the data setup process is successful,Wait for a completion event */
420                         ret_api = _pb_WaitEvent(event_id,
421                                                 SAPI_EVWAIT_VAL,
422                                                 VEHICLE_RET_ERROR_MIN,
423                                                 VEHICLE_RET_NORMAL, &event_val, POS_API_TIME_OUT_MS);
424                         if (RET_NORMAL != ret_api) {
425                             /* Return an internal error */
426                             ret = POS_RET_ERROR_INNER;
427                         } else {
428                             /* Return from Event Wait */
429                             /* Set event value (processing result) as return value */
430                             ret = (POS_RET_API)event_val;
431                         }
432                     } else {
433                         /* Message transmission processing failed */
434                         ret = POS_RET_ERROR_INNER;
435                     }
436                     /* Event deletion */
437                     ret_api = VehicleDeleteEvent(event_id);
438                 } else {
439                     /* Event generation failure */
440                     ret = POS_RET_ERROR_INNER;
441                 }
442             } else {
443                 /* After setting data,Immediate termination */
444
445                 /* Send NAVI Sensor Data Setting to Vehicle Sensor */
446                 ret_api = _pb_SndMsg_Ext(POS_THREAD_NAME,
447                                          CID_POSIF_SET_DATA,
448                                          sizeof(POS_MSGINFO), reinterpret_cast<void*>(&snd_msg), 0);
449                 if (ret_api != RET_NORMAL) {
450                     /* Message transmission failure */
451                     ret = POS_RET_ERROR_INNER;
452                 }
453             }
454         } else {
455             /* Insufficient resource */
456             ret = POS_RET_ERROR_RESOURCE;
457         }
458         /* Resource release */
459         VehicleReleaseResource();
460     }
461
462     return ret;
463 }
464
465 /**
466  * @brief
467  *  Sensor information acquisition(Internal processing)
468  *
469  * @param[in] did         DID      - Data ID for vehicle information
470  * @param[in] p_data     void*    - Pointer representing the storage destination of vehicle sensor information
471  * @param[in] dest_size    uint16_t - Storage destination size of vehicle sensor information(byte)
472  *
473  * @return  0 or more                        Stored data size(Include illegal)<br>
474  *          POS_RET_ERROR_CREATE_EVENT   Event generation failure<br>
475  *          POS_RET_ERROR_OUTOF_MEMORY   Shared memory allocation failed<br>
476  *          POS_RET_ERROR_SIZE           Storage destination size error<br>
477  *          POS_RET_ERROR_DID            Unregistered ID<br>
478  */
479 inline POS_RET_API PosGetProc(DID did, void *p_data, uint16_t dest_size) {
480     POS_RET_API       ret;              /* Return value            */
481     RET_API           ret_api;          /* System API return value        */
482     EventID           event_id;         /* Event ID           */
483     int32_t           event_val;        /* Event value           */
484     void             *share_top;        /* Start address of shared memory       */
485     uint32_t          share_size;       /* Size of shared memory area       */
486     uint16_t          offset;                /* Offset to free shared memory area    */
487     VEHICLE_SHARE_BLOCK_DAT *share_dat; /* Address of free shared memory area  */
488     VEHICLE_MSG_GET_VEHICLE_DATA_DAT data; /* Message data     */
489     PNO                  pno;           /* Calling thread PNO       */
490     uint32_t    pid;          /* Process ID          */
491     uint32_t    tid;          /* Thread ID            */
492     char      name[128];
493
494     /* Resource acquisition */
495     if (VehicleGetResource() == TRUE) {
496         /* Initialization */
497         event_id = 0;
498         event_val = 0;
499         memset(reinterpret_cast<void *>(&data), 0, sizeof(VEHICLE_MSG_GET_VEHICLE_DATA_DAT));
500
501         /* Event Generation */
502         pid = static_cast<uint32_t>(getpid());
503         tid = GetTid();
504
505         snprintf(name, sizeof(name), "PG_p%u_t%u", pid, tid);
506         pno = _pb_CnvName2Pno(name);
507         event_id = VehicleCreateEvent(pno);
508
509         if (0 != event_id) {
510             /* Successful event generation */
511
512             /* Allocate shared memory */
513             ret_api = VehicleLinkShareData(reinterpret_cast<void **>(&share_top), &share_size, &offset);
514             if (RET_NORMAL != ret_api) {
515                 /* Failed to allocate shared memory */
516                 ret = POS_RET_ERROR_OUTOF_MEMORY;
517             } else { /* When the shared memory is allocated successfully */
518                 /* Calculate start address of free shared memory area */
519                 share_dat = reinterpret_cast<VEHICLE_SHARE_BLOCK_DAT *>(reinterpret_cast<u_int8 *>(share_top)
520                             + offset);
521
522                 /* Send vehicle sensor information acquisition message */
523                 data.did        = did;
524                 data.pno        = pno;
525                 data.offset     = offset;
526                 data.size       = VEHICLE_SHARE_BLOCK_DSIZE;
527
528                 /* Messaging */
529                 ret_api = VehicleSndMsg(pno,
530                                         PNO_VEHICLE_SENSOR,
531                                         CID_VEHICLEIF_GET_VEHICLE_DATA,
532                                         sizeof(VEHICLE_MSG_GET_VEHICLE_DATA_DAT), (const void *)&data);
533
534                 /* Message transmission processing is successful */
535                 if (RET_NORMAL == ret_api) {
536                     /* Wait for completion event from vehicle sensor thread */
537                     ret_api = _pb_WaitEvent(event_id,
538                                             SAPI_EVWAIT_VAL,
539                                             VEHICLE_RET_ERROR_MIN,
540                                             VEHICLE_RET_NORMAL, &event_val, POS_API_TIME_OUT_MS);
541
542                     if (RET_NORMAL != ret_api) {
543                         /* Return an internal error */
544                         ret = POS_RET_ERROR_INNER;
545                     } else { /* Return from Event Wait */
546                         /* Link to shared memory */
547                         ret_api = _pb_LinkShareData(const_cast<char *>(VEHICLE_SHARE_NAME),
548                                                     &share_top, &share_size);
549
550                         /* Calculate the address of the shared memory storage area. */
551                         share_dat = reinterpret_cast<VEHICLE_SHARE_BLOCK_DAT *>(reinterpret_cast<u_int8 *>(share_top)
552                                     + offset);
553
554                         if (event_val < 0) {
555                             /* Vehicle sensor information acquisition failure */
556                             ret = (VEHICLE_RET_API)event_val;
557                         } else if (RET_NORMAL != ret_api) {
558                             /* Shared memory error */
559                             ret = POS_RET_ERROR_OUTOF_MEMORY;
560                         } else if (dest_size < share_dat->size) {
561                             /* Storage destination size error */
562                             ret = POS_RET_ERROR_SIZE;
563                         } else { /* Vehicle sensor information acquisition success */
564                             /* Copy from shared memory to user memory */
565                             memcpy(p_data, share_dat->data, (size_t)share_dat->size);
566
567                             /* Set Write Size to Return Value */
568                             ret = static_cast<int32>(share_dat->size);
569                         }
570                     }
571                 } else { /* Message transmission processing failed */
572                     /* Return an event generation failure */
573                     ret = POS_RET_ERROR_CREATE_EVENT;
574                 }
575                 /* Free shared memory */
576                 (void)VehicleUnLinkShareData(reinterpret_cast<VEHICLE_SHARE*>(share_top), offset);
577             }
578             /* Event deletion */
579             ret_api = VehicleDeleteEvent(event_id);
580         } else {
581             /* Event generation failure */
582             ret = POS_RET_ERROR_CREATE_EVENT;
583         }
584     } else {
585         /* Insufficient resource */
586         ret = POS_RET_ERROR_RESOURCE;
587     }
588     /* Resource release */
589     VehicleReleaseResource();
590
591     return ret;
592 }
593
594 /**
595  * @brief
596  *  Delivery registration process(Internal processing)
597  *
598  * @param[in]  notify_name      Destination thread name
599  * @param[in]  did        Pointer to an array of data IDs for vehicle information
600  * @param[in]  ctrl_flg      Delivery control<br>
601  *        Delivery registration: SENSOR_DELIVERY_REGIST<br>
602  *        Delivery stop: SENSOR_DELIVERY_STOP    (Note: Not mounted)<br>
603  *        Resume delivery: SENSOR_DELIVERY_RESTART  (Note: Not mounted)
604  * @param[in]  delivery_timing    Delivery timing<br>
605  *        Updating  : SENSOR_DELIVERY_TIMING_UPDATE<br>
606  *        Changing  : SENSOR_DELIVERY_TIMING_CHANGE
607  *
608  * @return  SENSOR_RET_NORMAL        Successful registration<br>
609  *      SENSOR_RET_ERROR_CREATE_EVENT  Event generation failure<br>
610  *      SENSOR_RET_ERROR_PARAM      Parameter error<br>
611  *      SENSOR_RET_ERROR_DID      Unregistered ID<br>
612  *      SENSOR_RET_ERROR_BUFFULL    FULL of delivery registers<br>
613  *      SENSOR_RET_ERROR_INNER      Internal abnormality<br>
614  */
615 inline SENSOR_RET_API PosRegisterListenerProc(PCSTR notify_name, DID did, u_int8 ctrl_flg, u_int8 delivery_timing) {
616     SENSOR_RET_API          ret;      /* Return value */
617     RET_API              ret_api;    /* System API return value */
618     EventID              event_id;    /* Event ID */
619     int32              event_val;    /* Event value */
620     VEHICLE_MSG_DELIVERY_ENTRY_DAT  data;      /* Message data */
621     PNO                pno;      /* Converted internal PNO  */
622
623     /* Resource acquisition */
624     if (VehicleGetResource() == TRUE) {
625         /* Initialization */
626         event_id = 0;
627         event_val = 0;
628
629         /* Get PNO from Thread Name */
630         pno = _pb_CnvName2Pno(notify_name);
631
632         /* Event Generation */
633         event_id = VehicleCreateEvent(pno);
634
635         if (0 != event_id) {
636             /* Successful event generation */
637
638             /*--------------------------------------------------------------*
639              *  Send Vehicle Sensor Information Delivery Registration Message          *
640              *--------------------------------------------------------------*/
641             /* Create Message Data */
642             data.did      = did;
643             data.pno      = pno;
644             data.delivery_timing  = delivery_timing;
645             data.ctrl_flg  = ctrl_flg;
646             data.event_id   = event_id;
647
648             /* Messaging */
649             ret_api = VehicleSndMsg(pno,
650                                     PNO_VEHICLE_SENSOR,
651                                     CID_VEHICLEIF_DELIVERY_ENTRY,
652                                     (uint16_t)sizeof(VEHICLE_MSG_DELIVERY_ENTRY_DAT), (const void *)&data);
653
654             if (RET_NORMAL == ret_api) {
655                 /* Message transmission processing is successful */
656                 /* Wait for completion event from vehicle sensor thread */
657                 ret_api = _pb_WaitEvent(event_id,
658                                         SAPI_EVWAIT_VAL,
659                                         VEHICLE_RET_ERROR_MIN,
660                                         VEHICLE_RET_NORMAL, &event_val, POS_API_TIME_OUT_MS);
661                 if (RET_NORMAL != ret_api) {
662                     /* Return an internal error */
663                     ret = SENSOR_RET_ERROR_INNER;
664                 } else {
665                     /* Return from Event Wait */
666                     /* Set event value (processing result) as return value */
667                     ret = (SENSOR_RET_API)event_val;
668                 }
669             } else {
670                 /* Message transmission processing failed */
671                 /* Return an internal error */
672                 ret = SENSOR_RET_ERROR_INNER;
673             }
674             /* Event deletion */
675             ret_api = VehicleDeleteEvent(event_id);
676         } else {
677             /* Event generation failure */
678             ret = SENSOR_RET_ERROR_CREATE_EVENT;
679         }
680     } else {
681         /* Insufficient resource */
682         ret = SENSOR_RET_ERROR_RESOURCE;
683     }
684     /* Resource release */
685     VehicleReleaseResource();
686
687     return ret;
688 }
689
690 /*******************************************************************************
691  * MODULE    : VehicleCreateEvent
692  * ABSTRACT  : Event creation process
693  * FUNCTION  : Generate an event
694  * ARGUMENT  : pno    : Thread ID
695  * NOTE      :
696  * RETURN    : Non-zero    : Event ID
697  *           : Zero      : Event generation failure
698  ******************************************************************************/
699 inline EventID VehicleCreateEvent(PNO pno) {
700     EventID    event_id;              /* Event ID */
701     char    event_name[32];          /* Event name character string buffer */
702     RET_API    ret_api;              /* System API return value */
703
704
705     /* Initialization of event name character string buffer */
706     memset(reinterpret_cast<void *>(event_name), 0, sizeof(event_name));
707     /* Event name creation */
708     snprintf(event_name, sizeof(event_name), "VEHICLE_%X", pno);
709
710     /* Event Generation */
711     event_id = _pb_CreateEvent(FALSE, 0, event_name);
712
713     if (0 != event_id) {
714         /* For successful event generation */
715
716         /* Initialize the event */
717         ret_api = _pb_SetEvent(event_id, SAPI_EVSET_ABSOLUTE, VEHICLE_EVENT_VAL_INIT);
718         if (RET_NORMAL != ret_api) {
719             /* Event initialization failed */
720             /* Delete Event and Return Event Generation Failed */
721             ret_api = VehicleDeleteEvent(event_id);
722             event_id = 0;
723         }
724     }
725
726     return(event_id);
727 }
728
729 /*******************************************************************************
730  * MODULE    : VehicleDeleteEvent
731  * ABSTRACT  : Event deletion processing
732  * FUNCTION  : Delete events
733  * ARGUMENT  : event_id  : Event ID of the event to delete
734  * NOTE      :
735  * RETURN    : RET_NORMAL  : Normal completion
736  *           : RET_EV_NONE  : Specified event does not exist
737  ******************************************************************************/
738 inline RET_API VehicleDeleteEvent(EventID event_id) {
739     return(_pb_DeleteEvent(event_id));
740 }
741
742 /*******************************************************************************
743  * MODULE    : VehicleLinkShareData
744  * ABSTRACT  : Link to shared memory
745  * FUNCTION  : Link to shared memory
746  * ARGUMENT  : **share_top  : Storage destination of shared memory top address
747  *           : *share_size  : Storage destination of shared memory area size
748  *           : *offset  : Offset storage destination to free shared memory area
749  * NOTE      :
750  * RETURN    : RET_NORMAL  : Normal completion
751  *           : RET_ERROR  : There is no shared memory area.
752  ******************************************************************************/
753 inline RET_API VehicleLinkShareData(void **share_top, uint32_t *share_size, uint16_t *offset) {
754     RET_API      ret_api;            /* System API return value */
755     SemID      sem_id;              /* Semaphore ID */
756     VEHICLE_SHARE  *share_top_tmp;
757     int32      i;
758
759     /* Initialization */
760     ret_api = RET_ERROR;
761
762     /* Create Semaphore */
763     sem_id = _pb_CreateSemaphore(const_cast<char *>(VEHICLE_SEMAPHO_NAME));
764     if (0 != sem_id) {
765         /* Semaphore Lock */
766         ret_api = _pb_SemLock(sem_id);
767         if (RET_NORMAL == ret_api) {
768             /* Link to shared memory */
769             ret_api = _pb_LinkShareData(const_cast<char *>(VEHICLE_SHARE_NAME), share_top, share_size);
770             if (RET_NORMAL == ret_api) {
771                 /* By searching the free shared memory area,Offset is calculated if there is free space. */
772                 share_top_tmp = reinterpret_cast<VEHICLE_SHARE *>(*share_top);
773
774                 /* Because the first block of the shared memory area is the control area,Loop from i = 1 */
775                 for (i = 1; i < VEHICLE_SHARE_BLOCK_NUM; i++) {
776                     if (VEHICLE_SHARE_UNLOCK == share_top_tmp->mng.lock_info[i]) {
777                         break;
778                     }
779                 }
780                 if (i < VEHICLE_SHARE_BLOCK_NUM) {
781                     /* Empty space */
782                     /* Lock the block */
783                     share_top_tmp->mng.lock_info[i] = VEHICLE_SHARE_LOCK;
784
785                     /* Calculate the offset to the block */
786                     *offset = static_cast<u_int16>(i * VEHICLE_SHARE_BLOCK_SIZE);
787
788                     /* Normal completion */
789                     ret_api = RET_NORMAL;
790                 } else {
791                     /* No free space */
792                     ret_api = RET_ERROR;
793                 }
794             } else {
795                 /* Failed link to shared memory */
796                 ret_api = RET_ERROR;
797             }
798             /* Semaphore unlock */
799             _pb_SemUnlock(sem_id);
800         } else {
801             /* Semaphore lock failed */
802             ret_api = RET_ERROR;
803         }
804     } else {
805         /* Semaphore creation failed */
806         ret_api = RET_ERROR;
807     }
808
809     return(ret_api);
810 }
811
812 /*******************************************************************************
813  * MODULE    : VehicleUnLinkShareData
814  * ABSTRACT  : Unlinking shared memory
815  * FUNCTION  : Unlink shared memory
816  * ARGUMENT  : *share_top  : Start address of shared memory
817  *           : offset    : Offset to shared memory free area
818  * NOTE      :
819  * RETURN    : RET_NORMAL  : Normal completion
820  *           : RET_ERROR  : There is no shared memory area./semaphore error
821  ******************************************************************************/
822 inline RET_API VehicleUnLinkShareData(VEHICLE_SHARE *share_top, uint16_t offset) {
823     RET_API      ret_api;            /* System API return value */
824     SemID      sem_id;              /* Semaphore ID */
825     int32      i;
826
827     /* Initialization */
828     ret_api = RET_ERROR;
829
830     /* Create Semaphore */
831     sem_id = _pb_CreateSemaphore(const_cast<char *>(VEHICLE_SEMAPHO_NAME));
832     if (0 != sem_id) {
833         /* Semaphore Lock */
834         ret_api = _pb_SemLock(sem_id);
835         if (RET_NORMAL == ret_api) {
836             /* Unlock the block */
837             i = static_cast<int32>(offset) / VEHICLE_SHARE_BLOCK_SIZE;
838             share_top->mng.lock_info[i] = VEHICLE_SHARE_UNLOCK;
839
840             /* Semaphore unlock */
841             _pb_SemUnlock(sem_id);
842
843             /* Normal completion */
844             ret_api = RET_NORMAL;
845         } else {
846             /* Semaphore lock failed */
847             ret_api = RET_ERROR;
848         }
849     } else {
850         /* Semaphore creation failed */
851         ret_api = RET_ERROR;
852     }
853
854     return(ret_api);
855 }
856
857 /*******************************************************************************
858  * MODULE    : VehicleSndMsg
859  * ABSTRACT  : Message transmission processing
860  * FUNCTION  : Send a message to the specified PNO
861  * ARGUMENT  : pno_src      : Source PNO
862  *           : pno_dest    : Destination PNO
863  *           : cid      : Command ID
864  *           : msg_len      : Message data body length
865  *           : *msg_data    : Pointer to message data
866  * NOTE      :
867  * RETURN    : RET_NORMAL    : Normal completion
868  *           : RET_ERRNOTRDY  : Destination process is not wakeup
869  *           : RET_ERRMSGFULL  : Message queue overflows
870  *           : RET_ERRPARAM    : Buffer size error
871  ******************************************************************************/
872 inline RET_API VehicleSndMsg(PNO pno_src, PNO pno_dest, CID cid, uint16_t msg_len, const void *msg_data) {
873     VEHICLE_MSG_BUF      msg_buf;    /* message buffer */
874     T_APIMSG_MSGBUF_HEADER  *msg_hdr;    /* Pointer to the message header */
875     RET_API          ret_api;    /* Return value */
876     PCSTR          thread_name;  /* Destination thread name        */
877
878
879     /* Internal debug log output */
880     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, 
881                   "+ [pno_src = 0x%x, pno_dest = 0x%x]", pno_src, pno_dest);
882
883     /* _CWORD71_ processing speed(Memset modification) */
884     /* Initializing the header of the message buffer */
885     memset(reinterpret_cast<void *>(&msg_buf.hdr), 0, sizeof(T_APIMSG_MSGBUF_HEADER));
886
887     /* Get pointer to send buffer */
888     msg_hdr = reinterpret_cast<T_APIMSG_MSGBUF_HEADER *>(reinterpret_cast<void *>(&msg_buf));
889
890     /*--------------------------------------------------------------*
891      *  Create message headers                  *
892      *--------------------------------------------------------------*/
893     msg_hdr->hdr.sndpno    = pno_src;    /* Source PNO */
894     msg_hdr->hdr.cid      = cid;    /* Command ID */
895     msg_hdr->hdr.msgbodysize  = msg_len;    /* Message data body length */
896
897     /*--------------------------------------------------------------*
898      *  Create message data                  *
899      *--------------------------------------------------------------*/
900     if ((0 != msg_data) && (0 != msg_len)) {
901         /* Set the message data */
902         memcpy(reinterpret_cast<void *>(msg_buf.data), msg_data, (size_t)msg_len);
903     }
904     /*--------------------------------------------------------------*
905      * Send messages                      *
906      *--------------------------------------------------------------*/
907     /* Get Thread Name from PNO */
908     if (pno_dest <= SYS_PNO_MAX) {
909         thread_name = POS_THREAD_NAME;
910     } else {
911         thread_name = _pb_CnvPno2Name(pno_dest);
912     }
913
914     if ((pno_dest <= SYS_PNO_MAX) && (pno_src <= SYS_PNO_MAX)) {
915         /* Internal debug log output */
916         FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "[LOG pno_dest = 0x%x]", pno_dest);
917
918         /* Internal Process Transmission and Reception Messages */
919         ret_api = _pb_SndMsg(pno_dest,
920                              (uint16_t)(sizeof(T_APIMSG_MSGBUF_HEADER) + msg_len),
921                              reinterpret_cast<void *>(&msg_buf), 0);
922     } else {
923         /* External Process Transmission and Reception Messages */
924         ret_api = _pb_SndMsg_Ext(thread_name,
925                                  cid,
926                                  (uint16_t)(msg_len),  /* Ignore->MISRA-C++:2008 Rule 5-0-5 */
927                                  reinterpret_cast<void *>(&(msg_buf.data)), 0);
928     }
929     FRAMEWORKUNIFIEDLOG(ZONE_26, __FUNCTION__, "-");
930
931     return(ret_api);
932 }
933
934 /**
935  * @brief
936  *  Resource Acquisition Decision
937  *
938  * @param[in] none
939  *
940  * @return  TRUE       Normal<br>
941  *          FALSE      Abnormality(Insufficient resource)
942  */
943 inline BOOL VehicleGetResource(void) {
944     BOOL ret[4] = {TRUE, TRUE, TRUE, TRUE};
945     uint8_t idx;
946
947     ret[1] = _pb_GetMsgResource();
948     ret[2] = _pb_GetMutexResource();
949     ret[3] = _pb_GetOtherResource();
950
951     for (idx = 1; idx < 4; idx++) {
952         if (ret[idx] == FALSE) {
953             ret[0] = FALSE;
954         }
955     }
956
957     return ret[0];
958 }
959
960 /**
961  * @brief
962  *  Resource release
963  *
964  * @param[in] none
965  *
966  * @return  none
967  */
968 inline void VehicleReleaseResource(void) {
969     _pb_ReleaseMsgResource();
970     _pb_ReleaseMutexResource();
971     _pb_ReleaseOtherResource();
972
973     return;
974 }
975
976 inline uint32_t GetTid(void) {
977     return (uint32_t)syscall(__NR_gettid);
978 }
979
980
981 #endif  // POSITIONING_CLIENT_INCLUDE_POS_PRIVATE_H_