BOROSOFTWAREAll work →
scantool/diag_os.h 163 lines
1#ifndef _DIAG_OS_H_
2#define _DIAG_OS_H_
3
4/*
5 * freediag - Vehicle Diagnostic Utility
6 *
7 *
8 * Copyright (C) 2001 Richard Almeida & Ibex Ltd ([email protected])
9 * (c) 2014-2015 fenugrec
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 *************************************************************************
26 *
27 * Public functions; wrappers for OS-specific functions
28 *
29 */
30
31#if defined(__cplusplus)
32extern "C" {
33#endif
34
35#include <stdbool.h>
36
37#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
38 #include <windows.h>
39typedef DWORD OS_ERRTYPE;
40#else
41typedef int OS_ERRTYPE;
42#endif
43
44#define ALARM_TIMEOUT 300 // ms interval timeout for timer callbacks (keepalive etc)
45
46/* Common prototypes but note that the source
47 * is different and defined in OS specific
48 * c files.
49 */
50//init, close : ret 0 if ok
51int diag_os_init(void);
52int diag_os_close(void);
53
54/** Millisecond sleep (blocking)
55 *
56 * @param ms requested delay
57 * @note This makes or breaks freediag... Coding this function
58 * is usually a nightmare on most OS'es. See doc/sourcetree_notes.txt
59 */
60void diag_os_millisleep(unsigned int ms);
61
62/** Check if a key was pressed
63 *
64 * @return 0 if no key was pressed
65 *
66 * Currently, it is only used in a few places to break long loops, with "press any key to stop" semantics.
67 * This returns immediately, to allow polling within a loop.
68 * The linux/unix implementation needs an "Enter" keypress since stdin is buffered !
69 */
70int diag_os_ipending(void);
71
72/** Measure & adjust OS timing performance.
73 *
74 * @note Should be called only once.
75 */
76void diag_os_calibrate(void);
77
78/** Return OS-specific error message
79 *
80 * @return error string or empty string if not found.
81 * @note Caller must not free() the string.
82 * This should only be used from OS-specific code ! (diag_os*.c and diag_tty_*.c )
83 */
84const char *diag_os_geterr(OS_ERRTYPE os_errno);
85
86/** Return current "time" in milliseconds.
87 *
88 * This must use a monotonic (i.e. always increasing) clock source; this
89 * means a *lot* of gettimeofday & similar functions are inadequate.
90 * This is important because this value is used to
91 * calculate time differentials.
92 * Time zero can be any reference unrelated to actual
93 * wall-clock time (unix EPOCH, system boot time, etc.)
94 * This does not need fine resolutions; 15-20ms is good enough.
95 *
96 * @return Monotonic time, in milliseconds, from an arbitrary 0 reference.
97 */
98unsigned long diag_os_getms(void);
99
100/** Get highest-resolution monotonic timestamp available.
101 *
102 * For use as a short duration stopwatch.
103 * Use diag_os_hrtus() to convert delta values to microseconds.
104 * @return Monotonic timestamp, in arbitrary units. @see diag_os_hrtus
105 */
106unsigned long long diag_os_gethrt(void);
107
108/** Convert an hrt timestamp delta to microseconds.
109 *
110 * @return microseconds
111 * @see diag_os_gethrt
112 */
113unsigned long long diag_os_hrtus(unsigned long long hrdelta);
114
115/* mutex wrapper stuff.
116 * the backends use pthread, C11, winAPI etc.
117 * lowest-common-denominator stuff here; regular mutexes (not necessarily recursive etc)
118 */
119#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
120 #include <windows.h>
121// No static mutex initialization on Windows ( CRITICAL_SECTION is an opaque type)
122typedef CRITICAL_SECTION diag_mtx;
123#elif defined(__unix__)
124 #include <pthread.h>
125typedef pthread_mutex_t diag_mtx;
126#else
127 #error Weird compilation environment, report this!
128#endif
129
130/** initialize mutex.
131 * must be deleted with diag_os_delmtx() after use
132 */
133void diag_os_initmtx(diag_mtx *mtx);
134
135/** initialize mutex statically initialized with LOCK_INITIALIZER.
136 * must be deleted with diag_os_delmtx() after use
137 */
138void diag_os_initstaticmtx(diag_mtx *mtx);
139
140/** delete unused mutex
141 */
142void diag_os_delmtx(diag_mtx *mtx);
143
144/** lock mutex, wait if busy */
145void diag_os_lock(diag_mtx *mtx);
146
147/** try to lock mutex, return 0 immediately if failed */
148bool diag_os_trylock(diag_mtx *mtx);
149
150/** unlock mutex */
151void diag_os_unlock(diag_mtx *mtx);
152
153/** move the console cursor up the specified number of lines and to column 1 */
154void diag_os_cursor_up(unsigned int lines);
155
156/** clear the console text from the cursor to the end of the current line */
157void diag_os_clrtoeol(void);
158
159#if defined(__cplusplus)
160}
161#endif
162#endif /*_DIAG_OS_H_ */
163