utilities/serviceipcclient/platform/s60/serviceipcsymbian.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /**
       
     2    This file is part of CWRT package **
       
     3 
       
     4    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). **
       
     5 
       
     6    This program is free software: you can redistribute it and/or modify
       
     7    it under the terms of the GNU (Lesser) General Public License as
       
     8    published by the Free Software Foundation, version 2.1 of the License.
       
     9    This program is distributed in the hope that it will be useful, but
       
    10    WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
       
    12    (Lesser) General Public License for more details. You should have
       
    13    received a copy of the GNU (Lesser) General Public License along
       
    14    with this program. If not, see <http://www.gnu.org/licenses/>.
       
    15 */
       
    16 
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <e32cmn.h>
       
    20 #include "serviceipcsymbian_p.h"
       
    21 
       
    22 namespace WRT
       
    23 {
       
    24 const TInt KIPCOperation = 0;
       
    25 const TInt KIPCGetBuffer = 1;
       
    26 const TInt KServerMajorVersionNumber = 1;
       
    27 const TInt KServerMinorVersionNumber = 0;
       
    28 const TInt KServerBuildVersionNumber = 0;
       
    29 
       
    30 
       
    31 /*!
       
    32  \class CServiceSymbianIPC
       
    33 
       
    34  Symbian Client backend for the service IPC
       
    35  */
       
    36 
       
    37 /*!
       
    38  Constructor
       
    39  */
       
    40 CServiceSymbianIPC::CServiceSymbianIPC() :
       
    41     CActive(CActive::EPriorityStandard), iDataSize(0)
       
    42 {
       
    43     CActiveScheduler::Add(this);
       
    44 }
       
    45 
       
    46 /*!
       
    47  Destructor 
       
    48  */
       
    49 CServiceSymbianIPC::~CServiceSymbianIPC()
       
    50 {
       
    51     Cancel();
       
    52     if (iSession.Handle()) {
       
    53         iSession.Close();
       
    54     }
       
    55     delete iAsyncData;
       
    56     delete iRequestData;
       
    57 }
       
    58 
       
    59 /*!
       
    60  2nd phased constructor
       
    61  */
       
    62 void CServiceSymbianIPC::ConstructL()
       
    63 {
       
    64 }
       
    65 
       
    66 /*!
       
    67  Two Phased Constructor
       
    68  */
       
    69 CServiceSymbianIPC* CServiceSymbianIPC::NewL()
       
    70 {
       
    71     CServiceSymbianIPC* self = new (ELeave) CServiceSymbianIPC();
       
    72     CleanupStack::PushL(self);
       
    73     self->ConstructL();
       
    74     CleanupStack::Pop(self);
       
    75     return self;
       
    76 }
       
    77 
       
    78 /*!
       
    79  Connect to the server
       
    80  @param aServerName name of the server to connect to
       
    81  @return true if connected, false if not
       
    82  */
       
    83 bool CServiceSymbianIPC::connect(const QString& aServerName)
       
    84 {
       
    85     // Version informaton
       
    86     TVersion version(KServerMajorVersionNumber, 
       
    87                      KServerMinorVersionNumber, 
       
    88                      KServerBuildVersionNumber);
       
    89     TPtrC serverName(reinterpret_cast<const TUint16*> (aServerName.utf16()));
       
    90     TInt err = iSession.Connect(serverName, version);
       
    91 
       
    92     return (err == KErrNone);
       
    93 }
       
    94 
       
    95 /*!
       
    96  Disconnect from the server
       
    97  */
       
    98 void CServiceSymbianIPC::disconnect()
       
    99 {
       
   100     iSession.Close();
       
   101 }
       
   102 
       
   103 /*!
       
   104  Starts the service
       
   105  @param aServerName server name
       
   106  @param aExeName server executable name
       
   107  */
       
   108 bool CServiceSymbianIPC::startServer(const QString& /*aServerName*/,
       
   109                                      const QString& aExeName)
       
   110 {
       
   111     TPtrC serverName(reinterpret_cast<const TUint16*> (aExeName.utf16()));
       
   112     TInt err = iSession.StartServer(serverName);
       
   113     return (err == KErrNone);
       
   114 }
       
   115 
       
   116 /*!
       
   117  Send a request synchronously
       
   118  @param aRequestType type of request to send to the server
       
   119  @param aData data to send to the server
       
   120  */
       
   121 bool CServiceSymbianIPC::sendSync(const QString& aRequestType,
       
   122                                   const QByteArray& aData)
       
   123 {
       
   124     // Convert from QString to TPtr
       
   125     TPtrC request(reinterpret_cast<const TUint16*> (aRequestType.utf16()));
       
   126     TPtrC8 data(reinterpret_cast<const TUint8*> (aData.constData()), aData.length());
       
   127 
       
   128     // Send data, 0 is new op
       
   129     TInt err;
       
   130     TInt dataSize = 0;
       
   131     TIpcArgs args(&request, &data);
       
   132     TRAP( err, dataSize = iSession.SendReceiveL(KIPCOperation,args) );
       
   133 
       
   134     // map return value
       
   135     if (err == KErrNone) {
       
   136         iDataSize = dataSize;
       
   137     }
       
   138     return (err == KErrNone);
       
   139 }
       
   140 
       
   141 /*!
       
   142  Send a request asynchronously
       
   143  @param aRequestType type of request to send to the server
       
   144  @param aData data to send to the server
       
   145  */
       
   146 void CServiceSymbianIPC::sendAsync(const QString& aRequestType,
       
   147                                    const QByteArray& aData)
       
   148 {
       
   149     delete iRequestData;
       
   150     iRequestData = NULL;
       
   151     TPtrC request(reinterpret_cast<const TUint16*> (aRequestType.utf16()));
       
   152     iRequestData = request.Alloc();
       
   153     delete iAsyncData;
       
   154     iAsyncData = NULL;
       
   155     TPtrC8 data(reinterpret_cast<const TUint8*> (aData.constData()), aData.length());
       
   156     iAsyncData = data.Alloc();
       
   157 
       
   158     // Send data
       
   159     iRequestDataPtr.Set(*iRequestData);
       
   160     iAsyncDataPtr.Set(*iAsyncData);
       
   161     TIpcArgs args(&iRequestDataPtr, &iAsyncDataPtr);
       
   162     iSession.SendReceive(KIPCOperation, args, iStatus);
       
   163     SetActive();
       
   164 }
       
   165 
       
   166 /*!
       
   167  Reads all data pending in the buffer
       
   168  @return QByteArray containing the result data
       
   169  */
       
   170 QByteArray CServiceSymbianIPC::readAll()
       
   171 {
       
   172     QByteArray rtn;
       
   173     TRAP_IGNORE( rtn = doReadAllL() );
       
   174 
       
   175     return rtn;
       
   176 }
       
   177 
       
   178 /*!
       
   179  Reads all data pending in the buffer, leaves if an error occurred
       
   180  @return QByteArray containing the result data
       
   181  */
       
   182 QByteArray CServiceSymbianIPC::doReadAllL()
       
   183 {
       
   184     // Read the data via IPC
       
   185     //
       
   186     CBufBase* buf = CBufFlat::NewL(iDataSize);
       
   187     CleanupStack::PushL(buf);
       
   188     buf->ResizeL(iDataSize);
       
   189     TPtr8 ptr(buf->Ptr(0));
       
   190     iSession.SendReceiveL(KIPCGetBuffer, TIpcArgs(&ptr));
       
   191 
       
   192     QByteArray convert((char *)ptr.Ptr(), ptr.Length());
       
   193 
       
   194     CleanupStack::PopAndDestroy(buf);
       
   195 
       
   196     // Deep copy, return variable is implicitly shared
       
   197     return convert;
       
   198 }
       
   199 
       
   200 /*!
       
   201  Maps error codes from Symbian error codes to Service IPC error codes
       
   202  @param aError Symbian error code
       
   203  @return mapped error code
       
   204  */
       
   205 int CServiceSymbianIPC::doMapErrors(TInt aError)
       
   206 {
       
   207     int error(0);
       
   208     switch (aError) {
       
   209     case KErrNone: {
       
   210         error = 0;
       
   211         break;
       
   212     }
       
   213     case KErrPermissionDenied:
       
   214     case KErrServerTerminated: {
       
   215         error = ServiceFwIPC::EConnectionClosed;
       
   216         break;
       
   217     }
       
   218     case KErrServerBusy: {
       
   219         error = ServiceFwIPC::EConnectionError;
       
   220         break;
       
   221     }
       
   222     case KErrArgument:
       
   223     case KErrNoMemory: {
       
   224         error = ServiceFwIPC::EIPCError;
       
   225         break;
       
   226     }
       
   227     default: {
       
   228         error = ServiceFwIPC::EUnknownError;
       
   229         break;
       
   230     }
       
   231     }
       
   232     return error;
       
   233 }
       
   234 /*!
       
   235  Waits until data is available for reading 
       
   236  @return bool always true, no need to wait
       
   237  */
       
   238 bool CServiceSymbianIPC::waitForRead()
       
   239 {
       
   240     // Symbian Client-server is blocking, so no need to wait for read
       
   241     return true;
       
   242 }
       
   243 
       
   244 /*!
       
   245  Active object callback
       
   246  */
       
   247 void CServiceSymbianIPC::RunL()
       
   248 {
       
   249     TInt err = iStatus.Int();
       
   250 
       
   251     // Callback to observers
       
   252     //
       
   253     if (err >= KErrNone) {
       
   254         iDataSize = err;
       
   255         emitReadyRead();
       
   256     } else {
       
   257         emitError(doMapErrors(err));
       
   258     }
       
   259 
       
   260     // Cleanup async request, no need to delete if client re-requested an async op
       
   261     if( !asyncPending() ) {
       
   262         delete iRequestData;
       
   263         iRequestData = NULL;
       
   264         delete iAsyncData;
       
   265         iAsyncData = NULL;
       
   266         iRequestDataPtr.Set(KNullDesC);
       
   267         iAsyncDataPtr.Set(KNullDesC8);
       
   268     }
       
   269 }
       
   270 
       
   271 /*!
       
   272  Active object cancel
       
   273  */
       
   274 void CServiceSymbianIPC::DoCancel()
       
   275 {
       
   276     // We can't cancel in the IPC design.
       
   277     TRequestStatus* status = &iStatus;
       
   278     User::RequestComplete(status, KErrCancel);
       
   279 }
       
   280 
       
   281 }
       
   282 // END OF FILE