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: DMSession.cc 20 21 General Description: Implementation of DMSession class. 22 23 ==================================================================================================*/ 24 25 #include "dmSession.h" 26 27 extern "C" { 28 #include "xpt-b64.h" 29 #include "stdio.h" 30 } 31 32 #include "xpl_Logger.h" 33 34 /*================================================================================================== 35 FUNCTION : DMSession::DMSession 36 37 DESCRIPTION : The class constructor. 38 ARGUMENT PASSED : 39 OUTPUT PARAMETER: 40 RETURN VALUE : 41 IMPORTANT NOTES : 42 ==================================================================================================*/ 43 DMSession::DMSession() 44 { 45 46 smlContentType = NULL; 47 smlEncodingType = SML_UNDEF; 48 49 sendInstanceId = 0xFF; 50 51 pWritePos = NULL; 52 pReadPos = NULL; 53 54 memset( &sendSmlDoc, 0, sizeof(sendSmlDoc)); 55 memset( &recvSmlDoc, 0, sizeof(recvSmlDoc)); 56 57 workspaceFreeSize = 0; 58 workspaceUsedSize = 0; 59 60 } 61 62 /*================================================================================================== 63 FUNCTION : DMSession::~DMSession 64 65 DESCRIPTION : The class destructor. 66 ARGUMENT PASSED : 67 OUTPUT PARAMETER: 68 RETURN VALUE : 69 IMPORTANT NOTES : 70 ==================================================================================================*/ 71 DMSession::~DMSession () 72 { 73 /* Free all the Client and Server Credential memory.*/ 74 UnRegisterDmEngineWithSyncmlToolkit(); 75 if ( smlContentType ) 76 DmFreeMem(smlContentType); 77 } 78 79 /*================================================================================================== 80 FUNCTION : DMSession::Init 81 82 DESCRIPTION : The class constructor. 83 ARGUMENT PASSED : sml_ContentType 84 sml_EncodingType 85 OUTPUT PARAMETER: 86 RETURN VALUE : 87 IMPORTANT NOTES : 88 ==================================================================================================*/ 89 SYNCML_DM_RET_STATUS_T DMSession::Init(BOOLEAN isWBXML) 90 { 91 INT32 size; 92 93 size = DmStrlen(SYNCML_CONTENT_TYPE_DM_WBXML)+1; 94 smlContentType = (UINT8 *)DmAllocMem(size); 95 if ( smlContentType == NULL ) 96 return SYNCML_DM_DEVICE_FULL; 97 98 if ( isWBXML ) 99 { 100 DmStrncpy((char *)smlContentType, SYNCML_CONTENT_TYPE_DM_WBXML, size); 101 smlEncodingType = SML_WBXML; 102 } 103 else 104 { 105 DmStrncpy((char *)smlContentType, SYNCML_CONTENT_TYPE_DM_XML, size); 106 smlEncodingType = SML_XML; 107 } 108 109 return SYNCML_DM_SUCCESS; 110 } 111 112 /*================================================================================================== 113 FUNCTION : DMSession::SetToolkitCallbacks 114 115 DESCRIPTION : This function will to set toolkit callback functions. 116 117 118 ARGUMENT PASSED : 119 OUTPUT PARAMETER: 120 RETURN VALUE : It returns SYNCML_DM_SUCCESS. 121 IMPORTANT NOTES : 122 ==================================================================================================*/ 123 SYNCML_DM_RET_STATUS_T 124 DMSession::SetToolkitCallbacks(SmlCallbacks_t * pSmlCallbacks) 125 { 126 127 pSmlCallbacks->startMessageFunc = NULL; 128 pSmlCallbacks->endMessageFunc = NULL; 129 130 pSmlCallbacks->addCmdFunc = NULL; 131 132 pSmlCallbacks->alertCmdFunc = NULL; 133 pSmlCallbacks->copyCmdFunc = NULL; 134 pSmlCallbacks->deleteCmdFunc = NULL; 135 pSmlCallbacks->getCmdFunc = NULL; 136 137 pSmlCallbacks->startAtomicFunc = NULL; 138 pSmlCallbacks->endAtomicFunc = NULL; 139 140 pSmlCallbacks->startSequenceFunc = NULL; 141 pSmlCallbacks->endSequenceFunc = NULL; 142 143 pSmlCallbacks->execCmdFunc = NULL; 144 pSmlCallbacks->statusCmdFunc = NULL; 145 pSmlCallbacks->replaceCmdFunc = NULL; 146 147 /* The transmitChunkFunc callback is required for support of Large Objects. */ 148 pSmlCallbacks->transmitChunkFunc = NULL; 149 150 return SYNCML_DM_SUCCESS; 151 } 152 153 /*================================================================================================== 154 FUNCTION : DMSession::RegisterDmEngineWithSyncmlToolkit 155 156 DESCRIPTION : This function will to register the DM Engine with the SyncML Toolkit. 157 158 The function will perform the following operations: 159 1) Call smlInit() to initialize the SyncML Toolkit. 160 2) Allocate all callback functions. 161 3) Call smlInitInstance() to initialize the SyncML instances. 162 163 ARGUMENT PASSED : 164 OUTPUT PARAMETER: 165 RETURN VALUE : It returns SYNCML_DM_SUCCESS when the DM Engine is registered with the SyncML 166 Toolkit successfully, SYNCML_DM_FAIL when any error occurs. 167 IMPORTANT NOTES : 168 ==================================================================================================*/ 169 SYNCML_DM_RET_STATUS_T 170 DMSession::RegisterDmEngineWithSyncmlToolkit(VoidPtr_t pUserData) 171 { 172 SmlOptions_t smlOptions; /* Structure describing the options and 173 * setting of this syncml process */ 174 SmlCallbacks_t smlCallbacks; /* Structure defining references to the 175 * applications callback implementations */ 176 SmlInstanceOptions_t smlInstOptionsRecvDoc; /* Structure specifying options to be used for 177 Recv DM doc instance */ 178 SmlInstanceOptions_t smlInstOptionsSendDoc; /* Structure specifying options to be used for 179 Send DM doc instance */ 180 181 Ret_t sml_ret_stat; 182 SYNCML_DM_RET_STATUS_T dm_ret_stat = SYNCML_DM_SUCCESS; 183 184 //fdp101: make sure we don't have un-inited fields in structs... 185 memset( &smlOptions, 0, sizeof(smlOptions)); 186 memset( &smlCallbacks, 0, sizeof(smlCallbacks)); 187 memset( &smlInstOptionsRecvDoc, 0, sizeof(smlInstOptionsRecvDoc)); 188 memset( &smlInstOptionsSendDoc, 0, sizeof(smlInstOptionsSendDoc)); 189 190 /* Set Toolkit options and setting of this syncml process */ 191 smlOptions.defaultPrintFunc = NULL; 192 193 /* maxWorkspaceAvailMem is the size which all workspaces in total MUST not exceed. Since we have 194 * send and recv document exist at the same time, the available memory size is 2 times of the 195 * DM_SMLTK_WORKSPACE_SIZE */ 196 smlOptions.maxWorkspaceAvailMem = NUMBER_OF_WORKSPACE * g_iDMWorkspaceSize; //DM_SMLTK_WORKSPACE_SIZE; 197 198 /* Initialize SyncML Toolkit */ 199 sml_ret_stat = smlInit(&smlOptions); 200 if (sml_ret_stat != SML_ERR_OK) 201 { 202 return (SYNCML_DM_FAIL); 203 } 204 205 /* Receiving DM document instance */ 206 smlInstOptionsRecvDoc.encoding = smlEncodingType; /* Use WBXML or XML encoding */ 207 smlInstOptionsRecvDoc.workspaceName = (char *)DmAllocMem(WORKSPACE_NAME_LEN); 208 DmStrcpy(smlInstOptionsRecvDoc.workspaceName, RECV_WORKSPACE_NAME); 209 smlInstOptionsRecvDoc.workspaceSize = g_iDMWorkspaceSize; //DM_SMLTK_WORKSPACE_SIZE; 210 211 /* Sending DM dcoument instance */ 212 smlInstOptionsSendDoc.encoding = smlEncodingType; /* Use WBXML or XML encoding */ 213 smlInstOptionsSendDoc.workspaceName = (char *)DmAllocMem(WORKSPACE_NAME_LEN); 214 smlLibStrcpy(smlInstOptionsSendDoc.workspaceName, SEND_WORKSPACE_NAME); 215 smlInstOptionsSendDoc.workspaceSize = g_iDMWorkspaceSize; //DM_SMLTK_WORKSPACE_SIZE; 216 217 /* Allocate function pointers for callbacks structure */ 218 /* Protocol Management Callbacks */ 219 dm_ret_stat = SetToolkitCallbacks(&smlCallbacks); 220 221 /* The transmitChunkFunc callback is required for support of Large Objects. */ 222 223 smlCallbacks.transmitChunkFunc = NULL; 224 225 /* Initialize the SyncML instance, get an instance Id, and assigns to it a workspace buffer in 226 which SyncML will assemble or parse XML documents. 227 */ 228 sml_ret_stat = smlInitInstance(&smlCallbacks, &smlInstOptionsRecvDoc, pUserData, &recvInstanceId); 229 if (sml_ret_stat != SML_ERR_OK) 230 { 231 /* Free the memory and exit now.*/ 232 DmFreeMem(smlInstOptionsRecvDoc.workspaceName); 233 DmFreeMem(smlInstOptionsSendDoc.workspaceName); 234 smlTerminate(); 235 return (SYNCML_DM_FAIL); 236 } 237 sml_ret_stat = smlInitInstance(&smlCallbacks, &smlInstOptionsSendDoc, pUserData, &sendInstanceId); 238 if (sml_ret_stat != SML_ERR_OK) 239 { 240 /* Undo the first instance, free the memory and exit.*/ 241 smlTerminateInstance(recvInstanceId); 242 DmFreeMem(smlInstOptionsRecvDoc.workspaceName); 243 DmFreeMem(smlInstOptionsSendDoc.workspaceName); 244 smlTerminate(); 245 return (SYNCML_DM_FAIL); 246 } 247 248 /* Free the memory which we allocated */ 249 DmFreeMem(smlInstOptionsRecvDoc.workspaceName); 250 DmFreeMem(smlInstOptionsSendDoc.workspaceName); 251 252 return (dm_ret_stat); 253 } 254 255 /*================================================================================================== 256 FUNCTION : DMSession::UnRegisterDmEngineWithSyncmlToolkit 257 258 DESCRIPTION : This function will be called by the SYNCML_DM_Session::SessionEnd() to un-register 259 the DM Engine with the SyncML Toolkit. 260 261 The function will perform the following operations: 262 1) Call smlTerminateInstance() to close the SyncML instance. 263 2) Call smlTerminate() to close the Toolkit session. 264 265 ARGUMENT PASSED : 266 OUTPUT PARAMETER: 267 RETURN VALUE : It returns SML_ERR_OK when the DM Engine is un-registered with the SyncML Toolkit 268 successfully, error code when any error occurs. 269 IMPORTANT NOTES : 270 ==================================================================================================*/ 271 void 272 DMSession::UnRegisterDmEngineWithSyncmlToolkit() 273 { 274 /* Close SyncML instance and Toolkit session */ 275 smlTerminateInstance(recvInstanceId); 276 smlTerminateInstance(sendInstanceId); 277 smlTerminate(); 278 } 279