Dependency updates and new API for logging

This commit is contained in:
2018-08-22 17:05:29 +02:00
parent d9d3babe3b
commit 81be0a9aea
9 changed files with 198 additions and 172 deletions
+44 -42
View File
@@ -1,8 +1,9 @@
'use strict';
const topLogPrefix = 'larvitsmpp: lib/utils.js: ',
defs = require(__dirname + '/defs.js'),
log = require('winston');
LUtils = require('larvitutils'),
lUtils = new LUtils(),
defs = require(__dirname + '/defs.js');
let bundleMsgId = 0;
@@ -29,7 +30,7 @@ function calcCmdLength(obj, cb) {
if (isNaN(paramType.size(obj.params[param]))) {
const err = new Error('Invalid param value "' + obj.params[param] + '" for param "' + param + '" and command "' + obj.cmdName + '". Is it of the right type?');
log.error(logPrefix + err.message);
exports.log.error(logPrefix + err.message);
return cb(err);
}
@@ -50,7 +51,7 @@ function calcCmdLength(obj, cb) {
cmdLength += tlvDef.type.size(tlvValue) + 4;
} catch (err) {
const manErr = new Error('Could not get size of TLV parameter "' + tlvName + '" with value "' + tlvValue + '", err: ' + err.message);
log.error(logPrefix + manErr.message);
exports.log.error(logPrefix + manErr.message);
return cb(manErr);
}
}
@@ -73,13 +74,13 @@ function writeBuffer(obj, cmdLength, cb) {
if (isNaN(cmdLength)) {
const err = new Error('cmdLength is NaN');
log.error(logPrefix + err.message);
exports.log.error(logPrefix + err.message);
return cb(err);
}
if (cmdLength < 16) {
const err = new Error('cmdLength is less than 16 (' + cmdLength + ')');
log.error(logPrefix + err.message);
exports.log.error(logPrefix + err.message);
return cb(err);
}
@@ -93,7 +94,7 @@ function writeBuffer(obj, cmdLength, cb) {
buff.writeUInt32BE(obj.seqNr, 12); // Sequence number as the fourth 4 octets
} catch (err) {
const manErr = new Error('Could not write PDU header, catched err: ' + err.message, obj);
log.error(logPrefix + manErr.message);
exports.log.error(logPrefix + manErr.message);
return cb(manErr);
}
@@ -103,13 +104,13 @@ function writeBuffer(obj, cmdLength, cb) {
paramSize = paramType.size(obj.params[param]);
if (Buffer.isBuffer(obj.params[param])) {
log.silly(logPrefix + 'Writing param "' + param + '" with content "' + obj.params[param].toString('hex') + '" and size "' + paramSize + '"');
exports.log.silly(logPrefix + 'Writing param "' + param + '" with content "' + obj.params[param].toString('hex') + '" and size "' + paramSize + '"');
} else {
if (param === 'sm_length') {
log.silly(logPrefix + 'sm_length is calculated by short_message: "' + obj.params.short_message.toString('hex') + '"');
exports.log.silly(logPrefix + 'sm_length is calculated by short_message: "' + obj.params.short_message.toString('hex') + '"');
}
log.silly(logPrefix + 'Writing param "' + param + '" with content "' + obj.params[param] + '"');
exports.log.silly(logPrefix + 'Writing param "' + param + '" with content "' + obj.params[param] + '"');
}
// Write parameter value to buffer using the types method write()
@@ -133,7 +134,7 @@ function writeBuffer(obj, cmdLength, cb) {
tlvSize = tlvDef.type.size(tlvValue);
log.silly(logPrefix + 'Writing TLV "' + tlvName + '" offset: ' + offset + ' value: "' + tlvValue + '"');
exports.log.silly(logPrefix + 'Writing TLV "' + tlvName + '" offset: ' + offset + ' value: "' + tlvValue + '"');
buff.writeUInt16BE(tlvId, offset);
buff.writeUInt16BE(tlvSize, offset + 2);
@@ -142,7 +143,7 @@ function writeBuffer(obj, cmdLength, cb) {
offset += tlvDef.type.size(tlvValue) + 4;
}
log.silly(logPrefix + 'Complete PDU: "' + buff.toString('hex') + '"');
exports.log.silly(logPrefix + 'Complete PDU: "' + buff.toString('hex') + '"');
cb(null, buff);
}
@@ -169,11 +170,11 @@ function decodeMsg(buffer, encoding, offset) {
}
if (defs.encodings[encoding] === undefined) {
log.info(logPrefix + 'Invalid encoding "' + encoding + '" given. Falling back to ASCII (0x01).');
exports.log.info(logPrefix + 'Invalid encoding "' + encoding + '" given. Falling back to ASCII (0x01).');
encoding = 'ASCII';
}
log.debug(logPrefix + 'Decoding msg. Encoding: "' + encoding + '" offset: "' + offset + '" buffer: "' + buffer.toString('hex') + '"');
exports.log.debug(logPrefix + 'Decoding msg. Encoding: "' + encoding + '" offset: "' + offset + '" buffer: "' + buffer.toString('hex') + '"');
return defs.encodings[encoding].decode(buffer.slice(offset));
}
@@ -208,11 +209,11 @@ function pduToObj(pdu, stupidNullByte, cb) {
return ! ! (this.cmdId & 0x80000000);
};
log.silly(logPrefix + 'Decoding PDU to Obj. PDU buff in hex: ' + pdu.toString('hex'));
exports.log.silly(logPrefix + 'Decoding PDU to Obj. PDU buff in hex: ' + pdu.toString('hex'));
if (pdu.length < 16) {
const err = new Error('PDU is to small, minimum size is 16, given size is ' + pdu.length);
log.warn(logPrefix + '' + err.message);
exports.log.warn(logPrefix + '' + err.message);
return cb(err);
}
@@ -225,19 +226,19 @@ function pduToObj(pdu, stupidNullByte, cb) {
// Lookup the command id in the definitions
if (defs.cmdsById[retObj.cmdId] === undefined) {
const err = new Error('Unknown PDU command id: ' + retObj.cmdId + ' PDU buff in hex: ' + pdu.toString('hex'));
log.warn(logPrefix + '' + err.message);
exports.log.warn(logPrefix + '' + err.message);
return cb(err);
}
if (isNaN(retObj.seqNr)) {
const err = new Error('Invalid seqNr, is not an interger: "' + retObj.seqNr + '"');
log.warn(logPrefix + '' + err.message);
exports.log.warn(logPrefix + '' + err.message);
return cb(err);
}
if (retObj.seqNr > 2147483646) {
const err = new Error('Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
log.warn(logPrefix + '' + err.message);
exports.log.warn(logPrefix + '' + err.message);
return cb(err);
}
@@ -254,12 +255,12 @@ function pduToObj(pdu, stupidNullByte, cb) {
retObj.params[param] = command.params[param].type.read(pdu, offset, retObj.params.sm_length);
paramSize = command.params[param].type.size(retObj.params[param]);
log.silly(logPrefix + 'Reading param "' + param + '" at offset ' + offset + ' with calculated size: ' + paramSize + ' content in hex: ' + pdu.slice(offset, offset + paramSize).toString('hex'));
exports.log.silly(logPrefix + 'Reading param "' + param + '" at offset ' + offset + ' with calculated size: ' + paramSize + ' content in hex: ' + pdu.slice(offset, offset + paramSize).toString('hex'));
if (param === 'short_message') {
// Check if we have a trailing NULL octet after the short_message. Some idiot thought that would be a good idea
// in some implementations, so we need to account for that.
if (stupidNullByte === true) {
log.silly(logPrefix + 'stupidNullByte is set, so short_message is followed by a NULL octet, increase paramSize one extra to account for that');
exports.log.silly(logPrefix + 'stupidNullByte is set, so short_message is followed by a NULL octet, increase paramSize one extra to account for that');
paramSize ++;
}
}
@@ -268,7 +269,7 @@ function pduToObj(pdu, stupidNullByte, cb) {
offset += paramSize;
} catch (err) {
const manErr = new Error('Failed to read param "' + param + '", err: ' + err.message);
log.error(logPrefix + '' + manErr.message);
exports.log.error(logPrefix + '' + manErr.message);
return cb(err);
}
}
@@ -285,7 +286,7 @@ function pduToObj(pdu, stupidNullByte, cb) {
tlvLength = pdu.readInt16BE(offset + 2);
} catch (err) {
const manErr = new Error('Unable to read TLV at offset "' + offset + '", given cmdLength: "' + retObj.cmdLength + '" pdu: ' + pdu.toString('hex') + ', err: ' + err.message);
log.error(logPrefix + '' + manErr.message);
exports.log.error(logPrefix + '' + manErr.message);
return cb(manErr);
}
@@ -298,7 +299,7 @@ function pduToObj(pdu, stupidNullByte, cb) {
'tagValue': tlvValue
};
log.verbose(logPrefix + 'Unknown TLV found. Hex ID: ' + tlvCmdId.toString(16) + ' length: ' + tlvLength + ' hex value: ' + tlvValue);
exports.log.verbose(logPrefix + 'Unknown TLV found. Hex ID: ' + tlvCmdId.toString(16) + ' length: ' + tlvLength + ' hex value: ' + tlvValue);
} else {
tlvValue = defs.tlvsById[tlvCmdId].type.read(pdu, offset + 4, tlvLength);
@@ -312,20 +313,20 @@ function pduToObj(pdu, stupidNullByte, cb) {
'tagValue': tlvValue
};
log.silly(logPrefix + 'TLV found: "' + defs.tlvsById[tlvCmdId].tag + '" ID: "' + tlvCmdId + '" value: "' + tlvValue + '"');
exports.log.silly(logPrefix + 'TLV found: "' + defs.tlvsById[tlvCmdId].tag + '" ID: "' + tlvCmdId + '" value: "' + tlvValue + '"');
}
offset = offset + 4 + tlvLength;
}
if (offset !== retObj.cmdLength && stupidNullByte === undefined) {
log.verbose(logPrefix + 'Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr + ' - retry with the stupid NULL byte for short_message');
exports.log.verbose(logPrefix + 'Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr + ' - retry with the stupid NULL byte for short_message');
return pduToObj(pdu, true, cb);
}
if (offset !== retObj.cmdLength) {
log.warn(logPrefix + 'Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr);
exports.log.warn(logPrefix + 'Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr);
}
// Decode the short message if it is set and esm_class is 0
@@ -335,7 +336,7 @@ function pduToObj(pdu, stupidNullByte, cb) {
retObj.params.short_message = decodeMsg(retObj.params.short_message, retObj.params.data_coding);
}
log.debug(logPrefix + 'Complete decoded PDU: ' + JSON.stringify(retObj));
exports.log.debug(logPrefix + 'Complete decoded PDU: ' + JSON.stringify(retObj));
cb(null, retObj);
}
@@ -353,7 +354,7 @@ function objToPdu(obj, cb) {
// Check so the command is ok
if (defs.cmds[obj.cmdName] === undefined) {
const err = new Error('Invalid cmdName: "' + obj.cmdName + '"');
log.warn(logPrefix + err.message);
exports.log.warn(logPrefix + err.message);
return cb(err);
}
@@ -364,20 +365,20 @@ function objToPdu(obj, cb) {
if (defs.errors[obj.cmdStatus] === undefined) {
const err = new Error('Invalid cmdStatus: "' + obj.cmdStatus + '"');
log.warn(logPrefix + err.message);
exports.log.warn(logPrefix + err.message);
return cb(err);
}
// Check so seqNr is ok
if (isNaN(seqNr)) {
const err = new Error('Invalid seqNr, is not an interger: "' + obj.seqNr + '"');
log.warn(logPrefix + err.message);
exports.log.warn(logPrefix + err.message);
return cb(err);
}
if (seqNr > 2147483646) {
const err = new Error('Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
log.warn(logPrefix + err.message);
exports.log.warn(logPrefix + err.message);
return cb(err);
}
@@ -394,7 +395,7 @@ function objToPdu(obj, cb) {
if (obj.params.data_coding === undefined) {
obj.params.data_coding = defs.encodings.detect(obj.params.short_message);
log.silly(logPrefix + 'data_coding "' + obj.params.data_coding + '" detected');
exports.log.silly(logPrefix + 'data_coding "' + obj.params.data_coding + '" detected');
// Now set the hex value
obj.params.data_coding = defs.consts.ENCODING[obj.params.data_coding];
@@ -404,10 +405,10 @@ function objToPdu(obj, cb) {
shortMsg = obj.params.short_message;
obj.params.short_message = encodeMsg(obj.params.short_message);
obj.params.sm_length = obj.params.short_message.length;
log.silly(logPrefix + 'Encoding message "' + shortMsg + '" to "' + obj.params.short_message.toString('hex') + '"');
exports.log.silly(logPrefix + 'Encoding message "' + shortMsg + '" to "' + obj.params.short_message.toString('hex') + '"');
}
log.debug(logPrefix + 'Complete object to encode: ' + JSON.stringify(obj));
exports.log.debug(logPrefix + 'Complete object to encode: ' + JSON.stringify(obj));
calcCmdLength(obj, function (err, cmdLength) {
if (err) return cb(err);
@@ -432,7 +433,7 @@ function pduReturn(pdu, status, params, tlvs, cb) {
let err = null;
if (Buffer.isBuffer(pdu)) {
log.silly(logPrefix + 'Ran with pdu as buffer, run pduToObj() and retry');
exports.log.silly(logPrefix + 'Ran with pdu as buffer, run pduToObj() and retry');
pduToObj(pdu, function (err, pduObj) {
if (err) return cb(err);
@@ -442,7 +443,7 @@ function pduReturn(pdu, status, params, tlvs, cb) {
return;
}
log.silly(logPrefix + 'ran');
exports.log.silly(logPrefix + 'ran');
if (typeof tlvs === 'function') {
cb = tlvs;
@@ -481,7 +482,7 @@ function pduReturn(pdu, status, params, tlvs, cb) {
if (err === null && defs.cmds[pdu.cmdName + '_resp'] === undefined) err = new Error('This command does not have a response listed. Given command: "' + pdu.cmdName + '"');
if (err !== null) {
log.warn(logPrefix + err.message);
exports.log.warn(logPrefix + err.message);
return cb(err);
}
@@ -580,7 +581,7 @@ function splitMsg(msg, encoding) {
// A single message could contain up to 1120 bits
// Return directly if the message fits into that
if (totBitCount < 1121) {
log.silly(logPrefix + 'bitCount below 1121 (' + totBitCount + ') return only one part');
exports.log.silly(logPrefix + 'bitCount below 1121 (' + totBitCount + ') return only one part');
return [defs.encodings[resolvedEncoding].encode(msg)];
}
@@ -590,7 +591,7 @@ function splitMsg(msg, encoding) {
bundleMsgId = 1;
}
log.silly(logPrefix + 'bundleMsgId set to ' + bundleMsgId);
exports.log.silly(logPrefix + 'bundleMsgId set to ' + bundleMsgId);
if (resolvedEncoding === 'ASCII') {
partCharLimit = 153;
@@ -677,7 +678,7 @@ function smsDlr(status, cb) {
if (sms.smsId === undefined) {
const err = new Error('Trying to send DLR with no smsId.');
log.warn(logPrefix + err.message);
exports.log.warn(logPrefix + err.message);
return cb(err);
}
@@ -696,7 +697,7 @@ function smsDlr(status, cb) {
shortMessage += ' stat:UNDELIVERABLE err:1 text:xxx';
}
log.verbose(logPrefix + 'Sending DLR message: "' + shortMessage + '"');
exports.log.verbose(logPrefix + 'Sending DLR message: "' + shortMessage + '"');
dlrPduObj.cmdName = 'deliver_sm';
dlrPduObj.params = {
@@ -733,3 +734,4 @@ exports.smppDate = smppDate;
exports.bitCount = bitCount;
exports.splitMsg = splitMsg;
exports.smsDlr = smsDlr;
exports.log = new lUtils.Log();