summaryrefslogtreecommitdiffstats
path: root/Master/Masterarbeit/src/XorayaPluginExecutor/CInterfaceManager.cpp
blob: eb196c43758ad5b3b7e6b6d7099fdeb7c4f47f49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*!
 * \file CInterfaceManager.cpp
 * \author S. Eisenhauer
 * \date 25.10.2011
 * \brief Implementation of CInterfaceManager
 */
#include "CInterfaceManager.h"
#include "x2e/CAN.hpp"
#include <stdlib.h>

CInterfaceManager::CInterfaceManager()
:m_pxDrv( x2e::System::OpenDriver() )
,m_enTimer(x2e::System::Timer::TIMER_2)
{
	x2e::status_t enRetCode;
	if( !m_pxDrv)
	{
		ERROR_PRINT("Error getting Driver! Exiting...");
		exit(1);
	}
	enRetCode = x2e::System::Timer::Configure(m_pxDrv, m_enTimer, nTIMER_INTERVAL_US, true, true);
	if( enRetCode != x2e::OK )
	{
		ERROR_PRINT("Error configuring timer %d: %d", m_enTimer, enRetCode);
		exit(1);
	}
	x2e::System::SetLEDState(m_pxDrv,x2e::System::LED_AUX,x2e::System::LED_ON);
	enEnableAllInterfaces();
}

CInterfaceManager::~CInterfaceManager()
{
	DEBUG_PRINT("entry");
	DEBUG_PRINT("exit");
}

x2e::status_t CInterfaceManager::enWriteCanMessage(
		int32_t i32InterfaceHandle,
		uint32_t u32MsgId,
		bool boIsExtended,
		bool boIsRtrFrame,
		const uint8_t* pu8Data,
		uint32_t u32DataLength)
{
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	return x2e::CAN::WriteMessage(
			m_pxDrv,
			i32InterfaceHandle,
			u32MsgId,
			boIsExtended,
			boIsRtrFrame,
			pu8Data,
			u32DataLength);
}

x2e::status_t CInterfaceManager::enGetQueueFillLevel(x2e::Log::queueFillLevel& stFillLevel)
{
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	return x2e::Log::RetrieveFillLevel(m_pxDrv,stFillLevel);
}

x2e::status_t CInterfaceManager::enPeekMessage(
		x2e::Log::queueFillLevel& stFillLevel,
		x2e::Log::messageDescription* pstMsgDescr,
		x2e::Log::framePayload* pstMsgPayload)
{
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	return x2e::Log::PeekNextMessage(m_pxDrv,stFillLevel,pstMsgDescr,pstMsgPayload);
}

x2e::status_t CInterfaceManager::enEnableInterface(int32_t i32RequestedInterface)
{
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	int32_t i32Handle = i32RequestedInterface;
	bool boSendAbility = true;
	x2e::CAN::transceiver enTransceiver= x2e::CAN::TRANSCEIVER_HIGHSPEED;
	x2e::Log::InterfaceId::ifid_t tLogIf;
	x2e::status_t enRes;
	uint32_t u32BaudRate = x2e::CAN::BAUDRATE_1000K;
	//uint32_t u32BaudRate = x2e::CAN::BAUDRATE_500K;
	m_tenState enInterfaceState = nenOn;
	uint32_t u32Slot = 0;
	uint32_t u32SlotPos = 0;
	uint32_t u32AttrSize = 0;

	enRes = x2e::CAN::GetInterface(m_pxDrv,&i32Handle,&enTransceiver,&boSendAbility, &tLogIf);
	if( x2e::OK != enRes)
	{
		ERROR_PRINT("Error getting interface %d: %d",i32Handle,enRes);
		exit(1);
	}
	if( i32Handle != i32RequestedInterface )
	{
		ERROR_PRINT("Request interface %d, got %d. Exiting",i32RequestedInterface,i32Handle);
		exit(1);
	}

	DEBUG_PRINT("Got interface %d",i32Handle);

	if( !boSendAbility )
	{
		ERROR_PRINT("Interface %d is not able to send",i32Handle);
		exit(1);
	}
	DEBUG_PRINT("Interface %d can send",i32Handle);

	enRes = x2e::CAN::SetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_BAUDRATE, &u32BaudRate, sizeof(u32BaudRate));
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("Error setting baudrate for interface %d",i32Handle);
		exit(1);
	}
//	u32AttrSize = sizeof(uint32_t);
//	enRes = x2e::CAN::GetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_BAUDRATE,(void*) &u32BaudRate, &u32AttrSize);
//	if( enRes != x2e::OK )
//	{
//		ERROR_PRINT("Error getting baudrate for interface %d: %d",i32Handle,enRes);
//		exit(1);
//	}
//	if( u32BaudRate == x2e::CAN::BAUDRATE_1000K )
//	{
//		DEBUG_PRINT("set baudrate to 1MBit/s on interface %d",i32Handle);
//	}
//	else
//	{
//		DEBUG_PRINT("baudrate interface %d is %d",i32Handle, u32BaudRate);
//	}

	enRes = SetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_INTERFACE_STATUS, &enInterfaceState, sizeof(m_tenState));
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("error enabling CAN interface %d: %d",i32Handle,enRes);
		exit(1);
	}
	DEBUG_PRINT("enabled interface %d",i32Handle);
	u32AttrSize = sizeof(uint32_t);
	enRes = x2e::CAN::GetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_SLOT,(void*) &u32Slot, &u32AttrSize);
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("Error getting Slot for interface %d: %d",i32Handle,enRes);
		exit(1);
	}
	u32AttrSize = sizeof(uint32_t);
	enRes = x2e::CAN::GetAttribute(m_pxDrv, i32Handle, x2e::CAN::ATTR_POSITION_ON_SLOT,(void*) &u32SlotPos, &u32AttrSize);
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("Error getting position on Slot for interface %d: %d",i32Handle,enRes);
		exit(1);
	}
	DEBUG_PRINT("Interface %d is on Slot %d pos %d",i32Handle,u32Slot,u32SlotPos);
	return x2e::OK;
}

x2e::status_t CInterfaceManager::enEnableAllInterfaces()
{
	x2e::status_t enRes;
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	for( uint32_t u32ActInterface = static_cast<uint32_t>(x2e::CAN::INTERFACE_1);
			u32ActInterface < static_cast<uint32_t>(x2e::CAN::INTERFACE_7);
			u32ActInterface++)
	{
		enRes = this->enEnableInterface(static_cast<int32_t>(u32ActInterface));
		if( enRes != x2e::OK )
		{
			ERROR_PRINT("Error enabling interface %d: %d"
					, static_cast<int32_t>(u32ActInterface),enRes);
			return enRes;
		}
	}
	return enRes;
}

x2e::status_t CInterfaceManager::enGetTimestamp( uint32_t* pu32TsHigh, uint32_t* pu32TsLow )
{
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	return x2e::System::GetCentralTimeStamp(m_pxDrv,pu32TsHigh,pu32TsLow);
}

x2e::status_t CInterfaceManager::enWait()
{
	if( !m_pxDrv )
	{
		return x2e::ERR_NOT_INITIALIZED;
	}
	return x2e::System::Timer::Wait(m_pxDrv, m_enTimer);
}

void CInterfaceManager::vDeinit()
{
	if( !m_pxDrv )
	{
		return;
	}
	x2e::status_t enRes;
	DEBUG_PRINT("entry");
	for( int32_t i32ActInterface = x2e::CAN::INTERFACE_1;
			i32ActInterface < x2e::CAN::INTERFACE_7;
			i32ActInterface++)
	{
		enRes = enDisableInterface( i32ActInterface );
	}
	x2e::System::SetLEDState(m_pxDrv,x2e::System::LED_AUX,x2e::System::LED_OFF);
	x2e::System::CloseDriver(m_pxDrv);
	m_pxDrv = NULL;
	DEBUG_PRINT("exit");
}

x2e::status_t CInterfaceManager::enDisableInterface(int32_t i32Interface)
{
	x2e::status_t enRes = x2e::OK;
	m_tenState enInterfaceState = nenOff;

	DEBUG_PRINT("disabling interface %d",i32Interface);

	enRes = SetAttribute(
			m_pxDrv,
			i32Interface,
			x2e::CAN::ATTR_INTERFACE_STATUS,
			&enInterfaceState,
			sizeof(m_tenState)
	);
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("Error disabling interface %d: %d"
				, i32Interface,enRes);
	}
	enRes = SetAttribute(
			m_pxDrv,
			i32Interface,
			x2e::CAN::ATTR_TRANSCEIVER_STATUS,
			&enInterfaceState,
			sizeof(m_tenState)
	);
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("Error switching tranceiver %d off: %d"
				, i32Interface,enRes);
	}
	enRes = SetAttribute(
			m_pxDrv,
			i32Interface,
			x2e::CAN::ATTR_LED_STATUS,
			&enInterfaceState,
			sizeof(m_tenState)
	);
	if( enRes != x2e::OK )
	{
		ERROR_PRINT("Error switching led %d off: %d"
				, i32Interface,enRes);
	}

	return enRes;
}