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_MESSAGE_S *reqmsg = (MSG_MESSAGE_S*) pReq->msg;
42 if (reqmsg->msgType.subType != MSG_SENDREQ_JAVA_MMS) {
43 // In case MMS read report, get address value later.
44 if(reqmsg->msgType.subType != MSG_READREPLY_MMS) {
45 if ((reqmsg->nAddressCnt == 0) || (reqmsg->nAddressCnt > MAX_TO_ADDRESS_CNT)) {
46 MSG_DEBUG("Recipient address count error [%d]", reqmsg->nAddressCnt );
47 return MSG_ERR_INVALID_MESSAGE;
51 /* Begin: Setting default values for submit request */
52 // pReq->msg.msgId = 0; // Set Request ID: internal use
53 // pReq->msg.folderId = MSG_OUTBOX_ID; // Set Folder ID
54 if (reqmsg->msgType.subType == MSG_RETRIEVE_MMS) {
55 reqmsg->networkStatus = MSG_NETWORK_RETRIEVING;
57 reqmsg->networkStatus = MSG_NETWORK_SENDING;
60 reqmsg->bRead = false;
61 reqmsg->bProtected = false;
62 reqmsg->priority = MSG_MESSAGE_PRIORITY_NORMAL;
63 reqmsg->direction = MSG_DIRECTION_TYPE_MO;
64 reqmsg->storageId = MSG_STORAGE_PHONE;
66 time_t curTime = time(NULL);
69 THROW(MsgException::INVALID_RESULT, "time error : %s", strerror(errno));
71 reqmsg->displayTime = curTime;
72 /* End : Setting default values for submit request */
74 //in case of JAVA MMS msg, parse mms transaction id from pMmsData
75 reqmsg->networkStatus = MSG_NETWORK_SENDING;
76 strncpy(trId, (char*)reqmsg->pMmsData+3,MMS_TR_ID_LEN);
77 MSG_DEBUG("JavaMMS transaction Id:%s ",trId);
80 // Convert MSG_MESSAGE_S to MSG_MESSAGE_INFO_S
81 convertMsgStruct(reqmsg, &(reqInfo.msgInfo));
83 convertSendOptStruct(&(pReq->sendOpt), &(reqInfo.sendOptInfo), reqmsg->msgType);
87 /* Register proxy info used for receiving sent status */
88 MSG_PROXY_INFO_S chInfo = {0};
90 chInfo.listenerFd = MsgProxyListener::instance()->getRemoteFd();
92 chInfo.handleAddr = (unsigned int) this;
94 /* Allocate Memory to Command Data */
95 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_REQUEST_INFO_S) + sizeof(MSG_PROXY_INFO_S);
97 // In case of JAVA MMS msg, add trId
98 if (reqmsg->msgType.subType == MSG_SENDREQ_JAVA_MMS)
99 cmdSize += sizeof(trId);
101 char cmdBuf[cmdSize];
102 bzero(cmdBuf, cmdSize);
104 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
106 // Set Command Parameters
107 pCmd->cmdType = MSG_CMD_SUBMIT_REQ;
110 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
113 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqInfo, sizeof(MSG_REQUEST_INFO_S));
114 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN+sizeof(MSG_REQUEST_INFO_S)), &chInfo, sizeof(MSG_PROXY_INFO_S));
116 // In case of JAVA MMS msg, add trId
117 if (reqmsg->msgType.subType == MSG_SENDREQ_JAVA_MMS)
118 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));
120 // Send Command to Messaging FW
121 char* pEventData = NULL;
122 AutoPtr<char> eventBuf(&pEventData);
124 write((char*)pCmd, cmdSize, &pEventData);
127 MSG_EVENT_S* pEvent = (MSG_EVENT_S*) pEventData;
129 int* pReqId = (int*) pEvent->data;
130 pReq->reqId = *pReqId;
131 MSG_DEBUG("SENT_REQ_ID: %d", pReq->reqId);
133 if (pEvent->eventType != MSG_EVENT_SUBMIT_REQ)
135 THROW(MsgException::INVALID_RESULT, "Event Data Error");
140 return pEvent->result;
144 MSG_ERROR_T MsgHandle::cancelReq(MSG_REQUEST_ID_T reqId)
146 // Allocate Memory to Command Data
147 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_REQUEST_ID_T);
149 char cmdBuf[cmdSize];
150 bzero(cmdBuf, cmdSize);
152 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
154 // Set Command Parameters
155 pCmd->cmdType = MSG_CMD_CANCEL_REQ;
158 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
161 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &reqId, sizeof(MSG_REQUEST_ID_T));
163 // Send Command to Messaging FW
164 char* pEventData = NULL;
165 AutoPtr<char> eventBuf(&pEventData);
168 write((char*)pCmd, cmdSize, &pEventData);
171 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
173 if (pEvent->eventType != MSG_EVENT_CANCEL_REQ)
175 THROW(MsgException::INVALID_RESULT, "Event Data Error");
178 return pEvent->result;
182 MSG_ERROR_T MsgHandle::regSentStatusCallback(msg_sent_status_cb onStatusChanged, void *pUserParam)
184 if (!onStatusChanged)
185 THROW(MsgException::INVALID_PARAM, "onStatusChanged is null");
187 MsgProxyListener* eventListener = MsgProxyListener::instance();
189 eventListener->start();
191 if (eventListener->regSentStatusEventCB(this, onStatusChanged, pUserParam) == false) // callback was already registered, just return SUCCESS
194 // Allocate Memory to Command Data
195 int cmdSize = sizeof(MSG_CMD_S) + sizeof(int); // cmd type, listenerFd
196 char cmdBuf[cmdSize];
197 bzero(cmdBuf, cmdSize);
198 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
200 // Set Command Parameters
201 pCmd->cmdType = MSG_CMD_REG_SENT_STATUS_CB;
204 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
206 int listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
208 MSG_DEBUG("remote fd %d", listenerFd);
210 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &listenerFd, sizeof(listenerFd));
212 MSG_DEBUG("reg status [%d : %s], %d", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType), listenerFd);
214 // Send Command to Messaging FW
215 char* pEventData = NULL;
216 AutoPtr<char> eventBuf(&pEventData);
218 write((char*)pCmd, cmdSize, &pEventData);
221 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
223 if (pEvent->eventType != MSG_EVENT_REG_SENT_STATUS_CB)
225 THROW(MsgException::INVALID_PARAM, "Event Data Error");
228 return pEvent->result;
232 MSG_ERROR_T MsgHandle::regSmsMessageCallback(msg_sms_incoming_cb onMsgIncoming, unsigned short port, void *pUserParam)
234 if( (!onMsgIncoming) )
235 THROW(MsgException::INVALID_PARAM, "Param %p", onMsgIncoming);
237 MsgProxyListener* eventListener = MsgProxyListener::instance();
239 eventListener->start();
241 if (eventListener->regMessageIncomingEventCB(this, onMsgIncoming, port, pUserParam) == false) // callback was already registered, just return SUCCESS
244 // Allocate Memory to Command Data
245 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
247 char cmdBuf[cmdSize];
248 bzero(cmdBuf, cmdSize);
250 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
252 // Set Command Parameters
253 pCmd->cmdType = MSG_CMD_REG_INCOMING_MSG_CB;
256 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
258 MSG_CMD_REG_INCOMING_MSG_CB_S cmdParam = {0};
260 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
261 cmdParam.msgType = MSG_SMS_TYPE;
262 cmdParam.port = port;
264 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
266 MSG_DEBUG("reg new msg [%s], fd %d, port %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, cmdParam.port);
268 // Send Command to Messaging FW
269 char* pEventData = NULL;
270 AutoPtr<char> eventBuf(&pEventData);
273 write((char*)pCmd, cmdSize, &pEventData);
276 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
278 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MSG_CB)
280 THROW(MsgException::INVALID_RESULT, "Event Data Error");
283 return pEvent->result;
287 MSG_ERROR_T MsgHandle::regMmsConfMessageCallback(msg_mms_conf_msg_incoming_cb onMMSConfMsgIncoming, const char *pAppId, void *pUserParam)
289 if( (!onMMSConfMsgIncoming) )
290 THROW(MsgException::INVALID_PARAM, "Param %p", onMMSConfMsgIncoming);
292 MsgProxyListener* eventListener = MsgProxyListener::instance();
294 eventListener->start();
296 if (eventListener->regMMSConfMessageIncomingEventCB(this, onMMSConfMsgIncoming, pAppId, pUserParam) == false) // callback was already registered, just return SUCCESS
299 // Allocate Memory to Command Data
300 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S);
302 char cmdBuf[cmdSize];
303 bzero(cmdBuf, cmdSize);
305 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
307 // Set Command Parameters
308 pCmd->cmdType = MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB;
311 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
313 MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S cmdParam = {0};
315 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
316 cmdParam.msgType = MSG_MMS_TYPE;
319 strncpy(cmdParam.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
321 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
323 MSG_DEBUG("reg new msg [%s], fd:%d, appId:%s", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd, (pAppId)? cmdParam.appId:"NULL" );
325 // Send Command to Messaging FW
326 char* pEventData = NULL;
327 AutoPtr<char> eventBuf(&pEventData);
330 write((char*)pCmd, cmdSize, &pEventData);
333 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
335 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_MMS_CONF_MSG_CB)
337 THROW(MsgException::INVALID_RESULT, "Event Data Error");
340 return pEvent->result;
344 MSG_ERROR_T MsgHandle::regSyncMLMessageCallback(msg_syncml_msg_incoming_cb onSyncMLMsgIncoming, void *pUserParam)
346 if( (!onSyncMLMsgIncoming) )
347 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgIncoming);
349 MsgProxyListener* eventListener = MsgProxyListener::instance();
351 eventListener->start();
353 if (eventListener->regSyncMLMessageIncomingEventCB(this, onSyncMLMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
356 // Allocate Memory to Command Data
357 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
359 char cmdBuf[cmdSize];
360 bzero(cmdBuf, cmdSize);
362 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
364 // Set Command Parameters
365 pCmd->cmdType = MSG_CMD_REG_INCOMING_SYNCML_MSG_CB;
368 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
370 MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S cmdParam = {0};
372 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
373 cmdParam.msgType = MSG_SMS_TYPE;
375 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
377 MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
379 // Send Command to Messaging FW
380 char* pEventData = NULL;
381 AutoPtr<char> eventBuf(&pEventData);
383 write((char*)pCmd, cmdSize, &pEventData);
386 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
388 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_SYNCML_MSG_CB)
390 THROW(MsgException::INVALID_RESULT, "Event Data Error");
393 return pEvent->result;
397 MSG_ERROR_T MsgHandle::regLBSMessageCallback(msg_lbs_msg_incoming_cb onLBSMsgIncoming, void *pUserParam)
399 if( (!onLBSMsgIncoming) )
400 THROW(MsgException::INVALID_PARAM, "Param %p", onLBSMsgIncoming);
402 MsgProxyListener* eventListener = MsgProxyListener::instance();
404 eventListener->start();
406 if (eventListener->regLBSMessageIncomingEventCB(this, onLBSMsgIncoming, pUserParam) == false) // callback was already registered, just return SUCCESS
409 // Allocate Memory to Command Data
410 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
411 char cmdBuf[cmdSize];
412 bzero(cmdBuf, cmdSize);
413 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
415 // Set Command Parameters
416 pCmd->cmdType = MSG_CMD_REG_INCOMING_LBS_MSG_CB;
419 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
421 MSG_CMD_REG_INCOMING_LBS_MSG_CB_S cmdParam = {0};
423 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
424 cmdParam.msgType = MSG_SMS_TYPE;
426 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
428 MSG_DEBUG("reg new msg [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
430 // Send Command to Messaging FW
431 char* pEventData = NULL;
432 AutoPtr<char> eventBuf(&pEventData);
435 write((char*)pCmd, cmdSize, &pEventData);
438 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
440 if (pEvent->eventType != MSG_EVENT_REG_INCOMING_LBS_MSG_CB)
442 THROW(MsgException::INVALID_RESULT, "Event Data Error");
445 return pEvent->result;
449 MSG_ERROR_T MsgHandle::regSyncMLMessageOperationCallback(msg_syncml_msg_operation_cb onSyncMLMsgOperation, void *pUserParam)
451 if( (!onSyncMLMsgOperation) )
452 THROW(MsgException::INVALID_PARAM, "Param %p", onSyncMLMsgOperation);
454 MsgProxyListener* eventListener = MsgProxyListener::instance();
456 eventListener->start();
458 if (eventListener->regSyncMLMessageOperationEventCB(this, onSyncMLMsgOperation, pUserParam) == false) // callback was already registered, just return SUCCESS
461 // Allocate Memory to Command Data
462 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S); //sizeof(int) + sizeof; // cmd type, listener fd
464 char cmdBuf[cmdSize];
465 bzero(cmdBuf, cmdSize);
467 MSG_CMD_S* pCmd = (MSG_CMD_S*) cmdBuf;
469 // Set Command Parameters
470 pCmd->cmdType = MSG_CMD_REG_SYNCML_MSG_OPERATION_CB;
473 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
475 MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S cmdParam = {0};
477 cmdParam.listenerFd = eventListener->getRemoteFd(); // fd that is reserved to the "listener thread" by msgfw daemon
478 cmdParam.msgType = MSG_SMS_TYPE;
480 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &cmdParam, sizeof(cmdParam));
482 MSG_DEBUG("register syncML msg operation callback [%s], fd %d", MsgDbgCmdStr(pCmd->cmdType), cmdParam.listenerFd);
484 // Send Command to Messaging FW
485 char* pEventData = NULL;
486 AutoPtr<char> eventBuf(&pEventData);
489 write((char*)pCmd, cmdSize, &pEventData);
492 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
494 if (pEvent->eventType != MSG_EVENT_REG_SYNCML_MSG_OPERATION_CB)
496 THROW(MsgException::INVALID_RESULT, "Event Data Error");
499 return pEvent->result;
503 MSG_ERROR_T MsgHandle::operateSyncMLMessage(MSG_MESSAGE_ID_T msgId)
506 THROW(MsgException::INVALID_PARAM, "Param msgId %d", msgId);
508 // Allocate Memory to Command Data
509 int cmdSize = sizeof(MSG_CMD_S) + sizeof(MSG_MESSAGE_ID_T);
511 char cmdBuf[cmdSize];
512 bzero(cmdBuf, cmdSize);
513 MSG_CMD_S* pCmd = (MSG_CMD_S*)cmdBuf;
515 // Set Command Parameters
516 pCmd->cmdType = MSG_CMD_SYNCML_OPERATION;
519 memcpy(pCmd->cmdCookie, mCookie, MAX_COOKIE_LEN);
522 memcpy((void*)((char*)pCmd+sizeof(MSG_CMD_TYPE_T)+MAX_COOKIE_LEN), &msgId, sizeof(MSG_MESSAGE_ID_T));
524 // Send Command to Messaging FW
525 char* pEventData = NULL;
526 AutoPtr<char> eventBuf(&pEventData);
529 write((char*)pCmd, cmdSize, &pEventData);
532 MSG_EVENT_S* pEvent = (MSG_EVENT_S*)pEventData;
534 if (pEvent->eventType != MSG_EVENT_SYNCML_OPERATION)
536 THROW(MsgException::INVALID_RESULT, "Event Data Error");
539 return pEvent->result;