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");
879 ReThrow(WrtDeviceApis::Commons::PlatformException);
883 void Sms::createSendMessage()
885 LoggerD("convert m_id= " << convertId(getIdRef()));
887 //prepare for add sms to draft folder
888 if (!m_messageData) {
889 //error if platform message not exists
890 LoggerE("message can not be updated");
891 Throw(WrtDeviceApis::Commons::PlatformException);
894 //update all sms data
895 if (getCurrentFolder() == DRAFTBOX) {
896 LoggerD("update all sms data");
900 updateSourceAddress();
906 // MSG_SENDINGOPT_S option = { false, false, false };
907 // option.option.smsSendOpt.bReplyPath = true;
908 // trying to get message from platform
910 // const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
912 // msg_set_message_id(m_messageData, msgId);
913 // msg_set_folder_id(m_messageData, platfromFolderId);
914 // msg_set_network_status(m_messageData, MSG_NETWORK_NOT_SEND);
916 setId(convertId(msgId));
917 setFolderType(DRAFTBOX);
918 setMessageStatus(MESSAGE_STATUS_DRAFT);
920 Catch(WrtDeviceApis::Commons::PlatformException) {
921 LoggerE("remove message error");
923 //releasing platform message structure
924 msg_release_struct(&m_messageData);
925 m_messageData = NULL;
934 void Sms::addMessageToDraft()
936 LoggerD("convert m_id= " << convertId(getIdRef()));
937 msg_struct_t sendOpt = NULL;
939 //prepare for add sms to draft folder
940 if (!m_messageData) {
941 //error if platform message not exists
942 LoggerE("message can not be updated");
943 Throw(WrtDeviceApis::Commons::PlatformException);
946 //update all sms data
947 if (getCurrentFolder() == DRAFTBOX) {
948 LoggerD("update all sms data");
952 updateSourceAddress();
957 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
958 msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
961 // trying to add message
962 int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
963 if(ret < MSG_SUCCESS) {
964 LoggerE("msg_add_message failed, error code=" << ret);
965 Throw(WrtDeviceApis::Commons::PlatformException);
972 LoggerD("Message ID : " << msgId);
975 LoggerD("Message ID is invailded ");
976 Throw(WrtDeviceApis::Commons::PlatformException);
979 msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
981 setId(convertId(msgId));
984 msg_release_struct(&sendOpt);
987 Catch(WrtDeviceApis::Commons::PlatformException) {
988 LoggerE("remove message error");
990 //releasing platform message structure
991 msg_release_struct(&m_messageData);
992 m_messageData = NULL;
993 msg_release_struct(&sendOpt);
1000 void Sms::readConversationId(msg_struct_t& messageData)
1003 msg_get_int_value(messageData, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
1004 LoggerD("conversationID : " <<tempInt);
1008 void Sms::readRecipientList(msg_struct_t& messageData)
1010 std::string phoneNumber;
1011 // int recipientCount = msg_get_address_count(messageData);
1012 // LoggerD("Recipient count " << recipientCount);
1014 msg_struct_list_s *addr_list;
1015 msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1018 char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1020 int recipientCount = addr_list->nCount;
1021 LoggerD("Recipient count " << recipientCount);
1024 for (int i = 0; i < recipientCount; ++i)
1026 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1029 if (MSG_DIRECTION_TYPE_MT == type)
1031 msg_struct_t addr_info = addr_list->msg_struct_info[i];
1032 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1033 phoneNumber = strNumber;
1035 if (validatePhoneNumber(phoneNumber))
1037 setSourceAddress(phoneNumber);
1038 setFrom(phoneNumber);
1040 LoggerD("MT number: " << phoneNumber);
1042 else if (MSG_DIRECTION_TYPE_MO == type)
1044 msg_struct_t addr_info = addr_list->msg_struct_info[i];
1045 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1046 LoggerD("MO number: " << strNumber);
1047 appendToRecipients(strNumber);
1051 LoggerE("Wrong type of recipient");
1052 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
1057 void Sms::readBody(msg_struct_t& messageData)
1059 //set abstraction body value
1061 msg_get_int_value(messageData, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1062 LoggerE("body Size : " <<tempInt);
1063 char msgText[tempInt+1];
1064 memset(msgText, 0, tempInt+1);
1065 msg_get_str_value(messageData, MSG_MESSAGE_SMS_DATA_STR, msgText, tempInt);
1066 // setBody(msg_sms_get_message_body(messageData));
1067 LoggerE("body : " <<msgText);
1071 void Sms::readFolder(msg_struct_t& messageData)
1074 msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1075 LoggerE("Folder : " <<tempInt);
1079 setFolderType(INBOX);
1082 setFolderType(OUTBOX);
1084 case MSG_SENTBOX_ID:
1085 setFolderType(SENTBOX);
1088 setFolderType(DRAFTBOX);
1091 LoggerE("Wrong folder id");
1092 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Unsupported folder id.");
1096 void Sms::readMessageStatus(msg_struct_t& messageData)
1099 msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
1100 LoggerD("readMessageStatus folder : " << tempValue);
1102 switch (tempValue) {
1104 setMessageStatus(MESSAGE_STATUS_LOADED);
1107 setMessageStatus(MESSAGE_STATUS_SENDING);
1109 case MSG_SENTBOX_ID:
1110 setMessageStatus(MESSAGE_STATUS_SENT);
1113 setMessageStatus(MESSAGE_STATUS_DRAFT);
1116 setMessageStatus(MESSAGE_STATUS_LOADED);
1122 void Sms::readDateTime(msg_struct_t& messageData)
1124 // tm* time = localtime(msg_get_time(messageData));
1125 // tm dateT = getDateTime();
1127 msg_get_int_value(messageData, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1129 LoggerE("readDateTime : " <<tempInt);
1131 tm* time = localtime((time_t*)&tempInt);
1133 LoggerE("localtime failed");
1134 Throw(WrtDeviceApis::Commons::PlatformException);
1139 void Sms::readReadStatus(msg_struct_t& messageData)
1143 msg_get_bool_value(messageData, MSG_MESSAGE_READ_BOOL, &tempBool);
1144 LoggerE("readReadStatus : " <<tempBool);
1145 setReadStatus(tempBool);
1148 void Sms::readSize(msg_struct_t& messageData)
1151 msg_get_int_value(messageData, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1152 // setSize(msg_get_message_body_size(messageData));
1153 LoggerE("readSize : " <<tempInt);
1157 msg_struct_t Sms::createNewCopyOfPLatformMsg(const msg_struct_t src) const
1162 msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1164 msg_get_int_value(src, MSG_MESSAGE_STORAGE_ID_INT, &tempInt);
1165 msg_set_int_value(msg, MSG_MESSAGE_STORAGE_ID_INT, tempInt);
1167 msg_get_int_value(src, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1168 msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1170 msg_get_int_value(src, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1171 char tempStr[tempInt+1];
1172 memset(tempStr, 0, tempInt+1);
1174 msg_get_str_value(src, MSG_MESSAGE_SMS_DATA_STR, tempStr, tempInt);
1175 msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, tempStr, strlen(tempStr));
1177 msg_get_int_value(src, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1178 msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, tempInt);
1180 // msg_set_network_status(msg, msg_get_network_status(src));
1181 msg_get_int_value(src, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1182 msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, tempInt);
1184 // msg_set_encode_type(msg, msg_get_encode_type(src));
1185 msg_get_int_value(src, MSG_MESSAGE_ENCODE_TYPE_INT, &tempInt);
1186 msg_set_int_value(msg, MSG_MESSAGE_ENCODE_TYPE_INT, tempInt);
1188 // msg_set_read_status(msg, msg_is_read(src));
1189 msg_get_bool_value(src, MSG_MESSAGE_READ_BOOL, &tempBool);
1190 msg_set_bool_value(msg, MSG_MESSAGE_READ_BOOL, tempBool);
1192 // msg_set_protect_status(msg, msg_is_protected(src));
1193 msg_get_bool_value(src, MSG_MESSAGE_PROTECTED_BOOL, &tempBool);
1194 msg_set_bool_value(msg, MSG_MESSAGE_PROTECTED_BOOL, tempBool);
1196 // msg_set_priority_info(msg, msg_get_priority_info(src));
1197 msg_get_int_value(src, MSG_MESSAGE_PRIORITY_INT, &tempInt);
1198 msg_set_int_value(msg, MSG_MESSAGE_PRIORITY_INT, tempInt);
1200 // msg_set_direction_info(msg, msg_get_direction_info(src));
1201 msg_get_int_value(src, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1202 msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT, tempInt);
1204 // msg_set_port(msg, msg_get_dest_port(src), msg_get_src_port(src));
1205 msg_get_int_value(src, MSG_MESSAGE_DEST_PORT_INT, &tempInt);
1206 msg_set_int_value(msg, MSG_MESSAGE_DEST_PORT_INT, tempInt);
1207 msg_get_int_value(src, MSG_MESSAGE_SRC_PORT_INT, &tempInt);
1208 msg_set_int_value(msg, MSG_MESSAGE_SRC_PORT_INT, tempInt);
1213 msg_struct_t Sms::createNewCopyOfPLatformMsgWithAddressList(const msg_struct_t src) const
1217 msg_struct_list_s *addr_list;
1218 msg_struct_list_s *new_addr_list;
1220 msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1222 msg_get_int_value(src, MSG_MESSAGE_STORAGE_ID_INT, &tempInt);
1223 msg_set_int_value(msg, MSG_MESSAGE_STORAGE_ID_INT, tempInt);
1225 msg_get_int_value(src, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1226 msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1228 msg_get_int_value(src, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1229 char tempStr[tempInt+1];
1230 memset(tempStr, 0, tempInt+1);
1232 msg_get_str_value(src, MSG_MESSAGE_SMS_DATA_STR, tempStr, tempInt);
1233 msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, tempStr, strlen(tempStr));
1235 msg_get_int_value(src, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1236 msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, tempInt);
1238 // msg_set_network_status(msg, msg_get_network_status(src));
1239 msg_get_int_value(src, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1240 msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, tempInt);
1242 // msg_set_encode_type(msg, msg_get_encode_type(src));
1243 msg_get_int_value(src, MSG_MESSAGE_ENCODE_TYPE_INT, &tempInt);
1244 msg_set_int_value(msg, MSG_MESSAGE_ENCODE_TYPE_INT, tempInt);
1246 // msg_set_read_status(msg, msg_is_read(src));
1247 msg_get_bool_value(src, MSG_MESSAGE_READ_BOOL, &tempBool);
1248 msg_set_bool_value(msg, MSG_MESSAGE_READ_BOOL, tempBool);
1250 // msg_set_protect_status(msg, msg_is_protected(src));
1251 msg_get_bool_value(src, MSG_MESSAGE_PROTECTED_BOOL, &tempBool);
1252 msg_set_bool_value(msg, MSG_MESSAGE_PROTECTED_BOOL, tempBool);
1254 // msg_set_priority_info(msg, msg_get_priority_info(src));
1255 msg_get_int_value(src, MSG_MESSAGE_PRIORITY_INT, &tempInt);
1256 msg_set_int_value(msg, MSG_MESSAGE_PRIORITY_INT, tempInt);
1258 // msg_set_direction_info(msg, msg_get_direction_info(src));
1259 msg_get_int_value(src, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1260 msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT, tempInt);
1262 // msg_set_port(msg, msg_get_dest_port(src), msg_get_src_port(src));
1263 msg_get_int_value(src, MSG_MESSAGE_DEST_PORT_INT, &tempInt);
1264 msg_set_int_value(msg, MSG_MESSAGE_DEST_PORT_INT, tempInt);
1265 msg_get_int_value(src, MSG_MESSAGE_SRC_PORT_INT, &tempInt);
1266 msg_set_int_value(msg, MSG_MESSAGE_SRC_PORT_INT, tempInt);
1269 msg_get_list_handle(src, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1270 msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&new_addr_list);
1272 new_addr_list->nCount = addr_list->nCount;
1274 for(int i=0; i<addr_list->nCount; i++)
1276 msg_struct_t addr_info = NULL;
1277 msg_struct_t new_addr_info = NULL;
1278 char addr_value[MAX_ADDRESS_VAL_LEN] = {0,};
1280 //get original address
1281 addr_info = addr_list->msg_struct_info[i];
1282 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
1283 msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
1285 //copy original address
1286 new_addr_info = new_addr_list->msg_struct_info[i];
1287 msg_set_str_value(new_addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
1288 msg_set_int_value(new_addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, tempInt);