diff --git a/README.md b/README.md index d6f3512..caad309 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ var pdu = require('pdu'); pdu.generate({ text:'Some text', receiver:999999999999, //MSISDN - encoding:'16bit' //Or 7bit if you're sending an ascii message. + encoding:'16bit' //Or 7bit if you're sending a message made of GSM standard alphabet characters. }); ``` diff --git a/pdu.js b/pdu.js index 259f9fe..6b2a328 100644 --- a/pdu.js +++ b/pdu.js @@ -1,5 +1,9 @@ var pduParser = {}; +var sevenBitDefault = new Array('@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', '\n', 'Ø', 'ø', '\r','Å', 'å','\u0394', '_', '\u03a6', '\u0393', '\u039b', '\u03a9', '\u03a0','\u03a8', '\u03a3', '\u0398', '\u039e','\x1b', 'Æ', 'æ', 'ß', 'É', ' ', '!', '"', '#', '¤', '%', '&', '\'', '(', ')','*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7','8', '9', ':', ';', '<', '=', '>', '?', '¡', 'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S','T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Ä', 'Ö', 'Ñ', 'Ü', '§', '¿', 'a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ä', 'ö', 'ñ','ü', 'à'); +var sevenBitEsc = new Array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '^', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '{', '}', '', '', '', '', '', '\\', '', '', '', '', '', '', '', '', '', '', '', '', '[', '~', ']', + '', '|', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '€', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''); + pduParser.parse = function(pdu) { //Cursor points to the last octet we've read. var cursor = 0; @@ -76,11 +80,10 @@ pduParser.parse = function(pdu) { var currentPart = pdu.slice(cursor+12, cursor+14); } - if(encoding === '16bit') - if(iei == '00') - cursor += (udhLength-2)*4; - else if(iei == '08') - cursor += ((udhLength-2)*4)+2; + if(iei == '00') + cursor += (udhLength-2)*4; + else if(iei == '08') + cursor += ((udhLength-2)*4)+2; else cursor += (udhLength-2)*2; } @@ -88,7 +91,9 @@ pduParser.parse = function(pdu) { if(encoding === '16bit') var text = pduParser.decode16Bit(pdu.slice(cursor), dataLength); else if(encoding === '7bit') - var text = pduParser.decode7Bit(pdu.slice(cursor), dataLength); + if (udhi && iei=='00') var text = pduParser.decode7Bit(pdu.slice(cursor), dataLength-7, 1); //If iei ==0, then there is some unpadding to do + else if (udhi && iei=='08') var text = pduParser.decode7Bit(pdu.slice(cursor), dataLength-8); //If no udhi or iei = 08 then no unpadding to do + else var text = pduParser.decode7Bit(pdu.slice(cursor), dataLength); else if(encoding === '8bit') var text = ''; //TODO @@ -142,7 +147,7 @@ pduParser.detectEncoding = function(dataCodingScheme) { pduParser.decode16Bit = function(data, length) { //We are getting ucs2 characters. var ucs2 = ''; - for(var i = 0;i<=data.length;i=i+4) { + for(var i = 0;i<=data.length-1;i=i+4) { ucs2 += String.fromCharCode("0x"+data[i]+data[i+1]+data[i+2]+data[i+3]); } @@ -160,12 +165,24 @@ pduParser.deSwapNibbles = function(nibbles) { return out; } -pduParser.decode7Bit = function(code, count) { +pduParser.decode7Bit = function(code, length, unPadding) { //We are getting 'septeps'. We should decode them. var binary = ''; for(var i = 0; i> bits; + if( i < inTextNumberArray.length - 1 ) + {octet |= (inTextNumberArray[i + 1] << (7 - bits))%256;} + out += ('00' + octet.toString(16)).slice(-2); + bits++; } + return out; +} - var hex = ''; - for(i in octets) - if(octets[i].length > 0) - hex += ('00'+(parseInt(octets[i], 2).toString(16))).slice(-2); - return hex; +pduParser.encode16Bit = function(inTextNumberArray) { + var out = ''; + for(var i = 0; i 70) - parts = message.text.length / 66; + var inTextNumberArray = this.messageToNumberArray(message); - else if(message.encoding === '7bit' && message.text.length > 160) - parts = message.text.length / 153; + if(message.encoding === '16bit' && inTextNumberArray.length > 70) + parts = inTextNumberArray.length / 66; + + else if(message.encoding === '7bit' && inTextNumberArray.length > 160) + parts = inTextNumberArray.length / 153; parts = Math.ceil(parts); @@ -279,7 +347,7 @@ pduParser.generate = function(message) { else var length = 153; } - var text = message.text.slice(i*length, (i*length)+length); + var text = inTextNumberArray.slice(i*length, (i*length)+length); if(message.encoding === '16bit') { user_data = pduParser.encode16Bit(text); @@ -289,8 +357,14 @@ pduParser.generate = function(message) { size += 6; //6 is the number of data headers we append. } else if(message.encoding === '7bit') { - user_data = pduParser.encode7Bit(text); - var size = user_data.length / 2; + if(parts > 1){ + user_data = pduParser.encode7Bit(text,1); + var size = 7 + text.length; + } + else { + user_data = pduParser.encode7Bit(text); + var size = text.length; + } } pdus[i] += ('00'+parseInt(size).toString(16)).slice(-2); @@ -310,14 +384,6 @@ pduParser.generate = function(message) { } -pduParser.encode16Bit = function(text) { - var out = ''; - for(var i = 0; i