/* using http://sourceforge.net/projects/libserial/ */ #include "SmsPdu.h" #include #include #include #include #include "util.h" using namespace std; namespace SmsPdu { vector BcdEncode(string input) { char buf[2] = " "; vector result; unsigned char current; for (unsigned int i=0; i vec) { ostringstream os; for (unsigned int i=0; i(vec.at(i)); } return os.str(); } std::string Encode8to7bit(vector vec) { string result; return result; } vector Encode7to8bit(std::string str) { vector buf; int shift = 0; for (unsigned int i=0; i>= shift; next <<= (7-shift); unsigned char byte = current | next; buf.push_back(byte); if (shift == 6) i++; shift = (shift+1) % 7; } return buf; } vector CreateSmsPdu(string to, string message, bool allowMultipart) { bool multipart = allowMultipart && message.length() > 160; const unsigned char UDHI = multipart ? 0x40 : 0; srand(time(0)); unsigned char csms_ref = rand() % 128; int part_count; const int PDU_LEN = 153; if (multipart) { part_count = message.length() / PDU_LEN; if (message.length() % PDU_LEN) part_count++; } else { part_count = 1; } vector result; for (int partnr = 0; partnr < part_count; ++partnr) { vector pdu; pdu.push_back(0x00); // use SMSC from phone pdu.push_back( 0x01|UDHI ); // 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 vector phone = BcdEncode(to); pdu.insert( pdu.end(), phone.begin(), phone.end()); pdu.push_back(0x00); // Protocol identifier pdu.push_back(0x00); // Data coding scheme string message_part; if (multipart) { message_part = message.substr(0, PDU_LEN); message.erase(0, PDU_LEN); pdu.push_back( message_part.length()+ 7 ); //UserDataLength pdu.push_back( 0x06 ); // UDH Len pdu.push_back( 0x00 ); // UDH Element Identifier pdu.push_back( 0x03 ); // UDH element length pdu.push_back( csms_ref ); // csms_ref reference pdu.push_back( part_count ); pdu.push_back( partnr+1 ); pdu.push_back( 0x00); } else { message_part = message.substr(0,160); //truncate to 160 pdu.push_back( message_part.length() ); //UserDataLength } vector userData = Encode7to8bit(message_part); pdu.insert( pdu.end(), userData.begin(), userData.end()); PduInfo info; info.len = pdu.size()-1; info.pdu = HexformatVector(pdu); result.push_back(info); } return result; } }