--- smsdaemon/SmsPdu.cpp 2008/06/11 21:18:04 60 +++ smsdaemon/SmsPdu.cpp 2008/06/12 18:54:24 68 @@ -10,7 +10,7 @@ #include #include - +#include "common.h" #include "util.h" @@ -21,31 +21,38 @@ { -vector BcdEncode(string input) -{ - char buf[2] = " "; - vector result; - unsigned char current; - for (unsigned int i=0; i vec) { ostringstream os; @@ -61,6 +68,38 @@ return os.str(); } +std::string Decode8to7bit(vector input) +{ + string result; + + int shift = 0; + for (unsigned int i=0; i0) ? input.at(i-1) : 0; + + current <<= shift; + + prev >>= (8-shift); + + unsigned char byte = current | prev; + byte &= 0x7F; + + result.append(1, byte); + + if (shift == 6) + result.append(1, input.at(i) >> 1); + + shift = (shift+1) % 7; + + } + + return result; +} + + + + vector Encode7to8bit(std::string str) { vector buf; @@ -92,16 +131,25 @@ { bool multipart = allowMultipart && message.length() > 160; - const unsigned char UHDI = multipart ? 0x40 : 0; + const unsigned char UDHI = multipart ? 0x40 : 0; srand(time(0)); - unsigned char csms_ref = rand() % 256; + unsigned char csms_ref = rand() % 128; int part_count; + + const int PDU_LEN = 153; + if (multipart) { - part_count = message.length() / 153; - if (message.length() % 153) + if (message.length() > 800) + { + Common::instance()->logMessage("Trying to send multipart sms > 800 bytes !!!"); + message = message.substr(0,800); + } + + part_count = message.length() / PDU_LEN; + if (message.length() % PDU_LEN) part_count++; } else @@ -115,34 +163,45 @@ vector pdu; pdu.push_back(0x00); // use SMSC from phone - pdu.push_back( 0x01|UHDI ); // first octet -- no timeout + 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 + pdu.push_back(0x81); // type of address (international nr + ISDN/telephone range) - else try 0x81 - vector phone = BcdEncode(to); + + string phone = EncodePhonenr(to); pdu.insert( pdu.end(), phone.begin(), phone.end()); pdu.push_back(0x00); // Protocol identifier - pdu.push_back(0x00); // Data coding scheme + pdu.push_back(0x00); // Data coding scheme string message_part; if (multipart) { - message_part = message.substr(0,153); - message.erase(0,153); + message_part = message.substr(0, PDU_LEN); + message.erase(0, PDU_LEN); - pdu.push_back( message_part.length()+5 ); //UserDataLength - pdu.push_back( 0x00 ); // UDH[0] - pdu.push_back( 0x03 ); // UDH[1] = UDH_LEN - pdu.push_back( csms_ref ); //UDH[2] + 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 ); + pdu.push_back( partnr+1 ); + pdu.push_back( 0x00); } else { - message_part = message.substr(0,160); //truncate to 160 + if (message.length() > 160) + { + message_part = message.substr(0,160); //truncate to 160 + Common::instance()->logMessage("Truncated message"); + } + else + { + message_part = message; + } pdu.push_back( message_part.length() ); //UserDataLength }