Sync with tizen 2.4
[platform/core/messaging/msg-service.git] / framework / transaction-manager / MsgTransManager.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <pthread.h>
22
23 #include <security-server.h>
24
25 #include "MsgDebug.h"
26 #include "MsgMemory.h"
27 #include "MsgException.h"
28 #include "MsgCppTypes.h"
29 #include "MsgContact.h"
30 #include "MsgIpcSocket.h"
31 #include "MsgGconfWrapper.h"
32 #include "MsgUtilFunction.h"
33 #include "MsgUtilFile.h"
34 #include "MsgLbs.h"
35 #include "MsgCmdHandler.h"
36 #include "MsgSettingHandler.h"
37 #include "MsgStorageHandler.h"
38 #include "MsgPluginManager.h"
39 #include "MsgTransManager.h"
40
41 #define MSG_CHECK_PRIVILEGE
42
43
44 /*==================================================================================================
45                                      IMPLEMENTATION OF MsgTransactionManager - Member Functions
46 ==================================================================================================*/
47 MsgTransactionManager* MsgTransactionManager::pInstance = NULL;
48 MsgIpcServerSocket MsgTransactionManager::servSock;
49
50
51 MsgTransactionManager::MsgTransactionManager() : running(false), mx(), cv(), eventQueue()
52 {
53         sentMsgMap.clear();
54 #ifdef MSG_PENDING_PUSH_MESSAGE
55         pushMsgList.clear();
56 #endif
57         statusCBFdMap.clear();
58         newMsgCBList.clear();
59         newMMSConfMsgCBList.clear();
60         newSyncMLMsgCBList.clear();
61         newLBSMsgCBList.clear();
62         javaMMSList.clear();
63         operationSyncMLMsgCBList.clear();
64         storageChangeFdMap.clear();
65
66         handlerMap.clear();
67
68 //      Fill in mMsgHandlers, as given in the below.
69         handlerMap[MSG_CMD_ADD_MSG]                             = &MsgAddMessageHandler;
70         handlerMap[MSG_CMD_ADD_SYNCML_MSG]              = &MsgAddSyncMLMessageHandler;
71         handlerMap[MSG_CMD_UPDATE_MSG]                  = &MsgUpdateMessageHandler;
72         handlerMap[MSG_CMD_UPDATE_READ]                         = &MsgUpdateReadStatusHandler;
73         handlerMap[MSG_CMD_UPDATE_PROTECTED]            = &MsgUpdateProtectedStatusHandler;
74         handlerMap[MSG_CMD_DELETE_MSG]                  = &MsgDeleteMessageHandler;
75         handlerMap[MSG_CMD_DELALL_MSGINFOLDER]  = &MsgDeleteAllMessageInFolderHandler;
76         handlerMap[MSG_CMD_MOVE_MSGTOFOLDER]    = &MsgMoveMessageToFolderHandler;
77         handlerMap[MSG_CMD_MOVE_MSGTOSTORAGE]   = &MsgMoveMessageToStorageHandler;
78         handlerMap[MSG_CMD_COUNT_MSG]                   = &MsgCountMessageHandler;
79         handlerMap[MSG_CMD_GET_MSG]                             = &MsgGetMessageHandler;
80         handlerMap[MSG_CMD_GET_FOLDERVIEWLIST]  = &MsgGetFolderViewListHandler;
81
82         handlerMap[MSG_CMD_ADD_FOLDER]                  = &MsgAddFolderHandler;
83         handlerMap[MSG_CMD_UPDATE_FOLDER]               = &MsgUpdateFolderHandler;
84         handlerMap[MSG_CMD_DELETE_FOLDER]               = &MsgDeleteFolderHandler;
85         handlerMap[MSG_CMD_GET_FOLDERLIST]              = &MsgGetFolderListHandler;
86
87         handlerMap[MSG_CMD_ADD_FILTER]                  = &MsgAddFilterHandler;
88         handlerMap[MSG_CMD_UPDATE_FILTER]                       = &MsgUpdateFilterHandler;
89         handlerMap[MSG_CMD_DELETE_FILTER]               = &MsgDeleteFilterHandler;
90         handlerMap[MSG_CMD_GET_FILTERLIST]              = &MsgGetFilterListHandler;
91         handlerMap[MSG_CMD_SET_FILTER_OPERATION]        = &MsgSetFilterOperationHandler;
92         handlerMap[MSG_CMD_GET_FILTER_OPERATION]        = &MsgGetFilterOperationHandler;
93         handlerMap[MSG_CMD_SET_FILTER_ACTIVATION] = &MsgSetFilterActivationHandler;
94
95         handlerMap[MSG_CMD_GET_MSG_TYPE]                        = &MsgGetMsgTypeHandler;
96
97         handlerMap[MSG_CMD_SUBMIT_REQ]                  = &MsgSubmitReqHandler;
98         handlerMap[MSG_CMD_CANCEL_REQ]                  = &MsgCancelReqHandler;
99
100         handlerMap[MSG_CMD_REG_SENT_STATUS_CB]  = &MsgRegSentStatusCallbackHandler;
101         handlerMap[MSG_CMD_REG_STORAGE_CHANGE_CB] = &MsgRegStorageChangeCallbackHandler;
102         handlerMap[MSG_CMD_REG_INCOMING_MSG_CB] = &MsgRegIncomingMsgCallbackHandler;
103         handlerMap[MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB]        = &MsgRegIncomingMMSConfMsgCallbackHandler;
104         handlerMap[MSG_CMD_REG_INCOMING_SYNCML_MSG_CB]  = &MsgRegIncomingSyncMLMsgCallbackHandler;
105         handlerMap[MSG_CMD_REG_INCOMING_PUSH_MSG_CB]    = &MsgRegIncomingPushMsgCallbackHandler;
106         handlerMap[MSG_CMD_REG_INCOMING_CB_MSG_CB]      = &MsgRegIncomingCBMsgCallbackHandler;
107         handlerMap[MSG_CMD_REG_INCOMING_LBS_MSG_CB]     = &MsgRegIncomingLBSMsgCallbackHandler;
108         handlerMap[MSG_CMD_REG_SYNCML_MSG_OPERATION_CB] = &MsgRegSyncMLMsgOperationCallbackHandler;
109         handlerMap[MSG_CMD_REG_REPORT_MSG_INCOMING_CB] = &MsgRegIncomingReportMsgCallbackHandler;
110
111         handlerMap[MSG_CMD_PLG_SENT_STATUS_CNF] = &MsgSentStatusHandler;
112         handlerMap[MSG_CMD_PLG_STORAGE_CHANGE_IND]      = &MsgStorageChangeHandler;
113         handlerMap[MSG_CMD_PLG_INCOMING_MSG_IND]        = &MsgIncomingMsgHandler;
114         handlerMap[MSG_CMD_PLG_INCOMING_MMS_CONF]       = &MsgIncomingMMSConfMsgHandler;
115         handlerMap[MSG_CMD_PLG_INCOMING_PUSH_IND]       = &MsgIncomingPushMsgHandler;
116         handlerMap[MSG_CMD_PLG_INCOMING_CB_IND] = &MsgIncomingCBMsgHandler;
117
118         handlerMap[MSG_CMD_PLG_INCOMING_SYNCML_IND] = &MsgIncomingSyncMLMsgHandler;
119         handlerMap[MSG_CMD_PLG_INCOMING_LBS_IND] = &MsgIncomingLBSMsgHandler;
120         handlerMap[MSG_CMD_PLG_INIT_SIM_BY_SAT] = &MsgInitSimBySatHandler;
121
122         handlerMap[MSG_CMD_GET_THREADVIEWLIST]  = &MsgGetThreadViewListHandler;
123         handlerMap[MSG_CMD_GET_CONVERSATIONVIEWLIST]    = &MsgGetConversationViewListHandler;
124         handlerMap[MSG_CMD_DELETE_THREADMESSAGELIST]    = &MsgDeleteThreadMessageListHandler;
125
126         handlerMap[MSG_CMD_GET_CONTACT_COUNT]   = &MsgCountMsgByContactHandler;
127         handlerMap[MSG_CMD_GET_QUICKPANEL_DATA]         = &MsgGetQuickPanelDataHandler;
128         handlerMap[MSG_CMD_COUNT_BY_MSGTYPE]    = &MsgCountMsgByTypeHandler;
129         handlerMap[MSG_CMD_RESET_DB]    = &MsgResetDatabaseHandler;
130         handlerMap[MSG_CMD_GET_MEMSIZE]         = &MsgGetMemSizeHandler;
131
132         handlerMap[MSG_CMD_BACKUP_MESSAGE]      = &MsgBackupMessageHandler;
133         handlerMap[MSG_CMD_RESTORE_MESSAGE] = &MsgRestoreMessageHandler;
134
135         handlerMap[MSG_CMD_UPDATE_THREAD_READ] = &MsgUpdateThreadReadStatusHandler;
136
137         handlerMap[MSG_CMD_SYNCML_OPERATION] = &MsgSyncMLMsgOperationHandler;
138         handlerMap[MSG_CMD_GET_REPORT_STATUS]                   = &MsgGetReportStatusHandler;
139
140         handlerMap[MSG_CMD_GET_THREAD_ID_BY_ADDRESS] = &MsgGetThreadIdByAddressHandler;
141         handlerMap[MSG_CMD_GET_THREAD_INFO] = &MsgGetThreadInfoHandler;
142         handlerMap[MSG_CMD_GET_SMSC_OPT] = &MsgGetConfigHandler;
143         handlerMap[MSG_CMD_GET_CB_OPT] = &MsgGetConfigHandler;
144         handlerMap[MSG_CMD_GET_SMS_SEND_OPT] = &MsgGetConfigHandler;
145         handlerMap[MSG_CMD_GET_MMS_SEND_OPT] = &MsgGetConfigHandler;
146         handlerMap[MSG_CMD_GET_MMS_RECV_OPT] = &MsgGetConfigHandler;
147         handlerMap[MSG_CMD_GET_PUSH_MSG_OPT] = &MsgGetConfigHandler;
148         handlerMap[MSG_CMD_GET_VOICE_MSG_OPT] = &MsgGetConfigHandler;
149         handlerMap[MSG_CMD_GET_GENERAL_MSG_OPT] = &MsgGetConfigHandler;
150         handlerMap[MSG_CMD_GET_MSG_SIZE_OPT] = &MsgGetConfigHandler;
151
152         handlerMap[MSG_CMD_SET_SMSC_OPT] = &MsgSetConfigHandler;
153         handlerMap[MSG_CMD_SET_CB_OPT] = &MsgSetConfigHandler;
154         handlerMap[MSG_CMD_SET_SMS_SEND_OPT] = &MsgSetConfigHandler;
155         handlerMap[MSG_CMD_SET_MMS_SEND_OPT] = &MsgSetConfigHandler;
156         handlerMap[MSG_CMD_SET_MMS_RECV_OPT] = &MsgSetConfigHandler;
157         handlerMap[MSG_CMD_SET_PUSH_MSG_OPT] = &MsgSetConfigHandler;
158         handlerMap[MSG_CMD_SET_VOICE_MSG_OPT] = &MsgSetConfigHandler;
159         handlerMap[MSG_CMD_SET_GENERAL_MSG_OPT] = &MsgSetConfigHandler;
160         handlerMap[MSG_CMD_SET_MSG_SIZE_OPT] = &MsgSetConfigHandler;
161
162         handlerMap[MSG_CMD_ADD_PUSH_EVENT] = &MsgAddPushEventHandler;
163         handlerMap[MSG_CMD_DELETE_PUSH_EVENT] = &MsgDeletePushEventHandler;
164         handlerMap[MSG_CMD_UPDATE_PUSH_EVENT] = &MsgUpdatePushEventHandler;
165         handlerMap[MSG_CMD_DELETE_MESSAGE_BY_LIST] = &MsgDeleteMessageByListHandler;
166         handlerMap[MSG_CMD_ADD_SIM_MSG] = &MsgAddSimMessageHandler;
167         handlerMap[MSG_CMD_PLG_RESEND_MESSAGE] = &MsgResendMessageHandler;
168 #ifdef MSG_PENDING_PUSH_MESSAGE
169         handlerMap[MSG_CMD_SEND_PENDING_PUSH_MESSAGE] = &MsgSendPendingPushMsgHandler;
170 #endif
171 #ifdef FEATURE_SMS_CDMA
172         handlerMap[MSG_CMD_PLG_CHECK_UNIQUENESS] = &MsgCheckUniquenessHandler;
173 #endif
174         handlerMap[MSG_CMD_UPDATE_IMSI] = &MsgUpdateIMSIHandler;
175 }
176
177
178 MsgTransactionManager::~MsgTransactionManager()
179 {
180 //      pthread_cond_init(&retCV, NULL); // = PTHREAD_COND_INITIALIZER;
181
182 }
183
184
185 MsgTransactionManager* MsgTransactionManager::instance()
186 {
187         if (!pInstance)
188                 pInstance = new MsgTransactionManager();
189
190         return pInstance;
191 }
192
193 static void* worker_event_queue(void* arg)
194 {
195         MsgTransactionManager::instance()->workerEventQueue();
196         return NULL;
197 }
198
199 void MsgTransactionManager::run()
200 {
201         servSock.open(MSG_SOCKET_PATH);
202
203         fd_set readfds = servSock.fdSet();
204         int nfds = 0;
205
206         MSG_DEBUG("Start Transaction Manager");
207
208         // Set Msg FW Ready Flag
209         if(MsgSettingSetBool(VCONFKEY_MSG_SERVER_READY, true) != MSG_SUCCESS)
210                 MSG_DEBUG("MsgSettingSetBool FAIL : VCONFKEY_MSG_SERVER_READY");
211         MSG_INFO("### VCONFKEY_MSG_SERVER_READY ###");
212
213      /* running worker for plg task */
214         pthread_t tv;
215         if (pthread_create (&tv, NULL, &worker_event_queue, NULL) != 0) {
216                 THROW(MsgException::SERVER_READY_ERROR, "cannot create thread [%d]", errno);
217         }
218
219         while(1)
220         {
221                 readfds = servSock.fdSet();
222                 nfds = servSock.maxFd();
223
224                 MSG_DEBUG("Wait For Select() : nfds %d", nfds);
225
226                 // set Status;
227 //              setTMStatus();
228
229                 if(select(nfds, &readfds, NULL, NULL, NULL) == -1) {
230                         THROW(MsgException::SELECT_ERROR, "select error : %s", strerror(errno));
231                 }
232
233                 try
234                 {
235                         for (int i=0 ; i < nfds; i++)
236                         {
237                                 if (FD_ISSET(i, &readfds))
238                                 {
239                                         if (i == servSock.fd()) // if it is socket connection request
240                                                 servSock.accept();
241                                         else
242                                                 handleRequest(i);
243                                 }
244                         }
245                 }
246                 catch (MsgException& e)
247                 {
248                         MSG_FATAL("%s", e.what());
249                 }
250                 catch (exception& e)
251                 {
252                         MSG_FATAL("%s", e.what());
253                 }
254
255                 // Release Memory
256                 MsgReleaseMemory();
257         }
258 }
259
260
261 void MsgTransactionManager::write(int fd, const char* buf, int len)
262 {
263         servSock.write(fd, buf, len);
264 }
265
266
267 void MsgTransactionManager::insertSentMsg(int reqId, MSG_PROXY_INFO_S* pPrxInfo)
268 {
269         if (pPrxInfo == NULL)
270                 THROW(MsgException::SENT_STATUS_ERROR, "Input Parameter is NULL");
271
272         MSG_DEBUG("msg for submit: reqId %d listenerFd %d handleAddr %x", reqId, pPrxInfo->listenerFd, pPrxInfo->handleAddr);
273
274         fd_map::iterator it = statusCBFdMap.find(pPrxInfo->listenerFd);
275
276         if (it == statusCBFdMap.end()) { // if the status CB is not registered
277                 MSG_DEBUG("No sent_status registered for fd %d", pPrxInfo->listenerFd);
278         } else {
279                 sentMsgMap.insert(make_pair(reqId, *pPrxInfo));
280         }
281 }
282
283
284 MSG_PROXY_INFO_S* MsgTransactionManager::getProxyInfo(int reqId)
285 {
286         sentmsg_map::iterator it = sentMsgMap.find(reqId);
287
288         if (it == sentMsgMap.end())
289         {
290                 MSG_DEBUG("No sent status cb found");
291                 return NULL;
292         }
293
294         return &(it->second);
295 }
296
297
298 void MsgTransactionManager::delProxyInfo(int reqId)
299 {
300         sentmsg_map::iterator it = sentMsgMap.find(reqId);
301
302         if (it == sentMsgMap.end())
303         {
304                 THROW(MsgException::SENT_STATUS_ERROR, "channel info does not exist");
305         }
306
307         sentMsgMap.erase(it);
308 }
309
310 void MsgTransactionManager::workerEventQueue()
311 {
312         MSG_CMD_S* pCmd = NULL;
313         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
314         char* pEventData = NULL;
315         AutoPtr<char> eventBuf(&pEventData);
316
317         int fd = -1;
318
319         while (1) {
320                 mx.lock();
321                 while (!eventQueue.front(&pCmd)) { /* if no item, wait */
322                         MSG_DEBUG ("waiting for task");
323                         cv.wait(mx.pMutex());
324                 }
325                 eventQueue.pop_front(); /* pop it from queue*/
326                 mx.unlock();
327
328                 if (!pCmd) {
329                         MSG_FATAL("pCmd NULL");
330                         continue;
331                 }
332
333                 memcpy (&fd, pCmd->cmdCookie, sizeof(int));
334                 if (fd < 0 ) {
335                         MSG_FATAL("fd [%d] < 0", fd);
336                         g_free (pCmd); pCmd = NULL;
337                         continue;
338                 }
339                 pfHandler = handlerMap[pCmd->cmdType];
340                 if (!pfHandler) {
341                         MSG_FATAL("No handler for %d", pCmd->cmdType);
342                         g_free (pCmd); pCmd = NULL;
343                         continue;
344                 }
345
346                 // run handler function
347                 int eventSize = pfHandler(pCmd, &pEventData);
348
349                 if (eventSize == 0 || pEventData == NULL) {
350                         MSG_FATAL("event size[%d] = 0 or event data = NULL", eventSize);
351                         g_free (pCmd); pCmd = NULL;
352                         continue;
353                 }
354
355                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
356                 servSock.write(fd, pEventData, eventSize);
357                 g_free (pCmd); pCmd = NULL;
358         }
359 }
360
361 void MsgTransactionManager::handleRequest(int fd)
362 {
363         MSG_BEGIN();
364
365         MSG_DEBUG("Event from fd %d", fd);
366
367         char* buf = NULL;
368         AutoPtr<char> wrap(&buf);
369         int len;
370         int ret = servSock.read(fd, &buf, &len);
371
372         if( ret == CLOSE_CONNECTION_BY_SIGNAL || ret == CLOSE_CONNECTION_BY_USER || ret < 0)
373         {
374                 MSG_DEBUG("Read value [%d]", ret);
375                 cleanup(fd);
376                 return;
377         }
378
379         if (len <= 0)
380                 THROW(MsgException::INVALID_RESULT, "read buffer size <= 0");
381
382         char* pEventData = NULL;
383         AutoPtr<char> eventBuf(&pEventData);
384
385         int eventSize = 0;
386
387         // decoding cmd from APP
388         MSG_CMD_S* pCmd = (MSG_CMD_S*) buf;
389         MSG_DEBUG("Command Type [%d : %s]", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType));
390
391         if (pCmd->cmdType > MSG_CMD_NUM)
392                 THROW(MsgException::OUT_OF_RANGE, "request CMD is not defined");
393
394         // check privilege
395 //      if (checkPrivilege(pCmd->cmdType, pCmd->cmdCookie) == false) {
396         if (checkPrivilege(fd, pCmd->cmdType) == false) {
397                 MSG_DEBUG("No Privilege rule. Not allowed.");
398 #ifdef MSG_CHECK_PRIVILEGE
399                 eventSize = sizeof(MSG_EVENT_S);
400
401                 pEventData = new char[eventSize];
402
403                 MSG_EVENT_S* pMsgEvent = (MSG_EVENT_S*)pEventData;
404
405                 pMsgEvent->eventType = pCmd->cmdType;
406                 pMsgEvent->result = MSG_ERR_PERMISSION_DENIED;
407
408                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
409                 servSock.write(fd, pEventData, eventSize);
410
411                 return;
412 #endif
413         }
414
415         // determine the handler based on pCmd->cmdType
416         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
417
418         switch (pCmd->cmdType) {
419         case MSG_CMD_PLG_SENT_STATUS_CNF:
420         case MSG_CMD_PLG_STORAGE_CHANGE_IND:
421         case MSG_CMD_PLG_INCOMING_MSG_IND:
422         case MSG_CMD_PLG_INCOMING_MMS_CONF:
423         case MSG_CMD_PLG_INCOMING_SYNCML_IND:
424         case MSG_CMD_PLG_INCOMING_LBS_IND:
425         case MSG_CMD_PLG_INIT_SIM_BY_SAT:
426         case MSG_CMD_PLG_INCOMING_PUSH_IND:
427         case MSG_CMD_PLG_INCOMING_CB_IND: {
428
429                 MSG_CMD_S* pCmdDup = (MSG_CMD_S*) calloc (1, len); /* pCmdDup should be freed afterward */
430                 memcpy (pCmdDup, pCmd, len);
431                 memcpy (pCmdDup->cmdCookie, &fd, sizeof(int)); /* Now, cmdCookie keeps fd for return */
432
433                 mx.lock(); /* aquire lock before adding cmd */
434                 eventQueue.push_back(pCmdDup);
435                 cv.signal(); /* wake up worker */
436                 mx.unlock();
437                 break;
438         }
439         default:
440                 pfHandler = handlerMap[pCmd->cmdType];
441                 if (!pfHandler)
442                         THROW(MsgException::INVALID_PARAM, "No handler for %d", pCmd->cmdType);
443
444                 // run handler function
445                 eventSize = pfHandler(pCmd, &pEventData);
446
447                 if (eventSize == 0 || pEventData == NULL)
448                         THROW(MsgException::INVALID_RESULT, "event size = 0 or event data = NULL");
449
450                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
451
452                 servSock.write(fd, pEventData, eventSize);
453         }
454
455         MSG_END();
456 }
457
458
459 // terminating the socket connection between ipc server and ipc client
460 void MsgTransactionManager::cleanup(int fd)
461 {
462         MSG_BEGIN();
463
464         servSock.close(fd);
465
466         MSG_DEBUG("fd %d disonnected", fd);
467
468         // remove sent msg info for fd
469         sentmsg_map::iterator sentmsg_it = sentMsgMap.begin();
470
471         for (; sentmsg_it != sentMsgMap.end(); sentmsg_it++)
472         {
473                 if (sentmsg_it->second.listenerFd == fd)
474                 {
475                         sentmsg_it->second.listenerFd = 0;
476                         sentmsg_it->second.handleAddr = 0;
477                 }
478         }
479
480         // remove sent status callback for fd
481         statusCBFdMap.erase(fd);
482
483         MSG_DEBUG("After erase fd [%d], statusCBFdMap has below.", fd);
484         fd_map::iterator it = statusCBFdMap.begin();
485         for (; it!=statusCBFdMap.end(); ++it)
486                 MSG_DEBUG("[%d]", it->first);
487
488         // remove all newMsgCBs for fd
489         newmsg_list::iterator newmsg_it = newMsgCBList.begin();
490
491         while (newmsg_it != newMsgCBList.end())
492         {
493                 if (newmsg_it->listenerFd == fd)
494                 {
495                         newmsg_it = newMsgCBList.erase(newmsg_it);
496                 }
497                 else
498                 {
499                         ++newmsg_it;
500                 }
501         }
502
503         // remove all newMMSConfMsgCBs for fd
504         mmsconf_list::iterator mmsconf_it = newMMSConfMsgCBList.begin();
505
506         while (mmsconf_it != newMMSConfMsgCBList.end())
507         {
508                 if (mmsconf_it->listenerFd == fd)
509                 {
510                         mmsconf_it = newMMSConfMsgCBList.erase(mmsconf_it);
511                 }
512                 else
513                 {
514                         ++mmsconf_it;
515                 }
516         }
517
518         // remove all newSyncMLMsgCBs for fd
519         syncmlmsg_list::iterator syncmlmsg_it = newSyncMLMsgCBList.begin();
520
521         while (syncmlmsg_it != newSyncMLMsgCBList.end())
522         {
523                 if (syncmlmsg_it->listenerFd == fd)
524                 {
525                         syncmlmsg_it = newSyncMLMsgCBList.erase(syncmlmsg_it);
526                 }
527                 else
528                 {
529                         ++syncmlmsg_it;
530                 }
531         }
532
533         // remove all newLBSMsgCBs for fd
534         lbsmsg_list::iterator lbsmsg_it = newLBSMsgCBList.begin();
535
536         while (lbsmsg_it != newLBSMsgCBList.end())
537         {
538                 if (lbsmsg_it->listenerFd == fd)
539                 {
540                         lbsmsg_it = newLBSMsgCBList.erase(lbsmsg_it);
541                 }
542                 else
543                 {
544                         ++lbsmsg_it;
545                 }
546         }
547
548         // remove all newPushMsgCBs for fd
549         pushmsg_list::iterator pushmsg_it = newPushMsgCBList.begin();
550
551         while (pushmsg_it != newPushMsgCBList.end())
552         {
553                 if (pushmsg_it->listenerFd == fd)
554                 {
555                         pushmsg_it = newPushMsgCBList.erase(pushmsg_it);
556                 }
557                 else
558                 {
559                         ++pushmsg_it;
560                 }
561         }
562
563         // remove all newCBMsgCBs for fd
564         cbmsg_list::iterator cbmsg_it = newCBMsgCBList.begin();
565         //bool bSave = false;
566
567         while (cbmsg_it != newCBMsgCBList.end())
568         {
569                 if (cbmsg_it->listenerFd == fd)
570                 {
571                         cbmsg_it = newCBMsgCBList.erase(cbmsg_it);
572                 }
573                 else
574                 {
575                         //if (cbmsg_it->bsave == true)
576                         //      bSave = true;
577                         ++cbmsg_it;
578                 }
579         }
580
581         // remove all operationSyncMLMsgCBs for fd
582         syncmlop_list::iterator syncmlop_it = operationSyncMLMsgCBList.begin();
583
584         while (syncmlop_it != operationSyncMLMsgCBList.end())
585         {
586                 if (syncmlop_it->listenerFd == fd)
587                 {
588                         syncmlop_it = operationSyncMLMsgCBList.erase(syncmlop_it);
589                 }
590                 else
591                 {
592                         ++syncmlop_it;
593                 }
594         }
595
596         // remove storage change callback for fd
597         storageChangeFdMap.erase(fd);
598
599         MSG_DEBUG("After erase fd [%d], storageChangeFdMap has below.", fd);
600         it = storageChangeFdMap.begin();
601         for (; it!=storageChangeFdMap.end(); ++it)
602                 MSG_DEBUG("[%d]", it->first);
603
604         // remove report msg incoming callback for fd
605         reportMsgCBFdMap.erase(fd);
606
607         MSG_DEBUG("After erase fd [%d], reportMsgCBFdMap has below.", fd);
608         it = reportMsgCBFdMap.begin();
609         for (; it!=reportMsgCBFdMap.end(); ++it)
610                 MSG_DEBUG("[%d]", it->first);
611
612         MSG_END();
613 }
614
615
616 #if 1
617 bool MsgTransactionManager::checkPrivilege(int fd, MSG_CMD_TYPE_T CmdType)
618 {
619         bool bAllowed = true;
620         switch(CmdType)
621         {
622         case MSG_CMD_GET_MSG:
623         case MSG_CMD_COUNT_MSG:
624         case MSG_CMD_COUNT_BY_MSGTYPE:
625         case MSG_CMD_REG_INCOMING_MSG_CB:
626         case MSG_CMD_REG_INCOMING_CB_MSG_CB:
627         case MSG_CMD_REG_INCOMING_PUSH_MSG_CB:
628         case MSG_CMD_REG_SENT_STATUS_CB:
629         case MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB:
630         case MSG_CMD_REG_INCOMING_SYNCML_MSG_CB:
631         case MSG_CMD_REG_INCOMING_LBS_MSG_CB:
632         case MSG_CMD_REG_SYNCML_MSG_OPERATION_CB:
633         case MSG_CMD_REG_REPORT_MSG_INCOMING_CB:
634         case MSG_CMD_GET_CONTACT_COUNT:
635         case MSG_CMD_GET_FOLDERLIST:
636         case MSG_CMD_GET_QUICKPANEL_DATA:
637         case MSG_CMD_GET_MEMSIZE:
638         case MSG_CMD_BACKUP_MESSAGE:
639         case MSG_CMD_REG_STORAGE_CHANGE_CB:
640         case MSG_CMD_GET_REPORT_STATUS:
641         case MSG_CMD_GET_THREAD_ID_BY_ADDRESS:
642         case MSG_CMD_GET_THREAD_INFO:
643         case MSG_CMD_SYNCML_OPERATION:
644         case MSG_CMD_GET_FILTERLIST:
645         case MSG_CMD_GET_FILTER_OPERATION:
646         case MSG_CMD_GET_SMSC_OPT:
647         case MSG_CMD_GET_CB_OPT:
648         case MSG_CMD_GET_SMS_SEND_OPT:
649         case MSG_CMD_GET_MMS_SEND_OPT:
650         case MSG_CMD_GET_MMS_RECV_OPT:
651         case MSG_CMD_GET_PUSH_MSG_OPT:
652         case MSG_CMD_GET_VOICE_MSG_OPT:
653         case MSG_CMD_GET_GENERAL_MSG_OPT:
654         case MSG_CMD_GET_MSG_SIZE_OPT:
655         {
656                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::read", "rw");
657                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
658                         MSG_DEBUG("No msg-service::read rw rule.");
659                         bAllowed = false;
660                 }
661         }
662         break;
663         case MSG_CMD_SUBMIT_REQ:
664         case MSG_CMD_SET_CB_OPT:
665         case MSG_CMD_ADD_PUSH_EVENT:
666         case MSG_CMD_DELETE_PUSH_EVENT:
667         case MSG_CMD_UPDATE_PUSH_EVENT:
668         case MSG_CMD_ADD_MSG:
669         case MSG_CMD_ADD_SYNCML_MSG:
670         case MSG_CMD_UPDATE_MSG:
671         case MSG_CMD_UPDATE_READ:
672         case MSG_CMD_UPDATE_PROTECTED:
673         case MSG_CMD_DELETE_MSG:
674         case MSG_CMD_DELALL_MSGINFOLDER:
675         case MSG_CMD_MOVE_MSGTOFOLDER:
676         case MSG_CMD_MOVE_MSGTOSTORAGE:
677         case MSG_CMD_DELETE_THREADMESSAGELIST:
678         case MSG_CMD_ADD_FOLDER:
679         case MSG_CMD_UPDATE_FOLDER:
680         case MSG_CMD_DELETE_FOLDER:
681         case MSG_CMD_RESET_DB:
682         case MSG_CMD_RESTORE_MESSAGE:
683         case MSG_CMD_DELETE_MESSAGE_BY_LIST:
684         case MSG_CMD_UPDATE_THREAD_READ:
685         case MSG_CMD_ADD_FILTER:
686         case MSG_CMD_UPDATE_FILTER:
687         case MSG_CMD_DELETE_FILTER:
688         case MSG_CMD_SET_FILTER_OPERATION:
689         case MSG_CMD_SET_FILTER_ACTIVATION:
690         case MSG_CMD_SET_SMSC_OPT:
691         case MSG_CMD_SET_SMS_SEND_OPT:
692         case MSG_CMD_SET_MMS_SEND_OPT:
693         case MSG_CMD_SET_MMS_RECV_OPT:
694         case MSG_CMD_SET_PUSH_MSG_OPT:
695         case MSG_CMD_SET_VOICE_MSG_OPT:
696         case MSG_CMD_SET_GENERAL_MSG_OPT:
697         case MSG_CMD_SET_MSG_SIZE_OPT:
698         {
699                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::write", "rw");
700                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
701                         MSG_DEBUG("No msg-service::write rw rule.");
702                         bAllowed = false;
703                 }
704         }
705         break;
706         }
707
708         return bAllowed;
709 }
710 #else
711 bool MsgTransactionManager::checkPrivilege(MSG_CMD_TYPE_T CmdType, const char *pCookie)
712 {
713         if (CmdType >= MSG_CMD_PLG_SENT_STATUS_CNF && CmdType <= MSG_CMD_PLG_INIT_SIM_BY_SAT)
714         {
715                 MSG_DEBUG("Request from Plug-in");
716                 return true;
717         }
718
719         // Get Cookie from APP
720         if (pCookie == NULL)
721         {
722                 MSG_DEBUG("Cookie is NULL");
723                 return false;
724         }
725
726 #ifdef MSG_FOR_DEBUG
727         for (int i = 0; i < MAX_COOKIE_LEN; i++)
728         {
729                 MSG_DEBUG("cookie : [%02x]", pCookie[i]);
730         }
731 #endif
732
733         // Check Cookie
734         size_t cookieSize;
735         gid_t gid;
736
737         cookieSize = security_server_get_cookie_size();
738
739         MSG_DEBUG("cookie size : [%d]", cookieSize);
740
741 //      char cookie[MAX_COOKIE_LEN];
742
743         // Get GID
744         if (CmdType == MSG_CMD_REG_INCOMING_SYNCML_MSG_CB)
745         {
746                 MSG_DEBUG("get GID for message_sync");
747                 gid = security_server_get_gid("message_sync");
748         }
749         else if (CmdType == MSG_CMD_REG_INCOMING_LBS_MSG_CB)
750         {
751                 MSG_DEBUG("get GID for message_lbs");
752                 gid = security_server_get_gid("message_lbs");
753         }
754         else
755         {
756                 MSG_DEBUG("get GID for message");
757                 gid = security_server_get_gid("message");
758         }
759
760         MSG_DEBUG("gid [%d]", gid);
761
762         int retVal = 0;
763
764         retVal = security_server_check_privilege(pCookie, gid);
765
766         if (retVal < 0)
767         {
768                 if (retVal == SECURITY_SERVER_API_ERROR_ACCESS_DENIED)
769                 {
770                         MSG_DEBUG("access denied !! [%d]", retVal);
771                 }
772                 else
773                 {
774                         MSG_DEBUG("fail to check privilege [%d]", retVal);
775                 }
776
777                 return false;
778         }
779
780         MSG_DEBUG("privilege check success !!");
781
782         return true;
783 }
784 #endif
785
786 void MsgTransactionManager::setSentStatusCB(int listenerFd)
787 {
788         if (listenerFd <= 0)
789                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d",listenerFd);
790
791         statusCBFdMap[listenerFd] = true;
792 }
793
794
795 void MsgTransactionManager::setIncomingMsgCB(MSG_CMD_REG_INCOMING_MSG_CB_S *pCbInfo)
796 {
797         if (!pCbInfo)
798         {
799                 MSG_FATAL("cbinfo NULL");
800                 return;
801         }
802
803         newmsg_list::iterator it = newMsgCBList.begin();
804
805         for (; it != newMsgCBList.end(); it++)
806         {
807                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (it->port == pCbInfo->port))
808                 {
809                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d, port %d", it->listenerFd, it->msgType, it->port);
810                         return;
811                 }
812         }
813
814         newMsgCBList.push_back(*pCbInfo);
815 }
816
817
818 void MsgTransactionManager::setMMSConfMsgCB(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S *pCbInfo)
819 {
820         if (!pCbInfo)
821         {
822                 MSG_FATAL("cbinfo NULL");
823                 return;
824         }
825
826         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
827
828         for (; it != newMMSConfMsgCBList.end(); it++)
829         {
830                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (!strncmp(it->appId, pCbInfo->appId, MAX_MMS_JAVA_APPID_LEN)))
831                 {
832                         MSG_DEBUG("Duplicated MMSConfMessageCB info fd:%d, mType:%d, appId:%s", it->listenerFd, it->msgType, it->appId);
833                         return;
834                 }
835         }
836
837         newMMSConfMsgCBList.push_back(*pCbInfo);
838 }
839
840
841 void MsgTransactionManager::setPushMsgCB(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S *pCbInfo)
842 {
843         if (!pCbInfo)
844         {
845                 MSG_FATAL("cbinfo NULL");
846                 return;
847         }
848
849         pushmsg_list::iterator it = newPushMsgCBList.begin();
850
851         for (; it != newPushMsgCBList.end(); it++)
852         {
853                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && !strncmp(it->appId, pCbInfo->appId, MAX_WAPPUSH_ID_LEN))
854                 {
855                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
856                         return;
857                 }
858         }
859
860         newPushMsgCBList.push_back(*pCbInfo);
861 }
862
863 void MsgTransactionManager::setCBMsgCB(MSG_CMD_REG_INCOMING_CB_MSG_CB_S *pCbInfo)
864 {
865         MSG_BEGIN();
866         if (!pCbInfo)
867         {
868                 MSG_FATAL("cbinfo NULL");
869                 return;
870         }
871
872         cbmsg_list::iterator it = newCBMsgCBList.begin();
873
874         for (; it != newCBMsgCBList.end(); it++)
875         {
876                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
877                 {
878                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
879                         return;
880                 }
881         }
882         MSG_DEBUG("bSave : [%d]", pCbInfo->bsave);
883
884         if(pCbInfo->bsave)
885                 if(MsgSettingSetBool(CB_SAVE, pCbInfo->bsave) != MSG_SUCCESS)
886                         MSG_DEBUG("MsgSettingSetBool FAIL: CB_SAVE");
887
888
889         newCBMsgCBList.push_back(*pCbInfo);
890
891         MSG_END();
892 }
893
894
895 void MsgTransactionManager::setSyncMLMsgCB(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S *pCbInfo)
896 {
897         if (!pCbInfo)
898         {
899                 MSG_FATAL("cbinfo NULL");
900                 return;
901         }
902
903         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
904
905         for (; it != newSyncMLMsgCBList.end(); it++)
906         {
907                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
908                 {
909                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
910                         return;
911                 }
912         }
913
914         newSyncMLMsgCBList.push_back(*pCbInfo);
915 }
916
917
918 void MsgTransactionManager::setLBSMsgCB(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S *pCbInfo)
919 {
920         if (!pCbInfo)
921         {
922                 MSG_FATAL("cbinfo NULL");
923                 return;
924         }
925
926         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
927
928         for (; it != newLBSMsgCBList.end(); it++)
929         {
930                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
931                 {
932                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
933                         return;
934                 }
935         }
936
937         newLBSMsgCBList.push_back(*pCbInfo);
938 }
939
940
941 void MsgTransactionManager::setJavaMMSList(MSG_CMD_REG_INCOMING_JAVAMMS_TRID_S *pTrId)
942 {
943         if (!pTrId)
944         {
945                 MSG_FATAL("trId NULL");
946                 return;
947         }
948
949         javamms_list::iterator it;
950
951         for (it = javaMMSList.begin(); it != javaMMSList.end(); it++)
952         {
953                 if (!strcmp(it->id, pTrId->id))
954                 {
955                         MSG_SEC_DEBUG("Duplicated javaMMS transaction Id:%s", it->id);
956                         return;
957                 }
958         }
959
960         javaMMSList.push_back(*pTrId);
961 }
962
963
964 void MsgTransactionManager::setSyncMLMsgOperationCB(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S *pCbInfo)
965 {
966         if (!pCbInfo)
967         {
968                 MSG_FATAL("cbinfo NULL");
969                 return;
970         }
971
972         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
973
974         for (; it != operationSyncMLMsgCBList.end(); it++)
975         {
976                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
977                 {
978                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
979                         return;
980                 }
981         }
982
983         operationSyncMLMsgCBList.push_back(*pCbInfo);
984 }
985
986
987 void MsgTransactionManager::setStorageChangeCB(int listenerFd)
988 {
989         if (listenerFd <= 0)
990                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d", listenerFd);
991
992         storageChangeFdMap[listenerFd] = true;
993 }
994
995
996 void MsgTransactionManager::setReportMsgCB(int listenerFd)
997 {
998         if (listenerFd <= 0)
999                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d", listenerFd);
1000
1001         reportMsgCBFdMap[listenerFd] = true;
1002 }
1003
1004
1005 javamms_list& MsgTransactionManager::getJavaMMSList()
1006 {
1007         return javaMMSList;
1008 }
1009
1010
1011 void MsgTransactionManager::broadcastIncomingMsgCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo)
1012 {
1013         MSG_BEGIN();
1014
1015         if ((msgInfo->msgPort.valid == true) && (msgInfo->msgPort.dstPort == MSG_LBS_PORT)) {
1016                 MSG_DEBUG("Message for LBS.");
1017
1018                 if (msgInfo->bTextSms == false) {
1019                         MSG_DEBUG("msgInfo->bTextSms == false");
1020
1021                         int fileSize = 0;
1022
1023                         char* pFileData = NULL;
1024                         AutoPtr<char> buf(&pFileData);
1025
1026 //                      if (MsgOpenAndReadFile(msgInfo->msgData, &pFileData, &fileSize) == true)
1027 //                              MsgLbsSms(pFileData, fileSize);
1028 //                      else
1029                                 MSG_DEBUG("MsgOpenAndReadFile failed.");
1030                 } else {
1031 //                      MsgLbsSms(msgInfo->msgText, (int)msgInfo->dataSize);
1032                 }
1033                 return;
1034         }
1035
1036         char* pEventData = NULL;
1037         AutoPtr<char> eventBuf(&pEventData);
1038
1039         char* encodedData = NULL;
1040         AutoPtr<char> buf(&encodedData);
1041         int dataSize = MsgEncodeMsgInfo(msgInfo, &encodedData);
1042
1043         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_INCOMING_MSG_IND, err, (void**)(&pEventData));
1044
1045         MSG_DEBUG("valid %d dstport %d", msgInfo->msgPort.valid, msgInfo->msgPort.dstPort);
1046
1047         newmsg_list::iterator it = newMsgCBList.begin();
1048
1049         for (; it != newMsgCBList.end(); it++)
1050         {
1051                 MSG_DEBUG("fd %d dstport %d",it->listenerFd, it->port);
1052
1053                 if ((msgInfo->msgPort.valid == false) && (it->port == 0)) {
1054                         MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
1055                         write(it->listenerFd, pEventData, eventSize);
1056                 } else if ((msgInfo->msgPort.valid == true) && (it->port == msgInfo->msgPort.dstPort)) {
1057                         MSG_DEBUG("Send incoming port msg to listener %d", it->listenerFd);
1058                         write(it->listenerFd, pEventData, eventSize);
1059                 }
1060         }
1061
1062         MSG_END();
1063 }
1064
1065
1066 void MsgTransactionManager::broadcastMMSConfCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo, const MMS_RECV_DATA_S *mmsRecvData)
1067 {
1068         MSG_BEGIN();
1069
1070         char* pEventData = NULL;
1071         AutoPtr<char> eventBuf(&pEventData);
1072
1073         char* encodedData = NULL;
1074         AutoPtr<char> buf(&encodedData);
1075         int dataSize = MsgEncodeMsgInfo(msgInfo, &encodedData);
1076
1077         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_INCOMING_MMS_CONF, err, (void**)(&pEventData));
1078
1079         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
1080
1081         for (; it != newMMSConfMsgCBList.end(); it++)
1082         {
1083                 MSG_DEBUG("fd:%d appId:%s",it->listenerFd, it->appId);
1084
1085                 if (mmsRecvData->msgAppId.valid == true)
1086                 {
1087                         if (!strcmp(it->appId, mmsRecvData->msgAppId.appId))
1088                         {
1089                                 MSG_DEBUG("Send incoming java msg to listener %d", it->listenerFd);
1090                                 write(it->listenerFd, pEventData, eventSize);
1091                         }
1092                 }
1093                 else
1094                 {
1095                         if (strlen(it->appId) <= 0)
1096                         {
1097                                 MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
1098                                 write(it->listenerFd, pEventData, eventSize);
1099                         }
1100                 }
1101         }
1102
1103
1104         MSG_END();
1105 }
1106
1107 void MsgTransactionManager::broadcastPushMsgCB(const msg_error_t err, const MSG_PUSH_MESSAGE_DATA_S *pushData)
1108 {
1109         MSG_BEGIN();
1110
1111         char* pEventData = NULL;
1112         AutoPtr<char> eventBuf(&pEventData);
1113 #ifndef MSG_CONTACTS_SERVICE_NOT_SUPPORTED
1114 #ifdef MSG_PENDING_PUSH_MESSAGE
1115         int bReady = MsgSettingGetInt(VCONFKEY_USER_SERVICE_READY);
1116         if(!bReady)
1117         {
1118                 MSG_PUSH_MESSAGE_DATA_S push_msg;
1119                 memcpy(&push_msg, pushData, sizeof(MSG_PUSH_MESSAGE_DATA_S));
1120                 pushMsgList.push_back(push_msg);
1121                 return;
1122         }
1123 #endif
1124 #endif
1125
1126         int eventSize = MsgMakeEvent(pushData, sizeof(MSG_PUSH_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND, err, (void**)(&pEventData));
1127
1128         pushmsg_list::iterator it = newPushMsgCBList.begin();
1129
1130         for (; it != newPushMsgCBList.end(); it++)
1131         {
1132                 MSG_DEBUG("registered_appid : %s, incoming_appid: %s", it->appId, pushData->pushAppId);
1133                 if (!strcmp(it->appId, pushData->pushAppId))
1134                 {
1135                         MSG_DEBUG("Send incoming Push information to listener %d", it->listenerFd);
1136                         write(it->listenerFd, pEventData, eventSize);
1137                 }
1138         }
1139
1140         MSG_END();
1141 }
1142
1143 void MsgTransactionManager::broadcastCBMsgCB(const msg_error_t err, const MSG_CB_MSG_S *cbMsg)
1144 {
1145         MSG_BEGIN();
1146
1147         char* pEventData = NULL;
1148         AutoPtr<char> eventBuf(&pEventData);
1149
1150         int eventSize = MsgMakeEvent(cbMsg, sizeof(MSG_CB_MSG_S), MSG_EVENT_PLG_INCOMING_CB_MSG_IND, err, (void**)(&pEventData));
1151
1152         cbmsg_list::iterator it = newCBMsgCBList.begin();
1153
1154         for (; it != newCBMsgCBList.end(); it++)
1155         {
1156                 MSG_DEBUG("Send incoming CB information to listener %d", it->listenerFd);
1157                 write(it->listenerFd, pEventData, eventSize);
1158         }
1159
1160         MSG_END();
1161 }
1162
1163 void MsgTransactionManager::broadcastSyncMLMsgCB(const msg_error_t err, const MSG_SYNCML_MESSAGE_DATA_S *syncMLData)
1164 {
1165         MSG_BEGIN();
1166
1167         char* pEventData = NULL;
1168         AutoPtr<char> eventBuf(&pEventData);
1169
1170         int eventSize = MsgMakeEvent(syncMLData, sizeof(MSG_SYNCML_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND, err, (void**)(&pEventData));
1171
1172         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
1173
1174         for (; it != newSyncMLMsgCBList.end(); it++)
1175         {
1176                 MSG_DEBUG("Send incoming SyncML information to listener %d", it->listenerFd);
1177                 write(it->listenerFd, pEventData, eventSize);
1178         }
1179
1180         MSG_END();
1181 }
1182
1183
1184 void MsgTransactionManager::broadcastLBSMsgCB(const msg_error_t err, const MSG_LBS_MESSAGE_DATA_S *lbsData)
1185 {
1186         MSG_BEGIN();
1187
1188 #if 0
1189         char* pEventData = NULL;
1190         AutoPtr<char> eventBuf(&pEventData);
1191
1192         int eventSize = MsgMakeEvent(lbsData, sizeof(MSG_LBS_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_LBS_MSG_IND, err, (void**)(&pEventData));
1193
1194         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
1195
1196         for (; it != newLBSMsgCBList.end(); it++)
1197         {
1198                 MSG_DEBUG("Send incoming LBS msg to listener %d", it->listenerFd);
1199                 write(it->listenerFd, pEventData, eventSize);
1200         }
1201 #else
1202 //      MsgLbsWapPush(lbsData->pushHeader, lbsData->pushBody, lbsData->pushBodyLen);
1203 #endif
1204         MSG_END();
1205 }
1206
1207
1208 void MsgTransactionManager::broadcastSyncMLMsgOperationCB(const msg_error_t err, const int msgId, const int extId)
1209 {
1210         MSG_BEGIN();
1211
1212         char* pEventData = NULL;
1213         AutoPtr<char> eventBuf(&pEventData);
1214
1215         char* encodedData = NULL;
1216         AutoPtr<char> buf(&encodedData);
1217
1218         // Encoding Storage Change Data
1219         int dataSize = MsgEncodeSyncMLOperationData(msgId, extId, &encodedData);
1220
1221         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_SYNCML_OPERATION, err, (void**)(&pEventData));
1222
1223         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
1224
1225         for( ; it != operationSyncMLMsgCBList.end() ; it++ )
1226         {
1227                 MSG_DEBUG("Send SyncML operation to listener %d", it->listenerFd);
1228                 write(it->listenerFd, pEventData, eventSize);
1229         }
1230
1231         MSG_END();
1232 }
1233
1234
1235 void MsgTransactionManager::broadcastStorageChangeCB(const msg_error_t err, const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList)
1236 {
1237         MSG_BEGIN();
1238
1239         if(pMsgIdList == NULL) {
1240                 MSG_DEBUG("pMsgIdList is NULL.");
1241                 return;
1242         }
1243
1244         MSG_DEBUG("storageChangeType [%d]", storageChangeType);
1245
1246         int dataSize = 0;
1247
1248         char* pEventData = NULL;
1249         AutoPtr<char> eventBuf(&pEventData);
1250
1251         char* encodedData = NULL;
1252         AutoPtr<char> buf(&encodedData);
1253
1254         // Encoding Storage Change Data
1255         dataSize = MsgEncodeStorageChangeData(storageChangeType, pMsgIdList, &encodedData);
1256
1257         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_STORAGE_CHANGE_IND, err, (void**)(&pEventData));
1258
1259         fd_map::iterator it = storageChangeFdMap.begin();
1260
1261         for (; it != storageChangeFdMap.end(); it++)
1262         {
1263                 MSG_DEBUG("Send Storage Change Callback to listener %d", it->first);
1264                 write(it->first, pEventData, eventSize);
1265         }
1266
1267         MSG_END();
1268 }
1269
1270
1271 void MsgTransactionManager::broadcastReportMsgCB(const msg_error_t err, const msg_report_type_t reportMsgType, const MSG_MESSAGE_INFO_S *pMsgInfo)
1272 {
1273         MSG_BEGIN();
1274
1275         if(pMsgInfo == NULL) {
1276                 MSG_DEBUG("pMsgInfo is NULL.");
1277                 return;
1278         }
1279
1280         MSG_DEBUG("reportMsgType [%d]", reportMsgType);
1281
1282         int dataSize = 0;
1283
1284         char* pEventData = NULL;
1285         AutoPtr<char> eventBuf(&pEventData);
1286
1287         char* encodedData = NULL;
1288         AutoPtr<char> buf(&encodedData);
1289
1290         // Encoding Storage Change Data
1291         dataSize = MsgEncodeReportMsgData(reportMsgType, pMsgInfo, &encodedData);
1292
1293         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_REPORT_MSG_INCOMING_IND, err, (void**)(&pEventData));
1294
1295         fd_map::iterator it = reportMsgCBFdMap.begin();
1296
1297         for (; it != reportMsgCBFdMap.end(); it++)
1298         {
1299                 MSG_DEBUG("Send Report Message Incoming Callback to listener %d", it->first);
1300                 write(it->first, pEventData, eventSize);
1301         }
1302
1303         MSG_END();
1304 }
1305
1306
1307 void MsgTransactionManager::setTMStatus()
1308 {
1309         MSG_BEGIN();
1310         mx.lock();
1311         cv.signal();
1312         mx.unlock();
1313         MSG_END();
1314 }
1315
1316
1317 void MsgTransactionManager::getTMStatus()
1318 {
1319         MSG_BEGIN();
1320         mx.lock();
1321
1322         int ret = 0;
1323
1324         ret = cv.timedwait(mx.pMutex(), 3);
1325
1326         mx.unlock();
1327
1328         if (ret == ETIMEDOUT)
1329         {
1330                 MSG_DEBUG("MsgTransactionManager::getTMStatus TIME-OUT");
1331         }
1332         MSG_END();
1333 }
1334
1335 #ifdef MSG_PENDING_PUSH_MESSAGE
1336 void MsgTransactionManager::sendPendingPushMsg(void)
1337 {
1338
1339         pushpending_list::iterator pushmsg_it = pushMsgList.begin();
1340         while(pushmsg_it != pushMsgList.end())
1341         {
1342                 MSG_PUSH_MESSAGE_DATA_S msg;
1343                 memset(&msg, 0x00, sizeof(MSG_PUSH_MESSAGE_DATA_S));
1344                 memcpy(msg.pushAppId, pushmsg_it->pushAppId, sizeof(msg.pushAppId));
1345                 memcpy(msg.pushBody, pushmsg_it->pushBody, sizeof(msg.pushBody));
1346                 memcpy(msg.pushContentType, pushmsg_it->pushContentType, sizeof(msg.pushContentType));
1347                 memcpy(msg.pushHeader, pushmsg_it->pushHeader, sizeof(msg.pushHeader));
1348                 msg.pushBodyLen = pushmsg_it->pushBodyLen;
1349                 MsgTransactionManager::instance()->broadcastPushMsgCB(MSG_SUCCESS, (const MSG_PUSH_MESSAGE_DATA_S *)&msg);
1350                 pushmsg_it = pushMsgList.erase(pushmsg_it);
1351         }
1352 }
1353 #endif