/* * Copyright 2008, 2009, 2010 Free Software Foundation, Inc. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion. * * This use of this software may be subject to additional restrictions. * See the LEGAL file in the main directory for details. */ #include #include #include #include "SMSMessages.h" #include using namespace std; using namespace GSM; using namespace SMS; ostream& SMS::operator<<(ostream& os, CPMessage::MessageType val) { switch(val) { case CPMessage::DATA: os<<"CP-DATA"; break; case CPMessage::ACK: os<<"CP-ACK"; break; case CPMessage::ERROR: os<<"CP-ERROR"; break; default : os<parse(RPDU); LOG(DEBUG) << "SMS RP-DATA " << *rp_data; } catch (SMSReadError) { LOG(WARNING) << "SMS parsing failed (above L3)"; // TODO:: send error back to the phone delete rp_data; rp_data = NULL; } catch (L3ReadError) { LOG(WARNING) << "SMS parsing failed (in L3)"; // TODO:: send error back to the phone delete rp_data; rp_data = NULL; } return rp_data; } TLMessage *SMS::parseTPDU(const TLFrame& TPDU) { LOG(DEBUG) << "SMS: parseTPDU MTI=" << TPDU.MTI(); // Handle just the uplink cases. switch ((TLMessage::MessageType)TPDU.MTI()) { case TLMessage::DELIVER_REPORT: case TLMessage::STATUS_REPORT: // FIXME -- Not implemented yet. LOG(WARNING) << "Unsupported TPDU type: " << (TLMessage::MessageType)TPDU.MTI(); return NULL; case TLMessage::SUBMIT: { TLSubmit *submit = new TLSubmit; submit->parse(TPDU); LOG(INFO) << "SMS SMS-SUBMIT " << *submit; return submit; } default: return NULL; } } void CPMessage::text(ostream& os) const { os << (CPMessage::MessageType)MTI(); os <<" TI=" << mTI; } void CPMessage::write(L3Frame& dest) const { // We override L3Message::write for the transaction identifier. dest.resize(bitsNeeded()); size_t wp = 0; // Note that 1/2-octet fields are reversed relative to Table 7.1. dest.writeField(wp,mTI,4); dest.writeField(wp,PD(),4); dest.writeField(wp,MTI(),8); writeBody(dest, wp); } void CPData::parseBody( const L3Frame& src, size_t &rp ) { mData.parseLV(src,rp); } void CPData::writeBody( L3Frame& dest, size_t &wp ) const { mData.writeLV(dest,wp); } void CPData::text(ostream& os) const { CPMessage::text(os); os << " RPDU=(" << mData << ")"; } void CPError::writeBody( L3Frame& dest, size_t &wp ) const { mCause.writeV(dest,wp); } void CPUserData::parseV(const L3Frame& src, size_t &rp, size_t expectedLength) { unsigned numBits = expectedLength*8; mRPDU.resize(numBits); src.segmentCopyTo(mRPDU,rp,numBits); rp += numBits; } void CPUserData::writeV(L3Frame& dest, size_t &wp) const { unsigned numBits = mRPDU.size(); mRPDU.copyToSegment(dest,wp,numBits); wp += numBits; } ostream& SMS::operator<<(ostream& os, RPMessage::MessageType val) { switch(val) { case RPMessage::Data: os<<"RP-DATA"; break; case RPMessage::Ack: os<<"RP-ACK"; break; case RPMessage::Error: os<<"RP-ERROR"; break; case RPMessage::SMMA: os<<"RP-SMMA"; break; default : os<255) vp=255; dest.writeField(wp,vp,8); } void TLValidityPeriod::text(ostream& os) const { char str[27]; time_t seconds = mExpiration.sec(); ctime_r(&seconds,str); str[24]='\0'; os << "expiration=(" << str << ")"; } void TLUserData::encode7bit(const char *text) { size_t wp = 0; // 1. Prepare. // Default alphabet (7-bit) mDCS = 0; // With 7-bit encoding TP-User-Data-Length count septets, i.e. just number // of characters. mLength = strlen(text); int bytes = (mLength*7+7)/8; int filler_bits = bytes*8-mLength*7; mRawData.resize(bytes*8); // 2. Write TP-UD // This tail() works because UD is always the last field in the PDU. BitVector chars = mRawData.tail(wp); for (unsigned i=0; i (mRawData.size())) { LOG(NOTICE) << "badly formatted TL-UD"; SMS_READ_ERROR; } size_t crp = 0; unsigned text_length = mLength; // Skip User-Data-Header. We don't decode it here. // User-Data-Header handling is described in GSM 03.40 9.2.3.24 // and is pictured in GSM 03.40 Figure 9.2.3.24 (a) if (mUDHI) { // Length-of-User-Data-Header unsigned udhl = mRawData.peekFieldReversed(crp,8); // Calculate UDH length in septets, including fill bits. unsigned udh_septets = (udhl*8 + 8 + 6) / 7; // Adjust actual text position and length. crp += udh_septets * 7; text_length -= udh_septets; LOG(DEBUG) << "UDHL(octets)=" << udhl << " UDHL(septets)=" << udh_septets << " pointer(bits)=" << crp << " text_length(septets)=" << text_length; } // Do decoding text.resize(text_length); for (unsigned i=0; i (src.size()-rp)) { LOG(NOTICE) << "badly formatted TL-UD"; SMS_READ_ERROR; } BitVector chars(src.tail(rp)); chars.LSB8MSB(); size_t crp=0; for (unsigned i=0; i