kr_sms_trigger_epoc.cpp

Go to the documentation of this file.
00001 /*
00002  !concept {:name => "Catching SMS control messages",
00003    :desc => "Quietly observing and processing SMS control messages on Symbian."}
00004 */
00005 
00006 /*
00007   References:
00008   http://wiki.forum.nokia.com/index.php/SMS_Utilities_API
00009 */
00010 
00011 #include "kr_sms_trigger_epoc.hpp"
00012 
00013 #include "rk_remokon.h"
00014 
00015 #include <smsustrm.h> // read stream
00016 
00017 CTOR_IMPL_CSmsTrigger;
00018 
00019 _LIT8(KControlTag, "##cl");
00020 
00021 void CSmsTrigger::ConstructL()
00022 {
00023   LEAVE_IF_ERROR_OR_SET_SESSION_OPEN(iSocketServ, iSocketServ.Connect());
00024   LEAVE_IF_ERROR_OR_SET_SESSION_OPEN(iSocket, 
00025              iSocket.Open(iSocketServ, 
00026               KSMSAddrFamily, 
00027               KSockDatagram, 
00028               KSMSDatagramProtocol));
00029   iSmsAddr.SetSmsAddrFamily(ESmsAddrMatchText); 
00030   iSmsAddr.SetTextMatch(KControlTag);
00031   User::LeaveIfError(iSocket.Bind(iSmsAddr));
00032 
00033   MakeRequest();
00034 }
00035 
00036 CSmsTrigger::~CSmsTrigger()
00037 {
00038   Cancel();
00039   SESSION_CLOSE_IF_OPEN(iSocket);
00040   SESSION_CLOSE_IF_OPEN(iSocketServ);
00041 }
00042 
00043 void CSmsTrigger::RunL()
00044 {
00045   TInt errCode = iStatus.Int();
00046   if (errCode) {
00047     RunError(errCode);
00048     return;
00049   }
00050 
00051   if (!iRead) {
00052     MakeRequest(); // next read
00053     return;
00054   }
00055 
00056   CSmsBuffer *smsBuffer = CSmsBuffer::NewL();
00057   CleanupStack::PushL(smsBuffer);
00058   // see enum TSmsPDUType
00059   CSmsMessage* message = CSmsMessage::NewL(iFs, CSmsPDU::ESmsDeliver, 
00060              smsBuffer);
00061   CleanupStack::Pop(); // smsBuffer, ownership taken by 'message'
00062   CleanupStack::PushL(message);
00063   {
00064     RSmsSocketReadStream readStream(iSocket);
00065     CleanupClosePushL(readStream);
00066     message->InternalizeL(readStream);
00067     CleanupStack::PopAndDestroy(); // readStream
00068   }
00069   TInt bufLen = smsBuffer->Length(); // number of characters
00070   RBuf des16;
00071   des16.CreateL(bufLen);
00072   des16.CleanupClosePushL();
00073   smsBuffer->Extract(des16, 0, bufLen);
00074 
00075 #if __DO_LOGGING__
00076   {
00077     HBufC8* des8 = ConvToUtf8ZL(des16);
00078     CleanupStack::PushL(des8);
00079     const char* str = (const char*)(des8->Ptr());
00080     logg("control message: '%s' (len %d)", str, bufLen);
00081     CleanupStack::PopAndDestroy(); // des8
00082   }
00083 #endif
00084 
00085   TLex lex(des16);
00086   lex.Inc(KControlTag().Length());
00087   TInt16 secs = 0;
00088   TInt parErr = lex.Val(secs);
00089   if (!parErr && (secs > 0)) {
00090     logg("control message: parsed to %d secs", secs);
00091     rk_Remokon* remokon = ac_get_Remokon(iAppContext);
00092     if (remokon) {
00093       GError* localError = NULL;
00094       if (!rk_Remokon_start_timed(remokon, secs, &localError)) {
00095   er_log_gerror(er_FREE, localError,
00096           "sms trigger: error starting remokon");
00097       }
00098     }
00099   }
00100 
00101   CleanupStack::PopAndDestroy(2); // des16, message
00102 
00103   // Report success.
00104   {
00105     // Third arg ignored?
00106     iSocket.Ioctl(KIoctlReadMessageSucceeded, iStatus, &iPckgBuf, KSolSmsProv);
00107     iRead = EFalse;
00108     SetActive();
00109   }
00110 }
00111 
00112 void CSmsTrigger::DoCancel()
00113 {
00114   iSocket.CancelIoctl();
00115 }
00116 
00117 // Leave or other error in RunL. This inactivates this AO as we do not
00118 // make further requests.
00119 TInt CSmsTrigger::RunError(TInt errCode)
00120 {
00121   er_log_symbian(0, errCode, "INACTIVATE: error in SMS trigger handling");
00122   return 0;
00123 }
00124 
00125 void CSmsTrigger::MakeRequest()
00126 {
00127   if (IsActive())
00128     Cancel();
00129   iPckgBuf() = KSockSelectRead;
00130   iSocket.Ioctl(KIOctlSelect, iStatus, &iPckgBuf, KSOLSocket);
00131   iRead = ETrue;
00132   SetActive();
00133 }
00134 
00135 /**
00136 
00137 Copyright 2011 Helsinki Institute for Information Technology (HIIT)
00138 and the authors. All rights reserved.
00139 
00140 Authors: Tero Hasu <tero.hasu@hut.fi>
00141 
00142 Permission is hereby granted, free of charge, to any person
00143 obtaining a copy of this software and associated documentation files
00144 (the "Software"), to deal in the Software without restriction,
00145 including without limitation the rights to use, copy, modify, merge,
00146 publish, distribute, sublicense, and/or sell copies of the Software,
00147 and to permit persons to whom the Software is furnished to do so,
00148 subject to the following conditions:
00149 
00150 The above copyright notice and this permission notice shall be
00151 included in all copies or substantial portions of the Software.
00152 
00153 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00154 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00155 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00156 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00157 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00158 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00159 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00160 SOFTWARE.
00161 
00162  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:54 2011 by Doxygen 1.6.1