Improve typing of constants data
[platform/core/messaging/msg-service.git] / framework / transaction-manager / MsgTransManager.cpp
1 /*
2  * msg-service
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18 */
19
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24
25 #include <security-server.h>
26
27 #include "MsgDebug.h"
28 #include "MsgMemory.h"
29 #include "MsgException.h"
30 #include "MsgCppTypes.h"
31 #include "MsgContact.h"
32 #include "MsgIpcSocket.h"
33 #include "MsgGconfWrapper.h"
34 #include "MsgUtilFunction.h"
35 #include "MsgCmdHandler.h"
36 #include "MsgSettingHandler.h"
37 #include "MsgStorageHandler.h"
38 #include "MsgPluginManager.h"
39 #include "MsgTransManager.h"
40
41 /*==================================================================================================
42                                      FUNCTION IMPLEMENTATION
43 ==================================================================================================*/
44 void MsgContactChangedCallback()
45 {
46         msg_id_list_s msgIdList;
47         memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
48
49         MsgTransactionManager::instance()->broadcastStorageChangeCB(MSG_SUCCESS, MSG_STORAGE_CHANGE_CONTACT, &msgIdList);
50 }
51
52 /*==================================================================================================
53                                      IMPLEMENTATION OF MsgTransactionManager - Member Functions
54 ==================================================================================================*/
55 MsgTransactionManager* MsgTransactionManager::pInstance = NULL;
56 MsgIpcServerSocket MsgTransactionManager::servSock;
57
58
59 MsgTransactionManager::MsgTransactionManager() : running(false), mx(), cv()
60 {
61         sentMsgMap.clear();
62
63         statusCBFdMap.clear();
64         newMsgCBList.clear();
65         newMMSConfMsgCBList.clear();
66         newSyncMLMsgCBList.clear();
67         newLBSMsgCBList.clear();
68         javaMMSList.clear();
69         operationSyncMLMsgCBList.clear();
70         storageChangeFdMap.clear();
71
72         handlerMap.clear();
73
74 //      Fill in mMsgHandlers, as given in the below.
75         handlerMap[MSG_CMD_ADD_MSG]                             = &MsgAddMessageHandler;
76         handlerMap[MSG_CMD_ADD_SYNCML_MSG]              = &MsgAddSyncMLMessageHandler;
77         handlerMap[MSG_CMD_UPDATE_MSG]                  = &MsgUpdateMessageHandler;
78         handlerMap[MSG_CMD_UPDATE_READ]                         = &MsgUpdateReadStatusHandler;
79         handlerMap[MSG_CMD_UPDATE_PROTECTED]            = &MsgUpdateProtectedStatusHandler;
80         handlerMap[MSG_CMD_DELETE_MSG]                  = &MsgDeleteMessageHandler;
81         handlerMap[MSG_CMD_DELALL_MSGINFOLDER]  = &MsgDeleteAllMessageInFolderHandler;
82         handlerMap[MSG_CMD_MOVE_MSGTOFOLDER]    = &MsgMoveMessageToFolderHandler;
83         handlerMap[MSG_CMD_MOVE_MSGTOSTORAGE]   = &MsgMoveMessageToStorageHandler;
84         handlerMap[MSG_CMD_COUNT_MSG]                   = &MsgCountMessageHandler;
85         handlerMap[MSG_CMD_GET_MSG]                             = &MsgGetMessageHandler;
86         handlerMap[MSG_CMD_GET_FOLDERVIEWLIST]  = &MsgGetFolderViewListHandler;
87
88         handlerMap[MSG_CMD_ADD_FOLDER]                  = &MsgAddFolderHandler;
89         handlerMap[MSG_CMD_UPDATE_FOLDER]               = &MsgUpdateFolderHandler;
90         handlerMap[MSG_CMD_DELETE_FOLDER]               = &MsgDeleteFolderHandler;
91         handlerMap[MSG_CMD_GET_FOLDERLIST]              = &MsgGetFolderListHandler;
92
93         handlerMap[MSG_CMD_ADD_FILTER]                  = &MsgAddFilterHandler;
94         handlerMap[MSG_CMD_UPDATE_FILTER]                       = &MsgUpdateFilterHandler;
95         handlerMap[MSG_CMD_DELETE_FILTER]               = &MsgDeleteFilterHandler;
96         handlerMap[MSG_CMD_GET_FILTERLIST]              = &MsgGetFilterListHandler;
97         handlerMap[MSG_CMD_SET_FILTER_OPERATION]        = &MsgSetFilterOperationHandler;
98         handlerMap[MSG_CMD_GET_FILTER_OPERATION]        = &MsgGetFilterOperationHandler;
99         handlerMap[MSG_CMD_SET_FILTER_ACTIVATION] = &MsgSetFilterActivationHandler;
100
101         handlerMap[MSG_CMD_GET_MSG_TYPE]                        = &MsgGetMsgTypeHandler;
102
103         handlerMap[MSG_CMD_SUBMIT_REQ]                  = &MsgSubmitReqHandler;
104         handlerMap[MSG_CMD_CANCEL_REQ]                  = &MsgCancelReqHandler;
105
106         handlerMap[MSG_CMD_REG_SENT_STATUS_CB]  = &MsgRegSentStatusCallbackHandler;
107         handlerMap[MSG_CMD_REG_STORAGE_CHANGE_CB] = &MsgRegStorageChangeCallbackHandler;
108         handlerMap[MSG_CMD_REG_INCOMING_MSG_CB] = &MsgRegIncomingMsgCallbackHandler;
109         handlerMap[MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB]        = &MsgRegIncomingMMSConfMsgCallbackHandler;
110         handlerMap[MSG_CMD_REG_INCOMING_SYNCML_MSG_CB]  = &MsgRegIncomingSyncMLMsgCallbackHandler;
111         handlerMap[MSG_CMD_REG_INCOMING_PUSH_MSG_CB]    = &MsgRegIncomingPushMsgCallbackHandler;
112         handlerMap[MSG_CMD_REG_INCOMING_CB_MSG_CB]      = &MsgRegIncomingCBMsgCallbackHandler;
113         handlerMap[MSG_CMD_REG_INCOMING_LBS_MSG_CB]     = &MsgRegIncomingLBSMsgCallbackHandler;
114         handlerMap[MSG_CMD_REG_SYNCML_MSG_OPERATION_CB] = &MsgRegSyncMLMsgOperationCallbackHandler;
115
116         handlerMap[MSG_CMD_PLG_SENT_STATUS_CNF] = &MsgSentStatusHandler;
117         handlerMap[MSG_CMD_PLG_STORAGE_CHANGE_IND]      = &MsgStorageChangeHandler;
118         handlerMap[MSG_CMD_PLG_INCOMING_MSG_IND]        = &MsgIncomingMsgHandler;
119         handlerMap[MSG_CMD_PLG_INCOMING_MMS_CONF]       = &MsgIncomingMMSConfMsgHandler;
120         handlerMap[MSG_CMD_PLG_INCOMING_PUSH_IND]       = &MsgIncomingPushMsgHandler;
121         handlerMap[MSG_CMD_PLG_INCOMING_CB_IND] = &MsgIncomingCBMsgHandler;
122
123         handlerMap[MSG_CMD_PLG_INCOMING_SYNCML_IND] = &MsgIncomingSyncMLMsgHandler;
124         handlerMap[MSG_CMD_PLG_INCOMING_LBS_IND] = &MsgIncomingLBSMsgHandler;
125         handlerMap[MSG_CMD_PLG_INIT_SIM_BY_SAT] = &MsgInitSimBySatHandler;
126
127         handlerMap[MSG_CMD_GET_THREADVIEWLIST]  = &MsgGetThreadViewListHandler;
128         handlerMap[MSG_CMD_GET_CONVERSATIONVIEWLIST]    = &MsgGetConversationViewListHandler;
129         handlerMap[MSG_CMD_DELETE_THREADMESSAGELIST]    = &MsgDeleteThreadMessageListHandler;
130
131         handlerMap[MSG_CMD_GET_CONTACT_COUNT]   = &MsgCountMsgByContactHandler;
132         handlerMap[MSG_CMD_GET_QUICKPANEL_DATA]         = &MsgGetQuickPanelDataHandler;
133         handlerMap[MSG_CMD_COUNT_BY_MSGTYPE]    = &MsgCountMsgByTypeHandler;
134         handlerMap[MSG_CMD_RESET_DB]    = &MsgResetDatabaseHandler;
135         handlerMap[MSG_CMD_GET_MEMSIZE]         = &MsgGetMemSizeHandler;
136
137         handlerMap[MSG_CMD_BACKUP_MESSAGE]      = &MsgBackupMessageHandler;
138         handlerMap[MSG_CMD_RESTORE_MESSAGE] = &MsgRestoreMessageHandler;
139
140         handlerMap[MSG_CMD_UPDATE_THREAD_READ] = &MsgUpdateThreadReadStatusHandler;
141
142         handlerMap[MSG_CMD_SYNCML_OPERATION] = &MsgSyncMLMsgOperationHandler;
143         handlerMap[MSG_CMD_GET_REPORT_STATUS]                   = &MsgGetReportStatusHandler;
144
145         handlerMap[MSG_CMD_GET_THREAD_ID_BY_ADDRESS] = &MsgGetThreadIdByAddressHandler;
146         handlerMap[MSG_CMD_GET_THREAD_INFO] = &MsgGetThreadInfoHandler;
147
148         handlerMap[MSG_CMD_GET_SMSC_OPT] = &MsgGetConfigHandler;
149         handlerMap[MSG_CMD_GET_CB_OPT] = &MsgGetConfigHandler;
150         handlerMap[MSG_CMD_GET_SMS_SEND_OPT] = &MsgGetConfigHandler;
151         handlerMap[MSG_CMD_GET_MMS_SEND_OPT] = &MsgGetConfigHandler;
152         handlerMap[MSG_CMD_GET_MMS_RECV_OPT] = &MsgGetConfigHandler;
153         handlerMap[MSG_CMD_GET_PUSH_MSG_OPT] = &MsgGetConfigHandler;
154         handlerMap[MSG_CMD_GET_VOICE_MSG_OPT] = &MsgGetConfigHandler;
155         handlerMap[MSG_CMD_GET_GENERAL_MSG_OPT] = &MsgGetConfigHandler;
156         handlerMap[MSG_CMD_GET_MSG_SIZE_OPT] = &MsgGetConfigHandler;
157
158         handlerMap[MSG_CMD_SET_SMSC_OPT] = &MsgSetConfigHandler;
159         handlerMap[MSG_CMD_SET_CB_OPT] = &MsgSetConfigHandler;
160         handlerMap[MSG_CMD_SET_SMS_SEND_OPT] = &MsgSetConfigHandler;
161         handlerMap[MSG_CMD_SET_MMS_SEND_OPT] = &MsgSetConfigHandler;
162         handlerMap[MSG_CMD_SET_MMS_RECV_OPT] = &MsgSetConfigHandler;
163         handlerMap[MSG_CMD_SET_PUSH_MSG_OPT] = &MsgSetConfigHandler;
164         handlerMap[MSG_CMD_SET_VOICE_MSG_OPT] = &MsgSetConfigHandler;
165         handlerMap[MSG_CMD_SET_GENERAL_MSG_OPT] = &MsgSetConfigHandler;
166         handlerMap[MSG_CMD_SET_MSG_SIZE_OPT] = &MsgSetConfigHandler;
167
168         handlerMap[MSG_CMD_ADD_PUSH_EVENT] = &MsgAddPushEventHandler;
169         handlerMap[MSG_CMD_DELETE_PUSH_EVENT] = &MsgDeletePushEventHandler;
170         handlerMap[MSG_CMD_UPDATE_PUSH_EVENT] = &MsgUpdatePushEventHandler;
171         handlerMap[MSG_CMD_DELETE_MESSAGE_BY_LIST] = &MsgDeleteMessageByListHandler;
172
173         handlerMap[MSG_CMD_CONTACT_SYNC] = &MsgContactSyncEventHandler;
174 }
175
176
177 MsgTransactionManager::~MsgTransactionManager()
178 {
179 //      pthread_cond_init(&retCV, NULL); // = PTHREAD_COND_INITIALIZER;
180
181 }
182
183
184 MsgTransactionManager* MsgTransactionManager::instance()
185 {
186         if (!pInstance)
187                 pInstance = new MsgTransactionManager();
188
189         return pInstance;
190 }
191
192
193 void MsgTransactionManager::run()
194 {
195         servSock.open(MSG_SOCKET_PATH);
196
197         fd_set readfds = servSock.fdSet();
198         int nfds = 0;
199
200         MSG_DEBUG("Start Transaction Manager");
201
202         while(1)
203         {
204                 readfds = servSock.fdSet();
205                 nfds = servSock.maxFd();
206
207                 MSG_DEBUG("Wait For Select() : nfds %d", nfds);
208
209                 // set Status;
210                 setTMStatus();
211
212                 if(select(nfds, &readfds, NULL, NULL, NULL) == -1) {
213                         THROW(MsgException::SELECT_ERROR, "select error : %s", strerror(errno));
214                 }
215
216                 try
217                 {
218                         for (int i=0 ; i < nfds; i++)
219                         {
220                                 if (FD_ISSET(i, &readfds))
221                                 {
222                                         if (i == servSock.fd()) // if it is socket connection request
223                                                 servSock.accept();
224                                         else
225                                                 handleRequest(i);
226                                 }
227                         }
228                 }
229                 catch (MsgException& e)
230                 {
231                         MSG_FATAL("%s", e.what());
232                 }
233                 catch (exception& e)
234                 {
235                         MSG_FATAL("%s", e.what());
236                 }
237
238                 // Release Memory
239                 //MsgReleaseMemory();
240         }
241 }
242
243
244 void MsgTransactionManager::write(int fd, const char* buf, int len)
245 {
246         servSock.write(fd, buf, len);
247 }
248
249
250 void MsgTransactionManager::insertSentMsg(int reqId, MSG_PROXY_INFO_S* pPrxInfo)
251 {
252         if (pPrxInfo == NULL)
253                 THROW(MsgException::SENT_STATUS_ERROR, "Input Parameter is NULL");
254
255         MSG_DEBUG("msg for submit: reqId %d listenerFd %d handleAddr %x", reqId, pPrxInfo->listenerFd, pPrxInfo->handleAddr);
256
257         fd_map::iterator it = statusCBFdMap.find(pPrxInfo->listenerFd);
258
259         if (it == statusCBFdMap.end()) { // if the status CB is not registered
260                 MSG_DEBUG("No sent_status registered for fd %d", pPrxInfo->listenerFd);
261         } else {
262                 sentMsgMap.insert(make_pair(reqId, *pPrxInfo));
263         }
264 }
265
266
267 MSG_PROXY_INFO_S* MsgTransactionManager::getProxyInfo(int reqId)
268 {
269         sentmsg_map::iterator it = sentMsgMap.find(reqId);
270
271         if (it == sentMsgMap.end())
272         {
273                 //THROW(MsgException::SENT_STATUS_ERROR, "No submit request for %d", reqId);
274                 MSG_DEBUG("No sent status cb found (exception: mobile tracker)");
275                 return NULL;
276         }
277
278         return &(it->second);
279 }
280
281
282 void MsgTransactionManager::delProxyInfo(int reqId)
283 {
284         sentmsg_map::iterator it = sentMsgMap.find(reqId);
285
286         if (it == sentMsgMap.end())
287         {
288                 THROW(MsgException::SENT_STATUS_ERROR, "channel info does not exist");
289         }
290
291         sentMsgMap.erase(it);
292 }
293
294
295 void MsgTransactionManager::handleRequest(int fd)
296 {
297         MSG_BEGIN();
298
299         MSG_DEBUG("Event from fd %d", fd);
300
301         char* buf = NULL;
302         AutoPtr<char> wrap(&buf);
303         int len;
304         int ret = servSock.read(fd, &buf, &len);
305
306         if( ret == CLOSE_CONNECTION_BY_SIGNAL || ret == CLOSE_CONNECTION_BY_USER || ret < 0)
307         {
308                 MSG_DEBUG("Read value [%d]", ret);
309                 cleanup(fd);
310                 return;
311         }
312
313         if (len <= 0)
314                 THROW(MsgException::INVALID_RESULT, "read buffer size <= 0");
315
316         char* pEventData = NULL;
317         AutoPtr<char> eventBuf(&pEventData);
318
319         int eventSize = 0;
320
321         // decoding cmd from APP
322         MSG_CMD_S* pCmd = (MSG_CMD_S*) buf;
323         MSG_DEBUG("Command Type [%d : %s]", pCmd->cmdType, MsgDbgCmdStr(pCmd->cmdType));
324
325         if (pCmd->cmdType > MSG_CMD_NUM)
326                 THROW(MsgException::OUT_OF_RANGE, "request CMD is not defined");
327
328         // check privilege
329         if (checkPrivilege(fd, pCmd->cmdType) == false) {
330                 MSG_DEBUG("No Privilege rule. Not allowed.");
331 #ifdef MSG_CHECK_PRIVILEGE
332                 eventSize = sizeof(MSG_EVENT_S);
333
334                 pEventData = new char[eventSize];
335
336                 MSG_EVENT_S* pMsgEvent = (MSG_EVENT_S*)pEventData;
337
338                 pMsgEvent->eventType = pCmd->cmdType;
339                 pMsgEvent->result = MSG_ERR_SECURITY_ERROR;
340
341                 MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
342                 servSock.write(fd, pEventData, eventSize);
343
344                 return;
345 #endif
346         }
347
348         // determine the handler based on pCmd->cmdType
349         int (*pfHandler)(const MSG_CMD_S*, char**) = NULL;
350
351         pfHandler = handlerMap[pCmd->cmdType];
352
353         if (!pfHandler)
354                 THROW(MsgException::INVALID_PARAM, "No handler for %d", pCmd->cmdType);
355
356         // run handler function
357         eventSize = pfHandler(pCmd, &pEventData);
358
359         if (eventSize == 0 || pEventData == NULL)
360                 THROW(MsgException::INVALID_RESULT, "event size = 0 or event data = NULL");
361
362         MSG_DEBUG("Replying to fd [%d], size [%d]", fd, eventSize);
363
364         servSock.write(fd, pEventData, eventSize);
365
366         MSG_END();
367 }
368
369
370 // terminating the socket connection between ipc server and ipc client
371 void MsgTransactionManager::cleanup(int fd)
372 {
373         MSG_BEGIN();
374
375         servSock.close(fd);
376
377         MSG_DEBUG("fd %d disonnected", fd);
378
379         // remove sent msg info for fd
380         sentmsg_map::iterator sentmsg_it = sentMsgMap.begin();
381
382         for (; sentmsg_it != sentMsgMap.end(); sentmsg_it++)
383         {
384                 if (sentmsg_it->second.listenerFd == fd)
385                 {
386                         sentmsg_it->second.listenerFd = 0;
387                         sentmsg_it->second.handleAddr = 0;
388                 }
389         }
390
391         // remove sent status callback for fd
392         statusCBFdMap.erase(fd);
393
394         // remove all newMsgCBs for fd
395         newmsg_list::iterator newmsg_it = newMsgCBList.begin();
396
397         while (newmsg_it != newMsgCBList.end())
398         {
399                 if (newmsg_it->listenerFd == fd)
400                 {
401                         newmsg_it = newMsgCBList.erase(newmsg_it);
402                 }
403                 else
404                 {
405                         ++newmsg_it;
406                 }
407         }
408
409         // remove all newMMSConfMsgCBs for fd
410         mmsconf_list::iterator mmsconf_it = newMMSConfMsgCBList.begin();
411
412         while (mmsconf_it != newMMSConfMsgCBList.end())
413         {
414                 if (mmsconf_it->listenerFd == fd)
415                 {
416                         mmsconf_it = newMMSConfMsgCBList.erase(mmsconf_it);
417                 }
418                 else
419                 {
420                         ++mmsconf_it;
421                 }
422         }
423
424         // remove all newSyncMLMsgCBs for fd
425         syncmlmsg_list::iterator syncmlmsg_it = newSyncMLMsgCBList.begin();
426
427         while (syncmlmsg_it != newSyncMLMsgCBList.end())
428         {
429                 if (syncmlmsg_it->listenerFd == fd)
430                 {
431                         syncmlmsg_it = newSyncMLMsgCBList.erase(syncmlmsg_it);
432                 }
433                 else
434                 {
435                         ++syncmlmsg_it;
436                 }
437         }
438
439         // remove all newLBSMsgCBs for fd
440         lbsmsg_list::iterator lbsmsg_it = newLBSMsgCBList.begin();
441
442         while (lbsmsg_it != newLBSMsgCBList.end())
443         {
444                 if (lbsmsg_it->listenerFd == fd)
445                 {
446                         lbsmsg_it = newLBSMsgCBList.erase(lbsmsg_it);
447                 }
448                 else
449                 {
450                         ++lbsmsg_it;
451                 }
452         }
453
454         // remove all newPushMsgCBs for fd
455         pushmsg_list::iterator pushmsg_it = newPushMsgCBList.begin();
456
457         while (pushmsg_it != newPushMsgCBList.end())
458         {
459                 if (pushmsg_it->listenerFd == fd)
460                 {
461                         pushmsg_it = newPushMsgCBList.erase(pushmsg_it);
462                 }
463                 else
464                 {
465                         ++pushmsg_it;
466                 }
467         }
468
469         // remove all newCBMsgCBs for fd
470         cbmsg_list::iterator cbmsg_it = newCBMsgCBList.begin();
471         bool bSave = false;
472
473         while (cbmsg_it != newCBMsgCBList.end())
474         {
475                 if (cbmsg_it->listenerFd == fd)
476                 {
477                         cbmsg_it = newCBMsgCBList.erase(cbmsg_it);
478                 }
479                 else
480                 {
481                         if(cbmsg_it->bsave == true)
482                                 bSave = true;
483                         ++cbmsg_it;
484                 }
485         }
486
487         // remove all operationSyncMLMsgCBs for fd
488         syncmlop_list::iterator syncmlop_it = operationSyncMLMsgCBList.begin();
489
490         while (syncmlop_it != operationSyncMLMsgCBList.end())
491         {
492                 if (syncmlop_it->listenerFd == fd)
493                 {
494                         syncmlop_it = operationSyncMLMsgCBList.erase(syncmlop_it);
495                 }
496                 else
497                 {
498                         ++syncmlop_it;
499                 }
500         }
501
502         // remove storage change callback for fd
503         storageChangeFdMap.erase(fd);
504
505         MSG_END();
506 }
507
508
509 bool MsgTransactionManager::checkPrivilege(int fd, MSG_CMD_TYPE_T CmdType)
510 {
511         bool bAllowed = true;
512         switch(CmdType)
513         {
514         case MSG_CMD_GET_MSG:
515         case MSG_CMD_COUNT_MSG:
516         case MSG_CMD_COUNT_BY_MSGTYPE:
517         {
518                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::read", "rw");
519                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
520                         MSG_DEBUG("No msg-service::read rw rule.");
521                         bAllowed = false;
522                 }
523         }
524         break;
525         case MSG_CMD_OPEN_HANDLE:
526         case MSG_CMD_SUBMIT_REQ:
527         case MSG_CMD_SET_CB_OPT:
528         case MSG_CMD_ADD_PUSH_EVENT:
529         case MSG_CMD_DELETE_PUSH_EVENT:
530         {
531                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::write", "rw");
532                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
533                         MSG_DEBUG("No msg-service::write rw rule.");
534                         bAllowed = false;
535                 }
536         }
537         break;
538         case MSG_CMD_REG_INCOMING_MSG_CB:
539         {
540                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::smstrigger", "rw");
541                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
542                         MSG_DEBUG("No msg-service::smstrigger rw rule.");
543                         bAllowed = false;
544                 }
545         }
546         break;
547         case MSG_CMD_REG_INCOMING_CB_MSG_CB:
548         {
549                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::cellbroadcast", "rw");
550                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
551                         MSG_DEBUG("No msg-service::cellbroadcast rw rule.");
552                         bAllowed = false;
553                 }
554         }
555         break;
556         case MSG_CMD_REG_INCOMING_PUSH_MSG_CB:
557         {
558                 int ret = security_server_check_privilege_by_sockfd(fd, "msg-service::wappush", "rw");
559                 if (ret == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
560                         MSG_DEBUG("No msg-service::wappush rw rule.");
561                         bAllowed = false;
562                 }
563         }
564         break;
565         }
566
567         return bAllowed;
568 }
569
570
571 void MsgTransactionManager::setSentStatusCB(int listenerFd)
572 {
573         if (listenerFd <= 0)
574                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d",listenerFd);
575
576         statusCBFdMap[listenerFd] = true;
577 }
578
579
580 void MsgTransactionManager::setIncomingMsgCB(MSG_CMD_REG_INCOMING_MSG_CB_S *pCbInfo)
581 {
582         if (!pCbInfo)
583         {
584                 MSG_FATAL("cbinfo NULL");
585                 return;
586         }
587
588         newmsg_list::iterator it = newMsgCBList.begin();
589
590         for (; it != newMsgCBList.end(); it++)
591         {
592                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (it->port == pCbInfo->port))
593                 {
594                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d, port %d", it->listenerFd, it->msgType, it->port);
595                         return;
596                 }
597         }
598
599         newMsgCBList.push_back(*pCbInfo);
600 }
601
602
603 void MsgTransactionManager::setMMSConfMsgCB(MSG_CMD_REG_INCOMING_MMS_CONF_MSG_CB_S *pCbInfo)
604 {
605         if (!pCbInfo)
606         {
607                 MSG_FATAL("cbinfo NULL");
608                 return;
609         }
610
611         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
612
613         for (; it != newMMSConfMsgCBList.end(); it++)
614         {
615                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType) && (!strncmp(it->appId, pCbInfo->appId, MAX_MMS_JAVA_APPID_LEN)))
616                 {
617                         MSG_DEBUG("Duplicated MMSConfMessageCB info fd:%d, mType:%d, appId:%s", it->listenerFd, it->msgType, it->appId);
618                         return;
619                 }
620         }
621
622         newMMSConfMsgCBList.push_back(*pCbInfo);
623 }
624
625
626 void MsgTransactionManager::setPushMsgCB(MSG_CMD_REG_INCOMING_PUSH_MSG_CB_S *pCbInfo)
627 {
628         if (!pCbInfo)
629         {
630                 MSG_FATAL("cbinfo NULL");
631                 return;
632         }
633
634         pushmsg_list::iterator it = newPushMsgCBList.begin();
635
636         for (; it != newPushMsgCBList.end(); it++)
637         {
638                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
639                 {
640                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
641                         return;
642                 }
643         }
644
645         newPushMsgCBList.push_back(*pCbInfo);
646 }
647
648 void MsgTransactionManager::setCBMsgCB(MSG_CMD_REG_INCOMING_CB_MSG_CB_S *pCbInfo)
649 {
650         if (!pCbInfo)
651         {
652                 MSG_FATAL("cbinfo NULL");
653                 return;
654         }
655
656         cbmsg_list::iterator it = newCBMsgCBList.begin();
657
658         for (; it != newCBMsgCBList.end(); it++)
659         {
660                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
661                 {
662                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
663                         return;
664                 }
665         }
666
667         if(pCbInfo->bsave)
668                 if(MsgSettingSetBool(CB_SAVE, pCbInfo->bsave) != MSG_SUCCESS)
669                         MSG_DEBUG("MsgSettingSetBool FAIL: CB_SAVE");
670
671
672         newCBMsgCBList.push_back(*pCbInfo);
673 }
674
675 void MsgTransactionManager::setSyncMLMsgCB(MSG_CMD_REG_INCOMING_SYNCML_MSG_CB_S *pCbInfo)
676 {
677         if (!pCbInfo)
678         {
679                 MSG_FATAL("cbinfo NULL");
680                 return;
681         }
682
683         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
684
685         for (; it != newSyncMLMsgCBList.end(); it++)
686         {
687                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
688                 {
689                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
690                         return;
691                 }
692         }
693
694         newSyncMLMsgCBList.push_back(*pCbInfo);
695 }
696
697
698 void MsgTransactionManager::setLBSMsgCB(MSG_CMD_REG_INCOMING_LBS_MSG_CB_S *pCbInfo)
699 {
700         if (!pCbInfo)
701         {
702                 MSG_FATAL("cbinfo NULL");
703                 return;
704         }
705
706         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
707
708         for (; it != newLBSMsgCBList.end(); it++)
709         {
710                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
711                 {
712                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
713                         return;
714                 }
715         }
716
717         newLBSMsgCBList.push_back(*pCbInfo);
718 }
719
720
721 void MsgTransactionManager::setJavaMMSList(MSG_CMD_REG_INCOMING_JAVAMMS_TRID_S *pTrId)
722 {
723         if (!pTrId)
724         {
725                 MSG_FATAL("trId NULL");
726                 return;
727         }
728
729         javamms_list::iterator it;
730
731         for (it = javaMMSList.begin(); it != javaMMSList.end(); it++)
732         {
733                 if (!strcmp(it->id, pTrId->id))
734                 {
735                         MSG_DEBUG("Duplicated javaMMS transaction Id:%s", it->id);
736                         return;
737                 }
738         }
739
740         javaMMSList.push_back(*pTrId);
741 }
742
743
744 void MsgTransactionManager::setSyncMLMsgOperationCB(MSG_CMD_REG_SYNCML_MSG_OPERATION_CB_S *pCbInfo)
745 {
746         if (!pCbInfo)
747         {
748                 MSG_FATAL("cbinfo NULL");
749                 return;
750         }
751
752         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
753
754         for (; it != operationSyncMLMsgCBList.end(); it++)
755         {
756                 if ((it->listenerFd == pCbInfo->listenerFd) && (it->msgType == pCbInfo->msgType))
757                 {
758                         MSG_DEBUG("Duplicated messageCB info fd %d, mType %d", it->listenerFd, it->msgType);
759                         return;
760                 }
761         }
762
763         operationSyncMLMsgCBList.push_back(*pCbInfo);
764 }
765
766
767 void MsgTransactionManager::setStorageChangeCB(int listenerFd)
768 {
769         if (listenerFd <= 0)
770                 THROW(MsgException::INVALID_PARAM,"InParam Error: listenerFd %d", listenerFd);
771
772         storageChangeFdMap[listenerFd] = true;
773 }
774
775
776 javamms_list& MsgTransactionManager::getJavaMMSList()
777 {
778         return javaMMSList;
779 }
780
781
782 void MsgTransactionManager::broadcastIncomingMsgCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo)
783 {
784         MSG_BEGIN();
785
786         char* pEventData = NULL;
787         AutoPtr<char> eventBuf(&pEventData);
788
789         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MSG_IND, err, (void**)(&pEventData));
790
791         MSG_DEBUG("valid %d dstport %d", msgInfo->msgPort.valid, msgInfo->msgPort.dstPort);
792
793         newmsg_list::iterator it = newMsgCBList.begin();
794
795         for (; it != newMsgCBList.end(); it++)
796         {
797                 MSG_DEBUG("fd %d dstport %d",it->listenerFd, it->port);
798
799                 if ((msgInfo->msgPort.valid == false) && (it->port == 0))
800                 {
801                         MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
802                         write(it->listenerFd, pEventData, eventSize);
803                 }
804                 else if ((msgInfo->msgPort.valid == true) && (it->port == msgInfo->msgPort.dstPort))
805                 {
806                         MSG_DEBUG("Send incoming port msg to listener %d", it->listenerFd);
807                         write(it->listenerFd, pEventData, eventSize);
808                 }
809         }
810
811         MSG_END();
812 }
813
814
815 void MsgTransactionManager::broadcastMMSConfCB(const msg_error_t err, const MSG_MESSAGE_INFO_S *msgInfo, const MMS_RECV_DATA_S *mmsRecvData)
816 {
817         MSG_BEGIN();
818
819         char* pEventData = NULL;
820         AutoPtr<char> eventBuf(&pEventData);
821
822         int eventSize = MsgMakeEvent(msgInfo, sizeof(MSG_MESSAGE_INFO_S), MSG_EVENT_PLG_INCOMING_MMS_CONF, err, (void**)(&pEventData));
823
824         mmsconf_list::iterator it = newMMSConfMsgCBList.begin();
825
826         for (; it != newMMSConfMsgCBList.end(); it++)
827         {
828                 MSG_DEBUG("fd:%d appId:%s",it->listenerFd, it->appId);
829
830                 if (mmsRecvData->msgAppId.valid == true)
831                 {
832                         if (!strcmp(it->appId, mmsRecvData->msgAppId.appId))
833                         {
834                                 MSG_DEBUG("Send incoming java msg to listener %d", it->listenerFd);
835                                 write(it->listenerFd, pEventData, eventSize);
836                         }
837                 }
838                 else
839                 {
840                         if (strlen(it->appId) <= 0)
841                         {
842                                 MSG_DEBUG("Send incoming normal msg to listener %d", it->listenerFd);
843                                 write(it->listenerFd, pEventData, eventSize);
844                         }
845                 }
846         }
847
848
849         MSG_END();
850 }
851
852 void MsgTransactionManager::broadcastPushMsgCB(const msg_error_t err, const MSG_PUSH_MESSAGE_DATA_S *pushData)
853 {
854         MSG_BEGIN();
855
856         char* pEventData = NULL;
857         AutoPtr<char> eventBuf(&pEventData);
858
859         int eventSize = MsgMakeEvent(pushData, sizeof(MSG_PUSH_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND, err, (void**)(&pEventData));
860
861         pushmsg_list::iterator it = newPushMsgCBList.begin();
862
863         for (; it != newPushMsgCBList.end(); it++)
864         {
865                 if (!strcmp(it->appId, pushData->pushAppId))
866                 {
867                         MSG_DEBUG("Send incoming Push information to listener %d", it->listenerFd);
868                         write(it->listenerFd, pEventData, eventSize);
869                 }
870         }
871
872         MSG_END();
873 }
874
875 void MsgTransactionManager::broadcastCBMsgCB(const msg_error_t err, const MSG_CB_MSG_S *cbMsg)
876 {
877         MSG_BEGIN();
878
879         char* pEventData = NULL;
880         AutoPtr<char> eventBuf(&pEventData);
881
882         int eventSize = MsgMakeEvent(cbMsg, sizeof(MSG_CB_MSG_S), MSG_EVENT_PLG_INCOMING_CB_MSG_IND, err, (void**)(&pEventData));
883
884         cbmsg_list::iterator it = newCBMsgCBList.begin();
885
886         for (; it != newCBMsgCBList.end(); it++)
887         {
888                 MSG_DEBUG("Send incoming CB information to listener %d", it->listenerFd);
889                 write(it->listenerFd, pEventData, eventSize);
890         }
891
892         MSG_END();
893 }
894
895 void MsgTransactionManager::broadcastSyncMLMsgCB(const msg_error_t err, const MSG_SYNCML_MESSAGE_DATA_S *syncMLData)
896 {
897         MSG_BEGIN();
898
899         char* pEventData = NULL;
900         AutoPtr<char> eventBuf(&pEventData);
901
902         int eventSize = MsgMakeEvent(syncMLData, sizeof(MSG_SYNCML_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND, err, (void**)(&pEventData));
903
904         syncmlmsg_list::iterator it = newSyncMLMsgCBList.begin();
905
906         for (; it != newSyncMLMsgCBList.end(); it++)
907         {
908                 MSG_DEBUG("Send incoming SyncML information to listener %d", it->listenerFd);
909                 write(it->listenerFd, pEventData, eventSize);
910         }
911
912         MSG_END();
913 }
914
915
916 void MsgTransactionManager::broadcastLBSMsgCB(const msg_error_t err, const MSG_LBS_MESSAGE_DATA_S *lbsData)
917 {
918         MSG_BEGIN();
919
920         char* pEventData = NULL;
921         AutoPtr<char> eventBuf(&pEventData);
922
923         int eventSize = MsgMakeEvent(lbsData, sizeof(MSG_LBS_MESSAGE_DATA_S), MSG_EVENT_PLG_INCOMING_LBS_MSG_IND, err, (void**)(&pEventData));
924
925         lbsmsg_list::iterator it = newLBSMsgCBList.begin();
926
927         for (; it != newLBSMsgCBList.end(); it++)
928         {
929                 MSG_DEBUG("Send incoming LBS msg to listener %d", it->listenerFd);
930                 write(it->listenerFd, pEventData, eventSize);
931         }
932
933         MSG_END();
934 }
935
936
937 void MsgTransactionManager::broadcastSyncMLMsgOperationCB(const msg_error_t err, const int msgId, const int extId)
938 {
939         MSG_BEGIN();
940
941         char* pEventData = NULL;
942         AutoPtr<char> eventBuf(&pEventData);
943
944         char* encodedData = NULL;
945         AutoPtr<char> buf(&encodedData);
946
947         // Encoding Storage Change Data
948         int dataSize = MsgEncodeSyncMLOperationData(msgId, extId, &encodedData);
949
950         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_SYNCML_OPERATION, err, (void**)(&pEventData));
951
952         syncmlop_list::iterator it = operationSyncMLMsgCBList.begin();
953
954         for( ; it != operationSyncMLMsgCBList.end() ; it++ )
955         {
956                 MSG_DEBUG("Send SyncML operation to listener %d", it->listenerFd);
957                 write(it->listenerFd, pEventData, eventSize);
958         }
959
960         MSG_END();
961 }
962
963
964 void MsgTransactionManager::broadcastStorageChangeCB(const msg_error_t err, const msg_storage_change_type_t storageChangeType, const msg_id_list_s *pMsgIdList)
965 {
966         MSG_BEGIN();
967
968         if(pMsgIdList == NULL) {
969                 MSG_DEBUG("pMsgIdList is NULL.");
970                 return;
971         }
972
973         MSG_DEBUG("storageChangeType [%d]", storageChangeType);
974
975         int dataSize = 0;
976
977         char* pEventData = NULL;
978         AutoPtr<char> eventBuf(&pEventData);
979
980         char* encodedData = NULL;
981         AutoPtr<char> buf(&encodedData);
982
983         // Encoding Storage Change Data
984         dataSize = MsgEncodeStorageChangeData(storageChangeType, pMsgIdList, &encodedData);
985
986         int eventSize = MsgMakeEvent(encodedData, dataSize, MSG_EVENT_PLG_STORAGE_CHANGE_IND, err, (void**)(&pEventData));
987
988         fd_map::iterator it = storageChangeFdMap.begin();
989
990         for (; it != storageChangeFdMap.end(); it++)
991         {
992                 MSG_DEBUG("Send Storage Change Callback to listener %d", it->first);
993                 write(it->first, pEventData, eventSize);
994         }
995
996         MSG_END();
997 }
998
999
1000 void MsgTransactionManager::setTMStatus()
1001 {
1002         MSG_BEGIN();
1003         mx.lock();
1004         cv.signal();
1005         mx.unlock();
1006         MSG_END();
1007 }
1008
1009
1010 void MsgTransactionManager::getTMStatus()
1011 {
1012         MSG_BEGIN();
1013         mx.lock();
1014
1015         int ret = 0;
1016
1017         ret = cv.timedwait(mx.pMutex(), 3);
1018
1019         mx.unlock();
1020
1021         if (ret == ETIMEDOUT)
1022         {
1023                 MSG_DEBUG("MsgTransactionManager::getTMStatus TIME-OUT");
1024         }
1025         MSG_END();
1026 }