summaryrefslogtreecommitdiffstats
path: root/positioning_base_library/library/src/_pbOther.cpp
blob: 73ff30040ef7fc64073c553426421d76b8ab58c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file
 *    _pbOther.cpp
 */

#include <stdio.h>
#include <unistd.h>
#include <asm/unistd.h>
#include <native_service/frameworkunified_types.h>

#include <vehicle_service/positioning_base_library.h>
#include "_pbInternalProc.h"
#include <other_service/VP_GetEnv.h>
#include "WPF_STD_private.h"
#include "tchar.h"



/*---------------------------------------------------------------------------------*
 * Define                                                                          *
 *---------------------------------------------------------------------------------*/
/* Shared memory */
#define POS_BASE_OTHER_PROC_ID    "POS_BASE_OTHER_PROC_ID"

#define MAX_OTHER_PROC_NUM      (32)     /** Maximum number of the management information to translate the process name to PNO      */
#define MAX_NUM_CTRL_TID        (16)     /** Maximum number of tje TID management for thread in Process       */

#define OTHER_PNO_BASE          (0x9000) /** Base number of local process            */

#define THREAD_NAME_LEN_MAX     (32)

#define FULL_OTHER_PROC_NUM     (MAX_OTHER_PROC_NUM - 4)  /** Threshold of the management information to translate the process name to PNO (no free) */
#define WARN_OTHER_PROC_NUM     (MAX_OTHER_PROC_NUM - 10) /** Threshold of the management information to translate the process name to PNO (warning) */

typedef void* (*_CWORD64_PROCMNG_START_ROUTINE)(void*);

/*---------------------------------------------------------------------------------*
 * Structure                                                                       *
 *---------------------------------------------------------------------------------*/
/*!
  @brief    Process identification information
 */
typedef struct {
    PNO     pno;                        /**< Process number    */
    char    name[THREAD_NAME_LEN_MAX];    /**< Process name        */
} PROC_ID;

/*!
  @brief    Process information
 */
typedef struct {
    PROC_ID  id[MAX_OTHER_PROC_NUM]; /**< Process identification information */
    uint32_t use_cnt;                 /**< Used number           */
    uint32_t rsv_cnt;                 /**< Reserved number       */
} PROC_INFO;

/*---------------------------------------------------------------------------------*
 * Grobal Value                                                                    *
 *---------------------------------------------------------------------------------*/
static HANDLE        g_h_app[MAX_NUM_CTRL_TID];       /** Application handle       */
static HANDLE        g_h_mtx;                        /** Shared-information-locking Mutex handle */
static HANDLE        g_h_shm;                        /** Shared memory handle */   // Coverity CID: 18787 compliant

static PROC_INFO*    g_p_proc_id_tbl;    /** Process Name-PNO Translation Table    */

/*---------------------------------------------------------------------------------*
 * Internal Function Prototype                                                     *
 *---------------------------------------------------------------------------------*/
/* Process number to PNO translation table manipulation functions */
static void        OtherSetPnoOfCnvTbl(u_int32 idx, PNO pno);
static void        OtherSetNameOfCnvTbl(u_int32 idx, PCSTR name);
static PNO        OtherGetPnoOfCnvTbl(u_int32 idx);
static PCSTR    OtherGetNameOfCnvTbl(u_int32 idx);
static u_int32    OtherSearchPnoOfCnvTbl(PNO pno);
static u_int32    OtherSearchNameOfCnvTbl(PCSTR name);
static void     OtherIncUseCntOfCnvTbl(void);
static void     OtherIncRsvCntOfCnvTbl(void);
static void     OtherDecRsvCntOfCnvTbl(void);

static void OtherCreateMutex(void);
static void OtherDeleteMutex(void);
static void OtherLockMutex(void);
static void OtherUnlockMutex(void);

/**
 * @brief
 *  Initialize other funtion
 *
 * @return RET_NORMAL    Normal completion
 */
RET_API ErrTrapInit(void) {
    RET_API    ret_api = RET_NORMAL;
    PROC_INFO **pp_tbl;
    u_int16    idx;

    pp_tbl = &g_p_proc_id_tbl; /* Set a pointer to a table to translate the process name to PNO */

    OtherCreateMutex(); /* Create Mutex for accessing shared info */
    OtherLockMutex(); /* Acquire Mutex for accessing shared info */

    /* Open shared memory for a table to translate the process name to PNO */
    g_h_shm = OpenSharedMemory(const_cast<char*>(POS_BASE_OTHER_PROC_ID), sizeof(PROC_INFO));

    /* If called for the first time within all processes, an error occurs and the following processing is performed. */
    if (g_h_shm == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be not NULL
        /* Create shared memory for a table to translate the name to PNO */
        g_h_shm = CreateSharedMemory(const_cast<char*>(POS_BASE_OTHER_PROC_ID), sizeof(PROC_INFO));
        /* In case of an error */
        if (g_h_shm == NULL) {  // LCOV_EXCL_BR_LINE 200: can not be NULL
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_CreateShareData ERROR " \
                "[g_h_shm:%p]", g_h_shm);
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            _pb_Exit();  // // LCOV_EXCL_LINE 200: can not be NULL
            /* don't arrive here. */
        }
    }

    OtherUnlockMutex(); /* Release Mutex for accessing shared info */

    /* Set the acquired shared memory address as a pointer for a table to translate the process name to PNO */
    *pp_tbl = reinterpret_cast<PROC_INFO*>(GetSharedMemoryPtr(g_h_shm));

    /* Table initialization */
    for (idx = 0; idx < MAX_OTHER_PROC_NUM; idx++) {
        /* Set PNO into the table to translate the process name to PNO (Overwrite from the second process onwards) */
        OtherSetPnoOfCnvTbl(idx, static_cast<PNO>(OTHER_PNO_BASE + idx));
    }

    return ret_api;
}

/**
 * @brief
 *  Terminate other function
 */
void ErrTrapTerm(void) {  // LCOV_EXCL_START 8:dead code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    CloseSharedMemory(g_h_shm);

    OtherDeleteMutex();
}
// LCOV_EXCL_STOP

/**
 * @brief
 *   Create Thread
 *
 * @param[in]  lp_thread_attributes    Not used
 * @param[in]  dw_stack_size            Initial stack size
 * @param[in]  lp_start_address        Address of the effective function of the thread
 * @param[in]  lp_parameter            Thread arguments
 * @param[in]  dw_creation_flags        Not used
 * @param[in]  lp_thread_id            Thread identifier
 * @param[in]  pno                    PNO
 * @param[in]  priority                Thread priority
 *
 * @return Non-zero:Normal status,  0:When an error occurs
 */
HANDLE _pb_CreateThread(LPSECURITY_ATTRIBUTES lp_thread_attributes, DWORD dw_stack_size, LPTHREAD_START_ROUTINE lp_start_address, LPVOID lp_parameter, DWORD dw_creation_flags, LPDWORD lp_thread_id, PNO pno, int32 priority) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    pthread_attr_t    st_thread_attr;
    pthread_t        ul_thread_id = 0;
    sched_param        st_thread_param = {0};
    HANDLE            handle = NULL;
    int32            lret = EOK;
    BOOL            bret = FALSE;

    /* null check */
    if (lp_thread_id  == NULL) {
        // no op
    } else {
        /* Initializing Attributes */
        lret = pthread_attr_init(&st_thread_attr);

        /* When the attribute initialization is successful */
        if (lret == EOK) {
            /* Do not inherit parent scheduling policies */
            lret = pthread_attr_setinheritsched(&st_thread_attr, PTHREAD_EXPLICIT_SCHED);
        }

        /* If you successfully configure policy inheritance */
        if (lret == EOK) {
            /* Scheduling settings */
            lret = pthread_attr_setschedpolicy(&st_thread_attr, SCHED_RR);
        }

        /* Successful Scheduling settings */
        if (lret == EOK) {
            /* Create a thread with the lowest priority so that the spawned thread */
            /* do not run until they are ready for processing                      */
            st_thread_param.sched_priority    = 1;
            lret = pthread_attr_setschedparam(&st_thread_attr, &st_thread_param);
        }

        /* If the priority setting is successful */
        if (lret == EOK) {
            lret = pthread_create(&ul_thread_id,
                                   NULL,
                                   (_CWORD64_PROCMNG_START_ROUTINE)lp_start_address,
                                   lp_parameter);
        }

        /* Successful pthread_create */
        if (lret == EOK) {
            bret = TRUE;
        }
    }

    /* When priority setting is successful */
    if (bret != FALSE) {
        /* Return value setting */
        handle         = (HANDLE)ul_thread_id;
        *lp_thread_id    = ul_thread_id;
    } else {
        /* Error log output */
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "lret ERROR [lret:%d]", lret);
    }

    return handle;
}
// LCOV_EXCL_STOP

