10 |
#include <time.h> |
#include <time.h> |
11 |
#include <stdlib.h> |
#include <stdlib.h> |
12 |
|
|
13 |
|
#include "common.h" |
14 |
#include "util.h" |
#include "util.h" |
15 |
|
|
16 |
|
|
21 |
{ |
{ |
22 |
|
|
23 |
|
|
|
vector<unsigned char> BcdEncode(string input) |
|
|
{ |
|
|
char buf[2] = " "; |
|
|
vector<unsigned char> result; |
|
24 |
|
|
25 |
unsigned char current; |
string SwitchChars(string input) |
26 |
for (unsigned int i=0; i<input.length(); ++i) |
{ |
27 |
|
for (unsigned int i=1; i<input.length(); i+=2) |
28 |
{ |
{ |
29 |
buf[0] = input.at(i); |
char tmp = input[i]; |
30 |
unsigned char tmp = atoi(buf); |
input[i] = input[i-1]; |
31 |
|
input[i-1] = tmp; |
|
if ( (i%2) == 0) |
|
|
{ |
|
|
current = tmp; |
|
|
} |
|
|
else |
|
|
{ |
|
|
current |= (tmp<<4); |
|
|
result.push_back(current); |
|
|
} |
|
32 |
} |
} |
33 |
|
return input; |
34 |
|
} |
35 |
|
|
36 |
return result; |
string EncodePhonenr(string input) |
37 |
|
{ |
38 |
|
if ( (input.length() % 2) == 1) |
39 |
|
input.append("F"); |
40 |
|
return SwitchChars(input); |
41 |
} |
} |
42 |
|
|
43 |
|
string DecodePhonenr(string input) |
44 |
|
{ |
45 |
|
input = SwitchChars(input); |
46 |
|
int last = input.length() -1; |
47 |
|
|
48 |
|
if (input.at(last) == 'F') |
49 |
|
input.erase(last,1); |
50 |
|
return input; |
51 |
|
} |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
string HexformatVector(vector<unsigned char> vec) |
string HexformatVector(vector<unsigned char> vec) |
57 |
{ |
{ |
58 |
ostringstream os; |
ostringstream os; |
68 |
return os.str(); |
return os.str(); |
69 |
} |
} |
70 |
|
|
71 |
|
std::string Decode8to7bit(vector<unsigned char> input) |
72 |
|
{ |
73 |
|
string result; |
74 |
|
|
75 |
|
int shift = 0; |
76 |
|
for (unsigned int i=0; i<input.size(); ++i) |
77 |
|
{ |
78 |
|
unsigned char current = input.at(i); |
79 |
|
unsigned char prev = (i>0) ? input.at(i-1) : 0; |
80 |
|
|
81 |
|
current <<= shift; |
82 |
|
|
83 |
|
prev >>= (8-shift); |
84 |
|
|
85 |
|
unsigned char byte = current | prev; |
86 |
|
byte &= 0x7F; |
87 |
|
|
88 |
|
result.append(1, byte); |
89 |
|
|
90 |
|
if (shift == 6) |
91 |
|
result.append(1, input.at(i) >> 1); |
92 |
|
|
93 |
|
shift = (shift+1) % 7; |
94 |
|
|
95 |
|
} |
96 |
|
|
97 |
|
return result; |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
vector<unsigned char> Encode7to8bit(std::string str) |
vector<unsigned char> Encode7to8bit(std::string str) |
104 |
{ |
{ |
105 |
vector<unsigned char> buf; |
vector<unsigned char> buf; |
131 |
{ |
{ |
132 |
bool multipart = allowMultipart && message.length() > 160; |
bool multipart = allowMultipart && message.length() > 160; |
133 |
|
|
134 |
const unsigned char UHDI = multipart ? 0x40 : 0; |
const unsigned char UDHI = multipart ? 0x40 : 0; |
135 |
|
|
136 |
srand(time(0)); |
srand(time(0)); |
137 |
unsigned char csms_ref = rand() % 256; |
unsigned char csms_ref = rand() % 128; |
138 |
|
|
139 |
int part_count; |
int part_count; |
140 |
|
|
141 |
|
const int PDU_LEN = 153; |
142 |
|
|
143 |
if (multipart) |
if (multipart) |
144 |
{ |
{ |
145 |
part_count = message.length() / 153; |
if (message.length() > 800) |
146 |
if (message.length() % 153) |
{ |
147 |
|
Common::instance()->logMessage("Trying to send multipart sms > 800 bytes !!!"); |
148 |
|
message = message.substr(0,800); |
149 |
|
} |
150 |
|
|
151 |
|
part_count = message.length() / PDU_LEN; |
152 |
|
if (message.length() % PDU_LEN) |
153 |
part_count++; |
part_count++; |
154 |
} |
} |
155 |
else |
else |
163 |
vector<unsigned char> pdu; |
vector<unsigned char> pdu; |
164 |
|
|
165 |
pdu.push_back(0x00); // use SMSC from phone |
pdu.push_back(0x00); // use SMSC from phone |
166 |
pdu.push_back( 0x01|UHDI ); // first octet -- no timeout |
pdu.push_back( 0x01|UDHI ); // first octet -- no timeout |
167 |
pdu.push_back(0x00); // TP-MR message reference |
pdu.push_back(0x00); // TP-MR message reference |
168 |
pdu.push_back(to.length() ); //length of phone nr |
pdu.push_back(to.length() ); //length of phone nr |
169 |
pdu.push_back(0x91); // type of address (international nr + ISDN/telephone range) - else try 0x81 |
pdu.push_back(0x81); // type of address (international nr + ISDN/telephone range) - else try 0x81 |
170 |
|
|
171 |
vector<unsigned char> phone = BcdEncode(to); |
|
172 |
|
string phone = EncodePhonenr(to); |
173 |
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
174 |
|
|
175 |
pdu.push_back(0x00); // Protocol identifier |
pdu.push_back(0x00); // Protocol identifier |
176 |
pdu.push_back(0x00); // Data coding scheme |
pdu.push_back(0x00); // Data coding scheme |
177 |
|
|
178 |
string message_part; |
string message_part; |
179 |
if (multipart) |
if (multipart) |
180 |
{ |
{ |
181 |
message_part = message.substr(0,153); |
message_part = message.substr(0, PDU_LEN); |
182 |
message.erase(0,153); |
message.erase(0, PDU_LEN); |
183 |
|
|
184 |
pdu.push_back( message_part.length()+5 ); //UserDataLength |
pdu.push_back( message_part.length()+ 7 ); //UserDataLength |
185 |
pdu.push_back( 0x00 ); // UDH[0] |
pdu.push_back( 0x06 ); // UDH Len |
186 |
pdu.push_back( 0x03 ); // UDH[1] = UDH_LEN |
pdu.push_back( 0x00 ); // UDH Element Identifier |
187 |
pdu.push_back( csms_ref ); //UDH[2] |
pdu.push_back( 0x03 ); // UDH element length |
188 |
|
pdu.push_back( csms_ref ); // csms_ref reference |
189 |
pdu.push_back( part_count ); |
pdu.push_back( part_count ); |
190 |
pdu.push_back( partnr ); |
pdu.push_back( partnr+1 ); |
191 |
|
pdu.push_back( 0x00); |
192 |
|
|
193 |
} |
} |
194 |
else |
else |
195 |
{ |
{ |
196 |
message_part = message.substr(0,160); //truncate to 160 |
if (message.length() > 160) |
197 |
|
{ |
198 |
|
message_part = message.substr(0,160); //truncate to 160 |
199 |
|
Common::instance()->logMessage("Truncated message"); |
200 |
|
} |
201 |
|
else |
202 |
|
{ |
203 |
|
message_part = message; |
204 |
|
} |
205 |
|
|
206 |
pdu.push_back( message_part.length() ); //UserDataLength |
pdu.push_back( message_part.length() ); //UserDataLength |
207 |
} |
} |