60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const { canBindTcpPort } = require('./jlinkPortCheck.cjs');
|
|
|
|
/** Preferred ports per session (dynamic fallback if busy). */
|
|
const JLINK_PORTS_BY_SESSION = {
|
|
receiver: {
|
|
gdb: 29021,
|
|
swo: 29022,
|
|
rtt: 19021,
|
|
},
|
|
transmitter: {
|
|
gdb: 29031,
|
|
swo: 29032,
|
|
rtt: 19022,
|
|
},
|
|
};
|
|
|
|
const GDB_PORT_RANGE = { min: 29000, max: 29199 };
|
|
const SWO_PORT_RANGE = { min: 29000, max: 29199 };
|
|
const RTT_PORT_RANGE = { min: 19020, max: 19199 };
|
|
|
|
const portCandidates = (preferred, min, max) => {
|
|
const ordered = [preferred];
|
|
for (let port = min; port <= max; port++) {
|
|
if (port !== preferred) ordered.push(port);
|
|
}
|
|
return ordered;
|
|
};
|
|
|
|
const pickTcpPort = async (preferred, range, reserved) => {
|
|
for (const port of portCandidates(preferred, range.min, range.max)) {
|
|
if (reserved.has(port)) continue;
|
|
if (await canBindTcpPort('127.0.0.1', port)) {
|
|
reserved.add(port);
|
|
return port;
|
|
}
|
|
}
|
|
|
|
throw new Error(
|
|
`사용 가능한 TCP 포트가 없습니다 (선호 ${preferred}, 범위 ${range.min}-${range.max}). JLinkGDBServerCL을 모두 종료한 뒤 다시 시도하세요.`
|
|
);
|
|
};
|
|
|
|
const allocateSessionPorts = async (sessionId, reserved = new Set()) => {
|
|
const preferred = JLINK_PORTS_BY_SESSION[sessionId];
|
|
if (!preferred) {
|
|
throw new Error(`Unknown session for port allocation: ${sessionId}`);
|
|
}
|
|
|
|
return {
|
|
gdb: await pickTcpPort(preferred.gdb, GDB_PORT_RANGE, reserved),
|
|
swo: await pickTcpPort(preferred.swo, SWO_PORT_RANGE, reserved),
|
|
rtt: await pickTcpPort(preferred.rtt, RTT_PORT_RANGE, reserved),
|
|
};
|
|
};
|
|
|
|
module.exports = {
|
|
JLINK_PORTS_BY_SESSION,
|
|
allocateSessionPorts,
|
|
};
|