1 |
torben |
59 |
/* using http://sourceforge.net/projects/libserial/ |
2 |
|
|
*/ |
3 |
|
|
|
4 |
|
|
|
5 |
torben |
60 |
#include "SmsPdu.h" |
6 |
|
|
|
7 |
torben |
59 |
#include <string> |
8 |
|
|
#include <sstream> |
9 |
|
|
|
10 |
torben |
60 |
#include <time.h> |
11 |
|
|
#include <stdlib.h> |
12 |
torben |
59 |
|
13 |
torben |
63 |
#include "common.h" |
14 |
torben |
59 |
#include "util.h" |
15 |
|
|
|
16 |
|
|
|
17 |
|
|
using namespace std; |
18 |
|
|
|
19 |
|
|
namespace SmsPdu |
20 |
|
|
{ |
21 |
|
|
|
22 |
|
|
|
23 |
torben |
68 |
|
24 |
torben |
69 |
string SwitchChars(string input) |
25 |
torben |
59 |
{ |
26 |
torben |
69 |
for (unsigned int i=1; i<input.length(); i+=2) |
27 |
|
|
{ |
28 |
|
|
char tmp = input[i]; |
29 |
|
|
input[i] = input[i-1]; |
30 |
|
|
input[i-1] = tmp; |
31 |
|
|
} |
32 |
|
|
return input; |
33 |
torben |
59 |
} |
34 |
|
|
|
35 |
torben |
69 |
string EncodePhonenr(string input) |
36 |
|
|
{ |
37 |
torben |
73 |
if ( input.at(0) == '+' ) |
38 |
|
|
input.erase(0,1); |
39 |
|
|
|
40 |
torben |
69 |
if ( (input.length() % 2) == 1) |
41 |
|
|
input.append("F"); |
42 |
|
|
return SwitchChars(input); |
43 |
|
|
} |
44 |
torben |
61 |
|
45 |
torben |
69 |
string DecodePhonenr(string input) |
46 |
|
|
{ |
47 |
|
|
input = SwitchChars(input); |
48 |
|
|
int last = input.length() -1; |
49 |
torben |
68 |
|
50 |
torben |
69 |
if (input.at(last) == 'F') |
51 |
|
|
input.erase(last,1); |
52 |
|
|
return input; |
53 |
|
|
} |
54 |
torben |
59 |
|
55 |
torben |
68 |
|
56 |
|
|
|
57 |
|
|
|
58 |
torben |
69 |
string HexformatVector(vector<unsigned char> vec) |
59 |
torben |
59 |
{ |
60 |
torben |
69 |
ostringstream os; |
61 |
torben |
59 |
|
62 |
torben |
69 |
for (unsigned int i=0; i<vec.size(); ++i) |
63 |
|
|
{ |
64 |
|
|
os.width(2); |
65 |
|
|
os.fill('0'); |
66 |
|
|
os << hex << uppercase << static_cast<unsigned int>(vec.at(i)); |
67 |
|
|
} |
68 |
torben |
59 |
|
69 |
|
|
|
70 |
torben |
69 |
return os.str(); |
71 |
|
|
} |
72 |
torben |
61 |
|
73 |
torben |
69 |
vector<unsigned char> HexDecodeString(string str) |
74 |
torben |
68 |
{ |
75 |
torben |
69 |
vector<unsigned char> vec; |
76 |
|
|
for (unsigned int i=0; i<str.length(); i+=2) |
77 |
|
|
vec.push_back( strtol( str.substr(i,2).c_str(), 0, 16 )); |
78 |
torben |
68 |
|
79 |
torben |
69 |
return vec; |
80 |
|
|
} |
81 |
torben |
68 |
|
82 |
torben |
73 |
std::string Decode8to7bit(vector<unsigned char> input, int shift_start) |
83 |
torben |
69 |
{ |
84 |
|
|
string result; |
85 |
torben |
68 |
|
86 |
torben |
70 |
int shift = shift_start; |
87 |
torben |
69 |
for (unsigned int i=0; i<input.size(); ++i) |
88 |
|
|
{ |
89 |
|
|
unsigned char current = input.at(i); |
90 |
|
|
unsigned char prev = (i>0) ? input.at(i-1) : 0; |
91 |
torben |
68 |
|
92 |
torben |
69 |
current <<= shift; |
93 |
torben |
68 |
|
94 |
torben |
69 |
prev >>= (8-shift); |
95 |
torben |
68 |
|
96 |
torben |
69 |
unsigned char byte = current | prev; |
97 |
|
|
byte &= 0x7F; |
98 |
torben |
68 |
|
99 |
torben |
69 |
result.append(1, byte); |
100 |
torben |
68 |
|
101 |
torben |
69 |
if (shift == 6) |
102 |
|
|
result.append(1, input.at(i) >> 1); |
103 |
torben |
61 |
|
104 |
torben |
69 |
shift = (shift+1) % 7; |
105 |
torben |
68 |
|
106 |
torben |
69 |
} |
107 |
torben |
68 |
|
108 |
torben |
69 |
return result; |
109 |
|
|
} |
110 |
torben |
68 |
|
111 |
torben |
59 |
|
112 |
torben |
69 |
|
113 |
|
|
|
114 |
torben |
73 |
vector<unsigned char> Encode7to8bit(std::string str, int shift_start) |
115 |
torben |
59 |
{ |
116 |
torben |
69 |
vector<unsigned char> buf; |
117 |
torben |
59 |
|
118 |
torben |
73 |
int shift = shift_start; |
119 |
torben |
69 |
for (unsigned int i=0; i<str.size(); ++i) |
120 |
|
|
{ |
121 |
|
|
unsigned char current = str.at(i) & 0x7F; |
122 |
|
|
unsigned char next = ( (i+1)<str.size()) ? str.at(i+1) : 0; |
123 |
|
|
next &= 0x7F; |
124 |
torben |
59 |
|
125 |
torben |
69 |
current >>= shift; |
126 |
|
|
next <<= (7-shift); |
127 |
torben |
59 |
|
128 |
torben |
69 |
unsigned char byte = current | next; |
129 |
|
|
buf.push_back(byte); |
130 |
torben |
59 |
|
131 |
torben |
69 |
if (shift == 6) |
132 |
|
|
i++; |
133 |
torben |
59 |
|
134 |
torben |
69 |
shift = (shift+1) % 7; |
135 |
|
|
|
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
return buf; |
139 |
torben |
59 |
} |
140 |
|
|
|
141 |
torben |
69 |
vector<PduInfo> CreateSmsPdu(string to, string message, bool allowMultipart) |
142 |
|
|
{ |
143 |
|
|
bool multipart = allowMultipart && message.length() > 160; |
144 |
torben |
59 |
|
145 |
torben |
69 |
const unsigned char UDHI = multipart ? 0x40 : 0; |
146 |
torben |
59 |
|
147 |
torben |
69 |
srand(time(0)); |
148 |
|
|
unsigned char csms_ref = rand() % 128; |
149 |
torben |
59 |
|
150 |
torben |
69 |
int part_count; |
151 |
torben |
59 |
|
152 |
torben |
69 |
const int PDU_LEN = 153; |
153 |
torben |
61 |
|
154 |
torben |
69 |
if (multipart) |
155 |
|
|
{ |
156 |
|
|
if (message.length() > 800) |
157 |
|
|
{ |
158 |
|
|
Common::instance()->logMessage("Trying to send multipart sms > 800 bytes !!!"); |
159 |
|
|
message = message.substr(0,800); |
160 |
|
|
} |
161 |
torben |
61 |
|
162 |
torben |
69 |
part_count = message.length() / PDU_LEN; |
163 |
|
|
if (message.length() % PDU_LEN) |
164 |
|
|
part_count++; |
165 |
|
|
} |
166 |
|
|
else |
167 |
torben |
65 |
{ |
168 |
torben |
69 |
part_count = 1; |
169 |
torben |
65 |
} |
170 |
|
|
|
171 |
torben |
69 |
vector<PduInfo> result; |
172 |
|
|
for (int partnr = 0; partnr < part_count; ++partnr) |
173 |
|
|
{ |
174 |
|
|
vector<unsigned char> pdu; |
175 |
torben |
59 |
|
176 |
torben |
69 |
pdu.push_back(0x00); // use SMSC from phone |
177 |
|
|
pdu.push_back( 0x01|UDHI ); // first octet -- no timeout |
178 |
|
|
pdu.push_back(0x00); // TP-MR message reference |
179 |
|
|
pdu.push_back(to.length() ); //length of phone nr |
180 |
|
|
pdu.push_back(0x81); // type of address (international nr + ISDN/telephone range) - else try 0x81 |
181 |
torben |
59 |
|
182 |
torben |
73 |
|
183 |
|
|
vector<unsigned char> phone = HexDecodeString( EncodePhonenr(to )); |
184 |
torben |
69 |
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
185 |
torben |
60 |
|
186 |
torben |
69 |
pdu.push_back(0x00); // Protocol identifier |
187 |
|
|
pdu.push_back(0x00); // Data coding scheme |
188 |
torben |
60 |
|
189 |
torben |
73 |
int shift_start = 0; |
190 |
torben |
69 |
string message_part; |
191 |
|
|
if (multipart) |
192 |
|
|
{ |
193 |
|
|
message_part = message.substr(0, PDU_LEN); |
194 |
|
|
message.erase(0, PDU_LEN); |
195 |
torben |
60 |
|
196 |
torben |
69 |
pdu.push_back( message_part.length()+ 7 ); //UserDataLength |
197 |
|
|
pdu.push_back( 0x06 ); // UDH Len |
198 |
|
|
pdu.push_back( 0x00 ); // UDH Element Identifier |
199 |
|
|
pdu.push_back( 0x03 ); // UDH element length |
200 |
|
|
pdu.push_back( csms_ref ); // csms_ref reference |
201 |
|
|
pdu.push_back( part_count ); |
202 |
|
|
pdu.push_back( partnr+1 ); |
203 |
|
|
pdu.push_back( 0x00); |
204 |
torben |
73 |
//shift_start = 6; |
205 |
torben |
63 |
} |
206 |
|
|
else |
207 |
|
|
{ |
208 |
torben |
69 |
if (message.length() > 160) |
209 |
|
|
{ |
210 |
|
|
message_part = message.substr(0,160); //truncate to 160 |
211 |
|
|
Common::instance()->logMessage("Truncated message"); |
212 |
|
|
} |
213 |
|
|
else |
214 |
|
|
{ |
215 |
|
|
message_part = message; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
pdu.push_back( message_part.length() ); //UserDataLength |
219 |
torben |
63 |
} |
220 |
torben |
60 |
|
221 |
torben |
73 |
vector<unsigned char> userData = Encode7to8bit(message_part, shift_start); |
222 |
torben |
69 |
|
223 |
|
|
pdu.insert( pdu.end(), userData.begin(), userData.end()); |
224 |
|
|
|
225 |
|
|
PduInfo info; |
226 |
|
|
info.len = pdu.size()-1; |
227 |
|
|
info.pdu = HexformatVector(pdu); |
228 |
|
|
result.push_back(info); |
229 |
|
|
|
230 |
torben |
60 |
} |
231 |
torben |
69 |
return result; |
232 |
|
|
} |
233 |
torben |
60 |
|
234 |
torben |
69 |
typedef vector<unsigned char>::iterator char_it; |
235 |
torben |
60 |
|
236 |
torben |
69 |
string DecodeRawPhonenr(char_it start, char_it end) |
237 |
|
|
{ |
238 |
|
|
ostringstream os; |
239 |
torben |
60 |
|
240 |
torben |
69 |
for (char_it it = start; it != end; ++it) |
241 |
|
|
{ |
242 |
|
|
os.width(2); |
243 |
|
|
os.fill('0'); |
244 |
|
|
os << hex << (int)(*it); |
245 |
|
|
} |
246 |
|
|
return DecodePhonenr(os.str()); |
247 |
|
|
} |
248 |
torben |
60 |
|
249 |
torben |
69 |
string DecodeTimestamp(char_it start, char_it end) |
250 |
|
|
{ |
251 |
|
|
string stamp = DecodeRawPhonenr(start,end); |
252 |
|
|
|
253 |
|
|
ostringstream os; |
254 |
|
|
|
255 |
|
|
os << stamp.substr(0,2) << "/"; //year |
256 |
|
|
os << stamp.substr(2,2) << "/"; //month |
257 |
|
|
os << stamp.substr(4,2) << ","; //day |
258 |
|
|
os << stamp.substr(6,2) << ":"; //hour |
259 |
|
|
os << stamp.substr(8,2) << ":"; //minute |
260 |
|
|
os << stamp.substr(10,2) ; //second |
261 |
|
|
|
262 |
|
|
int timezone = strtol(stamp.substr(12,2).c_str(), 0, 16); |
263 |
|
|
|
264 |
|
|
|
265 |
|
|
if (timezone > 127) |
266 |
|
|
os << "-"; |
267 |
|
|
else |
268 |
|
|
os << "+"; |
269 |
|
|
|
270 |
|
|
os.width(2); |
271 |
|
|
os.fill('0'); |
272 |
|
|
os << (timezone & 0x7F); |
273 |
|
|
|
274 |
|
|
return os.str(); |
275 |
torben |
60 |
} |
276 |
torben |
59 |
|
277 |
torben |
69 |
|
278 |
|
|
SMS ParseSmsPdu(std::string pdu_str) |
279 |
|
|
{ |
280 |
|
|
SMS result; |
281 |
|
|
|
282 |
|
|
vector<unsigned char> pdu = HexDecodeString(pdu_str); |
283 |
|
|
|
284 |
|
|
vector<unsigned char>::iterator it = pdu.begin(); |
285 |
|
|
|
286 |
|
|
it += (*it++); // Skip smsc info |
287 |
|
|
|
288 |
|
|
unsigned char deliver_first_octet = (*it++); |
289 |
torben |
70 |
|
290 |
|
|
bool UDHI = (deliver_first_octet & 0x40) > 0; |
291 |
|
|
|
292 |
torben |
69 |
unsigned char sender_len = (*it++); |
293 |
|
|
if ( (sender_len % 2) == 1) |
294 |
|
|
sender_len++; |
295 |
|
|
|
296 |
|
|
++it; //ignore Type-Of-Address |
297 |
|
|
|
298 |
|
|
result.sender = DecodeRawPhonenr( it, it+(sender_len/2) ); |
299 |
|
|
|
300 |
|
|
it += (sender_len/2); |
301 |
|
|
++it; //protocol identifier |
302 |
|
|
++it; //Data encoding |
303 |
|
|
|
304 |
|
|
result.timestamp = DecodeTimestamp(it, it+7); |
305 |
|
|
it += 7; |
306 |
|
|
|
307 |
torben |
70 |
|
308 |
torben |
69 |
unsigned char data_len = (*it++); |
309 |
|
|
|
310 |
torben |
70 |
int shift_start = 0; |
311 |
|
|
if (UDHI) |
312 |
|
|
{ |
313 |
|
|
int udh_len = (*it++); |
314 |
|
|
it += udh_len; //just ignore the User Data Header |
315 |
|
|
data_len -= udh_len; |
316 |
|
|
|
317 |
|
|
shift_start = udh_len+1; //make the 8to7bit decode start with the right shift level |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
|
321 |
torben |
69 |
vector<unsigned char> user_data; |
322 |
|
|
user_data.insert(user_data.end(), it, it+data_len); |
323 |
|
|
|
324 |
torben |
77 |
result.message = Decode8to7bit(user_data, shift_start).substr(0,data_len); |
325 |
torben |
69 |
|
326 |
|
|
|
327 |
|
|
return result; |
328 |
|
|
} |
329 |
|
|
|
330 |
torben |
59 |
} |