123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- using ST_CUBE_MES.Model;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace ST_CUBE_MES.Service
- {
- public class STLinkCliPrograrmService
- {
- public STLinkCliPrograrmService(string stlCliPath = "", string port = "", string customDefaultOption = null)
- {
- this._cli = new STLinkCliWrapService(stlCliPath, port, customDefaultOption);
- }
- public delegate void OnMsgRecevicedEvent(string msg);
- public event OnMsgRecevicedEvent OnMsgReceviced;
- public delegate void OnProgressChangedEvent(int currentStep, int maxStep);
- public event OnProgressChangedEvent OnProgressChanged;
- public string CliPath => _cli.CliPath;
- private readonly STLinkCliWrapService _cli;
- public Result StartProgramProcess(string filePath)
- {
- Result result = new Result() { IsSuccess = false };
- return result;
- result.Step = 1;
- var setOptionByteResult = SetOptionByte();
- if (setOptionByteResult != null)
- {
- result.ErrorMsg = setOptionByteResult.ErrorMsg;
- foreach (var pair in setOptionByteResult.Data)
- {
- result.Data[pair.Key] = pair.Value;
- }
- }
- if (setOptionByteResult == null || !setOptionByteResult.IsSuccess)
- {
- return result;
- }
- OnProgressChanged?.Invoke(1, 4);
- result.Step = 2;
- var eraseResult = Erase();
- if (eraseResult != null)
- {
- result.ErrorMsg = eraseResult.ErrorMsg;
- foreach (var pair in eraseResult.Data)
- {
- result.Data[pair.Key] = pair.Value;
- }
- }
- if (eraseResult == null || !eraseResult.IsSuccess)
- {
- return result;
- }
- OnProgressChanged?.Invoke(2, 4);
- result.Step = 3;
- var startProgramResult = StartProgram(filePath);
- if (startProgramResult != null)
- {
- result.ErrorMsg = startProgramResult.ErrorMsg;
- foreach (var pair in startProgramResult.Data)
- {
- result.Data[pair.Key] = pair.Value;
- }
- }
- if (startProgramResult == null || !startProgramResult.IsSuccess)
- {
- return result;
- }
- OnProgressChanged?.Invoke(3, 4);
- result.Step = 4;
- var getCheckSumResult = GetCheckSum(filePath);
- if (getCheckSumResult != null)
- {
- result.ErrorMsg = getCheckSumResult.ErrorMsg;
- foreach (var pair in getCheckSumResult.Data)
- {
- result.Data[pair.Key] = pair.Value;
- }
- }
- if (getCheckSumResult == null || !getCheckSumResult.IsSuccess)
- {
- return result;
- }
- OnProgressChanged?.Invoke(4, 4);
- result.IsSuccess = true;
- return result;
- }
- public Result SetOptionByte()
- {
- var result = new Result() { IsSuccess = true };
- _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
- var resultInt = _cli.SetOptionByte();
- _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
- result.IsSuccess = resultInt == 0;
- return result;
- void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
- {
- if (msg is null)
- return;
- if (msg.Contains("Error: No debug probe detected"))
- {
- result.ErrorMsg = "Error: No debug probe detected";
- }
- OnMsgReceviced?.Invoke(msg);
- }
- }
- public Result Erase()
- {
- var result = new Result() { IsSuccess = true };
- _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
- var resultInt = _cli.Erase();
- _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
- result.IsSuccess = resultInt == 0;
- return result;
- void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
- {
- if (msg is null)
- return;
- if (msg.Contains("Error: No debug probe detected"))
- {
- result.ErrorMsg = "Error: No debug probe detected";
- }
- OnMsgReceviced?.Invoke(msg);
- }
- }
- public Result GetCheckSum(string filePath)
- {
- if (!File.Exists(filePath))
- {
- return new Result()
- {
- IsSuccess = false,
- ErrorMsg = "ERROR: Check file not found",
- };
- }
- var result = new Result() { IsSuccess = true };
- _cli.OnMsgReceviced += stlCli_OnMsgReceviced_StartProgram;
- var resultInt = _cli.GetCheckSum(filePath);
- _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_StartProgram;
- result.IsSuccess = resultInt == 0;
- return result;
- void stlCli_OnMsgReceviced_StartProgram(string msg)
- {
- if (msg is null)
- return;
- Regex checksumRegex = new Regex("^(.*)Checksum = (0x.*)$");
- var match = checksumRegex.Match(msg);
- if (match.Success &&
- match.Groups.Count > 2 &&
- match.Groups[2].Value.StartsWith("0x"))
- {
- var checksum = match.Groups[2].Value;
- result.Data.Add("checksum", checksum);
- result.Data.Add("fileName", Path.GetFileName(filePath));
- OnMsgReceviced?.Invoke($"checksum: {checksum}");
- }
- if (msg.Contains("Error: No debug probe detected"))
- {
- result.ErrorMsg = "Error: No debug probe detected";
- }
- OnMsgReceviced?.Invoke(msg);
- }
- }
- public Result StartProgram(string filePath)
- {
- if (!File.Exists(filePath))
- {
- return new Result()
- {
- IsSuccess = false,
- ErrorMsg = "ERROR: Program file not found",
- };
- }
- var result = new Result() { IsSuccess = true };
- _cli.OnMsgReceviced += stlCli_OnMsgReceviced_StartProgram;
- var resultInt = _cli.StartProgram(filePath);
- _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_StartProgram;
- result.IsSuccess = resultInt == 0;
- return result;
- void stlCli_OnMsgReceviced_StartProgram(string msg)
- {
- if (msg is null)
- return;
- if (msg.Contains("Error: No debug probe detected"))
- {
- result.ErrorMsg = "Error: No debug probe detected";
- }
- OnMsgReceviced?.Invoke(msg);
- }
- }
- public Result GetMemCheckSum()
- {
- var result = new Result() { IsSuccess = true };
- _cli.OnMsgReceviced += stlCli_OnMsgReceviced_SetDefaultOptions;
- var resultInt = _cli.GetMemCheckSum();
- _cli.OnMsgReceviced -= stlCli_OnMsgReceviced_SetDefaultOptions;
- result.IsSuccess = resultInt == 0;
- return result;
- void stlCli_OnMsgReceviced_SetDefaultOptions(string msg)
- {
- if (msg is null)
- return;
- Regex checksumRegex = new Regex("^(.*)Checksum : (0x.*)$");
- var match = checksumRegex.Match(msg);
- if (match.Success &&
- match.Groups.Count > 2 &&
- match.Groups[2].Value.StartsWith("0x"))
- {
- var checksum = match.Groups[2].Value;
- //result.Data.Add("checksum", checksum);
- OnMsgReceviced?.Invoke($"memory checksum: {checksum}");
- }
- if (msg.Contains("Error: No debug probe detected"))
- {
- result.ErrorMsg = "Error: No debug probe detected";
- }
- OnMsgReceviced?.Invoke(msg);
- }
- }
- }
- }
|