Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*==================================================================================================
     18 
     19     Header Name: dmProcessScriptSession.cc
     20 
     21     General Description: Implementation of DMProcessScriptSession class.
     22 
     23 ==================================================================================================*/
     24 
     25 #include "dmProcessScriptSession.h"
     26 #include "dm_ua_handlecommand.h"
     27 #include "xpl_Logger.h"
     28 
     29 /*==================================================================================================
     30 FUNCTION        : DMProcessScriptSession::DMProcessScriptSession
     31 
     32 DESCRIPTION     : The class constructor.
     33 ARGUMENT PASSED : sml_ContentType
     34                   sml_EncodingType
     35 OUTPUT PARAMETER:
     36 RETURN VALUE    :
     37 IMPORTANT NOTES :
     38 ==================================================================================================*/
     39 DMProcessScriptSession::DMProcessScriptSession()
     40 {
     41 
     42     inAtomicCommand = FALSE;
     43     commandCount = 0;
     44     m_bSessionAborted = FALSE;
     45 
     46     serverSessionId  = 0;
     47     serverRetryCount = 0;
     48     clientRetryCount = 0;
     49 
     50     m_nSecState = DM_CLIENT_NO_SERVER_Y_AUTH;
     51     isServCredsMissing  = FALSE;
     52 
     53 #ifdef LOB_SUPPORT
     54    DMGetData lrgObjData;
     55    isLargeObjectSupported = FALSE;
     56    dmTreeObj.Get("./DevDetail/LrgObj", lrgObjData,SYNCML_DM_REQUEST_TYPE_SERVER);
     57    isLargeObjectSupported =  !lrgObjData.m_oData.compare("false");
     58    // Get default MaxObjectSize
     59    DMNode* pNode= dmTreeObj.FindNodeByURI("./DevDetail/Ext/MaxObjSize");
     60    dmtMaxObjectSize = dmTreeObj.readOneWordFromTree(pNode, 0);
     61 #endif
     62 }
     63 
     64 /*==================================================================================================
     65 FUNCTION        : DMProcessScriptSession::~DMProcessScriptSession
     66 
     67 DESCRIPTION     : The class destructor.
     68 ARGUMENT PASSED :
     69 OUTPUT PARAMETER:
     70 RETURN VALUE    :
     71 IMPORTANT NOTES :
     72 ==================================================================================================*/
     73 DMProcessScriptSession::~DMProcessScriptSession ()
     74 {
     75     while ( userData.aStatuses.size() > 0 )
     76     {
     77       smlFreeStatus((SmlStatusPtr_t)userData.aStatuses[0]);
     78       userData.aStatuses.remove(0);
     79     }
     80 
     81     while ( userData.aResults.size() > 0 )
     82     {
     83       smlFreeResults( userData.aResults[0]._pGetExecResult );
     84       userData.aResults[0]._pGetExecResult= NULL;
     85 
     86       if ( userData.aResults[0]._type == SYNCML_DM_RESULT_VALUE::Enum_Result_GetStruct ||
     87           userData.aResults[0]._type == SYNCML_DM_RESULT_VALUE::Enum_Result_GetStructData )
     88       {
     89 
     90         if (userData.aResults[0]._oGetStructPos.psRetData )
     91         {
     92           delete userData.aResults[0]._oGetStructPos.psRetData;
     93         }
     94       }
     95       userData.aResults.remove(0);
     96     }
     97 }
     98 
     99 /*==================================================================================================
    100 FUNCTION        : DMProcessScriptSession::IncCommandCount
    101 
    102 DESCRIPTION     : This function will increment the value of data member commandCount by 1.
    103 ARGUMENT PASSED : newCommandCount
    104 OUTPUT PARAMETER:
    105 RETURN VALUE    :
    106 IMPORTANT NOTES :
    107 ==================================================================================================*/
    108 void
    109 DMProcessScriptSession::IncCommandCount ()
    110 {
    111     commandCount++;
    112 }
    113 
    114 /*==================================================================================================
    115 Function:    DMProcessScriptSession::SetInAtomicCommand
    116 
    117 Description: The function will set the value of data member inAtomicCommand.
    118 ARGUMENT PASSED : newInAtomicCommand
    119 OUTPUT PARAMETER:
    120 RETURN VALUE    :
    121 IMPORTANT NOTES :
    122 ==================================================================================================*/
    123 void
    124 DMProcessScriptSession::SetInAtomicCommand (BOOLEAN newInAtomicCommand)
    125 {
    126     inAtomicCommand = newInAtomicCommand;
    127 }
    128 
    129 /*==================================================================================================
    130 Function:    DMProcessScriptSession::GetInAtomicCommand
    131 
    132 Description: The function will return the value of data member inAtomicCommand.
    133 ARGUMENT PASSED :
    134 OUTPUT PARAMETER:
    135 RETURN VALUE    : inAtomicCommand
    136 IMPORTANT NOTES :
    137 ==================================================================================================*/
    138 BOOLEAN
    139 DMProcessScriptSession::GetInAtomicCommand()
    140 {
    141     return inAtomicCommand;
    142 }
    143 
    144 
    145 /*==================================================================================================
    146 Function:    DMProcessScriptSession::GetServerSessionId
    147 
    148 Description: The function will return the value of data member serverSessionId.
    149 ARGUMENT PASSED :
    150 OUTPUT PARAMETER:
    151 RETURN VALUE    : serverSessionId
    152 IMPORTANT NOTES :
    153 ==================================================================================================*/
    154 UINT16
    155 DMProcessScriptSession::GetServerSessionId()
    156 {
    157     return serverSessionId;
    158 }
    159 
    160 
    161 /*==================================================================================================
    162 Function:    DMProcessScriptSession::GetSendInstanceId
    163 
    164 Description: The function will return the value of data member sendInstanceId.
    165 ARGUMENT PASSED :
    166 OUTPUT PARAMETER:
    167 RETURN VALUE    : sendInstanceId
    168 IMPORTANT NOTES :
    169 ==================================================================================================*/
    170 InstanceID_t
    171 DMProcessScriptSession::GetSendInstanceId ()
    172 {
    173     return sendInstanceId;
    174 }
    175 
    176 /*==================================================================================================
    177 Function:    DMProcessScriptSession::SetClientRetryCount
    178 
    179 Description: The function will set the clientRetryCount to a new value.
    180 ARGUMENT PASSED :
    181 OUTPUT PARAMETER:
    182 RETURN VALUE    : void
    183 IMPORTANT NOTES :
    184 ==================================================================================================*/
    185 void
    186 DMProcessScriptSession::SetClientRetryCount (UINT8 newcount)
    187 {
    188     clientRetryCount = newcount;
    189 }
    190 
    191 /*==================================================================================================
    192 Function:    DMProcessScriptSession::IncClientRetryCount
    193 
    194 Description: The function will increment the clientRetryCount by 1.
    195 ARGUMENT PASSED :
    196 OUTPUT PARAMETER:
    197 RETURN VALUE    : void
    198 IMPORTANT NOTES :
    199 ==================================================================================================*/
    200 void
    201 DMProcessScriptSession::IncClientRetryCount ()
    202 {
    203     clientRetryCount++;
    204 }
    205 
    206 
    207 /*==================================================================================================
    208 FUNCTION        : DMProcessScriptSession::SessionStart
    209 
    210 DESCRIPTION     : The UserAgen::SessionStart calls this function after it creates MgmtSession object.
    211                   The function will perform the following operations:
    212                   1) Call SessionStart() to setup the DM tree.
    213                   2) Register the DM engine with the SYNCML toolkit.
    214                   3) Connect the client with the server.
    215                   4) Build and send the package one.
    216 ARGUMENT PASSED : p_SessionStart
    217 OUTPUT PARAMETER:
    218 RETURN VALUE    :
    219 IMPORTANT NOTES :
    220 ==================================================================================================*/
    221 SYNCML_DM_RET_STATUS_T
    222 DMProcessScriptSession::Start(const UINT8 *docInputBuffer,
    223                                UINT32 inDocSize,
    224                                BOOLEAN isWBXML,
    225                                DMBuffer & oResult)
    226 {
    227     SYNCML_DM_RET_STATUS_T ret_stat = SYNCML_DM_FAIL;
    228     Ret_t                  sml_ret_stat;
    229 
    230     XPL_LOG_DM_SESS_Debug(("Entered DMProcessScriptSession::SessionStart, buf=%x,size=%d, wbxml=%d\n", docInputBuffer, inDocSize, isWBXML));
    231 
    232     XPL_LOG_DM_SESS_Debug(("Entered DMProcessScriptSession::SessionStart, resultbuf=%x", &oResult));
    233 
    234     ret_stat = Init(isWBXML);
    235     if ( ret_stat != SYNCML_DM_SUCCESS )
    236         return ret_stat;
    237 
    238     /* Register the DM engine with the SYNCML toolkit. */
    239     userData.pSessionMng = this;
    240     userData.pPkgBuilder = &m_oPkgBuilder;
    241     ret_stat = RegisterDmEngineWithSyncmlToolkit(&userData);
    242     XPL_LOG_DM_SESS_Debug(("after entered\n"));
    243 
    244     if ( ret_stat != SYNCML_DM_SUCCESS )
    245     {
    246         XPL_LOG_DM_SESS_Debug(("Exiting: RegisterDmEngineWithSyncmlToolkit failed\n"));
    247         return (ret_stat);
    248     }
    249 
    250 
    251     sml_ret_stat = smlLockWriteBuffer(recvInstanceId, &pWritePos, &workspaceFreeSize);
    252     XPL_LOG_DM_SESS_Debug(("after smlLockWriteBuffer\n"));
    253 
    254     if ( sml_ret_stat != SML_ERR_OK )
    255         return SYNCML_DM_FAIL;
    256 
    257     memcpy(pWritePos,docInputBuffer,inDocSize);
    258     XPL_LOG_DM_SESS_Debug(("after memcpy\n"));
    259 
    260     recvSmlDoc.dataSize = inDocSize;
    261 
    262     ret_stat = ParseMessage();
    263     XPL_LOG_DM_SESS_Debug(("after ParseMessage\n"));
    264 
    265     if (ret_stat != SYNCML_DM_SUCCESS)
    266        return ret_stat;
    267 
    268 
    269     smlLockReadBuffer(sendInstanceId, &pReadPos, &workspaceUsedSize);
    270     XPL_LOG_DM_SESS_Debug(("after smlLockReadBuffer\n"));
    271 
    272     /* Set sendSmlDoc point to workspace */
    273     if ( workspaceUsedSize )
    274     {
    275         oResult.assign(pReadPos,workspaceUsedSize);
    276         if ( oResult.getBuffer() == NULL )
    277             ret_stat = SYNCML_DM_DEVICE_FULL;
    278         else
    279             ret_stat = SYNCML_DM_SUCCESS;
    280     }
    281 
    282     XPL_LOG_DM_SESS_Debug(("dmProcessScriptSession:: workspaceUsedSize:%d, ret_stat:%d", workspaceUsedSize, ret_stat));
    283     XPL_LOG_DM_SESS_Debug(("dmProcessScriptSession:: oResult.getBuffer()=%x, size=%d\n", oResult.getBuffer(), oResult.getSize()));
    284     return ret_stat;
    285 }
    286 
    287 /*==================================================================================================
    288 FUNCTION        : DMProcessScriptSession::ParseMessage
    289 
    290 DESCRIPTION     : The UserAgent::TransportMsg will call this function after RecvMessage() call
    291                   returned.
    292                   The function will use SyncML toolkit smlProcessData() function to parse and
    293                   process commands in the package.
    294 ARGUMENT PASSED :
    295 OUTPUT PARAMETER:
    296 RETURN VALUE    :
    297 IMPORTANT NOTES :
    298 
    299 
    300              This method will perform the following operations:
    301              1) Unlock both of the Toolkit Buffers.
    302              2) Call smlProcessData for the first command.
    303              3) Loop around smlProcessData on the next command while checking for Multiple Messages
    304 
    305 ==================================================================================================*/
    306 SYNCML_DM_RET_STATUS_T
    307 DMProcessScriptSession::ParseMessage()
    308 {
    309     Ret_t        sml_ret_stat;
    310     SYNCML_DM_RET_STATUS_T  ret_stat = SYNCML_DM_SUCCESS;
    311 
    312     smlUnlockWriteBuffer(recvInstanceId, recvSmlDoc.dataSize);
    313 
    314     /* Process the first command in the incoming message */
    315     sml_ret_stat = smlProcessData(recvInstanceId, SML_FIRST_COMMAND);
    316 
    317     if (sml_ret_stat != SML_ERR_OK)
    318         return(SYNCML_DM_FAIL);
    319 
    320     /* Loop through the remaining commands until we reach the end of the
    321        current toolkit workspace */
    322     while (sml_ret_stat != SML_ERR_XLT_END_OF_BUFFER)
    323     {
    324         sml_ret_stat = smlProcessData(recvInstanceId, SML_NEXT_COMMAND);
    325         /* The callback routines set this value when they realize we need
    326            to begin processing the other buffer. */
    327 
    328         /* Anything wrong with the command, or inMultipleMessageMode is TRUE, break out here.
    329          * The callbacks set inMultipleMessageMode when they can no longer
    330          * fit anything into the outgoing message workspace
    331          */
    332         if ((sml_ret_stat != SML_ERR_OK) )
    333             break;
    334     }
    335 
    336     if (sml_ret_stat != SML_ERR_OK && sml_ret_stat != SML_ERR_XLT_END_OF_BUFFER)
    337     {
    338         ret_stat = dmTreeObj.GetLockContextManager().ReleaseIDInternal(SYNCML_DM_LOCKID_CURRENT, SYNCML_DM_ROLLBACK);
    339         ret_stat = SYNCML_DM_FAIL;
    340         KCDBG("dmProcessScriptSession:: sml_ret_stat:%d, ret_stat:%d", sml_ret_stat, ret_stat);
    341     }
    342 
    343     return (ret_stat);
    344 }
    345 
    346 
    347 /*==================================================================================================
    348 FUNCTION        : DMProcessScriptSession::SetToolkitCallbacks
    349 
    350 DESCRIPTION     : This function will to set toolkit callback functions.
    351 
    352 
    353 ARGUMENT PASSED :
    354 OUTPUT PARAMETER:
    355 RETURN VALUE    : It returns SYNCML_DM_SUCCESS.
    356 IMPORTANT NOTES :
    357 
    358 
    359 ==================================================================================================*/
    360 SYNCML_DM_RET_STATUS_T
    361 DMProcessScriptSession::SetToolkitCallbacks(SmlCallbacks_t * pSmlCallbacks)
    362 {
    363 
    364     pSmlCallbacks->startMessageFunc = HandleStartMessage;
    365     pSmlCallbacks->endMessageFunc   = HandleEndMessage;
    366 
    367     /* Sync Command callbacks */
    368     pSmlCallbacks->addCmdFunc       = HandleAddCommand;
    369 
    370     pSmlCallbacks->alertCmdFunc     = HandleAlertCommand;
    371     pSmlCallbacks->copyCmdFunc      = HandleCopyCommand;
    372     pSmlCallbacks->deleteCmdFunc    = HandleDeleteCommand;
    373     pSmlCallbacks->getCmdFunc       = HandleGetCommand;
    374 
    375     pSmlCallbacks->startAtomicFunc   = HandleStartAtomicCommand;
    376     pSmlCallbacks->endAtomicFunc     = HandleEndAtomicCommand;
    377 
    378     pSmlCallbacks->startSequenceFunc = HandleStartSequenceCommand;
    379     pSmlCallbacks->endSequenceFunc   = HandleEndSequenceCommand;
    380 
    381     pSmlCallbacks->execCmdFunc      = HandleExecCommand;
    382     pSmlCallbacks->statusCmdFunc    = HandleStatusCommand;
    383     pSmlCallbacks->replaceCmdFunc   = HandleReplaceCommand;
    384 
    385     /* The transmitChunkFunc callback is required for support of Large Objects. */
    386 
    387     pSmlCallbacks->transmitChunkFunc  = NULL;
    388 
    389     return SYNCML_DM_SUCCESS;
    390 }
    391 
    392 DMClientServerCreds * DMProcessScriptSession::GetClientServerCreds()
    393 {
    394     return &clientServerCreds;
    395 }
    396