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

Diff of /smsdaemon/SmsPdu.cpp

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

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

Legend:
Removed from v.60  
changed lines
  Added in v.176

  ViewVC Help
Powered by ViewVC 1.1.20