feat: add bsdl parser
This commit is contained in:
parent
7aff4f3f02
commit
351a230107
|
@ -1,3 +0,0 @@
|
|||
[submodule "server/src/BsdlParser"]
|
||||
path = server/src/BsdlParser
|
||||
url = git@github.com:SikongJueluo/python-bsdl-parser.git
|
|
@ -1,4 +1,5 @@
|
|||
obj
|
||||
bin
|
||||
bitstream
|
||||
bsdl
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -25,7 +25,6 @@
|
|||
<PackageReference Include="NLog" Version="5.4.0" />
|
||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.4.0" />
|
||||
<PackageReference Include="NSwag.AspNetCore" Version="14.3.0" />
|
||||
<PackageReference Include="pythonnet" Version="3.0.5" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
Subproject commit ac164eb16d7d9a9a387ff1f62cef249c65565bef
|
|
@ -0,0 +1,167 @@
|
|||
using DotNext;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace BsdlParser;
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public class BoundaryScanRegs
|
||||
{
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public class CellEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("cell_number")]
|
||||
[JsonRequired]
|
||||
public int? CellNumber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("cell_name")]
|
||||
[JsonRequired]
|
||||
public string? CellName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("port_id")]
|
||||
public string? PortID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("function")]
|
||||
[JsonRequired]
|
||||
public string? Function { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("safe_bit")]
|
||||
[JsonRequired]
|
||||
public string? SafeBit { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("ccell")]
|
||||
public string? CCell { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("disabel_value")]
|
||||
public string? DisableValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("disabel_result")]
|
||||
public string? DisableResult { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("register_length")]
|
||||
[JsonRequired]
|
||||
public int RegisterLength { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
[JsonProperty("registers")]
|
||||
[JsonRequired]
|
||||
public CellEntry[] Registers { get; set; } = new CellEntry[] { };
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public class Parser
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
private const string BOUNDARY_REGS_DESP = "boundary_registers.json";
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public JObject BoundaryRegsDesp { get; }
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public Parser()
|
||||
{
|
||||
var filePath = Path.Combine(Environment.CurrentDirectory, BOUNDARY_REGS_DESP);
|
||||
if (!Path.Exists(filePath))
|
||||
throw new Exception($"Counld not find boundary_registers.json in {filePath}");
|
||||
|
||||
this.BoundaryRegsDesp = JObject.Parse(File.ReadAllText(filePath));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
public Optional<int> GetBoundaryRegsNum()
|
||||
{
|
||||
var ret = this.BoundaryRegsDesp["register_length"];
|
||||
if (ret is null) return new();
|
||||
return Convert.ToInt32(ret);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public Optional<List<BoundaryScanRegs.CellEntry>> GetBoundaryLogicalPorts()
|
||||
{
|
||||
var registers = this.BoundaryRegsDesp["registers"]?.ToList();
|
||||
if (registers is null) return new();
|
||||
|
||||
var cellList = registers.Where((item) =>
|
||||
{
|
||||
return item["port_id"] is not null;
|
||||
});
|
||||
if (cellList is null) return new();
|
||||
|
||||
}
|
||||
|
||||
// public Result<string> GetLogicalPorts()
|
||||
// {
|
||||
// using (Py.GIL())
|
||||
// {
|
||||
// using (PyModule scope = Py.CreateScope())
|
||||
// {
|
||||
// string code = $@"
|
||||
// bsdl_parser = BsdlParser({this.filePath})
|
||||
// result = json.dumps(bsdl_parser.GetLogicPortDesp(), indent=2)
|
||||
// ";
|
||||
//
|
||||
// var localVariables = new PyDict();
|
||||
// scope.Exec(code, localVariables);
|
||||
// if (!localVariables.HasKey("result"))
|
||||
// return new(new Exception($"PythonNet doesn't has result from dict: {localVariables}"));
|
||||
//
|
||||
// var result = localVariables.GetItem("result");
|
||||
// if (result is null)
|
||||
// return new(new Exception($"PythonNet get null from dict: {localVariables}"));
|
||||
//
|
||||
// var resultString = result.ToString();
|
||||
// if (resultString is null)
|
||||
// return new(new Exception($"Pythonnet convert PyObject to string failed :{result}"));
|
||||
// return resultString;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
|
@ -337,17 +337,16 @@ public class JtagController : ControllerBase
|
|||
/// </summary>
|
||||
/// <param name="address">[TODO:parameter]</param>
|
||||
/// <param name="port">[TODO:parameter]</param>
|
||||
/// <param name="portNum">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
[HttpPost("BoundaryScan")]
|
||||
[EnableCors("Users")]
|
||||
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
||||
[ProducesResponseType(typeof(Exception), StatusCodes.Status500InternalServerError)]
|
||||
public async ValueTask<IResult> BoundaryScan(string address, int port, int portNum)
|
||||
public async ValueTask<IResult> BoundaryScan(string address, int port)
|
||||
{
|
||||
var jtagCtrl = new JtagClient.Jtag(address, port);
|
||||
var ret = await jtagCtrl.BoundaryScan(portNum);
|
||||
var ret = await jtagCtrl.BoundaryScan();
|
||||
if (!ret.IsSuccessful)
|
||||
{
|
||||
if (ret.Error is ArgumentException)
|
||||
|
@ -364,7 +363,7 @@ public class JtagController : ControllerBase
|
|||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class RemoteUpdater : ControllerBase
|
||||
public class RemoteUpdateController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
|
@ -745,18 +744,108 @@ public class DDSController : ControllerBase
|
|||
logger.Error(ret.Error);
|
||||
return TypedResults.InternalServerError(ret.Error);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// /// <summary>
|
||||
// /// [TODO:description]
|
||||
// /// </summary>
|
||||
// [ApiController]
|
||||
// [Route("api/[controller]")]
|
||||
// public class BsdlParserController : ControllerBase
|
||||
// {
|
||||
// private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
//
|
||||
// const string BSDL_PATH = "bsdl";
|
||||
//
|
||||
// /// <summary>
|
||||
// /// [TODO:description]
|
||||
// /// </summary>
|
||||
// /// <param name="address">[TODO:parameter]</param>
|
||||
// /// <param name="file">[TODO:parameter]</param>
|
||||
// /// <returns>[TODO:return]</returns>
|
||||
// [EnableCors("Users")]
|
||||
// [HttpPost("UploadBsdlModelFile")]
|
||||
// public async ValueTask<IResult> UploadBsdlModelFile(string address, IFormFile file)
|
||||
// {
|
||||
// if (file == null || file.Length == 0)
|
||||
// return TypedResults.BadRequest("未选择文件");
|
||||
//
|
||||
// // 生成安全的文件名(避免路径遍历攻击)
|
||||
// var fileName = Path.GetRandomFileName();
|
||||
// var uploadsFolder = Path.Combine(Environment.CurrentDirectory, $"{BSDL_PATH}/{address}");
|
||||
//
|
||||
// // 如果存在文件,则删除原文件再上传
|
||||
// if (Directory.Exists(uploadsFolder))
|
||||
// {
|
||||
// Directory.Delete(uploadsFolder, true);
|
||||
// }
|
||||
// Directory.CreateDirectory(uploadsFolder);
|
||||
//
|
||||
// var filePath = Path.Combine(uploadsFolder, fileName);
|
||||
//
|
||||
// using (var stream = new FileStream(filePath, FileMode.Create))
|
||||
// {
|
||||
// await file.CopyToAsync(stream);
|
||||
// }
|
||||
//
|
||||
// logger.Info($"Device {address} Upload Bitstream Successfully");
|
||||
// return TypedResults.Ok(true);
|
||||
// }
|
||||
//
|
||||
// /// <summary>
|
||||
// /// [TODO:description]
|
||||
// /// </summary>
|
||||
// /// <param name="address">[TODO:parameter]</param>
|
||||
// /// <returns>[TODO:return]</returns>
|
||||
// [EnableCors("Users")]
|
||||
// [HttpGet("GetLogicalPortsDescription")]
|
||||
// public IResult GetLogicalPortsDescription(string address)
|
||||
// {
|
||||
// // 检查文件
|
||||
// var fileDir = Path.Combine(Environment.CurrentDirectory, $"{BSDL_PATH}/{address}");
|
||||
// if (!Directory.Exists(fileDir))
|
||||
// return TypedResults.BadRequest("Empty bitstream, Please upload it first");
|
||||
//
|
||||
// try
|
||||
// {
|
||||
// // 读取文件
|
||||
// var filePath = Directory.GetFiles(fileDir)[0];
|
||||
//
|
||||
// // 下载比特流
|
||||
// var parser = new BsdlParser.Parser(filePath);
|
||||
// var ret = parser.GetLogicalPorts();
|
||||
//
|
||||
// if (ret.IsSuccessful)
|
||||
// {
|
||||
// logger.Info($"Device {address} Update bitstream successfully");
|
||||
// return TypedResults.Ok(ret.Value);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// logger.Error(ret.Error);
|
||||
// return TypedResults.InternalServerError(ret.Error);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// catch (Exception error)
|
||||
// {
|
||||
// return TypedResults.InternalServerError(error);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 数据控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class Data : ControllerBase
|
||||
public class DataController : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
|
@ -815,19 +904,3 @@ public class Data : ControllerBase
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 日志控制器
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class Log : ControllerBase
|
||||
{
|
||||
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
/// <summary>
|
||||
/// 日志文件路径
|
||||
/// </summary>
|
||||
private readonly string _logFilePath = Directory.GetFiles(Directory.GetCurrentDirectory())[0];
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -773,12 +773,12 @@ public class Jtag
|
|||
/// <summary>
|
||||
/// [TODO:description]
|
||||
/// </summary>
|
||||
/// <param name="portNum">[TODO:parameter]</param>
|
||||
/// <returns>[TODO:return]</returns>
|
||||
public async ValueTask<Result<BitArray>> BoundaryScan(int portNum)
|
||||
public async ValueTask<Result<BitArray>> BoundaryScan()
|
||||
{
|
||||
if (portNum <= 0)
|
||||
return new(new ArgumentException("The number of port couldn't be negative", nameof(portNum)));
|
||||
var paser = new BsdlParser.Parser();
|
||||
var portNum = paser.GetBoundaryRegsNum().Value;
|
||||
logger.Debug($"Get boundar scan registers number: {portNum}");
|
||||
|
||||
// Clear Data
|
||||
await MsgBus.UDPServer.ClearUDPData(this.address);
|
||||
|
@ -810,6 +810,16 @@ public class Jtag
|
|||
|
||||
var byteArray = Common.Number.UInt32ArrayToBytes(retData.Value);
|
||||
if (!byteArray.IsSuccessful) return new(byteArray.Error);
|
||||
return new BitArray(byteArray.Value);
|
||||
|
||||
var bitArray = new BitArray(byteArray.Value);
|
||||
if (bitArray is null)
|
||||
return new(new Exception($"Convert to BitArray failed"));
|
||||
bitArray.Length = portNum;
|
||||
return bitArray;
|
||||
}
|
||||
|
||||
// public async ValueTask<Result<BitArray>> BoundaryScanLogicalPorts()
|
||||
// {
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue