| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- const storageAccessProtocol = require('../../protocols/storage-access/index.js')
- const settingsService = require('../../store/settings-store.js')
- const transport = require('../../transport/ble-core.js')
- let syncedDeviceCaps = {
- addressWidth: 0,
- maxPacketLength: 0,
- memoryEndian: ''
- }
- function getConfiguredMaxPacketLength(value) {
- const settings = settingsService.getState()
- const numberValue = Number(value === undefined ? settings.parameterMaxPacketLength : value)
- if (Number.isFinite(numberValue) && Math.round(numberValue) === 0) return 0
- if (Number.isFinite(numberValue) && numberValue > 0) return Math.round(numberValue)
- return 64
- }
- function resolveMaxPacketLength(value) {
- const configuredMaxPacketLength = getConfiguredMaxPacketLength(value)
- const deviceMaxPacketLength = Number(syncedDeviceCaps.maxPacketLength || 0)
- if (!Number.isFinite(deviceMaxPacketLength) || deviceMaxPacketLength <= 0) return configuredMaxPacketLength
- if (configuredMaxPacketLength === 0) return Math.round(deviceMaxPacketLength)
- return Math.min(configuredMaxPacketLength, Math.round(deviceMaxPacketLength))
- }
- function normalizeProtocolIoOptions(options = {}) {
- const maxPacketLength = options.maxPacketLength === undefined ? options.maxFrameBytes : options.maxPacketLength
- const settings = settingsService.getState()
- const optionMemoryEndian = storageAccessProtocol.normalizeMemoryEndian(options.memoryEndian, '')
- const defaultMemoryEndian = storageAccessProtocol.normalizeMemoryEndian(settings.storageAccessDefaultEndian)
- return {
- expectedByteLength: options.expectedByteLength,
- maxFrameBytes: options.useDeviceCaps === false
- ? getConfiguredMaxPacketLength(maxPacketLength)
- : resolveMaxPacketLength(maxPacketLength),
- memoryEndian: optionMemoryEndian || defaultMemoryEndian,
- onChunk: options.onChunk,
- showModal: options.showModal !== false
- }
- }
- function updateSyncedDeviceCaps(caps = {}) {
- const addressWidth = Number(caps.addressWidth)
- const maxPacketLength = Number(caps.maxPacketLength)
- const memoryEndian = String(caps.memoryEndian || '').trim().toLowerCase()
- syncedDeviceCaps = {
- addressWidth: addressWidth === 16 || addressWidth === 32 ? addressWidth : 0,
- maxPacketLength: Number.isFinite(maxPacketLength) && maxPacketLength > 0
- ? Math.round(maxPacketLength)
- : 0,
- memoryEndian: memoryEndian === 'little' ? 'little' : (memoryEndian === 'big' ? 'big' : '')
- }
- }
- function getSyncedDeviceCaps() {
- return {
- ...syncedDeviceCaps
- }
- }
- function createTransportOptions(protocolIoOptions = {}) {
- return {
- maxFrameBytes: protocolIoOptions.maxFrameBytes,
- showModal: protocolIoOptions.showModal
- }
- }
- function notifyChunk(protocolIoOptions = {}, response, chunk) {
- if (typeof protocolIoOptions.onChunk === 'function') {
- protocolIoOptions.onChunk(response, chunk)
- }
- }
- function sendStorageFrame(frameBytes, label, expected, protocolIoOptions = {}) {
- return transport.sendManagedFrame(
- frameBytes,
- label,
- expected,
- createTransportOptions(protocolIoOptions)
- )
- }
- async function readMemory(area, startAddress, byteLength, label, kind, options = {}) {
- const protocolIoOptions = normalizeProtocolIoOptions(options)
- const normalizedArea = storageAccessProtocol.normalizeMemoryArea(area)
- const bytes = []
- const chunks = storageAccessProtocol.getReadChunks(startAddress, byteLength, {
- ...protocolIoOptions,
- area: normalizedArea
- })
- for (const chunk of chunks) {
- const response = await sendStorageFrame(
- storageAccessProtocol.buildReadFrame(normalizedArea, chunk.address, chunk.quantity, {
- maxFrameBytes: protocolIoOptions.maxFrameBytes,
- memoryEndian: protocolIoOptions.memoryEndian
- }),
- storageAccessProtocol.getChunkLabel(label, chunks, chunk),
- storageAccessProtocol.createExpected(normalizedArea, chunk.address, chunk.quantity, false, kind, {
- memoryEndian: protocolIoOptions.memoryEndian
- }),
- protocolIoOptions
- )
- if (!response) return null
- const dataBytes = Array.isArray(response.dataBytes) ? response.dataBytes : []
- dataBytes.forEach((byte, index) => {
- bytes[chunk.address - startAddress + index] = Number(byte) & 0xFF
- })
- notifyChunk(protocolIoOptions, response, chunk)
- }
- return bytes
- }
- async function writeMemory(area, startAddress, bytes, label, kind, options = {}) {
- const protocolIoOptions = normalizeProtocolIoOptions(options)
- const normalizedArea = storageAccessProtocol.normalizeMemoryArea(area)
- const chunks = storageAccessProtocol.getWriteChunks(startAddress, bytes, {
- ...protocolIoOptions,
- area: normalizedArea
- })
- for (const chunk of chunks) {
- const response = await sendStorageFrame(
- storageAccessProtocol.buildWriteFrame(normalizedArea, chunk.address, chunk.dataBytes, {
- maxFrameBytes: protocolIoOptions.maxFrameBytes,
- memoryEndian: protocolIoOptions.memoryEndian
- }),
- storageAccessProtocol.getChunkLabel(label, chunks, chunk),
- storageAccessProtocol.createExpected(normalizedArea, chunk.address, chunk.quantity, true, kind, {
- memoryEndian: protocolIoOptions.memoryEndian
- }),
- protocolIoOptions
- )
- if (!response) return false
- notifyChunk(protocolIoOptions, response, chunk)
- }
- return true
- }
- async function executeControl(operation, dataBytes, label, kind, options = {}) {
- const protocolIoOptions = normalizeProtocolIoOptions(options)
- const response = await sendStorageFrame(
- storageAccessProtocol.buildControlFrame(operation, dataBytes),
- label,
- storageAccessProtocol.createControlExpected(operation, kind, {
- expectedByteLength: protocolIoOptions.expectedByteLength
- }),
- protocolIoOptions
- )
- return response || null
- }
- module.exports = {
- executeControl,
- getSyncedDeviceCaps,
- normalizeProtocolIoOptions,
- readMemory,
- resolveMaxPacketLength,
- updateSyncedDeviceCaps,
- writeMemory
- }
|