2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "_pbSerial.h"
19 #include "WPF_STD_private.h"
20 #include "_pbInternalProc.h"
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;
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 */
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);
40 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
41 * _sys internal public APIs
42 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
44 /****************************************************************************
45 @brief SerialTableInit<BR>
46 Initialize each process
47 @outline SerialTableInit<BR>
48 Initialize each process
49 @type Completion return type
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 */
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
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? */
82 /****************************************************************************
83 @brief SerialObjectTimeoutAdd<BR>
84 Set read/write timeout
85 @outline SerialObjectTimeoutAdd<BR>
86 Set read/write timeout
87 @type Completion return type
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)
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;
104 bret = FindList(&p_list, h_obj);
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;
112 /* The list pointer is expected to be in the list but cannot be retrieved. */
116 /* Not exist in the list */
117 p_list = reinterpret_cast<SERIAL_TABLE*>(malloc(sizeof(SERIAL_TABLE)));
118 if (NULL != p_list) {
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);
126 /* Registration failure */
130 /* Registration success */
134 /* Falied to get memory */
140 /* Parameter error */
148 /****************************************************************************
149 @brief SerialObjectTimeoutGet<BR>
150 Get read/write timeout
151 @outline SerialObjectTimeoutGet<BR>
152 Get read/write timeout
153 @type Completion return type
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)
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;
168 if ((NULL != h_obj) && (NULL != dw_read_timeout) && (NULL != dw_write_timeout)) {
170 bret = FindList(&p_list, h_obj);
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;
178 /* The list pointer is expected to be in the list but cannot be retrieved. */
182 /* Not exist in the list */
187 /* Parameter error */
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
202 @param[in] HANDLE h_obj : Handle to set the mask
203 @param[in] DWORD dw_mask : Value of mask
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;
216 bret = FindList(&p_list, h_obj);
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));
224 /* The list pointer is expected to be in the list but cannot be retrieved. */
228 /* Not exist in the list */
229 p_list = reinterpret_cast<SERIAL_TABLE*>(malloc(sizeof(SERIAL_TABLE)));
230 if (NULL != p_list) {
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);
239 /* Registration failure */
243 /* registration success */
247 /* Failed to get memory */
253 /* Parameter error */
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
268 @param[in] HANDLE h_obj : Handle from which the mask is to be acquired
269 @param[out] DWORD* dw_mask : mask value
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;
280 if ((NULL != h_obj) && (NULL != dw_mask)) {
283 bret = FindList(&p_list, h_obj);
285 /* Exists in the list */
286 if (NULL != p_list) {
287 *dw_mask = p_list->dw_wait_mask;
290 /* The list pointer is expected to be in the list but cannot be retrieved. */
294 /* Not exist in the list */
299 /* Parameter error */
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
314 @param[in] HANDLE h_obj : Handle from which the setting is to be deleted
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;
327 bret = FindList(&p_list, h_obj);
329 /* Already exists in the list */
330 if (NULL != p_list) {
331 bret = DelList(p_list);
335 /* Failed to delete */
338 /* The list pointer is expected to be in the list but cannot be retrieved. */
342 /* Not exist in the list */
346 /* Parameter error */
353 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
355 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
357 /****************************************************************************
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
364 @param[out] SERIAL_TABLE** p_list : Found list pointer
365 @param[in] HANDLE h_obj : Handle to look for
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;
376 if ((NULL != h_obj) && (NULL != p_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) {
389 bret = FALSE; /* Parameter error */
396 /****************************************************************************
398 Append data to the end of the list
400 Append data to the end of the list
401 @type Completion return type
403 @param[in] SERIAL_TABLE* p_list : Data to add
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;
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;
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;
432 bret = FALSE; /* Parameter error */
439 /****************************************************************************
441 Remove Specified Data from a List
443 Remove Specified Data from a List
444 @type Completion return type
446 @param[in,out] SERIAL_TABLE* h_del_obj :
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;
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) {
464 /* Delete first data */
465 g_pst_serial_table = pNow->next;
468 pBef->next = h_del_obj->next;
477 bret = FALSE; /* Parameter error */
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
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
498 if (EOK == pthread_mutex_lock(&g_func_lock_mutex)) {
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
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
519 if (EOK == pthread_mutex_unlock(&g_func_lock_mutex)) {