var to let or const, better lints, code style and more
This commit is contained in:
+267
-316
@@ -1,49 +1,46 @@
|
||||
'use strict';
|
||||
|
||||
var log = require('winston'),
|
||||
defs = require('./defs'),
|
||||
bundleMsgId = 0;
|
||||
const topLogPrefix = 'larvitsmpp: lib/utils.js: ',
|
||||
defs = require(__dirname + '/defs.js'),
|
||||
log = require('winston');
|
||||
|
||||
let bundleMsgId = 0;
|
||||
|
||||
/**
|
||||
* Calculate cmdLength from object
|
||||
*
|
||||
* @param {object} obj
|
||||
* @param {function} callback(err, cmdLength)
|
||||
* @param {function} cb - cb(err, cmdLength)
|
||||
*/
|
||||
function calcCmdLength(obj, callback) {
|
||||
var cmdLength = 16, // All commands are at least 16 octets long
|
||||
err,
|
||||
param,
|
||||
paramType,
|
||||
tlvValue,
|
||||
tlvName,
|
||||
tlvDef;
|
||||
function calcCmdLength(obj, cb) {
|
||||
const logPrefix = topLogPrefix + 'calcCmdLength() - ';
|
||||
|
||||
let cmdLength = 16; // All commands are at least 16 octets long
|
||||
|
||||
// Handle params - All command params should always exists, even if they do not contain data.
|
||||
for (param in defs.cmds[obj.cmdName].params) {
|
||||
|
||||
for (const param in defs.cmds[obj.cmdName].params) {
|
||||
// Get the parameter type, int, string, cstring etc.
|
||||
// This is needed so we can calculate length etc
|
||||
paramType = defs.cmds[obj.cmdName].params[param].type;
|
||||
const paramType = defs.cmds[obj.cmdName].params[param].type;
|
||||
|
||||
if (obj.params[param] === undefined) {
|
||||
obj.params[param] = paramType.default;
|
||||
obj.params[param] = paramType.default;
|
||||
}
|
||||
|
||||
if (isNaN(paramType.size(obj.params[param]))) {
|
||||
err = new Error('Invalid param value "' + obj.params[param] + '" for param "' + param + '" and command "' + obj.cmdName + '". Is it of the right type?');
|
||||
log.error('larvitsmpp: lib/utils.js: calcCmdLength() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
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);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
cmdLength += paramType.size(obj.params[param]);
|
||||
}
|
||||
|
||||
// TLV params - optional parameters
|
||||
for (tlvName in obj.tlvs) {
|
||||
tlvValue = obj.tlvs[tlvName].tagValue;
|
||||
tlvDef = defs.tlvsById[obj.tlvs[tlvName].tagId];
|
||||
for (const tlvName in obj.tlvs) {
|
||||
const tlvValue = obj.tlvs[tlvName].tagValue;
|
||||
|
||||
let tlvDef = defs.tlvsById[obj.tlvs[tlvName].tagId];
|
||||
|
||||
if (tlvDef === undefined) {
|
||||
tlvDef = defs.tlvs.default;
|
||||
@@ -51,15 +48,14 @@ function calcCmdLength(obj, callback) {
|
||||
|
||||
try {
|
||||
cmdLength += tlvDef.type.size(tlvValue) + 4;
|
||||
} catch(e) {
|
||||
err = new Error('Could not get size of TLV parameter "' + tlvName + '" with value "' + tlvValue + '"');
|
||||
log.error('larvitsmpp: lib/utils.js: calcCmdLength() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
} 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);
|
||||
return cb(manErr);
|
||||
}
|
||||
}
|
||||
|
||||
callback(null, cmdLength);
|
||||
cb(null, cmdLength);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,63 +63,53 @@ function calcCmdLength(obj, callback) {
|
||||
*
|
||||
* @param {object} obj - the PDU object to be written to buffer
|
||||
* @param {number} cmdLength - The length of the pdu buffer
|
||||
* @param {function} callback(err, buff)
|
||||
* @param {function} cb - cb(err, buff)
|
||||
*/
|
||||
function writeBuffer(obj, cmdLength, callback) {
|
||||
var offset = 16, // Start the offset on the body
|
||||
buff,
|
||||
param,
|
||||
paramType,
|
||||
paramSize,
|
||||
tlvId,
|
||||
tlvName,
|
||||
tlvValue,
|
||||
tlvDef,
|
||||
tlvSize,
|
||||
err;
|
||||
function writeBuffer(obj, cmdLength, cb) {
|
||||
const logPrefix = topLogPrefix + 'writeBuffer() - ';
|
||||
|
||||
if (isNaN(cmdLength) || cmdLength < 16) {
|
||||
if (isNaN(cmdLength)) {
|
||||
err = new Error('cmdLength is NaN');
|
||||
}
|
||||
let offset = 16, // Start the offset on the body
|
||||
buff;
|
||||
|
||||
if (cmdLength < 16) {
|
||||
err = new Error('cmdLength is less than 16 (' + cmdLength + ')');
|
||||
}
|
||||
if (isNaN(cmdLength)) {
|
||||
const err = new Error('cmdLength is NaN');
|
||||
log.error(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
log.error('larvitsmpp: lib/utils.js: objToPdu() - writeBuffer() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
if (cmdLength < 16) {
|
||||
const err = new Error('cmdLength is less than 16 (' + cmdLength + ')');
|
||||
log.error(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
buff = new Buffer(cmdLength);
|
||||
|
||||
// Write PDU header
|
||||
try {
|
||||
buff.writeUInt32BE(cmdLength, 0); // Command length for the first 4 octets
|
||||
buff.writeUInt32BE(defs.cmds[obj.cmdName].id, 4); // Command id for the second 4 octets
|
||||
buff.writeUInt32BE(defs.errors[obj.cmdStatus], 8); // Command status for the third 4 octets
|
||||
buff.writeUInt32BE(obj.seqNr, 12); // Sequence number as the fourth 4 octets
|
||||
} catch (e) {
|
||||
err = new Error('Could not write PDU header, catched err: ' + e.message, obj);
|
||||
log.error('larvitsmpp: lib/utils.js: writeBuffer() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
buff.writeUInt32BE(cmdLength, 0); // Command length for the first 4 octets
|
||||
buff.writeUInt32BE(defs.cmds[obj.cmdName].id, 4); // Command id for the second 4 octets
|
||||
buff.writeUInt32BE(defs.errors[obj.cmdStatus], 8); // Command status for the third 4 octets
|
||||
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);
|
||||
return cb(manErr);
|
||||
}
|
||||
|
||||
// Cycle through the defs list to make sure the params are in the right order
|
||||
for (param in defs.cmds[obj.cmdName].params) {
|
||||
paramType = defs.cmds[obj.cmdName].params[param].type;
|
||||
paramSize = paramType.size(obj.params[param]);
|
||||
for (const param in defs.cmds[obj.cmdName].params) {
|
||||
const paramType = defs.cmds[obj.cmdName].params[param].type,
|
||||
paramSize = paramType.size(obj.params[param]);
|
||||
|
||||
if (Buffer.isBuffer(obj.params[param])) {
|
||||
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing param "' + param + '" with content "' + obj.params[param].toString('hex') + '" and size "' + paramSize + '"');
|
||||
log.silly(logPrefix + 'Writing param "' + param + '" with content "' + obj.params[param].toString('hex') + '" and size "' + paramSize + '"');
|
||||
} else {
|
||||
if (param === 'sm_length') {
|
||||
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - sm_length is calculated by short_message: "' + obj.params.short_message.toString('hex') + '"');
|
||||
log.silly(logPrefix + 'sm_length is calculated by short_message: "' + obj.params.short_message.toString('hex') + '"');
|
||||
}
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing param "' + param + '" with content "' + obj.params[param] + '"');
|
||||
log.silly(logPrefix + 'Writing param "' + param + '" with content "' + obj.params[param] + '"');
|
||||
}
|
||||
|
||||
// Write parameter value to buffer using the types method write()
|
||||
@@ -134,18 +120,20 @@ function writeBuffer(obj, cmdLength, callback) {
|
||||
}
|
||||
|
||||
// Cycle through the tlvs
|
||||
for (tlvName in obj.tlvs) {
|
||||
tlvId = obj.tlvs[tlvName].tagId;
|
||||
tlvValue = obj.tlvs[tlvName].tagValue;
|
||||
tlvDef = defs.tlvsById[tlvId];
|
||||
for (const tlvName in obj.tlvs) {
|
||||
const tlvValue = obj.tlvs[tlvName].tagValue,
|
||||
tlvId = obj.tlvs[tlvName].tagId;
|
||||
|
||||
let tlvDef = defs.tlvsById[tlvId],
|
||||
tlvSize;
|
||||
|
||||
if (tlvDef === undefined) {
|
||||
tlvDef = defs.tlvs.default;
|
||||
}
|
||||
|
||||
tlvSize = tlvDef.type.size(tlvValue);
|
||||
tlvSize = tlvDef.type.size(tlvValue);
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing TLV "' + tlvName + '" offset: ' + offset + ' value: "' + tlvValue + '"');
|
||||
log.silly(logPrefix + 'Writing TLV "' + tlvName + '" offset: ' + offset + ' value: "' + tlvValue + '"');
|
||||
|
||||
buff.writeUInt16BE(tlvId, offset);
|
||||
buff.writeUInt16BE(tlvSize, offset + 2);
|
||||
@@ -154,9 +142,9 @@ function writeBuffer(obj, cmdLength, callback) {
|
||||
offset += tlvDef.type.size(tlvValue) + 4;
|
||||
}
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Complete PDU: "' + buff.toString('hex') + '"');
|
||||
log.silly(logPrefix + 'Complete PDU: "' + buff.toString('hex') + '"');
|
||||
|
||||
callback(null, buff);
|
||||
cb(null, buff);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,30 +156,30 @@ function writeBuffer(obj, cmdLength, callback) {
|
||||
* @return {string} in utf8 format
|
||||
*/
|
||||
function decodeMsg(buffer, encoding, offset) {
|
||||
var checkEnc;
|
||||
const logPrefix = topLogPrefix + 'decodeMsg() - ';
|
||||
|
||||
if (offset === undefined) {
|
||||
offset = 0;
|
||||
}
|
||||
|
||||
for (checkEnc in defs.consts.ENCODING) {
|
||||
for (const checkEnc in defs.consts.ENCODING) {
|
||||
if (parseInt(encoding) === defs.consts.ENCODING[checkEnc] || encoding === checkEnc) {
|
||||
encoding = checkEnc;
|
||||
}
|
||||
}
|
||||
|
||||
if (defs.encodings[encoding] === undefined) {
|
||||
log.info('larvitsmpp: lib/utils.js: decodeMsg() - Invalid encoding "' + encoding + '" given. Falling back to ASCII (0x01).');
|
||||
log.info(logPrefix + 'Invalid encoding "' + encoding + '" given. Falling back to ASCII (0x01).');
|
||||
encoding = 'ASCII';
|
||||
}
|
||||
|
||||
log.debug('larvitsmpp: lib/utils.js: decodeMsg() - Decoding msg. Encoding: "' + encoding + '" offset: "' + offset + '" buffer: "' + buffer.toString('hex') + '"');
|
||||
log.debug(logPrefix + 'Decoding msg. Encoding: "' + encoding + '" offset: "' + offset + '" buffer: "' + buffer.toString('hex') + '"');
|
||||
|
||||
return defs.encodings[encoding].decode(buffer.slice(offset));
|
||||
}
|
||||
|
||||
function encodeMsg(str) {
|
||||
var encoding = defs.encodings.detect(str);
|
||||
const encoding = defs.encodings.detect(str);
|
||||
|
||||
return defs.encodings[encoding].encode(str);
|
||||
}
|
||||
@@ -201,107 +189,104 @@ function encodeMsg(str) {
|
||||
*
|
||||
* @param {buffer} pdu
|
||||
* @param {boolean} stupidNullByte - Define if the short_message should be followed by a stupid NULL byte - will be auto resolved if left undefined
|
||||
* @param {function} callback(err, obj)
|
||||
* @param {function} cb - cb(err, obj)
|
||||
*/
|
||||
function pduToObj(pdu, stupidNullByte, callback) {
|
||||
var retObj = {'params': {}, 'tlvs': {}},
|
||||
err = null,
|
||||
offset,
|
||||
command,
|
||||
param,
|
||||
tlvCmdId,
|
||||
tlvLength,
|
||||
tlvValue,
|
||||
paramSize;
|
||||
function pduToObj(pdu, stupidNullByte, cb) {
|
||||
const logPrefix = topLogPrefix + 'pduToObj() - ',
|
||||
retObj = {'params': {}, 'tlvs': {}};
|
||||
|
||||
let offset = 16, // 0-15 is the header, so the body starts at 16
|
||||
command;
|
||||
|
||||
if (typeof stupidNullByte === 'function') {
|
||||
callback = stupidNullByte;
|
||||
stupidNullByte = undefined;
|
||||
cb = stupidNullByte;
|
||||
stupidNullByte = undefined;
|
||||
}
|
||||
|
||||
// Returns true if this PDU is a response to another PDU
|
||||
retObj.isResp = function() {
|
||||
retObj.isResp = function () {
|
||||
return ! ! (this.cmdId & 0x80000000);
|
||||
};
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: pduToObj() - Decoding PDU to Obj. PDU buff in hex: ' + pdu.toString('hex'));
|
||||
log.silly(logPrefix + 'Decoding PDU to Obj. PDU buff in hex: ' + pdu.toString('hex'));
|
||||
|
||||
if (pdu.length < 16) {
|
||||
err = new Error('PDU is to small, minimum size is 16, given size is ' + pdu.length);
|
||||
log.warn('larvitsmpp: lib/utils.js: pduToObj() - ' + err.message);
|
||||
|
||||
callback(err);
|
||||
return;
|
||||
const err = new Error('PDU is to small, minimum size is 16, given size is ' + pdu.length);
|
||||
log.warn(logPrefix + '' + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// Read the PDU Header
|
||||
retObj.cmdLength = parseInt(pdu.readUInt32BE(0));
|
||||
retObj.cmdId = parseInt(pdu.readUInt32BE(4));
|
||||
retObj.cmdStatus = defs.errorsById[parseInt(pdu.readUInt32BE(8))];
|
||||
retObj.seqNr = parseInt(pdu.readUInt32BE(12));
|
||||
retObj.cmdLength = parseInt(pdu.readUInt32BE(0));
|
||||
retObj.cmdId = parseInt(pdu.readUInt32BE(4));
|
||||
retObj.cmdStatus = defs.errorsById[parseInt(pdu.readUInt32BE(8))];
|
||||
retObj.seqNr = parseInt(pdu.readUInt32BE(12));
|
||||
|
||||
// Lookup the command id in the definitions
|
||||
if (defs.cmdsById[retObj.cmdId] === undefined) {
|
||||
err = new Error('Unknown PDU command id: ' + retObj.cmdId + ' PDU buff in hex: ' + pdu.toString('hex'));
|
||||
const err = new Error('Unknown PDU command id: ' + retObj.cmdId + ' PDU buff in hex: ' + pdu.toString('hex'));
|
||||
log.warn(logPrefix + '' + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (isNaN(retObj.seqNr)) {
|
||||
err = new Error('Invalid seqNr, is not an interger: "' + retObj.seqNr + '"');
|
||||
} else if (retObj.seqNr > 2147483646) {
|
||||
err = new Error('Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
|
||||
const err = new Error('Invalid seqNr, is not an interger: "' + retObj.seqNr + '"');
|
||||
log.warn(logPrefix + '' + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// If error is found, do not proceed with execution
|
||||
if (err !== null) {
|
||||
log.warn('larvitsmpp: lib/utils.js: pduToObj() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
if (retObj.seqNr > 2147483646) {
|
||||
const err = new Error('Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
|
||||
log.warn(logPrefix + '' + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
command = defs.cmdsById[retObj.cmdId];
|
||||
retObj.cmdName = command.command;
|
||||
command = defs.cmdsById[retObj.cmdId];
|
||||
retObj.cmdName = command.command;
|
||||
|
||||
// Get all parameters from the body that should exists with this command
|
||||
offset = 16; // 0-15 is the header, so the body starts at 16
|
||||
for (param in command.params) {
|
||||
for (const param in command.params) {
|
||||
|
||||
// Get the parameter value by using the definition type read() function
|
||||
try {
|
||||
retObj.params[param] = command.params[param].type.read(pdu, offset, retObj.params.sm_length);
|
||||
paramSize = command.params[param].type.size(retObj.params[param]);
|
||||
let paramSize;
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: pduToObj() - Reading param "' + param + '" at offset ' + offset + ' with calculated size: ' + paramSize + ' content in hex: ' + pdu.slice(offset, offset + paramSize).toString('hex'));
|
||||
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'));
|
||||
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('larvitsmpp: lib/utils.js: pduToObj() - stupidNullByte is set, so short_message is followed by a NULL octet, increase paramSize one extra to account for that');
|
||||
log.silly(logPrefix + 'stupidNullByte is set, so short_message is followed by a NULL octet, increase paramSize one extra to account for that');
|
||||
paramSize ++;
|
||||
}
|
||||
}
|
||||
|
||||
// Increase the offset by the current params length
|
||||
offset += paramSize;
|
||||
} catch (e) {
|
||||
err = new Error('Failed to read param "' + param + '": ' + e.message);
|
||||
log.error('larvitsmpp: lib/utils.js: pduToObj() - ' + err.message);
|
||||
callback(err);
|
||||
|
||||
return;
|
||||
} catch (err) {
|
||||
const manErr = new Error('Failed to read param "' + param + '", err: ' + err.message);
|
||||
log.error(logPrefix + '' + manErr.message);
|
||||
return cb(err);
|
||||
}
|
||||
}
|
||||
|
||||
// If the length is greater than the current offset, there must be TLVs - resolve them!
|
||||
// The minimal size for a TLV is its head, 4 octets
|
||||
while ((offset + 4) < retObj.cmdLength) {
|
||||
let tlvLength,
|
||||
tlvCmdId,
|
||||
tlvValue;
|
||||
|
||||
try {
|
||||
tlvCmdId = pdu.readInt16BE(offset);
|
||||
tlvLength = pdu.readInt16BE(offset + 2);
|
||||
} catch (e) {
|
||||
err = new Error('Unable to read TLV at offset "' + offset + '", given cmdLength: "' + retObj.cmdLength + '" pdu: ' + pdu.toString('hex'));
|
||||
log.error('larvitsmpp: lib/utils.js: pduToObj() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
tlvCmdId = pdu.readInt16BE(offset);
|
||||
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);
|
||||
return cb(manErr);
|
||||
}
|
||||
|
||||
if (defs.tlvsById[tlvCmdId] === undefined) {
|
||||
@@ -313,7 +298,7 @@ function pduToObj(pdu, stupidNullByte, callback) {
|
||||
'tagValue': tlvValue
|
||||
};
|
||||
|
||||
log.verbose('larvitsmpp: lib/utils.js: pduToObj() - Unknown TLV found. Hex ID: ' + tlvCmdId.toString(16) + ' length: ' + tlvLength + ' hex value: ' + tlvValue);
|
||||
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);
|
||||
|
||||
@@ -322,116 +307,112 @@ function pduToObj(pdu, stupidNullByte, callback) {
|
||||
}
|
||||
|
||||
retObj.tlvs[defs.tlvsById[tlvCmdId].tag] = {
|
||||
'tagId': tlvCmdId,
|
||||
'tagName': defs.tlvsById[tlvCmdId].tag,
|
||||
'tagValue': tlvValue
|
||||
'tagId': tlvCmdId,
|
||||
'tagName': defs.tlvsById[tlvCmdId].tag,
|
||||
'tagValue': tlvValue
|
||||
};
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: pduToObj() - TLV found: "' + defs.tlvsById[tlvCmdId].tag + '" ID: "' + tlvCmdId + '" value: "' + tlvValue + '"');
|
||||
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('larvitsmpp: lib/utils.js: pduToObj() - Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr + ' - retry with the stupid NULL byte for short_message');
|
||||
log.verbose(logPrefix + 'Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr + ' - retry with the stupid NULL byte for short_message');
|
||||
|
||||
pduToObj(pdu, true, callback);
|
||||
return;
|
||||
return pduToObj(pdu, true, cb);
|
||||
}
|
||||
|
||||
if (offset !== retObj.cmdLength) {
|
||||
log.warn('larvitsmpp: lib/utils.js: pduToObj() - Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr);
|
||||
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
|
||||
// The esm_class 0x40 (64 int) means the short_message have a UDH
|
||||
// Thats why we return the short_message as a buffer
|
||||
if (retObj.params.short_message !== undefined && (retObj.params.esm_class & 0x40) !== 0x40) {
|
||||
retObj.params.short_message = decodeMsg(retObj.params.short_message, retObj.params.data_coding);
|
||||
retObj.params.short_message = decodeMsg(retObj.params.short_message, retObj.params.data_coding);
|
||||
}
|
||||
|
||||
log.debug('larvitsmpp: lib/utils.js: pduToObj() - Complete decoded PDU: ' + JSON.stringify(retObj));
|
||||
log.debug(logPrefix + 'Complete decoded PDU: ' + JSON.stringify(retObj));
|
||||
|
||||
callback(null, retObj);
|
||||
cb(null, retObj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an object to a PDU
|
||||
*
|
||||
* @param {object} obj - example {'cmdName': 'bind_transceiver_resp', 'cmdStatus': 'ESME_ROK', 'seqNr': 2} - to add parameters add a key 'params' as object
|
||||
* @param {function} callback(err, pdu)
|
||||
* @param {function} cb - cb(err, pdu)
|
||||
*/
|
||||
function objToPdu(obj, callback) {
|
||||
var err = null,
|
||||
shortMsg,
|
||||
seqNr;
|
||||
function objToPdu(obj, cb) {
|
||||
const logPrefix = topLogPrefix + 'objToPdu() - ',
|
||||
seqNr = parseInt(obj.seqNr);
|
||||
|
||||
// Check so the command is ok
|
||||
if (defs.cmds[obj.cmdName] === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: objToPdu() - Invalid cmdName: "' + obj.cmdName + '"');
|
||||
const err = new Error('Invalid cmdName: "' + obj.cmdName + '"');
|
||||
log.warn(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// Check so the command status is ok
|
||||
if (obj.cmdStatus === undefined) {
|
||||
// Default to OK
|
||||
obj.cmdStatus = 'ESME_ROK';
|
||||
obj.cmdStatus = 'ESME_ROK'; // Default to OK
|
||||
}
|
||||
|
||||
if (defs.errors[obj.cmdStatus] === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: objToPdu() - Invalid cmdStatus: "' + obj.cmdStatus + '"');
|
||||
const err = new Error('Invalid cmdStatus: "' + obj.cmdStatus + '"');
|
||||
log.warn(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// Check so seqNr is ok
|
||||
seqNr = parseInt(obj.seqNr);
|
||||
if (isNaN(seqNr)) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: objToPdu() - Invalid seqNr, is not an interger: "' + obj.seqNr + '"');
|
||||
} else if (seqNr > 2147483646) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: objToPdu() - Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
|
||||
const err = new Error('Invalid seqNr, is not an interger: "' + obj.seqNr + '"');
|
||||
log.warn(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// If error is found, do not proceed with execution
|
||||
if (err !== null) {
|
||||
log.warn(err.message);
|
||||
callback(err);
|
||||
return;
|
||||
if (seqNr > 2147483646) {
|
||||
const err = new Error('Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
|
||||
log.warn(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
// Params must be an object
|
||||
if (obj.params === undefined) {
|
||||
obj.params = {};
|
||||
obj.params = {};
|
||||
}
|
||||
|
||||
// If param "short_message" exists, encode it and set parameter "data_coding" accordingly
|
||||
if (obj.params.short_message !== undefined && ! Buffer.isBuffer(obj.params.short_message)) {
|
||||
let shortMsg;
|
||||
|
||||
// Detect encoding if is not set already
|
||||
if (obj.params.data_coding === undefined) {
|
||||
obj.params.data_coding = defs.encodings.detect(obj.params.short_message);
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: objToPdu() - data_coding "' + obj.params.data_coding + '" detected');
|
||||
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];
|
||||
obj.params.data_coding = defs.consts.ENCODING[obj.params.data_coding];
|
||||
}
|
||||
|
||||
// Acutally encode the string
|
||||
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('larvitsmpp: lib/utils.js: objToPdu() - encoding message "' + shortMsg + '" to "' + obj.params.short_message.toString('hex') + '"');
|
||||
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') + '"');
|
||||
}
|
||||
|
||||
log.debug('larvitsmpp: lib/utils.js: objToPdu() - Complete object to encode: ' + JSON.stringify(obj));
|
||||
log.debug(logPrefix + 'Complete object to encode: ' + JSON.stringify(obj));
|
||||
|
||||
calcCmdLength(obj, function(err, cmdLength) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
calcCmdLength(obj, function (err, cmdLength) {
|
||||
if (err) return cb(err);
|
||||
|
||||
writeBuffer(obj, cmdLength, callback);
|
||||
writeBuffer(obj, cmdLength, cb);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -442,93 +423,76 @@ function objToPdu(obj, callback) {
|
||||
* @param {string} status - see list at defs.errors - defaults to 'ESME_ROK' - no error (OPTIONAL)
|
||||
* @param {object} [params]
|
||||
* @param {object} [tlvs]
|
||||
* @param {function} [callback(err, pduBuffer)]
|
||||
* @param {function} [cb(err, pduBuffer)]
|
||||
*/
|
||||
function pduReturn(pdu, status, params, tlvs, callback) {
|
||||
var err = null,
|
||||
retPdu = {},
|
||||
param;
|
||||
function pduReturn(pdu, status, params, tlvs, cb) {
|
||||
const logPrefix = topLogPrefix + 'pduReturn() - ',
|
||||
retPdu = {};
|
||||
|
||||
let err = null;
|
||||
|
||||
if (Buffer.isBuffer(pdu)) {
|
||||
log.silly('larvitsmpp: lib/utils.js: pduReturn() - ran with pdu as buffer, run pduToObj() and retry');
|
||||
log.silly(logPrefix + 'Ran with pdu as buffer, run pduToObj() and retry');
|
||||
|
||||
pduToObj(pdu, function(err, pduObj) {
|
||||
if (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
pduToObj(pdu, function (err, pduObj) {
|
||||
if (err) return cb(err);
|
||||
|
||||
pduReturn(pduObj, status, params, tlvs, callback);
|
||||
pduReturn(pduObj, status, params, tlvs, cb);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: pduReturn() - ran');
|
||||
log.silly(logPrefix + 'ran');
|
||||
|
||||
if (typeof tlvs === 'function') {
|
||||
callback = tlvs;
|
||||
tlvs = undefined;
|
||||
cb = tlvs;
|
||||
tlvs = undefined;
|
||||
}
|
||||
|
||||
if (typeof params === 'function') {
|
||||
callback = params;
|
||||
params = {};
|
||||
tlvs = undefined;
|
||||
cb = params;
|
||||
params = {};
|
||||
tlvs = undefined;
|
||||
}
|
||||
|
||||
if (typeof status === 'function') {
|
||||
callback = status;
|
||||
status = 'ESME_ROK';
|
||||
params = {};
|
||||
tlvs = undefined;
|
||||
cb = status;
|
||||
status = 'ESME_ROK';
|
||||
params = {};
|
||||
tlvs = undefined;
|
||||
}
|
||||
|
||||
if (status === undefined) {
|
||||
status = 'ESME_ROK';
|
||||
status = 'ESME_ROK';
|
||||
}
|
||||
|
||||
if (callback === undefined) {
|
||||
callback = function() {};
|
||||
if (cb === undefined) {
|
||||
cb = function () {};
|
||||
}
|
||||
|
||||
if (params === undefined) {
|
||||
params = {};
|
||||
params = {};
|
||||
}
|
||||
|
||||
if (pdu === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: pduReturn() - PDU is undefined, cannot create response PDU');
|
||||
}
|
||||
|
||||
if (pdu.cmdName === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: pduReturn() - pdu.cmdName is undefined, cannot create response PDU');
|
||||
}
|
||||
|
||||
if (pdu.seqNr === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: pduReturn() - pdu.seqNr is undefined, cannot create response PDU');
|
||||
}
|
||||
|
||||
if (err === null && defs.errors[status] === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: pduReturn() - Invalid status: "' + status + '"');
|
||||
}
|
||||
|
||||
if (err === null && defs.cmds[pdu.cmdName + '_resp'] === undefined) {
|
||||
err = new Error('larvitsmpp: lib/utils.js: pduReturn() - This command does not have a response listed. Given command: "' + pdu.cmdName + '"');
|
||||
}
|
||||
if (pdu === undefined) err = new Error('PDU is undefined, cannot create response PDU');
|
||||
if (pdu.cmdName === undefined) err = new Error('pdu.cmdName is undefined, cannot create response PDU');
|
||||
if (pdu.seqNr === undefined) err = new Error('pdu.seqNr is undefined, cannot create response PDU');
|
||||
if (err === null && defs.errors[status] === undefined) err = new Error('Invalid status: "' + status + '"');
|
||||
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(err.message);
|
||||
callback(err);
|
||||
return;
|
||||
log.warn(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
retPdu.cmdName = pdu.cmdName + '_resp';
|
||||
retPdu.cmdStatus = status;
|
||||
retPdu.seqNr = pdu.seqNr;
|
||||
retPdu.params = params;
|
||||
retPdu.tlvs = tlvs;
|
||||
retPdu.cmdName = pdu.cmdName + '_resp';
|
||||
retPdu.cmdStatus = status;
|
||||
retPdu.seqNr = pdu.seqNr;
|
||||
retPdu.params = params;
|
||||
retPdu.tlvs = tlvs;
|
||||
|
||||
// Populate parameters that should exist in the response
|
||||
for (param in defs.cmds[pdu.cmdName + '_resp'].params) {
|
||||
for (const param in defs.cmds[pdu.cmdName + '_resp'].params) {
|
||||
|
||||
// Do not override the manually supplied parameters
|
||||
if (retPdu.params[param] === undefined) {
|
||||
@@ -536,9 +500,7 @@ function pduReturn(pdu, status, params, tlvs, callback) {
|
||||
}
|
||||
}
|
||||
|
||||
objToPdu(retPdu, function(err, retPdu) {
|
||||
callback(err, retPdu);
|
||||
});
|
||||
objToPdu(retPdu, cb);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -548,7 +510,7 @@ function pduReturn(pdu, status, params, tlvs, callback) {
|
||||
* @return {string}
|
||||
*/
|
||||
function smppDate(jsDateObj) {
|
||||
var uglyStr = '';
|
||||
let uglyStr = '';
|
||||
|
||||
uglyStr += jsDateObj.getFullYear().toString().substring(2);
|
||||
|
||||
@@ -606,20 +568,20 @@ function bitCount(msg, encoding) {
|
||||
* @return array of buffers
|
||||
*/
|
||||
function splitMsg(msg, encoding) {
|
||||
var msgPart = '',
|
||||
msgs = [],
|
||||
encoding = encoding || defs.encodings.detect(msg),
|
||||
totBitCount = bitCount(msg, encoding),
|
||||
udh,
|
||||
i,
|
||||
i2,
|
||||
partCharLimit;
|
||||
const resolvedEncoding = encoding || defs.encodings.detect(msg),
|
||||
totBitCount = bitCount(msg, resolvedEncoding),
|
||||
logPrefix = topLogPrefix + 'splitMsg() - ',
|
||||
msgs = [];
|
||||
|
||||
let msgPart = '',
|
||||
partCharLimit,
|
||||
i2;
|
||||
|
||||
// A single message could contain up to 1120 bits
|
||||
// Return directly if the message fits into that
|
||||
if (totBitCount < 1121) {
|
||||
log.silly('larvitsmpp: lib/utils.js: splitMsg() - bitCount below 1121 (' + totBitCount + ') return only one part');
|
||||
return [defs.encodings[encoding].encode(msg)];
|
||||
log.silly(logPrefix + 'bitCount below 1121 (' + totBitCount + ') return only one part');
|
||||
return [defs.encodings[resolvedEncoding].encode(msg)];
|
||||
}
|
||||
|
||||
bundleMsgId ++; // This will identify this message "bundle"
|
||||
@@ -628,17 +590,16 @@ function splitMsg(msg, encoding) {
|
||||
bundleMsgId = 1;
|
||||
}
|
||||
|
||||
log.silly('larvitsmpp: lib/utils.js: splitMsg() - bundleMsgId set to ' + bundleMsgId);
|
||||
log.silly(logPrefix + 'bundleMsgId set to ' + bundleMsgId);
|
||||
|
||||
if (encoding === 'ASCII') {
|
||||
if (resolvedEncoding === 'ASCII') {
|
||||
partCharLimit = 153;
|
||||
} else {
|
||||
partCharLimit = 67;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
i2 = 0;
|
||||
while (msg[i] !== undefined) {
|
||||
i2 = 0;
|
||||
for (let i = 0; msg[i] !== undefined; i ++) {
|
||||
msgPart += msg[i];
|
||||
|
||||
i2 ++;
|
||||
@@ -650,7 +611,7 @@ function splitMsg(msg, encoding) {
|
||||
i2 = 0;
|
||||
|
||||
// Add this msgPart minus the last character to the msgs array as an encoded buffer
|
||||
msgs.push(defs.encodings[encoding].encode(msgPart.slice(0, - 1)));
|
||||
msgs.push(defs.encodings[resolvedEncoding].encode(msgPart.slice(0, - 1)));
|
||||
|
||||
// Reset msgPart
|
||||
msgPart = '';
|
||||
@@ -658,30 +619,24 @@ function splitMsg(msg, encoding) {
|
||||
// Put i back one to account for the last character we removed from the msgPart
|
||||
i --;
|
||||
}
|
||||
|
||||
i ++;
|
||||
}
|
||||
|
||||
// Add the last msgPart to the msgs array
|
||||
msgs.push(defs.encodings[encoding].encode(msgPart));
|
||||
msgs.push(defs.encodings[resolvedEncoding].encode(msgPart));
|
||||
|
||||
// Add the UDH (http://en.wikipedia.org/wiki/Concatenated_SMS)
|
||||
i = 0;
|
||||
while (msgs[i] !== undefined) {
|
||||
|
||||
for (let i = 0; msgs[i] !== undefined; i ++) {
|
||||
// Create the UDH buffer
|
||||
udh = new Buffer([
|
||||
0x05, // Length of User Data Header, in this case 05.
|
||||
0x00, // Information Element Identifier, equal to 00 (Concatenated short messages, 8-bit reference number)
|
||||
0x03, // Length of the header, excluding the first two fields; equal to 03
|
||||
bundleMsgId, // CSMS reference number, must be same for all the SMS parts in the CSMS
|
||||
msgs.length, // Total number of parts. The value shall remain constant for every short message which makes up the concatenated short message. If the value is zero then the receiving entity shall ignore the whole information element
|
||||
i + 1 // This part's number in the sequence. The value shall start at 1 and increment for every short message which makes up the concatenated short message.
|
||||
const udh = new Buffer([
|
||||
0x05, // Length of User Data Header, in this case 05.
|
||||
0x00, // Information Element Identifier, equal to 00 (Concatenated short messages, 8-bit reference number)
|
||||
0x03, // Length of the header, excluding the first two fields; equal to 03
|
||||
bundleMsgId, // CSMS reference number, must be same for all the SMS parts in the CSMS
|
||||
msgs.length, // Total number of parts. The value shall remain constant for every short message which makes up the concatenated short message. If the value is zero then the receiving entity shall ignore the whole information element
|
||||
i + 1 // This part's number in the sequence. The value shall start at 1 and increment for every short message which makes up the concatenated short message.
|
||||
]);
|
||||
|
||||
msgs[i] = Buffer.concat([udh, msgs[i]]);
|
||||
|
||||
i ++;
|
||||
msgs[i] = Buffer.concat([udh, msgs[i]]);
|
||||
}
|
||||
|
||||
return msgs;
|
||||
@@ -692,83 +647,79 @@ function splitMsg(msg, encoding) {
|
||||
* This must be called from an sms object context
|
||||
*
|
||||
* @param {string} status - see list at defs.consts.MESSAGE_STATE - defaults to 'DELIVERED'
|
||||
* @param {function} callback(err, retPdu, dlrPduObj)
|
||||
* @param {function} cb - cb(err, retPdu, dlrPduObj)
|
||||
*/
|
||||
function smsDlr(status, callback) {
|
||||
var shortMessage,
|
||||
dlrPduObj,
|
||||
err,
|
||||
sms = this;
|
||||
function smsDlr(status, cb) {
|
||||
const logPrefix = topLogPrefix + 'smsDlr() - ',
|
||||
dlrPduObj = {},
|
||||
sms = this;
|
||||
|
||||
let shortMessage = 'id:' + sms.smsId + ' sub:001 ';
|
||||
|
||||
if (typeof status === 'function') {
|
||||
callback = status;
|
||||
status = undefined;
|
||||
cb = status;
|
||||
status = undefined;
|
||||
}
|
||||
|
||||
if (status === undefined || status === true || status === 2 || status === 'true') {
|
||||
status = 2;
|
||||
status = 2;
|
||||
} else if (defs.consts.MESSAGE_STATE[status] !== undefined) {
|
||||
status = defs.consts.MESSAGE_STATE[status];
|
||||
status = defs.consts.MESSAGE_STATE[status];
|
||||
} else if (defs.constsById.MESSAGE_STATE[status]) {
|
||||
status = parseInt(status);
|
||||
status = parseInt(status);
|
||||
} else {
|
||||
status = 5; // UNDELIVERABLE
|
||||
status = 5; // UNDELIVERABLE
|
||||
}
|
||||
|
||||
if (typeof callback !== 'function') {
|
||||
callback = function() {};
|
||||
if (typeof cb !== 'function') {
|
||||
cb = function () {};
|
||||
}
|
||||
|
||||
if (sms.smsId === undefined) {
|
||||
err = new Error('Trying to send DLR with no smsId.');
|
||||
log.warn('larvitsmpp: lib/utils.js: smsDlr() - ' + err.message);
|
||||
callback(err);
|
||||
return;
|
||||
const err = new Error('Trying to send DLR with no smsId.');
|
||||
log.warn(logPrefix + err.message);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
shortMessage = 'id:' + sms.smsId + ' sub:001 ';
|
||||
|
||||
if (status === 2) {
|
||||
shortMessage += 'dlvrd:1 ';
|
||||
shortMessage += 'dlvrd:1 ';
|
||||
} else {
|
||||
shortMessage += 'dlvrd:0 ';
|
||||
shortMessage += 'dlvrd:0 ';
|
||||
}
|
||||
|
||||
shortMessage += 'submit date:' + smppDate(sms.submitTime);
|
||||
shortMessage += ' done date:' + smppDate(new Date());
|
||||
shortMessage += 'submit date:' + smppDate(sms.submitTime);
|
||||
shortMessage += ' done date:' + smppDate(new Date());
|
||||
|
||||
if (status === 2) {
|
||||
shortMessage += ' stat:DELIVRD err:0 text:xxx';
|
||||
shortMessage += ' stat:DELIVRD err:0 text:xxx';
|
||||
} else {
|
||||
shortMessage += ' stat:UNDELIVERABLE err:1 text:xxx';
|
||||
shortMessage += ' stat:UNDELIVERABLE err:1 text:xxx';
|
||||
}
|
||||
|
||||
log.verbose('larvitsmpp: lib/utils.js: smsDlr() - Sending DLR message: "' + shortMessage + '"');
|
||||
log.verbose(logPrefix + 'Sending DLR message: "' + shortMessage + '"');
|
||||
|
||||
dlrPduObj = {
|
||||
'cmdName': 'deliver_sm',
|
||||
'params': {
|
||||
'source_addr': sms.from,
|
||||
'destination_addr': sms.to,
|
||||
'esm_class': 4,
|
||||
'short_message': shortMessage
|
||||
dlrPduObj.cmdName = 'deliver_sm';
|
||||
dlrPduObj.params = {
|
||||
'source_addr': sms.from,
|
||||
'destination_addr': sms.to,
|
||||
'esm_class': 4,
|
||||
'short_message': shortMessage
|
||||
};
|
||||
dlrPduObj.tlvs = {
|
||||
'receipted_message_id': {
|
||||
'tagId': 0x001E,
|
||||
'tagName': 'receipted_message_id',
|
||||
'tagValue': sms.smsId
|
||||
},
|
||||
'tlvs': {
|
||||
'receipted_message_id': {
|
||||
'tagId': 0x001E,
|
||||
'tagName': 'receipted_message_id',
|
||||
'tagValue': sms.smsId
|
||||
},
|
||||
'message_state': {
|
||||
'tagId': 0x0427,
|
||||
'tagName': 'message_state',
|
||||
'tagValue': status
|
||||
}
|
||||
'message_state': {
|
||||
'tagId': 0x0427,
|
||||
'tagName': 'message_state',
|
||||
'tagValue': status
|
||||
}
|
||||
};
|
||||
|
||||
sms.session.send(dlrPduObj, false, function(err, retPdu) {
|
||||
callback(err, retPdu, dlrPduObj);
|
||||
sms.session.send(dlrPduObj, false, function (err, retPdu) {
|
||||
cb(err, retPdu, dlrPduObj);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user