2 * Copyright 2012 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://www.tizenopensource.org/license
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 "MsgProxyListener.h"
24 #include "MsgHandle.h"
27 /*==================================================================================================
28 IMPLEMENTATION OF MsgHandle - Transport Member Functions
29 ==================================================================================================*/
30 msg_error_t MsgHandle::submitReq(MSG_REQUEST_S* pReq)
35 THROW(MsgException::INVALID_PARAM, "pReq is NULL");
37 MSG_REQUEST_INFO_S reqInfo = {0};
38 char trId[MMS_TR_ID_LEN+1] = {0};
40 msg_struct_s *msg_s = (msg_struct_s *)pReq->msg;
42 MSG_MESSAGE_HIDDEN_S *reqmsg = (MSG_MESSAGE_HIDDEN_S*)msg_s->data;
44 if (reqmsg->subType != MSG_SENDREQ_JAVA_MMS) {
45 // In case MMS read report, get address value later.
46 if(reqmsg->subType != MSG_READREPLY_MMS) {
47 if ((reqmsg->addr_list->nCount == 0) || (reqmsg->addr_list->nCount > MAX_TO_ADDRESS_CNT)) {
48 MSG_DEBUG("Recipient address count error [%d]", reqmsg->addr_list->nCount );
49 return MSG_ERR_INVALID_MESSAGE;
53 /* Begin: Setting default values for submit request */
54 // pReq->msg.msgId = 0; // Set Request ID: internal use
55 // pReq->msg.folderId = MSG_OUTBOX_ID; // Set Folder ID
56 if (reqmsg->subType == MSG_RETRIEVE_MMS) {
57 reqmsg->networkStatus = MSG_NETWORK_RETRIEVING;
59 reqmsg->networkStatus = MSG_NETWORK_SENDING;
62 reqmsg->bRead = false;
63 reqmsg->bProtected = false;
64 reqmsg->priority = MSG_MESSAGE_PRIORITY_NORMAL;
65 reqmsg->direction = MSG_DIRECTION_TYPE_MO;
66 reqmsg->storageId = MSG_STORAGE_PHONE;
68 time_t curTime = time(NULL);
71 THROW(MsgException::INVALID_RESULT, "time error : %s", strerror(errno));
73 reqmsg->displayTime = curTime;
74 /* End : Setting default values for submit request */
76 //in case of JAVA MMS msg, parse mms transaction id from pMmsData
77 reqmsg->networkStatus = MSG_NETWORK_SENDING;
78 strncpy(trId, (char*)reqmsg->pMmsData+3,MMS_TR_ID_LEN);
79 MSG_DEBUG("JavaMMS transaction Id:%s ",trId);
82 // Convert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S
83 convertMsgStruct(reqmsg, &(reqInfo.msgInfo));
85 MSG_MESSAGE_TYPE_S msgType = {0,};
87 msgType.mainType = reqmsg->mainType;
88 msgType.subType = reqmsg->subType;
89 msgType.classType = reqmsg->classType;
91 convertSendOptStruct((const MSG_SENDINGOPT_S *)pReq->sendOpt, &(reqInfo.sendOptInfo), msgType);
95 /* Register proxy info used for receiving sent status */
96 MSG_PROXY_INFO_S chInfo = {0};
98 chInfo.listenerFd = MsgProxyListener::instance()->getRemoteFd();
100 chInfo.handleAddr = (unsigned int) this;
102 /* Allocate Memory to Command Data */
103 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_REQUEST_INFO_S) + sizeof(MSG_PROXY_INFO_S);
105 // In case of JAVA MMS msg, add trId
106 if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
107 cmdSize += sizeof(trId);
109 char cmdBuf[cmdSize];
110 bzero(cmdBuf, cmdSize);
112 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
114 // Set Command Parameters
115 pCmd->cmdType = MSG_CMD_SUBMIT_REQ;
118 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
121 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqInfo, sizeof(MSG_REQUEST_INFO_S));
122 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_REQUEST_INFO_S)), &chInfo, sizeof(MSG_PROXY_INFO_S));
124 // In case of JAVA MMS msg, add trId
125 if (reqmsg->subType == MSG_SENDREQ_JAVA_MMS)
126 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_REQUEST_INFO_S)+sizeof(MSG_PROXY_INFO_S)), &trId, sizeof(trId));
128 // Send Command to Messaging FW
129 char* pEventData = NULL;
130 AutoPtr<char> eventBuf(&pEventData);
132 write((char*)pCmd, cmdSize, &pEventData);
135 MSG_EVENT_S* pEvent = (MSG_EVENT_S*) pEventData;
137 int* pReqId = (int*) pEvent->data;
138 pReq->reqId = *pReqId;
139 MSG_DEBUG("SENT_REQ_ID: %d", pReq->reqId);
141 if (pEvent->eventType != MSG_EVENT_SUBMIT_REQ)
143 THROW(MsgException::INVALID_RESULT, "Event Data Error");
148 return pEvent->result;
152 msg_error_t MsgHandle::cancelReq(msg_request_id_t reqId)
154 // Allocate Memory to Command Data
155 int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_request_id_t);
157 char cmdBuf[cmdSize];
158 bzero(cmdBuf, cmdSize);
160 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
162 // Set Command Parameters
163 pCmd->cmdType = MSG_CMD_CANCEL_REQ;
166 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
169 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqId, sizeof(msg_request_id_t));
171 // Send Command to Messaging FW
172 char* pEventData = NULL;
173 AutoPtr<char> eventBuf(&pEventData);
176 write((char*)pCmd, cmdSize, &pEventData);
179 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
181 if (pEvent->eventType != MSG_EVENT_CANCEL_REQ)
183 THROW(MsgException::INVALID_RESULT, "Event Data Error");
186 return pEvent->result;
190 msg_error_t MsgHandle::regSentStatusCallback(msg_sent_status_cb onStatusChanged, void *pUserParam)
192 if (!onStatusChanged)
193 THROW(MsgException::INVALID_PARAM, "onStatusChanged is null");
195 MsgProxyListener* eventListener = MsgProxyListener::instance();
197 eventListener->start();
199 if (eventListener->regSentStatusEventCB(this, onStatusChanged, pUserParam) == false) // callback was already registered, just return SUCCESS
202 // Allocate Memory to Command Data
203 int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); // cmd type, listenerFd
204 char cmdBuf[cmdSize];
205 bzero(cmdBuf, cmdSize);
206 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
208 // Set Command Parameters
209 pCmd->cmdType = MSG_CMD_REG_SENT_STATUS_CB;
212 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
214 int listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
216 MSG_DEBUG("remote fd %d", listenerFd);
218 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &listenerFd, sizeof(listenerFd));
220 MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), listenerFd);
222 // Send Command to Messaging FW
223 char* pEventData = NULL;
224 AutoPtr<char> eventBuf(&pEventData);
226 write((char*)pCmd, cmdSize, &pEventData);
229 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
231 if (pEvent->eventType != MSG_EVENT_REG_SENT_STATUS_CB)
233 THROW(MsgException::INVALID_PARAM, "Event Data Error");
236 return pEvent->result;
240 msg_error_t MsgHandle::regSmsMessageCallback(msg_sms_incoming_cb onMsgIncoming, unsigned short port, void *pUserParam)
242 if( (!onMsgIncoming) )
243 THROW(MsgException::INVALID_PARAM, "Param %p", onMsgIncoming);
245 MsgProxyListener* eventListener = MsgProxyListener::instance();
247 eventListener->start();
249 if (eventListener->regMessageIncomingEventCB(this, onMsgIncoming, port, pUserParam) == false) // callback was already registered, just return SUCCESS
252 // Allocate Memory to Command Data
253 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
255 char cmdBuf[cmdSize];
256 bzero(cmdBuf, cmdSize);
258 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
260 // Set Command Parameters
261 pCmd->cmdType = MSG_CMD_REG_INCOMING_MSG_CB;
264 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
266 MSG_CMD_REG_INCOMING_MSG_CB_S cmdParam = {0};
268 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
269 cmdParam.msgType = MSG_SMS_TYPE;
270 cmdParam.port = port;
272 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
274 MSG_DEBUG("reg new msg [%s], fd %d, port %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.port);
276 // Send Command to Messaging FW
277 char* pEventData = NULL;
278 AutoPtr<char> eventBuf(&pEventData);
281 write((char*)pCmd, cmdSize, &pEventData);
284 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
286 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MSG_CB)
288 THROW(MsgException::INVALID_RESULT, "Event Data Error");
291 return pEvent->result;
295 msg_error_t MsgHandle::regMmsConfMessageCallback(msg_mms_conf_msg_incoming_cb onMMSConfMsgIncoming, const char *pAppId, void *pUserParam)
297 if( (!onMMSConfMsgIncoming) )
298 THROW(MsgException::INVALID_PARAM, "Param %p", onMMSConfMsgIncoming);
300 MsgProxyListener* eventListener = MsgProxyListener::instance();
302 eventListener->start();
304 if (eventListener->regMMSConfMessageIncomingEventCB(this, onMMSConfMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
307 // Allocate Memory to Command Data
308 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S);
310 char cmdBuf[cmdSize];
311 bzero(cmdBuf, cmdSize);
313 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
315 // Set Command Parameters
316 pCmd->cmdType = MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB;
319 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
321 MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S cmdParam = {0};
323 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
324 cmdParam.msgType = MSG_MMS_TYPE;
327 strncpy(cmdParam.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
329 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
331 MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, (pAppId)? cmdParam.appId:"NULL" );
333 // Send Command to Messaging FW
334 char* pEventData = NULL;
335 AutoPtr<char> eventBuf(&pEventData);
338 write((char*)pCmd, cmdSize, &pEventData);
341 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
343 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MMS_CONF_MSG_CB)
345 THROW(MsgException::INVALID_RESULT, "Event Data Error");
348 return pEvent->result;
352 msg_error_t MsgHandle::regSyncMLMessageCallback(msg_syncml_msg_incoming_cb onSyncMLMsgIncoming, void *pUserParam)
354 if( (!onSyncMLMsgIncoming) )
355 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgIncoming);
357 MsgProxyListener* eventListener = MsgProxyListener::instance();
359 eventListener->start();
361 if (eventListener->regSyncMLMessageIncomingEventCB(this, onSyncMLMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
364 // Allocate Memory to Command Data
365 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
367 char cmdBuf[cmdSize];
368 bzero(cmdBuf, cmdSize);
370 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
372 // Set Command Parameters
373 pCmd->cmdType = MSG_CMD_REG_INCOMING_SYNCML_MSG_CB;
376 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
378 MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S cmdParam = {0};
380 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
381 cmdParam.msgType = MSG_SMS_TYPE;
383 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
385 MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
387 // Send Command to Messaging FW
388 char* pEventData = NULL;
389 AutoPtr<char> eventBuf(&pEventData);
391 write((char*)pCmd, cmdSize, &pEventData);
394 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
396 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_SYNCML_MSG_CB)
398 THROW(MsgException::INVALID_RESULT, "Event Data Error");
401 return pEvent->result;
405 msg_error_t MsgHandle::regLBSMessageCallback(msg_lbs_msg_incoming_cb onLBSMsgIncoming, void *pUserParam)
407 if( (!onLBSMsgIncoming) )
408 THROW(MsgException::INVALID_PARAM, "Param %p", onLBSMsgIncoming);
410 MsgProxyListener* eventListener = MsgProxyListener::instance();
412 eventListener->start();
414 if (eventListener->regLBSMessageIncomingEventCB(this, onLBSMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
417 // Allocate Memory to Command Data
418 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
419 char cmdBuf[cmdSize];
420 bzero(cmdBuf, cmdSize);
421 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
423 // Set Command Parameters
424 pCmd->cmdType = MSG_CMD_REG_INCOMING_LBS_MSG_CB;
427 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
429 MSG_CMD_REG_INCOMING_LBS_MSG_CB_S cmdParam = {0};
431 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
432 cmdParam.msgType = MSG_SMS_TYPE;
434 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
436 MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
438 // Send Command to Messaging FW
439 char* pEventData = NULL;
440 AutoPtr<char> eventBuf(&pEventData);
443 write((char*)pCmd, cmdSize, &pEventData);
446 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
448 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_LBS_MSG_CB)
450 THROW(MsgException::INVALID_RESULT, "Event Data Error");
453 return pEvent->result;
457 msg_error_t MsgHandle::regSyncMLMessageOperationCallback(msg_syncml_msg_operation_cb onSyncMLMsgOperation, void *pUserParam)
459 if( (!onSyncMLMsgOperation) )
460 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgOperation);
462 MsgProxyListener* eventListener = MsgProxyListener::instance();
464 eventListener->start();
466 if (eventListener->regSyncMLMessageOperationEventCB(this, onSyncMLMsgOperation, pUserParam) == false) // callback was already registered, just return SUCCESS
469 // Allocate Memory to Command Data
470 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
472 char cmdBuf[cmdSize];
473 bzero(cmdBuf, cmdSize);
475 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
477 // Set Command Parameters
478 pCmd->cmdType = MSG_CMD_REG_SYNCML_MSG_OPERATION_CB;
481 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
483 MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S cmdParam = {0};
485 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
486 cmdParam.msgType = MSG_SMS_TYPE;
488 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
490 MSG_DEBUG("register syncML msg operation callback [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
492 // Send Command to Messaging FW
493 char* pEventData = NULL;
494 AutoPtr<char> eventBuf(&pEventData);
497 write((char*)pCmd, cmdSize, &pEventData);
500 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
502 if (pEvent->eventType != MSG_EVENT_REG_SYNCML_MSG_OPERATION_CB)
504 THROW(MsgException::INVALID_RESULT, "Event Data Error");
507 return pEvent->result;
511 msg_error_t MsgHandle::operateSyncMLMessage(msg_message_id_t msgId)
514 THROW(MsgException::INVALID_PARAM, "Param msgId %d", msgId);
516 // Allocate Memory to Command Data
517 int cmdSize = sizeof(MSG_CMD_S) + sizeof(msg_message_id_t);
519 char cmdBuf[cmdSize];
520 bzero(cmdBuf, cmdSize);
521 MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
523 // Set Command Parameters
524 pCmd->cmdType = MSG_CMD_SYNCML_OPERATION;
527 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
530 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msgId, sizeof(msg_message_id_t));
532 // Send Command to Messaging FW
533 char* pEventData = NULL;
534 AutoPtr<char> eventBuf(&pEventData);
537 write((char*)pCmd, cmdSize, &pEventData);
540 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
542 if (pEvent->eventType != MSG_EVENT_SYNCML_OPERATION)
544 THROW(MsgException::INVALID_RESULT, "Event Data Error");
547 return pEvent->result;