Revize e6a01bd8
Přidáno uživatelem Pultak před více než 2 roky(ů)
ld_client/LDClient/detection/T32ApiFetcher.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Linq; |
|
4 |
using System.Runtime.InteropServices; |
|
5 |
using System.Text; |
|
6 |
using System.Threading.Tasks; |
|
1 |
using System.Runtime.InteropServices; |
|
7 | 2 |
|
8 |
namespace LDClient.detection {
|
|
3 |
namespace LDClient.detection;
|
|
9 | 4 |
|
10 |
public class T32ApiFetcher : AInfoFetcher, IT32Utils {
|
|
5 |
public class T32ApiFetcher : AInfoFetcher, IT32Utils { |
|
11 | 6 |
#if _WINDOWS |
12 |
private const string T32DllLibrary = "./lib/t32api64.dll"; |
|
7 |
/// <summary> |
|
8 |
/// Path to the Trace32 API library |
|
9 |
/// </summary> |
|
10 |
private const string T32DllLibrary = "./lib/t32api64.dll"; |
|
13 | 11 |
#else |
14 | 12 |
private const string T32DllLibrary = "./lib/t32api64.so"; |
15 | 13 |
#endif |
14 |
/// <summary> |
|
15 |
/// Address of the listening t32 application |
|
16 |
/// </summary> |
|
17 |
private readonly string _t32Address; |
|
18 |
|
|
19 |
/// <summary> |
|
20 |
/// Port of the listening t32 application |
|
21 |
/// </summary> |
|
22 |
private readonly string _t32Port; |
|
23 |
|
|
24 |
/// <summary> |
|
25 |
/// Size of the packets send/received from t32 application |
|
26 |
/// </summary> |
|
27 |
private readonly string _t32PacketLength; |
|
28 |
|
|
29 |
/// <summary> |
|
30 |
/// List of commands to be executed by though the t32 api |
|
31 |
/// </summary> |
|
32 |
private readonly string[] _commands; |
|
33 |
|
|
34 |
/// <summary> |
|
35 |
/// |
|
36 |
/// </summary> |
|
37 |
/// <param name="maxAttempts">Maximum number of attempts to locate and parse the .txt file</param> |
|
38 |
/// <param name="waitPeriodMs">Period (how often) the application tries to locate and parse the .txt file</param> |
|
39 |
/// <param name="infoFilePath">Path to the .txt file which is generated from the debugger</param> |
|
40 |
/// <param name="t32Address">Address of the listening t32 application</param> |
|
41 |
/// <param name="t32Port">Port of the listening t32 application</param> |
|
42 |
/// <param name="t32PacketLength"> Size of the packets send/received from t32 application </param> |
|
43 |
/// <param name="commands"> List of commands to be executed by though the t32 api</param> |
|
44 |
public T32ApiFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string t32Address, |
|
45 |
string t32Port, string t32PacketLength, string[] commands) : base(maxAttempts, waitPeriodMs, infoFilePath) { |
|
46 |
this._t32Address = t32Address; |
|
47 |
this._t32Port = t32Port; |
|
48 |
this._t32PacketLength = t32PacketLength; |
|
49 |
this._commands = commands; |
|
50 |
} |
|
16 | 51 |
|
17 |
private readonly string _t32Address; |
|
18 |
private readonly string _t32Port; |
|
19 |
private readonly string _t32PacketLength; |
|
20 |
private readonly string[] _commands; |
|
21 |
|
|
22 |
public T32ApiFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string t32Address, |
|
23 |
string t32Port, string t32PacketLength, string[] commands) : base(maxAttempts, waitPeriodMs, infoFilePath) { |
|
24 |
this._t32Address = t32Address; |
|
25 |
this._t32Port = t32Port; |
|
26 |
this._t32PacketLength = t32PacketLength; |
|
27 |
this._commands = commands; |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
32 |
private static extern int T32_Config(string s1, string s2); |
|
33 |
|
|
34 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
35 |
private static extern int T32_Init(); |
|
36 | 52 |
|
37 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
38 |
private static extern int T32_Attach(int dev); |
|
53 |
/// <summary> |
|
54 |
/// To see full documentation of following T32 API methods please check the https://www2.lauterbach.com/pdf/api_remote_c.pdf |
|
55 |
/// </summary> |
|
56 |
/// <param name="s1"></param> |
|
57 |
/// <param name="s2"></param> |
|
58 |
/// <returns>return code</returns> |
|
59 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
60 |
private static extern int T32_Config(string s1, string s2); |
|
61 |
|
|
62 |
/// <summary> |
|
63 |
/// To see full documentation of following T32 API methods please check the https://www2.lauterbach.com/pdf/api_remote_c.pdf |
|
64 |
/// </summary> |
|
65 |
/// <returns>Return code</returns> |
|
66 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
67 |
private static extern int T32_Init(); |
|
68 |
|
|
69 |
/// <summary> |
|
70 |
/// To see full documentation of following T32 API methods please check the https://www2.lauterbach.com/pdf/api_remote_c.pdf |
|
71 |
/// </summary> |
|
72 |
/// <param name="dev"></param> |
|
73 |
/// <returns>Return code</returns> |
|
74 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
75 |
private static extern int T32_Attach(int dev); |
|
76 |
|
|
77 |
/// <summary> |
|
78 |
/// To see full documentation of following T32 API methods please check the https://www2.lauterbach.com/pdf/api_remote_c.pdf |
|
79 |
/// </summary> |
|
80 |
/// <param name="command"></param> |
|
81 |
/// <returns>Return code</returns> |
|
82 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
83 |
private static extern int T32_Cmd(string command); |
|
84 |
|
|
85 |
/// <summary> |
|
86 |
/// To see full documentation of following T32 API methods please check the https://www2.lauterbach.com/pdf/api_remote_c.pdf |
|
87 |
/// </summary> |
|
88 |
/// <returns>Return code</returns> |
|
89 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
90 |
private static extern int T32_Exit(); |
|
91 |
|
|
92 |
|
|
93 |
/// <summary> |
|
94 |
/// This method initialized the connection with the trace32 application API. |
|
95 |
/// It setups the standard connection configuration a tries to connect to the API |
|
96 |
/// </summary> |
|
97 |
/// <returns>true on success</returns> |
|
98 |
public bool InitConnection() { |
|
99 |
Program.DefaultLogger.Debug("Trace32 connection initialization"); |
|
100 |
var config1 = T32_Config("NODE=", _t32Address); |
|
101 |
var config2 = T32_Config("PORT=", _t32Port); |
|
102 |
var config3 = T32_Config("PACKLEN=", _t32PacketLength); |
|
103 |
|
|
104 |
if (config1 != 0 || config2 != 0 || config3 != 0) { |
|
105 |
Program.DefaultLogger.Error("Trace32 API connection configuration failed."); |
|
106 |
return false; |
|
107 |
} |
|
108 |
var init = T32_Init(); |
|
109 |
if (init != 0) { |
|
110 |
Program.DefaultLogger.Error("Trace32 API connection init failed."); |
|
111 |
return false; |
|
112 |
} |
|
39 | 113 |
|
40 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
41 |
private static extern int T32_Cmd(string command); |
|
114 |
var attach = T32_Attach(1); |
|
115 |
if (attach != 0) { |
|
116 |
Program.DefaultLogger.Error("Trace32 API connection attach failed."); |
|
117 |
} |
|
42 | 118 |
|
43 |
[DllImport(T32DllLibrary, CharSet = CharSet.Ansi)] |
|
44 |
private static extern int T32_Exit(); |
|
45 |
|
|
46 |
public bool InitConnection() { |
|
47 |
Program.DefaultLogger.Debug("Trace32 connection initialization"); |
|
48 |
var config1 = T32_Config("NODE=", _t32Address); |
|
49 |
var config2 = T32_Config("PORT=", _t32Port); |
|
50 |
var config3 = T32_Config("PACKLEN=", _t32PacketLength); |
|
119 |
Program.DefaultLogger.Info("Trace32 connection established"); |
|
120 |
return true; |
|
121 |
} |
|
51 | 122 |
|
52 |
if (config1 != 0 || config2 != 0 || config3 != 0) { |
|
53 |
Program.DefaultLogger.Error("Trace32 API connection configuration failed."); |
|
54 |
return false; |
|
55 |
} |
|
56 |
var init = T32_Init(); |
|
57 |
if (init != 0) { |
|
58 |
Program.DefaultLogger.Error("Trace32 API connection init failed."); |
|
123 |
/// <summary> |
|
124 |
/// Method executes all passed commands though the trace32 API |
|
125 |
/// </summary> |
|
126 |
/// <returns>true on success</returns> |
|
127 |
public bool ExecuteCommands() { |
|
128 |
Program.DefaultLogger.Info("Trace32 API commands execution."); |
|
129 |
foreach (var command in _commands) { |
|
130 |
Program.DefaultLogger.Debug($"Executing Trace32 command '{command}'."); |
|
131 |
var ret = T32_Cmd(command); |
|
132 |
if (ret != 0) { |
|
133 |
Program.DefaultLogger.Error($"Execution of command '{command}' failed. Return code {ret}."); |
|
59 | 134 |
return false; |
60 | 135 |
} |
61 |
|
|
62 |
var attach = T32_Attach(1); |
|
63 |
if (attach != 0) { |
|
64 |
Program.DefaultLogger.Error("Trace32 API connection attach failed."); |
|
65 |
} |
|
66 |
|
|
67 |
Program.DefaultLogger.Info("Trace32 connection established"); |
|
68 |
return true; |
|
69 |
} |
|
70 |
|
|
71 |
public bool ExecuteCommands() { |
|
72 |
Program.DefaultLogger.Info("Trace32 API commands execution."); |
|
73 |
foreach (var command in _commands) { |
|
74 |
Program.DefaultLogger.Debug($"Executing Trace32 command '{command}'."); |
|
75 |
var ret = T32_Cmd(command); |
|
76 |
if (ret != 0) { |
|
77 |
Program.DefaultLogger.Error($"Execution of command '{command}' failed. Return code {ret}."); |
|
78 |
return false; |
|
79 |
} |
|
80 |
} |
|
81 |
Program.DefaultLogger.Info("All Trace32 commands executed successfully."); |
|
82 |
return true; |
|
83 |
} |
|
84 |
|
|
85 |
public bool CloseConnection() { |
|
86 |
Program.DefaultLogger.Debug("Trace32 connection exit"); |
|
87 |
return T32_Exit() == 0; |
|
88 | 136 |
} |
137 |
Program.DefaultLogger.Info("All Trace32 commands executed successfully."); |
|
138 |
return true; |
|
139 |
} |
|
89 | 140 |
|
90 |
protected override bool FetchData() {
|
|
91 |
var connected = false;
|
|
92 |
for (var i = 0; i < _maxAttempts; i++) {
|
|
93 |
connected = InitConnection();
|
|
94 |
if (connected) {
|
|
95 |
break;
|
|
96 |
}
|
|
97 |
Thread.Sleep((int)_waitPeriodMs);
|
|
141 |
/// <summary>
|
|
142 |
/// This method closes the connection with the trace32 application api
|
|
143 |
/// </summary>
|
|
144 |
/// <returns>true on success</returns>
|
|
145 |
public bool CloseConnection() {
|
|
146 |
Program.DefaultLogger.Debug("Trace32 connection exit");
|
|
147 |
return T32_Exit() == 0;
|
|
148 |
}
|
|
98 | 149 |
|
150 |
/// <summary> |
|
151 |
/// Method tries to fetch the data from the trace32 application. |
|
152 |
/// Upon connection fail it tries again for specified number of times |
|
153 |
/// </summary> |
|
154 |
/// <returns>true on success</returns> |
|
155 |
protected override bool FetchData() { |
|
156 |
var connected = false; |
|
157 |
for (var i = 0; i < _maxAttempts; i++) { |
|
158 |
connected = InitConnection(); |
|
159 |
if (connected) { |
|
160 |
break; |
|
99 | 161 |
} |
100 |
return connected && ExecuteCommands() && CloseConnection(); |
|
162 |
Thread.Sleep((int)_waitPeriodMs); |
|
163 |
|
|
101 | 164 |
} |
165 |
return connected && ExecuteCommands() && CloseConnection(); |
|
102 | 166 |
} |
103 |
} |
|
167 |
} |
Také k dispozici: Unified diff
re #9712 Added documentation of the newly implemented methods + refactoring