apply FSL(Flora Software License)
[framework/messaging/msg-service.git] / framework / transaction-manager / MsgTransManager.cpp
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
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
7   *
8   *    http://www.tizenopensource.org/license
9   *
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.
15   */
16
17 #include <errno.h>
18 #include <stdlib.h>
19 #include <sys/socket.h>
20 #include <sys/stat.h>
21
22 #include <security-server.h>
23
24 #include "MsgDebug.h"
25 #include "MsgMemory.h"
26 #include "MsgException.h"
27 #include "MsgCppTypes.h"
28 #include "MsgContact.h"
29 #include "MsgIpcSocket.h"
30 #include "MsgGconfWrapper.h"
31 #include "MsgUtilFunction.h"
32 #include "MsgCmdHandler.h"
33 #include "MsgSettingHandler.h"
34 #include "MsgStorageHandler.h"
35 #include "MsgPluginManager.h"
36 #include "MsgTransManager.h"
37
38 /*==================================================================================================
39                                      FUNCTION IMPLEMENTATION
40 ==================================================================================================*/
41 void MsgContactChangedCallback()
42 {
43         MSG_MSGID_LIST_S msgIdList;
44         memset(&msgIdList, 0x00, sizeof(MSG_MSGID_LIST_S));
45
46         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_CONTACT, &msgIdList);
47 }
48
49 /*==================================================================================================
50                                      IMPLEMENTATION OF MsgTransactionManager - Member Functions
51 ==================================================================================================*/
52 MsgTransactionManager* MsgTransactionManager::pInstance = NULL;
53 MsgIpcServerSocket MsgTransactionManager::servSock;
54
55
56 MsgTransactionManager::MsgTransactionManager() : running(false), mx(), cv()
57 {
58         sentMsgMap.clear();
59
60         statusCBFdMap.clear();
61         newMsgCBList.clear();
62         newMMSConfMsgCBList.clear();
63         newSyncMLMsgCBList.clear();
64         newLBSMsgCBList.clear();
65         javaMMSList.clear();
66         operationSyncMLMsgCBList.clear();
67         storageChangeFdMap.clear();
68
69         handlerMap.clear();
70
71 //      Fill in mMsgHandlers, as given in the below.
72         handlerMap[MSG_CMD_ADD_MSG]                             = &MsgAddMessageHandler;
73         handlerMap[MSG_CMD_ADD_SYNCML_MSG]              = &MsgAddSyncMLMessageHandler;
74         handlerMap[MSG_CMD_UPDATE_MSG]                  = &MsgUpdateMessageHandler;
75         handlerMap[MSG_CMD_UPDATE_READ]                         = &MsgUpdateReadStatusHandler;
76         handlerMap[MSG_CMD_UPDATE_PROTECTED]            = &MsgUpdateProtectedStatusHandler;
77         handlerMap[MSG_CMD_DELETE_MSG]                  = &MsgDeleteMessageHandler;
78         handlerMap[MSG_CMD_DELALL_MSGINFOLDER]  = &MsgDeleteAllMessageInFolderHandler;
79         handlerMap[MSG_CMD_MOVE_MSGTOFOLDER]    = &MsgMoveMessageToFolderHandler;
80         handlerMap[MSG_CMD_MOVE_MSGTOSTORAGE]   = &MsgMoveMessageToStorageHandler;
81         handlerMap[MSG_CMD_COUNT_MSG]                   = &MsgCountMessageHandler;
82         handlerMap[MSG_CMD_GET_MSG]                             = &MsgGetMessageHandler;
83         handlerMap[MSG_CMD_GET_FOLDERVIEWLIST]  = &MsgGetFolderViewListHandler;
84
85         handlerMap[MSG_CMD_ADD_FOLDER]                  = &MsgAddFolderHandler;
86         handlerMap[MSG_CMD_UPDATE_FOLDER]               = &MsgUpdateFolderHandler;
87         handlerMap[MSG_CMD_DELETE_FOLDER]               = &MsgDeleteFolderHandler;
88         handlerMap[MSG_CMD_GET_FOLDERLIST]              = &MsgGetFolderListHandler;
89
90         handlerMap[MSG_CMD_SET_CONFIG]                  = &MsgSetConfigHandler;
91         handlerMap[MSG_CMD_GET_CONFIG]                  = &MsgGetConfigHandler;
92         handlerMap[MSG_CMD_GET_MSG_TYPE]                        = &MsgGetMsgTypeHandler;
93
94         handlerMap[MSG_CMD_SUBMIT_REQ]                  = &MsgSubmitReqHandler;
95         handlerMap[MSG_CMD_CANCEL_REQ]                  = &MsgCancelReqHandler;
96
97         handlerMap[MSG_CMD_REG_SENT_STATUS_CB]  = &MsgRegSentStatusCallbackHandler;
98         handlerMap[MSG_CMD_REG_STORAGE_CHANGE_CB] = &MsgRegStorageChangeCallbackHandler;
99         handlerMap[MSG_CMD_REG_INCOMING_MSG_CB] = &MsgRegIncomingMsgCallbackHandler;
100         handlerMap[MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB]        = &MsgRegIncomingMMSConfMsgCallbackHandler;
101         handlerMap[MSG_CMD_REG_INCOMING_SYNCML_MSG_CB]  = &MsgRegIncomingSyncMLMsgCallbackHandler;
102         handlerMap[MSG_CMD_REG_INCOMING_LBS_MSG_CB]     = &MsgRegIncomingLBSMsgCallbackHandler;
103         handlerMap[MSG_CMD_REG_SYNCML_MSG_OPERATION_CB] = &MsgRegSyncMLMsgOperationCallbackHandler;
104
105         handlerMap[MSG_CMD_PLG_SENT_STATUS_CNF] = &MsgSentStatusHandler;
106         handlerMap[MSG_CMD_PLG_STORAGE_CHANGE_IND]      = &MsgStorageChangeHandler;
107         handlerMap[MSG_CMD_PLG_INCOMING_MSG_IND]        = &MsgIncomingMsgHandler;
108         handlerMap[MSG_CMD_PLG_INCOMING_MMS_CONF]       = &MsgIncomingMMSConfMsgHandler;
109
110         handlerMap[MSG_CMD_PLG_INCOMING_SYNCML_IND] = &MsgIncomingSyncMLMsgHandler;
111         handlerMap[MSG_CMD_PLG_INCOMING_LBS_IND] = &MsgIncomingLBSMsgHandler;
112         handlerMap[MSG_CMD_PLG_INIT_SIM_BY_SAT] = &MsgInitSimBySatHandler;
113
114         handlerMap[MSG_CMD_GET_THREADVIEWLIST]  = &MsgGetThreadViewListHandler;
115         handlerMap[MSG_CMD_GET_CONVERSATIONVIEWLIST]    = &MsgGetConversationViewListHandler;
116         handlerMap[MSG_CMD_DELETE_THREADMESSAGELIST]    = &MsgDeleteThreadMessageListHandler;
117
118         handlerMap[MSG_CMD_GET_CONTACT_COUNT]   = &MsgCountMsgByContactHandler;
119         handlerMap[MSG_CMD_GET_QUICKPANEL_DATA]         = &MsgGetQuickPanelDataHandler;
120         handlerMap[MSG_CMD_COUNT_BY_MSGTYPE]    = &MsgCountMsgByTypeHandler;
121         handlerMap[MSG_CMD_RESET_DB]    = &MsgResetDatabaseHandler;
122         handlerMap[MSG_CMD_GET_MEMSIZE]         = &MsgGetMemSizeHandler;
123
124         handlerMap[MSG_CMD_UPDATE_THREAD_READ] = &MsgUpdateThreadReadStatusHandler;
125
126         handlerMap[MSG_CMD_SYNCML_OPERATION] = &MsgSyncMLMsgOperationHandler;
127         handlerMap[MSG_CMD_GET_REPORT_STATUS]                   = &MsgGetReportStatusHandler;
128 }
129
130
131 MsgTransactionManager::~MsgTransactionManager()
132 {
133
134 }
135
136
137 MsgTransactionManager* MsgTransactionManager::instance()
138 {
139         if (!pInstance)
140                 pInstance = new MsgTransactionManager();
141
142         return pInstance;
143 }
144
145
146 void MsgTransactionManager::run()
147 {
148         servSock.open(MSG_SOCKET_PATH);
149
150         fd_set readfds = servSock.fdSet();
151         int nfds = 0;
152
153         MSG_DEBUG("Start Transaction Manager");
154
155         while(1)
156         {
157                 readfds = servSock.fdSet();
158                 nfds = servSock.maxFd();
159
160                 MSG_DEBUG("Wait For Select() : nfds %d", nfds);
161
162                 // set Status;
163                 setTMStatus();
164
165                 if( select(nfds, &readfds, NULL, NULL, NULL) == -1)
166                 {
167                         THROW(MsgException::SELECT_ERROR, strerror(errno));
168                 }
169
170                 try
171                 {
172                         for (int i=0 ; i < nfds; i++)
173                         {
174                                 if (FD_ISSET(i, &readfds))
175                                 {
176                                         if (i == servSock.fd()) // if it is socket connection request
177                                                 servSock.accept();
178                                         else
179                                                 handleRequest(i);
180                                 }
181                         }
182                 }
183                 catch (MsgException& e)
184                 {
185                         MSG_FATAL("%s", e.what());
186                 }
187                 catch (exception& e)
188                 {
189                         MSG_FATAL("%s", e.what());
190                 }
191
192                 // Release Memory
193                 MsgReleaseMemory();
194         }
195 }
196
197
198 void MsgTransactionManager::write(int fd, const char* buf, int len)
199 {
200         servSock.write(fd, buf, len);
201 }
202
203
204 void MsgTransactionManager::insertSentMsg(int reqId, MSG_PROXY_INFO_S* pPrxInfo)
205 {
206         if (pPrxInfo == NULL)
207                 THROW(MsgException::SENT_STATUS_ERROR, "Input Parameter is NULL");
208
209         MSG_DEBUG("msg for submit: reqId %d listenerFd %d handleAddr %x", reqId, pPrxInfo->listenerFd, pPrxInfo->handleAddr);
210
211         fd_map::iterator it = statusCBFdMap.find(pPrxInfo->listenerFd);
212
213         if (it == statusCBFdMap.end()) { // if the status CB is not registered
214                 MSG_DEBUG("No sent_status registered for fd %d", pPrxInfo->listenerFd);
215         } else {
216                 sentMsgMap.insert(make_pair(reqId, *pPrxInfo));
217         }
218 }
219
220
221 MSG_PROXY_INFO_S* MsgTransactionManager::getProxyInfo(int reqId)
222 {
223         sentmsg_map::iterator it = sentMsgMap.find(reqId);
224
225         if (it == sentMsgMap.end())
226         {
227                 MSG_DEBUG("No sent status cb found (exception: mobile tracker)");
228                 return NULL;
229         }
230
231         return &(it->second);
232 }
233
234
235 void MsgTransactionManager::delProxyInfo(int reqId)
236 {
237         sentmsg_map::iterator it = sentMsgMap.find(reqId);
238
239         if (it == sentMsgMap.end())
240         {
241                 THROW(MsgException::SENT_STATUS_ERROR, "channel info does not exist");
242         }
243
244         sentMsgMap.erase(it);
245 }
246
247
248 void MsgTransactionManager::handleRequest(int fd)
249 {
250         MSG_BEGIN();
251
252         MSG_DEBUG("Event from fd %d", fd);
253
254         char* buf = NULL;
255         AutoPtr<char> wrap(&buf);
256         int len;
257         int ret = servSock.read(fd, &buf, &len);
258
259         if( ret == CLOSE_CONNECTION_BY_SIGNAL || ret == CLOSE_CONNECTION_BY_USER || ret < 0)
260         {
261                 MSG_DEBUG("Read value [%d]", ret);
262                 cleanup(fd);
263                 return;
264         }
265
266         if (len == 0)
267                 THROW(MsgException::INVALID_RESULT, "read buffer size = 0");
268
269         char* pEventData = NULL;
270         AutoPtr<char> eventBuf(&pEventData);
271
272         int eventSize = 0;
273
274         // decoding cmd from APP
275         MSG_CMD_S* pCmd = (MSG_CMD_S*) buf;
276         MSG_DEBUG("Command Type [%d : %s]", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType));
277
278         if (pCmd->cmdType > MSG_CMD_NUM)
279                 THROW(MsgException::OUT_OF_RANGE, "request CMD is not defined");
280
281         if (pCmd->cmdType < MSG_CMD_GET_REPORT_STATUS)
282         {
283                 // check privilege
284                 if (checkPrivilege(pCmd->cmdType, pCmd->cmdCookie) == false)
285                 {
286 #ifdef MSG_CHECK_PRIVILEGE
287                         eventSize = sizeof(MSG_EVENT_S);
288
289                         pEventData = new char[eventSize];
290
291                         MSG_EVENT_S* pMsgEvent = (MSG_EVENT_S*)pEventData;
292
293                         pMsgEvent->eventType = pCmd->cmdType;
294                         pMsgEvent->result = MSG_ERR_SECURITY_ERROR;
295
296                         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
297                         servSock.write(fd, pEventData, eventSize);
298
299                         return;
300 #endif
301                 }
302         }
303
304         // determine the handler based on pCmd->cmdType
305         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
306
307         pfHandler = handlerMap[pCmd->cmdType];
308
309         if (!pfHandler)
310                 THROW(MsgException::INVALID_PARAM, "No handler for %d", pCmd->cmdType);
311
312         // run handler function
313         eventSize = pfHandler(pCmd, &pEventData);
314
315         if (eventSize == 0 || pEventData == NULL)
316                 THROW(MsgException::INVALID_RESULT, "event size = 0 or event data = NULL");
317
318         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
319
320         servSock.write(fd, pEventData, eventSize);
321
322         MSG_END();
323 }
324
325
326 // terminating the socket connection between ipc server and ipc client
327 void MsgTransactionManager::cleanup(int fd)
328 {
329         MSG_BEGIN();
330
331         servSock.close(fd);
332
333         MSG_DEBUG("fd %d disonnected", fd);
334
335         // remove sent msg info for fd
336         sentmsg_map::iterator sentmsg_it = sentMsgMap.begin();
337
338         for (; sentmsg_it != sentMsgMap.end(); sentmsg_it++)
339         {
340                 if (sentmsg_it->second.listenerFd == fd)
341                 {
342                         sentmsg_it->second.listenerFd = 0;
343                         sentmsg_it->second.handleAddr = 0;
344                 }
345         }
346
347         // remove sent status callback for fd
348         statusCBFdMap.erase(fd);
349
350         // remove all newMsgCBs for fd
351         newmsg_list::iterator newmsg_it = newMsgCBList.begin();
352
353         while (newmsg_it != newMsgCBList.end())
354         {
355                 if (newmsg_it->listenerFd == fd)
356                 {
357                         newmsg_it = newMsgCBList.erase(newmsg_it);
358                 }
359                 else
360                 {
361                         ++newmsg_it;
362                 }
363         }
364
365         // remove all newMMSConfMsgCBs for fd
366         mmsconf_list::iterator mmsconf_it = newMMSConfMsgCBList.begin();
367
368         while (mmsconf_it != newMMSConfMsgCBList.end())
369         {
370                 if (mmsconf_it->listenerFd == fd)
371                 {
372                         mmsconf_it = newMMSConfMsgCBList.erase(mmsconf_it);
373                 }
374                 else
375                 {
376                         ++mmsconf_it;
377                 }
378         }
379
380         // remove all newSyncMLMsgCBs for fd
381         syncmlmsg_list::iterator syncmlmsg_it = newSyncMLMsgCBList.begin();
382
383         while (syncmlmsg_it != newSyncMLMsgCBList.end())
384         {
385                 if (syncmlmsg_it->listenerFd == fd)
386                 {
387                         syncmlmsg_it = newSyncMLMsgCBList.erase(syncmlmsg_it);
388                 }
389                 else
390                 {
391                         ++syncmlmsg_it;
392                 }
393         }
394
395         // remove all newLBSMsgCBs for fd
396         lbsmsg_list::iterator lbsmsg_it = newLBSMsgCBList.begin();
397
398         while (lbsmsg_it != newLBSMsgCBList.end())
399         {
400                 if (lbsmsg_it->listenerFd == fd)
401                 {
402                         lbsmsg_it = newLBSMsgCBList.erase(lbsmsg_it);
403                 }
404                 else
405                 {
406                         ++lbsmsg_it;
407                 }
408         }
409
410         // remove all operationSyncMLMsgCBs for fd
411         syncmlop_list::iterator syncmlop_it = operationSyncMLMsgCBList.begin();
412
413         while (syncmlop_it != operationSyncMLMsgCBList.end())
414         {
415                 if (syncmlop_it->listenerFd == fd)
416                 {
417                         syncmlop_it = operationSyncMLMsgCBList.erase(syncmlop_it);
418                 }
419                 else
420                 {
421                         ++syncmlop_it;
422                 }
423         }
424
425         // remove storage change callback for fd
426         storageChangeFdMap.erase(fd);
427
428         MSG_END();
429 }
430
431
432 bool MsgTransactionManager::checkPrivilege(MSG_CMD_TYPE_T CmdType, const char *pCookie)
433 {
434         if (CmdType >= MSG_CMD_PLG_SENT_STATUS_CNF && CmdType <= MSG_CMD_PLG_INIT_SIM_BY_SAT)
435         {
436                 MSG_DEBUG("Request from Plug-in");
437                 return true;
438         }
439
440         // Get Cookie from APP
441         if (pCookie == NULL)
442         {
443                 MSG_DEBUG("Cookie is NULL");
444                 return false;
445         }
446
447         // Check Cookie
448         size_t cookieSize;
449         gid_t gid;
450
451         cookieSize = security_server_get_cookie_size();
452
453         MSG_DEBUG("cookie size : [%d]", cookieSize);
454
455         // Get GID
456         if (CmdType == MSG_CMD_REG_INCOMING_SYNCML_MSG_CB)
457         {
458                 MSG_DEBUG("get GID for message_sync");
459                 gid = security_server_get_gid("message_sync");
460         }
461         else if (CmdType == MSG_CMD_REG_INCOMING_LBS_MSG_CB)
462         {
463                 MSG_DEBUG("get GID for message_lbs");
464                 gid = security_server_get_gid("message_lbs");
465         }
466         else
467         {
468                 MSG_DEBUG("get GID for message");
469                 gid = security_server_get_gid("message");
470         }
471
472         MSG_DEBUG("gid [%d]", gid);
473
474         int retVal = 0;
475
476         retVal = security_server_check_privilege(pCookie, gid);
477
478         if (retVal < 0)
479         {
480                 if (retVal == SECURITY_SERVER_API_ERROR_ACCESS_DENIED)
481                 {
482                         MSG_DEBUG("access denied !! [%d]", retVal);
483                 }
484                 else
485                 {
486                         MSG_DEBUG("fail to check privilege [%d]", retVal);
487                 }
488
489                 return false;
490         }
491
492         MSG_DEBUG("privilege check success !!");
493
494         return true;
495 }
496
497
498 void MsgTransactionManager::setSentStatusCB(int listenerFd)
499 {
500         if (listenerFd <= 0)
501                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d",listenerFd);
502
503         statusCBFdMap[listenerFd] = true;
504 }
505
506
507 void MsgTransactionManager::setIncomingMsgCB(MSG_CMD_REG_INCOMING_MSG_CB_S *pCbInfo)
508 {
509         if (!pCbInfo)
510         {
511                 MSG_FATAL("cbinfo NULL");
512                 return;
513         }
514
515         newmsg_list::iterator it = newMsgCBList.begin();
516
517         for (; it != newMsgCBList.end(); it++)
518         {
519                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (it->port == pCbInfo->port))
520                 {
521                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d, port %d", it->listenerFd, it->msgType, it->port);
522                         return;
523                 }
524         }
525
526         newMsgCBList.push_back(*pCbInfo);
527 }
528
529
530 void MsgTransactionManager::setMMSConfMsgCB(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S *pCbInfo)
531 {
532         if (!pCbInfo)
533         {
534                 MSG_FATAL("cbinfo NULL");
535                 return;
536         }
537
538         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
539
540         for (; it != newMMSConfMsgCBList.end(); it++)
541         {
542                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (!strncmp(it->appId, pCbInfo->appId, MAX_MMS_JAVA_APPID_LEN)))
543                 {
544                         MSG_DEBUG("Duplicated MMSConfMessageCB info fd:%d, mType:%d, appId:%s", it->listenerFd, it->msgType, it->appId);
545                         return;
546                 }
547         }
548
549         newMMSConfMsgCBList.push_back(*pCbInfo);
550 }
551
552
553 void MsgTransactionManager::setSyncMLMsgCB(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S *pCbInfo)
554 {
555         if (!pCbInfo)
556         {
557                 MSG_FATAL("cbinfo NULL");
558                 return;
559         }
560
561         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
562
563         for (; it != newSyncMLMsgCBList.end(); it++)
564         {
565                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
566                 {
567                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
568                         return;
569                 }
570         }
571
572         newSyncMLMsgCBList.push_back(*pCbInfo);
573 }
574
575
576 void MsgTransactionManager::setLBSMsgCB(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S *pCbInfo)
577 {
578         if (!pCbInfo)
579         {
580                 MSG_FATAL("cbinfo NULL");
581                 return;
582         }
583
584         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
585
586         for (; it != newLBSMsgCBList.end(); it++)
587         {
588                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
589                 {
590                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
591                         return;
592                 }
593         }
594
595         newLBSMsgCBList.push_back(*pCbInfo);
596 }
597
598
599 void MsgTransactionManager::setJavaMMSList(MSG_CMD_REG_INCOMING_JAVAMMS_TRID_S *pTrId)
600 {
601         if (!pTrId)
602         {
603                 MSG_FATAL("trId NULL");
604                 return;
605         }
606
607         javamms_list::iterator it;
608
609         for (it = javaMMSList.begin(); it != javaMMSList.end(); it++)
610         {
611                 if (!strcmp(it->id, pTrId->id))
612                 {
613                         MSG_DEBUG("Duplicated javaMMS transaction Id:%s", it->id);
614                         return;
615                 }
616         }
617
618         javaMMSList.push_back(*pTrId);
619 }
620
621
622 void MsgTransactionManager::setSyncMLMsgOperationCB(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S *pCbInfo)
623 {
624         if (!pCbInfo)
625         {
626                 MSG_FATAL("cbinfo NULL");
627                 return;
628         }
629
630         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
631
632         for (; it != operationSyncMLMsgCBList.end(); it++)
633         {
634                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
635                 {
636                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
637                         return;
638                 }
639         }
640
641         operationSyncMLMsgCBList.push_back(*pCbInfo);
642 }
643
644
645 void MsgTransactionManager::setStorageChangeCB(int listenerFd)
646 {
647         if (listenerFd <= 0)
648                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d", listenerFd);
649
650         storageChangeFdMap[listenerFd] = true;
651 }
652
653
654 javamms_list& MsgTransactionManager::getJavaMMSList()
655 {
656         return javaMMSList;
657 }
658
659
660 void MsgTransactionManager::broadcastIncomingMsgCB(const MSG_ERROR_T err, const MSG_MESSAGE_INFO_S *msgInfo)
661 {
662         MSG_BEGIN();
663
664         char* pEventData = NULL;
665         AutoPtr<char> eventBuf(&pEventData);
666
667         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MSG_IND, err, (void**)(&pEventData));
668
669         MSG_DEBUG("valid %d dstport %d", msgInfo->msgPort.valid, msgInfo->msgPort.dstPort);
670
671         newmsg_list::iterator it = newMsgCBList.begin();
672
673         for (; it != newMsgCBList.end(); it++)
674         {
675                 MSG_DEBUG("fd %d dstport %d",it->listenerFd, it->port);
676
677                 if ((msgInfo->msgPort.valid == false) && (it->port == 0))
678                 {
679                         MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
680                         write(it->listenerFd, pEventData, eventSize);
681                 }
682                 else if ((msgInfo->msgPort.valid == true) && (it->port == msgInfo->msgPort.dstPort))
683                 {
684                         MSG_DEBUG("Send incoming port msg to listener %d", it->listenerFd);
685                         write(it->listenerFd, pEventData, eventSize);
686                 }
687         }
688
689         MSG_END();
690 }
691
692
693 void MsgTransactionManager::broadcastMMSConfCB(const MSG_ERROR_T err, const MSG_MESSAGE_INFO_S *msgInfo, const MMS_RECV_DATA_S *mmsRecvData)
694 {
695         MSG_BEGIN();
696
697         char* pEventData = NULL;
698         AutoPtr<char> eventBuf(&pEventData);
699
700         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MMS_CONF, err, (void**)(&pEventData));
701
702         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
703
704         for (; it != newMMSConfMsgCBList.end(); it++)
705         {
706                 MSG_DEBUG("fd:%d appId:%s",it->listenerFd, it->appId);
707
708                 if (mmsRecvData->msgAppId.valid == true)
709                 {
710                         if (!strcmp(it->appId, mmsRecvData->msgAppId.appId))
711                         {
712                                 MSG_DEBUG("Send incoming java msg to listener %d", it->listenerFd);
713                                 write(it->listenerFd, pEventData, eventSize);
714                         }
715                 }
716                 else
717                 {
718                         if (strlen(it->appId) <= 0)
719                         {
720                                 MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
721                                 write(it->listenerFd, pEventData, eventSize);
722                         }
723                 }
724         }
725
726
727         MSG_END();
728 }
729
730
731 void MsgTransactionManager::broadcastSyncMLMsgCB(const MSG_ERROR_T err, const MSG_SYNCML_MESSAGE_DATA_S *syncMLData)
732 {
733         MSG_BEGIN();
734
735         char* pEventData = NULL;
736         AutoPtr<char> eventBuf(&pEventData);
737
738         int eventSize = MsgMakeEvent(syncMLData, sizeof(MSG_SYNCML_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND, err, (void**)(&pEventData));
739
740         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
741
742         for (; it != newSyncMLMsgCBList.end(); it++)
743         {
744                 MSG_DEBUG("Send incoming SyncML information to listener %d", it->listenerFd);
745                 write(it->listenerFd, pEventData, eventSize);
746         }
747
748         MSG_END();
749 }
750
751
752 void MsgTransactionManager::broadcastLBSMsgCB(const MSG_ERROR_T err, const MSG_LBS_MESSAGE_DATA_S *lbsData)
753 {
754         MSG_BEGIN();
755
756         char* pEventData = NULL;
757         AutoPtr<char> eventBuf(&pEventData);
758
759         int eventSize = MsgMakeEvent(lbsData, sizeof(MSG_LBS_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_LBS_MSG_IND, err, (void**)(&pEventData));
760
761         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
762
763         for (; it != newLBSMsgCBList.end(); it++)
764         {
765                 MSG_DEBUG("Send incoming LBS msg to listener %d", it->listenerFd);
766                 write(it->listenerFd, pEventData, eventSize);
767         }
768
769         MSG_END();
770 }
771
772
773 void MsgTransactionManager::broadcastSyncMLMsgOperationCB(const MSG_ERROR_T err, const int msgId, const int extId)
774 {
775         MSG_BEGIN();
776
777         char* pEventData = NULL;
778         AutoPtr<char> eventBuf(&pEventData);
779
780         char* encodedData = NULL;
781         AutoPtr<char> buf(&encodedData);
782
783         // Encoding Storage Change Data
784         int dataSize = MsgEncodeSyncMLOperationData(msgId, extId, &encodedData);
785
786         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_SYNCML_OPERATION, err, (void**)(&pEventData));
787
788         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
789
790         for( ; it != operationSyncMLMsgCBList.end() ; it++ )
791         {
792                 MSG_DEBUG("Send SyncML operation to listener %d", it->listenerFd);
793                 write(it->listenerFd, pEventData, eventSize);
794         }
795
796         MSG_END();
797 }
798
799
800 void MsgTransactionManager::broadcastStorageChangeCB(const MSG_ERROR_T err, const MSG_STORAGE_CHANGE_TYPE_T storageChangeType, const MSG_MSGID_LIST_S *pMsgIdList)
801 {
802         MSG_BEGIN();
803
804         if(pMsgIdList == NULL) {
805                 MSG_DEBUG("pMsgIdList is NULL.");
806                 return;
807         }
808
809         MSG_DEBUG("storageChangeType [%d]", storageChangeType);
810
811         int dataSize = 0;
812
813         char* pEventData = NULL;
814         AutoPtr<char> eventBuf(&pEventData);
815
816         char* encodedData = NULL;
817         AutoPtr<char> buf(&encodedData);
818
819         // Encoding Storage Change Data
820         dataSize = MsgEncodeStorageChangeData(storageChangeType, pMsgIdList, &encodedData);
821
822         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_STORAGE_CHANGE_IND, err, (void**)(&pEventData));
823
824         fd_map::iterator it = storageChangeFdMap.begin();
825
826         for (; it != storageChangeFdMap.end(); it++)
827         {
828                 MSG_DEBUG("Send Storage Change Callback to listener %d", it->first);
829                 write(it->first, pEventData, eventSize);
830         }
831
832         MSG_END();
833 }
834
835
836 void MsgTransactionManager::setTMStatus()
837 {
838         MSG_BEGIN();
839         mx.lock();
840         cv.signal();
841         mx.unlock();
842         MSG_END();
843 }
844
845
846 void MsgTransactionManager::getTMStatus()
847 {
848         MSG_BEGIN();
849         mx.lock();
850
851         int ret = 0;
852
853         ret = cv.timedwait(mx.pMutex(), 3);
854
855         mx.unlock();
856
857         if (ret == ETIMEDOUT)
858         {
859                 MSG_DEBUG("MsgTransactionManager::getTMStatus TIME-OUT");
860         }
861         MSG_END();
862 }