2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.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
8 * http://floralicense.org
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.
20 #include <security-server.h>
23 #include "MsgCppTypes.h"
24 #include "MsgException.h"
25 #include "MsgUtilFile.h"
26 #include "MsgGconfWrapper.h"
27 #include "MsgProxyListener.h"
28 #include "MsgHandle.h"
30 /*==================================================================================================
31 IMPLEMENTATION OF MsgHandle - Control Member Functions
32 ==================================================================================================*/
33 MsgHandle::MsgHandle() : mCounter(0), mClientSock()
35 memset(mConnectionId, 0x00, sizeof(mConnectionId));
36 memset(mCookie, 0x00, sizeof(mCookie));
40 MsgHandle::~MsgHandle()
46 void MsgHandle::openHandle()
53 // server is currently booting and service is not available until the end of booting
54 MsgSettingGetBool(VCONFKEY_MSG_SERVER_READY, &bReady);
56 if (bReady == false) {
57 THROW(MsgException::SERVER_READY_ERROR, "Msg Server is not ready !!!!!");
59 MSG_DEBUG("Msg Server is ready !!!!!");
63 // cookieSize = security_server_get_cookie_size();
65 // MSG_DEBUG("cookie size : [%d]", cookieSize);
68 // ret = security_server_request_cookie(mCookie, cookieSize);
71 // MSG_DEBUG("security_server_request_cookie() error!! [%d]", ret);
80 void MsgHandle::closeHandle(MsgHandle* pHandle)
84 //Remove CB List of closing Handle
85 MsgProxyListener* eventListener = MsgProxyListener::instance();
87 eventListener->clearListOfClosedHandle(pHandle);
88 //eventListener->stop();
97 void MsgHandle::connectSocket()
99 mClientSock.connect(MSG_SOCKET_PATH);
103 void MsgHandle::disconnectSocket()
109 void MsgHandle::write(const char *pCmdData, int cmdSize, char **ppEvent)
111 if (pCmdData == NULL || ppEvent == NULL) {
112 THROW(MsgException::INVALID_PARAM, "Param is NULL");
117 // Send Command to MSG FW
118 ret = mClientSock.write(pCmdData, cmdSize);
120 THROW(MsgException::IPC_ERROR, "IPC write error");
122 // Receive Result from MSG FW
125 if (*ppEvent == NULL) {
126 THROW(MsgException::INVALID_RESULT, "event is NULL");
132 void MsgHandle::read(char **ppEvent)
134 unsigned int dataSize = 0;
136 dataSize = mClientSock.read(ppEvent, &dataSize);
139 THROW(MsgException::IPC_ERROR, "Server closed connection");
144 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_HIDDEN_S *pSrc, MSG_MESSAGE_INFO_S *pDest)
148 pDest->msgId = pSrc->msgId;
149 pDest->threadId = pSrc->threadId;
150 pDest->folderId = pSrc->folderId;
151 pDest->msgType.mainType = pSrc->mainType;
152 pDest->msgType.subType = pSrc->subType;
153 pDest->msgType.classType= pSrc->classType;
154 pDest->storageId = pSrc->storageId;
156 msg_struct_list_s *addr_info_s = pSrc->addr_list;
159 msg_struct_s *addr_info = NULL;
160 MSG_ADDRESS_INFO_S *address = NULL;
162 pDest->nAddressCnt = addr_info_s->nCount;
164 for (int i = 0; i < addr_info_s->nCount; i++)
166 addr_info = (msg_struct_s *)addr_info_s->msg_struct_info[i];
167 address = (MSG_ADDRESS_INFO_S *)addr_info->data;
169 pDest->addressList[i].addressType = address->addressType;
170 pDest->addressList[i].recipientType = address->recipientType;
171 pDest->addressList[i].contactId = address->contactId;
172 strncpy(pDest->addressList[i].addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
173 strncpy(pDest->addressList[i].displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
174 pDest->addressList[i].displayName[MAX_DISPLAY_NAME_LEN] = '\0';
178 strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
179 strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
181 pDest->displayTime = pSrc->displayTime;
182 pDest->networkStatus = pSrc->networkStatus;
183 pDest->encodeType = pSrc->encodeType;
184 pDest->bRead = pSrc->bRead;
185 pDest->bProtected = pSrc->bProtected;
186 pDest->bBackup = pSrc->bBackup;
187 pDest->priority = pSrc->priority;
188 pDest->direction = pSrc->direction;
191 pDest->msgPort.valid = pSrc->bPortValid;
193 if (pDest->msgPort.valid == true) {
194 pDest->msgPort.dstPort = pSrc->dstPort;
195 pDest->msgPort.srcPort = pSrc->srcPort;
198 MSG_DEBUG("nSize = %d", pSrc->dataSize);
200 if (pSrc->mainType == MSG_SMS_TYPE){
201 pDest->bTextSms = true;
202 pDest->dataSize = pSrc->dataSize;
204 memset(pDest->msgText, 0x00, sizeof(pDest->msgText));
206 if (pSrc->dataSize > MAX_MSG_TEXT_LEN) {
207 // Save Message Data into File
208 char fileName[MSG_FILENAME_LEN_MAX+1];
209 memset(fileName, 0x00, sizeof(fileName));
211 if(MsgCreateFileName(fileName) == false)
212 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
214 MSG_DEBUG("Save pSrc->pData into file : size[%d] name[%s]", pDest->dataSize, fileName);
216 if (MsgWriteIpcFile(fileName, (char*)pSrc->pData, pSrc->dataSize) == false)
217 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
219 memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
220 strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
222 pDest->bTextSms = false;
225 if (pDest->encodeType == MSG_ENCODE_8BIT)
226 memcpy(pDest->msgText, pSrc->pData, pSrc->dataSize);
228 strncpy(pDest->msgText, (char*)pSrc->pData, pSrc->dataSize);
231 MSG_DEBUG("pData = %s", pSrc->pData);
232 MSG_DEBUG("msgText = %s", pDest->msgText);
233 } else if (pSrc->mainType == MSG_MMS_TYPE) {
235 pDest->bTextSms = false;
236 pDest->dataSize = pSrc->dataSize;
238 if(pSrc->subType == MSG_READREPLY_MMS) {
239 memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
241 if (pSrc->mmsDataSize < MAX_MSG_DATA_LEN)
242 memcpy(pDest->msgData, pSrc->pMmsData, pSrc->mmsDataSize);
244 memcpy(pDest->msgData, pSrc->pMmsData, MAX_MSG_DATA_LEN);
247 // Save Message Data into File
248 char fileName[MSG_FILENAME_LEN_MAX+1];
249 memset(fileName, 0x00, sizeof(fileName));
251 if(MsgCreateFileName(fileName) == false)
252 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
254 // change file extension in case of java MMS msg
255 if (pSrc->subType == MSG_SENDREQ_JAVA_MMS) {
257 pFileNameExt = strstr(fileName,"DATA");
258 strncpy(pFileNameExt,"JAVA", MSG_FILENAME_LEN_MAX);
261 MSG_DEBUG("Save Message Data into file : size[%d] name[%s]", pSrc->mmsDataSize, fileName);
262 if (MsgWriteIpcFile(fileName, (char*)pSrc->pMmsData, pSrc->mmsDataSize) == false)
263 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
265 memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
266 strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
268 strncpy(pDest->msgText, (char*)pSrc->pData, MAX_MSG_TEXT_LEN);
271 if (strlen(pSrc->thumbPath) > 0) {
272 memset(pDest->thumbPath, 0x00, sizeof(pDest->thumbPath));
273 memcpy(pDest->thumbPath, pSrc->thumbPath, sizeof(pDest->thumbPath));
282 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_INFO_S *pSrc, MSG_MESSAGE_HIDDEN_S *pDest)
286 pDest->msgId = pSrc->msgId;
287 pDest->threadId = pSrc->threadId;
288 pDest->folderId = pSrc->folderId;
289 pDest->mainType = pSrc->msgType.mainType;
290 pDest->subType = pSrc->msgType.subType;
291 pDest->storageId = pSrc->storageId;
293 strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
294 strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
296 pDest->displayTime = pSrc->displayTime;
297 pDest->networkStatus = pSrc->networkStatus;
298 pDest->encodeType = pSrc->encodeType;
299 pDest->bRead = pSrc->bRead;
300 pDest->bProtected = pSrc->bProtected;
301 pDest->bBackup = pSrc->bBackup;
302 pDest->priority = pSrc->priority;
303 pDest->direction = pSrc->direction;
306 pDest->bPortValid = pSrc->msgPort.valid;
308 if (pDest->bPortValid == true) {
309 pDest->dstPort = pSrc->msgPort.dstPort;
310 pDest->srcPort = pSrc->msgPort.srcPort;
313 if(pSrc->thumbPath[0] != '\0')
314 strncpy(pDest->thumbPath, pSrc->thumbPath, MSG_FILEPATH_LEN_MAX);
316 pDest->addr_list->nCount = pSrc->nAddressCnt;
318 msg_struct_s *addr_info_s = NULL;
319 MSG_ADDRESS_INFO_S *addr_info = NULL;
321 for (int i = 0; i < pDest->addr_list->nCount; i++)
323 addr_info_s = (msg_struct_s *)pDest->addr_list->msg_struct_info[i];
324 addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
326 addr_info->addressType = pSrc->addressList[i].addressType;
327 addr_info->recipientType = pSrc->addressList[i].recipientType;
328 addr_info->contactId = pSrc->addressList[i].contactId;
329 strncpy(addr_info->addressVal, pSrc->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
330 strncpy(addr_info->displayName, pSrc->addressList[i].displayName, MAX_DISPLAY_NAME_LEN);
331 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
335 if (pSrc->bTextSms == false) {
338 char* pFileData = NULL;
339 AutoPtr<char> buf(&pFileData);
341 pDest->dataSize = pSrc->dataSize;
343 // Get Message Data from File
344 if (pSrc->networkStatus != MSG_NETWORK_RETRIEVE_FAIL) {
346 if (MsgOpenAndReadFile(pSrc->msgData, &pFileData, &fileSize) == false)
347 THROW(MsgException::FILE_ERROR, "MsgOpenAndReadFile error");
349 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
350 if (pDest->encodeType == MSG_ENCODE_8BIT) {
351 pDest->pData = (void*)new char[fileSize];
352 memset(pDest->pData, 0x00, fileSize);
353 memcpy(pDest->pData, pFileData, fileSize);
355 pDest->pData = (void*)new char[fileSize+1];
356 memset(pDest->pData, 0x00, fileSize+1);
357 strncpy((char*)pDest->pData, pFileData, fileSize);
360 if (pSrc->msgText[0] != '\0') {
361 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
362 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
363 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
365 pDest->mmsDataSize = fileSize;
366 pDest->pMmsData = (void*)new char[fileSize];
367 memset(pDest->pMmsData, 0x00, fileSize);
368 memcpy(pDest->pMmsData, pFileData, fileSize);
369 MSG_DEBUG("Get Message Data from file : size[%d] name[%s]", pDest->mmsDataSize, pSrc->msgData);
373 pDest->dataSize = pSrc->dataSize;
375 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
376 if (pDest->encodeType == MSG_ENCODE_8BIT || pSrc->msgType.subType == MSG_ETWS_SMS) {
377 pDest->pData = (void*)new char[pDest->dataSize];
378 memset(pDest->pData, 0x00, pDest->dataSize);
379 memcpy(pDest->pData, pSrc->msgText, pDest->dataSize);
381 pDest->pData = (void*)new char[pDest->dataSize+1];
382 memset(pDest->pData, 0x00, pDest->dataSize+1);
383 strncpy((char*)pDest->pData, pSrc->msgText, pDest->dataSize);
386 if (pSrc->msgText[0] != '\0') {
387 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
388 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
389 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
392 pDest->mmsDataSize = pDest->dataSize;
393 pDest->pMmsData = (void*)new char[pDest->dataSize];
394 memset(pDest->pMmsData, 0x00, pDest->dataSize);
395 memcpy(pDest->pMmsData, pSrc->msgData, pDest->dataSize);
403 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_S* pSrc, MSG_SENDINGOPT_INFO_S* pDest, MSG_MESSAGE_TYPE_S msgType)
407 pDest->bSetting = pSrc->bSetting;
409 if (pSrc->bSetting == false) {
410 MSG_DEBUG("No Sending Option");
414 pDest->bDeliverReq = pSrc->bDeliverReq;
415 pDest->bKeepCopy = pSrc->bKeepCopy;
417 MSG_DEBUG("pDest->bSetting = %d", pDest->bSetting);
418 MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
419 MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
421 if (msgType.mainType == MSG_SMS_TYPE) {
422 msg_struct_s *pStruct = (msg_struct_s *)pSrc->smsSendOpt;
425 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
428 pDest->option.smsSendOptInfo.bReplyPath = pSms->bReplyPath;
431 } else if (msgType.mainType == MSG_MMS_TYPE) {
432 msg_struct_s *pStruct = (msg_struct_s *)pSrc->mmsSendOpt;
435 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
438 pDest->option.mmsSendOptInfo.priority = pMms->priority;
439 pDest->option.mmsSendOptInfo.bReadReq = pMms->bReadReq;
441 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
442 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
444 if (pMms->expiryTime == 0) {
445 pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_NONE;
446 pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
448 pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_RELATIVE;
449 pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
452 if (pMms->bUseDeliveryCustomTime == true) {
453 pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = true;
455 pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = false;
457 pDest->option.mmsSendOptInfo.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
458 pDest->option.mmsSendOptInfo.deliveryTime.time = pMms->deliveryTime;
460 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pDest->option.mmsSendOptInfo.expiryTime.time);
469 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_INFO_S* pSrc, MSG_SENDINGOPT_S* pDest, MSG_MESSAGE_TYPE_S msgType)
473 pDest->bDeliverReq = pSrc->bDeliverReq;
474 pDest->bKeepCopy = pSrc->bKeepCopy;
476 MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
477 MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
479 if (msgType.mainType == MSG_SMS_TYPE) {
480 msg_struct_s *pStruct = (msg_struct_s *)pDest->smsSendOpt;
481 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
482 pSms->bReplyPath = pSrc->option.smsSendOptInfo.bReplyPath;
483 } else if (msgType.mainType == MSG_MMS_TYPE) {
484 msg_struct_s *pStruct = (msg_struct_s *)pDest->mmsSendOpt;
485 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
486 pMms->priority = pSrc->option.mmsSendOptInfo.priority;
487 pMms->bReadReq = pSrc->option.mmsSendOptInfo.bReadReq;
488 pMms->expiryTime = pSrc->option.mmsSendOptInfo.expiryTime.time;
489 pMms->deliveryTime = pSrc->option.mmsSendOptInfo.deliveryTime.time;
491 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
492 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
493 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pMms->expiryTime);
500 int MsgHandle::getSettingCmdSize(MSG_OPTION_TYPE_T optionType)
502 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_OPTION_TYPE_T);
506 case MSG_GENERAL_OPT :
507 cmdSize += sizeof(MSG_GENERAL_OPT_S);
509 case MSG_SMS_SENDOPT :
510 cmdSize += sizeof(MSG_SMS_SENDOPT_S);
513 cmdSize += sizeof(MSG_SMSC_LIST_S);
515 case MSG_MMS_SENDOPT :
516 cmdSize += sizeof(MSG_MMS_SENDOPT_S);
518 case MSG_MMS_RECVOPT :
519 cmdSize += sizeof(MSG_MMS_RECVOPT_S);
521 case MSG_MMS_STYLEOPT :
522 cmdSize += sizeof(MSG_MMS_STYLEOPT_S);
524 case MSG_PUSHMSG_OPT :
525 cmdSize += sizeof(MSG_PUSHMSG_OPT_S);
528 cmdSize += sizeof(MSG_CBMSG_OPT_S);
530 case MSG_VOICEMAIL_OPT :
531 cmdSize += sizeof(MSG_VOICEMAIL_OPT_S);
533 case MSG_MSGSIZE_OPT :
534 cmdSize += sizeof(MSG_MSGSIZE_OPT_S);
542 bool MsgHandle::CheckEventData(char *pEventData)
544 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
546 switch (pEvent->eventType)
548 case MSG_EVENT_PLG_SENT_STATUS_CNF :
549 case MSG_EVENT_PLG_INCOMING_MSG_IND :
550 case MSG_EVENT_PLG_INCOMING_MMS_CONF :
551 case MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND :
552 case MSG_EVENT_PLG_INCOMING_LBS_MSG_IND :
553 case MSG_EVENT_SYNCML_OPERATION :
554 case MSG_EVENT_PLG_STORAGE_CHANGE_IND :
555 case MSG_EVENT_PLG_INCOMING_CB_MSG_IND :
556 case MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND :