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 |
|
|
16 |
|
|
17 |
using namespace std; |
using namespace std; |
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 |
|
|
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 |
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 |
{ |
{ |
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 |
|
{ |
293 |
|
Logger::logMessage("ParseUdh(): empty udh"); |
294 |
|
return; |
295 |
|
} |
296 |
|
|
297 |
|
if (udh[0] != 0) |
298 |
|
{ |
299 |
|
Logger::logMessage("unknown UDH type"); |
300 |
|
return; |
301 |
|
} |
302 |
|
|
303 |
|
if (udh.size() < 5) |
304 |
|
{ |
305 |
|
Logger::logMessage("UDH to short to be multipart"); |
306 |
|
return; |
307 |
|
} |
308 |
|
|
309 |
|
part.group = udh[2]; |
310 |
|
part.count = udh[3]; |
311 |
|
part.id = udh[4]; |
312 |
|
} |
313 |
|
|
314 |
|
|
315 |
|
SmsPart ParseSmsPduWorker(std::string pdu_str) |
316 |
|
{ |
317 |
|
|
318 |
vector<unsigned char> pdu = HexDecodeString(pdu_str); |
vector<unsigned char> pdu = HexDecodeString(pdu_str); |
319 |
|
|
331 |
|
|
332 |
++it; //ignore Type-Of-Address |
++it; //ignore Type-Of-Address |
333 |
|
|
334 |
result.sender = DecodeRawPhonenr( it, it+(sender_len/2) ); |
string sender = DecodeRawPhonenr( it, it+(sender_len/2) ); |
335 |
|
|
336 |
it += (sender_len/2); |
it += (sender_len/2); |
337 |
++it; //protocol identifier |
++it; //protocol identifier |
338 |
++it; //Data encoding |
++it; //Data encoding |
339 |
|
|
340 |
result.timestamp = DecodeTimestamp(it, it+7); |
string timestamp = DecodeTimestamp(it, it+7); |
341 |
it += 7; |
it += 7; |
342 |
|
|
343 |
|
|
344 |
unsigned char data_len = (*it++); |
unsigned char data_len = (*it++); |
345 |
|
|
346 |
|
|
347 |
|
SmsPart part; |
348 |
|
part.group = -1; |
349 |
|
|
350 |
int shift_start = 0; |
int shift_start = 0; |
351 |
|
|
352 |
if (UDHI) |
if (UDHI) |
353 |
{ |
{ |
354 |
int udh_len = (*it++); |
int udh_len = (*it++); |
355 |
it += udh_len; //just ignore the User Data Header |
|
356 |
|
vector<unsigned char> udh; |
357 |
|
for (int i=0; i<udh_len; i++) |
358 |
|
{ |
359 |
|
udh.push_back (*it++); |
360 |
|
} |
361 |
|
ParseUdh(udh,part); |
362 |
|
|
363 |
data_len -= udh_len; |
data_len -= udh_len; |
364 |
|
|
365 |
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 |
369 |
vector<unsigned char> user_data; |
vector<unsigned char> user_data; |
370 |
user_data.insert(user_data.end(), it, it+data_len); |
user_data.insert(user_data.end(), it, it+data_len); |
371 |
|
|
372 |
result.message = Decode8to7bit(user_data, shift_start).substr(0,data_len); |
string message = Decode8to7bit(user_data, shift_start).substr(0,data_len); |
373 |
|
|
374 |
|
message = Util::str_trim(message); |
375 |
|
|
|
result.message = Util::str_trim(result.message); |
|
376 |
|
|
377 |
|
part.message = message; |
378 |
|
part.sender = sender; |
379 |
|
|
380 |
return result; |
return part; |
381 |
} |
} |
382 |
|
|
383 |
} |
} |