Created
November 4, 2018 19:17
-
-
Save Helco/49b636fa6956d38c265e6606dfa62ee9 to your computer and use it in GitHub Desktop.
Small nodejs script to test out qemu control communication with RebbleOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const net = require("net"); | |
const repl = require("repl"); | |
const sock = net.connect(63771); | |
sock.on('close', () => process.exit(0)); | |
function flat(arr) | |
{ | |
if (!Array.isArray(arr)) | |
return [ arr ]; | |
return arr | |
.map(n => flat(n)) | |
.reduce((p, c) => { p.push.apply(p, c); return p }, []); | |
} | |
global.send = function send(data) { | |
data = flat(data); | |
const buf = Buffer.from(data); | |
console.log("Send:", buf); | |
sock.write(buf); | |
} | |
global.ui16 = function ui16(num) { | |
const buf = Buffer.alloc(2); | |
buf.writeUInt16BE(num, 0); | |
return [...buf]; | |
} | |
global.cstr = function cstr(str) { | |
return [...Buffer.from(str + "\0", "utf8")]; | |
} | |
global.packet = function packet(protocol, data) { | |
return [ | |
ui16(0xFEED), | |
ui16(protocol), | |
ui16(data.length), | |
data, | |
ui16(0xBEEF) | |
]; | |
} | |
global.sendpacket = function sendpacket(protocol, data) { | |
send(packet(protocol, data)); | |
} | |
repl.start({ | |
prompt: '> ', | |
input: process.stdin, | |
output: process.stdout, | |
useGlobal: true | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment