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