/**
 * @brief
 *   Get the thread ID
 *
 * @return Thread ID
 */
uint32_t PbGetTid(void) {
    uint32_t ul_tid;

    ul_tid = (uint32_t)syscall(__NR_gettid);

    return ul_tid;
}

/**
 * @brief
 *   Get the local thread ID
 *
 *   Local thread ID = [0, 1, 2, ...]<br>
 *   The local thread ID is unique in the process, and dynamically assigned in the order in which <br>
 *   this API was called during the process execution. <br>
 *
 * @return Local thread ID
 */
uint32_t PbGetLocalTid(void) {
    static uint32_t g_tid[MAX_NUM_CTRL_TID] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
            0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
            0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF,
            0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
                                              };  /** In-process thread ID management table */

    uint32_t ul_tid;
    uint32_t ul_idx;
    uint32_t ul_local_tid = 0xFFFFFFFF;

    ul_tid = PbGetTid();

    OtherLockMutex(); /* Get Mutex for accessing shared info */

    for (ul_idx = 0; ul_idx < MAX_NUM_CTRL_TID; ul_idx++) {
        if (g_tid[ul_idx] == ul_tid) {
            ul_local_tid = ul_idx;
        }
    }

    if (ul_local_tid == 0xFFFFFFFF) {
        for (ul_idx = 0; ul_idx < MAX_NUM_CTRL_TID; ul_idx++) {
            if (g_tid[ul_idx] == 0xFFFFFFFF) {
                g_tid[ul_idx] = ul_tid;
                ul_local_tid = ul_idx;

                break;
            }
        }

        if (ul_local_tid == 0xFFFFFFFF) {
            /* forbidden */
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Local Tid buffer is overfull!!");
            _pb_Exit();

            /* don't arrive here. */
        }
    }

    OtherUnlockMutex(); /* Release Mutex for accessing shared info */

    return ul_local_tid;
}


/**
 * @brief
 *   Get Application Handle
 *
 *    Get the application handle of the invoking thread.
 *
 * @return Non-zero:Normal status,  0:When an error occurs
 */
HANDLE _pb_GetAppHandle(void) {     // NOLINT(readability/nolint)  WPF_SYSAPI.h  API
    const uint32_t offset = PbGetLocalTid();

    return g_h_app[offset];
}


/**
 * @brief
 *   Set Application Handle
 *
 *    Set the application handle of the invoking thread.
 *
 * @param[in]  name Process name
 */
void _pb_SetAppHandle(HANDLE h_app) {     // NOLINT(readability/nolint)  WPF_SYSAPI.h  API
    const uint32_t offset = PbGetLocalTid();

    OtherLockMutex(); /* Get Mutex for accessing shared info */

    g_h_app[offset] = h_app;

    OtherUnlockMutex(); /* Release Mutex for accessing shared info */

    return;
}


/**
 * @brief
 *   Convert process name to pno
 *
 *   Translate process name to PNO.<br>
 *   If the process name specified in the argument is the first name to <br>
 *   be translated by this API, a new PNO is assigned and returned. <br>
 *   If NULL is specified, 0 is returned.
 *
 * @param[in]  name Process name
 *
 * @return Process number
 */
PNO _pb_CnvName2Pno(PCSTR name) {     // NOLINT(readability/nolint)  WPF_SYSAPI.h  API
    u_int32 idx;
    PNO pno = 0;
    size_t len;

    /* null check */
    if (name == NULL) {
        pno = 0;
    } else {
        len = _tcslen(name);
        if (len >= THREAD_NAME_LEN_MAX) {
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argument ERROR!! " \
                "Length of thread name is too long(>=%d). [len:%zu]", THREAD_NAME_LEN_MAX, len);
        } else {
            OtherLockMutex();

            idx = OtherSearchNameOfCnvTbl(name);

            if (idx != MAX_OTHER_PROC_NUM) {
                pno = OtherGetPnoOfCnvTbl(idx);
            } else {
                idx = OtherSearchNameOfCnvTbl("");
                OtherSetNameOfCnvTbl(idx, name);
                /* Increment using-counter */
                OtherIncUseCntOfCnvTbl();
                pno = OtherGetPnoOfCnvTbl(idx);
            }

            OtherUnlockMutex();
        }
    }

    return pno;
}

/**
 * @brief
 *   Convert pno to process name
 *
 *    Translate PNO to the process name.
 *   Return the process name set by _pb_CnvName2Pno to the PNO argument.
 *   If a non-PNO value is given by _pb_CnvName2Pno is specified,
 *   NULL is returned.
 *
 * @param[in]  pno Process number
 *
 * @return Process name
 */
PCSTR _pb_CnvPno2Name(PNO pno) {     // NOLINT(readability/nolint)  WPF_SYSAPI.h  API
    u_int32 idx;
    PCSTR name = NULL;

    OtherLockMutex();

    idx = OtherSearchPnoOfCnvTbl(pno);

    if (idx != MAX_OTHER_PROC_NUM) {
        name = OtherGetNameOfCnvTbl(idx);
    }

    OtherUnlockMutex();

    return name;
}


/**
 * @brief
 *   Get environment variables
 *
 * @param[in]   Environment variable name
 * @param[in]   Pointer to environment variable value
 */
void GetEnv(const char* p_env_str, char* p_env_buff) {  // LCOV_EXCL_START 8:dead code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    VP_GetEnv(p_env_str, p_env_buff);

    FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "VP_GetEnv:%s=%s", p_env_str, p_env_buff);

    return;
}
// LCOV_EXCL_STOP

/*---------------------------------------------------------------------------------*
 * Local Function                                                                  *
 *---------------------------------------------------------------------------------*/
/**
 * @brief
 *   PNO setting(The table to translate the process name to PNO)
 *
 *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
 *
 * @param[in]    u_int32 idx        Table accessor
 * @param[in]    PNO    pno    Process number
 *
 * @return none
 */
static void OtherSetPnoOfCnvTbl(u_int32 idx, PNO pno) {
    /* check index */
    if (idx >= MAX_OTHER_PROC_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
        /* forbidden */
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d, pno:%d]", idx, pno);
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
        /* don't arrive here. */
    }

    g_p_proc_id_tbl->id[idx].pno = pno;

    return;
}

/**
 * @brief
 *   Set process name (The table to translate the process name to PNO)
 *
 *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
 *
 * @param[in]    u_int32    idx        Table accessor
 * @param[in]    PCSTR        name    Process name
 *
 * @return none
 */
static void OtherSetNameOfCnvTbl(u_int32 idx, PCSTR name) {
    /* check index */
    if (idx >= MAX_OTHER_PROC_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
        /* forbidden */
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d, name:%s]", idx, name);
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
        /* don't arrive here. */
    } else {
        _tcscpy(g_p_proc_id_tbl->id[idx].name, name);
    }

    return;
}

/**
 * @brief
 *   Get PNO (The table to translate the process name to PNO)
 *
 *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
 *
 * @param[in]    u_int32    idx    Table accessor
 *
 * @return PNO
 */
static PNO OtherGetPnoOfCnvTbl(u_int32 idx) {
    /* check index */
    if (idx >= MAX_OTHER_PROC_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
        /* forbidden */
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
        /* don't arrive here. */
    }

    return g_p_proc_id_tbl->id[idx].pno;
}

/**
 * @brief
 *   Get process name (The table to translate the process name to PNO)
 *
 *   If an invalid value is specified for an argument, the system assumes that it is a design problem and calls _pb_Exit().
 *
 * @param[in]    u_int32    idx    Table accessor
 *
 * @return PCSTR
 */
static PCSTR OtherGetNameOfCnvTbl(u_int32 idx) {
    /* check index */
    if (idx >= MAX_OTHER_PROC_NUM) {  // LCOV_EXCL_BR_LINE 6: idx cannot greater
        /* forbidden */
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Argment ERROR [idx:%d]", idx);
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        _pb_Exit();  // LCOV_EXCL_LINE 6: idx cannot greater
        /* don't arrive here. */
    }

    return g_p_proc_id_tbl->id[idx].name;
}

/**
 * @brief
 *   Retrieve PNO (The table to translate the process name to PNO)
 *
 *   If the PNO specified in the argument exists in the table, return the index for access.
 *   If not exists, return the maximum number of local PNO controls (MAX_OTHER_PROC_NUM).
 *
 * @param[in]    PNO    pno    Process number
 *
 * @return u_int32    Table accessor
 */
static u_int32 OtherSearchPnoOfCnvTbl(PNO pno) {
    u_int32 idx;
    PNO lPno;

    for (idx = 0; idx < MAX_OTHER_PROC_NUM; idx++) {
        lPno = OtherGetPnoOfCnvTbl(idx);

        if (lPno == pno) {
            break;
        }
    }

    return idx;
}

/**
 * @brief
 *   Retrieve process name (The table to translate the process name to PNO)
 *
 *   If the process specified by the argument exists in the table, return the index for access.
 *   If not exists, return the maximum number of local PNO controls (MAX_OTHER_PROC_NUM).
 *
 * @param[in]    PCSTR name Process name
 *
 * @return u_int32    Table accessor
 */
static u_int32 OtherSearchNameOfCnvTbl(PCSTR name) {
    int32    ret;
    u_int32 idx;

    for (idx = 0; idx < MAX_OTHER_PROC_NUM; idx++) {
        ret = _tcscmp(g_p_proc_id_tbl->id[idx].name, name);

        /* If there is a match */
        if (ret == 0) {
            break;
        }
    }

    return idx;
}


/**
 * @brief
 *   Create Mutex for accessing shared info
 *
 * @param[in]    none
 *
 * @return none
 */
static void OtherCreateMutex(void) {
    g_h_mtx = _pb_CreateMutex(NULL, 0, "Other_Mutex");
    if (g_h_mtx == NULL) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "_pb_CreateMutex ERROR [g_h_mtx:%p]", g_h_mtx);
        _pb_Exit();
        /* don't arrive here. */
    }

    return;
}

/**
 * @brief
 *   Delete Mutex for accessing shared info
 *
 * @param[in]  none
 *
 * @return none
 */
static void OtherDeleteMutex(void) {  // LCOV_EXCL_START 8:dead code
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    DWORD    ret;
    ret = PbDeleteMutex(g_h_mtx);
    if (ret != WAIT_OBJECT_0) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "PbDeleteMutex ERROR " \
            "[ret:%lu, g_h_mtx:%p]", ret, g_h_mtx);
        _pb_Exit();
        /* don't arrive here. */
    }

    return;
}
// LCOV_EXCL_STOP

/**
 * @brief
 *   Get Mutex for accessing shared info
 *
 * @param[in]  none
 *
 * @return none
 */
static void OtherLockMutex(void) {
    DWORD    ret;
    ret = PbMutexLock(g_h_mtx, INFINITE);
    if (ret != WAIT_OBJECT_0) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "PbMutexLock ERROR " \
            "[ret:%lu, g_h_mtx:%p]", ret, g_h_mtx);
        _pb_Exit();
        /* don't arrive here. */
    }

    return;
}

/**
 * @brief
 *   Open Mutex for Accessing Shared Info
 *
 * @param[in]  none
 *
 * @return none
 */
static void OtherUnlockMutex(void) {
    BOOL    ret;
    ret = PbMutexUnlock(g_h_mtx);
    if (ret != TRUE) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "PbMutexUnlock ERROR " \
            "[ret:%d, g_h_mtx:%p]", ret, g_h_mtx);
        _pb_Exit();
        /* don't arrive here. */
    }

    return;
}

/**
 * @brief
 *   Get dump information
 *
 * @param[out] pBuf      Dump info
 */
void _pb_GetDebugOtherMngTbl(void* pBuf) {
    static uint8_t  buf[DEBUG_DUMP_MAX_SIZE];
    static uint8_t  bufTmp[64];
    uint32_t         i;

    if (pBuf != NULL) {
        memset(&buf[0], 0x00, sizeof(buf));
        snprintf(reinterpret_cast<char *>(&buf[0]), sizeof(buf), "Other");
        if (g_p_proc_id_tbl == NULL) {  // LCOV_EXCL_BR_LINE 200: g_p_proc_id_tbl can not be NULL
            // LCOV_EXCL_START 200: g_p_proc_id_tbl can not be NULL
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            strncat(reinterpret_cast<char *>(&buf[0]), "\n NULL", strlen("\n NULL"));
            // LCOV_EXCL_STOP
        } else {
            for (i = 0; i < MAX_OTHER_PROC_NUM; i++) {
                memset(&bufTmp[0], 0x00, sizeof(bufTmp));
                snprintf(reinterpret_cast<char *>(&bufTmp[0]), sizeof(bufTmp),
                        "\n [%02d] pno:0x%04x, name:%s",
                        i,
                        g_p_proc_id_tbl->id[i].pno,
                        g_p_proc_id_tbl->id[i].name);
                strncat(reinterpret_cast<char *>(&buf[0]), reinterpret_cast<char *>(&bufTmp[0]), \
                    strlen(reinterpret_cast<char *>(&bufTmp[0])));
            }
        }
        memcpy(pBuf, &buf[0], sizeof(buf));
    }
}

/**
 * @brief
 *   Increment the usage counter of the table to translate process name to PNO
 *
 * @param[in] none
 */
static void OtherIncUseCntOfCnvTbl(void) {
    g_p_proc_id_tbl->use_cnt++;
    return;
}

/**
 * @brief
 *   Increment the counter to reserve the table to translate the process name to PNO
 *
 * @param[in] none
 */
static void OtherIncRsvCntOfCnvTbl(void) {
    g_p_proc_id_tbl->rsv_cnt++;
    return;
}

/**
 * @brief
 *   Decrement the counter to reserve the table to translate the process name to PNO
 *
 * @param[in] none
 */
static void OtherDecRsvCntOfCnvTbl(void) {
    g_p_proc_id_tbl->rsv_cnt--;
    return;
}

/**
 * @brief
 *   Determine resources ready (The table to translate the process name to PNO)
 *
 * @param[in]  none
 *
 * @return BOOL
 * @retval TRUE  : Normal
 * @retval FALSE : Error (Resource shortage)
 */
BOOL _pb_GetOtherResource(void) {
    BOOL ret = TRUE;
    uint32_t cnt;

    OtherLockMutex();

    /* Increment reserved counter */
    OtherIncRsvCntOfCnvTbl();

    cnt = g_p_proc_id_tbl->use_cnt + g_p_proc_id_tbl->rsv_cnt;
    if (cnt >= FULL_OTHER_PROC_NUM) {
        ret = FALSE;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Lack of resources " \
            "[FATAL][use_cnt:%d rsv_cnt:%d]", g_p_proc_id_tbl->use_cnt, g_p_proc_id_tbl->rsv_cnt);
    } else if (cnt >= WARN_OTHER_PROC_NUM) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Lack of resources " \
            "[WARN][use_cnt:%d rsv_cnt:%d]", g_p_proc_id_tbl->use_cnt, g_p_proc_id_tbl->rsv_cnt);
    }

    OtherUnlockMutex();

    return ret;
}

/**
 * @brief
 *   Release resources (The table to translate process name to PNO)
 *
 * @param[in]  none
 *
 * @return none
 */
void _pb_ReleaseOtherResource(void) {
    OtherLockMutex();

    /* Decrement reserved counter */
    OtherDecRsvCntOfCnvTbl();

    OtherUnlockMutex();

    return;
}