1
0

protocol-implementation.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 为故障位,bit3 为读写位,bit4/bit5 暂时保留,bit0~bit2 表示地址模式或区域。' },
  14. { id: 'addr', text: 'bit0~bit2 为 0x7 时下发 32 位地址;0x1~0x4 对应 DATA、IDATA、XDATA、CODE 的 16 位地址;0x0、0x5、0x6 保留。' },
  15. { id: 'info', text: 'bit0、bit1、bit2、bit3、bit6 全为 1 时为特殊指令,即 CMD=0x4F;OP=0x05 返回 CodeInfo 地址、长度、CodeInfo 读取地址宽度、目标内存字节序与最大包长。' },
  16. { id: 'struct', text: 'CodeInfo 使用 TYPE + LEN8 + VALUE 的纯 TLV 信息块;0x20/0x21 表示 16 位地址入口,0x28/0x29 表示 32 位地址入口。' },
  17. { id: 'source', text: '协议实现源码已暂时移除,设置页仅保留文件占位与说明,避免给出过期从机实现。' }
  18. ]
  19. }
  20. const FILES = [
  21. {
  22. badge: '从机',
  23. details: [
  24. '源码暂未提供。',
  25. '后续需要按新 CMD 位定义重新规划从机头文件。',
  26. '当前页面仅保留文件占位。'
  27. ],
  28. location: 'User/function/storage_access.h',
  29. name: 'storage_access.h',
  30. role: '从机协议头文件',
  31. sourceKey: 'STORAGE_ACCESS_H',
  32. summary: '源码暂时移除,等待协议实现方案确认。'
  33. },
  34. {
  35. badge: '从机',
  36. details: [
  37. '源码暂未提供。',
  38. '后续需要重新设计普通读写、特殊指令、异常响应和 CodeInfo 描述符读取。',
  39. '当前页面仅保留文件占位。'
  40. ],
  41. location: 'User/function/storage_access.c',
  42. name: 'storage_access.c',
  43. role: '从机协议实现',
  44. sourceKey: 'STORAGE_ACCESS_C',
  45. summary: '源码暂时移除,等待协议实现方案确认。'
  46. },
  47. {
  48. badge: '定义',
  49. details: [
  50. '源码暂未提供。',
  51. '后续再决定是否在此提供 struct.h 示例。',
  52. '当前页面仅保留文件占位。'
  53. ],
  54. location: 'struct.h',
  55. name: 'struct.h',
  56. role: '结构体定义参考',
  57. sourceKey: 'STRUCT_H',
  58. summary: '结构体定义参考暂时移除,等待后续实现规划。'
  59. }
  60. ]
  61. function getSourceText(file) {
  62. return String(SOURCES[file && file.sourceKey] || '')
  63. }
  64. function getLineCount(text) {
  65. if (!text) return 0
  66. return text.split(/\r?\n/).length
  67. }
  68. function formatSourceMeta(text) {
  69. const chars = text.length
  70. const lines = getLineCount(text)
  71. return `${lines} 行 / ${chars} 字符`
  72. }
  73. function getSourceMetaText(text) {
  74. return text ? formatSourceMeta(text) : '暂未提供'
  75. }
  76. function normalizeIndex(index) {
  77. const numberValue = Number(index)
  78. if (!Number.isFinite(numberValue)) return 0
  79. return Math.max(0, Math.min(FILES.length - 1, Math.round(numberValue)))
  80. }
  81. function getFiles(activeIndex = 0) {
  82. const normalizedIndex = normalizeIndex(activeIndex)
  83. return FILES.map((file, index) => ({
  84. ...file,
  85. active: index === normalizedIndex,
  86. id: file.name,
  87. sourceAvailable: !!getSourceText(file),
  88. sourceMeta: getSourceMetaText(getSourceText(file))
  89. }))
  90. }
  91. function getState(activeIndex = 0) {
  92. const normalizedIndex = normalizeIndex(activeIndex)
  93. const activeFile = FILES[normalizedIndex]
  94. const sourceText = getSourceText(activeFile)
  95. return {
  96. storageProtocolImplementationActiveFile: {
  97. ...activeFile,
  98. sourceAvailable: !!sourceText,
  99. sourceMeta: getSourceMetaText(sourceText)
  100. },
  101. storageProtocolImplementationActiveIndex: normalizedIndex,
  102. storageProtocolImplementationFiles: getFiles(normalizedIndex),
  103. storageProtocolImplementationGuide: {
  104. ...GUIDE,
  105. points: GUIDE.points.map((point) => ({ ...point }))
  106. }
  107. }
  108. }
  109. module.exports = {
  110. TITLE,
  111. VIEW_ID,
  112. getSourceText(index = 0) {
  113. return getSourceText(FILES[normalizeIndex(index)])
  114. },
  115. getState
  116. }