/[projects]/smsdaemon/SmsPdu.cpp
ViewVC logotype

Diff of /smsdaemon/SmsPdu.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 69 by torben, Fri Jun 13 06:38:43 2008 UTC revision 142 by torben, Sun Dec 7 15:51:02 2008 UTC
# Line 13  Line 13 
13  #include "common.h"  #include "common.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    
# Line 36  namespace SmsPdu Line 34  namespace SmsPdu
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);
# Line 78  namespace SmsPdu Line 79  namespace SmsPdu
79                  return vec;                  return vec;
80          }          }
81    
82          std::string Decode8to7bit(vector<unsigned char> input)          std::string Decode8to7bit(vector<unsigned char> input, int shift_start)
83          {          {
84                  string result;                  string result;
85    
86                  int shift = 0;                  int shift = shift_start;
87                  for (unsigned int i=0; i<input.size(); ++i)                  for (unsigned int i=0; i<input.size(); ++i)
88                  {                  {
89                          unsigned char current = input.at(i);                          unsigned char current = input.at(i);
# Line 110  namespace SmsPdu Line 111  namespace SmsPdu
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;
# Line 143  namespace SmsPdu Line 144  namespace SmsPdu
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;
# Line 178  namespace SmsPdu Line 178  namespace SmsPdu
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
# Line 199  namespace SmsPdu Line 200  namespace SmsPdu
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                          {                          {
# Line 216  namespace SmsPdu Line 217  namespace SmsPdu
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    
# Line 275  namespace SmsPdu Line 276  namespace SmsPdu
276    
277          SMS ParseSmsPdu(std::string pdu_str)          SMS ParseSmsPdu(std::string pdu_str)
278          {          {
                 SMS result;  
279    
280                  vector<unsigned char> pdu = HexDecodeString(pdu_str);                  vector<unsigned char> pdu = HexDecodeString(pdu_str);
281    
# Line 284  namespace SmsPdu Line 284  namespace SmsPdu
284                  it += (*it++); // Skip smsc info                  it += (*it++); // Skip smsc info
285    
286                  unsigned char deliver_first_octet = (*it++);                  unsigned char deliver_first_octet = (*it++);
287    
288                    bool UDHI = (deliver_first_octet & 0x40) > 0;
289    
290                  unsigned char sender_len = (*it++);                  unsigned char sender_len = (*it++);
291                  if ( (sender_len % 2) == 1)                  if ( (sender_len % 2) == 1)
292                          sender_len++;                          sender_len++;
293    
294                  ++it; //ignore Type-Of-Address                  ++it; //ignore Type-Of-Address
295    
296                  result.sender = DecodeRawPhonenr( it, it+(sender_len/2) );                  string sender = DecodeRawPhonenr( it, it+(sender_len/2) );
297    
298                  it += (sender_len/2);                  it += (sender_len/2);
299                  ++it; //protocol identifier                  ++it; //protocol identifier
300                  ++it; //Data encoding                  ++it; //Data encoding
301    
302                  result.timestamp = DecodeTimestamp(it, it+7);                  string timestamp = DecodeTimestamp(it, it+7);
303                  it += 7;                  it += 7;
304    
305    
306                  unsigned char data_len = (*it++);                  unsigned char data_len = (*it++);
307    
308                    int shift_start = 0;
309                    if (UDHI)
310                    {
311                            int udh_len = (*it++);
312                            it += udh_len; //just ignore the User Data Header
313                            data_len -= udh_len;
314    
315                            shift_start = udh_len+1; //make the 8to7bit decode start with the right shift level
316                    }
317    
318    
319                  vector<unsigned char> user_data;                  vector<unsigned char> user_data;
320                  user_data.insert(user_data.end(), it, it+data_len);                  user_data.insert(user_data.end(), it, it+data_len);
321                                    
322                  result.message = Decode8to7bit(user_data);                  string message = Decode8to7bit(user_data, shift_start).substr(0,data_len);
323    
324                    message = Util::str_trim(message);
325                                    
326                    SMS result;
327                    result.SetMessage(message);
328                    result.SetSender(sender);
329    
330                  return result;                  return result;
331          }          }

Legend:
Removed from v.69  
changed lines
  Added in v.142

  ViewVC Help
Powered by ViewVC 1.1.20