2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 #include <net_connection.h>
21 #include <Commons/Exception.h>
22 #include "ReqReceiverMessage.h"
23 #include "Messaging.h"
24 #include "CallbackMgr.h"
25 #include "MsgServiceHandleMgr.h"
29 #include <msg_storage.h>
35 const char* EMPTY_ID = "0";
41 Sms::Sms(const string& id) :
46 LoggerD("m_id=" << getIdRef());
47 LoggerD("m_msgType=" << getMessageType());
49 if (getIdRef().empty()) {
52 readExistingMessage();
58 Sms::Sms(const std::string& id, const msg_struct_t msg_data) :
63 LoggerD("m_id=" << getIdRef());
64 LoggerD("m_msgType=" << getMessageType());
66 LoggerD("created msg_data : " << msg_data);
67 m_messageData = createNewCopyOfPLatformMsgWithAddressList(msg_data);
68 LoggerD("created m_messageData : " << m_messageData);
70 readConversationId(m_messageData);
71 readRecipientList(m_messageData);
72 readBody(m_messageData);
73 readFolder(m_messageData);
74 readDateTime(m_messageData);
75 readReadStatus(m_messageData);
76 readSize(m_messageData);
77 readMessageStatus(m_messageData);
78 // setMessageStatus(MESSAGE_STATUS_LOADED);
87 // release platform message structure
88 LoggerD("release m_messageData : " << m_messageData);
89 msg_release_struct(&m_messageData);
93 void Sms::update(bool draftsOnly)
95 LoggerD("updating m_id=" << getIdRef());
98 //error if platform message not exists
99 LoggerE("message can not be updated");
100 Throw(WrtDeviceApis::Commons::PlatformException);
103 //update all sms data
104 if (!draftsOnly || getCurrentFolder() == DRAFTBOX) {
108 updateSourceAddress();
113 void Sms::readAllData()
115 if (getIdRef().empty() || (EMPTY_ID == getIdRef())) {
116 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
121 // release platform message if it was created previously
122 msg_release_struct(&m_messageData);
123 m_messageData = NULL;
125 msg_struct_t sendOpt = NULL;
128 // create new platform structure
129 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
130 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
132 msg_message_id_t l_id = convertId(getIdRef());
133 LoggerD("reading message id=" << l_id);
134 // trying to get message from platform
135 if (MSG_SUCCESS == msg_get_message(MsgGetCommonHandle(), l_id, m_messageData, sendOpt))
137 LoggerD("message found with msgId=" << getIdRef());
139 // read all mms dat to abstraction layer
140 readConversationId(m_messageData);
141 // readRecipientList(m_messageData);
142 readBody(m_messageData);
143 readFolder(m_messageData);
144 readDateTime(m_messageData);
145 readReadStatus(m_messageData);
146 readSize(m_messageData);
147 readMessageStatus(m_messageData);
150 LoggerE("message not found with msgId=" << getIdRef());
152 Throw(WrtDeviceApis::Commons::PlatformException);
155 msg_release_struct(&sendOpt);
157 Catch(WrtDeviceApis::Commons::PlatformException) {
158 LoggerE("exception");
160 msg_release_struct(&sendOpt);
165 void Sms::moveToFolder(const FolderType newFolder)
172 const msg_folder_id_t platfromFolderId =
173 Messaging::convertFolderToPlatform(newFolder);
175 //call platform api to move message to other folder
176 if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
177 platfromFolderId) != MSG_SUCCESS) {
178 Throw(WrtDeviceApis::Commons::PlatformException);
182 Catch(WrtDeviceApis::Commons::PlatformException) {
183 LoggerE("remove message error");
189 void Sms::moveToFolder(const std::string& newFolder)
195 void Sms::copyToFolder(const FolderType newFolder)
200 msg_message_t msg = msg_new_message();
204 MSG_SENDINGOPT_S option = { false, false, false };
205 option.option.smsSendOpt.bReplyPath = true;
206 // trying to get message from platform
208 msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
210 const msg_folder_id_t platfromFolderId =
211 Messaging::convertFolderToPlatform(newFolder);
212 // new id will me set (initialize to 0)
213 msg_set_message_id(msg, 0);
214 msg_set_folder_id(msg, platfromFolderId);
216 // trying to add message
217 int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
218 if (error != MSG_SUCCESS) {
219 LoggerE("msg_add_message failed, error code=" << error);
220 Throw(WrtDeviceApis::Commons::PlatformException);
223 //releasing platform message structure
224 msg_release_message(&msg);
228 Catch(WrtDeviceApis::Commons::PlatformException) {
229 LoggerE("remove message error");
231 //releasing platform message structure
232 msg_release_message(&msg);
239 void Sms::copyToFolder(const std::string& newFolder)
247 LoggerD("sending message, id=" << getIdRef());
249 if(m_messageData == NULL)
251 //prepare for sending sms
254 MsgGetCommonHandle();
256 Catch(WrtDeviceApis::Commons::PlatformException)
258 LoggerD("getHandle failed");
259 Throw(WrtDeviceApis::Commons::PlatformException);
265 msg_struct_list_s *addr_list = NULL;
266 msg_struct_list_s *new_addr_list = NULL;
267 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
272 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
273 msg_get_int_value(m_messageData, MSG_MESSAGE_ID_INT, &tempIdInt);
275 nToCnt = addr_list->nCount;
276 LoggerD("nToCnt : " << nToCnt);
278 for (int i = 0; i < nToCnt; ++i)
280 msg_struct_t new_msg = createNewCopyOfPLatformMsg(m_messageData);
281 msg_set_int_value(new_msg, MSG_MESSAGE_ID_INT, tempIdInt);
283 msg_get_list_handle(new_msg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&new_addr_list);
285 msg_get_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
286 LoggerD("RECIPIENT_TYPE : " << tempInt);
287 if(MSG_RECIPIENTS_TYPE_TO != tempInt)
293 msg_get_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
294 msg_set_str_value(new_addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
295 msg_set_int_value(new_addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_TO);
296 new_addr_list->nCount = 1;
298 setMessageStatus(MESSAGE_STATUS_SENDING);
300 req.queue.push(new_msg);
304 DPL::Mutex::ScopedLock lock(&m_mutex);
305 m_sendRequests.push(req);
310 LoggerD("sending method finished");
312 return 0; //sms handle is -1;
315 void Sms::sendingCallback(msg_struct_t sent_status)
318 // "sendingCallback callback received. Req id = " <<
319 // sent_status->reqId << " Status = " << (int)sent_status->status <<
320 // ", Recipient=" << getRecipient());
321 int status = MSG_NETWORK_SEND_FAIL;
323 msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
325 DPL::Mutex::ScopedLock lock(&m_mutex);
327 if (status != MSG_NETWORK_SEND_SUCCESS) {
328 m_sendRequests.front().failed = true; // TODO mutex
329 //send callback, error for recipient
330 LoggerD("Send failed with error " << status);
331 setSendingStatusFailed(getRecipient());
332 setMessageStatus(MESSAGE_STATUS_FAILED);
334 setSendingStatusOk(getRecipient());
335 setMessageStatus(MESSAGE_STATUS_SENT);
338 msg_release_struct(¤tQueue().front());
339 currentQueue().pop();
341 //schedule another sub message send
345 void Sms::OnEventReceived(const int&)
352 FolderType Sms::toFolder(const std::string &folder)
354 if (folder == "INBOX") {
356 } else if (folder == "OUTBOX") {
358 } else if (folder == "SENTBOX") {
360 } else if (folder == "DRAFTBOX") {
363 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");
366 bool Sms::getCellularOn()
368 auto cellularOn = true;
369 void* connectionHandle;
370 if (CONNECTION_ERROR_NONE == connection_create(&connectionHandle)) {
371 connection_cellular_state_e cellularState;
372 if (CONNECTION_ERROR_NONE == connection_get_cellular_state (connectionHandle,
374 if (CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE == cellularState || CONNECTION_CELLULAR_STATE_FLIGHT_MODE == cellularState) {
376 LoggerW("Cellular State Error" << cellularState);
379 LoggerE("Can't obtain state of cellular connection");
381 connection_destroy(connectionHandle);
383 LoggerE("Can't obtain state of cellular connection");
388 bool Sms::getFlightModeOn()
391 if (vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &flightMode) != 0) {
393 LoggerW("Can't read flight mode from variable: "
394 << VCONFKEY_TELEPHONY_FLIGHT_MODE);
397 LoggerW("Flight Mode active. All network operations will fail.");
402 void Sms::sendSubMessage()
406 DPL::Mutex::ScopedLock lock(&m_mutex);
408 // if current request queue gets empty, try next one
409 while (!m_sendRequests.empty()) {
410 // send as long as the sending fails until there are no more messages
411 for (msg_error_t err = MSG_ERR_UNKNOWN;
412 err != MSG_SUCCESS && !currentQueue().empty(); )
414 if (getFlightModeOn() || !getCellularOn()) {
415 err = MSG_ERR_TRANSPORT_ERROR;
416 LoggerW("SMS send error. No transport possible over network");
418 err = CallbackMgrSingleton::Instance().registerAndSend(
419 msg_sms_send_message,
420 currentQueue().front(),
423 LoggerI("Sending Message (submit request) result:" << err);
424 if (err != MSG_SUCCESS) {
425 LoggerD("Sending Message (submit request) failed!!! err=" << err);
426 msg_release_struct(¤tQueue().front());
427 currentQueue().pop();
428 m_sendRequests.front().failed = true;
429 // send callback, error for recipient
430 // TODO: Add error processing based at err
431 if (MSG_ERR_TRANSPORT_ERROR == err || MSG_ERR_NO_SIM == err) {
432 setSendingStatusFailed(getRecipient(),
433 EventOnSendingFailed::NO_CONNECTION);
435 setSendingStatusFailed(getRecipient());
440 // empty queue -> notify about results
441 if (currentQueue().empty()) {
443 if (m_sendRequests.front().failed) {
444 setSendingStatusFailed();
446 setSendingStatusOk();
449 // this request is finished, try next one
450 m_sendRequests.pop();
457 void Sms::setSendingStatusOk(const string &recipient)
459 LoggerD("enter, success number is : " << recipient);
460 //success callback should be executed here
461 ReqReceiverMessage *requestReceiver = getRequestReceiver();
462 if (requestReceiver) {
463 EventMessagingServicePtr event = getMessagingServiceEvent();
464 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
465 event->m_successRecipients.push_back(recipient);
467 msg_struct_list_s *addr_list = NULL;
468 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
470 if ( event->m_successRecipients.size() + event->m_failRecipients.size() == (unsigned int)addr_list->nCount)
472 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
476 EventSendMessagePtr event = getSendMessageEvent();
477 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
478 event->m_successRecipients.push_back(recipient);
479 //LoggerD("total address size : " << msg_get_address_count(m_messageData) << " Status size : " << event->m_successRecipients.size() + event->m_failRecipients.size());
480 if ( event->m_successRecipients.size() + event->m_failRecipients.size() == (unsigned int)msg_get_address_count(m_messageData))
482 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventSendMessage >::ManualAnswer(event);
489 void Sms::setSendingStatusFailed(const string &recipient,
490 EventOnSendingFailed::ErrorCode error)
494 using namespace WrtDeviceApis::Commons;
495 LoggerD("enter, fail number is : " << recipient);
496 //error callback should be executed here
497 EventOnSendingFailedEmitterPtr emitter = getEmitter();
499 EventOnSendingFailedPtr event(new EventOnSendingFailed);
500 event->setError(error);
501 emitter->emit(event);
503 ReqReceiverMessage* requestReceiver = getRequestReceiver();
504 if (requestReceiver) {
505 LoggerD("calling JS error callback");
506 EventMessagingServicePtr event = getMessagingServiceEvent();
507 auto errorCode = ExceptionCodes::UnknownException;
509 case EventOnSendingFailed::NO_NETWORKING:
510 errorCode = ExceptionCodes::PlatformWrongStateException;
512 case EventOnSendingFailed::NO_CONNECTION:
513 errorCode = ExceptionCodes::PlatformWrongStateException;
515 case EventOnSendingFailed::BAD_PAYLOAD:
516 errorCode = ExceptionCodes::InvalidArgumentException;
518 case EventOnSendingFailed::UNKNOWN:
519 errorCode = ExceptionCodes::UnknownException;
522 LoggerW("Unknown error code for send fail");
524 LoggerD("Send error " << error << " converted to " << errorCode);
525 event->setExceptionCode(errorCode);
526 event->m_successRecipients.push_back(recipient);
527 msg_struct_list_s *addr_list = NULL;
528 msg_get_list_handle(m_messageData,
529 MSG_MESSAGE_ADDR_LIST_STRUCT,
531 auto successSize = event->m_successRecipients.size();
532 auto failedSize = event->m_failRecipients.size();
533 auto originalSize = (unsigned int)addr_list->nCount;
534 if (successSize + failedSize == originalSize) {
535 LoggerD("Manual answer");
536 requestReceiver->EventRequestReceiver< EventMessagingService >::
547 // delete message from platform
548 if (msg_delete_message(MsgGetCommonHandle(),
549 convertId(getId())) != MSG_SUCCESS) {
550 LoggerE("delete message failed");
551 Throw(WrtDeviceApis::Commons::PlatformException);
553 LoggerD("message deleted succesfully");
557 Catch(WrtDeviceApis::Commons::PlatformException) {
558 LoggerE("remove message error");
563 void Sms::createNewMessage()
567 // release platform message if it was created previously
568 msg_release_struct(&m_messageData);
569 m_messageData = NULL;
571 // create new platform message
572 LoggerD("create messeage instance");
573 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
574 LoggerD("created message : " << m_messageData);
578 //default message initialization
583 setMessageStatus(MESSAGE_STATUS_DRAFT);
585 tm dateT = getDateTime();
586 msg_set_int_value(m_messageData, MSG_MESSAGE_DISPLAY_TIME_INT, (int)mktime(&dateT));
588 setMessageStatus(MESSAGE_STATUS_CREATED);
589 LoggerD("Message created successfully, msgId=" << getId());
591 Catch(WrtDeviceApis::Commons::PlatformException) {
592 msg_release_struct(&m_messageData);
593 m_messageData = NULL;
594 LoggerE("Problem with message creation, cleaning");
598 void Sms::readExistingMessage()
600 if (getIdRef().empty() || (EMPTY_ID == getIdRef())) {
601 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
606 // release platform message if it was created previously
607 msg_release_struct(&m_messageData);
608 m_messageData = NULL;
610 msg_struct_t sendOpt = NULL;
613 // create new platform structure
614 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
615 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
617 msg_message_id_t l_id = convertId(getIdRef());
618 LoggerD("reading message id=" << l_id);
619 // trying to get message from platform
620 if (MSG_SUCCESS == msg_get_message(MsgGetCommonHandle(), l_id, m_messageData, sendOpt))
622 LoggerD("message found with msgId=" << getIdRef());
624 // read all mms dat to abstraction layer
625 readConversationId(m_messageData);
626 readRecipientList(m_messageData);
627 readBody(m_messageData);
628 readFolder(m_messageData);
629 readDateTime(m_messageData);
630 readReadStatus(m_messageData);
631 readSize(m_messageData);
632 readMessageStatus(m_messageData);
635 LoggerE("message not found with msgId=" << getIdRef());
637 Throw(WrtDeviceApis::Commons::PlatformException);
640 msg_release_struct(&sendOpt);
642 Catch(WrtDeviceApis::Commons::PlatformException) {
643 LoggerE("exception");
645 msg_release_struct(&sendOpt);
649 void Sms::updateBody()
651 // check if abstraction body value has been changed
655 LoggerI("updating platfoprm body");
657 // change body value in platform structure
658 if ( getBodyRef().length() > 0)
660 if (msg_set_str_value(m_messageData, MSG_MESSAGE_SMS_DATA_STR,
661 const_cast<char*>(getBodyRef().c_str()), getBodyRef().length()) != MSG_SUCCESS)
663 LoggerE("problem with update Body");
664 Throw(WrtDeviceApis::Commons::PlatformException);
666 // set flag as true - platform synchronized with abstraction
667 setBodyValidity(true);
669 // set flag as true - platform synchronized with abstraction
674 // check if abstraction recipient value has been changed
675 if (getToRecipients().isValid()) {
679 LoggerI("SMS updating platform recipients");
681 msg_struct_list_s *addr_list = NULL;
682 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
683 int nToCnt = addr_list->nCount;
685 LogInfo("nToCnt size " << nToCnt);
689 for (int i = 0; i < nToCnt; i++) {
690 msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, (char *)"", 0);
691 msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_DISPLAYNAME_STR, (char *)"", 0);
692 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
693 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_UNKNOWN);
694 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_CONTACT_ID_INT, 0);
697 addr_list->nCount = nToCnt;
700 vector<string> to = getToRecipients().getRecipients();
702 // update addresses in platform structure
705 LoggerI("updating to");
706 for (size_t i = 0; i < to.size(); i++)
708 string tmpAddr = to[i];
709 if(validatePhoneNumber(tmpAddr))
711 if (i >= MAX_TO_ADDRESS_CNT)
713 LoggerE("max number of recipient exceeded");
716 LoggerD("adding to[" << i << "]=" << to[i]);
718 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
720 if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT,
721 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS)
723 LoggerE("problem with adding to address");
724 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
727 if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
728 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS)
730 LoggerE("problem with adding to address");
731 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
734 addr_list->nCount ++;
735 LoggerD("addr_list->nCount =" << addr_list->nCount);
739 LoggerE("wrong phone number format");
740 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "wrong phone number format");
749 void Sms::updateFrom()
751 // check if abstraction from field value has been changed
752 if (getFromValidity()) {
753 // do not update if not changed
756 LoggerI("updating platform from");
760 // set flag as true - platform synchronized with abstraction
761 setFromValidity(true);
764 void Sms::updateSourceAddress()
766 // check if abstraction source address value has been changed
767 if (getSourceAddressValidity()) {
768 // do not update if not changed
771 LoggerI("updating platform source address");
775 // set flag as true - platform synchronized with abstraction
776 setSourceAddressValidity(true);
779 void Sms::updateReadStatus()
781 LoggerI("updating platform read status: " << isRead());
782 if (isReadStatusValid()) {
783 // do not update if not changed
786 if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
787 LoggerE("problem with setting subject");
788 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
790 setReadStatusValidity(true);
793 void Sms::updateIsRead()
795 LoggerD("updating m_id=" << getIdRef());
797 if (!m_messageData) {
798 //error if platform message not exists
799 LoggerE("message can not be updated");
800 Throw(WrtDeviceApis::Commons::PlatformException);
803 // check if abstraction from m_isReadChange value has been changed
804 if (isReadChangeStatusValid()) {
805 // do not update if not changed
811 if (this->getIdRef().empty()) {
812 LoggerE("existing msgId is zero, remove msg not done");
813 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
814 "existing msgId is zero, remove msg not done");
818 msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
820 LoggerE("Failed to update isRead");
821 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
824 Catch(WrtDeviceApis::Commons::PlatformException) {
825 LoggerE("platform error occurs");
827 setisReadChangeStatusValidity(true);
831 void Sms::updateMessage()
833 LoggerD("updating m_id=" << getIdRef());
834 msg_error_t err = MSG_SUCCESS;
836 msg_struct_t msg = NULL;
837 msg_struct_t sendOpt = NULL;
839 if (!m_messageData) {
840 //error if platform message not exists
841 LoggerE("message can not be updated");
842 Throw(WrtDeviceApis::Commons::PlatformException);
848 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
849 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
851 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
853 if (err != MSG_SUCCESS)
855 LoggerE("Get Message Failed!");
856 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
859 if (this->getIdRef().empty()) {
860 LoggerE("existing msgId is zero, update msg not done");
861 ThrowMsg(WrtDeviceApis::Commons::PlatformException,
862 "existing msgId is zero, update msg not done");
867 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
869 LoggerE("Failed to update message");
870 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
872 msg_release_struct(&msg);
873 msg_release_struct(&sendOpt);
875 Catch(WrtDeviceApis::Commons::PlatformException) {
876 msg_release_struct(&msg);
877 msg_release_struct(&sendOpt);
878 LoggerE("platform error occurs");
882 void Sms::createSendMessage()
884 LoggerD("convert m_id= " << convertId(getIdRef()));
886 //prepare for add sms to draft folder
887 if (!m_messageData) {
888 //error if platform message not exists
889 LoggerE("message can not be updated");
890 Throw(WrtDeviceApis::Commons::PlatformException);
893 //update all sms data
894 if (getCurrentFolder() == DRAFTBOX) {
895 LoggerD("update all sms data");
899 updateSourceAddress();
905 // MSG_SENDINGOPT_S option = { false, false, false };
906 // option.option.smsSendOpt.bReplyPath = true;
907 // trying to get message from platform
909 // const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
911 // msg_set_message_id(m_messageData, msgId);
912 // msg_set_folder_id(m_messageData, platfromFolderId);
913 // msg_set_network_status(m_messageData, MSG_NETWORK_NOT_SEND);
915 setId(convertId(msgId));
916 setFolderType(DRAFTBOX);
917 setMessageStatus(MESSAGE_STATUS_DRAFT);
919 Catch(WrtDeviceApis::Commons::PlatformException) {
920 LoggerE("remove message error");
922 //releasing platform message structure
923 msg_release_struct(&m_messageData);
924 m_messageData = NULL;
933 void Sms::addMessageToDraft()
935 LoggerD("convert m_id= " << convertId(getIdRef()));
936 msg_struct_t sendOpt = NULL;
938 //prepare for add sms to draft folder
939 if (!m_messageData) {
940 //error if platform message not exists
941 LoggerE("message can not be updated");
942 Throw(WrtDeviceApis::Commons::PlatformException);
945 //update all sms data
946 if (getCurrentFolder() == DRAFTBOX) {
947 LoggerD("update all sms data");
951 updateSourceAddress();
956 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
957 msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
960 // trying to add message
961 int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
962 if(ret < MSG_SUCCESS) {
963 LoggerE("msg_add_message failed, error code=" << ret);
964 Throw(WrtDeviceApis::Commons::PlatformException);
971 LoggerD("Message ID : " << msgId);
974 LoggerD("Message ID is invailded ");
975 Throw(WrtDeviceApis::Commons::PlatformException);
978 msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
980 setId(convertId(msgId));
983 msg_release_struct(&sendOpt);
986 Catch(WrtDeviceApis::Commons::PlatformException) {
987 LoggerE("remove message error");
989 //releasing platform message structure
990 msg_release_struct(&m_messageData);
991 m_messageData = NULL;
992 msg_release_struct(&sendOpt);
999 void Sms::readConversationId(msg_struct_t& messageData)
1002 msg_get_int_value(messageData, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
1003 LoggerD("conversationID : " <<tempInt);
1007 void Sms::readRecipientList(msg_struct_t& messageData)
1009 std::string phoneNumber;
1010 // int recipientCount = msg_get_address_count(messageData);
1011 // LoggerD("Recipient count " << recipientCount);
1013 msg_struct_list_s *addr_list;
1014 msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1017 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1019 int recipientCount = addr_list->nCount;
1020 LoggerD("Recipient count " << recipientCount);
1023 for (int i = 0; i < recipientCount; ++i)
1025 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1028 if (MSG_DIRECTION_TYPE_MT == type)
1030 msg_struct_t addr_info = addr_list->msg_struct_info[i];
1031 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1032 phoneNumber = strNumber;
1034 if (validatePhoneNumber(phoneNumber))
1036 setSourceAddress(phoneNumber);
1037 setFrom(phoneNumber);
1039 LoggerD("MT number: " << phoneNumber);
1041 else if (MSG_DIRECTION_TYPE_MO == type)
1043 msg_struct_t addr_info = addr_list->msg_struct_info[i];
1044 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1045 LoggerD("MO number: " << strNumber);
1046 appendToRecipients(strNumber);
1050 LoggerE("Wrong type of recipient");
1051 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
1056 void Sms::readBody(msg_struct_t& messageData)
1058 //set abstraction body value
1060 msg_get_int_value(messageData, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1061 LoggerE("body Size : " <<tempInt);
1062 char msgText[tempInt+1];
1063 memset(msgText, 0, tempInt+1);
1064 msg_get_str_value(messageData, MSG_MESSAGE_SMS_DATA_STR, msgText, tempInt);
1065 // setBody(msg_sms_get_message_body(messageData));
1066 LoggerE("body : " <<msgText);
1070 void Sms::readFolder(msg_struct_t& messageData)
1073 msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1074 LoggerE("Folder : " <<tempInt);
1078 setFolderType(INBOX);
1081 setFolderType(OUTBOX);
1083 case MSG_SENTBOX_ID:
1084 setFolderType(SENTBOX);
1087 setFolderType(DRAFTBOX);
1090 LoggerE("Wrong folder id");
1091 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Unsupported folder id.");
1095 void Sms::readMessageStatus(msg_struct_t& messageData)
1098 msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
1099 LoggerD("readMessageStatus folder : " << tempValue);
1101 switch (tempValue) {
1103 setMessageStatus(MESSAGE_STATUS_LOADED);
1106 setMessageStatus(MESSAGE_STATUS_SENDING);
1108 case MSG_SENTBOX_ID:
1109 setMessageStatus(MESSAGE_STATUS_SENT);
1112 setMessageStatus(MESSAGE_STATUS_DRAFT);
1115 setMessageStatus(MESSAGE_STATUS_LOADED);
1121 void Sms::readDateTime(msg_struct_t& messageData)
1123 // tm* time = localtime(msg_get_time(messageData));
1124 // tm dateT = getDateTime();
1126 msg_get_int_value(messageData, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1128 LoggerE("readDateTime : " <<tempInt);
1130 tm* time = localtime((time_t*)&tempInt);
1132 LoggerE("localtime failed");
1133 Throw(WrtDeviceApis::Commons::PlatformException);
1138 void Sms::readReadStatus(msg_struct_t& messageData)
1142 msg_get_bool_value(messageData, MSG_MESSAGE_READ_BOOL, &tempBool);
1143 LoggerE("readReadStatus : " <<tempBool);
1144 setReadStatus(tempBool);
1147 void Sms::readSize(msg_struct_t& messageData)
1150 msg_get_int_value(messageData, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1151 // setSize(msg_get_message_body_size(messageData));
1152 LoggerE("readSize : " <<tempInt);
1156 msg_struct_t Sms::createNewCopyOfPLatformMsg(const msg_struct_t src) const
1161 msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1163 msg_get_int_value(src, MSG_MESSAGE_STORAGE_ID_INT, &tempInt);
1164 msg_set_int_value(msg, MSG_MESSAGE_STORAGE_ID_INT, tempInt);
1166 msg_get_int_value(src, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1167 msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1169 msg_get_int_value(src, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1170 char tempStr[tempInt+1];
1171 memset(tempStr, 0, tempInt+1);
1173 msg_get_str_value(src, MSG_MESSAGE_SMS_DATA_STR, tempStr, tempInt);
1174 msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, tempStr, strlen(tempStr));
1176 msg_get_int_value(src, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1177 msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, tempInt);
1179 // msg_set_network_status(msg, msg_get_network_status(src));
1180 msg_get_int_value(src, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1181 msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, tempInt);
1183 // msg_set_encode_type(msg, msg_get_encode_type(src));
1184 msg_get_int_value(src, MSG_MESSAGE_ENCODE_TYPE_INT, &tempInt);
1185 msg_set_int_value(msg, MSG_MESSAGE_ENCODE_TYPE_INT, tempInt);
1187 // msg_set_read_status(msg, msg_is_read(src));
1188 msg_get_bool_value(src, MSG_MESSAGE_READ_BOOL, &tempBool);
1189 msg_set_bool_value(msg, MSG_MESSAGE_READ_BOOL, tempBool);
1191 // msg_set_protect_status(msg, msg_is_protected(src));
1192 msg_get_bool_value(src, MSG_MESSAGE_PROTECTED_BOOL, &tempBool);
1193 msg_set_bool_value(msg, MSG_MESSAGE_PROTECTED_BOOL, tempBool);
1195 // msg_set_priority_info(msg, msg_get_priority_info(src));
1196 msg_get_int_value(src, MSG_MESSAGE_PRIORITY_INT, &tempInt);
1197 msg_set_int_value(msg, MSG_MESSAGE_PRIORITY_INT, tempInt);
1199 // msg_set_direction_info(msg, msg_get_direction_info(src));
1200 msg_get_int_value(src, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1201 msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT, tempInt);
1203 // msg_set_port(msg, msg_get_dest_port(src), msg_get_src_port(src));
1204 msg_get_int_value(src, MSG_MESSAGE_DEST_PORT_INT, &tempInt);
1205 msg_set_int_value(msg, MSG_MESSAGE_DEST_PORT_INT, tempInt);
1206 msg_get_int_value(src, MSG_MESSAGE_SRC_PORT_INT, &tempInt);
1207 msg_set_int_value(msg, MSG_MESSAGE_SRC_PORT_INT, tempInt);
1212 msg_struct_t Sms::createNewCopyOfPLatformMsgWithAddressList(const msg_struct_t src) const
1216 msg_struct_list_s *addr_list;
1217 msg_struct_list_s *new_addr_list;
1219 msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1221 msg_get_int_value(src, MSG_MESSAGE_STORAGE_ID_INT, &tempInt);
1222 msg_set_int_value(msg, MSG_MESSAGE_STORAGE_ID_INT, tempInt);
1224 msg_get_int_value(src, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1225 msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1227 msg_get_int_value(src, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1228 char tempStr[tempInt+1];
1229 memset(tempStr, 0, tempInt+1);
1231 msg_get_str_value(src, MSG_MESSAGE_SMS_DATA_STR, tempStr, tempInt);
1232 msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, tempStr, strlen(tempStr));
1234 msg_get_int_value(src, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1235 msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, tempInt);
1237 // msg_set_network_status(msg, msg_get_network_status(src));
1238 msg_get_int_value(src, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1239 msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, tempInt);
1241 // msg_set_encode_type(msg, msg_get_encode_type(src));
1242 msg_get_int_value(src, MSG_MESSAGE_ENCODE_TYPE_INT, &tempInt);
1243 msg_set_int_value(msg, MSG_MESSAGE_ENCODE_TYPE_INT, tempInt);
1245 // msg_set_read_status(msg, msg_is_read(src));
1246 msg_get_bool_value(src, MSG_MESSAGE_READ_BOOL, &tempBool);
1247 msg_set_bool_value(msg, MSG_MESSAGE_READ_BOOL, tempBool);
1249 // msg_set_protect_status(msg, msg_is_protected(src));
1250 msg_get_bool_value(src, MSG_MESSAGE_PROTECTED_BOOL, &tempBool);
1251 msg_set_bool_value(msg, MSG_MESSAGE_PROTECTED_BOOL, tempBool);
1253 // msg_set_priority_info(msg, msg_get_priority_info(src));
1254 msg_get_int_value(src, MSG_MESSAGE_PRIORITY_INT, &tempInt);
1255 msg_set_int_value(msg, MSG_MESSAGE_PRIORITY_INT, tempInt);
1257 // msg_set_direction_info(msg, msg_get_direction_info(src));
1258 msg_get_int_value(src, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1259 msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT, tempInt);
1261 // msg_set_port(msg, msg_get_dest_port(src), msg_get_src_port(src));
1262 msg_get_int_value(src, MSG_MESSAGE_DEST_PORT_INT, &tempInt);
1263 msg_set_int_value(msg, MSG_MESSAGE_DEST_PORT_INT, tempInt);
1264 msg_get_int_value(src, MSG_MESSAGE_SRC_PORT_INT, &tempInt);
1265 msg_set_int_value(msg, MSG_MESSAGE_SRC_PORT_INT, tempInt);
1268 msg_get_list_handle(src, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1269 msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&new_addr_list);
1271 new_addr_list->nCount = addr_list->nCount;
1273 for(int i=0; i<addr_list->nCount; i++)
1275 msg_struct_t addr_info = NULL;
1276 msg_struct_t new_addr_info = NULL;
1277 char addr_value[MAX_ADDRESS_VAL_LEN] = {0,};
1279 //get original address
1280 addr_info = addr_list->msg_struct_info[i];
1281 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
1282 msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
1284 //copy original address
1285 new_addr_info = new_addr_list->msg_struct_info[i];
1286 msg_set_str_value(new_addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
1287 msg_set_int_value(new_addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, tempInt);