XCP_SIM
xcp_tl_timeout.c
1 /*
2  * BlueParrot XCP
3  *
4  * (C) 2007-2022 by Christoph Schueler <github.com/Christoph2,
5  * cpu12.gems@googlemail.com>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * s. FLOSS-EXCEPTION.txt
24  */
25 
27 #include "xcp_tl_timeout.h"
28 #include "xcp.h"
31 /*
32  *
33  * Time-out handling functions for Transport-Layer.
34  *
35  */
36 
37 typedef void (*void_function)(void);
38 
39 static uint32_t XcpTl_TimeoutValue = 0UL;
40 static void_function XcpTl_TimeoutFunction = XCP_NULL;
41 static bool XcpTl_TimeoutRunning = XCP_FALSE;
42 
43 static void XcpTl_TimeoutInit(uint16_t timeout_value,
44  void (*timeout_function)(void)) {
45  XcpTl_TimeoutValue = timeout_value;
46  XcpTl_TimeoutRunning = XCP_FALSE;
47  XcpTl_TimeoutFunction = timeout_function;
48 }
49 
50 static void XcpTl_TimeoutStart(void) {
51  XcpTl_TimeoutValue = XcpHw_GetTimerCounterMS();
52  XcpTl_TimeoutRunning = XCP_TRUE;
53 }
54 
55 static void XcpTl_TimeoutStop(void) { XcpTl_TimeoutRunning = XCP_FALSE; }
56 
57 static void XcpTl_TimeoutCheck(void) {
58  if (!XcpTl_TimeoutRunning) {
59  return;
60  }
61  if ((XcpHw_GetTimerCounterMS() - XcpTl_TimeoutValue) > XcpTl_TimeoutValue) {
62  if (XcpTl_TimeoutFunction != XCP_NULL) {
63  XcpTl_TimeoutFunction();
64  }
65  }
66 }
67 
68 static void XcpTl_TimeoutReset(void) {
69  XcpTl_TimeoutValue = XcpHw_GetTimerCounterMS();
70 }