Merge from master

This commit is contained in:
2015-05-08 21:40:45 +02:00
5 changed files with 138 additions and 24 deletions
+36 -14
View File
@@ -116,6 +116,8 @@ function writeBuffer(obj, cmdLength, callback) {
// Write parameter value to buffer using the types method write()
paramType.write(obj.params[param], buff, offset);
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing param "' + param + '" with content "' + obj.params[param] + '"');
// Increase the offset for the next param
offset += paramType.size(obj.params[param]);
}
@@ -132,7 +134,7 @@ function writeBuffer(obj, cmdLength, callback) {
tlvSize = tlvDef.type.size(tlvValue);
log.silly('larvitsmpp: lib/utils.js: objToPdu() - writeBuffer() - Writing TLV "' + tlvName + '" offset: ' + offset + ' value: "' + tlvValue + '"');
log.silly('larvitsmpp: lib/utils.js: writeBuffer() - Writing TLV "' + tlvName + '" offset: ' + offset + ' value: "' + tlvValue + '"');
buff.writeUInt16BE(tlvId, offset);
buff.writeUInt16BE(tlvSize, offset + 2);
@@ -178,9 +180,10 @@ function encodeMsg(str) {
* Transforms a PDU to an object
*
* @param buf pdu
* @param bol stupidNullByte - Define if the short_message should be followed by a stupid NULL byte - will be auto resolved if left undefined
* @param func callback(err, obj)
*/
function pduToObj(pdu, callback) {
function pduToObj(pdu, stupidNullByte, callback) {
var retObj = {'params': {}, 'tlvs': {}},
err = null,
offset,
@@ -191,6 +194,11 @@ function pduToObj(pdu, callback) {
tlvValue,
paramSize;
if (typeof stupidNullByte === 'function') {
callback = stupidNullByte;
stupidNullByte = undefined;
}
// Returns true if this PDU is a response to another PDU
retObj.isResponse = function() {
return ! ! (this.cmdId & 0x80000000);
@@ -248,9 +256,8 @@ function pduToObj(pdu, callback) {
// 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 (pdu.slice(offset + retObj.params.sm_length, offset + retObj.params.sm_length + 1).toString('hex') === '00') {
log.silly('larvitsmpp: lib/utils.js: pduToObj() - short_message is followed by a NULL octet, increase paramSize one extra 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');
paramSize ++;
}
}
@@ -267,7 +274,8 @@ function pduToObj(pdu, callback) {
}
// If the length is greater than the current offset, there must be TLVs - resolve them!
while (offset < retObj.cmdLength) {
// The minimal size for a TLV is its head, 4 octets
while ((offset + 4) < retObj.cmdLength) {
try {
tlvCmdId = pdu.readInt16BE(offset);
tlvLength = pdu.readInt16BE(offset + 2);
@@ -275,6 +283,7 @@ function pduToObj(pdu, callback) {
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;
}
if (defs.tlvsById[tlvCmdId] === undefined) {
@@ -299,21 +308,32 @@ function pduToObj(pdu, callback) {
'tagName': defs.tlvsById[tlvCmdId].tag,
'tagValue': tlvValue
};
log.silly('larvitsmpp: lib/utils.js: pduToObj() - 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');
pduToObj(pdu, true, callback);
return;
}
if (offset !== retObj.cmdLength) {
log.warn('larvitsmpp: lib/utils.js: pduToObj() - Offset (' + offset + ') !== cmdLength (' + retObj.cmdLength + ') for seqNr: ' + retObj.seqNr);
}
// Decode the short message if it is set
if (retObj.params.short_message !== undefined) {
retObj.params.short_message = decodeMsg(retObj.params.short_message, retObj.params.data_coding);
callback(null, retObj);
} else {
// We need to do the standard callback in an else statement since
// this always would be called before the above callback if we didn't
callback(null, retObj);
}
log.debug('larvitsmpp: lib/utils.js: pduToObj() - Complete decoded PDU: ' + JSON.stringify(retObj));
callback(null, retObj);
}
/**
@@ -357,7 +377,7 @@ function objToPdu(obj, callback) {
return;
}
// All params are mandatory. Set them if they are not set
// Params must be an object
if (obj.params === undefined) {
obj.params = {};
}
@@ -379,6 +399,8 @@ function objToPdu(obj, callback) {
log.silly('larvitsmpp: lib/utils.js: objToPdu() - encoding message "' + shortMsg + '" to "' + obj.params.short_message.toString('hex') + '"');
}
log.debug('larvitsmpp: lib/utils.js: objToPdu() - Complete object to encode: ' + JSON.stringify(obj));
calcCmdLength(obj, function(err, cmdLength) {
if (err) {
callback(err);
@@ -622,4 +644,4 @@ exports.objToPdu = objToPdu;
exports.pduReturn = pduReturn;
exports.smppDate = smppDate;
exports.bitCount = bitCount;
exports.splitMsg = splitMsg;
exports.splitMsg = splitMsg;