-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathUartKeywords.cs
210 lines (185 loc) · 7.89 KB
/
UartKeywords.cs
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
//
// Copyright (c) 2010-2024 Antmicro
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
using System;
using Antmicro.Renode.Peripherals.UART;
using Antmicro.Renode.Testing;
using Antmicro.Renode.Time;
namespace Antmicro.Renode.RobotFramework
{
internal class UartKeywords : TestersProvider<TerminalTester, IUART>, IRobotFrameworkKeywordProvider
{
public void Dispose()
{
}
[RobotFrameworkKeyword(replayMode: Replay.Always)]
public void SetDefaultUartTimeout(float timeout)
{
globalTimeout = timeout;
}
[RobotFrameworkKeyword(replayMode: Replay.Always)]
public void SetDefaultTester(int? id)
{
SetDefaultTesterId(id);
}
[RobotFrameworkKeyword]
public string GetTerminalTesterReport(int? testerId = null)
{
return GetTesterOrThrowException(testerId).GetReport();
}
[RobotFrameworkKeyword]
public void ClearTerminalTesterReport(int? testerId = null)
{
GetTesterOrThrowException(testerId).ClearReport();
}
[RobotFrameworkKeyword(replayMode: Replay.Always)]
public int CreateTerminalTester(string uart, float? timeout = null, string machine = null, string endLineOption = null, bool? defaultPauseEmulation = null, bool? defaultMatchNextLine = null)
{
this.defaultPauseEmulation = defaultPauseEmulation.GetValueOrDefault();
this.defaultMatchNextLine = defaultMatchNextLine.GetValueOrDefault();
return CreateNewTester(uartObject =>
{
var timeoutInSeconds = timeout ?? globalTimeout;
TerminalTester tester;
if(Enum.TryParse<EndLineOption>(endLineOption, out var result))
{
tester = new TerminalTester(TimeInterval.FromSeconds(timeoutInSeconds), result);
}
else
{
tester = new TerminalTester(TimeInterval.FromSeconds(timeoutInSeconds));
}
tester.AttachTo(uartObject);
return tester;
}, uart, machine);
}
[RobotFrameworkKeyword]
public TerminalTesterResult WaitForPromptOnUart(string prompt, int? testerId = null, float? timeout = null, bool treatAsRegex = false,
bool? pauseEmulation = null)
{
return WaitForLineOnUart(prompt, timeout, testerId, treatAsRegex, true, pauseEmulation);
}
[RobotFrameworkKeyword]
public TerminalTesterResult WaitForLineOnUart(string content, float? timeout = null, int? testerId = null, bool treatAsRegex = false,
bool includeUnfinishedLine = false, bool? pauseEmulation = null, bool? matchNextLine = null)
{
TimeInterval? timeInterval = null;
if(timeout.HasValue)
{
timeInterval = TimeInterval.FromSeconds(timeout.Value);
}
var tester = GetTesterOrThrowException(testerId);
var result = tester.WaitFor(content, timeInterval, treatAsRegex, includeUnfinishedLine,
pauseEmulation ?? defaultPauseEmulation, matchNextLine ?? defaultMatchNextLine);
if(result == null)
{
OperationFail(tester);
}
return result;
}
[RobotFrameworkKeyword]
public TerminalTesterResult WaitForLinesOnUart(string[] content, float? timeout = null, int? testerId = null, bool treatAsRegex = false,
bool includeUnfinishedLine = false, bool? pauseEmulation = null, bool? matchFromNextLine = null)
{
TimeInterval? timeInterval = null;
if(timeout.HasValue)
{
timeInterval = TimeInterval.FromSeconds(timeout.Value);
}
var tester = GetTesterOrThrowException(testerId);
var result = tester.WaitFor(content, timeInterval, treatAsRegex, includeUnfinishedLine,
pauseEmulation ?? defaultPauseEmulation, matchFromNextLine ?? defaultMatchNextLine);
if(result == null)
{
OperationFail(tester);
}
return result;
}
[RobotFrameworkKeyword]
public void ShouldNotBeOnUart(string content, float? timeout = null, int? testerId = null, bool treatAsRegex = false,
bool includeUnfinishedLine = false)
{
TimeInterval? timeInterval = null;
if(timeout.HasValue)
{
timeInterval = TimeInterval.FromSeconds(timeout.Value);
}
var tester = GetTesterOrThrowException(testerId);
var result = tester.WaitFor(content, timeInterval, treatAsRegex, includeUnfinishedLine, false);
if(result != null)
{
throw new InvalidOperationException($"Terminal tester failed!\n\nUnexpected entry has been found on UART#:\n{result.line}");
}
}
[RobotFrameworkKeyword]
public TerminalTesterResult WaitForNextLineOnUart(float? timeout = null, int? testerId = null, bool? pauseEmulation = null)
{
TimeInterval? timeInterval = null;
if(timeout.HasValue)
{
timeInterval = TimeInterval.FromSeconds(timeout.Value);
}
var tester = GetTesterOrThrowException(testerId);
var result = tester.NextLine(timeInterval, pauseEmulation ?? defaultPauseEmulation);
if(result == null)
{
OperationFail(tester);
}
return result;
}
[RobotFrameworkKeyword]
public TerminalTesterResult SendKeyToUart(byte c, int? testerId = null)
{
return WriteCharOnUart((char)c, testerId);
}
[RobotFrameworkKeyword]
public TerminalTesterResult WriteCharOnUart(char c, int? testerId = null)
{
GetTesterOrThrowException(testerId).Write(c.ToString());
return new TerminalTesterResult(string.Empty, 0);
}
[RobotFrameworkKeyword]
public TerminalTesterResult WriteToUart(string content, int? testerId = null)
{
GetTesterOrThrowException(testerId).Write(content);
return new TerminalTesterResult(string.Empty, 0);
}
[RobotFrameworkKeyword]
public TerminalTesterResult WriteLineToUart(string content = "", int? testerId = null, bool waitForEcho = true, bool? pauseEmulation = null)
{
var tester = GetTesterOrThrowException(testerId);
tester.WriteLine(content);
if(waitForEcho && tester.WaitFor(content, includeUnfinishedLine: true, pauseEmulation: pauseEmulation ?? defaultPauseEmulation) == null)
{
OperationFail(tester);
}
return new TerminalTesterResult(string.Empty, 0);
}
[RobotFrameworkKeyword]
public void TestIfUartIsIdle(float timeout, int? testerId = null, bool? pauseEmulation = null)
{
var tester = GetTesterOrThrowException(testerId);
var result = tester.IsIdle(TimeInterval.FromSeconds(timeout), pauseEmulation ?? defaultPauseEmulation);
if(!result)
{
OperationFail(tester);
}
}
[RobotFrameworkKeyword]
public void WriteCharDelay(float delay, int? testerId = null)
{
var tester = GetTesterOrThrowException(testerId);
tester.WriteCharDelay = TimeSpan.FromSeconds(delay);
}
private void OperationFail(TerminalTester tester)
{
throw new InvalidOperationException($"Terminal tester failed!\n\nFull report:\n{tester.GetReport()}");
}
private bool defaultPauseEmulation;
private bool defaultMatchNextLine;
private float globalTimeout = 8;
}
}