Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / _pbEvent.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
19  *    _pbEvent.cpp
20  */
21
22 /*---------------------------------------------------------------------------------*
23  * Include Files                                                                   *
24  *---------------------------------------------------------------------------------*/
25 #include <vehicle_service/positioning_base_library.h>
26 #include "_pbEvent_Internal.h"
27 #include "_pbInternalProc.h"
28 #include "WPF_STD_private.h"
29 #include "tchar.h"
30
31 /*---------------------------------------------------------------------------------*
32  * Internal Function Prototype                                                     *
33  *---------------------------------------------------------------------------------*/
34 static BOOL    FindEventTable(PB_EVENT*, TCHAR*, u_int32*);
35 static BOOL    AllocNewEventTable(PB_EVENT*, u_int32*);
36
37 static void       FreeEventTable(PB_EVENT* p_event_table, int index);
38 static RET_API    SetProc(PB_EVENT_OPEN_HANDLE* p_event_open, int32 i_mode, int32 l_val, int32* lp_val);
39 static RET_API    WaitProc(PB_EVENT_OPEN_HANDLE*, WAITING_CONDITION*, u_int32);
40 static BOOL       CheckCondition(PB_EVENT* p_sys_event, DWORD wcn, int32 l_event_data);
41
42 static EventID    EventCreateNewEventInSystem(u_int8, int32, TCHAR*);
43 static EventID    EventCreateNewEventInProcess(u_int32);
44
45 static RET_API    EventSendSignal(PB_EVENT_OPEN_HANDLE*, u_int32);
46 static RET_API    EventWaitForSignal(PB_EVENT_OPEN_HANDLE*, u_int32, u_int32);
47
48 static BOOL EventCreateMutex(PB_EVENT_OPEN_HANDLE*);
49 static void    EventLockMutex(PB_EVENT_OPEN_HANDLE*);
50 static void    EventUnlockMutex(PB_EVENT_OPEN_HANDLE*);
51 static void    EventDeleteMutex(PB_EVENT_OPEN_HANDLE*);
52
53 void GetDebugEventMngTblSysEvent(void* p_buf, PB_EVENT* p_evt, uint8_t* p_indent);
54
55 /*---------------------------------------------------------------------------------*
56  * Grobal Value                                                                    *
57  *---------------------------------------------------------------------------------*/
58 static PB_EVENT_INSTANCE g_instance;  // NOLINT(readability/nolint)
59
60 static uint8_t g_my_proc_cnt; /* Invoking process counter value */
61
62 /*---------------------------------------------------------------------------------*
63  * Function                                                                        *
64  *---------------------------------------------------------------------------------*/
65 /**
66  * @brief
67  *   Initializing Event-Related Processing
68  *
69  *   Instantiate and initialize system API event related processing.
70  *   Creates a flags in the CLS event library.
71  *
72  * @return    RET_NORMAL    Normal completion<br>
73  *            RET_ERRINIT    Initialization error
74  */
75 RET_API EventInit(void) {
76     PB_EVENT_INSTANCE    *p_inst      = &g_instance;
77     PB_EVENT*     p_event_table        = NULL;
78     u_int32       ul_share_mem_size     = 0;
79     RET_API       ret_api            = RET_ERROR;
80     char          c_share_mem_name[32]  = {0};
81     char          c_sem_name[32]       = {0};
82     void          *pv_share_mem_addr    = NULL;
83     int32 n;
84     RET_API        l_ret_api;
85
86     /* Create Mutex */
87     _tcscpy(c_sem_name, "POS_BASE_EVENT_MUTEX");
88     p_inst->id_event_table_sem = _pb_CreateSemaphore(c_sem_name);  // LCOV_EXCL_BR_LINE 200: can not be 0
89     if (p_inst->id_event_table_sem == 0) /* When mutex creation fails */ {  // LCOV_EXCL_BR_LINE 200: can not be 0
90         // LCOV_EXCL_START 200: can not be 0
91         AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
92         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
93             "_pb_CreateSemaphore ERROR [name:%s]", c_sem_name);
94         _pb_Exit();
95         // LCOV_EXCL_STOP
96     }
97
98     l_ret_api = _pb_SemLock(p_inst->id_event_table_sem); /* Get event-control-table-locking Mutex */
99     if (l_ret_api != RET_NORMAL) {
100         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
101     }
102
103     /* Initialize table of handles. */
104     for (n = 0; n < MAX_PB_EVENTS; n++) {
105         p_inst->p_handle_table[n] = NULL;
106     }
107
108     /* Generate shared memory name */
109     _tcscpy(c_share_mem_name, "POS_BASE_EVENT_TABLE");
110
111     /* Link to event information storage area    */
112     ret_api = _pb_LinkShareData(c_share_mem_name, &pv_share_mem_addr, &ul_share_mem_size);
113     if (ret_api != RET_NORMAL) /* When the link fails    */ {
114         /* Generate shared memory */
115         ret_api = _pb_CreateShareData(c_share_mem_name,
116             static_cast<u_int32>((sizeof(PB_EVENT) * MAX_PB_EVENTS)), &pv_share_mem_addr);
117
118         /* Terminate processing when generating fails */
119         if (ret_api != RET_NORMAL) {
120             ret_api = RET_ERRINIT;    /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
121         } else {
122             /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
123             /* Event information storage area initialization processing */
124             p_event_table = reinterpret_cast<PB_EVENT*>(pv_share_mem_addr);
125             for (n = 0; n < MAX_PB_EVENTS; n++) {
126                 memset(reinterpret_cast<void *>(p_event_table[n].event_name), 0, \
127                     sizeof(p_event_table[n].event_name));
128                 p_event_table[n].l_event_val = 0;
129                 for (int wcn = 0; wcn < MAX_PB_EVENT_WAIT_THREADS; wcn++) {
130                     p_event_table[n].st_condition[wcn].uc_use_flag    = FALSE;    /* Initialize to unused */
131                     p_event_table[n].st_condition[wcn].uc_waiting    = FALSE;
132                     p_event_table[n].st_condition[wcn].us_mode    = 0;
133                     p_event_table[n].st_condition[wcn].ul_mask    = 0;        /* Initialize Mask Value */
134                     p_event_table[n].st_condition[wcn].l_min_val    = 0;
135                     p_event_table[n].st_condition[wcn].l_max_val    = 0;
136                     /* Initialize event values at WaitEvent Returns */
137                     p_event_table[n].st_condition[wcn].l_last_val    = 0;
138                     p_event_table[n].st_condition[wcn].flag_id[p_event_table->proc_cnt] = 0;
139                 }
140
141                 p_event_table[n].l_process_ref     = 0;
142                 p_event_table[n].l_reset_data     = 0;
143                 p_event_table[n].uc_manual_reset = _CWORD64_EVENT_MANUALRESET_OFF;
144             }
145         }
146     } else {
147         /* When the link is successful */
148         p_event_table = reinterpret_cast<PB_EVENT*>(pv_share_mem_addr);
149         p_event_table->proc_cnt++;
150     }
151
152     if (ret_api == RET_NORMAL)    /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */ {
153         g_my_proc_cnt = p_event_table->proc_cnt;
154         /* Get the address of the acquired event information storage area. */
155         p_inst->h_shared_memory = (HANDLE)pv_share_mem_addr;
156         p_inst->p_event_table = reinterpret_cast<PB_EVENT*>(pv_share_mem_addr);
157     }
158
159     _pb_SemUnlock(p_inst->id_event_table_sem);  // LCOV_EXCL_BR_LINE 200: no branch
160
161     return ret_api;
162 }
163
164 /**
165  * @brief
166  *   Event-related instance destruction processing
167  *
168  *   Delete a Flag from the CLS Event Library (Not implemented)
169  *
170  * @return    RET_NORMAL    Normal completion<br>
171  *            RET_ERROR    ABEND
172  */
173 RET_API EventTerm(void) {  // LCOV_EXCL_START 8:dead code
174     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
175     PB_EVENT_INSTANCE        *p_inst = &g_instance;
176     char    c_share_mem_name[32] = {0};
177
178     RET_API    ret_api = RET_NORMAL;
179
180     /* TODO:Implement processing to delete event flags */
181
182     /* Generate shared memory name */
183     _tcscpy(c_share_mem_name, "POS_BASE_EVENT_TABLE");
184
185     /* Discard the semaphore if it has already been created */
186     if (p_inst->id_event_table_sem != 0) {
187         PbDeleteSemaphore(p_inst->id_event_table_sem);
188         p_inst->id_event_table_sem = 0;
189     }
190
191     /* Discard the shared memory if it has already been created */
192     if (p_inst->h_shared_memory != NULL) {
193         /* Release shared memory */
194         PbDeleteShareData(c_share_mem_name);
195         p_inst->h_shared_memory = NULL;
196     }
197
198     return ret_api;
199 }
200 // LCOV_EXCL_STOP
201
202 /**
203  * @brief
204  *   Create the event
205  *
206  *   Create an event with the specified name and returns the event ID.<br>
207  *    If it has already been generated, the event ID is searched and returned.
208  *
209  * @param[in]    uc_manual_reset
210  * @param[in]    l_init_data
211  * @param[in]    *cp_event_name    Pointer to the names of the event to be generated (NULL termination string)
212  *
213  * @return    Non-zero    Generated event ID<br>
214  *            0        Event generation error
215  */
216 EventID _pb_CreateEvent(u_int8 uc_manual_reset, int32 l_init_data,  // NOLINT(readability/nolint)
217         char *cp_event_name) {  // NOLINT(readability/nolint)
218     PB_EVENT_INSTANCE    *p_inst = &g_instance;
219     EventID ret_event_id = 0;
220     TCHAR    *p_event_name    = NULL;
221     u_int32    index        = 0;
222     BOOL    bret         = FALSE;
223     BOOL    check_status    = TRUE; /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
224     RET_API    l_ret_api;
225
226     /* Parameter check */
227     if ((cp_event_name == NULL) ||
228             (uc_manual_reset >= _CWORD64_EVENT_MANUALRESET_MAX)) {
229         check_status    = FALSE; /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
230     }
231
232 #ifdef UNICODE
233     /* Event name character limit processing */
234     if (strlen(cp_event_name) > MAX_EVENT_NAME_LEN) {
235         _pb_Exit();  /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
236     }
237
238     TCHAR unicodeEventName[MAX_EVENT_NAME_LEN + 1];    /* Maxmum nunber of characters + NULL area */
239     mbstowcs(unicodeEventName, cp_event_name, MAX_EVENT_NAME_LEN);
240     p_event_name = unicodeEventName;
241 #else
242     p_event_name = cp_event_name;
243 #endif  // UNICODE
244
245     if ((check_status == TRUE) && /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
246             (p_event_name[0] == __TEXT('\0'))) {
247         check_status    = FALSE; /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
248     }
249
250     /* Event name character limit processing */
251     if ((check_status == TRUE) && /* _CWORD71_:QAC++4020:Multiple exit points found. MISRA-C++ Rule 6-6-5 */
252             (_tcslen(p_event_name) < MAX_EVENT_NAME_LEN)) {
253         l_ret_api = _pb_SemLock(p_inst->id_event_table_sem);                    /* Mutex from here */
254         if (l_ret_api != RET_NORMAL) {
255             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
256         }
257
258         /* Search the event table by the specified event name */
259         bret = FindEventTable(p_inst->p_event_table, p_event_name, &index);
260         /* If the same event already exists on the system */
261         if (bret != FALSE) {
262             ret_event_id = EventCreateNewEventInProcess(index);  // LCOV_EXCL_BR_LINE 200: no branch
263         } else {
264             /* When creating a new file */
265             ret_event_id = EventCreateNewEventInSystem(uc_manual_reset, l_init_data, p_event_name);  // LCOV_EXCL_BR_LINE 200: no branch  // NOLINT(whitespace/line_length)
266         }
267
268         _pb_SemUnlock(p_inst->id_event_table_sem);  // LCOV_EXCL_BR_LINE 200: no branch
269     }
270
271     return ret_event_id;
272 }
273
274 /**
275  * @brief
276  *   Set the event
277  *
278  *   Set the event value by specifying the event ID acquired when the event was created.<br>
279  *    The event value setting modes are as follows.<br>
280  *      SAPI_EVSET_ABSOLUTE    : Absolute value setting(Specify the value to be set.)<br>
281  *      SAPI_EVSET_RELATE        : Relative value setting(Specifies the value relative to the current value.)
282  *
283  * @param[in]    event_id    Specify the event ID for which the event value is to be set.
284  * @param[in]    l_set_mode    Specify the event value setting mode
285  * @param[in]    l_Val        Specify the event value to be set
286  *
287  * @return    RET_NORMAL        Normal completion<br>
288  *            RET_ERRPARAM    Configuration mode error<br>
289  *            RET_EV_NONE        Specified event does not exist<br>
290  *            RET_EV_MAX        The set event value exceeds the maximum value<br>
291  *            RET_EV_MIN        The set event value is below the minimum value.
292  */
293 RET_API _pb_SetEvent(EventID event_id, int32 l_set_mode, int32 l_val) {   // NOLINT(readability/nolint)
294     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
295     PB_EVENT_INSTANCE        *p_inst        = &g_instance;
296     RET_API        ret_sts     = RET_EV_NONE;
297     int32        l_work_val = 0;
298     u_int32        ul_index     = (u_int32)event_id - 1;
299
300     /* Parameter check */
301     if (ul_index < MAX_PB_EVENTS) {
302         /* If the specified event ID value is within range */
303         p_event_open = p_inst->p_handle_table[ul_index];
304         /* If the specified event ID is registered in the table, */
305         if (p_event_open != NULL) {
306             /* Determine the event setting mode and call the event value setting function. */
307             if (l_set_mode == SAPI_EVSET_ABSOLUTE) {
308                 ret_sts = SetProc(p_event_open, EVSET_ABSOLUTE, l_val, &l_work_val);  // LCOV_EXCL_BR_LINE 200: no branch  // NOLINT(whitespace/line_length)
309             } else if (l_set_mode == SAPI_EVSET_RELATE) {
310                 ret_sts = SetProc(p_event_open, EVSET_RELATE, l_val, &l_work_val);  // LCOV_EXCL_BR_LINE 200: no branch
311             } else {
312                 ret_sts = RET_ERRPARAM;
313             }
314         }
315     }
316
317     return ret_sts;
318 }
319
320 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
321  * MODULE    : SetandEvent()
322  * ABSTRACT  : Event value AND setting process
323  * NOTE      : Set the logical AND result of the specified mask value to the event value of the specified event ID.
324  * ARGUMENT  : EventID        event_id        Specify the event ID to wait for an event
325  *           : u_int32        ul_mask        Mask value to be logically ANDed with the event value
326  *           : int32*        pl_val        Pointer to the area to store the pre-event value
327  * RETURN    : RET_API    RET_NORMAL        Normal completion
328  *           :             RET_EV_NONE        Specified event does not exist
329  *           :             RET_ERROR
330  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
331 RET_API
332 SetandEvent(EventID event_id, u_int32 ul_mask, int32* pl_val) {  // LCOV_EXCL_START 8:dead code
333     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
334     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
335     PB_EVENT_INSTANCE        *p_inst         = &g_instance;
336     RET_API        ret_sts     = RET_EV_NONE;
337     u_int32        ul_index     = (u_int32)event_id - 1;
338
339     /* Parameter check */
340     if ((ul_index < MAX_PB_EVENTS) &&
341             (pl_val != NULL)) {
342         /* If the specified event ID value is within range */
343         p_event_open = p_inst->p_handle_table[ul_index];
344         /* If the specified event ID is registered in the table, */
345         if (p_event_open != NULL) {
346             ret_sts = SetProc(p_event_open, EVSET_AND, static_cast<int32>(ul_mask), pl_val);
347         }
348     }
349
350     return ret_sts;
351 }
352 // LCOV_EXCL_STOP
353
354 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
355  * MODULE    : SetorEvent()
356  * ABSTRACT  : Event value OR setting process
357  * NOTE      : Set the logical OR result of the specified mask value and the event value of the specified event ID.
358  * ARGUMENT  : EventID        event_id        Specify the event ID to wait for an event.
359  *           : u_int32        ul_mask        Mask value to be logically ANDed with the event value
360  *           : int32*        pl_val        Pointer to the area to store the pre-event value
361  * RETURN    : RET_API    RET_NORMAL        Normal completion
362  *           :             RET_EV_NONE        Specified event does not exist
363  *           :             RET_ERROR
364  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
365 RET_API
366 SetorEvent(EventID event_id, u_int32 ul_mask, int32* pl_val) {  // LCOV_EXCL_START 8:dead code
367     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
368     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
369     PB_EVENT_INSTANCE        *p_inst        = &g_instance;
370     RET_API        ret_sts     = RET_EV_NONE;
371     u_int32        ul_index     = (u_int32)event_id - 1;
372
373     /* Parameter check */
374     if ((ul_index < MAX_PB_EVENTS) &&
375             (pl_val != NULL)) {
376         /* If the specified event ID value is within range */
377         p_event_open = p_inst->p_handle_table[ul_index];
378
379         /* If the specified event ID is registered in the table, */
380         if (p_event_open != NULL) {
381             ret_sts = SetProc(p_event_open, EVSET_OR, static_cast<int32>(ul_mask), pl_val);
382         }
383     }
384
385     return ret_sts;
386 }
387 // LCOV_EXCL_STOP
388
389 /**
390  * @brief
391  *   Wait for the event
392  *
393  *   Wait until the event value of the specified event ID reaches the specified range.
394  *
395  * @param[in]    event_id        Specify the event ID for which the event value is to be set.
396  * @param[in]    l_wait_mode        Monitoring mode of event * Current only SAPI_EVWAIT_VAL is allowed
397  * @param[in]    l_min_val            Minimum Event Wait
398  * @param[in]    l_max_val        Maximum value waiting for an event
399  * @param[in]    *pl_event_val    Pointer to the event value storage area after waiting for an event
400  * @param[in]    ul_mill_sec_time    Timeout period(ms)
401  *
402 _ * @return    RET_NORMAL        Normal completion<br>
403  *            RET_EV_NONE        Specified event does not exist<br>
404  *            RET_ERROR        Other errors
405  */
406 RET_API _pb_WaitEvent(EventID event_id, int32 l_wait_mode, int32 l_min_val,   // NOLINT(readability/nolint)
407         int32 l_max_val, int32* pl_event_val, u_int32 ul_mill_sec_time) {     // NOLINT(readability/nolint)
408     PB_EVENT_OPEN_HANDLE     *p_event_open = NULL;
409     PB_EVENT_INSTANCE        *p_inst        = &g_instance;
410     WAITING_CONDITION        st_condition = {0};
411     RET_API        ret_sts     = RET_EV_NONE;
412     u_int32        ul_index     = (u_int32)event_id - 1;
413
414     /* Parameter check */
415     if ((pl_event_val != NULL) &&
416             (ul_index < MAX_PB_EVENTS)) {
417         p_event_open = p_inst->p_handle_table[ul_index];
418
419         /* If the specified event ID is registered in the table, */
420         if (p_event_open != NULL) {
421             /* Set Wait Mode and Mask Value to Parameter Blk */
422             st_condition.us_mode  = EVWAIT_VAL;
423             st_condition.l_min_val = l_min_val;
424             st_condition.l_max_val = l_max_val;
425
426             /* Call the event wait processing */
427             ret_sts = WaitProc(p_event_open, &st_condition, ul_mill_sec_time);  // LCOV_EXCL_BR_LINE 200: no branch
428             if (ret_sts == RET_NORMAL) {
429                 *pl_event_val = st_condition.l_last_val;
430             }
431         }
432     }
433
434     return ret_sts;
435 }
436
437 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
438  * MODULE    : WaitallclrEvent()
439  * ABSTRACT  : Event Bit Clear Wait
440  * NOTE      : Wait until all the bits specified by the mask value are cleared
441  *           : for the event value of the specified event ID.
442  * ARGUMENT  : EventID    event_id            Specifies the event ID to wait for an event.
443  *           : u_int32    ul_mask            Mask value waiting for an event (Bit pattern)
444  *           : int32*    pl_val            Pointer to the event value storage area after waiting for an event
445  *           : u_itn32    ul_mill_sec_time    Timeout period(ms)
446  * RETURN    : RET_API    RET_NORMAL        Normal completion
447  *           :             RET_EV_NONE        Specified event does not exist
448  *           :             RET_ERROR        Maximum number of waiting threads exceeded
449  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
450 RET_API
451 WaitallclrEvent(EventID event_id, u_int32 ul_mask, int32* pl_val, u_int32 ul_mill_sec_time) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
452     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
453     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
454     PB_EVENT_INSTANCE        *p_inst        = &g_instance;
455     WAITING_CONDITION        st_condition = {0};
456     RET_API        ret_sts     = RET_EV_NONE;
457     u_int32        ul_index     = static_cast<u_int32>(event_id) - 1;
458
459     /* Parameter check */
460     if ((pl_val != NULL) &&
461             (ul_index < MAX_PB_EVENTS)) {
462         p_event_open = p_inst->p_handle_table[ul_index];
463
464         /* If the specified event ID is registered in the table, */
465         if (p_event_open != NULL) {
466             /* Set Wait Mode and Mask Value to Parameter Blk */
467             st_condition.us_mode = EVWAIT_ALLCLR;
468             st_condition.ul_mask = ul_mask;
469
470             /* Call the event wait processing */
471             ret_sts = WaitProc(p_event_open, &st_condition, ul_mill_sec_time);
472             if (ret_sts == RET_NORMAL) {
473                 *pl_val = st_condition.l_last_val;
474             }
475         }
476     }
477
478     return ret_sts;
479 }
480 // LCOV_EXCL_STOP
481
482 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
483  * MODULE    : WaitanysetEvent()
484  * ABSTRACT  : Event Bit Set Waiting Process
485  * NOTE      : Wait until one of the bits specified by the mask value is set
486  *           : for the event value of the specified event ID.
487  * ARGUMENT  : EventID        event_id            Specify the event ID to wait for an event.
488  *           : u_int32        ul_mask            Mask value waiting for an event
489  *           : int32*        ipVal            Pointer to the event value storage area after waiting for an event
490  *           : u_itn32        ul_mill_sec_time    Timeout period(ms)
491  * RETURN    : RET_API    RET_NORMAL        Normal completion
492  *           : RET_EV_NONE        Specified event does not exist
493  *           : RET_ERROR        When the maximum number of waiting events is exceeded
494  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
495 RET_API
496 WaitanysetEvent(EventID event_id, u_int32 ul_mask, int32* pl_val, u_int32 ul_mill_sec_time) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
497     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
498     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
499     PB_EVENT_INSTANCE        *p_inst        = &g_instance;
500     WAITING_CONDITION        st_condition = {0};
501     RET_API        ret_sts     = RET_EV_NONE;
502     u_int32        ul_index     = (u_int32)event_id - 1;
503
504     /* Parameter check */
505     if ((pl_val != NULL) &&
506             (ul_index < MAX_PB_EVENTS)) {
507         p_event_open = p_inst->p_handle_table[ul_index];
508
509         /* If the specified event ID is registered in the table, */
510         if (p_event_open != NULL) {
511             /* Set Wait Mode and Mask Value to Parameter Blk */
512             st_condition.us_mode = EVWAIT_ANYSET;
513             st_condition.ul_mask = ul_mask;
514
515             /* Call the event wait processing */
516             ret_sts = WaitProc(p_event_open, &st_condition, ul_mill_sec_time);
517             if (ret_sts == RET_NORMAL) {
518                 *pl_val = st_condition.l_last_val;
519             }
520         }
521     }
522
523     return ret_sts;
524 }
525 // LCOV_EXCL_STOP
526
527 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
528  * MODULE    : LookupEvent()
529  * ABSTRACT  : Event value reading process
530  * NOTE      : Read the event value of the specified event ID.
531  * ARGUMENT  : EventID    event_id            Specify the event ID to read the event value from.
532  *           : int32    *iEventVal        Pointer to the read event value storage area
533  * RETURN    : RET_API    RET_NORMAL        Normal completion
534  *           :             RET_EV_NONE        Specified event does not exist
535  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
536 RET_API
537 LookupEvent(EventID event_id, int32* pl_event_val) {  // LCOV_EXCL_START 8:dead code
538     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
539     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
540     PB_EVENT_INSTANCE        *p_inst = &g_instance;
541     u_int32        ul_index     = (u_int32)event_id - 1;
542     RET_API        ret_sts     = RET_EV_NONE;
543
544     /* Parameter check */
545     if ((pl_event_val != NULL) &&
546             (ul_index < MAX_PB_EVENTS)) {
547         p_event_open = p_inst->p_handle_table[ul_index];
548
549         /* When the specified event ID is already registered in the table */
550         if (p_event_open != NULL) {
551             EventLockMutex(p_event_open);
552             /* Store the current event value. */
553             *pl_event_val = static_cast<int32>(p_event_open->p_sys_event->l_event_val);
554             EventUnlockMutex(p_event_open);
555             ret_sts = RET_NORMAL;
556         }
557     }
558
559     return ret_sts;
560 }
561 // LCOV_EXCL_STOP
562
563 /**
564  * @brief
565  *    Delete the event
566  *
567  *   Delete the event with the specified event ID.
568  *
569  * @param[in]    event_id        Specify the event ID for which the event value is to be set.
570  *
571  * @return    RET_NORMAL        Normal completion<br>
572  *            RET_EV_NONE        Specified event does not exist
573  */
574 RET_API _pb_DeleteEvent(EventID event_id) {   // NOLINT(readability/nolint)
575     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
576     PB_EVENT_INSTANCE       *p_inst         = &g_instance;
577     RET_API         ret_api = RET_EV_NONE;
578     u_int32         index = static_cast<u_int32>(event_id) - 1;
579     EV_ERR          ev_err;
580     RET_API         l_ret_api;
581
582     /* Parameter check */
583     if (index < MAX_PB_EVENTS) {
584         p_event_open = p_inst->p_handle_table[index];
585         /* When the specified event ID is registered in the table */
586         if (p_event_open != NULL) {
587             ret_api = RET_NORMAL;
588         }
589     }
590
591     /* Parameter normal */
592     if (ret_api == RET_NORMAL) {
593         l_ret_api = _pb_SemLock(p_inst->id_event_table_sem);
594         if (l_ret_api != RET_NORMAL) {
595             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_SemLock failed");
596         }
597
598         /* When no one references in the same process */
599         if ((p_event_open->l_thread_ref - 1) <= 0) {
600             /* Delete event flag */
601             ev_err = EV_destroy_flag(p_event_open->p_sys_event->st_condition[0].flag_id[g_my_proc_cnt]);
602             /* When initialization fails */
603             if (ev_err == EV_OK) {
604                 p_event_open->p_sys_event->st_condition[0].flag_id[g_my_proc_cnt] = 0;
605             } else {
606                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
607                     "EV_destroy_flag ERROR!! [ev_err=%d, flag_id=0x%x]", \
608                     ev_err, p_event_open->p_sys_event->st_condition[0].flag_id[g_my_proc_cnt]);
609
610                 ret_api = RET_ERROR;
611             }
612         }
613
614         /* When the event flag is deleted successfully */
615         if (ret_api == RET_NORMAL) {
616             /* Reduce the number of event references in the same process */
617             p_event_open->l_thread_ref--;
618
619             /* When no one references in the same process */
620             if (p_event_open->l_thread_ref <= 0) {
621                 /* Reduce the number of event references in the system */
622                 p_event_open->p_sys_event->l_process_ref--;
623             }
624
625             /* When no one references in the system */
626             if (p_event_open->p_sys_event->l_process_ref <= 0) {
627                 /* Initialization of the target area */
628                 FreeEventTable(p_inst->p_event_table, index);
629             }
630
631             /* If no one references in the same process, release the resource here */
632             if (p_event_open->l_thread_ref <= 0) {
633                 /* Exclusive deletion for the target event */
634                 EventDeleteMutex(p_event_open);
635
636                 /* Open the heap area storing the target event. */
637                 /*  */
638                 PbProcessHeapFree(0, p_inst->p_handle_table[index]);
639                 p_inst->p_handle_table[index] = NULL;
640             }
641         }
642
643         _pb_SemUnlock(p_inst->id_event_table_sem);
644     }
645
646     return ret_api;
647 }
648
649 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
650  * MODULE    : ResetEvent()
651  * ABSTRACT  : Event Clear
652  * NOTE      : Specified event clear processing
653  * ARGUMENT  : EventID    event_id            Event ID to reset
654  * RETURN    : RET_API    RET_NORMAL        Normal completion
655  *           :             RET_EV_NONE        ABEND
656  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
657 RET_API
658 ResetEvent(EventID event_id) {  // LCOV_EXCL_START 8:dead code
659     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
660     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
661     PB_EVENT_INSTANCE       *p_inst       = &g_instance;
662     RET_API        ret_sts   = RET_EV_NONE;
663     u_int32        ul_index  = (u_int32)event_id - 1;
664
665     /* Parameter check */
666     if (ul_index < MAX_PB_EVENTS) {
667         p_event_open = p_inst->p_handle_table[ul_index];
668
669         /* When the specified event ID is already registered in the table */
670         if (p_event_open != NULL) {
671             EventLockMutex(p_event_open);
672
673             /* Clear the event value */
674             p_event_open->p_sys_event->l_event_val = p_event_open->p_sys_event->l_reset_data;
675
676             EventUnlockMutex(p_event_open);
677             ret_sts = RET_NORMAL;
678         }
679     }
680
681     return ret_sts;
682 }
683 // LCOV_EXCL_STOP
684
685 /* Private functions. */
686
687 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
688  * MODULE    : SetProc()
689  * ABSTRACT  : General Event Configuration Processing
690  * NOTE      : Sets the event according to the specified event setting method.
691  * ARGUMENT  : PB_EVENT_OPEN_HANDLE*    p_event_open    Pointer to manage event waiting for the event TBL
692  *           : int32                    i_mode        Event setting method
693  *           : int32                    iVal        Event setting value
694  *           : int32*                    ipVal        Pointer to the area to store the pre-event value
695  * RETURN    : RET_API    RET_NORMAL        Normal completion
696  *           :             RET_ERROR
697  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
698 static RET_API
699 SetProc(PB_EVENT_OPEN_HANDLE* p_event_open, int32 i_mode, int32 l_val, int32* lpVal) {
700     RET_API    ret_sts         = RET_NORMAL;
701     int32    lTempEventData    = 0;
702     int32     lTestValue       = 0;
703     BOOL    bCastCondFlag      = FALSE;
704
705     EventLockMutex(p_event_open);
706
707     /* Get current event value */
708     lTempEventData = p_event_open->p_sys_event->l_event_val;
709     *lpVal           = p_event_open->p_sys_event->l_event_val;                /* Set the value before the event operation */
710
711     /* Switch Processing by event configuration mode */
712     switch (i_mode) {  // LCOV_EXCL_BR_LINE 200:only the first two cases will be called
713         case EVSET_ABSOLUTE:                     /* In absolute mode */ {
714             /* Updating event values with specified values */
715             lTempEventData = l_val;
716             break;
717         }
718         case EVSET_RELATE:                         /* In relative setting mode */ {
719             lTestValue = lTempEventData + l_val;
720             /* Exceeding representable event value */
721             if ((l_val > 0) && (lTempEventData > lTestValue)) {
722                 ret_sts = RET_EV_MAX;
723             }
724             /* Below representable event value */
725             if ((l_val < 0) && (lTempEventData < lTestValue)) {
726                 ret_sts = RET_EV_MIN;
727             }
728             /* Normal range */
729             if (ret_sts == RET_NORMAL) {
730                 /* Add specified value to event value */
731                 lTempEventData += l_val;
732             }
733             break;
734         }
735         case EVSET_AND: {  // LCOV_EXCL_BR_LINE 200: i_mode cannot be this value
736             // LCOV_EXCL_START 200: i_mode cannot be this value
737             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
738             lTempEventData &= ((u_int32)l_val);    /* Logical AND of the event value and the specified value */
739             break;
740             // LCOV_EXCL_STOP
741         }
742         case EVSET_OR: {  // LCOV_EXCL_BR_LINE 200: i_mode cannot be this value
743             // LCOV_EXCL_START 200: i_mode cannot be this value
744             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
745             lTempEventData |= ((u_int32)l_val);    /* Logical AND of the event value and the specified value */
746             break;
747             // LCOV_EXCL_STOP
748         }
749         default:  /* Event setting mode error */  // LCOV_EXCL_BR_LINE 200: i_mode cannot be this value
750             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
751             ret_sts = RET_ERRPARAM;    // LCOV_EXCL_LINE 200: i_mode cannot be this value
752     }
753
754     /* When the manual reset function is enabled */
755
756     if (ret_sts == RET_NORMAL) {
757         /* When the manual reset function is enabled */
758         if (p_event_open->p_sys_event->uc_manual_reset == _CWORD64_EVENT_MANUALRESET_ON) {
759             /* Set event value */
760             p_event_open->p_sys_event->l_event_val = lTempEventData;
761         }
762
763         /* Loop for the maximum number of waiting threads per event and check the condition of event wait processing of the state TBL. */
764         for (DWORD wcn = 0; wcn < MAX_PB_EVENT_WAIT_THREADS; wcn++) {
765             /* If the event wait flag is waiting, */
766             if (p_event_open->p_sys_event->st_condition[wcn].uc_waiting == TRUE) {
767                 /* Check if event wait conditions are met */
768                 BOOL bret = CheckCondition(p_event_open->p_sys_event, wcn, lTempEventData);
769                 /* If the event wait conditions are met, */
770                 if (bret == TRUE) {
771                     bCastCondFlag = TRUE;
772                     /* Save the event value at the time of SetEvent issuance (at the time of WaitEvent return). */
773                     p_event_open->p_sys_event->st_condition[wcn].l_last_val = lTempEventData;
774
775                     /* Processing to prevent concurrent SetEvent from more than one threads for a single event which is in WAIT state */
776                     /* Set WAIT status to wait-canceled */
777                     p_event_open->p_sys_event->st_condition[wcn].uc_waiting = FALSE;
778                     /* Setting the default min value for event */
779                     p_event_open->p_sys_event->st_condition[wcn].l_min_val = MIN_EVENT_VAL;
780                     /* Setting the default max event */
781                     p_event_open->p_sys_event->st_condition[wcn].l_max_val = MAX_EVENT_VAL;
782
783                     /* Signal issuance */
784                     (void)EventSendSignal(p_event_open, static_cast<int>(wcn));
785                 }
786             }
787         }
788
789         /* When the manual reset function is disabled */
790         if (p_event_open->p_sys_event->uc_manual_reset != _CWORD64_EVENT_MANUALRESET_ON) {
791             /* If no one has issued the event */
792             if (bCastCondFlag == FALSE) {
793                 /* Set event value */
794                 p_event_open->p_sys_event->l_event_val = lTempEventData;
795             } else {
796                 /* If issued event */
797                 /* Reset event value */
798                 p_event_open->p_sys_event->l_event_val = p_event_open->p_sys_event->l_reset_data;
799             }
800         }
801     }
802
803     EventUnlockMutex(p_event_open);
804
805     return ret_sts;
806 }
807
808 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
809  * MODULE    : WaitProc()
810  * ABSTRACT  : Generic Event Wait Processing
811  * NOTE      : Wait for an event according to the wait queue of the specified event.
812  * ARGUMENT  : PB_EVENT_OPEN_HANDLE*    p_event_open    Pointer to TBL which is managed waiting for events
813  *           : WAITING_CONDITION*        st_condition    Pointer to the event wait condition setting parameter
814  * RETURN    : RET_API    RET_NORMAL        Normal completion
815  *           :             RET_ERROR        The maximum number of waits has been exceeded, or a parameter error has occurred.
816  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
817 static RET_API
818 WaitProc(PB_EVENT_OPEN_HANDLE* p_event_open, WAITING_CONDITION* st_condition, u_int32 ul_mill_sec_time) {
819     RET_API        ret_sts = RET_ERROR;
820     u_int32        ul_wcn = 0;
821
822     /* Get semaphore for event table */
823     EventLockMutex(p_event_open);
824
825     /* Loop for the maximum number of waiting threads per event and retrieve free area of state TBL */
826     for (ul_wcn = 0; ul_wcn < MAX_PB_EVENT_WAIT_THREADS; ul_wcn++) {
827         if ((p_event_open->p_sys_event->st_condition[ul_wcn].uc_use_flag == FALSE) && \
828                 (p_event_open->p_sys_event->st_condition[ul_wcn].uc_waiting == FALSE)) {
829             /* For the unused state TBL */
830             /* If the event wait flag is released, */
831             /* finish searching when free area is found */
832             ret_sts = RET_NORMAL;
833             break;
834         }
835     }
836
837     /* If there is free space in the state TBL */
838     if (ret_sts == RET_NORMAL) {
839         /* Set wait rule for free space of state TBL */
840         /* Determine the wait rule */
841         switch (st_condition->us_mode) {  // LCOV_EXCL_BR_LINE 200: can not be EVWAIT_ALLCLR and EVWAIT_ANYSET
842             case EVWAIT_VAL:            /* For range waiting */
843             {
844                 /* Set event monitoring mode */
845                 p_event_open->p_sys_event->st_condition[ul_wcn].us_mode  = st_condition->us_mode;
846                 /* Set the minimum value for establishing an event */
847                 p_event_open->p_sys_event->st_condition[ul_wcn].l_min_val = st_condition->l_min_val;
848                 /* Set the maximum value for establishing an event */
849                 p_event_open->p_sys_event->st_condition[ul_wcn].l_max_val = st_condition->l_max_val;
850                 break;
851             }
852             case EVWAIT_ALLCLR:  /* If waiting for the specified bit to be cleared */  // LCOV_EXCL_BR_LINE 200: can not be EVWAIT_ALLCLR
853             case EVWAIT_ANYSET:  /* If waiting for the specified bit to be set */  // LCOV_EXCL_BR_LINE 200: can not be EVWAIT_ANYSET
854              {
855                 // LCOV_EXCL_START 200: can not be EVWAIT_ANYSET and EVWAIT_ALLCLR
856                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
857                 /* Set event monitoring mode */
858                 p_event_open->p_sys_event->st_condition[ul_wcn].us_mode = st_condition->us_mode;
859                 /* Set event wait mask value */
860                 p_event_open->p_sys_event->st_condition[ul_wcn].ul_mask = st_condition->ul_mask;
861                 break;
862                 // LCOV_EXCL_STOP
863             }
864             default:
865                 ret_sts = RET_ERROR;
866         }
867     }
868
869     /* When the specified mode is normal */
870     if (ret_sts == RET_NORMAL) {
871         /* Check if event wait conditions are met */
872         BOOL bret = CheckCondition(p_event_open->p_sys_event, ul_wcn, p_event_open->p_sys_event->l_event_val);
873
874         /* Target event received */
875         if (bret == TRUE) {
876             /* Set the received event value */
877             st_condition->l_last_val = p_event_open->p_sys_event->l_event_val;
878             /* Since it does not wait for an event, set the initial value to the state TBL. */
879             p_event_open->p_sys_event->st_condition[ul_wcn].uc_waiting = FALSE;
880             p_event_open->p_sys_event->st_condition[ul_wcn].uc_use_flag = FALSE;
881             /* Set the default minimum value for the event */
882             p_event_open->p_sys_event->st_condition[ul_wcn].l_min_val = MIN_EVENT_VAL;
883             /* Set the default maximum value for the event */
884             p_event_open->p_sys_event->st_condition[ul_wcn].l_max_val = MAX_EVENT_VAL;
885
886             /* When the manual reset function is disabled */
887             if (p_event_open->p_sys_event->uc_manual_reset != _CWORD64_EVENT_MANUALRESET_ON) {
888                 /* Initialize event values */
889                 p_event_open->p_sys_event->l_event_val = p_event_open->p_sys_event->l_reset_data;
890             }
891         } else {
892             /* When no event is received */
893             /* Set event wait state in free area of state TBL */
894             /* Set event wait flag to waiting */
895             p_event_open->p_sys_event->st_condition[ul_wcn].uc_waiting = TRUE;
896             /* Set table usage flag in use */
897             p_event_open->p_sys_event->st_condition[ul_wcn].uc_use_flag = TRUE;
898
899             /* Perform event wait */
900             ret_sts = EventWaitForSignal(p_event_open, ul_wcn, ul_mill_sec_time);
901             /* Set event wait flag to unused */
902             p_event_open->p_sys_event->st_condition[ul_wcn].uc_waiting = FALSE;
903             /* Set table usage flag to unused */
904             p_event_open->p_sys_event->st_condition[ul_wcn].uc_use_flag = FALSE;
905             /* Set the default minimum value for the event */
906             p_event_open->p_sys_event->st_condition[ul_wcn].l_min_val = MIN_EVENT_VAL;
907             /* Setting the default maximum value for the event */
908             p_event_open->p_sys_event->st_condition[ul_wcn].l_max_val = MAX_EVENT_VAL;
909             /* Set event return value */
910             st_condition->l_last_val = p_event_open->p_sys_event->st_condition[ul_wcn].l_last_val;
911         }
912     }
913
914     /* Release semaphore for event table */
915     EventUnlockMutex(p_event_open);
916
917     return ret_sts;
918 }
919
920 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
921  * MODULE    : FindEventTable()
922  * ABSTRACT  : Event Table Search Processing
923  * NOTE      : Search the event table for the specified event name and return the index number 
924  *           : of the event if it has been already registerd.
925  * ARGUMENT  : PB_EVENT    *p_event_table    Pointer to the start of event table array in the shared memory
926  *           : TCHAR        *ptcEventName    Event name to search
927  *           : u_int32*        puc_index        storage area for the index number of the specified event table
928  * RETURN    : BOOL            FALSE            No specified event
929  *           :                 TRUE            Specified Event Yes
930  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
931 static BOOL
932 FindEventTable(PB_EVENT* p_event_table, TCHAR* ptcEventName, u_int32* puc_index) {
933     u_int32    ul_index = 0;
934     BOOL       bret = FALSE;
935
936     for (ul_index = 0; ul_index < MAX_PB_EVENTS; ul_index++) {
937         if (_tcscmp(p_event_table[ul_index].event_name, ptcEventName) == 0) {
938             /* Save target index */
939             *puc_index = ul_index;
940             bret = TRUE;
941             break;
942         }
943     }
944
945     return bret;
946 }
947
948 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
949  * MODULE    : AllocNewEventTable()
950  * ABSTRACT  : Event table allocation processing
951  * NOTE      : Search the event table pointed to from the beginning for an area in
952  *           : which the event name is not registered, and returns its index number.
953  *           : The event table structure is allocated as an array in shared memory.
954  *           : One element of the array is the event table structure, and its index
955  *           : plus one is used as the event ID.
956  *           : Whether the event table structure is in use or unused is determined 
957  *           : by whether the event name is set or not.
958  *           : Note: Since the Mutex part inside this function was deleted to
959  *           : fix a bug caused by Mutex leak, use Mutex around this function from the
960  *           : outside before using this function.
961  * ARGUMENT  : PB_EVENT    *p_event_table    Start pointer of the event table array in shared memory
962  *           : TCHAR        *name            Event name to reserve table            Note: Currently unused.
963  *           : HANDLE        hMutex            Mutex handle for event table    Note: Currently unused.
964  * RETURN    : DWORD        other than -1       Index number of the allocted event table
965  *           :                 -1                There is no free space in the event table.
966  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
967 static BOOL
968 AllocNewEventTable(PB_EVENT* p_event_table, u_int32* puc_index) {
969     u_int32    ul_index = 0;
970     BOOL       bret = FALSE;
971
972     for (ul_index = 0; ul_index < MAX_PB_EVENTS; ul_index++) {
973         if (p_event_table[ul_index].event_name[0] == __TEXT('\0')) {
974             *puc_index = ul_index;
975             bret = TRUE;
976             break;
977         }
978     }
979
980     return bret;
981 }
982
983 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
984  * MODULE    : FreeEventTable()
985  * ABSTRACT  : Event table release processing
986  * NOTE      : Initialize the event name and event value of the index number
987  *           : of the specified event table to make them free.
988  * ARGUMENT  : PB_EVENT    *p_event_table    Start pointer of the event table array in shared memory
989  *           : int            index            Index number of the event table to release
990  *           : HANDLE        hMutex            Mutex handle for event table
991  * RETURN    : None
992  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
993 static void
994 FreeEventTable(PB_EVENT* p_event_table, int index) {
995     p_event_table[index].event_name[0] = __TEXT('\0');
996     p_event_table[index].l_event_val = 0;
997 }
998
999 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1000  * MODULE    : CheckCondition()
1001  * ABSTRACT  : Event condition determination processing
1002  * NOTE      : Check whether the event value of the specified event table is 
1003  *           : satisfied as an event wait condition.
1004  * ARGUMENT  : PB_EVENT    *p_event_table    Start pointer of the event table array in shared memory
1005  *           : DWORD        wcn                Index number of the event table to be checked
1006  * RETURN    : BOOL            TRUE            Condition satisfied
1007  *           :                FALSE            Condition not met
1008  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1009 static BOOL CheckCondition(PB_EVENT* p_sys_event, DWORD wcn, int32 l_event_data) {
1010     BOOL    bret = FALSE;
1011
1012     if (p_sys_event != NULL) {  // LCOV_EXCL_BR_LINE 6: p_sys_event can not be NULL
1013         /* Determine the wait mode of the event state TBL. */
1014         switch (p_sys_event->st_condition[wcn].us_mode) {  // LCOV_EXCL_BR_LINE 200: EVWAIT_ALLCLR and EVWAIT_ANYSET will not be called  // NOLINT(whitespace/line_length)
1015             case EVWAIT_VAL:        /* For value range wait */
1016             {
1017                 /* Check whether the event value is within the condition satisfied range */
1018                 if ((l_event_data >= p_sys_event->st_condition[wcn].l_min_val) &&
1019                         (l_event_data <= p_sys_event->st_condition[wcn].l_max_val)) {
1020                     bret = TRUE;
1021                 }
1022                 break;
1023             }
1024             case EVWAIT_ALLCLR:  /* When waiting for all specified bits to be cleared */  // LCOV_EXCL_BR_LINE 200: can not be EVWAIT_ALLCLR  // NOLINT(whitespace/line_length)
1025             {
1026                 // LCOV_EXCL_START 200: can not be EVWAIT_ALLCLR
1027                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1028                 if ((((u_int32)l_event_data) & p_sys_event->st_condition[wcn].ul_mask) == EVENT_BIT_ZERO) {
1029                     bret = TRUE;
1030                 }
1031                 break;
1032                 // LCOV_EXCL_STOP
1033             }
1034             case EVWAIT_ANYSET:  /* If the specified bit is waiting to set any bits */  // LCOV_EXCL_BR_LINE 200: can not be EVWAIT_ANYSET  // NOLINT(whitespace/line_length)
1035             {
1036                 // LCOV_EXCL_START 200: can not be EVWAIT_ALLCLR
1037                 AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1038                 if ((((u_int32)l_event_data) & p_sys_event->st_condition[wcn].ul_mask) != EVENT_BIT_ZERO) {
1039                     bret = TRUE;
1040                 }
1041                 break;
1042                 // LCOV_EXCL_STOP
1043             }
1044             default:                /* If the wait mode is out of range, */
1045                 break;                /* return with error */
1046         }
1047     }
1048
1049     return bret;
1050 }
1051
1052 /**
1053  * @brief
1054  *   Event generation processing
1055  *
1056  * @param[in]   u_int8  uc_manual_reset
1057  * @param[in]   int32   l_init_data
1058  * @param[in]    char    *cp_event_name    Pointer to the names of event to be generated (NULL termination string)
1059  * @return EventID    Non-zero    Event ID created<br>
1060  *                    0        Event generation error
1061  */
1062 static EventID    EventCreateNewEventInSystem(u_int8 uc_manual_reset, int32 l_init_data, TCHAR* p_event_name) {
1063     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
1064     PB_EVENT_INSTANCE       *p_inst         = &g_instance;
1065     PB_EVENT                *p_sys_event    = NULL;
1066     EventID     ret_event_id = 0;
1067     u_int32     ul_index     = 0;
1068     BOOL        bret         = FALSE;
1069     EV_ERR      ev_err;
1070
1071     /* Parameter check */
1072     if (p_event_name != NULL) {  // LCOV_EXCL_BR_LINE 6: p_event_name can not be NULL
1073         /* Parameter normal */
1074         /* Get the index number of the newly created event table */
1075         bret = AllocNewEventTable(p_inst->p_event_table, &ul_index);
1076
1077         /* When there is no free space */
1078         if (bret == FALSE) {
1079             /* Error log output */
1080             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_CWORD64_api.dll:_pb_CreateEvent : AllocNewEventTable Full... \r\n");
1081         }
1082     }
1083
1084     /* When there is free space */
1085     if (bret != FALSE) {
1086         /* allocate event table to generate from heap */
1087         /*  */
1088         p_event_open = reinterpret_cast<PB_EVENT_OPEN_HANDLE*>(PbProcessHeapAlloc(0, sizeof(PB_EVENT_OPEN_HANDLE)));  // LCOV_EXCL_BR_LINE 200: can not be NULL  // NOLINT(whitespace/line_length)
1089
1090         /* Failure in allocating heap area */
1091         if (p_event_open == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be NULL
1092             // LCOV_EXCL_START 200: can not be NULL
1093             AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
1094             bret = FALSE;
1095             /* Error log output */
1096             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
1097                     "_CWORD64_api.dll:_pb_CreateEvent : CreateHeap ... GetAddr[0x%08x], size[%ld] \r\n",
1098                     p_event_open, sizeof(PB_EVENT_OPEN_HANDLE));
1099             // LCOV_EXCL_STOP
1100         }
1101     }
1102
1103     /* When the heap area can be allocated */
1104     if (bret != FALSE) {
1105         /* Initialization of generated event management information */
1106         p_event_open->index        = ul_index;
1107         p_event_open->p_sys_event    = &p_inst->p_event_table[ul_index];
1108         p_event_open->l_thread_ref    = 1;
1109
1110         /* Initialization processing of event information storage area */
1111         p_sys_event = p_event_open->p_sys_event;
1112         _tcscpy(p_sys_event->event_name, p_event_name);        /*    Event name registration                */
1113         p_sys_event->l_event_val     = l_init_data;            /*    Default setting                    */
1114         for (u_int32 ul_wcn = 0; ul_wcn < MAX_PB_EVENT_WAIT_THREADS; ul_wcn++) {
1115             p_sys_event->st_condition[ul_wcn].uc_use_flag = FALSE;
1116             p_sys_event->st_condition[ul_wcn].uc_waiting = FALSE;
1117             p_sys_event->st_condition[ul_wcn].us_mode    = 0;
1118             p_sys_event->st_condition[ul_wcn].l_min_val    = MIN_EVENT_VAL;
1119             p_sys_event->st_condition[ul_wcn].l_max_val    = MAX_EVENT_VAL;
1120
1121             /* Create Event Flag */
1122             ev_err = EV_create_flag_auto_id(&(p_sys_event->st_condition[0].flag_id[g_my_proc_cnt]));  // LCOV_EXCL_BR_LINE 200: no branch  // NOLINT(whitespace/line_length)
1123             if (ev_err != EV_OK) /* When initialization fails */ {
1124                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1125                     "EV_create_flag_auto_id ERROR!! [ev_err=%d, flag_id=0x%x", \
1126                     ev_err, p_sys_event->st_condition[0].flag_id[g_my_proc_cnt]);
1127
1128                 /* Release heap space */
1129                 PbProcessHeapFree(0, p_event_open);  // LCOV_EXCL_BR_LINE 200: no branch
1130
1131                 ret_event_id = 0;
1132                 bret = FALSE;
1133             }
1134         }
1135
1136         if (bret != FALSE) {
1137             p_sys_event->l_process_ref      = 1;                    /* Set the number of references to this event.    */
1138             p_sys_event->l_reset_data     = l_init_data;            /* Default setting                    */
1139             p_sys_event->uc_manual_reset = uc_manual_reset;        /* Setting for a manual reset            */
1140
1141             /* Create an event table Mutex and set it in the event table. */
1142             bret = EventCreateMutex(p_event_open);
1143             /* If generating fails, reset is executed. */
1144             if (bret == FALSE) {
1145                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "_CWORD64_api.dll:%s:LINE %d\r\n", LTEXT(__FILE__), __LINE__); \
1146                 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "CreateMutex Err ... Event Name[%s]\r\n", p_event_name);
1147                 _pb_Exit();
1148             }
1149
1150             /* Register event table with event instance */
1151             p_inst->p_handle_table[ul_index] = p_event_open;
1152             ret_event_id = ul_index + 1;
1153         }
1154     }
1155
1156     return ret_event_id;
1157 }
1158
1159 /**
1160  * @brief
1161  *   Event generation processing
1162  *
1163  * @param[in]    char    *cpEventName    Pointer to name of the event to be generated (NULL termination string)
1164  * @return EventID    Non-zero    Event ID created<br>
1165  *                    0        Event generation error
1166  */
1167 static EventID    EventCreateNewEventInProcess(u_int32 index) {
1168     PB_EVENT_OPEN_HANDLE    *p_event_open = NULL;
1169     PB_EVENT_INSTANCE       *p_inst = &g_instance;
1170     EventID    ret_event_id = 0;
1171     EV_ERR     ev_err;
1172
1173     /* Already created in the same process    */
1174     if (p_inst->p_handle_table[index] != NULL) {  // LCOV_EXCL_BR_LINE 200: can not be NULL
1175         /* When the number of event references in the same process is less than the upper limit */
1176         if (p_inst->p_handle_table[index]->l_thread_ref < _CWORD64_EVENT_MAXOPEN_IN_PROCESS) {
1177             /* Increase the number of thread references */
1178             (p_inst->p_handle_table[index]->l_thread_ref)++;
1179             ret_event_id = index + 1;
1180         } else {
1181             /* When the number of event references in the same process is the upper limit */
1182             /* Error log output */
1183             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
1184                                        "_pb_CreateEvent Err ... Event Max In Process : EventName[%s]\r\n",
1185                                p_inst->p_handle_table[index]->p_sys_event->event_name);
1186         }
1187     } else {
1188         /* Creating for the first time in the process */
1189         /* Checking the upper limit of the reference count of the same event in the system */
1190         if (p_inst->p_event_table[index].l_process_ref < _CWORD64_EVENT_MAXOPEN_IN_SYSTEM) {
1191             /* Allocate event table to generate from heap */
1192             /*  */
1193             p_event_open = reinterpret_cast<PB_EVENT_OPEN_HANDLE*>(PbProcessHeapAlloc(0, \
1194                 sizeof(PB_EVENT_OPEN_HANDLE)));
1195
1196             /* Failure in allocating heap area */
1197             if (p_event_open == NULL) {
1198                 /* Error log output */
1199                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1200                         "_CWORD64_api.dll:_pb_CreateEvent : CreateHeap ... GetAddr[0x%08x], size[%ld] \r\n", \
1201                         p_event_open, sizeof(PB_EVENT_OPEN_HANDLE));
1202             }
1203         } else {
1204             FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, 
1205                                        "_pb_CreateEvent Err ... Event Max In sYSTEM : EventName[%s]\r\n",
1206                                p_inst->p_handle_table[index]->p_sys_event->event_name);
1207         }
1208
1209         /* When heap allocation is successful */
1210         if (p_event_open != NULL) {
1211             /* When it is not created in the same process, set each data. */
1212             /* Set the index to which the event name is registered */
1213             p_event_open->index       = index;
1214             /* Initialize the reference count of the threads referencing this event in the same process. */
1215             p_event_open->l_thread_ref = 1;
1216             /* Set event instance start address */
1217             p_event_open->p_sys_event  = &p_inst->p_event_table[index];
1218             /* Add the reference count of the process referencing this event in the system. */
1219             p_event_open->p_sys_event->l_process_ref++;
1220
1221             /* Create an event flag */
1222             ev_err = EV_create_flag_auto_id(&(p_event_open->p_sys_event->st_condition[0].flag_id[g_my_proc_cnt]));
1223             if (ev_err != EV_OK) /* When initialization fails */ {
1224                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1225                     "EV_create_flag_auto_id ERROR!! [ev_err=%d, flag_id=0x%x]",
1226                                ev_err, p_event_open->p_sys_event->st_condition[0].flag_id[g_my_proc_cnt]);
1227
1228                 /* Release heap space */
1229                 PbProcessHeapFree(0, p_event_open);
1230
1231                 ret_event_id = 0;
1232             } else {
1233                 /* Even if event information already exists in the system, the Mutex is created for each process. */
1234                 (void)_pb_CreateMutex(NULL, FALSE, p_event_open->p_sys_event->name_of_mutex);
1235
1236                 p_inst->p_handle_table[index] = p_event_open;   /* Register event tables with event instance */
1237                 ret_event_id = index + 1;
1238             }
1239         }
1240     }
1241
1242     return ret_event_id;
1243 }
1244
1245 /**
1246  * @brief
1247  *   Send the signal
1248  *
1249  *   Sends the specified event signal.
1250  *
1251  *   - Sending signals in the CLS event library
1252  *
1253  * @param[in]    PB_EVENT_OPEN_HANDLE *p_evet_open_handle
1254  * @param[in]    u_int32    ul_index
1255  *
1256  * @return RET_NORMAL        Normal completion<br>
1257  *           RET_ERROR        Other errors
1258  */
1259 static RET_API EventSendSignal(PB_EVENT_OPEN_HANDLE *p_evet_open_handle, u_int32 ul_index) {
1260     RET_API             ret_api = RET_NORMAL;
1261     PB_EVENT_INSTANCE   *p_inst = &g_instance;
1262     EV_ERR    ev_err;
1263     int32     i;
1264     BOOL      errFlag = TRUE;
1265
1266     if ((p_evet_open_handle != NULL) &&
1267             (ul_index < MAX_PB_EVENT_WAIT_THREADS)) {
1268         for (i = 0; i <= p_inst->p_event_table->proc_cnt; i++) {
1269             /* Signal issuance */
1270             if (p_evet_open_handle->p_sys_event->st_condition[ul_index].flag_id[i] != 0) {
1271                 ev_err = EV_set_flag(p_evet_open_handle->p_sys_event->st_condition[ul_index].flag_id[i], 1);
1272                 if (ev_err == EV_OK) {
1273                     errFlag = FALSE;
1274                 }
1275             }
1276         }
1277     }
1278
1279     if (errFlag == TRUE) /* Event issuance NG */ {
1280         ret_api = RET_ERROR;
1281     }
1282
1283     return ret_api;
1284 }
1285
1286 /**
1287  * @brief
1288  *   Wait for the signal
1289  *
1290  *   Wait until the specified signal is received. Timeout can be specified (ms).
1291  *   When this API is called, the semaphore for the event element must be acquired.
1292  *
1293  *   Receive a signal in the CLS event library.
1294  *
1295  * @param[in]    *p_evet_open_handle
1296  * @param[in]    ul_index
1297  * @param[in]    ul_mill_sec_time
1298  *
1299  * @return    RET_NORMAL        Normal completion<br>
1300  *            RET_ERRTIMEOUT    Timeout End<br>
1301  *            RET_ERROR        Other errors
1302  */
1303 static RET_API EventWaitForSignal(PB_EVENT_OPEN_HANDLE *p_evet_open_handle, u_int32 ul_index, \
1304     u_int32 ul_mill_sec_time) {
1305     RET_API    ret_api = RET_ERRTIMEOUT;
1306     EV_ERR     ev_err;
1307     EV_Flag    ev_flag;
1308     u_int32    timeOutCnt = 0;
1309
1310     /* Parameter check */
1311     if ((p_evet_open_handle != NULL) && (ul_index < MAX_PB_EVENT_WAIT_THREADS)) {  // LCOV_EXCL_BR_LINE 6: param can not be invalid  // NOLINT(whitespace/line_length)
1312         /* Release semaphore for event table */
1313         EventUnlockMutex(p_evet_open_handle);  // LCOV_EXCL_BR_LINE 200: no branch
1314
1315         /* Distribute processing by timeout period */
1316         /* To check the event occurrence status */
1317         if (ul_mill_sec_time == 0) {
1318             /* Untreated */
1319         } else if (ul_mill_sec_time == INFINITE) {
1320             /* Without timeout */
1321             ret_api = RET_NORMAL;
1322
1323             FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, "EV_wait_flag CALL [flag_id=0x%x]",
1324                            p_evet_open_handle->p_sys_event->st_condition[ul_index].flag_id[g_my_proc_cnt]);
1325
1326             /* Wait for event flag */
1327             ev_err = EV_wait_flag(p_evet_open_handle->p_sys_event->st_condition[ul_index].flag_id[g_my_proc_cnt], \
1328                 &ev_flag);  // LCOV_EXCL_BR_LINE 200: no branch
1329             if (ev_err != EV_OK) {
1330                 FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1331                     "EV_wait_flag ERROR!! [ev_err=%d]", ev_err);
1332                 ret_api = RET_ERROR;
1333             } else {
1334                 FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, \
1335                     "EV_wait_flag RETURN [ev_err=%d]", ev_err);
1336             }
1337         } else {
1338             /* When the timeout period is specified */
1339             FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, "EV_get_flag CALL [flag_id=0x%x]",
1340                            p_evet_open_handle->p_sys_event->st_condition[ul_index].flag_id[g_my_proc_cnt]);
1341
1342             while (1) {
1343                 /* Get elag event */
1344                 ev_err = EV_get_flag(p_evet_open_handle->p_sys_event->st_condition[ul_index].flag_id[g_my_proc_cnt], \
1345                     &ev_flag);  // LCOV_EXCL_BR_LINE 200: no branch
1346                 if (ev_err == EV_OK) {
1347                     if (ev_flag.flagID == EV_NO_ID) {
1348                         timeOutCnt++;
1349                         if (timeOutCnt <= ul_mill_sec_time) {
1350                             usleep(1000);  // LCOV_EXCL_BR_LINE 200: no branch
1351                         } else {
1352                             break; /* Timeout error */
1353                         }
1354                     } else {
1355                         ret_api = RET_NORMAL;
1356                         break;
1357                     }
1358                 } else {
1359                     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, \
1360                         "EV_get_flag ERROR!! [ev_err=%d]", ev_err);
1361                     ret_api = RET_ERROR;
1362                     break;
1363                 }
1364             }
1365
1366             FRAMEWORKUNIFIEDLOG(ZONE_20, __FUNCTION__, "EV_get_flag BREAK [ret_api=%d]", ret_api);
1367         }
1368
1369         /* Get event table semaphore */
1370         EventLockMutex(p_evet_open_handle);  // LCOV_EXCL_BR_LINE 200: no branch
1371     } else {
1372         /* Parameter error */
1373         ret_api = RET_ERROR;
1374     }
1375
1376     return ret_api;
1377 }
1378
1379 /**
1380  * @brief
1381  *   Create the mutex for event
1382  *
1383  * @param[in]    *p_evet_open_handle
1384  *
1385  * @return    TRUE        Normal completion<br>
1386  *            FALSE        ABENDs
1387  */
1388 static BOOL EventCreateMutex(PB_EVENT_OPEN_HANDLE *p_evet_open_handle) {
1389     static u_int8    idx = 0;
1390     uint32_t    ulPid;    /* Process ID */
1391     BOOL        bret   = FALSE;
1392     TCHAR       name[NAME_MAX];
1393     HANDLE      handle;
1394
1395     /* Parameter check */
1396     if (p_evet_open_handle != NULL) {  // LCOV_EXCL_BR_LINE 6: p_evet_open_handle can not be NULL
1397         ulPid = (uint32_t)getpid();
1398
1399         wsprintf(name, __TEXT("POS_BASE_EVENT_MUTEX%05d_p%d"), idx, ulPid);
1400
1401         /****************************************/
1402         /* Create Mutex                         */
1403         /****************************************/
1404         handle = _pb_CreateMutex(NULL, FALSE, name);
1405         if (handle != NULL) {
1406             _tcscpy(p_evet_open_handle->p_sys_event->name_of_mutex, name);
1407             idx++;
1408             bret = TRUE;
1409         }
1410     }
1411
1412     /* When mutex processing fails */
1413     if (bret != TRUE) {
1414         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "bret ERROR [bret:%d]", bret);
1415     }
1416
1417     return bret;
1418 }
1419
1420 /**
1421  * @brief
1422  *   Lock the mutex for event
1423  *
1424  * @param[in] *p_evet_open_handle
1425  */
1426 static void EventLockMutex(PB_EVENT_OPEN_HANDLE *p_evet_open_handle) {
1427     DWORD     lret = WAIT_FAILED;
1428     HANDLE    handle;
1429
1430     if (p_evet_open_handle != NULL) {  // LCOV_EXCL_BR_LINE 6: p_evet_open_handle can not be NULL
1431         /* Get handle from Mutex name */
1432         handle = _pb_CreateMutex(NULL, FALSE, p_evet_open_handle->p_sys_event->name_of_mutex);
1433
1434         /****************************************/
1435         /* Get Mutex                            */
1436         /****************************************/
1437         lret = PbMutexLock(handle, INFINITE);
1438
1439         /* Cancel by deleting the generated portion when a handle was acquired */
1440         (void)PbDeleteMutex(handle);
1441     }
1442
1443     /* When mutex processing fails */
1444     if (lret != WAIT_OBJECT_0) {
1445         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lret ERROR [lret:%lu]", lret);
1446     }
1447
1448     return;
1449 }
1450
1451 /**
1452  * @brief
1453  *   Unlock the mutex for event
1454  *
1455  * @param[in] *p_evet_open_handle
1456  */
1457 static void EventUnlockMutex(PB_EVENT_OPEN_HANDLE *p_evet_open_handle) {
1458     BOOL      bret = FALSE;
1459     HANDLE    handle;
1460
1461     if (p_evet_open_handle != NULL) {  // LCOV_EXCL_BR_LINE 6: p_evet_open_handle can not be NULL
1462         /* Get handle from Mutex name */
1463         handle = _pb_CreateMutex(NULL, FALSE, p_evet_open_handle->p_sys_event->name_of_mutex);
1464
1465         /****************************************/
1466         /* Release Mutex                        */
1467         /****************************************/
1468         bret = PbMutexUnlock(handle);
1469
1470         /* Cancel by deleting the generated portion when a handle was acquired */
1471         (void)PbDeleteMutex(handle);
1472     }
1473
1474     /* When mutex processing fails */
1475     if (bret != TRUE) {
1476         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "bret ERROR [bret:%d]", bret);
1477     }
1478
1479     return;
1480 }
1481
1482 /**
1483  * @brief
1484  *   delete the mutex for event
1485  *
1486  * @param[in] *p_evet_open_handle
1487  */
1488 static void EventDeleteMutex(PB_EVENT_OPEN_HANDLE *p_evet_open_handle) {
1489     RET_API   ret_api = RET_ERROR;
1490     HANDLE    handle;
1491
1492     if (p_evet_open_handle != NULL) {  // LCOV_EXCL_BR_LINE 6: p_evet_open_handle can not be NULL
1493         /* Get handle from Mutex name */
1494         handle = _pb_CreateMutex(NULL, FALSE, p_evet_open_handle->p_sys_event->name_of_mutex);
1495
1496         /****************************************/
1497         /* Delete Mutex                         */
1498         /****************************************/
1499         ret_api = static_cast<RET_API>(PbDeleteMutex(handle));    /* Coverity CID:18817 Comment Managed */
1500
1501         /* Cancel by deleting the generated portion when a handle was acquired */
1502         (void)PbDeleteMutex(handle);
1503     }
1504
1505     /* When mutex processing fails */
1506     if (ret_api != RET_NORMAL) {
1507         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "ret_api ERROR [ret_api:%d]", ret_api);
1508     }
1509
1510     return;
1511 }
1512
1513 /**
1514  * @brief
1515  *   Get dump information
1516  *
1517  * @param[out]    p_buf      Dump info
1518  * @param[in/out] p_len      Buffer size
1519  */
1520 void _pb_GetDebugEventMngTbl(void* p_buf, uint8_t* p_len) {
1521     PB_EVENT_INSTANCE    *p_inst = &g_instance;
1522     static uint8_t        buf[DEBUG_DUMP_MAX_SIZE];
1523     static uint8_t        bufHdlTbl[DEBUG_DUMP_MAX_SIZE];
1524     static uint8_t        bufSysEvt[DEBUG_DUMP_MAX_SIZE];
1525     static uint8_t        buf_tmp[DEBUG_DUMP_MAX_SIZE];
1526     uint8_t                buf_indent[16];
1527     uint32_t               i;
1528     PB_EVENT_OPEN_HANDLE* p_hdl_tbl;
1529     uint8_t                cnt = 0;
1530
1531     if ((p_buf != NULL) && (p_len != NULL)) {
1532         memset(&buf[0], 0x00, sizeof(buf));
1533         memset(&bufSysEvt, 0x00, sizeof(bufSysEvt));
1534         snprintf(reinterpret_cast<char *>(&buf_indent[0]), sizeof(buf_indent), "  ");
1535         GetDebugEventMngTblSysEvent(&bufSysEvt[0], p_inst->p_event_table, &buf_indent[0]);  // LCOV_EXCL_BR_LINE 200: no branch  // NOLINT(whitespace/line_length)
1536         snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf),
1537                 "Event-1\n ShrMem:%p\n idEvt:%d\n Evt:\n%s",
1538                 p_inst->h_shared_memory,
1539                 p_inst->id_event_table_sem,
1540                 &bufSysEvt[0]);
1541         memcpy(p_buf, &buf[0], sizeof(buf));
1542         p_buf = reinterpret_cast<void *>((reinterpret_cast<uint8_t *>(p_buf)) + sizeof(buf));
1543         cnt++;
1544         if (cnt < *p_len) {
1545             memset(&bufHdlTbl[0], 0x00, sizeof(bufHdlTbl));
1546             for (i = 0; i < MAX_PB_EVENTS; i++) {
1547                 // p_handle_table
1548                 memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1549                 p_hdl_tbl = p_inst->p_handle_table[i];
1550                 if (p_hdl_tbl == NULL) {
1551                     snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1552                             "\n  [%d] NULL",
1553                             i);
1554                 } else {
1555                     memset(&bufSysEvt[0], 0x00, sizeof(bufSysEvt));
1556                     snprintf(reinterpret_cast<char *>(&buf_indent[0]), sizeof(buf_indent), "    ");
1557                     GetDebugEventMngTblSysEvent(&bufSysEvt[0], p_hdl_tbl->p_sys_event, &buf_indent[0]);
1558                     snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1559                             "\n  [%d]\n   h_heap:%p, index:%lu, l_thread_ref:%d\n   p_sys_event:\n%s",
1560                             i,
1561                             p_hdl_tbl->h_heap,
1562                             p_hdl_tbl->index,
1563                             p_hdl_tbl->l_thread_ref,
1564                             &bufSysEvt[0]);
1565                 }
1566                 strncat(reinterpret_cast<char *>(&bufHdlTbl[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1567                     strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1568                 if (((i + 1) % 4) == 0) {
1569                     cnt++;
1570                     memset(&buf[0], 0x00, sizeof(buf));
1571                     snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf),
1572                             "Event-%d\n Handle:%s",
1573                             cnt,
1574                             &bufHdlTbl[0]);
1575                     memcpy(p_buf, &buf[0], sizeof(buf));
1576                     p_buf = reinterpret_cast<void *>((reinterpret_cast<uint8_t *>(p_buf)) + sizeof(buf));
1577                     memset(&bufHdlTbl[0], 0x00, sizeof(bufHdlTbl));
1578                     if (cnt >= *p_len) {
1579                         break;
1580                     }
1581                 }
1582             }
1583         }
1584         if (cnt < *p_len) {
1585             if (bufHdlTbl[0] != 0x00) {
1586                 cnt++;
1587                 memset(&buf[0], 0x00, sizeof(buf));
1588                 snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf),
1589                         "Event-%d\n Handle:%s",
1590                         cnt,
1591                         &bufHdlTbl[0]);
1592                 memcpy(p_buf, &buf[0], sizeof(buf));
1593             }
1594             *p_len = cnt;
1595         }
1596     }
1597 }
1598
1599 /**
1600  * @brief
1601  *   Get dump information(PB_EVENT)
1602  *
1603  * @param[out] p_buf      Dump info
1604  * @param[in]  pEvt      PB_EVENT
1605  * @param[in]  pIndent   Indenting
1606  */
1607 void GetDebugEventMngTblSysEvent(void* p_buf, PB_EVENT* pEvt, uint8_t* pIndent) {
1608     static uint8_t buf[DEBUG_DUMP_MAX_SIZE];
1609     static uint8_t buf_condition[1024];
1610     static uint8_t buf_flag_id[512];
1611     static uint8_t buf_tmp[DEBUG_DUMP_MAX_SIZE];
1612     uint32_t i;
1613     uint32_t e;
1614
1615     if ((p_buf != NULL) && (pEvt != NULL)) {  // LCOV_EXCL_BR_LINE 6: p_buf and pEvt can not be NULL
1616         memset(&buf, 0x00, sizeof(buf));
1617         memset(&buf_condition, 0x00, sizeof(buf_condition));
1618         snprintf(reinterpret_cast<char *>(&(buf_condition)), sizeof(buf_condition), "stCnd:");
1619         for (i = 0; i < MAX_PB_EVENT_WAIT_THREADS; i++) {
1620             memset(&buf_flag_id, 0x00, sizeof(buf_flag_id));
1621             for (e = 0; e < MAX_EVENT_PROC_NUM; e++) {
1622                 memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1623                 snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1624                         "[%d]0x%08x ", e, pEvt->st_condition[i].flag_id[e]);
1625                 strncat(reinterpret_cast<char *>(&buf_flag_id[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1626                     strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1627             }
1628             memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1629             snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1630                     "\n%s   [%d] UseFlg:%d, Wait:%d, Mode:%d, Mask:%d, Min:%d, Max:%d, Last:%d, flag:%s",
1631                     pIndent,
1632                     i,
1633                     pEvt->st_condition[i].uc_use_flag,
1634                     pEvt->st_condition[i].uc_waiting,
1635                     pEvt->st_condition[i].us_mode,
1636                     pEvt->st_condition[i].ul_mask,
1637                     pEvt->st_condition[i].l_min_val,
1638                     pEvt->st_condition[i].l_max_val,
1639                     pEvt->st_condition[i].l_last_val,
1640                     &buf_flag_id[0]);
1641             strncat(reinterpret_cast<char *>(&buf_condition[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1642                 strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1643         }
1644         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1645         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1646                 "%s EvtName:%s",
1647                 pIndent,
1648                 pEvt->event_name);
1649         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1650             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1651         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1652         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1653                 "\n%s EvtVal:%d",
1654                 pIndent,
1655                 pEvt->l_event_val);
1656         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1657             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1658         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1659         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1660                 "\n%s %s",
1661                 pIndent,
1662                 &buf_condition[0]);
1663         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1664             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1665         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1666         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1667                 "\n%s ProcRef:%d",
1668                 pIndent,
1669                 pEvt->l_process_ref);
1670         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1671             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1672         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1673         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1674                 "\n%s ResetData:%d",
1675                 pIndent,
1676                 pEvt->l_reset_data);
1677         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1678             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1679         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1680         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1681                 "\n%s ManualReset:%d",
1682                 pIndent,
1683                 pEvt->uc_manual_reset);
1684         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1685             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1686         memset(&buf_tmp[0], 0x00, sizeof(buf_tmp));
1687         snprintf(reinterpret_cast<char *>(&buf_tmp[0]), sizeof(buf_tmp),
1688                 "\n%s name_of_mutex:%s",
1689                 pIndent,
1690                 pEvt->name_of_mutex);
1691         strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&buf_tmp[0]), \
1692             strlen(reinterpret_cast<char *>(&buf_tmp[0])));
1693         memcpy(p_buf, &buf[0], sizeof(buf));
1694     }
1695 }