1 |
torben |
59 |
/* using http://sourceforge.net/projects/libserial/ |
2 |
|
|
*/ |
3 |
|
|
|
4 |
|
|
|
5 |
torben |
60 |
#include "SmsPdu.h" |
6 |
|
|
|
7 |
torben |
59 |
#include <string> |
8 |
|
|
#include <sstream> |
9 |
|
|
|
10 |
torben |
60 |
#include <time.h> |
11 |
|
|
#include <stdlib.h> |
12 |
torben |
59 |
|
13 |
torben |
63 |
#include "common.h" |
14 |
torben |
59 |
#include "util.h" |
15 |
|
|
|
16 |
|
|
|
17 |
|
|
using namespace std; |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
namespace SmsPdu |
21 |
|
|
{ |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
vector<unsigned char> BcdEncode(string input) |
25 |
|
|
{ |
26 |
|
|
char buf[2] = " "; |
27 |
|
|
vector<unsigned char> result; |
28 |
|
|
|
29 |
|
|
unsigned char current; |
30 |
|
|
for (unsigned int i=0; i<input.length(); ++i) |
31 |
|
|
{ |
32 |
|
|
buf[0] = input.at(i); |
33 |
|
|
unsigned char tmp = atoi(buf); |
34 |
|
|
|
35 |
|
|
if ( (i%2) == 0) |
36 |
|
|
{ |
37 |
|
|
current = tmp; |
38 |
|
|
} |
39 |
|
|
else |
40 |
|
|
{ |
41 |
|
|
current |= (tmp<<4); |
42 |
|
|
result.push_back(current); |
43 |
|
|
} |
44 |
|
|
} |
45 |
|
|
|
46 |
torben |
61 |
if ((input.length() % 2) == 1) |
47 |
|
|
{ |
48 |
|
|
current |= 0xF0; |
49 |
|
|
result.push_back(current); |
50 |
|
|
} |
51 |
|
|
|
52 |
torben |
59 |
return result; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
string HexformatVector(vector<unsigned char> vec) |
56 |
|
|
{ |
57 |
|
|
ostringstream os; |
58 |
|
|
|
59 |
|
|
for (unsigned int i=0; i<vec.size(); ++i) |
60 |
|
|
{ |
61 |
|
|
os.width(2); |
62 |
|
|
os.fill('0'); |
63 |
|
|
os << hex << uppercase << static_cast<unsigned int>(vec.at(i)); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
return os.str(); |
68 |
|
|
} |
69 |
|
|
|
70 |
torben |
61 |
std::string Encode8to7bit(vector<unsigned char> vec) |
71 |
|
|
{ |
72 |
|
|
string result; |
73 |
|
|
|
74 |
|
|
return result; |
75 |
|
|
} |
76 |
|
|
|
77 |
torben |
59 |
vector<unsigned char> Encode7to8bit(std::string str) |
78 |
|
|
{ |
79 |
|
|
vector<unsigned char> buf; |
80 |
|
|
|
81 |
|
|
int shift = 0; |
82 |
|
|
for (unsigned int i=0; i<str.size(); ++i) |
83 |
|
|
{ |
84 |
|
|
unsigned char current = str.at(i) & 0x7F; |
85 |
|
|
unsigned char next = ( (i+1)<str.size()) ? str.at(i+1) : 0; |
86 |
|
|
next &= 0x7F; |
87 |
|
|
|
88 |
|
|
current >>= shift; |
89 |
|
|
next <<= (7-shift); |
90 |
|
|
|
91 |
|
|
unsigned char byte = current | next; |
92 |
|
|
buf.push_back(byte); |
93 |
|
|
|
94 |
|
|
if (shift == 6) |
95 |
|
|
i++; |
96 |
|
|
|
97 |
|
|
shift = (shift+1) % 7; |
98 |
|
|
|
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
return buf; |
102 |
|
|
} |
103 |
|
|
|
104 |
torben |
60 |
vector<PduInfo> CreateSmsPdu(string to, string message, bool allowMultipart) |
105 |
torben |
59 |
{ |
106 |
torben |
60 |
bool multipart = allowMultipart && message.length() > 160; |
107 |
torben |
59 |
|
108 |
torben |
61 |
const unsigned char UDHI = multipart ? 0x40 : 0; |
109 |
torben |
59 |
|
110 |
torben |
60 |
srand(time(0)); |
111 |
torben |
61 |
unsigned char csms_ref = rand() % 128; |
112 |
torben |
59 |
|
113 |
torben |
60 |
int part_count; |
114 |
torben |
61 |
|
115 |
|
|
const int PDU_LEN = 153; |
116 |
|
|
|
117 |
torben |
60 |
if (multipart) |
118 |
|
|
{ |
119 |
torben |
65 |
if (message.length() > 800) |
120 |
|
|
{ |
121 |
|
|
Common::instance()->logMessage("Trying to send multipart sms > 800 bytes !!!"); |
122 |
|
|
message = message.substr(0,800); |
123 |
|
|
} |
124 |
|
|
|
125 |
torben |
61 |
part_count = message.length() / PDU_LEN; |
126 |
|
|
if (message.length() % PDU_LEN) |
127 |
torben |
60 |
part_count++; |
128 |
|
|
} |
129 |
|
|
else |
130 |
|
|
{ |
131 |
|
|
part_count = 1; |
132 |
|
|
} |
133 |
torben |
59 |
|
134 |
torben |
60 |
vector<PduInfo> result; |
135 |
|
|
for (int partnr = 0; partnr < part_count; ++partnr) |
136 |
|
|
{ |
137 |
|
|
vector<unsigned char> pdu; |
138 |
torben |
59 |
|
139 |
torben |
60 |
pdu.push_back(0x00); // use SMSC from phone |
140 |
torben |
61 |
pdu.push_back( 0x01|UDHI ); // first octet -- no timeout |
141 |
torben |
60 |
pdu.push_back(0x00); // TP-MR message reference |
142 |
|
|
pdu.push_back(to.length() ); //length of phone nr |
143 |
torben |
66 |
pdu.push_back(0x81); // type of address (international nr + ISDN/telephone range) - else try 0x81 |
144 |
torben |
59 |
|
145 |
torben |
60 |
vector<unsigned char> phone = BcdEncode(to); |
146 |
|
|
pdu.insert( pdu.end(), phone.begin(), phone.end()); |
147 |
|
|
|
148 |
|
|
pdu.push_back(0x00); // Protocol identifier |
149 |
torben |
61 |
pdu.push_back(0x00); // Data coding scheme |
150 |
torben |
60 |
|
151 |
|
|
string message_part; |
152 |
|
|
if (multipart) |
153 |
|
|
{ |
154 |
torben |
61 |
message_part = message.substr(0, PDU_LEN); |
155 |
|
|
message.erase(0, PDU_LEN); |
156 |
torben |
60 |
|
157 |
torben |
62 |
pdu.push_back( message_part.length()+ 7 ); //UserDataLength |
158 |
|
|
pdu.push_back( 0x06 ); // UDH Len |
159 |
torben |
61 |
pdu.push_back( 0x00 ); // UDH Element Identifier |
160 |
|
|
pdu.push_back( 0x03 ); // UDH element length |
161 |
|
|
pdu.push_back( csms_ref ); // csms_ref reference |
162 |
torben |
60 |
pdu.push_back( part_count ); |
163 |
torben |
61 |
pdu.push_back( partnr+1 ); |
164 |
torben |
62 |
pdu.push_back( 0x00); |
165 |
torben |
60 |
|
166 |
|
|
} |
167 |
|
|
else |
168 |
|
|
{ |
169 |
torben |
63 |
if (message.length() > 160) |
170 |
|
|
{ |
171 |
|
|
message_part = message.substr(0,160); //truncate to 160 |
172 |
|
|
Common::instance()->logMessage("Truncated message"); |
173 |
|
|
} |
174 |
|
|
else |
175 |
|
|
{ |
176 |
|
|
message_part = message; |
177 |
|
|
} |
178 |
torben |
60 |
|
179 |
|
|
pdu.push_back( message_part.length() ); //UserDataLength |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
vector<unsigned char> userData = Encode7to8bit(message_part); |
183 |
|
|
|
184 |
|
|
pdu.insert( pdu.end(), userData.begin(), userData.end()); |
185 |
|
|
|
186 |
|
|
PduInfo info; |
187 |
|
|
info.len = pdu.size()-1; |
188 |
|
|
info.pdu = HexformatVector(pdu); |
189 |
|
|
result.push_back(info); |
190 |
|
|
|
191 |
|
|
} |
192 |
|
|
return result; |
193 |
torben |
59 |
} |
194 |
|
|
|
195 |
|
|
} |