This repository was archived by the owner on Apr 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkissTNC.js
277 lines (245 loc) · 5.25 KB
/
kissTNC.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
var util = require("util");
var events = require("events");
var SerialPort = require("serialport").SerialPort;
var ax25 = require("./index.js");
var kissTNC = function(args) {
var self = this;
events.EventEmitter.call(this);
var properties = {
'serialPort' : 0,
'baudRate' : 0,
'txDelay' : 50,
'persistence' : 63,
'slotTime' : 10,
'txTail' : 1,
'fullDuplex' : false
}
this.__defineSetter__(
"serialPort",
function(serialPort) {
if(typeof serialPort != "string")
throw "kissTNC: Invalid or no serialPort argument provided.";
properties.serialPort = serialPort;
}
);
this.__defineGetter__(
"serialPort",
function() {
return properties.serialPort;
}
);
this.__defineSetter__(
"baudRate",
function(baudRate) {
if(typeof baudRate != "number")
throw "kissTNC: Invalid or no baudRate argument provided.";
properties.baudRate = baudRate;
}
);
this.__defineGetter__(
"baudRate",
function() {
return properties.baudRate;
}
);
this.__defineSetter__(
"txDelay",
function(txDelay) {
if( typeof txDelay != "number"
||
txDelay < 0
||
txDelay > 255
) {
throw "kissTNC: Invalid txDelay";
}
properties.txDelay = txDelay / 10;
sendFrame(ax25.kissDefs.TXDELAY, [properties.txDelay]);
}
);
this.__defineGetter__(
"txDelay",
function() {
return properties.txDelay * 10;
}
);
this.__defineSetter__(
"persistence",
function(persistence) {
if( typeof persistence != "number"
||
persistence < 0
||
persistence > 1
) {
throw "kissTNC: Invalid persistence";
}
properites.persistence = (persistence * 256) - 1;
sendFrame(ax25.kissDefs.PERSISTENCE, [properties.persistence]);
}
);
this.__defineGetter__(
"persistence",
function() {
return (properties.persistence / 256) + 1;
}
);
this.__defineSetter__(
"slotTime",
function(slotTime) {
if( typeof slotTime != "number"
||
slotTime < 0
||
slotTime > 255
) {
throw "kissTNC: Invalid slotTime";
}
properties.slotTime = slotTime / 10;
sendFrame(ax25.kissDefs.SLOTTIME, [properties.slotTIme]);
}
);
this.__defineGetter__(
"slotTime",
function() {
return properties.slotTime * 10;
}
);
this.__defineSetter__(
"txTail",
function(txTail) {
if( typeof txTail != "number"
||
txTail < 0
||
txTail > 255
)
throw "kissTNC: Invalid txTail";
properties.txTail = txTail / 10;
sendFrame(ax25.kissDefs.TXTAIL, [properties.txTail]);
}
);
this.__defineGetter__(
"txTail",
function() {
return properties.txTail * 10;
}
);
this.__defineSetter__(
"fullDuplex",
function(fullDuplex) {
if(typeof fullDuplex != "boolean")
throw "kissTNC: fullDuplex must be boolean";
properties.fullDuplex = fullDuplex;
sendFrame(
ax25.kissDefs.FULLDUPLEX,
[(properties.fullDuplex) ? 1 : 0]
);
}
);
this.__defineGetter__(
"fullDuplex",
function() {
return (properties.fullDuplex == 1) ? true : false;
}
);
this.serialPort = args.serialPort;
this.baudRate = args.baudRate;
var dataBuffer = [];
var sendFrame = function(command, data) {
if(!(data instanceof Array))
throw "ax25.kissTNC: Invalid send data";
data.unshift(command);
data.unshift(ax25.kissDefs.FEND);
data.push(ax25.kissDefs.FEND);
serialHandle.write(
data,
function(err, result) {
if(typeof err != "undefined")
self.emit("error", "kissTNC: Send error: " + err);
if(typeof result != "undefined")
self.emit("sent", "kissTNC: Send result: " + result);
}
);
}
var dataHandler = function(data) {
var str = "";
var escaped = false;
for(var d = 0; d < data.length; d++) {
if(data[d] == ax25.kissDefs.FESC) {
escaped = true;
continue;
}
if(escaped && data[d] == ax25.kissDefs.TFEND)
data[d] = ax25.kissDefs.FEND;
if(escaped && data[d] == ax25.kissDefs.TFESC)
data[d] = ax25.kissDefs.FESC;
if(escaped || data[d] != ax25.kissDefs.FEND)
dataBuffer.push(data[d]);
if(!escaped && data[d] == ax25.kissDefs.FEND && dataBuffer.length > 1) {
self.emit("frame", dataBuffer.slice(1));
dataBuffer = [];
}
if(escaped)
escaped = false;
}
}
var serialHandle = new SerialPort(
properties.serialPort, {
'baudRate' : properties.baudRate
}
);
serialHandle.on(
"error",
function(err) {
self.emit("error", "kissTNC: Serial port error: " + err);
}
);
serialHandle.on(
"open",
function() {
for(var a in args) {
if( a == "serialPort"
||
a == "baudRate"
||
typeof self[a] == "undefined"
||
typeof self[a] == "function"
) {
continue;
}
self[a] = args[a];
}
self.emit("opened");
}
);
serialHandle.on(
"close",
function() {
self.emit("closed");
}
);
serialHandle.on(
"data",
function(data) {
dataHandler(data);
}
);
this.setHardware = function(value) {
sendFrame(ax25.kissDefs.SETHARDWARE, [value]);
}
this.send = function(data) {
if(!(data instanceof Array))
throw "kissTNC.send: data type mismatch.";
sendFrame(ax25.kissDefs.DATAFRAME, data);
}
this.exitKISS = function() {
sendFrame(ax25.kissDefs.RETURN, []);
}
this.close = function() {
serialHandle.close();
}
}
util.inherits(kissTNC, events.EventEmitter);
module.exports = kissTNC;