Init basesystem source codes.
[staging/basesystem.git] / video_in_hal / vehicleservice / positioning_base_library / library / src / _pbSerial.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 #include "_pbSerial.h"
19 #include "WPF_STD_private.h"
20 #include "_pbInternalProc.h"
21
22 typedef struct SerialTable {
23     HANDLE   h_handle;            /* Registration handle */
24     DWORD    dw_wait_mask;            /* SetMask */
25     DWORD    dw_read_timeout;        /* milisec */
26     DWORD    dw_write_timeout;        /* milisec */
27     struct SerialTable *next;
28 } SERIAL_TABLE;
29
30 static SERIAL_TABLE *g_pst_serial_table = NULL;
31 static pthread_mutex_t g_func_lock_mutex = PTHREAD_MUTEX_INITIALIZER; /* Consider replacing it later */
32
33 /* Prototype declarations */
34 static BOOL FindList(SERIAL_TABLE **p_list, HANDLE h_obj);
35 static BOOL AddList(SERIAL_TABLE *p_add_list);
36 static BOOL DelList(SERIAL_TABLE *h_del_obj);
37 static BOOL FunctionLock(void);
38 static BOOL FunctionUnlock(void);
39
40 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
41 * _sys internal public APIs
42 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
43
44 /****************************************************************************
45 @brief            SerialTableInit<BR>
46                 Initialize each process
47 @outline        SerialTableInit<BR>
48                 Initialize each process
49 @type            Completion return type
50
51 @return            BOOL
52 @retval            TRUE    :    Normal
53 @retval            FALSE    :    Error
54 *****************************************************************************/
55 BOOL SerialTableInit(void) {  // LCOV_EXCL_START 8:dead code
56     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
57     /* do nothing at this time */
58     return TRUE;
59 }
60 // LCOV_EXCL_STOP
61
62 /****************************************************************************
63 @brief            SerialTableTerm<BR>
64                 termination processing for each process
65 @outline        SerialTableTerm<BR>
66                 termination processing for each process
67 @type            Completion return type
68
69 @return            BOOL
70 @retval            TRUE    :    Normal
71 @retval            FALSE    :    Error
72 *****************************************************************************/
73 BOOL SerialTableTerm(void) {  // LCOV_EXCL_START 8:dead code
74     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
75     if (NULL != g_pst_serial_table) {
76         /* delete the list? */
77     }
78     return TRUE;
79 }
80 // LCOV_EXCL_STOP
81
82 /****************************************************************************
83 @brief            SerialObjectTimeoutAdd<BR>
84                 Set read/write timeout
85 @outline        SerialObjectTimeoutAdd<BR>
86                 Set read/write timeout
87 @type            Completion return type
88
89 @param[in]    HANDLE    h_obj            :    Handle to set the timeout
90 @param[in]    DWORD    dw_read_timeout    :    Timeout to read (Millisecond)
91 @param[in]    DWORD    dw_write_timeout    :   Timeout to write (Millisecond)
92
93 @return            BOOL
94 @retval            TRUE    :    Normal
95 @retval            FALSE    :    Error
96 *****************************************************************************/
97 BOOL SerialObjectTimeoutAdd(HANDLE h_obj, DWORD dw_read_timeout, DWORD dw_write_timeout) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
98     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
99     SERIAL_TABLE *p_list = NULL;
100     BOOL bret = FALSE;
101
102     if (NULL != h_obj) {
103         FunctionLock();
104         bret = FindList(&p_list, h_obj);
105         if (TRUE == bret) {
106             /* Already exists in the list */
107             if (NULL != p_list) {
108                 p_list->dw_read_timeout = dw_read_timeout;
109                 p_list->dw_write_timeout = dw_write_timeout;
110                 bret = TRUE;
111             } else {
112                 /* The list pointer is expected to be in the list but cannot be retrieved. */
113                 bret = FALSE;
114             }
115         } else {
116             /* Not exist in the list */
117             p_list = reinterpret_cast<SERIAL_TABLE*>(malloc(sizeof(SERIAL_TABLE)));
118             if (NULL != p_list) {
119                 p_list->next = NULL;
120                 p_list->dw_wait_mask = 0;
121                 p_list->h_handle = h_obj;
122                 p_list->dw_read_timeout = dw_read_timeout;
123                 p_list->dw_write_timeout = dw_write_timeout;
124                 bret = AddList(p_list);
125                 if (FALSE == bret) {
126                     /* Registration failure */
127                     free(p_list);
128                     bret = FALSE;
129                 } else {
130                     /* Registration success */
131                     bret = TRUE;
132                 }
133             } else {
134                 /* Falied to get memory */
135                 bret = FALSE;
136             }
137         }
138         FunctionUnlock();
139     } else {
140         /* Parameter error */
141         bret = FALSE;
142     }
143
144     return bret;
145 }
146 // LCOV_EXCL_STOP
147
148 /****************************************************************************
149 @brief            SerialObjectTimeoutGet<BR>
150                 Get read/write timeout
151 @outline        SerialObjectTimeoutGet<BR>
152                 Get read/write timeout
153 @type            Completion return type
154
155 @param[in]        HANDLE    h_obj            :    Handle for getting the timeout
156 @param[out]        DWORD*    dw_read_timeout    :    Timeout to read (Millisecond)
157 @param[out]        DWORD*    dw_write_timeout    :    Timeout to write (Millisecond)
158
159 @return            BOOL
160 @retval            TRUE    :    Normal
161 @retval            FALSE    :    Error
162 *****************************************************************************/
163 BOOL SerialObjectTimeoutGet(HANDLE h_obj, DWORD *dw_read_timeout, DWORD *dw_write_timeout) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
164     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
165     SERIAL_TABLE *p_list = NULL;
166     BOOL bret = FALSE;
167
168     if ((NULL != h_obj) && (NULL != dw_read_timeout) && (NULL != dw_write_timeout)) {
169         FunctionLock();
170         bret = FindList(&p_list, h_obj);
171         if (TRUE == bret) {
172             /* Exists in the list */
173             if (NULL != p_list) {
174                 *dw_read_timeout = p_list->dw_read_timeout;
175                 *dw_write_timeout = p_list->dw_write_timeout;
176                 bret = TRUE;
177             } else {
178                 /* The list pointer is expected to be in the list but cannot be retrieved. */
179                 bret = FALSE;
180             }
181         } else {
182             /* Not exist in the list */
183             bret = FALSE;
184         }
185         FunctionUnlock();
186     } else {
187         /* Parameter error */
188         bret = FALSE;
189     }
190
191     return bret;
192 }
193 // LCOV_EXCL_STOP
194
195 /****************************************************************************
196 @brief            SerialObjectWaitmaskAdd<BR>
197                 Set the mask of event wait factor
198 @outline        SerialObjectWaitmaskAdd<BR>
199                 Set the mask of event wait factor
200 @type            Completion return type
201
202 @param[in]    HANDLE    h_obj    :    Handle to set the mask
203 @param[in]    DWORD    dw_mask    :    Value of mask
204
205 @return            BOOL
206 @retval            TRUE    :    Normal
207 @retval            FALSE    :    Error
208 *****************************************************************************/
209 BOOL SerialObjectWaitmaskAdd(HANDLE h_obj, DWORD dw_mask) {  // LCOV_EXCL_START 8:dead code
210     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
211     SERIAL_TABLE *p_list = NULL;
212     BOOL bret = FALSE;
213
214     if (NULL != h_obj) {
215         FunctionLock();
216         bret = FindList(&p_list, h_obj);
217         if (TRUE == bret) {
218             /* already exists in the list */
219             if (NULL != p_list) {
220                 /* Clear unused flags */
221                 p_list->dw_wait_mask = (DWORD)((dw_mask) & (EV_RXCHAR | EV_ERROR | EV_DSR));
222                 bret = TRUE;
223             } else {
224                 /* The list pointer is expected to be in the list but cannot be retrieved. */
225                 bret = FALSE;
226             }
227         } else {
228             /* Not exist in the list */
229             p_list = reinterpret_cast<SERIAL_TABLE*>(malloc(sizeof(SERIAL_TABLE)));
230             if (NULL != p_list) {
231                 p_list->next = NULL;
232                 p_list->h_handle = h_obj;
233                 p_list->dw_read_timeout = INFINITE;    /* Infinity wait as initial value */
234                 p_list->dw_write_timeout = INFINITE;    /* Infinity wait as initial value */
235                 /* Clear unused flags */
236                 p_list->dw_wait_mask = (DWORD)((dw_mask) & (EV_RXCHAR | EV_ERROR | EV_DSR));
237                 bret = AddList(p_list);
238                 if (FALSE == bret) {
239                     /* Registration failure */
240                     free(p_list);
241                     bret = FALSE;
242                 } else {
243                     /* registration success */
244                     bret = TRUE;
245                 }
246             } else {
247                 /* Failed to get memory */
248                 bret = FALSE;
249             }
250         }
251         FunctionUnlock();
252     } else {
253         /* Parameter error */
254         bret = FALSE;
255     }
256
257     return bret;
258 }
259 // LCOV_EXCL_STOP
260
261 /****************************************************************************
262 @brief            SerialObjectWaitmaskGet<BR>
263                 Get the set mask value from a handle
264 @outline        SerialObjectWaitmaskGet<BR>
265                 Get the set mask value from a handle
266 @type            Completion return type
267
268 @param[in]        HANDLE    h_obj    :    Handle from which the mask is to be acquired
269 @param[out]        DWORD*    dw_mask    :    mask value
270
271 @return            BOOL
272 @retval            TRUE    :    Normal
273 @retval            FALSE    :    Error
274 *****************************************************************************/
275 BOOL SerialObjectWaitmaskGet(HANDLE h_obj, DWORD *dw_mask) {  // LCOV_EXCL_START 8:dead code
276     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
277     SERIAL_TABLE *p_list = NULL;
278     BOOL bret = FALSE;
279
280     if ((NULL != h_obj) && (NULL != dw_mask)) {
281         *dw_mask = 0;
282         FunctionLock();
283         bret = FindList(&p_list, h_obj);
284         if (TRUE == bret) {
285             /* Exists in the list */
286             if (NULL != p_list) {
287                 *dw_mask = p_list->dw_wait_mask;
288                 bret = TRUE;
289             } else {
290                 /* The list pointer is expected to be in the list but cannot be retrieved. */
291                 bret = FALSE;
292             }
293         } else {
294             /* Not exist in the list */
295             bret = FALSE;
296         }
297         FunctionUnlock();
298     } else {
299         /* Parameter error */
300         bret = FALSE;
301     }
302
303     return bret;
304 }
305 // LCOV_EXCL_STOP
306
307 /****************************************************************************
308 @brief            SerialObjectDel<BR>
309                 Delete Timeout and Mask Setting
310 @outline        SerialObjectDel<BR>
311                 Delete Timeout and Mask Setting
312 @type            Completion return type
313
314 @param[in]    HANDLE    h_obj    :    Handle from which the setting is to be deleted
315
316 @return            BOOL
317 @retval            TRUE    :    Normal
318 @retval            FALSE    :    Error
319 *****************************************************************************/
320 BOOL SerialObjectDel(HANDLE h_obj) {  // LCOV_EXCL_START 8:dead code
321     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
322     SERIAL_TABLE *p_list = NULL;
323     BOOL bret = FALSE;
324
325     if (NULL != h_obj) {
326         FunctionLock();
327         bret = FindList(&p_list, h_obj);
328         if (TRUE == bret) {
329             /* Already exists in the list */
330             if (NULL != p_list) {
331                 bret = DelList(p_list);
332                 if (TRUE == bret) {
333                     free(p_list);
334                 } else {
335                     /* Failed to delete */
336                 }
337             } else {
338                 /* The list pointer is expected to be in the list but cannot be retrieved. */
339                 bret = FALSE;
340             }
341         } else {
342             /* Not exist in the list */
343         }
344         FunctionUnlock();
345     } else {
346         /* Parameter error */
347     }
348
349     return bret;
350 }
351 // LCOV_EXCL_STOP
352
353 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
354 * Private APIs
355 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
356
357 /****************************************************************************
358 @brief            FindList<BR>
359                 Searching for a Handle in the List
360 @outline        FindList<BR>
361                 Searching for a Handle in the List
362 @type            Completion return type
363
364 @param[out]        SERIAL_TABLE**    p_list    :    Found list pointer
365 @param[in]        HANDLE            h_obj    :    Handle to look for
366
367 @return            BOOL
368 @retval            TRUE    :    Normal (p_list != NULL)
369 @retval            FALSE    :    Error (p_list == NULL)
370 *****************************************************************************/
371 static BOOL FindList(SERIAL_TABLE **p_list, HANDLE h_obj) {  // LCOV_EXCL_START 8:dead code
372     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
373     SERIAL_TABLE *pNow = NULL;
374     BOOL bret = FALSE;
375
376     if ((NULL != h_obj) && (NULL != p_list)) {
377         /* Search list */
378         pNow = g_pst_serial_table;
379         while (NULL != pNow) {
380             /* h_obj and pNow->h_handle are pointer type.*/
381             if ((int64_t)h_obj == (int64_t)pNow->h_handle) {
382                 *p_list = pNow;
383                 bret = TRUE;
384                 break;
385             }
386             pNow = pNow->next;
387         }
388     } else {
389         bret = FALSE; /* Parameter error */
390     }
391
392     return bret;
393 }
394 // LCOV_EXCL_STOP
395
396 /****************************************************************************
397 @brief            AddList<BR>
398                 Append data to the end of the list
399 @outline        AddList<BR>
400                 Append data to the end of the list
401 @type            Completion return type
402
403 @param[in]        SERIAL_TABLE*    p_list    :    Data to add
404
405 @return            BOOL
406 @retval            TRUE    :    Normal
407 @retval            FALSE    :    Error
408 *****************************************************************************/
409 static BOOL AddList(SERIAL_TABLE *p_add_list) {  // LCOV_EXCL_START 8:dead code
410     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
411     SERIAL_TABLE *pNow = NULL;
412     BOOL bret = FALSE;
413
414     if (NULL != p_add_list) {
415         /* Add unregistered data */
416         if (NULL == g_pst_serial_table) {
417             g_pst_serial_table = p_add_list;
418             bret = TRUE;
419         } else {
420             /* Add to end of list */
421             pNow = g_pst_serial_table;
422             while (NULL != pNow) {
423                 if (NULL == pNow->next) {
424                     pNow->next = p_add_list;
425                     bret = TRUE;
426                     break;
427                 }
428                 pNow = pNow->next;
429             }
430         }
431     } else {
432         bret = FALSE; /* Parameter error */
433     }
434
435     return bret;
436 }
437 // LCOV_EXCL_STOP
438
439 /****************************************************************************
440 @brief            DelList<BR>
441                 Remove Specified Data from a List
442 @outline        DelList<BR>
443                 Remove Specified Data from a List
444 @type            Completion return type
445
446 @param[in,out]    SERIAL_TABLE*    h_del_obj    :
447
448 @return            BOOL
449 @retval            TRUE    :    Normal
450 @retval            FALSE    :    Error
451 *****************************************************************************/
452 static BOOL DelList(SERIAL_TABLE *h_del_obj) {  // LCOV_EXCL_START 8:dead code
453     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
454     SERIAL_TABLE *pNow = NULL;
455     SERIAL_TABLE *pBef = NULL;
456     BOOL bret = FALSE;
457
458     if (NULL != h_del_obj) {
459         /* Add to end of list */
460         pNow = g_pst_serial_table;
461         while (NULL != pNow) {
462             if (h_del_obj == pNow) {
463                 if (NULL == pBef) {
464                     /* Delete first data */
465                     g_pst_serial_table = pNow->next;
466                 } else {
467                     /* Others */
468                     pBef->next = h_del_obj->next;
469                 }
470                 bret = TRUE;
471                 break;
472             }
473             pBef = pNow;
474             pNow = pNow->next;
475         }
476     } else {
477         bret = FALSE; /* Parameter error */
478     }
479
480     return bret;
481 }
482 // LCOV_EXCL_STOP
483
484 /****************************************************************************
485 @brief            FunctionLock<BR>
486                 Start locking g_pst_serial_table
487 @outline        FunctionLock<BR>
488                 Start locking g_pst_serial_table
489 @type            Completion return type
490
491 @return            BOOL
492 @retval            TRUE    :    Normal
493 @retval            FALSE    :    Error
494 *****************************************************************************/
495 static BOOL FunctionLock(void) {  // LCOV_EXCL_START 8:dead code
496     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
497     BOOL bret = FALSE;
498     if (EOK == pthread_mutex_lock(&g_func_lock_mutex)) {
499         bret = TRUE;
500     }
501     return bret;
502 }
503 // LCOV_EXCL_STOP
504
505 /***************************************************************************
506 @brief            FunctionUnlock<BR>
507                 Terminate locking of g_pst_serial_table
508 @outline        FunctionUnlock<BR>
509                 Terminate locking of g_pst_serial_table
510 @type            Completion return type
511
512 @return            BOOL
513 @retval            TRUE    :    Normal
514 @retval            FALSE    :    Error
515 *****************************************************************************/
516 static BOOL FunctionUnlock(void) {  // LCOV_EXCL_START 8:dead code
517     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
518     BOOL bret = FALSE;
519     if (EOK == pthread_mutex_unlock(&g_func_lock_mutex)) {
520         bret = TRUE;
521     }
522     return bret;
523 }
524 // LCOV_EXCL_STOP
525