diff --git a/lib/client.js b/lib/client.js index 58e6607..6702403 100644 --- a/lib/client.js +++ b/lib/client.js @@ -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); } } @@ -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) { @@ -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'); @@ -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); @@ -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) { diff --git a/lib/transport.js b/lib/transport.js index 8827172..faec062 100644 --- a/lib/transport.js +++ b/lib/transport.js @@ -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() {