Some updates

This commit is contained in:
2015-05-01 19:37:08 +02:00
parent 9548b03472
commit 1fb075194e
3 changed files with 168 additions and 39 deletions
+64 -11
View File
@@ -48,7 +48,14 @@ function calcCmdLength(obj, callback) {
tlvDef = defs.tlvs.default;
}
cmdLength += tlvDef.type.size(tlvValue) + 4;
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;
}
}
callback(null, cmdLength);
@@ -192,8 +199,8 @@ function pduToObj(pdu, callback) {
log.silly('larvitsmpp: lib/utils.js: pduToObj() - Decoding PDU to Obj. PDU buff in hex: ' + pdu.toString('hex'));
if (pdu.length < 16) {
err = new Error('larvitsmpp: lib/utils.js: pduToObj() - PDU is to small, minimum size is 16, given size is ' + pdu.length);
log.warn(err.message);
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;
@@ -207,18 +214,18 @@ function pduToObj(pdu, callback) {
// Lookup the command id in the definitions
if (defs.cmdsById[retObj.cmdId] === undefined) {
err = new Error('larvitsmpp: lib/utils.js: pduToObj() - Unknown PDU command id: ' + retObj.cmdId);
err = new Error('Unknown PDU command id: ' + retObj.cmdId);
}
if (isNaN(retObj.seqNr)) {
err = new Error('larvitsmpp: lib/utils.js: pduToObj() - Invalid seqNr, is not an interger: "' + retObj.seqNr + '"');
err = new Error('Invalid seqNr, is not an interger: "' + retObj.seqNr + '"');
} else if (retObj.seqNr > 2147483646) {
err = new Error('larvitsmpp: lib/utils.js: pduToObj() - Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
err = new Error('Invalid seqNr, maximum size of 2147483646 (0x7fffffff) exceeded.');
}
// If error is found, do not proceed with execution
if (err !== null) {
log.warn(err.message);
log.warn('larvitsmpp: lib/utils.js: pduToObj() - ' + err.message);
callback(err);
return;
}
@@ -251,7 +258,8 @@ function pduToObj(pdu, callback) {
// Increase the offset by the current params length
offset += paramSize;
} catch (e) {
err = new Error('larvitsmpp: lib/utils.js: pduToObj() - Failed to read param "' + param + '": ' + e.message);
err = new Error('Failed to read param "' + param + '": ' + e.message);
log.error('larvitsmpp: lib/utils.js: pduToObj() - ' + err.message);
callback(err);
return;
@@ -260,8 +268,14 @@ function pduToObj(pdu, callback) {
// If the length is greater than the current offset, there must be TLVs - resolve them!
while (offset < retObj.cmdLength) {
tlvCmdId = pdu.readInt16BE(offset);
tlvLength = pdu.readInt16BE(offset + 2);
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);
}
if (defs.tlvsById[tlvCmdId] === undefined) {
tlvValue = pdu.slice(offset + 4, offset + 4 + tlvLength).toString('hex');
@@ -475,9 +489,48 @@ function pduReturn(pdu, status, params, tlvs, callback) {
});
}
/**
* Format a js date object as ugly SMPP date format
*
* @param obj jsDateObj
* @return str
*/
function smppDate(jsDateObj) {
var uglyStr = '';
uglyStr += jsDateObj.getFullYear().toString().substring(2);
if (jsDateObj.getMonth() < 10) {
uglyStr += '0';
}
uglyStr += jsDateObj.getMonth();
if (jsDateObj.getDate() < 10) {
uglyStr += '0';
}
uglyStr += jsDateObj.getDate();
if (jsDateObj.getHours() < 10) {
uglyStr += '0';
}
uglyStr += jsDateObj.getHours();
if (jsDateObj.getMinutes() < 10) {
uglyStr += '0';
}
uglyStr += jsDateObj.getMinutes();
return uglyStr;
}
// Expose some functions
exports.decodeMsg = decodeMsg;
exports.encodeMsg = encodeMsg;
exports.pduToObj = pduToObj;
exports.objToPdu = objToPdu;
exports.pduReturn = pduReturn;
exports.pduReturn = pduReturn;
exports.smppDate = smppDate;