Fix path to various non-standard directories
[staging/basesystem.git] / service / vehicle / positioning_base_library / library / src / _pbOSCtrl.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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
19  File name      : _pbOSCtrl.cpp
20  System name    :
21  Subsystem name :
22  Title          :
23 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25 #include <unistd.h>
26 #include <stdint.h>
27 #include <sys/mman.h>
28 #include <vehicle_service/positioning_base_library.h>
29 #include "WPF_STD_private.h"
30 #include "_pbInternalProc.h"
31 #include "_pbSerial.h"
32 /* For CreateFile--> */
33 #include <sys/select.h>
34 #include <fcntl.h>
35 #include <strings.h>
36 #include <termios.h>
37 /* For CreateFile <-- */
38
39 #define GPS_FUNC_DEBUG_MSG__CWORD71__FILE 0
40
41 #if GPS_FUNC_DEBUG_MSG__CWORD71__FILE
42 #define FILE_OUT_LINE    50
43 static u_int8    file_buf[FILE_OUT_LINE][1024];
44 static u_int16    line_size_buf[FILE_OUT_LINE];
45
46 /* Log destination selection (Activate the one you want to output.) */
47 #define POS_GPS_R_LOGFILE_PATH "/var/local/lib/basesystem/nv/BS/vs/positioning_base_library/rwdata/Test_Pos_gps_read_data.txt"
48 #define POS_GPS_W_LOGFILE_PATH "/var/local/lib/basesystem/nv/BS/vs/positioning_base_library/rwdata/Test_Pos_gps_write_data.txt"
49
50 #endif /* GPS_FUNC_DEBUG_MSG__CWORD71__FILE */
51
52 /* For CreateFile--> */
53 #define BAUDRATE B9600
54 #define MODEMDEVICE "/dev/tty.gps"
55 /* For CreateFile <-- */
56
57 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
58 * MODULE    : ClearCommError
59 * ABSTRACT  : Get the communication error information and report the current status of the communication device.
60 * NOTE      :
61 * ARGUMENT  :
62 * RETURN    : BOOL defined
63 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
64 /* Wait for replacement */
65 BOOL ClearCommError(HANDLE h_file, LPDWORD lp_errors, LPCOMSTAT lp_stat) {  // LCOV_EXCL_START 8:dead code
66     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
67     return 0;
68 // LCOV_EXCL_STOP
69 }
70
71
72 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
73 * MODULE    : GetCommState
74 * ABSTRACT  : Store the current control settings of the specified communication device in the device control block (DCB struct).
75 * NOTE      :
76 * ARGUMENT  :
77 * RETURN    : BOOL defined
78 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
79 /* Wait for replacement */ /* TODO IHFLOW OHFLOW declarations are missing */
80 BOOL GetCommState(HANDLE h_file, LPDCB lp_dcb) {  // LCOV_EXCL_START 8:dead code
81     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
82     return 1;
83 // LCOV_EXCL_STOP
84 }
85
86 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
87 * MODULE    : GetCommTimeouts
88 * ABSTRACT  : Get the timeout parameters for all read and write operations performed on the specified communication device.
89 * NOTE      :
90 * ARGUMENT  :
91 * RETURN    : BOOL defined
92 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
93
94 BOOL GetCommTimeouts(HANDLE h_file, LPCOMMTIMEOUTS lp_comm_timeouts) {  // LCOV_EXCL_START 8:dead code
95     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
96 #if 1    /* GPF  */
97     BOOL bret = FALSE;
98     DWORD dw_read = 0;
99     DWORD dw_write = 0;
100
101     if ((INVALID_HANDLE_VALUE != h_file) && (NULL != lp_comm_timeouts)) {
102         /* Initialization */
103         memset(lp_comm_timeouts, 0, sizeof(COMMTIMEOUTS));
104         bret = SerialObjectTimeoutGet(h_file, &dw_read, &dw_write);
105         if (TRUE == bret) {
106             lp_comm_timeouts->write_total_timeout_constant = dw_write;
107             if (0 == dw_read) {
108                 /* Return as 0? or return this setting? */
109                 lp_comm_timeouts->read_interval_timeout = INFINITE;
110                 lp_comm_timeouts->read_total_timeout_constant = 0;
111                 lp_comm_timeouts->read_total_timeout_multiplier = 0;
112             } else {
113                 lp_comm_timeouts->read_total_timeout_constant = dw_read;
114             }
115         } else {
116             /* not exist in the list */
117             lp_comm_timeouts->write_total_timeout_constant = INFINITE;
118             lp_comm_timeouts->read_total_timeout_constant = INFINITE;
119             bret = TRUE;
120         }
121     }
122
123     return bret;
124
125 #endif /* GPF  */
126 }
127 // LCOV_EXCL_STOP
128
129 /**
130  * @brief
131  *  Clear Communication Buffer
132  *
133  *  Discard all characters in the output buffer or input buffer of the specified resource
134  *
135  *
136  * @param[in] h_file     Handle of the communication resource
137  * @param[in] dw_flags   Operation to perform
138  *
139  * @return   Processing result
140  * @retval   TRUE      processing succeeded
141  * @retval   FALSE     Processing failed
142  */
143 BOOL PurgeComm(HANDLE h_file, DWORD dw_flags) {  // LCOV_EXCL_START 8:dead code
144     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
145     BOOL ret = 0;
146     int res = -1;
147     /* h_file is FD,  and less than INT_MAX(0x7fffffff) */
148     /* Flush received but unreceived data */
149     res = tcflush(static_cast<int>((long)h_file), TCIFLUSH);  // NOLINT(runtime/int)
150     if (res != -1) {
151         ret = 1;
152     }
153     return ret;
154 }
155 // LCOV_EXCL_STOP
156
157 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
158 * MODULE    : SetCommMask
159 * ABSTRACT  : Specify a set of events to monitor for a specific communication device.
160 * NOTE      :
161 * ARGUMENT  :
162 * RETURN    : BOOL defined
163 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
164
165 BOOL SetCommMask(HANDLE h_file, DWORD dw_evt_mask) {  // LCOV_EXCL_START 8:dead code
166     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
167 #if 1    /* GPF  */
168     BOOL bret = FALSE;
169
170     bret = SerialObjectWaitmaskAdd(h_file, dw_evt_mask);
171     return bret;
172 #endif /* GPF  */
173 }
174 // LCOV_EXCL_STOP
175
176 /**
177  * @brief
178  *
179  * Communication setting
180  *
181  * Set the serial communication according to the setting information.
182  *
183  * @param[in] h_file     Communication handle
184  * @param[in] lp_dcb     Serial port setting information
185  *
186  * @return   Processing result
187  * @retval   TRUE      processing succeeded
188  * @retval   FALSE     Processing failed
189  */
190 BOOL SetCommState(HANDLE h_file, LPDCB lp_dcb) {  // LCOV_EXCL_START 8:dead code
191     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
192     struct termios newtio;
193 #if 1
194     bzero(&newtio, sizeof(newtio));
195     cfmakeraw(&newtio);   /* RAW mode */
196     newtio.c_cflag |= (BAUDRATE |  CS8 | CLOCAL | CREAD);/*Baud:9600*/
197     newtio.c_lflag &= ~ECHO;
198 #endif
199      /* h_file is FD,  and less than INT_MAX(0x7fffffff) */
200     tcflush(static_cast<int>((long)h_file), TCIFLUSH);  // NOLINT(runtime/int)
201     tcsetattr(static_cast<int>((long)h_file), TCSANOW, &newtio);  // NOLINT(runtime/int)
202
203     return 1;
204 }
205 // LCOV_EXCL_STOP
206
207
208 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
209 * MODULE    : SetCommTimeouts
210 * ABSTRACT  : Set the timeout parameter for all read and write operations performed on the specified communication device.
211 * NOTE      :
212 * ARGUMENT  :
213 * RETURN    : BOOL defined
214 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
215
216 BOOL SetCommTimeouts(HANDLE h_file, LPCOMMTIMEOUTS lp_comm_timeouts) {  // LCOV_EXCL_START 8:dead code
217     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
218 #if 1    /* GPF  */
219     BOOL bret = FALSE;
220     DWORD dw_read_timeout = INFINITE;
221     DWORD dw_write_timeout = INFINITE;
222
223     /* Calculate timeout time */
224     if ((INFINITE == lp_comm_timeouts->read_interval_timeout)
225             && (0 == lp_comm_timeouts->read_total_timeout_constant)
226             && (0 == lp_comm_timeouts->read_total_timeout_multiplier)) {
227         /* Set without waiting */
228         dw_read_timeout = 0;
229     } else {
230         dw_read_timeout = lp_comm_timeouts->read_total_timeout_constant;
231     }
232
233
234     dw_write_timeout = lp_comm_timeouts->write_total_timeout_constant;
235
236     bret = SerialObjectTimeoutAdd(h_file, dw_read_timeout, dw_write_timeout);
237
238     return bret;
239 #endif /* GPF  */
240 }
241 // LCOV_EXCL_STOP
242
243 /**
244  * @brief
245  *
246  * Waiting for communication event
247  *
248  * Wait for an event to be generated for a specified handle
249  * Note : Continue to wait indefinitely until data reception/error occurs
250  *
251  * @param[in]  h_file         Communication handle
252  * @param[out] lp_evt_mask     Pointer to the variable to receive the event
253  * @param[in]  lp_overlapped  OVERLAPPED Pointer to a struct[Note:Not used]
254  *
255  * @return   Processing result
256  * @retval   TRUE     Processing succeeded
257  * @retval   FALSE    Processing failed
258  */
259 BOOL WaitCommEvent(HANDLE h_file, LPDWORD lp_evt_mask, LPOVERLAPPED lp_overlapped) {  // LCOV_EXCL_START 8:dead code
260     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
261     BOOL ret = 0;
262     int res = -1;
263     int fd;
264     fd_set fds_set_err;
265     fd_set fds_set;    // Set of file descriptor
266
267     struct timeval tv;
268
269     /* Monitor for 0.5 seconds */
270     tv.tv_sec = 1;
271     tv.tv_usec = 0;
272      /* h_file is FD,  and less than INT_MAX(0x7fffffff) */
273     fd = static_cast<int>((long)h_file);  // NOLINT(runtime/int)
274
275     /* Initialization */
276     FD_ZERO(&fds_set);
277     FD_ZERO(&fds_set_err);
278
279     FD_SET(fd, &fds_set);
280     FD_SET(fd, &fds_set_err);
281
282     res = select(fd + 1, &fds_set, NULL, &fds_set_err, &tv);
283
284     if (res > 0) {
285         if (FD_ISSET(fd, &fds_set)) {
286             *lp_evt_mask = EV_RXCHAR;
287             ret = 1;
288
289         } else {
290             *lp_evt_mask = EV_ERROR;
291             ret = 0;
292         }
293     } else {
294         ret = 0;
295     }
296     return ret;
297 }
298 // LCOV_EXCL_STOP
299
300 /**
301  * @brief
302  *
303  * Create file
304  *
305  * Create or open an object and return a handle which can be used to access the object
306  *
307  * @param lp_file_name                Not used
308  * @param dw_desired_access           Not used
309  * @param dw_share_mode               Not used
310  * @param lp_security_attributes      Not used
311  * @param dw_creation_disposition     Not used
312  * @param dw_flags_and_attributes     Not used
313  * @param h_template_file               Not used
314  *
315  * @return   Handle
316  */
317 HANDLE CreateFile(LPCTSTR lp_file_name, DWORD dw_desired_access, DWORD dw_share_mode, LPSECURITY_ATTRIBUTES lp_security_attributes, DWORD dw_creation_disposition, DWORD dw_flags_and_attributes, HANDLE h_template_file) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
318     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
319     int fd;
320     int loop = 1;
321
322     while (loop == 1) {
323         fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
324         if (fd != -1) {
325             break;
326         }
327         sleep(1);
328     }
329     /*  fd is FD,  and less than INT_MAX(0x7fffffff) and the return data type is HANDLE. */
330     return (HANDLE)((long)(fd));  // NOLINT(runtime/int)
331 }
332 // LCOV_EXCL_STOP
333
334 /**
335  * @brief
336  *
337  * Close file
338  *
339  * Close an open file
340  *
341  * @param[in] h_object   Handle
342  *
343  * @return   Processing result
344  * @retval   TRUE     Processing succeeded
345  * @retval   FALSE    Processing failed
346  */
347 BOOL CloseFile(HANDLE h_object) {  // LCOV_EXCL_START 8:dead code
348     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
349     if (h_object != reinterpret_cast<void*>(-1)) {
350          /* h_object is FD,  and less than INT_MAX(0x7fffffff) */
351         close(static_cast<int>((long)h_object));  // NOLINT(runtime/int)
352     } else {
353         /* nop */
354     }
355     return 0;
356 }
357 // LCOV_EXCL_STOP
358
359 /**
360  * @brief
361  *
362  * Write File
363  *
364  * Writing Data to a File
365  *
366  * @param[in]  h_file                  Handle
367  * @param[in]  lp_buffer               buffer to write
368  * @param[out] n_number_of_bytes_to_write  Maximum writing size
369  * @param[out] lpNumberOfBytesWrite   Writing size
370  * @param      lp_overlapped           Not used
371  *
372  * @return   Processing result
373  * @retval   TRUE     Processing succeeded
374  * @retval   FALSE    Processing failed
375  */
376 BOOL WriteFile(HANDLE h_file, LPCVOID lp_buffer, DWORD n_number_of_bytes_to_write, LPDWORD lp_number_of_bytes_written, LPOVERLAPPED lp_overlapped) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)
377     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
378     int res = -1;
379     BOOL ret = 0;
380     int fd;
381     fd_set fds_set;    /* Set of file descriptors */
382     fd_set fds_set_err;
383
384     struct timeval tv;
385
386 #if GPS_FUNC_DEBUG_MSG__CWORD71__FILE
387     static FILE*            fp = NULL;              /* For debugging            */
388     int i;
389 #endif /* GPS_FUNC_DEBUG_MSG__CWORD71__FILE */
390
391     /* monitor for 2 seconds.*/
392     tv.tv_sec = 2;
393     tv.tv_usec = 0;
394      /* h_file is FD,  and less than INT_MAX(0x7fffffff) */
395     fd = static_cast<int>((long)h_file);  // NOLINT(runtime/int)
396
397     /* Initialization */
398     FD_ZERO(&fds_set);
399     FD_ZERO(&fds_set_err);
400
401     FD_SET(fd, &fds_set);
402     FD_SET(fd, &fds_set_err);
403
404     res = select(fd + 1, &fds_set, NULL, &fds_set_err, &tv);
405     if (res > 0) {
406         if (FD_ISSET(fd, &fds_set)) {
407              /* h_file is FD,  and less than INT_MAX(0x7fffffff) */
408             res = static_cast<int>(write(static_cast<int>((long)h_file), lp_buffer,  // NOLINT(runtime/int)
409                     n_number_of_bytes_to_write));
410             *lp_number_of_bytes_written = res;
411
412 #if GPS_FUNC_DEBUG_MSG__CWORD71__FILE
413             if (fp == NULL) {
414                 /* File initialization */
415                 fp = fopen(POS_GPS_W_LOGFILE_PATH, "w+");
416             } else {
417                 fp = fopen(POS_GPS_W_LOGFILE_PATH, "a+");
418             }
419             for (i = 0; i < n_number_of_bytes_to_write; i++) {
420                 fprintf(fp, "%02x ", *(reinterpret_cast<char *>(lp_buffer) + i));
421             }
422             fprintf(fp, "\n");
423             fclose(fp);
424 #endif /* GPS_FUNC_DEBUG_MSG__CWORD71__FILE */
425         }
426     } else {
427     }
428
429     if (res != -1) {
430         ret = 1;
431     }
432
433     return ret;
434 }
435 // LCOV_EXCL_STOP
436
437 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
438 * MODULE    : Wsprintf
439 * ABSTRACT  : Format a string and store the value in a buffer. If any of the arguments are passed,
440 *            Format according to the corresponding format specifier in the format control string and copies it to the output buffer.
441 * NOTE      :
442 * ARGUMENT  :
443 * RETURN    : int defined
444 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
445 int Wsprintf(LPTSTR lp_out, LPCTSTR lp_fmt, ...) {  // LCOV_EXCL_START 8:dead code
446     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
447 #if 1    /* GPF  */
448     int rtn;
449     va_list args;
450
451     va_start(args, lp_fmt);
452     /* Seems to be occured problems because the buffer size is not known. */
453     rtn = vswprintf(reinterpret_cast<wchar_t*>(lp_out), 256, reinterpret_cast<const wchar_t*>(lp_fmt), args);
454     va_end(args);
455
456     return rtn;
457 #endif /* GPF  */
458 }
459 // LCOV_EXCL_STOP
460
461 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
462 * MODULE    : PbSystemTimeToFileTime
463 * ABSTRACT  : Convert the system date and time to 64-bit format file time.
464 * NOTE      :
465 * ARGUMENT  :
466 * RETURN    : BOOL defined
467 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
468 BOOL PbSystemTimeToFileTime(const SYSTEMTIME* lp_system_time, LPFILETIME lp_file_time) {  // LCOV_EXCL_START 8:dead code
469     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
470     return FALSE;
471 }
472 // LCOV_EXCL_STOP
473
474 /**
475  * Function name : MunmapDeviceIo<br>
476  *          Register unmapping<br>
477  *
478  * Overview   : Remove the mapping of a register to memory.<br>
479  *
480  * -# Release the mapping of the specified register to memory.
481  *
482  * @param h_dev      [I]Handle to the device object
483  * @param map_size   [I]Size of mapped device I/O memory
484  *
485  * @return   RET_API      Processing result
486  * @retval   RET_NORMAL   Processing succeeded
487  * @retval   RET_ERROR    Processing failed
488  */
489 /* Wait for replacement */
490 RET_API MunmapDeviceIo(HANDLE h_dev, u_int32 map_size) {  // LCOV_EXCL_START 8:dead code
491     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
492     return RET_NORMAL;
493 // LCOV_EXCL_STOP
494 }
495
496 /**
497  * Function name : MmapDeviceIo<br>
498  *          Register mapping<br>
499  *
500  * Overview   : Map registers to memory.<br>
501  *
502  * -# Map the specified register to memory.
503  *
504  * @param map_size   [I]Size of mapped device I/O memory
505  * @param map_addr   [I]Mapped physical address
506  *
507  * @return   HANDLE    Processing result
508  * @retval   Except NULL  Handle
509  * @retval   NULL      Processing failed
510  */
511 /* Wait for replacement */
512 HANDLE MmapDeviceIo(u_int32 map_size, u_int32 map_addr) {  // LCOV_EXCL_START 8:dead code
513     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
514     return RET_NORMAL;
515 // LCOV_EXCL_STOP
516 }
517
518 /**
519  * @brief
520  *
521  * Kill
522  *
523  * Exit program
524  *
525  * @param[in]  p_func         Calling function
526  * @param[in]  line          Number of caller rows
527  */
528 void _pb_Exit_d(const char* p_func, int line) {  // LCOV_EXCL_START 8:dead code  // NOLINT(whitespace/line_length)  // NOLINT(readability/nolint)  WPF_SYSAPI.h API
529     AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
530     int ret = -1;
531
532     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "FORBIDDEN ERROR [Called by:%s, Line:%d]", p_func, line);
533     exit(ret);
534
535     return;
536 }
537 // LCOV_EXCL_STOP
538
539
540 /* GPF001_sample_ttaka add end */
541