Skip to content
Snippets Groups Projects
Commit 8963eb57 authored by deuce's avatar deuce
Browse files

More cleanup stuff.

parent f54c38b7
No related branches found
No related tags found
No related merge requests found
...@@ -41,18 +41,21 @@ require('sockdefs.js', 'SOCK_STREAM'); ...@@ -41,18 +41,21 @@ require('sockdefs.js', 'SOCK_STREAM');
var AGWPE = { var AGWPE = {
TNC:function(host, port, user, pass) { TNC:function(host, port, user, pass) {
var self = this; var self = this;
var pinfo;
var port0;
var authf;
var parr;
var i;
var m;
var pn;
if (host === undefined) if (host === undefined)
host = "127.0.0.1"; host = "127.0.0.1";
if (port === undefined) if (port === undefined)
port = 8000; port = 8000;
this.host = host;
this.port = port;
this.sock = new Socket(SOCK_STREAM, "AGWPE");
if (!this.sock.connect(this.host, this.port, 10))
throw("Unable to connect to AGWPE server");
this.ports = {};
this.callbacks = { this.callbacks = {
'G':[],
'R':[ 'R':[
{ {
func: function(frame) { func: function(frame) {
...@@ -70,22 +73,26 @@ var AGWPE = { ...@@ -70,22 +73,26 @@ var AGWPE = {
throw("Invalid Version Response Data Length!"); throw("Invalid Version Response Data Length!");
} }
} }
], ]
'G':[]
}; };
this.host = host;
this.port = port;
this.ports = {};
this.sock = new Socket(SOCK_STREAM, "AGWPE");
this.port = function(port) this.tnc_port = function(port, name)
{ {
this.parent = self;
var pself = this; var pself = this;
this.__proto__ = AGWPE._portProto;
if (port === undefined) if (port === undefined)
throw("No port specified for port constructor"); throw("No port specified for port constructor");
if (name === undefined)
name = "Port "+(port+1);
this.__proto__ = AGWPE._portProto;
this.parent = self;
this.port = port; this.port = port;
this.calls = []; this.calls = [];
this.callbacks = { this.callbacks = {
'g':[], // Garbage from DireWolf 'g':[], // Garbage from DireWolf
'y':[], 'y':[],
...@@ -117,13 +124,17 @@ var AGWPE = { ...@@ -117,13 +124,17 @@ var AGWPE = {
this.rawRx = false; this.rawRx = false;
this.frames = []; this.frames = [];
/*
* This needs to be here, not in the prototype
* because it uses pself.
*/
this.frame = function(kind) this.frame = function(kind)
{ {
this.__proto__ = AGWPE._frameProto;
this.parent = pself;
if (kind === undefined) if (kind === undefined)
throw("Frame being created with no kind"); throw("Frame being created with no kind");
this.__proto__ = AGWPE._frameProto;
this.parent = pself;
this.port = pself.port; this.port = pself.port;
this.kind = kind; this.kind = kind;
this.pid = 0xf0; // I-frame this.pid = 0xf0; // I-frame
...@@ -132,14 +143,15 @@ var AGWPE = { ...@@ -132,14 +143,15 @@ var AGWPE = {
this.data = ''; this.data = '';
}; };
/*
* This needs to be here, not in the prototype
* because it uses pself.
*/
this.connection = function(from, to, via_pid) this.connection = function(from, to, via_pid)
{ {
this.__proto__ = AGWPE._connProto;
this.parent = pself;
var cself = this; var cself = this;
var via; var via = [];
var pid; var pid = 0xf0;
var r;
if (from === undefined) if (from === undefined)
throw("Connection from undefined callsign"); throw("Connection from undefined callsign");
...@@ -147,17 +159,17 @@ var AGWPE = { ...@@ -147,17 +159,17 @@ var AGWPE = {
throw("Connection from unregistered callsign"); throw("Connection from unregistered callsign");
if (to === undefined) if (to === undefined)
throw("Connection to undefined call"); throw("Connection to undefined call");
if (via_pid === undefined) { if (via_pid !== undefined) {
via = []; if (Array.isArray(via_pid))
pid = 0xf0;
}
else if (Array.isArray(via_pid)) {
via = via_pid; via = via_pid;
pid = 0xf0; else
}
else {
pid = parseInt(via_pid, 10); pid = parseInt(via_pid, 10);
} }
if (via.length > 7)
throw("Connect via path too long: "+via.length);
this.__proto__ = AGWPE._connProto;
this.parent = pself;
this.from = from; this.from = from;
this.to = to; this.to = to;
this.via = via; this.via = via;
...@@ -187,25 +199,29 @@ var AGWPE = { ...@@ -187,25 +199,29 @@ var AGWPE = {
} }
] ]
}; };
if (via.length > 7)
throw("Connect via path too long: "+via.length);
this.connected = false; this.connected = false;
this.disconnected = false;
pself.connections[from+"\x00"+to] = this; pself.connections[from+"\x00"+to] = this;
if (via.length === 0) if (via.length === 0)
pself._connect(from, to, pid); pself._connect(from, to, pid);
else else
pself._viaConnect(from, to, via); pself._viaConnect(from, to, via);
/*
* This needs to be here, not in the prototype
* because it uses cself.
*/
this.frame = function(kind) this.frame = function(kind)
{ {
this.__proto__ = AGWPE._frameProto;
this.parent = cself;
if (kind === undefined) if (kind === undefined)
throw("Frame being created with no kind"); throw("Frame being created with no kind");
this.__proto__ = AGWPE._frameProto;
this.parent = cself;
this.port = pself.port; this.port = pself.port;
this.kind = kind; this.kind = kind;
this.pid = cself.pid; // I-frame this.pid = cself.pid;
this.from = cself.from; this.from = cself.from;
this.to = cself.to; this.to = cself.to;
this.data = ''; this.data = '';
...@@ -213,9 +229,12 @@ var AGWPE = { ...@@ -213,9 +229,12 @@ var AGWPE = {
}; };
}; };
var port0 = new this.port(0); if (!this.sock.connect(this.host, this.port, 10))
throw("Unable to connect to AGWPE server");
// Do global things on port 0... this is hacky.
port0 = new this.tnc_port(0);
if (user !== undefined && pass !== undefined) { if (user !== undefined && pass !== undefined) {
var authf = new port0.frame('P'); authf = new port0.frame('P');
authf.data = user; authf.data = user;
while (authf.data.length < 255) while (authf.data.length < 255)
authf.data += '\x00'; authf.data += '\x00';
...@@ -224,40 +243,22 @@ var AGWPE = { ...@@ -224,40 +243,22 @@ var AGWPE = {
authf.data += '\x00'; authf.data += '\x00';
self.sock.send(authf.bin); self.sock.send(authf.bin);
} }
var pinfo = port0.askPorts(); pinfo = port0.askPorts();
var parr = pinfo.split(/;/); parr = pinfo.split(/;/);
var i;
var m;
var pn;
for (i=0; i<parseInt(parr[0]); i++) { for (i=0; i<parseInt(parr[0]); i++) {
m = parr[i+1].match(/^Port([0-9]+)/); m = parr[i+1].match(/^Port([0-9]+)/);
if (m !== null) { if (m !== null) {
pn = parseInt(m[1]); pn = parseInt(m[1]);
this.ports[pn-1] = new this.port(pn-1); this.ports[pn-1] = new this.tnc_port(pn-1, parr[i+1]);
} }
} }
var pver = port0.askVersion(); port0.askVersion();
this.ver = pver;
}, },
_frameProto:{}, _frameProto:{},
_connProto:{}, _connProto:{},
_portProto:{} _portProto:{}
}; };
AGWPE.TNC.prototype.frame = function(kind)
{
this.__proto__ = AGWPE._frameProto;
if (kind === undefined)
throw("Frame being created with no kind");
this.port = 0;
this.kind = kind;
this.pid = 0xf0;
this.from = '';
this.to = '';
this.data = '';
};
AGWPE.TNC.prototype.cycle = function(timeout) AGWPE.TNC.prototype.cycle = function(timeout)
{ {
var f; var f;
...@@ -265,6 +266,7 @@ AGWPE.TNC.prototype.cycle = function(timeout) ...@@ -265,6 +266,7 @@ AGWPE.TNC.prototype.cycle = function(timeout)
if (timeout === undefined) if (timeout === undefined)
timeout = 0; timeout = 0;
function handle_callbacks(ctx, frame) { function handle_callbacks(ctx, frame) {
var i; var i;
if (ctx.callbacks[frame.kind] !== undefined) { if (ctx.callbacks[frame.kind] !== undefined) {
...@@ -334,6 +336,38 @@ AGWPE.TNC.prototype.cycle = function(timeout) ...@@ -334,6 +336,38 @@ AGWPE.TNC.prototype.cycle = function(timeout)
} }
}; };
AGWPE.TNC.prototype.frame = function(kind)
{
this.__proto__ = AGWPE._frameProto;
if (kind === undefined)
throw("Frame being created with no kind");
this.port = 0;
this.kind = kind;
this.pid = 0xf0;
this.from = '';
this.to = '';
this.data = '';
};
AGWPE.TNC.prototype.getFrame = function()
{
var resp = this.sock.recv(36);
var len = ascii(resp[28]);
var ret = new this.frame('\x00');
ret.port = ascii(resp[0]);
ret.kind = resp[4];
ret.pid = ascii(resp[6]);
ret.from = resp.substr(8,10).split(/\x00/)[0];
ret.to = resp.substr(18,10).split(/\x00/)[0];
len |= ascii(resp[29] << 8);
len |= ascii(resp[30] << 16);
len |= ascii(resp[31] << 24);
ret.data = this.sock.recv(len);
return ret;
};
Object.defineProperty(AGWPE._frameProto, "bin", { Object.defineProperty(AGWPE._frameProto, "bin", {
get: function bin() { get: function bin() {
var ret = ''; var ret = '';
...@@ -374,7 +408,6 @@ Object.defineProperty(AGWPE._frameProto, "bin", { ...@@ -374,7 +408,6 @@ Object.defineProperty(AGWPE._frameProto, "bin", {
} }
}); });
AGWPE._portProto._packetCallback = { AGWPE._portProto._packetCallback = {
func:function(frame) { func:function(frame) {
var i; var i;
...@@ -398,7 +431,6 @@ AGWPE._portProto._packetCallback = { ...@@ -398,7 +431,6 @@ AGWPE._portProto._packetCallback = {
AGWPE._portProto.askVersion = function() AGWPE._portProto.askVersion = function()
{ {
var f = new this.frame('R'); var f = new this.frame('R');
var resp;
var ret = {}; var ret = {};
var done = false; var done = false;
...@@ -421,7 +453,6 @@ AGWPE._portProto.askVersion = function() ...@@ -421,7 +453,6 @@ AGWPE._portProto.askVersion = function()
AGWPE._portProto.askPorts = function() AGWPE._portProto.askPorts = function()
{ {
var f = new this.frame('G'); var f = new this.frame('G');
var resp;
var data; var data;
this.parent.callbacks.G.push({ this.parent.callbacks.G.push({
...@@ -437,18 +468,9 @@ AGWPE._portProto.askPorts = function() ...@@ -437,18 +468,9 @@ AGWPE._portProto.askPorts = function()
return data; return data;
}; };
// 'g' command (port capabilities - just garbage on direwolf)
// TODO: 'k' command (RX raw AX25 frames)
// TODO: 'm' command (RX Monitor AX25 frames)
// 'H' command (recently heard - not implemented on direwolf)
AGWPE._portProto.registerCall = function(call) AGWPE._portProto.registerCall = function(call)
{ {
var f = new this.frame('X'); var f = new this.frame('X');
var resp;
var r; var r;
if (this.calls.indexOf(call) !== -1) if (this.calls.indexOf(call) !== -1)
...@@ -473,14 +495,13 @@ AGWPE._portProto.registerCall = function(call) ...@@ -473,14 +495,13 @@ AGWPE._portProto.registerCall = function(call)
this.calls.push(call); this.calls.push(call);
return true; return true;
default: default:
throw("Unexpected registerCall status: "+ascii(resp.data[0])); throw("Unexpected registerCall status: "+r);
} }
}; };
AGWPE._portProto.unRegisterCall = function(call) AGWPE._portProto.unRegisterCall = function(call)
{ {
var f = new this.frame('x'); var f = new this.frame('x');
var resp;
if (this.calls.indexOf(call) == -1) if (this.calls.indexOf(call) == -1)
return; return;
...@@ -492,7 +513,6 @@ AGWPE._portProto.unRegisterCall = function(call) ...@@ -492,7 +513,6 @@ AGWPE._portProto.unRegisterCall = function(call)
AGWPE._portProto.askOutstanding = function() AGWPE._portProto.askOutstanding = function()
{ {
var f = new this.frame('y'); var f = new this.frame('y');
var resp;
var ret; var ret;
this.callbacks.y.push({ this.callbacks.y.push({
...@@ -507,7 +527,7 @@ AGWPE._portProto.askOutstanding = function() ...@@ -507,7 +527,7 @@ AGWPE._portProto.askOutstanding = function()
return true; return true;
} }
}); });
this.sock.send(f.bin); this.parent.sock.send(f.bin);
while (ret === undefined) while (ret === undefined)
this.parent.cycle(0.01); this.parent.cycle(0.01);
return ret; return ret;
...@@ -516,7 +536,6 @@ AGWPE._portProto.askOutstanding = function() ...@@ -516,7 +536,6 @@ AGWPE._portProto.askOutstanding = function()
AGWPE._portProto.toggleMonitor = function() AGWPE._portProto.toggleMonitor = function()
{ {
var f = new this.frame('m'); var f = new this.frame('m');
var resp;
var ret; var ret;
this.parent.sock.send(f.bin); this.parent.sock.send(f.bin);
...@@ -526,79 +545,15 @@ AGWPE._portProto.toggleMonitor = function() ...@@ -526,79 +545,15 @@ AGWPE._portProto.toggleMonitor = function()
AGWPE._portProto.toggleRaw = function() AGWPE._portProto.toggleRaw = function()
{ {
var f = new this.frame('k'); var f = new this.frame('k');
var resp;
var ret; var ret;
this.parent.sock.send(f.bin); this.parent.sock.send(f.bin);
this.rawRx = !this.rawRx; this.rawRx = !this.rawRx;
}; };
AGWPE._connProto.askOutstanding = function()
{
var f = new this.frame('Y');
var resp;
var ret;
this.callbacks.Y.push({
oneshot:true,
func:function(frame) {
if (frame.data.length !== 4)
throw("Invalid length in connection askOutstanding reply: "+frame.data.length);
ret = ascii(frame.data[0]);
ret |= ascii(frame.data[1]) << 8;
ret |= ascii(frame.data[2]) << 16;
ret |= ascii(frame.data[3]) << 24;
return true;
}
});
this.sock.send(f.bin);
while (ret === undefined)
this.parent.cycle(0.01);
return ret;
};
AGWPE._connProto.doClose = function()
{
var i;
this.connected = false;
for (i in this.parent.connections) {
if (this.parent.connections[i].connected == false)
delete this.parent.connections[i];
}
};
AGWPE._connProto.close = function()
{
var f = new this.frame('d');
var resp;
var ret;
var i;
if (!this.connected)
return;
this.parent.parent.sock.send(f.bin);
};
AGWPE._connProto.send = function(data)
{
var f = new this.frame('D');
var resp;
var ret;
if (!this.connected)
throw("send on unconnected connection");
if (data === undefined)
throw("send with undefined data");
f.data = data;
this.parent.parent.sock.send(f.bin);
};
AGWPE._portProto.sendUNPROTO = function(from, to, arg3, arg4) AGWPE._portProto.sendUNPROTO = function(from, to, arg3, arg4)
{ {
var f = new this.frame('M'); var f = new this.frame('M');
var resp;
var ret;
var via = []; var via = [];
var data = ''; var data = '';
var head = ''; var head = '';
...@@ -644,7 +599,6 @@ AGWPE._portProto.sendRaw = function(data) ...@@ -644,7 +599,6 @@ AGWPE._portProto.sendRaw = function(data)
AGWPE._portProto._connect = function(from, to, pid) AGWPE._portProto._connect = function(from, to, pid)
{ {
var f; var f;
var resp;
if (pid !== 0xf0) if (pid !== 0xf0)
f = new this.frame('C'); f = new this.frame('C');
...@@ -674,33 +628,74 @@ AGWPE._portProto._viaConnect = function(from, to, via) ...@@ -674,33 +628,74 @@ AGWPE._portProto._viaConnect = function(from, to, via)
this.parent.sock.send(f.bin); this.parent.sock.send(f.bin);
}; };
AGWPE.TNC.prototype.getFrame = function() AGWPE._connProto.askOutstanding = function()
{ {
var resp = this.sock.recv(36); var f = new this.frame('Y');
var len = ascii(resp[28]); var ret;
var ret = new this.frame('\x00');
ret.port = ascii(resp[0]); this.callbacks.Y.push({
ret.kind = resp[4]; oneshot:true,
ret.pid = ascii(resp[6]); func:function(frame) {
ret.from = resp.substr(8,10).split(/\x00/)[0]; if (frame.data.length !== 4)
ret.to = resp.substr(18,10).split(/\x00/)[0]; throw("Invalid length in connection askOutstanding reply: "+frame.data.length);
len |= ascii(resp[29] << 8); ret = ascii(frame.data[0]);
len |= ascii(resp[30] << 16); ret |= ascii(frame.data[1]) << 8;
len |= ascii(resp[31] << 24); ret |= ascii(frame.data[2]) << 16;
ret.data = this.sock.recv(len); ret |= ascii(frame.data[3]) << 24;
return true;
}
});
this.parent.parent.sock.send(f.bin);
while (ret === undefined)
this.parent.parent.cycle(0.01);
return ret; return ret;
}; };
AGWPE._connProto.doClose = function()
{
var i;
this.connected = false;
this.disconnected = true;
for (i in this.parent.connections) {
if (this.parent.connections[i].disconnected == true)
delete this.parent.connections[i];
}
};
AGWPE._connProto.close = function()
{
var f = new this.frame('d');
if (this.disconnected)
return;
this.parent.parent.sock.send(f.bin);
};
AGWPE._connProto.send = function(data)
{
var f = new this.frame('D');
if (!this.connected)
throw("send on unconnected connection");
if (data === undefined)
throw("send with undefined data");
f.data = data;
this.parent.parent.sock.send(f.bin);
};
var tnc = new AGWPE.TNC('127.0.0.1', 8000); var tnc = new AGWPE.TNC('127.0.0.1', 8000);
tnc.ports[0].toggleMonitor(); tnc.ports[0].toggleMonitor();
tnc.ports[0].callbacks.pkt.push({ tnc.ports[0].callbacks.pkt.push({
func:function(frame) { func:function(frame) {
var cleaned = frame.data.replace(/[\x00-\x1f]/g, function (match) {
return format("<0x%02x>", ascii(match));
});
print("Port "+frame.port+" Got '"+frame.kind+"' frame PID: "+frame.pid+"\nFrom: \""+frame.from+"\"\nTo: \""+frame.to+"\"\nData: \""+frame.data+"\""); print("Port "+frame.port+" Got '"+frame.kind+"' frame PID: "+frame.pid+"\nFrom: \""+frame.from+"\"\nTo: \""+frame.to+"\"\nData: \""+frame.data+"\"");
this.frames.shift(); this.frames.shift();
} }
}); });
while(1) while(1)
tnc.cycle(1); tnc.cycle(1);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment