Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 6cde905f

Přidáno uživatelem Pultak před více než 2 roky(ů)

re #9712 Added t32 api fetcher

Zobrazit rozdíly:

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;
7

  
8
namespace LDClient.detection {
9

  
10
    public class T32ApiFetcher : AInfoFetcher, IT32Utils {
11
#if _WINDOWS
12
        private const string T32DllLibrary = "./lib/t32api64.dll";
13
#else
14
        private const string T32DllLibrary = "./lib/t32api64.so";
15
#endif
16

  
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

  
37
        [DllImport(T32DllLibrary, CharSet = CharSet.Ansi)]
38
        private static extern int T32_Attach(int dev);
39

  
40
        [DllImport(T32DllLibrary, CharSet = CharSet.Ansi)]
41
        private static extern int T32_Cmd(string command);
42

  
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);
51

  
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.");
59
                return false;
60
            }
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
        }
89

  
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);
98

  
99
            }
100
            return connected && ExecuteCommands() && CloseConnection();
101
        }
102
    }
103
}
ld_client/LDClient/detection/T32RemFetcher.cs
1
using System.Diagnostics;
2
using LDClient.utils;
3
using LDClient.utils.loggers;
4

  
5
namespace LDClient.detection {
6

  
7
    public class T32RemFetcher : AInfoFetcher{
8

  
9
        /// <summary>
10
        /// Path to the t32rem.exe file which is used to send commands to the debugger.
11
        /// </summary>
12
        private readonly string _f32RemExecutable;
13

  
14
        /// <summary>
15
        /// Arguments (commands) sent to the debugger in order to generate a .txt file
16
        /// containing all the desired information.
17
        /// </summary>
18
        private readonly string[] _f32RemArguments;
19

  
20
        /// <summary>
21
        /// Status code indicating a successful termination of t32rem.exe
22
        /// </summary>
23
        private readonly int _f32SuccessExitCode;
24

  
25

  
26
        /// <summary>
27
        /// Timeout used when waiting for the t32rem.exe to finish.
28
        /// </summary>
29
        private readonly int _f32WaitTimeoutMs;
30

  
31
        /// <summary>
32
        /// Instance of ProcessUtils which encapsulates common functionality
33
        /// when it comes to dealing with processes (limited by the needs of this application).
34
        /// </summary>
35
        public IProcessUtils ProcessUtils;
36

  
37
        /// <summary>
38
        /// Creates an instance of this class.
39
        /// </summary>
40
        /// <param name="maxAttempts">Maximum number of attempts to locate and parse the .txt file</param>
41
        /// <param name="waitPeriodMs">Period (how often) the application tries to locate and parse the .txt file</param>
42
        /// <param name="infoFilePath">Path to the .txt file which is generated from the debugger</param>
43
        /// <param name="f32RemExecutable">Path to the t32rem.exe file which is used to send commands to the debugger</param>
44
        /// <param name="f32RemArguments">Arguments (commands) sent to the debugger in order to generate a .txt file containing all the desired information.</param>
45
        /// <param name="f32SuccessExitCode">Status code indicating a successful termination of t32rem.exe</param>
46
        /// <param name="f32WaitTimeoutMs">Timeout used when waiting for the t32rem.exe to finish</param>
47

  
48
        public T32RemFetcher(uint maxAttempts, uint waitPeriodMs, string infoFilePath, string f32RemExecutable,
49
            string[] f32RemArguments, int f32SuccessExitCode, int f32WaitTimeoutMs) : base(maxAttempts, waitPeriodMs, infoFilePath) {
50
            _f32RemExecutable = f32RemExecutable;
51
            _f32RemArguments = f32RemArguments;
52
            _f32SuccessExitCode = f32SuccessExitCode;
53
            _f32WaitTimeoutMs = f32WaitTimeoutMs;
54
            ProcessUtils = new ProcessUtils();
55

  
56
        }
57

  
58
        protected override bool FetchData() {
59
            if (_f32RemArguments == null) {
60
                Program.DefaultLogger.Error($"Failed to run {_f32RemExecutable} - no parameters were given");
61
                return false;
62
            }
63
            // Execute one all arguments (commands) one by one.
64
            foreach (var argument in _f32RemArguments) {
65
                if (!ProcessUtils.ExecuteNewProcess(_f32RemExecutable, argument, _f32WaitTimeoutMs, _f32SuccessExitCode)) {
66
                    return false;
67
                }
68
            }
69
            return true;
70
        }
71
    }
72
}

Také k dispozici: Unified diff