|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import * as manifest from './manifest'; |
| 3 | +import { SvdResolver } from './svd-resolver'; |
| 4 | +import { PeripheralNode, PeripheralOptions } from './views/nodes/peripheralnode'; |
| 5 | +import { SvdData, SVDParser } from './svd-parser'; |
| 6 | +import { parseStringPromise } from 'xml2js'; |
| 7 | +import { readFromUrl } from './utils'; |
| 8 | +import { SvdRegistry } from './svd-registry'; |
| 9 | + |
| 10 | +const pathToUri = (path: string): vscode.Uri => { |
| 11 | + try { |
| 12 | + return vscode.Uri.file(path); |
| 13 | + } catch (e) { |
| 14 | + return vscode.Uri.parse(path); |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +const getData = async <T>(definition: string, ...params: unknown[]) : Promise<T | undefined> => { |
| 19 | + if(definition.startsWith('command:')) { |
| 20 | + const command = definition.substring('command:'.length); |
| 21 | + return vscode.commands.executeCommand(command, ...params) as Promise<T | undefined>; |
| 22 | + } |
| 23 | + return definition as T; |
| 24 | +}; |
| 25 | + |
| 26 | +export class PeripheralsProvider { |
| 27 | + readonly svdResolver: SvdResolver; |
| 28 | + constructor(protected session: vscode.DebugSession, protected context: vscode.ExtensionContext) { |
| 29 | + const registry = new SvdRegistry(); |
| 30 | + this.svdResolver = new SvdResolver(registry); |
| 31 | + } |
| 32 | + |
| 33 | + public async cacheKey() : Promise<string | vscode.Uri | undefined> { |
| 34 | + const getPeripheralsCacheKeyConfig = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<string>(manifest.CONFIG_PERIPHERALS_CACHE_KEY) || manifest.DEFAULT_PERIPHERALS_CACHE_KEY; |
| 35 | + const getPeripheralsCacheKey = this.session.configuration[getPeripheralsCacheKeyConfig]; |
| 36 | + |
| 37 | + if(getPeripheralsCacheKey) { |
| 38 | + return getPeripheralsCacheKey; |
| 39 | + } |
| 40 | + |
| 41 | + const wsFolderPath = this.session.workspaceFolder ? this.session.workspaceFolder.uri : vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0].uri; |
| 42 | + const svdPath = await this.svdResolver.resolve(this.session, wsFolderPath); |
| 43 | + return svdPath; |
| 44 | + } |
| 45 | + |
| 46 | + public async getPeripherals(): Promise<PeripheralNode[] | undefined> { |
| 47 | + const getPeripheralsConfig = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<string>(manifest.CONFIG_PERIPHERALS) || manifest.DEFAULT_PERIPHERALS; |
| 48 | + const getPeripherals = this.session.configuration[getPeripheralsConfig]; |
| 49 | + |
| 50 | + let thresh = this.session.configuration[manifest.CONFIG_ADDRGAP]; |
| 51 | + if (!thresh) { |
| 52 | + thresh = vscode.workspace.getConfiguration(manifest.PACKAGE_NAME).get<number>(manifest.CONFIG_ADDRGAP) || manifest.DEFAULT_ADDRGAP; |
| 53 | + } |
| 54 | + |
| 55 | + if (((typeof thresh) === 'number') && (thresh < 0)) { |
| 56 | + thresh = -1; // Never merge register reads even if adjacent |
| 57 | + } else { |
| 58 | + // Set the threshold between 0 and 32, with a default of 16 and a mukltiple of 8 |
| 59 | + thresh = ((((typeof thresh) === 'number') ? Math.max(0, Math.min(thresh, 32)) : 16) + 7) & ~0x7; |
| 60 | + } |
| 61 | + |
| 62 | + if(getPeripherals) { |
| 63 | + return this.getPeripheralsDynamic(thresh, getPeripherals); |
| 64 | + } else { |
| 65 | + return this.getPeripheralsFromSVD(thresh); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private async getPeripheralsDynamic(thresh: number, command: string): Promise<PeripheralNode[] | undefined> { |
| 70 | + const poptions = await getData<PeripheralOptions[]>(command, this.session); |
| 71 | + if(!poptions?.length) { |
| 72 | + return undefined; |
| 73 | + } |
| 74 | + const peripherials = poptions.map((options) => new PeripheralNode(thresh, options)); |
| 75 | + const enumTypeValuesMap = {}; |
| 76 | + for (const p of peripherials) { |
| 77 | + p.resolveDeferedEnums(enumTypeValuesMap); // This can throw an exception |
| 78 | + p.collectRanges(); |
| 79 | + } |
| 80 | + return peripherials; |
| 81 | + } |
| 82 | + |
| 83 | + private async getPeripheralsFromSVD(thresh: number): Promise<PeripheralNode[] | undefined> { |
| 84 | + const wsFolderPath = this.session.workspaceFolder ? this.session.workspaceFolder.uri : vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders[0].uri; |
| 85 | + |
| 86 | + const svdPath = await this.svdResolver.resolve(this.session, wsFolderPath); |
| 87 | + |
| 88 | + if (!svdPath) { |
| 89 | + return undefined; |
| 90 | + } |
| 91 | + |
| 92 | + let svdData: SvdData | undefined; |
| 93 | + |
| 94 | + try { |
| 95 | + let contents: ArrayBuffer | undefined; |
| 96 | + |
| 97 | + if (svdPath.startsWith('http')) { |
| 98 | + contents = await readFromUrl(svdPath); |
| 99 | + } else { |
| 100 | + const uri = pathToUri(svdPath); |
| 101 | + contents = await vscode.workspace.fs.readFile(uri); |
| 102 | + } |
| 103 | + |
| 104 | + if (contents) { |
| 105 | + const decoder = new TextDecoder(); |
| 106 | + const xml = decoder.decode(contents); |
| 107 | + svdData = await parseStringPromise(xml); |
| 108 | + } |
| 109 | + } catch(e) { |
| 110 | + // eslint-disable-next-line no-console |
| 111 | + console.warn(e); |
| 112 | + } |
| 113 | + |
| 114 | + if (!svdData) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + try { |
| 119 | + const parser = new SVDParser(); |
| 120 | + return parser.parseSVD(svdData, thresh); |
| 121 | + } catch(e) { |
| 122 | + return undefined; |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments