10 |
#include <time.h> |
#include <time.h> |
11 |
#include <stdlib.h> |
#include <stdlib.h> |
12 |
|
|
13 |
#include "common.h" |
#include "Logger.h" |
14 |
#include "util.h" |
#include "Util.h" |
15 |
|
|
|
#include <iostream> |
|
16 |
|
|
17 |
using namespace std; |
using namespace std; |
18 |
|
|
|
|
|
19 |
namespace SmsPdu |
namespace SmsPdu |
20 |
{ |
{ |
21 |
|
|
34 |
|
|
35 |
string EncodePhonenr(string input) |
string EncodePhonenr(string input) |
36 |
{ |
{ |
37 |
|
if ( input.at(0) == '+' ) |
38 |
|
input.erase(0,1); |
39 |
|
|
40 |
if ( (input.length() % 2) == 1) |
if ( (input.length() % 2) == 1) |
41 |
input.append("F"); |
input.append("F"); |
42 |
return SwitchChars(input); |
return SwitchChars(input); |
79 |
return vec; |
return vec; |
80 |
} |
} |
81 |
|
|
82 |
std::string Decode8to7bit(vector<unsigned char> input, int shift_start = 0) |
std::string Decode8to7bit(vector<unsigned char> input, int shift_start) |
83 |
{ |
{ |
84 |
string result; |
string result; |
85 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
vector<unsigned char> Encode7to8bit(std::string str) |
vector<unsigned char> Encode7to8bit(std::string str, int shift_start) |
115 |
{ |
{ |
116 |
vector<unsigned char> buf; |
vector<unsigned char> buf; |
117 |
|
|
118 |
int shift = 0; |
int shift = shift_start; |
119 |
for (unsigned int i=0; i<str.size(); ++i) |
for (unsigned int i=0; i<str.size(); ++i) |
120 |
{ |
{ |
121 |
unsigned char current = str.at(i) & 0x7F; |
unsigned char current = str.at(i) & 0x7F; |
144 |
|
|
145 |
const unsigned char UDHI = multipart ? 0x40 : 0; |
const unsigned char UDHI = multipart ? 0x40 : 0; |
146 |
|
|
|
srand(time(0)); |
|
147 |
unsigned char csms_ref = rand() % 128; |
unsigned char csms_ref = rand() % 128; |
148 |
|
|
149 |
int part_count; |
int part_count; |
154 |
{ |
{ |
155 |
if (message.length() > 800) |
if (message.length() > 800) |
156 |
{ |
{ |
157 |
Common::instance()->logMessage("Trying to send multipart sms > 800 bytes !!!"); |
Logger::logMessage("Trying to send multipart sms > 800 bytes !!!"); |
158 |
message = message.substr(0,800); |
message = message.substr(0,800); |
159 |
} |
} |
160 |
|
|
178 |
pdu.push_back(to.length() ); //length of phone nr |
pdu.push_back(to.length() ); //length of phone nr |
179 |
pdu.push_back(0x81); // 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 |
180 |
|
|
181 |
|
|
182 |
string phone = EncodePhonenr(to); |
vector<unsigned char> phone = HexDecodeString( EncodePhonenr(to )); |
183 |
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
184 |
|
|
185 |
pdu.push_back(0x00); // Protocol identifier |
pdu.push_back(0x00); // Protocol identifier |
186 |
pdu.push_back(0x00); // Data coding scheme |
pdu.push_back(0x00); // Data coding scheme |
187 |
|
|
188 |
|
int shift_start = 0; |
189 |
string message_part; |
string message_part; |
190 |
if (multipart) |
if (multipart) |
191 |
{ |
{ |
192 |
message_part = message.substr(0, PDU_LEN); |
message_part = message.substr(0, PDU_LEN); |
193 |
message.erase(0, PDU_LEN); |
message.erase(0, PDU_LEN-1); |
194 |
|
|
195 |
pdu.push_back( message_part.length()+ 7 ); //UserDataLength |
pdu.push_back( message_part.length()+ 7 ); //UserDataLength |
196 |
pdu.push_back( 0x06 ); // UDH Len |
pdu.push_back( 0x06 ); // UDH Len |
200 |
pdu.push_back( part_count ); |
pdu.push_back( part_count ); |
201 |
pdu.push_back( partnr+1 ); |
pdu.push_back( partnr+1 ); |
202 |
pdu.push_back( 0x00); |
pdu.push_back( 0x00); |
203 |
|
//shift_start = 6; |
204 |
} |
} |
205 |
else |
else |
206 |
{ |
{ |
207 |
if (message.length() > 160) |
if (message.length() > 160) |
208 |
{ |
{ |
209 |
message_part = message.substr(0,160); //truncate to 160 |
message_part = message.substr(0,160); //truncate to 160 |
210 |
Common::instance()->logMessage("Truncated message"); |
Logger::logMessage("Truncated message"); |
211 |
} |
} |
212 |
else |
else |
213 |
{ |
{ |
217 |
pdu.push_back( message_part.length() ); //UserDataLength |
pdu.push_back( message_part.length() ); //UserDataLength |
218 |
} |
} |
219 |
|
|
220 |
vector<unsigned char> userData = Encode7to8bit(message_part); |
vector<unsigned char> userData = Encode7to8bit(message_part, shift_start); |
221 |
|
|
222 |
pdu.insert( pdu.end(), userData.begin(), userData.end()); |
pdu.insert( pdu.end(), userData.begin(), userData.end()); |
223 |
|
|
274 |
} |
} |
275 |
|
|
276 |
|
|
277 |
|
|
278 |
SMS ParseSmsPdu(std::string pdu_str) |
SMS ParseSmsPdu(std::string pdu_str) |
279 |
{ |
{ |
280 |
SMS result; |
SmsPart part = ParseSmsPduWorker(pdu_str); |
281 |
|
|
282 |
|
|
283 |
|
SMS sms; |
284 |
|
sms.SetMessage(part.message); |
285 |
|
sms.SetSender(part.sender); |
286 |
|
return sms; |
287 |
|
} |
288 |
|
|
289 |
|
void ParseUdh(vector<unsigned char>& udh, SmsPart& part) |
290 |
|
{ |
291 |
|
if (udh.size() == 0) { |
292 |
|
Logger::logMessage("ParseUdh(): empty udh"); |
293 |
|
return; |
294 |
|
} |
295 |
|
|
296 |
|
if (udh[0] != 0) { |
297 |
|
Logger::logMessage("unknown UDH type"); |
298 |
|
return; |
299 |
|
} |
300 |
|
|
301 |
|
part.group = udh[2]; |
302 |
|
part.count = udh[3]; |
303 |
|
part.id = udh[4]; |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
|
SmsPart ParseSmsPduWorker(std::string pdu_str) |
308 |
|
{ |
309 |
|
|
310 |
vector<unsigned char> pdu = HexDecodeString(pdu_str); |
vector<unsigned char> pdu = HexDecodeString(pdu_str); |
311 |
|
|
323 |
|
|
324 |
++it; //ignore Type-Of-Address |
++it; //ignore Type-Of-Address |
325 |
|
|
326 |
result.sender = DecodeRawPhonenr( it, it+(sender_len/2) ); |
string sender = DecodeRawPhonenr( it, it+(sender_len/2) ); |
327 |
|
|
328 |
it += (sender_len/2); |
it += (sender_len/2); |
329 |
++it; //protocol identifier |
++it; //protocol identifier |
330 |
++it; //Data encoding |
++it; //Data encoding |
331 |
|
|
332 |
result.timestamp = DecodeTimestamp(it, it+7); |
string timestamp = DecodeTimestamp(it, it+7); |
333 |
it += 7; |
it += 7; |
334 |
|
|
335 |
|
|
336 |
unsigned char data_len = (*it++); |
unsigned char data_len = (*it++); |
337 |
|
|
338 |
|
|
339 |
|
SmsPart part; |
340 |
|
part.group = -1; |
341 |
|
|
342 |
int shift_start = 0; |
int shift_start = 0; |
343 |
|
|
344 |
if (UDHI) |
if (UDHI) |
345 |
{ |
{ |
346 |
int udh_len = (*it++); |
int udh_len = (*it++); |
347 |
cout << "UDH_LEN:" << udh_len << endl; |
|
348 |
it += udh_len; //just ignore the User Data Header |
vector<unsigned char> udh; |
349 |
|
for (int i=0; i<udh_len; i++) |
350 |
|
{ |
351 |
|
udh.push_back (*it++); |
352 |
|
} |
353 |
|
ParseUdh(udh,part); |
354 |
|
|
355 |
data_len -= udh_len; |
data_len -= udh_len; |
356 |
|
|
357 |
shift_start = udh_len+1; //make the 8to7bit decode start with the right shift level |
shift_start = udh_len+1; //make the 8to7bit decode start with the right shift level |
358 |
} |
} |
|
cout << hex<< (int)(*it) << endl;; |
|
359 |
|
|
360 |
|
|
361 |
vector<unsigned char> user_data; |
vector<unsigned char> user_data; |
362 |
user_data.insert(user_data.end(), it, it+data_len); |
user_data.insert(user_data.end(), it, it+data_len); |
363 |
|
|
364 |
result.message = Decode8to7bit(user_data, shift_start); |
string message = Decode8to7bit(user_data, shift_start).substr(0,data_len); |
365 |
|
|
366 |
|
message = Util::str_trim(message); |
367 |
|
|
368 |
|
|
369 |
|
part.message = message; |
370 |
|
part.sender = sender; |
371 |
|
|
372 |
return result; |
return part; |
373 |
} |
} |
374 |
|
|
375 |
} |
} |