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