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