-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
279 lines (253 loc) · 7.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// this code was originally written by Swen-Peter Ekkebus and Andrew Alexander.
//Array with "The 7 bit defaultalphabet"
var sevenbitdefault = ['@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', '\n', 'Ø', 'ø', '\r','Å', 'å','\u0394', '_', '\u03a6', '\u0393', '\u039b', '\u03a9', '\u03a0','\u03a8', '\u03a3', '\u0398', '\u039e','€', 'Æ', 'æ', 'ß', 'É', ' ', '!', '"', '#', '¤', '%', '&', '\'', '(', ')','*', '+', ',', '-', '.', '/', '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', 'ä', 'ö', 'ñ','ü', 'à'];
function phoneNumberMap(character) {
// return character;
if ((character >= '0') && (character <= '9')) {
return character;
}
switch (character.toUpperCase()) {
case '*':
return 'A';
case '#':
return 'B';
case 'A':
return 'C';
case 'B':
return 'D';
case 'C':
return 'E';
// case '+':
// return '+'; // An exception to fit with current processing ...
default:
return 'F';
}
}
function toHex(i) {
var sHex = "0123456789ABCDEF";
var out = "";
out = sHex.charAt(i&0xf);
i>>=4;
out = sHex.charAt(i&0xf) + out;
return out;
}
// function to convert a integer into a bit String
function intToBin(x,size) {
//sp
var base = 2;
var num = parseInt(x);
var bin = num.toString(base);
for (var i=bin.length;i<size;i++) {
bin = "0" + bin;
}
return bin;
}
// function te convert a bit string into a integer
function binToInt(x) {
//sp
var total = 0;
var power = parseInt(x.length) - 1;
for (var i=0;i<x.length;i++) {
if(x.charAt(i) == '1') {
total = total +Math.pow(2,power);
}
power --;
}
return total;
}
//function to convert integer to Hex
function intToHex(i) {
// sp
var sHex = "0123456789ABCDEF";
var h = "";
i = parseInt(i);
for (var j = 0; j <= 3; j++) {
h += sHex.charAt((i >> (j * 8 + 4)) & 0x0F) + sHex.charAt((i >> (j * 8)) & 0x0F);
}
return h.substring(0,2);
}
// function to convert semioctets to a string
function semiOctetToString(inp) {
// sp
var out = "";
for (var i=0;i<inp.length;i=i+2) {
var temp = inp.substring(i,i+2);
out = out + phoneNumberMap(temp.charAt(1)) + phoneNumberMap(temp.charAt(0));
}
return out;
}
function getSevenBit(character) {
// sp
for (var i=0;i<sevenbitdefault.length;i++) {
if(sevenbitdefault[i] == character) {
return i;
}
}
// alert("No 7 bit char for " + character);
return 0;
}
function getEightBit(character) {
return character;
}
function get16Bit(character) {
return character;
}
function stringToPDU(inpString, phoneNumber, smscNumber, bitSize, mclass, valid, receipt) {
// AJA fixed SMSC processing
var octetFirst = "";
var octetSecond = "";
var output = "";
var i;
var firstOctet; // = "1100";
var REIVER_NUMBER_FORMAT = "81"; // national
var REIVER_NUMBER_LENGTH;
var PROTO_ID = "00";
var DCS = 0;
var VALID_PERIOD = ""; // AA
var REIVER_NUMBER;
var DATA_ENCODING;
var userDataSize;
var current;
var currentOctet;
//Make header
var SMSC_INFO_LENGTH = 0;
var SMSC_LENGTH = 0;
var SMSC_NUMBER_FORMAT = "";
var SMSC = "";
if (!bitSize) {
bitSize = 8;
}
if (smscNumber) {
SMSC_NUMBER_FORMAT = "81"; // national
if (smscNumber[0] == '+') {
SMSC_NUMBER_FORMAT = "91"; // international
smscNumber = smscNumber.substr(1);
} else {
if (smscNumber[0] !='0') {
SMSC_NUMBER_FORMAT = "91"; // international
}
}
if (smscNumber.length%2 != 0) {
// add trailing F
smscNumber += "F";
}
SMSC = semiOctetToString(smscNumber);
SMSC_INFO_LENGTH = ((SMSC_NUMBER_FORMAT + "" + SMSC).length)/2;
SMSC_LENGTH = SMSC_INFO_LENGTH;
}
if (SMSC_INFO_LENGTH < 10) {
SMSC_INFO_LENGTH = "0" + SMSC_INFO_LENGTH;
}
if (receipt) {
if (valid) {
firstOctet = "3100"; // 18 is mask for validity period // 10 indicates relative
} else {
firstOctet = "2100";
}
} else {
if (valid) {
firstOctet = "1100";
} else {
firstOctet = "0100";
}
}
if (phoneNumber[0] == '+') {
REIVER_NUMBER_FORMAT = "91"; // international
phoneNumber = phoneNumber.substr(1); //,phoneNumber.length-1);
} else {
if (phoneNumber[0] != '0') {
REIVER_NUMBER_FORMAT = "91"; // international
}
}
REIVER_NUMBER_LENGTH = intToHex(phoneNumber.length);
if(phoneNumber.length%2 != 0) {
// add trailing F
phoneNumber+= "F";
}
REIVER_NUMBER = semiOctetToString(phoneNumber);
if ([0, 1, 2, 3].indexOf(mclass) !== -1) {
// AJA
DCS = mclass | 0x10;
}
switch(bitSize) {
case 7:
break;
case 8:
DCS = DCS | 4;
break;
case 16:
DCS = DCS | 8;
break;
}
DATA_ENCODING = intToHex(DCS);
// var DATA_ENCODING = "00"; // Default
// if (bitSize == 8)
// {
// DATA_ENCODING = "04";
// }
// else if (bitSize == 16)
// {
// DATA_ENCODING = "08";
// }
if (valid) {
VALID_PERIOD = intToHex(valid); // AA
}
switch (bitSize) {
case 7:
userDataSize = intToHex(inpString.length);
for (i=0;i<=inpString.length;i++) {
if (i==inpString.length) {
if (octetSecond != "") {
// AJA Fix overshoot
output = output + "" + (intToHex(binToInt(octetSecond)));
}
break;
}
current = intToBin(getSevenBit(inpString.charAt(i)),7);
if (i!=0 && i%8!=0) {
octetFirst = current.substring(7-(i)%8);
currentOctet = octetFirst + octetSecond; //put octet parts together
output+= "" + (intToHex(binToInt(currentOctet)));
octetSecond = current.substring(0,7-(i)%8); //set net second octet
} else {
octetSecond = current.substring(0,7-(i)%8);
}
}
break;
case 8:
userDataSize = intToHex(inpString.length);
var CurrentByte = 0;
for (i=0;i<inpString.length;i++) {
CurrentByte = getEightBit(inpString.charCodeAt(i));
output = output + "" + ( toHex( CurrentByte ) );
}
break;
case 16:
userDataSize = intToHex(inpString.length * 2);
var myChar = 0;
for (i = 0; i < inpString.length; i++) {
myChar = get16Bit(inpString.charCodeAt(i));
output = output + "" + ( toHex((myChar & 0xff00) >> 8)) + ( toHex(myChar & 0xff) );
}
break;
}
var header = SMSC_INFO_LENGTH + SMSC_NUMBER_FORMAT + SMSC + firstOctet + REIVER_NUMBER_LENGTH + REIVER_NUMBER_FORMAT + REIVER_NUMBER + PROTO_ID + DATA_ENCODING + VALID_PERIOD + userDataSize;
/*
console.log('HEADER:', SMSC_INFO_LENGTH, SMSC_NUMBER_FORMAT, SMSC, firstOctet, REIVER_NUMBER_LENGTH, REIVER_NUMBER_FORMAT,
REIVER_NUMBER, PROTO_ID, DATA_ENCODING, VALID_PERIOD, userDataSize);
*/
// console.log('OUTPUT:', output);
var PDU = header + output;
var AT = "AT+CMGS=" + (PDU.length/2 - SMSC_LENGTH - 1) ; // Add /2 for PDU length AJA - I think the SMSC information should also be excluded
// var bStep=18;
// for(var breakUp=1;breakUp*bStep < PDU.length;breakUp++)
// {
// PDU = PDU.substr(0,breakUp*bStep+breakUp-1) + " " + PDU.substr(breakUp*bStep+breakUp-1);
// }
//CMGW
return {
command: AT,
pdu: PDU
};
}
module.exports = stringToPDU;