1 |
/* using http://sourceforge.net/projects/libserial/ |
2 |
*/ |
3 |
|
4 |
|
5 |
#include <string> |
6 |
#include <sstream> |
7 |
|
8 |
|
9 |
|
10 |
|
11 |
#include "util.h" |
12 |
|
13 |
|
14 |
using namespace std; |
15 |
|
16 |
|
17 |
namespace SmsPdu |
18 |
{ |
19 |
|
20 |
|
21 |
vector<unsigned char> BcdEncode(string input) |
22 |
{ |
23 |
char buf[2] = " "; |
24 |
vector<unsigned char> result; |
25 |
|
26 |
unsigned char current; |
27 |
for (unsigned int i=0; i<input.length(); ++i) |
28 |
{ |
29 |
buf[0] = input.at(i); |
30 |
unsigned char tmp = atoi(buf); |
31 |
|
32 |
if ( (i%2) == 0) |
33 |
{ |
34 |
current = tmp; |
35 |
} |
36 |
else |
37 |
{ |
38 |
current |= (tmp<<4); |
39 |
result.push_back(current); |
40 |
} |
41 |
} |
42 |
|
43 |
return result; |
44 |
} |
45 |
|
46 |
string HexformatVector(vector<unsigned char> vec) |
47 |
{ |
48 |
ostringstream os; |
49 |
|
50 |
for (unsigned int i=0; i<vec.size(); ++i) |
51 |
{ |
52 |
os.width(2); |
53 |
os.fill('0'); |
54 |
os << hex << uppercase << static_cast<unsigned int>(vec.at(i)); |
55 |
} |
56 |
|
57 |
|
58 |
return os.str(); |
59 |
} |
60 |
|
61 |
vector<unsigned char> Encode7to8bit(std::string str) |
62 |
{ |
63 |
vector<unsigned char> buf; |
64 |
|
65 |
int shift = 0; |
66 |
for (unsigned int i=0; i<str.size(); ++i) |
67 |
{ |
68 |
unsigned char current = str.at(i) & 0x7F; |
69 |
unsigned char next = ( (i+1)<str.size()) ? str.at(i+1) : 0; |
70 |
next &= 0x7F; |
71 |
|
72 |
current >>= shift; |
73 |
next <<= (7-shift); |
74 |
|
75 |
unsigned char byte = current | next; |
76 |
buf.push_back(byte); |
77 |
|
78 |
if (shift == 6) |
79 |
i++; |
80 |
|
81 |
shift = (shift+1) % 7; |
82 |
|
83 |
} |
84 |
|
85 |
return buf; |
86 |
} |
87 |
|
88 |
string CreateSmsPdu(string to, string message, int& len) |
89 |
{ |
90 |
message = message.substr(0,160); //truncate to 160 |
91 |
|
92 |
vector<unsigned char> pdu; |
93 |
|
94 |
pdu.push_back(0x00); // use SMSC from phone |
95 |
pdu.push_back(0x01); // first octet -- no timeout |
96 |
pdu.push_back(0x00); // TP-MR message reference |
97 |
pdu.push_back(to.length() ); //length of phone nr |
98 |
pdu.push_back(0x91); // type of address (international nr + ISDN/telephone range) - else try 0x81 |
99 |
|
100 |
vector<unsigned char> phone = BcdEncode(to); |
101 |
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
102 |
|
103 |
pdu.push_back(0x00); // Protocol identifier |
104 |
pdu.push_back(0x00); // Data coding scheme |
105 |
pdu.push_back( message.length() ); //UserDataLength |
106 |
|
107 |
vector<unsigned char> userData = Encode7to8bit(message); |
108 |
pdu.insert( pdu.end(), userData.begin(), userData.end()); |
109 |
|
110 |
len = pdu.size()-1; |
111 |
return HexformatVector(pdu); |
112 |
} |
113 |
|
114 |
} |