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