Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle communication for object behind another object #185

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Client extends EventEmitter {
const apduBuffer = Buffer.concat(this._segmentStore);
this._segmentStore = [];
type &= ~baEnum.PduConReqBits.SEGMENTED_MESSAGE;
this._handlePdu(adr, type, apduBuffer, 0, apduBuffer.length);
this._handlePdu(adr, type, apduBuffer, 0, apduBuffer.length, null);
}
}

Expand Down Expand Up @@ -263,7 +263,7 @@ class Client extends EventEmitter {
}
}

_processUnconfirmedServiceRequest(address, type, service, buffer, offset, length) {
_processUnconfirmedServiceRequest(address, type, service, buffer, offset, length, npdu) {
let result;
debug('Handle this._processUnconfirmedServiceRequest');
if (service === baEnum.UnconfirmedServiceChoice.I_AM) {
Expand All @@ -287,7 +287,13 @@ class Client extends EventEmitter {
* console.log('address: ', device.address, ' - deviceId: ', device.deviceId, ' - maxApdu: ', device.maxApdu, ' - segmentation: ', device.segmentation, ' - vendorId: ', device.vendorId);
* });
*/
this.emit('iAm', {address: address, deviceId: result.deviceId, maxApdu: result.maxApdu, segmentation: result.segmentation, vendorId: result.vendorId});
let net = 0;
let adr = [];
if (npdu != undefined && npdu.source != undefined) {
net = npdu.source.net;
adr = npdu.source.adr;
}
this.emit('iAm', {address: address, deviceId: result.deviceId, maxApdu: result.maxApdu, segmentation: result.segmentation, vendorId: result.vendorId, net: net, adr: adr});
} else if (service === baEnum.UnconfirmedServiceChoice.WHO_IS) {
result = baServices.whoIs.decode(buffer, offset, length);
if (!result) return debug('Received invalid WhoIs message');
Expand Down Expand Up @@ -371,13 +377,13 @@ class Client extends EventEmitter {
}
}

_handlePdu(address, type, buffer, offset, length) {
_handlePdu(address, type, buffer, offset, length, npdu) {
let result;
// Handle different PDU types
switch (type & baEnum.PDU_TYPE_MASK) {
case baEnum.PduTypes.UNCONFIRMED_REQUEST:
result = baApdu.decodeUnconfirmedServiceRequest(buffer, offset);
this._processUnconfirmedServiceRequest(address, result.type, result.service, buffer, offset + result.len, length - result.len);
this._processUnconfirmedServiceRequest(address, result.type, result.service, buffer, offset + result.len, length - result.len, npdu);
break;
case baEnum.PduTypes.SIMPLE_ACK:
result = baApdu.decodeSimpleAck(buffer, offset);
Expand Down Expand Up @@ -434,7 +440,7 @@ class Client extends EventEmitter {
msgLength -= result.len;
if (msgLength <= 0) return debug('No APDU data -> Drop package');
const apduType = baApdu.getDecodedType(buffer, offset);
this._handlePdu(remoteAddress, apduType, buffer, offset, msgLength);
this._handlePdu(remoteAddress, apduType, buffer, offset, msgLength, result);
}

_receiveData(buffer, remoteAddress) {
Expand Down
6 changes: 5 additions & 1 deletion lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class Transport extends EventEmitter {
}

send(buffer, offset, receiver) {
this._server.send(buffer, 0, offset, this._settings.port, receiver);
if (receiver && receiver.net > 0) {
this._server.send(buffer, 0, offset, this._settings.port, receiver.address);
} else {
this._server.send(buffer, 0, offset, this._settings.port, receiver);
}
}

open() {
Expand Down