2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "MsgCppTypes.h"
22 #include "MsgException.h"
23 #include "MsgContact.h"
24 #include "MsgUtilFile.h"
25 #include "MsgGconfWrapper.h"
26 #include "MsgProxyListener.h"
27 #include "MsgHandle.h"
28 #include "MsgSqliteWrapper.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()
50 // server is currently booting and service is not available until the end of booting
51 MsgSettingGetBool(VCONFKEY_MSG_SERVER_READY, &bReady);
53 if (bReady == false) {
54 THROW(MsgException::SERVER_READY_ERROR, "Msg Server is not ready !!!!!");
56 MSG_INFO("Msg Server is ready !!!!!");
64 void MsgHandle::closeHandle(MsgHandle* pHandle)
68 //Remove CB List of closing Handle
69 MsgProxyListener* eventListener = MsgProxyListener::instance();
71 eventListener->clearListOfClosedHandle(pHandle);
72 //eventListener->stop();
77 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
78 // Close Contact Service
79 if (MsgCloseContactSvc() != MSG_SUCCESS) {
80 MSG_DEBUG("Fail to close contact service.");
82 #endif // MSG_CONTACTS_SERVICE_NOT_SUPPORTED
83 removeDbHandle(); /* unregister db handler */
89 void MsgHandle::connectSocket()
91 mClientSock.connect(MSG_SOCKET_PATH);
95 void MsgHandle::disconnectSocket()
101 void MsgHandle::write(const char *pCmdData, int cmdSize, char **ppEvent)
103 if (pCmdData == NULL || ppEvent == NULL) {
104 THROW(MsgException::INVALID_PARAM, "Param is NULL");
109 // Send Command to MSG FW
110 ret = mClientSock.write(pCmdData, cmdSize);
112 THROW(MsgException::IPC_ERROR, "IPC write error");
114 char *tmpEvent = NULL;
118 // Receive Result from MSG FW
121 if(tmpEvent == NULL) {
122 MSG_DEBUG("Event Data is NULL!!");
126 if(!CheckEventData(tmpEvent)) {
137 void MsgHandle::read(char **ppEvent)
139 unsigned int dataSize = 0;
141 int ret = mClientSock.read(ppEvent, &dataSize);
143 if (ret == CLOSE_CONNECTION_BY_SIGNAL) {
144 THROW(MsgException::IPC_ERROR, "Server closed connection");
149 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_HIDDEN_S *pSrc, MSG_MESSAGE_INFO_S *pDest)
153 pDest->msgId = pSrc->msgId;
154 pDest->threadId = pSrc->threadId;
155 pDest->folderId = pSrc->folderId;
156 pDest->msgType.mainType = pSrc->mainType;
157 pDest->msgType.subType = pSrc->subType;
158 pDest->msgType.classType= pSrc->classType;
159 pDest->storageId = pSrc->storageId;
161 if (g_list_length(pSrc->addressList) > 0) {
162 msg_struct_s *addr_info = NULL;
163 MSG_ADDRESS_INFO_S *address = NULL;
165 pDest->nAddressCnt = g_list_length(pSrc->addressList);
166 pDest->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * pDest->nAddressCnt];
167 memset(pDest->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * pDest->nAddressCnt);
169 for (int i = 0; i < pDest->nAddressCnt; i++) {
170 addr_info = (msg_struct_s *)g_list_nth_data(pSrc->addressList,(guint)i);
171 address = (MSG_ADDRESS_INFO_S *)addr_info->data;
173 pDest->addressList[i].addressType = address->addressType;
174 pDest->addressList[i].recipientType = address->recipientType;
175 pDest->addressList[i].contactId = address->contactId;
176 strncpy(pDest->addressList[i].addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
177 strncpy(pDest->addressList[i].displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
181 msg_struct_list_s *addr_info_s = pSrc->addr_list;
183 if (addr_info_s && (addr_info_s->nCount > 0)) {
184 msg_struct_s *addr_info = NULL;
185 MSG_ADDRESS_INFO_S *address = NULL;
187 pDest->nAddressCnt = addr_info_s->nCount;
189 pDest->addressList = (MSG_ADDRESS_INFO_S *)new char[sizeof(MSG_ADDRESS_INFO_S) * addr_info_s->nCount];
190 memset(pDest->addressList, 0x00, sizeof(MSG_ADDRESS_INFO_S) * addr_info_s->nCount);
192 for (int i = 0; i < addr_info_s->nCount; i++) {
193 addr_info = (msg_struct_s *)addr_info_s->msg_struct_info[i];
195 address = (MSG_ADDRESS_INFO_S *)addr_info->data;
197 pDest->addressList[i].addressType = address->addressType;
198 pDest->addressList[i].recipientType = address->recipientType;
199 pDest->addressList[i].contactId = address->contactId;
200 strncpy(pDest->addressList[i].addressVal, address->addressVal, MAX_ADDRESS_VAL_LEN);
201 strncpy(pDest->addressList[i].displayName, address->displayName, MAX_DISPLAY_NAME_LEN);
206 strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
207 strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
209 pDest->displayTime = pSrc->displayTime;
210 pDest->networkStatus = pSrc->networkStatus;
211 pDest->encodeType = pSrc->encodeType;
212 pDest->bRead = pSrc->bRead;
213 pDest->bProtected = pSrc->bProtected;
214 pDest->bBackup = pSrc->bBackup;
215 pDest->priority = pSrc->priority;
216 pDest->direction = pSrc->direction;
219 pDest->msgPort.valid = pSrc->bPortValid;
221 if (pDest->msgPort.valid == true) {
222 pDest->msgPort.dstPort = pSrc->dstPort;
223 pDest->msgPort.srcPort = pSrc->srcPort;
226 MSG_DEBUG("nSize = %d", pSrc->dataSize);
228 if (pSrc->mainType == MSG_SMS_TYPE){
229 if (pSrc->pData != NULL) {
230 pDest->bTextSms = true;
231 pDest->dataSize = pSrc->dataSize;
233 memset(pDest->msgText, 0x00, sizeof(pDest->msgText));
235 if (pSrc->dataSize > MAX_MSG_TEXT_LEN) {
236 // Save Message Data into File
237 char fileName[MSG_FILENAME_LEN_MAX+1];
238 memset(fileName, 0x00, sizeof(fileName));
240 if(MsgCreateFileName(fileName) == false)
241 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
243 MSG_SEC_DEBUG("Save pSrc->pData into file : size[%d] name[%s]", pDest->dataSize, fileName);
245 if (MsgWriteIpcFile(fileName, (char*)pSrc->pData, pSrc->dataSize) == false)
246 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
248 memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
249 strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
251 pDest->bTextSms = false;
254 if (pDest->encodeType == MSG_ENCODE_8BIT)
255 memcpy(pDest->msgText, pSrc->pData, pSrc->dataSize);
257 strncpy(pDest->msgText, (char*)pSrc->pData, pSrc->dataSize);
260 MSG_DEBUG("pData = %s", pSrc->pData);
262 MSG_DEBUG("pSrc->pData is NULL.");
263 pDest->bTextSms = true;
267 MSG_SEC_DEBUG("msgText = %s", pDest->msgText);
268 } else if (pSrc->mainType == MSG_MMS_TYPE) {
270 pDest->bTextSms = false;
271 pDest->dataSize = pSrc->dataSize;
273 if(pSrc->subType == MSG_READREPLY_MMS) {
274 memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
276 if (pSrc->mmsDataSize < MAX_MSG_DATA_LEN)
277 memcpy(pDest->msgData, pSrc->pMmsData, pSrc->mmsDataSize);
279 memcpy(pDest->msgData, pSrc->pMmsData, MAX_MSG_DATA_LEN);
282 // Save Message Data into File
283 char fileName[MSG_FILENAME_LEN_MAX+1];
284 memset(fileName, 0x00, sizeof(fileName));
286 if(MsgCreateFileName(fileName) == false)
287 THROW(MsgException::FILE_ERROR, "MsgCreateFileName error");
289 // change file extension in case of java MMS msg
290 if (pSrc->subType == MSG_SENDREQ_JAVA_MMS) {
291 char* pFileNameExt = NULL;
292 pFileNameExt = strstr(fileName,"DATA");
294 snprintf(pFileNameExt, strlen("JAVA")+1, "JAVA");
297 MSG_SEC_DEBUG("Save Message Data into file : size[%d] name[%s]", pSrc->mmsDataSize, fileName);
298 if (MsgWriteIpcFile(fileName, (char*)pSrc->pMmsData, pSrc->mmsDataSize) == false)
299 THROW(MsgException::FILE_ERROR, "MsgWriteIpcFile error");
301 memset(pDest->msgData, 0x00, sizeof(pDest->msgData));
302 strncpy(pDest->msgData, fileName, MAX_MSG_DATA_LEN);
304 strncpy(pDest->msgText, (char*)pSrc->pData, MAX_MSG_TEXT_LEN);
307 if (strlen(pSrc->thumbPath) > 0) {
308 memset(pDest->thumbPath, 0x00, sizeof(pDest->thumbPath));
309 memcpy(pDest->thumbPath, pSrc->thumbPath, sizeof(pDest->thumbPath));
314 pDest->sim_idx = pSrc->simIndex;
320 void MsgHandle::convertMsgStruct(const MSG_MESSAGE_INFO_S *pSrc, MSG_MESSAGE_HIDDEN_S *pDest)
324 pDest->msgId = pSrc->msgId;
325 pDest->threadId = pSrc->threadId;
326 pDest->folderId = pSrc->folderId;
327 pDest->mainType = pSrc->msgType.mainType;
328 pDest->subType = pSrc->msgType.subType;
329 pDest->storageId = pSrc->storageId;
331 strncpy(pDest->replyAddress, pSrc->replyAddress, MAX_PHONE_NUMBER_LEN);
332 strncpy(pDest->subject, pSrc->subject, MAX_SUBJECT_LEN);
334 pDest->displayTime = pSrc->displayTime;
335 pDest->networkStatus = pSrc->networkStatus;
336 pDest->encodeType = pSrc->encodeType;
337 pDest->bRead = pSrc->bRead;
338 pDest->bProtected = pSrc->bProtected;
339 pDest->bBackup = pSrc->bBackup;
340 pDest->priority = pSrc->priority;
341 pDest->direction = pSrc->direction;
342 pDest->simIndex = pSrc->sim_idx;
345 pDest->bPortValid = pSrc->msgPort.valid;
347 if (pDest->bPortValid == true) {
348 pDest->dstPort = pSrc->msgPort.dstPort;
349 pDest->srcPort = pSrc->msgPort.srcPort;
352 if(pSrc->thumbPath[0] != '\0')
353 strncpy(pDest->thumbPath, pSrc->thumbPath, MSG_FILEPATH_LEN_MAX);
355 int maxCnt = (pSrc->nAddressCnt > 10)? MAX_TO_ADDRESS_CNT: pSrc->nAddressCnt;
357 pDest->addr_list->nCount = maxCnt;
359 msg_struct_s *addr_info_s = NULL;
360 MSG_ADDRESS_INFO_S *addr_info = NULL;
362 for (int i = 0; i < maxCnt; i++) {
363 addr_info_s = (msg_struct_s *)pDest->addr_list->msg_struct_info[i];
364 addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
366 addr_info->addressType = pSrc->addressList[i].addressType;
367 addr_info->recipientType = pSrc->addressList[i].recipientType;
368 addr_info->contactId = pSrc->addressList[i].contactId;
369 strncpy(addr_info->addressVal, pSrc->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
370 strncpy(addr_info->displayName, pSrc->addressList[i].displayName, MAX_DISPLAY_NAME_LEN);
371 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
374 for (int i = 0; i < pSrc->nAddressCnt; i++) {
375 addr_info_s = new msg_struct_s;
376 memset(addr_info_s, 0x00, sizeof(msg_struct_s));
378 addr_info_s->type = MSG_STRUCT_ADDRESS_INFO;
379 addr_info_s->data = new MSG_ADDRESS_INFO_S;
380 memset(addr_info_s->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
382 addr_info = (MSG_ADDRESS_INFO_S *)addr_info_s->data;
384 addr_info->addressType = pSrc->addressList[i].addressType;
385 addr_info->recipientType = pSrc->addressList[i].recipientType;
386 addr_info->contactId = pSrc->addressList[i].contactId;
387 strncpy(addr_info->addressVal, pSrc->addressList[i].addressVal, MAX_ADDRESS_VAL_LEN);
388 strncpy(addr_info->displayName, pSrc->addressList[i].displayName, MAX_DISPLAY_NAME_LEN);
389 addr_info->displayName[MAX_DISPLAY_NAME_LEN] = '\0';
391 pDest->addressList = g_list_append(pDest->addressList, addr_info_s);
395 if (pSrc->bTextSms == false) {
398 char* pFileData = NULL;
399 unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
401 pDest->dataSize = pSrc->dataSize;
403 // Get Message Data from File
404 if (MsgOpenAndReadFile(pSrc->msgData, &pFileData, &fileSize) == false)
405 THROW(MsgException::FILE_ERROR, "MsgOpenAndReadFile error");
407 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
408 if (pDest->encodeType == MSG_ENCODE_8BIT) {
409 pDest->pData = (void*)new char[fileSize];
410 memset(pDest->pData, 0x00, fileSize);
411 memcpy(pDest->pData, pFileData, fileSize);
413 pDest->pData = (void*)new char[fileSize+1];
414 memset(pDest->pData, 0x00, fileSize+1);
415 strncpy((char*)pDest->pData, pFileData, fileSize);
418 if (pSrc->msgText[0] != '\0') {
419 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
420 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
421 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
423 pDest->mmsDataSize = fileSize;
424 pDest->pMmsData = (void*)new char[fileSize];
425 memset(pDest->pMmsData, 0x00, fileSize);
426 memcpy(pDest->pMmsData, pFileData, fileSize);
427 MSG_SEC_DEBUG("Get Message Data from file : size[%d] name[%s]", pDest->mmsDataSize, pSrc->msgData);
431 pDest->dataSize = pSrc->dataSize;
433 if (pSrc->msgType.mainType == MSG_SMS_TYPE) {
434 if (pDest->encodeType == MSG_ENCODE_8BIT || pSrc->msgType.subType == MSG_ETWS_SMS) {
435 pDest->pData = (void*)new char[pDest->dataSize];
436 memset(pDest->pData, 0x00, pDest->dataSize);
437 memcpy(pDest->pData, pSrc->msgText, pDest->dataSize);
439 pDest->pData = (void*)new char[pDest->dataSize+1];
440 memset(pDest->pData, 0x00, pDest->dataSize+1);
441 strncpy((char*)pDest->pData, pSrc->msgText, pDest->dataSize);
444 if (pSrc->msgText[0] != '\0') {
445 pDest->pData = (void*)new char[strlen(pSrc->msgText)+1];
446 memset(pDest->pData, 0x00, strlen(pSrc->msgText)+1);
447 strncpy((char*)pDest->pData, pSrc->msgText, strlen(pSrc->msgText));
450 pDest->mmsDataSize = pDest->dataSize;
451 pDest->pMmsData = (void*)new char[pDest->dataSize];
452 memset(pDest->pMmsData, 0x00, pDest->dataSize);
453 memcpy(pDest->pMmsData, pSrc->msgData, pDest->dataSize);
461 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_S* pSrc, MSG_SENDINGOPT_INFO_S* pDest, MSG_MESSAGE_TYPE_S msgType)
465 pDest->bSetting = pSrc->bSetting;
467 if (pSrc->bSetting == false) {
468 MSG_DEBUG("No Sending Option");
472 pDest->bDeliverReq = pSrc->bDeliverReq;
473 pDest->bKeepCopy = pSrc->bKeepCopy;
475 MSG_DEBUG("pDest->bSetting = %d", pDest->bSetting);
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 *)pSrc->smsSendOpt;
483 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
486 pDest->option.smsSendOptInfo.bReplyPath = pSms->bReplyPath;
489 } else if (msgType.mainType == MSG_MMS_TYPE) {
490 msg_struct_s *pStruct = (msg_struct_s *)pSrc->mmsSendOpt;
493 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
496 pDest->option.mmsSendOptInfo.priority = pMms->priority;
497 pDest->option.mmsSendOptInfo.bReadReq = pMms->bReadReq;
499 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
500 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
502 if (pMms->expiryTime == 0) {
503 pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_NONE;
504 pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
506 pDest->option.mmsSendOptInfo.expiryTime.type = MMS_TIMETYPE_RELATIVE;
507 pDest->option.mmsSendOptInfo.expiryTime.time = pMms->expiryTime;
510 if (pMms->bUseDeliveryCustomTime == true) {
511 pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = true;
513 pDest->option.mmsSendOptInfo.bUseDeliveryCustomTime = false;
515 pDest->option.mmsSendOptInfo.deliveryTime.type = MMS_TIMETYPE_RELATIVE;
516 pDest->option.mmsSendOptInfo.deliveryTime.time = pMms->deliveryTime;
518 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pDest->option.mmsSendOptInfo.expiryTime.time);
527 void MsgHandle::convertSendOptStruct(const MSG_SENDINGOPT_INFO_S* pSrc, MSG_SENDINGOPT_S* pDest, MSG_MESSAGE_TYPE_S msgType)
531 pDest->bDeliverReq = pSrc->bDeliverReq;
532 pDest->bKeepCopy = pSrc->bKeepCopy;
534 MSG_DEBUG("pDest->bDeliverReq = %d", pDest->bDeliverReq);
535 MSG_DEBUG("pDest->bKeepCopy = %d", pDest->bKeepCopy);
537 if (msgType.mainType == MSG_SMS_TYPE) {
538 msg_struct_s *pStruct = (msg_struct_s *)pDest->smsSendOpt;
539 SMS_SENDINGOPT_S *pSms = (SMS_SENDINGOPT_S *)pStruct->data;
540 pSms->bReplyPath = pSrc->option.smsSendOptInfo.bReplyPath;
541 } else if (msgType.mainType == MSG_MMS_TYPE) {
542 msg_struct_s *pStruct = (msg_struct_s *)pDest->mmsSendOpt;
543 MMS_SENDINGOPT_S *pMms = (MMS_SENDINGOPT_S *)pStruct->data;
544 pMms->priority = pSrc->option.mmsSendOptInfo.priority;
545 pMms->bReadReq = pSrc->option.mmsSendOptInfo.bReadReq;
546 pMms->expiryTime = pSrc->option.mmsSendOptInfo.expiryTime.time;
547 pMms->deliveryTime = pSrc->option.mmsSendOptInfo.deliveryTime.time;
549 MSG_DEBUG("pDest->option.mmsSendOpt.priority = %d", pMms->priority);
550 MSG_DEBUG("pDest->option.mmsSendOpt.bReadReq = %d", pMms->bReadReq);
551 MSG_DEBUG("pDest->option.mmsSendOpt.expiryTime = %d", pMms->expiryTime);
558 int MsgHandle::getSettingCmdSize(MSG_OPTION_TYPE_T optionType)
560 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_OPTION_TYPE_T);
564 case MSG_GENERAL_OPT :
565 cmdSize += sizeof(MSG_GENERAL_OPT_S);
567 case MSG_SMS_SENDOPT :
568 cmdSize += sizeof(MSG_SMS_SENDOPT_S);
571 cmdSize += sizeof(MSG_SMSC_LIST_S);
573 case MSG_MMS_SENDOPT :
574 cmdSize += sizeof(MSG_MMS_SENDOPT_S);
576 case MSG_MMS_RECVOPT :
577 cmdSize += sizeof(MSG_MMS_RECVOPT_S);
579 case MSG_MMS_STYLEOPT :
580 cmdSize += sizeof(MSG_MMS_STYLEOPT_S);
582 case MSG_PUSHMSG_OPT :
583 cmdSize += sizeof(MSG_PUSHMSG_OPT_S);
586 cmdSize += sizeof(MSG_CBMSG_OPT_S);
588 case MSG_VOICEMAIL_OPT :
589 cmdSize += sizeof(MSG_VOICEMAIL_OPT_S);
591 case MSG_MSGSIZE_OPT :
592 cmdSize += sizeof(MSG_MSGSIZE_OPT_S);
600 bool MsgHandle::CheckEventData(char *pEventData)
602 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
604 switch (pEvent->eventType)
606 case MSG_EVENT_PLG_SENT_STATUS_CNF :
607 case MSG_EVENT_PLG_INCOMING_MSG_IND :
608 case MSG_EVENT_PLG_INCOMING_MMS_CONF :
609 case MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND :
610 case MSG_EVENT_PLG_INCOMING_LBS_MSG_IND :
611 case MSG_EVENT_PLG_STORAGE_CHANGE_IND :
612 case MSG_EVENT_PLG_INCOMING_CB_MSG_IND :
613 case MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND :
614 case MSG_EVENT_PLG_REPORT_MSG_INCOMING_IND :