local M = {} --- nrfutil emite JSONL (un objeto por línea), no un array. local function extract_devices(obj) if type(obj) ~= "table" or type(obj.data) ~= "table" then return nil end if obj.data.devices then return obj.data.devices end if type(obj.data.data) == "table" and obj.data.data.devices then return obj.data.data.devices end return nil end function M.devices() local raw = vim.fn.system({ "nrfutil", "device", "list", "--json" }) if vim.v.shell_error ~= 0 then return {} end local devs = {} for _, line in ipairs(vim.split(raw, "\n", { plain = true })) do if line ~= "" then local ok, obj = pcall(vim.json.decode, line) if ok and obj then local list = extract_devices(obj) if list then for _, d in ipairs(list) do if d.serialNumber then local name = (d.devkit and d.devkit.deviceFamily) or (d.usb and d.usb.product) or "DK" table.insert(devs, { sn = d.serialNumber, name = name }) end end if #devs > 0 then break end end end end end return devs end -- Modos soportados y el flag que añaden a `west flash`. local MODES = { flash = { flag = nil, title = "hego://flash" }, erase = { flag = "--erase", title = "hego://flash-erase" }, recover = { flag = "--recover", title = "hego://flash-recover" }, } local CONFIRM = { erase = "Se borra TODA la flash: particiones, factory data y estado de comisión.\n¿Seguir?", recover = "Se borra toda la flash Y se desbloquea APPROTECT.\n" .. "Es lo que hay que usar cuando el flash normal falla con " .. "AP-Protect / readback protection.\n¿Seguir?", } local function argv(opts, sn, mode) local a = { "flash", "-d", opts.build_dir, "--dev-id", sn } if MODES[mode].flag then table.insert(a, MODES[mode].flag) end return a end --- mode: "flash" (por defecto) | "erase" | "recover" function M.run(opts, mode) mode = mode or "flash" if not MODES[mode] then vim.notify("[hego] modo de flash desconocido: " .. tostring(mode), vim.log.levels.ERROR) return end if CONFIRM[mode] then if vim.fn.confirm(CONFIRM[mode], "&Sí\n&No", 2) ~= 1 then return end end local devs = M.devices() if #devs == 0 then vim.notify("[hego] no hay ninguna placa conectada", vim.log.levels.WARN) return end local exec = require("hego.exec") local function go(sn) exec.run_async(exec.ncs(opts, argv(opts, sn, mode)), { title = MODES[mode].title, cwd = vim.fn.getcwd(), env = exec.zephyr_env(opts), }) end if #devs == 1 then return go(devs[1].sn) end vim.ui.select(devs, { prompt = "¿A qué placa?", format_item = function(d) return d.name .. " (" .. d.sn .. ")" end, }, function(c) if c then go(c.sn) end end) end --- Plan B: recuperar la placa sin pasar por west ni por sysbuild. --- Útil si `west flash --recover` se atraganta con el multi-dominio. function M.device_recover(opts) local devs = M.devices() if #devs == 0 then vim.notify("[hego] no hay ninguna placa conectada", vim.log.levels.WARN) return end if vim.fn.confirm("Se borra toda la flash de la placa y se desbloquea APPROTECT.\n¿Seguir?", "&Sí\n&No", 2) ~= 1 then return end local exec = require("hego.exec") local function go(sn) -- Ojo: esto NO va envuelto en el toolchain, es nrfutil a pelo. exec.run_async({ "nrfutil", "device", "recover", "--serial-number", sn }, { title = "hego://device-recover", cwd = vim.fn.getcwd(), }) end if #devs == 1 then return go(devs[1].sn) end vim.ui.select(devs, { prompt = "¿Qué placa recupero?", format_item = function(d) return d.name .. " (" .. d.sn .. ")" end, }, function(c) if c then go(c.sn) end end) end return M