protocol-implementation.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const VIEW_ID = 'storageProtocolImplementation'
  2. const TITLE = '协议实现'
  3. const SOURCES = {
  4. STORAGE_ACCESS_C: '',
  5. STORAGE_ACCESS_H: '',
  6. STRUCT_H: ''
  7. }
  8. const GUIDE = {
  9. kicker: '存储访问协议',
  10. title: '协议说明与实现规划',
  11. text: '存储访问协议普通读写使用 CMD + ADDR + LEN + DATA + CRC16-CCITT-FALSE 帧格式,不带从机地址。当前先固定上位机协议模型,从机源码参考暂不提供,后续再单独规划实现细节。',
  12. points: [
  13. { id: 'frame', text: 'CMD bit7 为故障位,仅回帧出现;bit6 为特殊指令位;普通读写中 bit3 为读写位,bit4/bit5 保留,bit0~bit2 表示区域。' },
  14. { id: 'addr', text: '普通区域 0x00 为 codeinfo 只读描述符;0x01/0x02/0x03/0x04 为 DATA、IDATA、XDATA、CODE 的 16 位地址;0x07 为 ADDR32 的 32 位地址;0x05~0x06 保留。' },
  15. { id: 'info', text: 'bit6=1 时 bit0~bit5 表示特殊指令码;当前仅定义 0x01 复位,因此复位帧 CMD=0x41。' },
  16. { id: 'control', text: 'CodeInfo 同步先发送 00+CRC 读取 area=0x00 描述符,数据区返回 TLV 起始 addr32、len16、地址位宽和最大包长。' },
  17. { id: 'struct', text: 'CodeInfo 使用 TYPE + LEN8 + VALUE 的纯 TLV 信息块;0x01~0x08 为固定内存入口,VALUE 为 addr(2/4)+len16+name[32];单独变量由 TLV 给长度,UI 或 enum 导入配置解释类型。' },
  18. { id: 'source', text: '协议实现源码已暂时移除,设置页仅保留文件占位与说明,避免给出过期从机实现。' }
  19. ]
  20. }
  21. const FILES = [
  22. {
  23. badge: '从机',
  24. details: [
  25. '源码暂未提供。',
  26. '后续需要按新 CMD 位定义重新规划从机头文件。',
  27. '当前页面仅保留文件占位。'
  28. ],
  29. location: 'User/function/storage_access.h',
  30. name: 'storage_access.h',
  31. role: '从机协议头文件',
  32. sourceKey: 'STORAGE_ACCESS_H',
  33. summary: '源码暂时移除,等待协议实现方案确认。'
  34. },
  35. {
  36. badge: '从机',
  37. details: [
  38. '源码暂未提供。',
  39. '后续需要重新设计普通读写、特殊指令、异常响应和 CodeInfo 描述符读取。',
  40. '当前页面仅保留文件占位。'
  41. ],
  42. location: 'User/function/storage_access.c',
  43. name: 'storage_access.c',
  44. role: '从机协议实现',
  45. sourceKey: 'STORAGE_ACCESS_C',
  46. summary: '源码暂时移除,等待协议实现方案确认。'
  47. },
  48. {
  49. badge: '定义',
  50. details: [
  51. '源码暂未提供。',
  52. '后续再决定是否在此提供 struct.h 示例。',
  53. '当前页面仅保留文件占位。'
  54. ],
  55. location: 'struct.h',
  56. name: 'struct.h',
  57. role: '结构体定义参考',
  58. sourceKey: 'STRUCT_H',
  59. summary: '结构体定义参考暂时移除,等待后续实现规划。'
  60. }
  61. ]
  62. function getSourceText(file) {
  63. return String(SOURCES[file && file.sourceKey] || '')
  64. }
  65. function getLineCount(text) {
  66. if (!text) return 0
  67. return text.split(/\r?\n/).length
  68. }
  69. function formatSourceMeta(text) {
  70. const chars = text.length
  71. const lines = getLineCount(text)
  72. return `${lines} 行 / ${chars} 字符`
  73. }
  74. function getSourceMetaText(text) {
  75. return text ? formatSourceMeta(text) : '暂未提供'
  76. }
  77. function normalizeIndex(index) {
  78. const numberValue = Number(index)
  79. if (!Number.isFinite(numberValue)) return 0
  80. return Math.max(0, Math.min(FILES.length - 1, Math.round(numberValue)))
  81. }
  82. function getFiles(activeIndex = 0) {
  83. const normalizedIndex = normalizeIndex(activeIndex)
  84. return FILES.map((file, index) => ({
  85. ...file,
  86. active: index === normalizedIndex,
  87. id: file.name,
  88. sourceAvailable: !!getSourceText(file),
  89. sourceMeta: getSourceMetaText(getSourceText(file))
  90. }))
  91. }
  92. function getState(activeIndex = 0) {
  93. const normalizedIndex = normalizeIndex(activeIndex)
  94. const activeFile = FILES[normalizedIndex]
  95. const sourceText = getSourceText(activeFile)
  96. return {
  97. storageProtocolImplementationActiveFile: {
  98. ...activeFile,
  99. sourceAvailable: !!sourceText,
  100. sourceMeta: getSourceMetaText(sourceText)
  101. },
  102. storageProtocolImplementationActiveIndex: normalizedIndex,
  103. storageProtocolImplementationFiles: getFiles(normalizedIndex),
  104. storageProtocolImplementationGuide: {
  105. ...GUIDE,
  106. points: GUIDE.points.map((point) => ({ ...point }))
  107. }
  108. }
  109. }
  110. module.exports = {
  111. TITLE,
  112. VIEW_ID,
  113. getSourceText(index = 0) {
  114. return getSourceText(FILES[normalizeIndex(index)])
  115. },
  116. getState
  117. }